-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtdiff.c
2743 lines (2499 loc) · 62.4 KB
/
tdiff.c
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
/*
tdiff - tree diffs
Main()
Copyright (C) 1999, 2008, 2014, 2019, 2022, 2024 Philippe Troin <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#if HAVE_TERMIOS_H
# include <termios.h>
#endif
#if HAVE_SYS_TTY_H
# include <sys/tty.h>
#endif
#if HAVE_GETDENTS
# if HAVE_SYS_DIRENT_H
# include <sys/dirent.h>
# elif HAVE_GETDENTS64_SYSCALL_H
# include "linux_getdents64.c"
# elif HAVE_GETDENTS_SYSCALL_H
# include "linux_getdents.c"
# else
# error HAVE_GETDENTS is set, but I do not know how to get it !!!
# endif
#endif
#if HAVE_GETOPT_LONG
# include <getopt.h>
#endif
#if !HAVE_LSTAT
# define lstat(f,b) stat(f,b)
#endif
#if HAVE_SYS_MKDEV_H
# include <sys/mkdev.h>
#elif HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>
#elif ! HAVE_MAJOR_MINOR_FUNCTIONS
# error Cannot find major() and minor()
#endif
#if HAVE_LGETXATTR
#include <sys/xattr.h>
#endif
#if HAVE_ACL
#include <sys/acl.h>
# if HAVE_ACL_CMP
# include <acl/libacl.h>
# endif
#endif
#include "tdiff.h"
#include "str_list.h"
#include "utils.h"
#include "ent_pair_cache.h"
#include "hard_links_cache.h"
#include "st_xtime_ns.h"
#define UNUSED(x) ((void)(x))
#define XATTR_BUF_SIZE 16384
#define GETDIRLIST_DENTBUF_SIZE 8192
#define XREADLINK_BUF_SIZE 1024
#define CMPFILE_BUF_SIZE 16384
#define VERB_STATS 1
#define VERB_SKIPS 2
#define VERB_HASH_STATS 3
#define VERB_MEM_STATS 4
typedef long long counter_t;
typedef struct dexe_s
{
char ** argv;
char ** arg1;
char ** arg2;
} dexe_t;
#define OPT_MODE_OR_DEFAULT ((unsigned)0)
#define OPT_MODE_AND_DEFAULT ((unsigned)~0)
typedef struct option_s
{
unsigned int verbosity_level:8;
unsigned int dirs:1;
unsigned int type:1;
unsigned int mode:1;
unsigned int uid:1;
unsigned int gid:1;
unsigned int nlink:1;
unsigned int hardlinks:1;
unsigned int mtime:1;
unsigned int atime:1;
unsigned int ctime:1;
unsigned int size:1;
unsigned int blocks:1;
unsigned int contents:1;
unsigned int major:1;
unsigned int minor:1;
unsigned int xattr:1;
unsigned int flags:1;
unsigned int acl:1;
unsigned int mode_and;
unsigned int mode_or;
unsigned int exec:1;
unsigned int exec_always:1;
unsigned int follow_symlinks:1;
genhash_t* exclusions;
dexe_t exec_args;
dexe_t exec_always_args;
size_t root1_length;
size_t root2_length;
ent_pair_cache_t *inocache;
hard_links_cache_t *hardlinks1;
hard_links_cache_t *hardlinks2;
str_list_t *empty_str_list;
struct
{
counter_t singles;
counter_t compared;
counter_t contents_compared;
counter_t excluded;
counter_t skipped_same;
counter_t skipped_cache;
} stats;
} options_t;
typedef struct str_list_client_data_s
{
options_t *opts;
} str_list_client_data_t;
static str_list_t *get_dir_list(const char* path, const struct stat*, int*);
static int dodiff(options_t* opts, const char* p1, const char* p2);
static char* pconcat(const char* p1, const char* p2);
static int exec_process(const dexe_t *dex, const char* p1, const char* p2);
static int drop_acl_xattrs(const char*);
#if DEBUG
static void print_options(const options_t*);
#else
# define print_options(a)
#endif
static void
xperror(const char* msg, const char *filename)
{
fprintf(stderr, "%s: %s: %s: %s (errno=%d)\n",
progname, filename, msg, strerror(errno), errno);
}
/*
* Common printing
*/
static void
report_missing(int which, const char* f, const char* what, str_list_client_data_t* client_data)
{
const char* subp;
int rootlen;
/**/
switch(which)
{
case 1:
subp = f+(rootlen = client_data->opts->root1_length);
break;
case 2:
subp = f+(rootlen = client_data->opts->root2_length);
break;
default:
fprintf(stderr, "%s: report_missing(): unexpected which=%d, aborting...\n",
progname, which);
exit(XIT_INTERNALERROR);
}
if (*subp)
{
if (*subp != '/')
{
fprintf(stderr, "%s: report_missing(): unexpected subp=\"%c\" (code=%d), aborting...\n",
progname, isprint(*subp) ? *subp : '?', *subp);
exit(XIT_INTERNALERROR);
}
++subp;
}
else
{
subp = "(top-level)";
}
printf("%s: %s: %s: only present in %.*s\n",
progname, subp, what, rootlen, f);
}
/*
* Xattrs comparisons
*/
#if HAVE_LGETXATTR
static str_list_t*
get_xattr_list(const char* path)
{
str_list_t *rv;
char *buf;
size_t buf_size = XATTR_BUF_SIZE;
ssize_t read_size;
const char *p;
const char *p_e;
/**/
buf = xmalloc(buf_size);
again:
read_size = llistxattr(path, buf, buf_size);
if (read_size == -1)
switch(errno)
{
case ENOTSUP:
str_list_new(&rv);
return rv;
case ERANGE:
free(buf);
buf = xmalloc(buf_size *= 2);
goto again;
default:
xperror("cannot get extended attribute list, llistxattr()", path);
free(buf);
return NULL;
}
str_list_new(&rv);
for (p = buf, p_e = buf+read_size;
p < p_e && *p;
p += strlen(p)+1)
str_list_push(rv, p);
free(buf);
return rv;
}
static int
report_missing_xattr(int which, const char* f, const char* xn, str_list_client_data_t* client_data)
{
static const char prefix[] = "xattr ";
size_t buf_len;
char *buf;
#if HAVE_ACL
if (drop_acl_xattrs(xn))
return XIT_OK;
#endif
buf_len = sizeof(prefix)-1+strlen(xn)+1;
buf = xmalloc(buf_len);
snprintf(buf, buf_len, "%s%s", prefix, xn);
report_missing(which, f, buf, client_data);
free(buf);
return XIT_DIFF;
}
static void*
get_xattr(const char* p, const char* name, size_t *returned_size)
{
size_t buf_size = XATTR_BUF_SIZE;
void *buf = xmalloc(buf_size);
ssize_t sz;
/**/
*returned_size = 0;
again:
sz = lgetxattr(p, name, buf, buf_size);
if (sz == -1)
switch(errno)
{
case ERANGE:
free(buf);
buf = xmalloc(buf_size *= 2);
goto again;
default:
free(buf);
return 0;
}
*returned_size = sz;
return buf;
}
static int
compare_xattrs(const char* p1, const char* p2,
const char* e1, const char* e2,
str_list_client_data_t* client_data)
{
int rv = XIT_OK;
size_t sz1 = 0;
size_t sz2 = 0;
void* buf1;
void* buf2;
#if HAVE_ACL
if (drop_acl_xattrs(e1))
return rv;
#endif
buf1 = get_xattr(p1, e1, &sz1);
buf2 = get_xattr(p2, e2, &sz2);
if (!buf1)
{
BUMP_EXIT_CODE(rv, XIT_SYS);
xperror("cannot get extended attribute list, lgetxattr()", p1);
goto ret;
}
if (!buf2)
{
BUMP_EXIT_CODE(rv, XIT_SYS);
xperror("cannot get extended attribute list, lgetxattr()", p2);
goto ret;
}
if (sz1 != sz2 || memcmp(buf1, buf2, sz1))
{
printf("%s: %s: xattr %s: contents differ\n",
progname, p1+client_data->opts->root1_length+1, e1);
BUMP_EXIT_CODE(rv, XIT_DIFF);
}
ret:
if (buf1)
free(buf1);
if (buf2)
free(buf2);
return rv;
}
static int
drop_acl_xattrs(const char *xn)
{
return (!strcmp(xn, "system.posix_acl_access")
|| !strcmp(xn, "system.posix_acl_default")
|| !strcmp(xn, "trusted.SGI_ACL_FILE")
|| !strcmp(xn, "trusted.SGI_ACL_DEFAULT"));
}
#endif
/*
* ACL comparisons
*/
#if HAVE_ACL
static int
get_acl(const char* path, acl_type_t acltype, acl_t *pacl)
{
acl_t acl = acl_get_file(path, acltype);
if (acl == NULL)
switch(errno)
{
case ENOTSUP:
case EINVAL:
*pacl = NULL;
return 0;
default:
xperror("cannot get ACL, acl_get_file()", path);
return XIT_SYS;
}
*pacl = acl;
return 0;
}
static str_list_t *
get_acl_list(const char* path, acl_t acl)
{
ssize_t acllen;
char *acls;
char *p;
str_list_t *rv;
/**/
str_list_new(&rv);
if (acl == NULL) return rv;
acls = acl_to_text(acl, &acllen);
if (!acls)
{
xperror("cannot get ACL, acl_to_text()", path);
return NULL;
}
const char* beg_user = acls;
enum {
STATE_FIRST_WS,
STATE_USER,
STATE_USER_FIRST_COLON,
STATE_PERM1,
STATE_PERM2,
STATE_PERM3,
STATE_LAST_WS,
STATE_COMMENT
} state = STATE_FIRST_WS;
for (p = acls; p < acls+acllen; ++p)
switch(state)
{
case STATE_FIRST_WS:
if (!isspace(*p))
state = STATE_USER;
beg_user = p;
break;
case STATE_USER:
case STATE_USER_FIRST_COLON:
if (*p == ':')
++state;
else if (isspace(*p))
{
fprintf(stderr, "%s: get_acl_list(): space-like character \"%c\" (code=%d) while parsing acl \"%s\", aborting...\n",
progname, isprint(*p) ? *p : '?', *p, p);
exit(XIT_INTERNALERROR);
}
break;
case STATE_PERM1:
case STATE_PERM2:
case STATE_PERM3:
switch(*p)
{
case 'r':
case 'w':
case 'x':
case '-':
break;
default:
fprintf(stderr, "%s: get_acl_list(): unexpected character in state %d: \"%c\" (code=%d)\n",
progname, state, isprint(*p) ? *p : '?', *p);
exit(XIT_INTERNALERROR);
}
if (state == STATE_PERM3)
{
char buf[p-beg_user+2];
memcpy(buf, beg_user, p-beg_user+1);
buf[p-beg_user+1] = 0;
buf[p-beg_user-3] = 0;
if (strcmp(buf, "user:")
&& strcmp(buf, "group:")
&& strcmp(buf, "other:"))
str_list_push_length(rv, buf, p-beg_user+2);
}
state += 1;
break;
case STATE_LAST_WS:
switch (*p)
{
case '#':
state = STATE_COMMENT;
break;
case '\n':
state = STATE_FIRST_WS;
break;
default:
if (!isspace(*p))
{
fprintf(stderr, "%s: get_acl_list(): unexpected non-whitespace character in state %d: \"%c\" (code=%d), aborting...\n",
progname, state, isprint(*p) ? *p : '?', *p);
exit(XIT_INTERNALERROR);
}
}
break;
case STATE_COMMENT:
if (*p == '\n')
state = STATE_FIRST_WS;
break;
default:
fprintf(stderr, "%s: get_acl_list(): unexpected state %d, aborting...\n",
progname, state);
exit(XIT_INTERNALERROR);
}
if (state != STATE_FIRST_WS)
{
fprintf(stderr, "%s: get_acl_list(): unexpected state %d at end, aborting...\n",
progname, state);
exit(XIT_INTERNALERROR);
}
acl_free(acls);
return rv;
}
typedef struct acl_compare_client_data_s
{
str_list_client_data_t cmn;
const char* acldescr;
} acl_compare_client_data_t;
static int
report_missing_acl(int which, const char* f, const char* xn, str_list_client_data_t* common_client_data)
{
acl_compare_client_data_t* client_data = (acl_compare_client_data_t*)common_client_data;
static const char infix[] = " acl ";
size_t buf_len;
char *buf;
/**/
buf_len = strlen(client_data->acldescr)+sizeof(infix)-1+strlen(xn)+1;
buf = xmalloc(buf_len);
snprintf(buf, buf_len, "%s%s%s", client_data->acldescr, infix, xn);
report_missing(which, f, buf, &client_data->cmn);
free(buf);
return XIT_DIFF;
}
static int
compare_acls(const char* p1, const char* p2,
const char* e1, const char* e2,
str_list_client_data_t* common_client_data)
{
acl_compare_client_data_t* client_data = (acl_compare_client_data_t*)common_client_data;
int rv = XIT_OK;
const char *v1;
const char *v2;
UNUSED(p2);
v1 = e1 + strlen(e1)+1;
v2 = e2 + strlen(e1)+1;
if (strcmp(v1, v2))
{
printf("%s: %s: %s acl %s: %s %s\n",
progname, p1+client_data->cmn.opts->root1_length+1, client_data->acldescr, e1, v1, v2);
BUMP_EXIT_CODE(rv, XIT_DIFF);
}
return rv;
}
static int
diff_acl(options_t* opts, const char* p1, const char* p2,
acl_type_t acltype, const char* acldescr)
{
acl_t acl1 = NULL;
acl_t acl2 = NULL;
str_list_t *acls1 = NULL;
str_list_t *acls2 = NULL;
int rv, rv2;
acl_compare_client_data_t client_data;
/**/
rv = get_acl(p1, acltype, &acl1);
rv2 = get_acl(p2, acltype, &acl2);
if (rv || rv2) {
rv = rv || rv2;
goto clean_acls;
}
if (acl1 == NULL && acl2 == NULL) return 0;
#if HAVE_ACL_CMP
if (acl1 != NULL && acl2 != NULL)
{
int ret = acl_cmp(acl1, acl2);
switch(ret)
{
case -1:
xperror("cannot compare ACLs, acl_cmp()", p1);
rv = XIT_SYS;
goto clean_acls;
case 0:
rv = 0;
goto clean_acls;
case 1:
break;
default:
fprintf(stderr, "%s: %s: acl_cmp(): unexpected result %d\n",
progname, p1, ret);
return XIT_SYS;
}
}
#endif /* HAVE_ACL_CMP */
acls1 = get_acl_list(p1, acl1);
acls2 = get_acl_list(p2, acl2);
client_data.cmn.opts = opts;
client_data.acldescr = acldescr;
rv = str_list_compare(p1, p2,
acls1, acls2,
report_missing_acl,
compare_acls,
(str_list_client_data_t*)&client_data);
str_list_destroy(acls1);
str_list_destroy(acls2);
clean_acls:
if (acl1 != NULL) acl_free(acl1);
if (acl2 != NULL) acl_free(acl2);
return rv;
}
#endif /* HAVE_ACL */
/*
* Utilities
*/
static int
open_path(const char* path, const struct stat *sbuf)
{
int fd;
int flags;
/**/
flags = O_RDONLY;
switch((sbuf->st_mode) & S_IFMT)
{
case S_IFDIR:
flags = O_RDONLY;
#if HAVE_O_DIRECTORY
flags |= O_DIRECTORY;
#endif /* HAVE_O_DIRECTORY */
break;
case S_IFREG:
flags = O_RDONLY;
break;
#if HAVE_S_IFLNK && HAVE_O_PATH && HAVE_O_NOFOLLOW
case S_IFLNK:
flags = O_PATH|O_NOFOLLOW;
break;
#endif /* HAVE_S_IFLNK && HAVE_O_PATH && HAVE_O_NOFOLLOW */
default:
#if HAVE_O_PATH
flags = O_PATH;
#else /* ! HAVE_O_PATH */
flags = O_RDONLY;
#endif /* ! HAVE_O_PATH */
break;
}
#if HAVE_O_NOATIME
flags |= O_NOATIME;
#endif /* HAVE_O_NOATIME */
fd = open(path, flags);
#if HAVE_O_NOATIME
if (fd < 0 && errno == EPERM)
fd = open(path, flags & ~O_NOATIME);
#endif
return fd;
}
/*
* Directory comparisons
*/
static str_list_t *
get_dir_list(const char* path, const struct stat *sbuf, int *fd)
#if HAVE_GETDENTS
{
char dentbuf[GETDIRLIST_DENTBUF_SIZE];
str_list_t * rv = NULL;
int nread;
/**/
if (*fd < 0)
*fd = open_path(path, sbuf);
if (*fd < 0)
goto err_open;
str_list_new(&rv);
while((nread = getdents(*fd, (void*)dentbuf,
GETDIRLIST_DENTBUF_SIZE))>0)
{
struct STRUCT_DIRENT *dent;
/**/
for (dent = (struct STRUCT_DIRENT*) dentbuf;
(char*)dent - dentbuf < nread;
dent = (struct STRUCT_DIRENT*) ( (char*)dent + dent->d_reclen ))
{
if ( (dent->d_name[0]=='.'
&& (dent->d_name[1] == 0
|| (dent->d_name[1]=='.'
&& dent->d_name[2]== 0))))
continue;
str_list_push(rv, dent->d_name);
}
}
if (nread<0)
goto err_dents;
return rv;
err_dents:
str_list_destroy(rv);
rv = NULL;
err_open:
xperror("cannot get directory listing, getdents()", path);
return rv;
}
#else /* ! HAVE_GETDENTS */
{
#if HAVE_FDOPENDIR
int dupfd;
#endif /* HAVE_FDOPENDIR */
DIR *dir;
struct dirent *dent;
str_list_t *rv = NULL;
/**/
#if HAVE_FDOPENDIR
if (*fd < 0)
*fd = open_path(path, sbuf);
if ( *fd < 0 )
goto err;
dupfd = dup(*fd);
if (dupfd < 0)
goto err;
dir = fdopendir(dupfd);
if (!dir)
{
close(dupfd);
goto err;
}
#else /* ! HAVE_FDOPENDIR */
UNUSED(sbuf);
UNUSED(fd);
dir = opendir(path);
if (!dir)
goto err;
#endif /* ! HAVE_FDOPENDIR */
str_list_new(&rv);
while ((dent = readdir(dir)))
{
if (dent->d_name[0]=='.'
&& (dent->d_name[1] == 0
|| (dent->d_name[1]=='.'
&& dent->d_name[2]== 0)))
continue;
str_list_push(rv, dent->d_name);
}
if (closedir(dir))
{
str_list_destroy(rv);
rv = NULL;
goto err;
}
return rv;
err:
xperror("cannot get directory listing, readdir()", path);
return rv;
}
#endif /* ! HAVE_GETDENTS */
static int
report_missing_file(int which, const char* d, const char *f, str_list_client_data_t* client_data)
{
if ( ! client_data->opts->dirs )
return XIT_OK;
if ( gh_find(client_data->opts->exclusions, f, NULL) )
++ client_data->opts->stats.excluded;
else
{
const char* subp;
int rootlen;
size_t fp_len = strlen(f)+strlen(d)+1;
char fp[fp_len];
++ client_data->opts->stats.singles;
switch(which)
{
case 1:
subp = d+(rootlen = client_data->opts->root1_length);
break;
case 2:
subp = d+(rootlen = client_data->opts->root2_length);
break;
default:
fprintf(stderr, "%s: report_missing_file(): unexpected which=%d, aborting...\n",
progname, which);
exit(XIT_INTERNALERROR);
}
if (*subp)
{
if (*subp != '/')
{
fprintf(stderr, "%s: report_missing_file(): unexpected subp=\"%c\" (code=%d), aborting...\n",
progname, isprint(*subp) ? *subp : '?', *subp);
exit(XIT_INTERNALERROR);
}
++subp;
}
else
{
subp = "";
}
snprintf(fp, fp_len, "%s%s%s", subp, *subp ? "/" : "", f);
printf("%s: %s: only present in %.*s\n",
progname, fp, rootlen, d);
}
return XIT_DIFF;
}
static int
compare_directories(const char* p1, const char* p2,
const char* e1, const char* e2,
str_list_client_data_t* client_data)
{
int rv = XIT_OK;
UNUSED(e2);
/**/
if (gh_find(client_data->opts->exclusions, e1, NULL))
++ client_data->opts->stats.excluded;
else
{
char *np1 = pconcat(p1, e1);
char *np2 = pconcat(p2, e1);
rv = dodiff(client_data->opts, np1, np2);
free(np1);
free(np2);
}
return rv;
}
/*
* Hard links comparisons
*/
static int
report_missing_hard_link(int which, const char* d, const char *f, str_list_client_data_t* client_data)
{
static const char prefix[] = "hard link to ";
size_t buf_len;
char *buf;
buf_len = sizeof(prefix)-1+strlen(f)+1;
buf = xmalloc(buf_len);
snprintf(buf, buf_len, "%s%s", prefix, f);
report_missing(which, d, buf, client_data);
free(buf);
return XIT_DIFF;
}
/*
* Reporting utilities
*/
static const char*
get_file_type(mode_t m)
{
switch(m & S_IFMT)
{
case S_IFDIR: return "directory";
case S_IFREG: return "regular-file";
case S_IFCHR: return "character-device";
case S_IFBLK: return "block-device";
#if HAVE_S_IFIFO
case S_IFIFO: return "fifo";
#endif
#if HAVE_S_IFLNK
case S_IFLNK: return "symbolic-link";
#endif
#if HAVE_S_IFSOCK
case S_IFSOCK: return "socket";
#endif
#if HAVE_S_IFDOOR
case S_IFDOOR: return "door";
#endif
#if HAVE_S_IFWHT
case S_IFWHT: return "whiteout";
#endif
default: return "unknown";
}
}
static void
format_time(char* obuf, size_t buf_size, time_t fsecs, int fnsecs)
{
size_t written;
struct tm tms;
/**/
localtime_r(&fsecs, &tms);
written = strftime(obuf, buf_size, "%Y-%m-%d %H:%M:%S", &tms);
if (fnsecs >= 0 && written < buf_size)
snprintf(obuf+written, buf_size-written, ".%09d", fnsecs);
}
static void
report_time_discrepancy(const char* f, const char* whattime,
time_t f1secs, time_t f2secs,
int f1nsecs, int f2nsecs)
{
char t1[256];
char t2[256];
/**/
format_time(t1, sizeof(t1), f1secs, f1nsecs);
format_time(t2, sizeof(t2), f2secs, f2nsecs);
printf("%s: %s: %s: [%s] [%s]\n",
progname, f, whattime, t1, t2);
}
static int
compare_files(const char* f1, const struct stat *sbuf1, int *fd1,
const char* f2, const struct stat *sbuf2, int *fd2)
{
char buf1[CMPFILE_BUF_SIZE];
char buf2[CMPFILE_BUF_SIZE];
ssize_t toread;
int rv = XIT_OK;
/**/
if (sbuf1->st_size != sbuf2->st_size)
{
fprintf(stderr, "%s: compare_files called on files of different sizes\n",
progname);
BUMP_EXIT_CODE(rv, XIT_INTERNALERROR);
return rv;
}
if (sbuf1->st_size == 0)
{
return rv;
}
if (*fd1 < 0)
*fd1 = open_path(f1, sbuf1);
if (*fd1 < 0)
{
xperror("cannot open file", f1);
BUMP_EXIT_CODE(rv, XIT_SYS);
return rv;
}
if (*fd2 < 0)
*fd2 = open_path(f2, sbuf2);
if (*fd2 < 0)
{
xperror("cannot open file", f2);
BUMP_EXIT_CODE(rv, XIT_SYS);
return rv;
}
for (toread = sbuf1->st_size; toread>0; )
{
int nread1, nread2;