File: sysconftool

package info (click to toggle)
courier 0.73.1-1.6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 45,472 kB
  • ctags: 11,257
  • sloc: ansic: 146,531; cpp: 24,330; sh: 15,964; perl: 4,558; makefile: 3,305; sed: 16
file content (375 lines) | stat: -rwxr-xr-x 6,627 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/perl
# Copyright 2000 Double Precision, Inc.  See COPYING for
# distribution information.
 
use IO::File;
use Getopt::Long;

my $exitcode=0;

my $ver;
my $noclobber;
my $force;
my $require;

my $myversion="0.17";

exit 1 unless GetOptions("v" => \$ver, "n" => \$noclobber,
			 "f" => \$force, "r=s" => \$require);

print "$myversion\n" if $ver;

die "$0: Version not supported.\n"
    if $require && versioncmp($myversion, $require) < 0;

while ($#ARGV >= 0)
{
    my $filename=shift @ARGV;

    $filename =~ s/\.dist$//;

    my $rc;

    eval {
	$rc=sysconftool($filename, $noclobber, $force);
    } ;

    if ($@)
    {
	$rc=9;

	$@ .= "\n" unless $@ =~ /\n/s;
	print "$@";
    }

    $exitcode=$rc if $rc > $exitcode;
}

exit ($exitcode);

sub sysconftool {
    my $filename=shift;
    my $noclobber=shift;
    my $force=shift;

    my $distfile=new IO::File;

    die "$filename.dist: $!\n" if ! $distfile->open("< $filename.dist");

    my ($distheader, $distver);

    ($distheader, $distver)= sysconftool_readver($distfile);

    die "$filename.dist: configuration header not found.\n" unless $distver;

    my $oldfile=new IO::File;

    if ( ! $oldfile->open($filename))
    {
	$oldfile=undef;
    }
    else
    {
	my ($dummy, $configver);

	($dummy, $configver)= sysconftool_readver($oldfile);

	if (! defined $dummy)
	{
	    $oldfile=undef; # Legacy config file
	}
	elsif ($configver eq $distver)
	{
	    return 0 unless $force;
	}
    }

    my %old_settings;
    my %old_version;

    # If there's an old file, read old settings.

    if (defined $oldfile)
    {
	my $configname="";
	my $configversion="";
	my $line;
	my $resetflag=0;

	while (defined ($line=<$oldfile>))
	{
	    if ($line =~ /^\#/)
	    {
		$configname=$configversion="" if $resetflag;
		$resetflag=0;

		if ($line =~ /^\#\#NAME:(.*):(.*)/)
		{
		    ($configname, $configversion)=($1, $2);
		    
		    $configname =~ s/[ \t]//g;
		    $configversion =~ s/[ \t]//g;

		    $old_version{$configname}=$configversion;
		}
	    }
	    else
	    {
		$resetflag=1;
		$old_settings{$configname} .= $line
		    if $configname;
	    }
	}
	$oldfile=undef;
    }

    my $newfile=new IO::File;

    die "$filename.new: $!\n"
	if ! $newfile->open($noclobber ? ">/dev/null":">$filename.new");

    eval {
	{
	    my $f=$filename;

	    $f =~ s:^.*/([^/]*)$:$1:;

	    print $f . ":\n";
	}

	# Try to carry over ownership and perms

	my @inode=stat($distfile);

	die $! unless $#inode > 0;

	if (! $noclobber)
	{
	    chown $inode[4], $inode[5], "$filename.new";
	    chmod $inode[2], "$filename.new";
	}

	(print $newfile $distheader) || die $!;

	sysconftool_writeout($newfile, $distfile, \%old_settings,
			     \%old_version, "$filename.dist");
    } ;

    if ($@)
    {
	$newfile=undef;
	unlink "$filename.new";
	die "$filename.new: $@";
    }

    $newfile=undef;

    rename "$filename", "$filename.bak" unless $noclobber;
    rename "$filename.new", "$filename" unless $noclobber;
    return 0;
}

# Read the version header from the file.

sub sysconftool_readver {
    my $fh=shift;

    my $header;
    my $cnt;

    for (;;)
    {
	my $line=<$fh>;

	last if ! defined $line || ++$cnt > 20;

	$header .= $line;

	return ($header, $line) if $line =~ /^\#\#VERSION:/;
    }

    return undef;
}

#
# Read the dist file, write out the config file, and try to piece it back
# from the old config file.

sub sysconftool_writeout {
    my $newfile=shift;
    my $oldfile=shift;
    my $old_settings=shift;
    my $old_version=shift;
    my $filename=shift;

    my $line;

    my $prefix_comment=0;
    my $old_setting="";

    my $last_setting=undef;
    my $prev_setting=undef;

    while (defined($line=<$oldfile>))
    {
	if (! ($line =~ /^\#/))
	{
	    if ($prev_setting)
	    {
		# Before the first line of a new configuration setting
		# print the obsoleted config setting (commented out).

		(print $newfile $prev_setting) || die $!;
		$prev_setting=undef;
	    }
	    if ($prefix_comment > 0)
	    {
		# Keeping old config setting, comment out the new dist
		# setting.

		if ($prefix_comment < 2)
		{
		    $prefix_comment=2;
		    (print $newfile "#\n# DEFAULT SETTING from $filename:\n") || die $!;
		}
		$line = "#$line";
	    }
	}
	elsif ($line =~ /^\#\#NAME:(.*):(.*)/)
	{
	    ($configname, $configversion)=($1, $2);

	    $configname =~ s/[ \t]//g;
	    $configversion =~ s/[ \t]//g;

	    $prefix_comment=0;

	    if (defined $last_setting)
	    {
		# Write out old config setting before we go to the next
		# setting in the dist file.

		(print $newfile $last_setting) || die $!;
		$last_setting=undef;
	    }

	    if ( defined $$old_settings{$configname})
	    {
		if ($$old_version{$configname} eq $configversion)
		{
		    # Setting didn't change in the dist file, keep
		    # current settings.

		    print "  $configname: unchanged\n";
		    $prefix_comment=1;
		    $last_setting=$$old_settings{$configname};
		}
		else
		{
		    # Must install updated setting.  Carefully comment
		    # out the current setting.

		    print "  $configname: UPDATED\n";

		    my @lines=
			split (/\n/s,"$$old_settings{$configname}\n");

		    push @lines, "" if $#lines < 0;

		    grep (s/^/\# /, @lines);

		    $prev_setting= "#\n# Previous setting (inserted by sysconftool):\n#\n" .
			join("\n", @lines) . "\n#\n";
		}
	    }
	    else
	    {
		print "  $configname: new\n";
	    }
	}

	(print $newfile $line) || die $!;
    }

    # Write out any pending settings.

    if (defined $last_setting)
    {
	(print $newfile $last_setting) || die $!;
	$last_setting=undef;
    }

    if ($prev_setting)
    {
	(print $newfile $prev_setting) || die $!;
    }
}

#######

# Not everyone has Sort::Version, so we roll our own here.  It's not that bad.

sub versioncmp {
    my @a=split (/\./, shift);
    my @b=split (/\./, shift);

    for (;;)
    {
	my $a=shift @a;
	my $b=shift @b;

	last if (! defined $a) && (! defined $b);

	return -1 if ! defined $a;
	return 1 if ! defined $b;

	my @ap=versionsplitclass($a);
	my @bp=versionsplitclass($b);

	for (;;)
	{
	    my $a=shift @ap;
	    my $b=shift @bp;

	    last if (! defined $a) && (! defined $b);

	    return -1 if ! defined $a;
	    return 1 if ! defined $b;

	    my $n;

	    if ( $a =~ /[0-9]/)
	    {
		$n= $a <=> $b;
	    }
	    else
	    {
		$n= $a cmp $b;
	    }

	    return $n if $n;
	}
    }
    return 0;
}

sub versionsplitclass {
    my $v=shift;
    my @a;

    while ( $v ne "")
    {
	if ($v =~ /^([0-9]+)(.*)/)
	{
	    push @a, $1;
	    $v=$2;
	}
	else
	{
	    die unless $v =~ /^([^0-9]+)(.*)/;
	    push @a, $1;
	    $v=$2;
	}
    }
    return @a;
}