Debian Codegraph @

nspatch

raw
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: 0BSD OR MIT-0
3
4 use warnings;
5 use strict;
6
7 use File::Temp qw(tempfile);
8 use Getopt::Std;
9 use POSIX;
10
11 sub wail { warn "nspatch: @_\n"; }
12 sub fail { die "nspatch: @_\n"; }
13 sub fale { die "nspatch: @_: $!\n"; }
14
15 sub version {
16 while (<DATA>) {
17 print if m{^=head1 VERSION} ... m{^=head1 }
18 and not m{^=head1 };
19 }
20 exit;
21 }
22
23 sub usage {
24 print STDERR <<EOF;
25 usage: nspatch [patch-opts] -- [diff-opts] -- [update-opts]
26 Runs `nsdiff [diff-opts] | nsupdate [update-opts]`.
27 Output is suppressed unless there is an error. The script
28 retries if it fails because of a concurrent update.
29 nspatch options:
30 -h display full documentation
31 -V display version information
32 -r count retry count (default 2)
33 -v turn on verbose output
34 EOF
35 exit 1;
36 }
37 my %opt;
38 usage unless getopts '-hVrv', \%opt;
39 version if $opt{V};
40 exec "perldoc -oterm -F $0" if $opt{h};
41 $opt{r} //= 2;
42 usage unless $opt{r} =~ m{^\d+$};
43 usage unless 1 == grep m{^--$}, @ARGV;
44
45 my @nsdiff = ('nsdiff', '-v', $opt{v} ? 'qr' : '');
46 while (@ARGV) {
47 my $arg = shift;
48 last if $arg eq '--';
49 push @nsdiff, $arg;
50 }
51 my @nsupdate = ('nsupdate', @ARGV);
52
53 sub slurp ($) {
54 my $f = shift;
55 open my $h, '<', $f or fale "open $f";
56 undef $/;
57 return <$h>;
58 }
59 sub dupout ($$) {
60 no strict 'refs';
61 my ($dst,$src) = @_;
62 open $dst, '>&', $src or fale 'dup';
63 }
64 sub mktmp {
65 return tempfile('nspatch.XXXXXXXXXX',
66 TMPDIR => 1, UNLINK => 1);
67 }
68 sub tmpf {
69 my $dup = shift;
70 my ($fh,$name) = mktmp;
71 dupout $dup, $fh;
72 return $name;
73 }
74
75 dupout 'XSTDOUT', 'STDOUT';
76 dupout 'XSTDERR', 'STDERR';
77 sub runtmp {
78 wail "running @_" if $opt{v};
79 my $out = tmpf 'STDOUT';
80 my $err = tmpf 'STDERR';
81 my $x = system @_;
82 dupout 'STDOUT', 'XSTDOUT';
83 dupout 'STDERR', 'XSTDERR';
84 return ($out, $err, $x);
85 }
86
87 my ($dashh,$dash) = mktmp;
88 print $dashh "----\n";
89 close $dashh;
90
91 RETRY: for (;;) {
92 my ($diffout,$differr,$diffx) = runtmp @nsdiff;
93 system 'cat', $dash, $diffout, $dash, $differr, $dash
94 if $opt{v} or ($diffx != 0 && $diffx != 256);
95 exit 0 if $diffx == 0;
96 fail "bad exit status from @nsdiff" if $diffx != 256;
97 open STDIN, '<', $diffout or fale 'open';
98 my ($upout,$uperr,$upx) = runtmp @nsupdate;
99 system 'cat', $dash, $upout, $dash, $uperr, $dash if $opt{v};
100 exit 0 if $upx == 0;
101 if (slurp($uperr) eq "update failed: NXRRSET\n" and $opt{r}--) {
102 wail "trying again" if $opt{v};
103 next RETRY;
104 } else {
105 system 'cat', $dash, $diffout, $dash, $differr,
106 $dash, $upout, $dash, $uperr, $dash unless $opt{v};
107 fail "bad exit status from @nsupdate";
108 }
109 }
110
111 __END__
112
113 =head1 NAME
114
115 nspatch - run `nsdiff | nsupdate` with error handling
116
117 =head1 SYNOPSIS
118
119 nspatch [B<-hVv>] [B<-r> I<count>]
120 -- [nsdiff options] -- [nsupdate options]
121
122 =head1 DESCRIPTION
123
124 The B<nspatch> utility runs `C<nsdiff | nsupdate>` and checks that
125 both programs complete successfully. It suppresses their output unless
126 there is an error, in a manner suitable for running from B<cron>.
127
128 The B<nsupdate> script produced by B<nsdiff> includes a prerequisite
129 check to detect and fail if there is a concurrent update. These
130 failures are detected by B<nspatch> which retries the update.
131
132 Rather than using a pipe, B<nspatch> uses temporary files to store the
133 output of B<nsdiff> and B<nsupdate>.
134
135 =head1 OPTIONS
136
137 =over
138
139 =item B<-h>
140
141 Display this documentation.
142
143 =item B<-V>
144
145 Display version information.
146
147 =item B<-r> I<count>
148
149 If the update fails because of a concurrent update, B<nspatch> will
150 retry up to I<count> times. The default retry I<count> is 2.
151
152 =item B<-v>
153
154 Turn on verbose mode, so the output from B<nsdiff> and B<nsupdate> is
155 printed even if they are successful. (By default it is suppressed.)
156
157 The verbose option is passed on to B<nsdiff>. If B<nspatch> is not
158 given the B<-v> option, it passes the B<-v ''> option to B<nsdiff>. If
159 B<nspatch> is given the B<-v> option, it passes the B<-v 'qr'> option
160 to B<nsdiff>.
161
162 =back
163
164 =head1 EXIT STATUS
165
166 The B<nspatch> utility returns 0 if no change is required or if the
167 update is successful, or 1 if there is an error.
168
169 =head1 ENVIRONMENT
170
171 =over
172
173 =item C<TMPDIR>
174
175 Location for temporary files.
176
177 =back
178
179 =head1 VERSION
180
181 This is nspatch-1.85 <https://dotat.at/prog/nsdiff/>
182
183 Written by Tony Finch <[email protected]> <[email protected]>
184 at Cambridge University Information Services.
185 You may do anything with this. It has no warranty.
186
187 =head1 SEE ALSO
188
189 nsdiff(1), nsupdate(1), cron(8)
190
191 =cut