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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved
*
* module:
* anal.c
*
* purpose:
* routines to analyze the file trees and figure out what has changed
* and queue files for reconciliation. It also contains tree enumeration
* routines to for other purposes (pruning and link location).
*
* contents:
*
* change analysis:
* analyze .... (top level) analyze all files in the tree for changes
* summary .... print out change/reconciliation statistics for each base
* check_file . (static) look for changes and queue file for reconciliation
* check_changes (static) figure out if a particular file has changed
* queue_file . (static) add a file to the reconciliation list
*
* other tree enumeration functions:
* prune_file . (static) recursive descent and actual pruning
* prune ...... (top level) initiate pruning analysis for nonexistant files
* find_link .. look for other files to which a file may be a link
* link_update. propagate changed stat info to all other links
* same_name .. (static) figure out if two nodes describe same file
*
* misc:
* push_name .. maintain a running full pathname as we descend
* pop_name ... maintain a running full pathname as we pop back
* get_name ... return full pathname for the current file
*
* notes:
* analysis is limited to files that were evaluated in the previous
* pass ... since we don't have complete information about files that
* were not evaluated in the previous pass.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "messages.h"
#include "filesync.h"
#include "database.h"
#include "debug.h"
/*
* routines:
*/
void push_name(const char *);
void pop_name();
char *get_name(struct file *);
static errmask_t check_file(struct file *fp);
static diffmask_t check_changes(struct file *fp, int first, int second);
static int prune_file(struct file *fp);
static void queue_file(struct file *fp);
/*
* globals
*/
static struct file *changes; /* list of files to be reconciled */
static long total_files; /* total number of files being considered */
static long est_deletes; /* estimated number of files to be deleted */
static long est_rmdirs; /* est rmdirs of non-empty directories */
int inum_changes; /* LISTed directories whose I#s changed */
/*
* routine:
* analyze
*
* purpose:
* top level routine for the analysis/reconciliation process
*
* parameters:
* none
*
* returns:
* error mask
*
* notes:
* a critical side effect of this routine is the creation of
* the reconciliation list, an ordered list of files that
* needed to be processed in the subsequent reconciliation pass
*/
errmask_t
analyze()
{ struct base *bp;
struct file *fp;
int errs = 0;
int err;
int percentage;
bool_t aborted = FALSE;
char msgbuf[MAX_LINE];
/*
* run through all bases and directories looking for files
* that have been renamed. This must be done before the
* difference analysis because a directory rename can introduce
* radical restructuring into a name-based tree.
*/
for (bp = bases; bp; bp = bp->b_next) {
for (fp = bp->b_files; fp; fp = fp->f_next)
if (fp->f_flags & F_EVALUATE)
errs |= find_renames(fp);
}
/*
* run through all bases and files looking for candidates
* note, however that we only descend into trees that have
* the evaluate flag turned on. As a result of new rules or
* restriction arguments, we may be deliberatly ignoring
* large amounts of the baseline. This means we won't do
* any stats to update the information in those nodes, and
* they will be written back just as they were.
*
* note that there is code to prune out baseline nodes for
* files that no longer exist, but that code is in reconcile
* and will never get a chance to run on nodes that aren't
* analyzed.
*
* we also want to run though all nodes with STAT errors
* so that we can put them on the reconciliation list.
*/
for (bp = bases; bp; bp = bp->b_next) {
for (fp = bp->b_files; fp; fp = fp->f_next)
if (fp->f_flags & (F_EVALUATE|F_STAT_ERROR))
errs |= check_file(fp);
}
/*
* my greatest fear is that someday, somehow, by messing with
* variables or baselines or who-knows-what, that someone will
* run a reconciliation against a large tree that doesn't correspond
* to the baseline, and I will infer that a bazillion files have
* been deleted and will propagate the slaughter before anyone
* can say somebody stop that maniac.
*
* in order to prevent such a possibility, we have a few different
* sanity checks. There is, of course, a tradeoff here between
* danger and irritation. The current set of heuristics for whether
* or not to generate a warning are (any of)
*
* at least CONFIRM_MIN files have been deleted AND
* CONFIRM_PCT of all files have been deleted
*
* the inode number on a LISTed directory has changed
*
* a non-empty directory has been deleted.
*/
msgbuf[0] = 0;
percentage = (est_deletes * 100) / (total_files ? total_files : 1);
if (est_deletes >= CONFIRM_MIN && percentage >= CONFIRM_PCT)
sprintf(msgbuf, gettext(WARN_deletes), est_deletes);
else if (inum_changes > 0)
sprintf(msgbuf, gettext(WARN_ichange), inum_changes);
else if (est_rmdirs)
sprintf(msgbuf, gettext(WARN_rmdirs), est_rmdirs);
if (msgbuf[0])
confirm(msgbuf);
/*
* TRICK:
* the change list contains both files that have changed
* (and probably warrant reconciliation) and files that
* we couldn't get up-to-date stat information on. The
* latter files should just be flagged as being in conflict
* so they can be reported in the summary. The same is
* true of all subsequent files if we abort reconciliation.
*/
for (fp = changes; fp; fp = fp->f_rnext)
if (aborted || (fp->f_flags & F_STAT_ERROR)) {
fp->f_flags |= F_CONFLICT;
/* if it isn't in the baseline yet, don't add it */
if ((fp->f_flags & F_IN_BASELINE) == 0)
fp->f_flags |= F_REMOVE;
fp->f_problem = aborted ? PROB_aborted : PROB_restat;
(fp->f_base)->b_unresolved++;
errs |= ERR_UNRESOLVED;
if (opt_verbose)
fprintf(stdout,
gettext(aborted ? V_suppressed
: V_nostat),
fp->f_fullname);
} else {
err = reconcile(fp);
errs |= err;
if (opt_halt && (err & ERR_ABORT)) {
fprintf(stderr, gettext(ERR_abort_h));
aborted = TRUE;
}
}
return (errs);
}
/*
* routine:
* prune_file
*
* purpose:
* to look for file entries that should be pruned from baseline
* prune the current file if it needs pruning, and recursively
* descend if it is a directory.
*
* parameters:
* pointer to file node
*/
static int
prune_file(struct file *fp)
{ struct file *cp;
int prunes = 0;
/* if node hasn't been evaluated, mark it for removal */
if ((fp->f_flags & (F_EVALUATE|F_STAT_ERROR)) == 0) {
fp->f_flags |= F_REMOVE;
prunes++;
if (opt_debug & DBG_ANAL)
fprintf(stderr, "ANAL: PRUNE %s\n", fp->f_name);
}
/* now check our children */
for (cp = fp->f_files; cp; cp = cp->f_next)
prunes += prune_file(cp);
return (prunes);
}
/*
* routine:
* prune
*
* purpose:
* to prune the baseline of entries that no longer correspond to
* existing rules.
*
* notes:
* This routine just calls prune_file on the top of each base tree.
*/
int
prune()
{ struct base *bp;
struct file *fp;
int prunes = 0;
for (bp = bases; bp; bp = bp->b_next) {
for (fp = bp->b_files; fp; fp = fp->f_next)
prunes += prune_file(fp);
if ((bp->b_flags & F_EVALUATE) == 0)
bp->b_flags |= F_REMOVE;
}
return (prunes);
}
/*
* routine:
* summary
*
* purpose:
* to print out statics and conflict lists
*/
void
summary()
{ struct base *bp;
struct file *fp;
extern bool_t need_super;
(void) fflush(stdout);
for (bp = bases; bp; bp = bp->b_next) {
/* see if this base was irrelevant */
if ((bp->b_flags & F_EVALUATE) == 0)
continue;
/* print out a summary for this base */
fprintf(stderr, gettext(SUM_hd),
bp->b_src_spec, bp->b_dst_spec, bp->b_totfiles);
fprintf(stderr, gettext(SUM_dst),
bp->b_dst_copies, bp->b_dst_deletes, bp->b_dst_misc);
fprintf(stderr, gettext(SUM_src),
bp->b_src_copies, bp->b_src_deletes, bp->b_src_misc);
if (bp->b_unresolved)
fprintf(stderr, gettext(SUM_unresolved),
bp->b_unresolved);
/* print out a list of unreconciled files for this base */
for (fp = changes; fp; fp = fp->f_rnext) {
if (fp->f_base != bp)
continue;
if ((fp->f_flags & F_CONFLICT) == 0)
continue;
fprintf(stderr, "\t\t%s (%s)\n", fp->f_fullname,
fp->f_problem ? fp->f_problem : "???");
}
fprintf(stderr, "\n");
}
if (need_super)
fprintf(stderr, gettext(WARN_super));
}
/*
* routine:
* check_file
*
* purpose:
* figure out if a file requires reconciliation and recursively
* descend into all sub-files and directories
*
* parameters:
* base pointer
* file pointer
*
* returns:
* error mask
* built up changes needed list
* updated statistics
*
* notes:
* this routine builds up a path name as it descends through
* the tree (see push_name, pop_name, get_name).
*/
static errmask_t
check_file(struct file *fp)
{ struct file *cp;
int errs = 0;
if ((fp->f_flags & F_STAT_ERROR) == 0) {
/* see if the source has changed */
fp->f_info[OPT_BASE].f_modtime = fp->f_s_modtime;
fp->f_info[OPT_BASE].f_ino = fp->f_s_inum;
fp->f_info[OPT_BASE].f_d_maj = fp->f_s_maj;
fp->f_info[OPT_BASE].f_d_min = fp->f_s_min;
fp->f_info[OPT_BASE].f_nlink = fp->f_s_nlink;
fp->f_srcdiffs |= check_changes(fp, OPT_BASE, OPT_SRC);
/* see if the destination has changed */
fp->f_info[OPT_BASE].f_modtime = fp->f_d_modtime;
fp->f_info[OPT_BASE].f_ino = fp->f_d_inum;
fp->f_info[OPT_BASE].f_d_maj = fp->f_d_maj;
fp->f_info[OPT_BASE].f_d_min = fp->f_d_min;
fp->f_info[OPT_BASE].f_nlink = fp->f_d_nlink;
fp->f_dstdiffs |= check_changes(fp, OPT_BASE, OPT_DST);
/* if nobody thinks the file exists, baseline needs pruning */
if ((fp->f_flags & (F_IN_SOURCE|F_IN_DEST)) == 0) {
fp->f_srcdiffs |= D_DELETE;
fp->f_dstdiffs |= D_DELETE;
}
/* keep track of possible deletions to look for trouble */
if ((fp->f_dstdiffs | fp->f_srcdiffs) & D_DELETE) {
est_deletes++;
/* see if file is (or has been) a non-empty directory */
if (fp->f_files)
est_rmdirs++;
}
}
/* if we found differences, queue the file for reconciliation */
if (fp->f_srcdiffs || fp->f_dstdiffs || fp->f_flags & F_STAT_ERROR) {
queue_file(fp);
if (opt_debug & DBG_ANAL) {
fprintf(stderr, "ANAL: src=%s",
showflags(diffmap, fp->f_srcdiffs));
fprintf(stderr, " dst=%s",
showflags(diffmap, fp->f_dstdiffs));
fprintf(stderr, " flgs=%s",
showflags(fileflags, fp->f_flags));
fprintf(stderr, " name=%s\n", fp->f_fullname);
}
}
/* bump the total file count */
fp->f_base->b_totfiles++;
total_files++;
/* if this is not a directory, we're done */
if (fp->f_files == 0)
return (errs);
/*
* If this is a directory, we need to recursively analyze
* our children, but only children who have been evaluated.
* If a node has not been evaluated, then we don't have
* updated stat information and there is nothing to analyze.
*
* we also want to run though all nodes with STAT errors
* so that we can put them on the reconciliation list.
* If a directory is unreadable on one side, all files
* under that directory (ON BOTH SIDES) must be marked as
* blocked by stat errors.
*/
push_name(fp->f_name);
for (cp = fp->f_files; cp; cp = cp->f_next) {
if (fp->f_flags & F_STAT_ERROR)
cp->f_flags |= F_STAT_ERROR;
if (cp->f_flags & (F_EVALUATE|F_STAT_ERROR))
errs |= check_file(cp);
}
pop_name();
return (errs);
}
/*
* routine:
* check_changes
*
* purpose:
* to figure out what has changed for a specific file
*
* parameters:
* file pointer
* the reference info
* the info to be checked for changes
*
* returns:
* diff mask
*
* notes:
* this routine doesn't pretend to understand what happened.
* it merely enumerates the ways in which the files differ.
*/
static diffmask_t
check_changes(struct file *fp, int ref, int new)
{ struct fileinfo *rp, *np;
int mask = 0;
int type;
rp = &fp->f_info[ref];
np = &fp->f_info[new];
if (np->f_uid != rp->f_uid)
mask |= D_UID;
if (np->f_gid != rp->f_gid)
mask |= D_GID;
if (np->f_mode != rp->f_mode)
mask |= D_PROT;
type = np->f_type;
if (type != rp->f_type) {
if (type == 0)
mask |= D_DELETE;
else if (rp->f_type == 0)
mask |= D_CREATE;
else
mask |= D_TYPE;
} else if (type == S_IFBLK || type == S_IFCHR) {
/*
* for special files, we only look at the maj/min
*/
if (np->f_rd_maj != rp->f_rd_maj)
mask |= D_SIZE;
if (np->f_rd_min != rp->f_rd_min)
mask |= D_SIZE;
} else if (type != S_IFDIR) {
/*
* for directories, we don't look directly at
* the contents, so these fields don't mean
* anything. If the directories have changed
* in any interesting way, we'll find it by
* walking the tree.
*/
if (np->f_modtime > rp->f_modtime)
mask |= D_MTIME;
if (np->f_size != rp->f_size)
mask |= D_SIZE;
if (np->f_nlink != rp->f_nlink)
mask |= D_LINKS;
}
if (cmp_acls(rp, np) == 0)
mask |= D_FACLS;
return (mask);
}
/*
* routine:
* same_name
*
* purpose:
* to figure out whether or not two databsae nodes actually refer to
* the same file.
*
* parameters:
* pointers to two file description nodes
* which side we should check
*
* returns:
* TRUE/FALSE
*
* notes:
* if a single directory is specified in multiple base pairs, it
* is possible to have multiple nodes in the database describing
* the same file. This routine is supposed to detect those cases.
*
* what should be a trivial string comparison is complicated by
* the possibility that the two nodes might describe the same file
* from base directories at different depths. Thus, rather than
* comparing two strings, we really want to compare the concatenation
* of two pairs of strings. Unfortunately calling full_name would
* be awkward right now, so instead we have our own comparison
* routine that automatically skips from the first string to
* the second.
*/
static bool_t
same_name(struct file *f1, struct file *f2, side_t srcdst)
{
char *s1, *s2, *x1, *x2;
if (srcdst == OPT_SRC) {
s1 = (f1->f_base)->b_src_name;
s2 = (f2->f_base)->b_src_name;
} else {
s1 = (f1->f_base)->b_dst_name;
s2 = (f2->f_base)->b_dst_name;
}
x1 = f1->f_fullname;
x2 = f2->f_fullname;
/*
* Compare the two names, and if they differ before they end
* this is a non-match. If they both end at the same time,
* this is a match.
*
* The trick here is that each string is actually the logical
* concatenation of two strings, and we need to automatically
* wrap from the first to the second string in each pair. There
* is no requirement that the two (concatenated) strings be
* broken at the same point, so we have a slightly baroque
* comparsion loop.
*/
while (*s1 && *s1 == *s2) {
/*
* strings have been identical so far, so advance the
* pointers and continue the comparison. The trick
* is that when either string ends, we have to wrap
* over to its extension.
*/
s1++; s2++;
if (*s1 && *s2)
continue;
/*
* at least one of the strings has ended.
* there is an implicit slash between the string
* and its extension, and this has to be matched
* against the other string.
*/
if (*s1 != *s2) {
if (*s1 == 0 && *s2 == '/')
s2++;
else if (*s2 == 0 && *s1 == '/')
s1++;
else
/* the disagreement doesn't come at a slash */
break;
}
/*
* if either string has ended, wrap to its extension
*/
if (*s1 == 0 && x1 != 0) {
s1 = x1;
x1 = 0;
}
if (*s2 == 0 && x2 != 0) {
s2 = x2;
x2 = 0;
}
}
return (*s1 == *s2);
}
/*
* routine:
* find_link
*
* purpose:
* to figure out if there is a file to which we should
* be creating a link (rather than making a copy)
*
* parameters:
* file node for the file to be created (that we hope is merely a link)
* which side is to be changed (src/dst)
*
* return:
* 0 no link is appropriate
* else pointer to file node for link referent
*
* notes:
* there are a few strange heuristics in this routine and I
* wouldn't bet my soul that I got all of them right. The general
* theory is that when a new file is created, we look to see if it
* is a link to another file on the changed side, and if it is, we
* find the corresponding file on the unchanged side.
*
* cases we want to be able to handle:
* 1. one or more links are created to a prexisting file
* 2. a preexisting only link is renamed
* 3. a rename of one of multiple links to a preexisting file
* 4. a single file is created with multiple links
*/
struct file *
find_link(struct file *fp, side_t srcdst)
{ struct file *lp;
side_t chgside, tgtside;
struct fileinfo *chgp, *tgtp, *basp, *fcp, *ftp;
/* chg = side on which the change was noticed */
/* tgt = side to which the change is to be propagated */
chgside = (srcdst == OPT_SRC) ? OPT_DST : OPT_SRC;
tgtside = (srcdst == OPT_SRC) ? OPT_SRC : OPT_DST;
fcp = &fp->f_info[chgside];
ftp = &fp->f_info[tgtside];
/*
* cases 1 and 3
*
* When a new link is created, we should be able to find
* another file in the changed hierarchy that has the same
* I-node number. We expect it to be on the changed list
* because the link count will have gone up or because all
* of the copies are new. If we find one, then the new file
* on the receiving file should be a link to the corresponding
* existing file.
*
* case 4
*
* the first link will be dealt with as a copy, but all
* subsequent links should find an existing file analogous
* to one of the links on the changed side, and create
* corresponding links on the other side.
*
* in each of these cases, there should be multiple links
* on the changed side. If the linkcount on the changed
* side is one, we needn't bother searching for other links.
*/
if (fcp->f_nlink > 1)
for (lp = changes; lp; lp = lp->f_rnext) {
/* finding the same node doesn't count */
if (fp == lp)
continue;
tgtp = &lp->f_info[tgtside];
chgp = &lp->f_info[chgside];
/*
* if the file doesn't already exist on the target side
* we cannot make a link to it
*/
if (tgtp->f_mode == 0)
continue;
/*
* if this is indeed a link, then the prospective file on
* the changed side will have the same dev/inum as the file
* we are looking for
*/
if (fcp->f_d_maj != chgp->f_d_maj)
continue;
if (fcp->f_d_min != chgp->f_d_min)
continue;
if (fcp->f_ino != chgp->f_ino)
continue;
/*
* if the target side is already a link to this file,
* then there is no new link to be created
* FIX: how does this interact with copies over links
*/
if ((ftp->f_d_maj == tgtp->f_d_maj) &&
(ftp->f_d_min == tgtp->f_d_min) &&
(ftp->f_ino == tgtp->f_ino))
continue;
/*
* there is a pathological situation where a single file
* might appear under multiple base directories. This is
* damned awkward to detect in any other way, so we must
* check to see if we have just found another database
* instance for the same file (on the changed side).
*/
if ((fp->f_base != lp->f_base) && same_name(fp, lp, chgside))
continue;
if (opt_debug & DBG_ANAL)
fprintf(stderr, "ANAL: FIND LINK %s and %s\n",
fp->f_fullname, lp->f_fullname);
return (lp);
}
/*
* case 2: a simple rename of the only link
*
* In this case, there may not be any other existing file on
* the changed side that has the same I-node number. There
* might, however, be a record of such a file in the baseline.
* If we can find an identical file with a different name that
* has recently disappeared, we have a likely rename.
*/
for (lp = changes; lp; lp = lp->f_rnext) {
/* finding the same node doesn't count */
if (fp == lp)
continue;
tgtp = &lp->f_info[tgtside];
chgp = &lp->f_info[chgside];
/*
* if the file still exists on the changed side this is
* not a simple rename, and in fact the previous pass
* would have found it.
*/
if (chgp->f_mode != 0)
continue;
/*
* the inode number for the new link on the changed
* side must match the inode number for the old link
* from the baseline.
*/
if (fcp->f_d_maj != ((srcdst == OPT_SRC) ? lp->f_d_maj
: lp->f_s_maj))
continue;
if (fcp->f_d_min != ((srcdst == OPT_SRC) ? lp->f_d_min
: lp->f_s_min))
continue;
if (fcp->f_ino != ((srcdst == OPT_SRC) ? lp->f_d_inum
: lp->f_s_inum))
continue;
/* finding a file we are already linked to doesn't help */
if ((ftp->f_d_maj == tgtp->f_d_maj) &&
(ftp->f_d_min == tgtp->f_d_min) &&
(ftp->f_ino == tgtp->f_ino))
continue;
/*
* there is a danger that we will confuse an
* inode reallocation with a rename. We should
* only consider this to be a rename if the
* new file is identical to the old one
*/
basp = &lp->f_info[OPT_BASE];
if (fcp->f_type != basp->f_type)
continue;
if (fcp->f_size != basp->f_size)
continue;
if (fcp->f_mode != basp->f_mode)
continue;
if (fcp->f_uid != basp->f_uid)
continue;
if (fcp->f_gid != basp->f_gid)
continue;
if (opt_debug & DBG_ANAL)
fprintf(stderr, "ANAL: FIND RENAME %s and %s\n",
fp->f_fullname, lp->f_fullname);
return (lp);
}
return (0);
}
/*
* routine:
* has_other_links
*
* purpose:
* to determine whether or not there is more that one link to a
* particular file. We are willing to delete a link to a file
* that has changed if we will still have other links to it.
* The trick here is that we only care about links under our
* dominion.
*
* parameters:
* file pointer to node we are interested in
* which side we are looking to additional links on
*
* returns:
* TRUE if there are multiple links
* FALSE if this is the only one we know of
*/
bool_t
has_other_links(struct file *fp, side_t srcdst)
{ struct file *lp;
struct fileinfo *fip, *lip;
fip = &fp->f_info[srcdst];
/* if the link count is one, there couldn't be others */
if (fip->f_nlink < 2)
return (FALSE);
/* look for any other files for the same inode */
for (lp = changes; lp; lp = lp->f_rnext) {
/* finding the same node doesn't count */
if (fp == lp)
continue;
lip = &lp->f_info[srcdst];
/*
* file must still exist on this side
*/
if (lip->f_mode == 0)
continue;
/*
* if this is indeed a link, then the prospective file on
* the changed side will have the same dev/inum as the file
* we are looking for
*/
if (lip->f_d_maj != fip->f_d_maj)
continue;
if (lip->f_d_min != fip->f_d_min)
continue;
if (lip->f_ino != fip->f_ino)
continue;
/*
* we have found at least one other link
*/
return (TRUE);
}
return (FALSE);
}
/*
* routine:
* link_update
*
* purpose:
* to propoagate a stat change to all other file nodes that
* correspond to the same I-node on the changed side
*
* parameters:
* file pointer for the updated file
* which side was changed
*
* returns:
* void
*
* notes:
* if we have copied onto a file, we have copied onto all
* of its links, but since we do all stats before we do any
* copies, the stat information recently collected for links
* is no longer up-to-date, and this would result in incorrect
* reconciliation (redundant copies).
*
* There is an assumption here that all links to a changed
* file will be in the change list. This is true for almost
* all cases not involving restriction. If we do fail to
* update the baseline for a file that was off the change list,
* the worst that is likely to happen is that we will think
* it changed later (but will almost surely find that both
* copies agree).
*/
void
link_update(struct file *fp, side_t which)
{ struct file *lp;
for (lp = changes; lp; lp = lp->f_rnext) {
/* finding the current entry doesn't count */
if (lp == fp)
continue;
/* look for same i#, maj, min on changed side */
if (lp->f_info[which].f_ino != fp->f_info[which].f_ino)
continue;
if (lp->f_info[which].f_d_maj != fp->f_info[which].f_d_maj)
continue;
if (lp->f_info[which].f_d_min != fp->f_info[which].f_d_min)
continue;
/*
* this appears to be another link to the same file
* so the updated stat information for one must be
* correct for the other.
*/
lp->f_info[which].f_type = fp->f_info[which].f_type;
lp->f_info[which].f_size = fp->f_info[which].f_size;
lp->f_info[which].f_mode = fp->f_info[which].f_mode;
lp->f_info[which].f_uid = fp->f_info[which].f_uid;
lp->f_info[which].f_gid = fp->f_info[which].f_gid;
lp->f_info[which].f_modtime = fp->f_info[which].f_modtime;
lp->f_info[which].f_modns = fp->f_info[which].f_modns;
lp->f_info[which].f_nlink = fp->f_info[which].f_nlink;
lp->f_info[which].f_rd_maj = fp->f_info[which].f_rd_maj;
lp->f_info[which].f_rd_min = fp->f_info[which].f_rd_min;
if (opt_debug & DBG_STAT)
fprintf(stderr,
"STAT: UPDATE LINK, file=%s, mod=%08lx.%08lx\n",
lp->f_name, lp->f_info[which].f_modtime,
lp->f_info[which].f_modns);
}
}
/*
* routine:
* queue_file
*
* purpose:
* append a file to the list of needed reconciliations
*
* parameters:
* pointer to file
*
* notes:
* when a request is appended to the reconciliation list,
* we fill in the full name. We delayed this in hopes that
* it wouldn't be necessary (saving cycles and memory)
*
* There is some funny business with modification times.
* In general, we queue files in order of the latest modification
* time so that propagations preserve relative ordering. There
* are, however, a few important exceptions:
* 1. all directory creations happen at time zero,
* so that they are created before any files can
* be added to them.
* 2. all directory deletions happen at time infinity-depth,
* so that everything else can be removed before the
* directories themselves are removed.
* 3. all file deletions happen at time infinity-depth
* so that (in renames) the links will preceed the unlinks.
*/
static void
queue_file(struct file *fp)
{ struct file **pp, *np;
#define TIME_ZERO 0L /* the earliest possible time */
#define TIME_LONG 0x7FFFFFFF /* the latest possible time */
/*
* figure out the modification time for sequencing purposes
*/
if ((fp->f_srcdiffs|fp->f_dstdiffs) & D_DELETE) {
/*
* deletions are performed last, and depth first
*/
fp->f_modtime = TIME_LONG - fp->f_depth;
} else if (fp->f_info[OPT_SRC].f_type != S_IFDIR &&
fp->f_info[OPT_DST].f_type != S_IFDIR) {
/*
* for most files we use the latest mod time
*/
fp->f_modtime = fp->f_info[OPT_SRC].f_modtime;
fp->f_modns = fp->f_info[OPT_SRC].f_modns;
if (fp->f_modtime < fp->f_info[OPT_DST].f_modtime) {
fp->f_modtime = fp->f_info[OPT_DST].f_modtime;
fp->f_modns = fp->f_info[OPT_DST].f_modns;
}
} else {
/*
* new directory creations need to happen before anything
* else and are automatically sequenced in traversal order
*/
fp->f_modtime = TIME_ZERO;
}
/*
* insertion is time ordered, and for equal times,
* insertions is in (pre-order) traversal order
*/
for (pp = &changes; (np = *pp) != 0; pp = &np->f_rnext) {
if (fp->f_modtime > np->f_modtime)
continue;
if (fp->f_modtime < np->f_modtime)
break;
if (fp->f_modns < np->f_modns)
break;
}
fp->f_fullname = strdup(get_name(fp));
fp->f_rnext = np;
*pp = fp;
}
/*
* routines:
* push_name/pop_name/get_name
*
* purpose:
* maintain a name stack so we can form name of a particular file
* as the concatenation of all of the names between it and the
* (know to be fully qualified) base directory.
*
* notes:
* we go to this trouble because most files never change and
* so we don't need to associate full names with every one.
* This stack is maintained during analysis, and if we decide
* to add a file to the reconciliation list, we can use the
* stack to generate a fully qualified name at that time.
*
* we compress out '/./' when we return a name. Given that the
* stack was built by a tree walk, the only place a /./ should
* appear is at the first level after the base ... but there
* are legitimate ways for them to appear there.
*
* these names can get deep, so we dynamically size our name buffer
*/
static const char *namestack[ MAX_DEPTH + 1 ];
static int namedepth = 0;
static int namelen = 0;
void
push_name(const char *name)
{
namestack[ namedepth++ ] = name;
namelen += 2 + strlen(name);
/* make sure we don't overflow our name stack */
if (namedepth >= MAX_DEPTH) {
fprintf(stderr, gettext(ERR_deep), name);
exit(ERR_OTHER);
}
}
void
pop_name(void)
{
namelen -= 2 + strlen(namestack[--namedepth]);
namestack[ namedepth ] = 0;
#ifdef DBG_ERRORS
/* just a little sanity check here */
if (namedepth <= 0) {
if (namedepth < 0) {
fprintf(stderr, "ASSERTION FAILURE: namedepth < 0\n");
exit(ERR_OTHER);
} else if (namelen != 0) {
fprintf(stderr, "ASSERTION FAILURE: namelen != 0\n");
exit(ERR_OTHER);
}
}
#endif
}
char
*get_name(struct file *fp)
{ int i;
static char *namebuf = 0;
static int buflen = 0;
/* make sure we have an adequate buffer */
i = namelen + 1 + strlen(fp->f_name);
if (buflen < i) {
for (buflen = MAX_PATH; buflen < i; buflen += MAX_NAME);
namebuf = (char *) realloc(namebuf, buflen);
}
/* assemble the name */
namebuf[0] = 0;
for (i = 0; i < namedepth; i++) {
if (strcmp(namestack[i], ".")) {
strcat(namebuf, namestack[i]);
strcat(namebuf, "/");
}
}
strcat(namebuf, fp->f_name);
return (namebuf);
}
|