-
Notifications
You must be signed in to change notification settings - Fork 0
/
frak
executable file
·1189 lines (1040 loc) · 41.2 KB
/
frak
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
our $VERSION = '2.6 (2024-07-25)';
#
# frak -- Show changes between two AFS trees.
#
# Perhaps the single most useful AFS utility if you use replicated volumes
# regularly. Compares two AFS trees to each other, generally the read/write
# and read-only versions of the same volume, and presents a readable summary
# of the differences so that you can see exactly what will be changed when the
# volume is released.
#
# "Don't be a _little_ paranoid; worry about everything, or let it all go."
# -- James Alan Gardner, _Trapped_
#
# Written by Neil Crellin <[email protected]>
# Updated by Russ Allbery <[email protected]>
# Copyright 1998, 1999, 2004, 2011, 2013
# The Board of Trustees of the Leland Stanford Junior University
#
# Updated by Bill MacAllister <[email protected]>
# Copyright 2018
# Bill MacAllister <[email protected]>
#
# This program is free software; you may redistribute it and/or modify it
# under the same terms as Perl itself.
##############################################################################
# Modules and declarations
##############################################################################
use 5.006;
use strict;
use warnings;
use vars qw($CHANGEDIR $CROSSMOUNT $DEBUG $LOGFILE $MUNGE $NODIFF $QUIET
$ROOTRO $ROOTRW);
use Cwd qw(cwd);
use File::Find qw(find);
use File::stat qw(lstat);
use Getopt::Long qw(GetOptions);
use POSIX qw(strftime);
use Stat::lsMode qw(format_mode);
##############################################################################
# Site configuration
##############################################################################
# The default limit on the size of a diff in lines that will be included.
our $FRAK_MAXDIFF = 200;
# The full path to a suitable diff that supports -u.
our $DIFF = 'diff';
# The full path to fs and vos. vos may be in an sbin directory, which may
# not be on the user's path by default, so check there first.
our $FS = 'fs';
our $VOS = grep { -x $_ } qw(/usr/local/sbin/vos /usr/sbin/vos);
$VOS ||= 'vos';
# Load the configuration file if it exists.
if (-f '/etc/afs-admin-tools/config') {
require '/etc/afs-admin-tools/config';
}
##############################################################################
# Utility functions
##############################################################################
# Quote an output string so that it will be safely and correctly parsed by
# bundle. Passes through anything that looks like a variable reference to
# $ORIG or $DEST at the beginning of the string, however (be careful of this).
sub bundle_quote ( @ ) {
my @strings = @_;
for (@strings) {
s/([\\\"\'\$\s])/\\$1/g;
s/(\\\n)/\'$1\'/g;
s/^\\\$(ORIG|DEST)/\$$1/;
}
return join(' ', @strings);
}
# Given the stat information for a file, return all of the various bundle
# variable settings to recreate that.
sub bundle_mode ( $ ) {
my ($stat) = @_;
my @vars = ('owner=' . $stat->uid, 'group=' . $stat->gid);
push(@vars, sprintf('mode=%lo', $stat->mode & 07777));
push(@vars, 'atime=' . $stat->atime, 'mtime=' . $stat->mtime);
return @vars;
}
# Return a file name with the variable substitutions done for bundle.
sub bundle_name ( $$$ ) {
my ($name, $base, $var) = @_;
$name =~ s(^\Q$base\E) (\$$var);
return $name;
}
# Return the fs command, as a list, to set the ACL on the given file to the
# given ACL string (in the compressed form that is returned by getacl).
sub bundle_setacl ( $$ ) {
my ($name, $acl) = @_;
my @acl = split(/=,/, $acl);
unshift(@acl, $FS, 'setacl', $name);
my $negative;
@acl = map {
if (!/^--/ || $negative) {
$_;
} else {
('-negative', $_);
}
} @acl;
push(@acl, '-clear');
return @acl;
}
##############################################################################
# Output functions
##############################################################################
# Print a header using equal signs.
sub header ( $ ) {
my ($header) = @_;
my $space = length($header) + 4;
my $lead = (74 - $space) / 2;
$header = ' ' . $header . ' ';
my $out = '=' x $lead . $header . '=' x (74 - $lead - $space) . "\n";
if (!$QUIET) {
print $out;
}
if ($LOGFILE) {
print LOG $out;
}
}
# Add a file to the changed area. Takes the name of the file and the prefix
# (new or old), and returns the file name in the changes area for use with
# later revert or apply directives.
sub changes ( $$ ) {
my ($file, $type) = @_;
if (!$CHANGEDIR) {
return;
}
my $new = $file;
$new =~ s(^\$(?:ORIG|DEST)) ($type)
or die "$0: failed to build file name for $file\n";
print CHANGES bundle_quote('file', $file, $new), "\n";
return $new;
}
# Add something to the revert bundle if appropriate.
sub revert ( @ ) {
my (@action) = @_;
if (!$CHANGEDIR) {
return;
}
print REVERT bundle_quote(@action), "\n";
}
# Add something to the apply bundle if appropriate.
sub apply ( @ ) {
my (@action) = @_;
if (!$CHANGEDIR) {
return;
}
print APPLY bundle_quote(@action), "\n";
}
# Return the ls-style file listing for a particular file. This doesn't do any
# particularly fancy formatting at the moment, although it certainly could.
# Takes the stat object for the file and a flag that, if set, suppresses the
# modification time display.
sub lslout ( $;$ ) {
my ($stat, $nodate) = @_;
if (!$stat) {
return "---------- NO SUCH FILE????";
}
my $modestr = format_mode($stat->mode);
my $username = getpwuid($stat->uid) || $stat->uid;
my $groupname = getgrgid($stat->gid) || $stat->gid;
my $lastchanged = strftime('%Y-%m-%d %T', localtime $stat->mtime);
my $output = sprintf("%s %4d %-8s %-8s %8d",
$modestr, $stat->nlink, $username, $groupname, $stat->size);
if (!$nodate) {
$output .= " $lastchanged";
}
return $output;
}
# Print the name of a file. This will eventually do some programmatic munging
# of the file name on the way out the door. Takes a prefix, the file name,
# and then a suffix if any.
sub print_name ( $$;$ ) {
my ($prefix, $name, $suffix) = @_;
if (defined $suffix) {
$suffix = ' ' . $suffix;
} else {
$suffix = '';
}
if ($MUNGE) {
$name =~ s/^$ROOTRW/.../;
}
if (!$QUIET) {
print "$prefix: $name$suffix\n";
}
if ($LOGFILE) {
print LOG "$prefix: $name$suffix\n";
}
}
# Print the ls-style file listing for a file, the symlink value, or the mount
# point value. Takes the prefix and the info hash, and an optional flag
# saying whether to display the directory listing even if this is a mount
# point.
sub print_ls ( $$;$ ) {
my ($prefix, $info, $forcels) = @_;
my $output;
if ($$info{islink}) {
$output = "-> $$info{link}";
} elsif (defined $$info{volume} && !$forcels) {
$output = "-> #$$info{volume}";
} else {
$output = lslout($$info{stat});
}
$output = ' ' . ($prefix ? $prefix . ': ' : '') . $output;
if (!$QUIET) {
print $output, "\n";
}
if ($LOGFILE) {
print LOG $output, "\n";
}
}
# Print an ACL list, taking a prefix.
sub print_acl ( $$ ) {
my ($prefix, $acl) = @_;
my $output = ' ACL ' . $prefix . ': ' . $acl . "\n";
if (!$QUIET) {
print $output;
}
if ($LOGFILE) {
print LOG $output;
}
}
# Run diff on two files if we're configured to do so and they're both text
# files. Print the diff output if it's under the configured limit; otherwise
# print a message saying the output has been suppressed.
sub print_diff ( $$ ) {
my ($old, $new) = @_;
if ($NODIFF) {
return '';
}
if (!(-T $old && -T $new)) {
return '';
}
# Fork diff carefully.
my $pid = open(DIFF, '-|');
if (!defined $pid) {
die "$0: cannot fork: $!\n";
} elsif ($pid == 0) {
open(STDERR, '>&STDOUT') or die "$0: cannot dup stdout: $!\n";
exec($DIFF, '-u', $old, $new)
or die "$0: cannot exec $DIFF -u $old $new: $!\n";
}
# Gather the output.
local $_;
my (@diff, $diff);
while (<DIFF>) {
if ($. <= 2) {
s{(\s)(?:\Q$ROOTRO\E|\Q$ROOTRW\E)} {$1...};
}
if ($. <= $FRAK_MAXDIFF) {
push(@diff, $_);
}
}
my $output;
if ($. > $FRAK_MAXDIFF) {
$output = " LARGE diff output suppressed, $. lines\n";
} else {
$output = join('', @diff);
}
close DIFF;
if (!$QUIET) {
print $output;
}
if ($LOGFILE) {
print LOG $output;
}
}
##############################################################################
# AFS information
##############################################################################
# Given a mount point, get the volume name of the volume mounted there or
# undef if it is not a mount point.
sub lsmount ( $ ) {
my ($path) = @_;
my $pid = open(LSMOUNT, '-|');
if (!defined $pid) {
die "$0: cannot fork: $!\n";
} elsif ($pid == 0) {
open(STDERR, '>&STDOUT') or die "$0: cannot dup stdout: $!\n";
exec($FS, 'lsmount', $path)
or die "$0: cannot exec $FS lsmount for $path: $!\n";
}
local $/;
my $output = <LSMOUNT>;
close LSMOUNT;
if ($DEBUG) {
print map { "===> $_ ($?)\n" } split(/\n/, $output);
}
if ($? != 0) {
return;
}
my ($name)
= ($output =~ /^\S+ is a mount point for volume \'[%\#](\S+)\'$/);
return $name;
}
# Get the ACL for a directory, returning the ACL as a dense, comma-separated
# string. Map full privileges to "all" and prefix negative rights with --
# (which may be theoretically ambiguous, but shouldn't be a problem in
# practice). Returns NOT_IN_AFS if the path is apparently not in AFS.
sub getacl ( $ ) {
my ($dir) = @_;
# Fork off fs listacl carefully.
my $pid = open(LISTACL, '-|');
if (!defined $pid) {
die "$0: cannot fork: $!\n";
} elsif ($pid == 0) {
open(STDERR, '>&STDOUT') or die "$0: cannot dup stdout: $!\n";
exec($FS, 'listacl', $dir)
or die "$0: cannot exec $FS listacl for $dir: $!\n";
}
# Read the results. The first line is either a header or an error
# message; if it's an error message, check to see if it says that the path
# isn't in AFS and return NOT_IN_AFS in that situation. Otherwise, look
# for the "Normal rights:" and "Negative rights:" headers. Assume that
# all negative rights come after all normal rights.
local $_;
my $error = <LISTACL>;
if ($error =~ /it is possible that .* is not in AFS/) {
return 'NOT_IN_AFS';
}
my ($acl, $prefix) = ('', '');
while (<LISTACL>) {
if (/Normal rights:/) {
next;
}
if (/Negative rights:/) {
$prefix = '--';
next;
}
my ($pts, $perms) = split;
if ($perms eq 'rlidwka') {
$perms = 'all';
}
$acl .= $prefix . $pts . '=' . $perms . ',';
}
close LISTACL;
if ($? != 0) {
warn $error;
die "$0: $FS listacl failed for $dir with status ", ($? >> 8), "\n";
}
$acl =~ s/,$//;
return $acl;
}
##############################################################################
# Analysis
##############################################################################
# Gather information about a file and return it as a hash. The keys of the
# hash are set as follows: stat gets a File::stat object for it, islink and
# isdir are booleans indicating whether the file is a link or a directory,
# link holds the value of the link if it is a link, volume holds the volume it
# is a mount point for if it is a mount point and undef otherwise, and acl
# holds the ACL for the directory if it is a directory.
sub examine ( $ ) {
my ($file) = @_;
my %info;
$info{name} = $file;
$info{stat} = lstat $file;
if ($info{stat}) {
$info{islink} = -l _;
$info{isdir} = -d _;
if ($info{islink}) {
$info{link} = readlink $file;
}
if ($info{isdir}) {
$info{volume} = lsmount $file;
my $isroot = ($file eq $ROOTRW || $file eq $ROOTRO);
if (!defined $info{volume} || $CROSSMOUNT || $isroot) {
$info{acl} = getacl $file;
}
}
}
return %info;
}
# Print out the appropriate information for something brand new that has
# appeared (no corresponding file in the read-only path).
sub compare_new ( $ ) {
if ($DEBUG) {
print "==> Comparing new file\n";
}
my %info = %{ $_[0] };
if ($info{islink}) {
print_name('New link', $info{name});
revert('delete', $info{bname});
apply('link', $info{link}, $info{bname});
} elsif (defined $info{volume}) {
print_name('New mountpoint', $info{name}, "-> #$info{volume}");
revert('system', $FS, 'rmmount', $info{bname});
apply('system', $FS, 'mkmount', $info{bname}, $info{volume});
} elsif ($info{isdir}) {
print_name('New directory', $info{name});
revert('system', '/bin/rm', '-rf', $info{bname});
apply('dir', $info{bname}, bundle_mode($info{stat}));
apply('system', bundle_setacl($info{bname}, $info{acl}));
} else {
print_name('New', $info{name});
my $saved = changes($info{bname}, 'new');
revert('delete', $info{bname});
apply('file', $saved, $info{bname});
}
if (!defined $info{volume}) {
print_ls('', \%info);
}
if ($info{isdir} && $info{acl}) {
print_acl('is', $info{acl});
}
}
# Print out the appropriate information when the read/write volume has a link.
# Takes the information for both the read/write and read-only versions.
sub compare_link ( $$ ) {
if ($DEBUG) {
print "==> Comparing link\n";
}
my %rwinfo = %{ $_[0] };
my %roinfo = %{ $_[1] };
if ($roinfo{islink}) {
if ($rwinfo{link} eq $roinfo{link}) {
return;
}
print_name('Link change', $rwinfo{name});
revert('link', $roinfo{link}, $rwinfo{bname});
apply('link', $rwinfo{link}, $rwinfo{bname});
} elsif (defined $roinfo{volume}) {
print_name('Mountpoint replaced by link', $rwinfo{name});
revert('delete', $rwinfo{bname});
revert('system', $FS, 'mkmount', $rwinfo{bname}, $roinfo{volume});
apply('system', $FS, 'rmmount', $rwinfo{bname});
apply('link', $rwinfo{link}, $rwinfo{bname});
} elsif ($roinfo{isdir}) {
print_name('Directory replaced by link', $rwinfo{name});
revert('delete', $rwinfo{bname});
revert('dir', $rwinfo{bname}, bundle_mode($roinfo{stat}));
apply('system', '/bin/rm', '-rf', $rwinfo{bname});
apply('link', $rwinfo{link}, $rwinfo{bname});
} else {
print_name('Non-link replaced by link', $rwinfo{name});
my $saved = changes($roinfo{bname}, 'old');
revert('file', $saved, $rwinfo{bname});
apply('link', $rwinfo{link}, $rwinfo{bname});
}
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
}
# Print out the appropriate information when the read/write volume has a mount
# point. Takes the information for both the read/write and read-only
# versions.
sub compare_mount ( $$ ) {
if ($DEBUG) {
print "==> Comparing mountpoint\n";
}
my %rwinfo = %{ $_[0] };
my %roinfo = %{ $_[1] };
if ($roinfo{islink}) {
print_name('Link replaced by mountpoint', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
revert('system', $FS, 'rmmount', $rwinfo{bname});
revert('link', $roinfo{link}, $rwinfo{bname});
apply('delete', $rwinfo{bname});
apply('system', $FS, 'mkmount', $rwinfo{bname}, $rwinfo{volume});
} elsif (defined $roinfo{volume}) {
if ($rwinfo{volume} ne $roinfo{volume} && $rwinfo{name} ne $ROOTRW) {
print_name('Mountpoint change', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
revert('system', $FS, 'rmmount', $rwinfo{bname});
revert('system', $FS, 'mkmount', $rwinfo{bname}, $roinfo{volume});
apply('system', $FS, 'rmmount', $rwinfo{bname});
apply('system', $FS, 'mkmount', $rwinfo{bname}, $rwinfo{volume});
} elsif ($rwinfo{name} eq $ROOTRW || $CROSSMOUNT) {
if (!$rwinfo{stat}) {
return;
}
my $old = lslout($roinfo{stat}, 1);
my $new = lslout($rwinfo{stat}, 1);
if ($new ne $old) {
print_name('Changed directory', $rwinfo{name});
print_ls('WAS', \%roinfo, 1);
print_ls('NOW', \%rwinfo, 1);
revert('dir', $rwinfo{bname}, bundle_mode($roinfo{stat}));
apply('dir', $rwinfo{bname}, bundle_mode($rwinfo{stat}));
}
if ($rwinfo{acl} eq $roinfo{acl}) {
return;
}
print_name('ACL changed', $rwinfo{name});
print_acl('WAS', $roinfo{acl});
print_acl('NOW', $rwinfo{acl});
revert('system', bundle_setacl($rwinfo{bname}, $roinfo{acl}));
apply('system', bundle_setacl($rwinfo{bname}, $rwinfo{acl}));
}
} elsif ($roinfo{isdir}) {
print_name('Directory replacd by mountpoint', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
revert('system', $FS, 'rmmount', $rwinfo{bname});
revert('dir', $rwinfo{bname}, bundle_mode($roinfo{stat}));
apply('system', '/bin/rm', '-rf', $rwinfo{bname});
apply('system', $FS, 'mkmount', $rwinfo{bname}, $rwinfo{volume});
} else {
print_name('File replaced by mountpoint', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
my $saved = changes($roinfo{bname}, 'old');
revert('system', $FS, 'rmmount', $rwinfo{bname});
revert('file', $saved, $rwinfo{bname});
apply('delete', $rwinfo{bname});
apply('system', $FS, 'mkmount', $rwinfo{bname}, $rwinfo{volume});
}
}
# Print out the appropriate information when the read/write volume has a
# directory. Takes the information for both the read/write and read-only
# versions.
sub compare_dir ( $$ ) {
if ($DEBUG) {
print "==> Comparing directory\n";
}
my %rwinfo = %{ $_[0] };
my %roinfo = %{ $_[1] };
if ($roinfo{islink}) {
print_name('Link replaced by directory', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
revert('system', '/bin/rm', '-rf', $rwinfo{bname});
revert('link', $roinfo{link}, $rwinfo{bname});
apply('delete', $rwinfo{bname});
apply('dir', $rwinfo{bname}, bundle_mode($rwinfo{stat}));
} elsif (defined $roinfo{volume}) {
print_name('Mountpoint replaced by directory', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
revert('system', '/bin/rm', '-rf', $rwinfo{bname});
revert('system', $FS, 'mkmount', $rwinfo{bname}, $roinfo{volume});
apply('system', $FS, 'rmmount', $rwinfo{bname});
apply('dir', $rwinfo{bname}, bundle_mode($rwinfo{stat}));
} elsif ($roinfo{isdir}) {
my $old = lslout($roinfo{stat}, 1);
my $new = lslout($rwinfo{stat}, 1);
if ($new ne $old) {
print_name('Changed directory', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
revert('dir', $rwinfo{bname}, bundle_mode($roinfo{stat}));
apply('dir', $rwinfo{bname}, bundle_mode($rwinfo{stat}));
}
if ($rwinfo{acl} ne $roinfo{acl}) {
print_name('ACL changed', $rwinfo{name});
print_acl('WAS', $roinfo{acl});
print_acl('NOW', $rwinfo{acl});
revert('system', bundle_setacl($rwinfo{bname}, $roinfo{acl}));
apply('system', bundle_setacl($rwinfo{bname}, $rwinfo{acl}));
}
} else {
print_name('File replaced by directory', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
my $saved = changes($roinfo{bname}, 'old');
revert('system', '/bin/rm', '-rf', $rwinfo{bname});
revert('file', $saved, $rwinfo{bname});
apply('delete', $rwinfo{bname});
apply('dir', $rwinfo{bname}, bundle_mode($rwinfo{stat}));
}
}
# Print out the appropriate information when the read/write volume has a
# regular file. Takes the information for both the read/write and read-only
# versions.
sub compare_file ( $$ ) {
if ($DEBUG) {
print "==> Comparing file\n";
}
my %rwinfo = %{ $_[0] };
my %roinfo = %{ $_[1] };
if ($roinfo{islink}) {
print_name('Link replaced by non-link', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
my $saved = changes($rwinfo{bname}, 'new');
revert('link', $roinfo{link}, $rwinfo{bname});
apply('file', $saved, $rwinfo{bname});
} elsif (defined $roinfo{volume}) {
print_name('Mountpoint replaced by file', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
my $saved = changes($rwinfo{bname}, 'new');
revert('delete', $rwinfo{bname});
revert('system', $FS, 'mkmount', $rwinfo{bname}, $roinfo{volume});
apply('system', $FS, 'rmmount', $rwinfo{bname});
apply('file', $saved, $rwinfo{bname});
} elsif ($roinfo{isdir}) {
print_name('Directory replaced by file', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
my $saved = changes($rwinfo{bname}, 'new');
revert('delete', $rwinfo{bname});
revert('dir', $rwinfo{bname}, bundle_mode($roinfo{stat}));
apply('system', '/bin/rm', '-rf', $rwinfo{bname});
apply('file', $saved, $rwinfo{bname});
} else {
my $newls = lslout($rwinfo{stat});
my $oldls = lslout($roinfo{stat});
if (!($newls ne $oldls)) {
return;
}
print_name('Changed', $rwinfo{name});
print_ls('WAS', \%roinfo);
print_ls('NOW', \%rwinfo);
print_diff($roinfo{name}, $rwinfo{name});
my $old = changes($roinfo{bname}, 'old');
my $new = changes($rwinfo{bname}, 'new');
revert('file', $old, $rwinfo{bname});
apply('file', $new, $rwinfo{bname});
}
}
# Compare in the forward direction (read/write to read-only). This will pick
# up file creations and changes, but will not pick up file deletions. This
# function is called for each file found in the read/write volume.
sub analyze_forward {
if ($DEBUG) {
print "=> Inspecting $File::Find::name\n";
}
my $rw = $File::Find::name;
my $ro = $rw;
$ro =~ s(^\Q$ROOTRW\E)($ROOTRO);
my %rwinfo = examine($rw);
my %roinfo = examine($ro);
$rwinfo{bname} = bundle_name($rwinfo{name}, $ROOTRW, 'DEST');
$roinfo{bname} = bundle_name($roinfo{name}, $ROOTRO, 'ORIG');
# If the stat failed, that probably means that we have a mount point to a
# volume that doesn't exist. Handle that case specially.
if (!$rwinfo{stat}) {
$rwinfo{volume} = lsmount $rw;
if (!defined $rwinfo{volume}) {
return;
}
}
# If this was a mount point, prune here.
$File::Find::prune = 1
if ($rw ne $ROOTRW && $rwinfo{volume} && !$CROSSMOUNT);
$File::Find::prune = 1
if ($ro ne $ROOTRO && $roinfo{volume} && !$CROSSMOUNT);
# Do the analysis, or rather call all the individual functions that do the
# analysis.
if (!$roinfo{stat}) { compare_new(\%rwinfo) }
elsif ($rwinfo{islink}) { compare_link(\%rwinfo, \%roinfo) }
elsif (defined $rwinfo{volume}) { compare_mount(\%rwinfo, \%roinfo) }
elsif ($rwinfo{isdir}) { compare_dir(\%rwinfo, \%roinfo) }
else { compare_file(\%rwinfo, \%roinfo) }
}
# Compare in the reverse direction (read-only to read/write). The only thing
# we pick up here are deletions, so we ignore any case where the object exists
# in the read/write volume (since we would have reported on it during the
# forward pass). This function is called for each file found in the read-only
# volume.
sub analyze_reverse {
if ($DEBUG) {
print "=> Inspecting $File::Find::name\n";
}
my $ro = $File::Find::name;
my $rw = $ro;
$rw =~ s(^\Q$ROOTRO\E)($ROOTRW);
my %rwinfo = examine($rw);
# If this was a mount point, prune here. Note that we prune based on the
# read/write path because we want to be able to avoid the stat of the
# read-only side if the read/write side exists. Because of the forward
# pass, this won't cause us to miss anything significant.
if ($rw ne $ROOTRW && $rwinfo{volume} && !$CROSSMOUNT) {
$File::Find::prune = 1;
}
if ($rwinfo{stat}) {
return;
}
# Now check the read-only side. We also prune if the read-only side is a
# mount point, of course.
my %roinfo = examine($ro);
$File::Find::prune = 1
if ($ro ne $ROOTRO && $roinfo{volume} && !$CROSSMOUNT);
$rwinfo{bname} = bundle_name($rwinfo{name}, $ROOTRW, 'DEST');
$roinfo{bname} = bundle_name($roinfo{name}, $ROOTRO, 'ORIG');
# If stat failed, that probably means that we have a mount point to a
# volume that no longer exists. Handle that case as a special situation.
if (!$roinfo{stat}) {
$roinfo{volume} = lsmount $ro;
if (!defined $roinfo{volume}) {
return;
}
}
# Analyze the difference and report appropriately.
if ($roinfo{islink}) {
print_name('Deleted link', $rwinfo{name});
print_ls('', \%roinfo);
revert('link', $roinfo{link}, $rwinfo{bname});
apply('delete', $rwinfo{bname});
} elsif (defined $roinfo{volume}) {
print_name('Deleted mountpoint', $rwinfo{name}, "-> #$roinfo{volume}");
revert('system', $FS, 'mkmount', $rwinfo{bname}, $roinfo{volume});
apply('system', $FS, 'rmmount', $rwinfo{bname});
} elsif ($roinfo{isdir}) {
print_name('Deleted directory', $rwinfo{name});
revert('dir', $rwinfo{bname}, bundle_mode($roinfo{stat}));
apply('system', '/bin/rm', '-rf', $rwinfo{bname});
} else {
print_name('Deleted', $rwinfo{name});
my $saved = changes($roinfo{bname}, 'old');
revert('file', $saved, $rwinfo{bname});
apply('delete', $rwinfo{bname});
}
}
##############################################################################
# Main routine
##############################################################################
# Trim extraneous garbage from the path.
my $fullpath = $0;
$0 =~ s%.*/%%;
# Make sure we get output in the right order.
$| = 1;
# Parse our options.
my ($help, $logfile, $nolog, $version);
Getopt::Long::config('bundling');
GetOptions(
'cross-mounts|C' => \$CROSSMOUNT,
'change-dir|c=s' => \$CHANGEDIR,
'debug|D' => \$DEBUG,
'max-diff-lines|d=i' => \$FRAK_MAXDIFF,
'help|h' => \$help,
'no-log|L' => \$nolog,
'log-file|l=s' => \$logfile,
'munge|m' => \$MUNGE,
'quiet|q' => \$QUIET,
'suppress-diff|s' => \$NODIFF,
'version|v' => \$version
) or exit 1;
if ($help) {
exec('perldoc', '-t', $fullpath);
exit 1;
} elsif ($version) {
print "frak $VERSION\n";
exit 1;
}
if ($logfile) {
$LOGFILE = $logfile;
}
# Check the directories we're given. If the -m option is used, we should
# instead have a single argument naming the volume and will be mounting it in
# the current directory.
if (@ARGV > 2 || @ARGV < 1) {
die "Usage: $0 [-CDhLmqsv] [-c <change>] [-d <n>] [-l <log>] "
. "<rw> [<ro>]\n";
}
my ($rw, $ro, $volume, $mounted);
if (@ARGV == 1 && $ARGV[0] !~ m%/%) {
$MUNGE = 1;
$volume = $ARGV[0];
$volume =~ s/\.readonly$//;
my $rovolume = $volume . '.readonly';
$rw = "frak-$volume";
$ro = "frak-$rovolume";
system($FS, 'mkmount', $ro, $rovolume) == 0
or die "$0: failed to create mount for $rovolume\n";
system($FS, 'mkmount', '-rw', $rw, $volume) == 0
or die "$0: failed to create mount for $volume\n";
$mounted = 1;
if (!($nolog || $logfile)) {
$LOGFILE = $volume;
}
} else {
($rw, $ro) = @ARGV;
if (@ARGV == 1 && $rw =~ m%/afs/[^.]%) {
warn "$0: only argument specifies a read-only path, correcting\n";
$rw =~ s%/afs/%/afs/.%;
}
if (!$ro) {
$ro = $rw;
$ro =~ s%/afs/\.%/afs/%;
if ($rw eq $ro) {
die "$0: cannot intuit read-only path from read/write path\n";
}
}
$volume = lsmount($rw);
if (!defined $volume) {
warn "$0: $rw is not a mountpoint for its volume\n";
}
}
# Get rid of any trailing slashes.
s%/+$%% for ($rw, $ro);
# Check the validity of the read/write directory.
if (!-d $rw) {
die "$0: read/write path $rw not found or not a directory\n";
}
# Set the global variables.
($ROOTRW, $ROOTRO) = ($rw, $ro);
# If a changedir is requested, set up the file handles and files.
if ($CHANGEDIR) {
open(CHANGES, "> $CHANGEDIR/changes.b")
or die "$0: cannot create $CHANGEDIR/changes.b: $!\n";
open(REVERT, "> $CHANGEDIR/revert.b")
or die "$0: cannot create $CHANGEDIR/revert.b: $!\n";
open(APPLY, "> $CHANGEDIR/apply.b")
or die "$0: cannot create $CHANGEDIR/apply.b: $!\n";
mkdir("$CHANGEDIR/old", 0755)
or die "$0: cannot create $CHANGEDIR/old: $!\n";
mkdir("$CHANGEDIR/new", 0755)
or die "$0: cannot create $CHANGEDIR/new: $!\n";
print CHANGES "ORIG=", bundle_quote($ROOTRO), "\n";
print CHANGES "DEST=", bundle_quote($ROOTRW), "\n";
print REVERT "ORIG=", bundle_quote($ROOTRO), "\n";
print REVERT "DEST=", bundle_quote($ROOTRW), "\n";
print APPLY "ORIG=", bundle_quote($ROOTRO), "\n";
print APPLY "DEST=", bundle_quote($ROOTRW), "\n";
}
# If a log is requested, create the log file.
if ($LOGFILE) {
open(LOG, "> $LOGFILE")
or die "$0: cannot create log file $LOGFILE: $!\n";
}
# Print the header if we have a volume.
if (defined $volume) {
header($volume);
}
# Do the actual work.
$File::Find::dont_use_nlink = 1;
find({ wanted => \&analyze_forward, no_chdir => 1 }, $ROOTRW);
if (!$QUIET) {
print "\n";
}
if ($LOGFILE) {
print LOG "\n";
}
find({ wanted => \&analyze_reverse, no_chdir => 1 }, $ROOTRO);
if (!$QUIET) {
print "\n";
}
if ($LOGFILE) {
print LOG "\n";
}
# Remove the mount points if we created them. Try to do this even if we
# exited on error.
END {
if ($mounted) {
system($FS, 'rmmount', "frak-$volume.readonly") == 0
or warn "$0: cannot remove ro mount point for volume\n";
system($FS, 'rmmount', "frak-$volume") == 0
or warn "$0: cannot remove rw mount point for volume\n";
}
}
__END__
############################################################################
# Documentation
############################################################################
=for stopwords
non-AFS AFS -CDhLmqsv Crellin afs-admin-tools changedir frak fs --munge
ro-path rw-path vos Battlestar Galactica --change-dir
=head1 NAME
frak - Show changes between two AFS trees
=head1 SYNOPSIS
frak [B<-CDhLmqsv>] [B<-c> I<changedir>] [B<-d> I<max-diff-lines>] [B<-l>
I<logfile>] (I<volume> | I<rw-path> [ I<ro-path> ])
=head1 DESCRIPTION
B<frak> is a tool for comparing the structure and contents of two
directory trees in AFS. Its most common use is to determine the changes
in a read/write AFS volume relative to the read-only copy of the same
volume, to ensure that it's safe to release, but it can be used to compare
any two arbitrary AFS trees. It can even be used in a limited fashion to
compare non-AFS trees, although in that case C<diff -r> may be more
appropriate.
B<frak> understands mount points and directory ACLs and will detect
changes in those as well as more typical changes in file size,
permissions, or existence. It also knows not to cross mount points
(unless given the B<-C> option). Note that two files with the same
permissions and the same size will be considered identical; the file is
not actually compared with diff if its other information matches.
If two files are different and are determined to be text files (using the
C<-T> probe in Perl; see L<perlfunc> for more information), C<diff -u>
will be run on the two files. This output will be included in the B<frak>
output provided it's less than 100 lines long (controllable with B<-d>).
Otherwise, just the length of the diff in lines will be given. Diffs can
be suppressed completely with B<-s>.
The paths to compare may be specified in three ways. A volume name can be
given as the sole argument (distinguished from a path by the fact that it
doesn't contain a C</>). In this case, the read-only and read-write
versions of that volume will be mounted in the current working directory
(so the current working directory must be in AFS), as F<frak-I<volume>>
and F<frak-I<volume>.readonly>, and then compared. A path may be given as
the sole argument, in which case it is taken to be the path to the
read-write version of the tree and should begin with C</afs/.>. It will
be compared against the read-only path to the same tree, formed by
removing the period after C</afs/>. Or, finally, two paths may be given,
and they will be taken to be the read-write path (the newer tree) and the
read-only path (the older tree) respectively. Please note that this is
the exact opposite order from what one would pass to B<diff>.
If a volume is specified, B<frak> will by default log the output to a file
named after the volume in the current directory, as well as printing the
output to standard output. To suppress the file log, use B<-L>. To
suppress the output to standard out, use B<-q>. The name and location of
the log file can be changed with B<-l>. If a path is specified, B<frak>
will by default only print to standard output. To also log to a file,
specify a log file location with B<-l>. In either case, the log file will
be overwritten if it already exists.
If a volume is specified, paths in the output will be shown relative to
the root of the volume. If a path is specified, paths in the output will
be complete paths unless B<-m> is given. If B<-m> is given, paths will be
relative to the root of the tree specified by the path given on the
command line.
The name B<frak> comes from the swear word used in I<Battlestar
Galactica>, which should give you some ideas about the origin of this
program.
=head1 OPTIONS
=over 4
=item B<-C>, B<--cross-mounts>
Normally, B<frak> will never cross a mount point. If this option is
given, it will keep comparing through mount points. Be careful when using
this option, since B<frak> will happily recurse through all AFS file
systems in the world, and remember that circular path structures are
possible in AFS. B<frak> does no checking to make sure that it doesn't
revisit the same volume endlessly.
=item B<-c> I<changedir>, B<--change-dir>=I<changedir>
When given this option, B<frak> creates three bundles I<changedir>, as
well as directories named F<new> and F<old>. The first bundle is named
F<changes.b>, which if run will populate the F<new> and F<old> directories
with all of the files that have changed between the two trees. The second