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
|
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Various functions to work with meta entries.
*
* Copyright (C) 2007-2008 David Härdeman <david@hardeman.nu>
* Copyright (C) 2012-2018 Przemyslaw Pawelczyk <przemoc@gmail.com>
*
* 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; only version 2 of the License is applicable.
*
* 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/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#if !defined(NO_XATTR) || !(NO_XATTR+0)
# include <sys/xattr.h>
#endif /* !NO_XATTR */
#include <limits.h>
#include <dirent.h>
#include <sys/mman.h>
#include <utime.h>
#include <fcntl.h>
#include <stdint.h>
#include <errno.h>
#include <time.h>
#include <sys/param.h>
#ifndef BSD
# include <bsd/string.h>
#endif
#include "metastore.h"
#include "metaentry.h"
#include "utils.h"
#ifndef PATH_MAX
# define PATH_MAX 4096
#endif
#if !defined(NO_XATTR) || !(NO_XATTR+0)
/* Free's a metaentry and all its parameters */
static void
mentry_free(struct metaentry *m)
{
unsigned i;
if (!m)
return;
free(m->path);
free(m->owner);
free(m->group);
for (i = 0; i < m->xattrs; i++) {
free(m->xattr_names[i]);
free(m->xattr_values[i]);
}
free(m->xattr_names);
free(m->xattr_values);
free(m->xattr_lvalues);
free(m);
}
#endif /* !NO_XATTR */
/* Allocates an empty metahash table */
static struct metahash *
mhash_alloc(void)
{
struct metahash *mhash;
mhash = xmalloc(sizeof(struct metahash));
memset(mhash, 0, sizeof(struct metahash));
return mhash;
}
/* Generates a hash key (using djb2) */
static unsigned
hash(const char *str)
{
unsigned hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c;
return hash % HASH_INDEXES;
}
/* Allocates an empty metaentry */
static struct metaentry *
mentry_alloc(void)
{
struct metaentry *mentry;
mentry = xmalloc(sizeof(struct metaentry));
memset(mentry, 0, sizeof(struct metaentry));
return mentry;
}
/* Does a bisect search for the closest match in a metaentry list */
static struct metaentry *
mentry_find(const char *path, struct metahash *mhash)
{
struct metaentry *base;
unsigned key;
if (!mhash) {
msg(MSG_ERROR, "%s called with empty hash table\n", __func__);
return NULL;
}
key = hash(path);
for (base = mhash->bucket[key]; base; base = base->next) {
if (!strcmp(base->path, path))
return base;
}
return NULL;
}
/* Inserts a metaentry into a metaentry list */
static void
mentry_insert(struct metaentry *mentry, struct metahash *mhash)
{
unsigned key;
key = hash(mentry->path);
mentry->next = mhash->bucket[key];
mhash->bucket[key] = mentry;
}
#ifdef DEBUG
/* Prints a metaentry */
static void
mentry_print(const struct metaentry *mentry)
{
int i;
if (!mentry || !mentry->path) {
msg(MSG_DEBUG,
"Incorrect meta entry passed to printmetaentry\n");
return;
}
msg(MSG_DEBUG, "===========================\n");
msg(MSG_DEBUG, "Dump of metaentry %p\n", mentry);
msg(MSG_DEBUG, "===========================\n");
msg(MSG_DEBUG, "path\t\t: %s\n", mentry->path);
msg(MSG_DEBUG, "owner\t\t: %s\n", mentry->owner);
msg(MSG_DEBUG, "group\t\t: %s\n", mentry->group);
msg(MSG_DEBUG, "mtime\t\t: %ld\n", (unsigned long)mentry->mtime);
msg(MSG_DEBUG, "mtimensec\t: %ld\n", (unsigned long)mentry->mtimensec);
msg(MSG_DEBUG, "mode\t\t: %ld\n", (unsigned long)mentry->mode);
for (i = 0; i < mentry->xattrs; i++) {
msg(MSG_DEBUG, "xattr[%i]\t: %s=\"", i, mentry->xattr_names[i]);
binary_print(mentry->xattr_values[i], mentry->xattr_lvalues[i]);
msg(MSG_DEBUG, "\"\n");
}
msg(MSG_DEBUG, "===========================\n\n");
}
/* Prints all metaentries in a metaentry list */
static void
mentries_print(const struct metahash *mhash)
{
const struct metaentry *mentry;
int index;
for (index = 0; index < HASH_INDEXES; index++)
for (mentry = mhash->bucket[index]; mentry; mentry = mentry->next)
mentry_print(mentry);
msg(MSG_DEBUG, "%i entries in total\n", mhash->count);
}
#endif
/* Creates a metaentry for the file/dir/etc at path */
struct metaentry *
mentry_create(const char *path, int with_mtime)
{
#if !defined(NO_XATTR) || !(NO_XATTR+0)
ssize_t lsize, vsize;
char *list, *attr;
#endif /* !NO_XATTR */
struct stat sbuf;
struct passwd *pbuf;
struct group *gbuf;
#if !defined(NO_XATTR) || !(NO_XATTR+0)
int i;
#endif /* !NO_XATTR */
struct metaentry *mentry;
if (lstat(path, &sbuf)) {
msg(MSG_ERROR, "lstat failed for %s: %s\n",
path, strerror(errno));
return NULL;
}
pbuf = xgetpwuid(sbuf.st_uid);
if (!pbuf) {
msg(MSG_ERROR, "getpwuid failed for %s: uid %i not found\n",
path, (int)sbuf.st_uid);
return NULL;
}
gbuf = xgetgrgid(sbuf.st_gid);
if (!gbuf) {
msg(MSG_ERROR, "getgrgid failed for %s: gid %i not found\n",
path, (int)sbuf.st_gid);
return NULL;
}
mentry = mentry_alloc();
mentry->path = xstrdup(path);
mentry->pathlen = strlen(mentry->path);
mentry->owner = xstrdup(pbuf->pw_name);
mentry->group = xstrdup(gbuf->gr_name);
mentry->mode = sbuf.st_mode & 0177777;
if (with_mtime) {
mentry->mtime = sbuf.st_mtim.tv_sec;
mentry->mtimensec = sbuf.st_mtim.tv_nsec;
}
else {
mentry->mtime = 0;
mentry->mtimensec = MTIME_NONE;
}
/* symlinks have no xattrs */
if (S_ISLNK(mentry->mode))
return mentry;
#if !defined(NO_XATTR) || !(NO_XATTR+0)
lsize = listxattr(path, NULL, 0);
if (lsize < 0) {
/* Perhaps the FS doesn't support xattrs? */
if (errno == ENOTSUP)
return mentry;
msg(MSG_ERROR, "listxattr failed for %s: %s\n",
path, strerror(errno));
return NULL;
}
list = xmalloc(lsize);
lsize = listxattr(path, list, lsize);
if (lsize < 0) {
msg(MSG_ERROR, "listxattr failed for %s: %s\n",
path, strerror(errno));
free(list);
return NULL;
}
i = 0;
for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
if (*attr == '\0')
continue;
i++;
}
if (i == 0)
return mentry;
mentry->xattrs = i;
mentry->xattr_names = xmalloc(i * sizeof(char *));
mentry->xattr_values = xmalloc(i * sizeof(char *));
mentry->xattr_lvalues = xmalloc(i * sizeof(ssize_t));
i = 0;
for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
if (*attr == '\0')
continue;
mentry->xattr_names[i] = xstrdup(attr);
mentry->xattr_values[i] = NULL;
vsize = getxattr(path, attr, NULL, 0);
if (vsize < 0) {
msg(MSG_ERROR, "getxattr failed for %s: %s\n",
path, strerror(errno));
free(list);
mentry->xattrs = i + 1;
mentry_free(mentry);
return NULL;
}
mentry->xattr_lvalues[i] = vsize;
mentry->xattr_values[i] = xmalloc(vsize);
vsize = getxattr(path, attr, mentry->xattr_values[i], vsize);
if (vsize < 0) {
msg(MSG_ERROR, "getxattr failed for %s: %s\n",
path, strerror(errno));
free(list);
mentry->xattrs = i + 1;
mentry_free(mentry);
return NULL;
}
i++;
}
free(list);
#endif /* !NO_XATTR */
return mentry;
}
/* Cleans up a path and makes it relative to cwd unless it is absolute */
static char *
normalize_path(const char *orig)
{
char *real = realpath(orig, NULL);
char cwd[PATH_MAX];
char *result;
size_t cwdlen;
getcwd(cwd, PATH_MAX);
if (!real)
return NULL;
cwdlen = strlen(cwd);
/* If CWD=="/", make cwdlen=0 to not omit slash when building rel path. */
cwdlen -= !strcmp("/", cwd);
if (!strncmp(real, cwd, cwdlen)) {
result = xmalloc(strlen(real) - cwdlen + 1 + 1);
result[0] = '\0';
strcat(result, ".");
strcat(result, real + cwdlen);
} else {
result = xstrdup(real);
}
free(real);
return result;
}
/* Internal function for the recursive path walk */
static void
mentries_recurse(const char *path, struct metahash *mhash, msettings *st)
{
struct stat sbuf;
struct metaentry *mentry;
char tpath[PATH_MAX];
DIR *dir;
struct dirent *dent;
if (!path)
return;
if (lstat(path, &sbuf)) {
msg(MSG_ERROR, "lstat failed for %s: %s\n",
path, strerror(errno));
return;
}
mentry = mentry_create(path, st->do_mtime >= 0);
if (!mentry)
return;
mentry_insert(mentry, mhash);
if (S_ISDIR(sbuf.st_mode)) {
dir = opendir(path);
if (!dir) {
msg(MSG_ERROR, "opendir failed for %s: %s\n",
path, strerror(errno));
return;
}
while ((dent = readdir(dir))) {
if (!strcmp(dent->d_name, ".") ||
!strcmp(dent->d_name, "..") ||
(!st->do_git && !strcmp(dent->d_name, ".git"))
)
continue;
snprintf(tpath, PATH_MAX, "%s/%s", path, dent->d_name);
tpath[PATH_MAX - 1] = '\0';
mentries_recurse(tpath, mhash, st);
}
closedir(dir);
}
}
/* Recurses opath and adds metadata entries to the metaentry list */
void
mentries_recurse_path(const char *opath, struct metahash **mhash, msettings *st)
{
char *path = normalize_path(opath);
if (!(*mhash))
*mhash = mhash_alloc();
mentries_recurse(path, *mhash, st);
free(path);
}
/* Stores metaentries to a Format 0 file */
static void
mentries_tofile_v0(const struct metahash *mhash, FILE * to)
{
const struct metaentry *mentry;
int key;
unsigned i;
for (key = 0; key < HASH_INDEXES; key++) {
for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
write_string(mentry->path, to);
write_string(mentry->owner, to);
write_string(mentry->group, to);
write_int((uint64_t)mentry->mtime, 8, to);
write_int((uint64_t)mentry->mtimensec, 8, to);
write_int((uint64_t)mentry->mode, 2, to);
write_int(mentry->xattrs, 4, to);
for (i = 0; i < mentry->xattrs; i++) {
write_string(mentry->xattr_names[i], to);
write_int(mentry->xattr_lvalues[i], 4, to);
write_binary_string(mentry->xattr_values[i],
mentry->xattr_lvalues[i], to);
}
}
}
}
/* Compare two mentries by path */
static int
mentry_cmp_path(const struct metaentry *a, const struct metaentry *b)
{
return strcmp(a->path, b->path);
}
/* Compare two mentries by path, as is sutiable for qsort(3) */
static int
mentry_cmp_path_q(const void *a, const void *b)
{
return mentry_cmp_path(*((const struct metaentry **) a),
*((const struct metaentry **) b));
}
/* Stores metaentries to a Format 1 file */
static void
mentries_tofile_v1(const struct metahash *mhash, FILE * to)
{
const struct metaentry *mentry;
const struct metaentry **entarr;
int key, entries = 0;
struct tm tm;
char tmbuf[20];
unsigned i;
for (key = 0; key < HASH_INDEXES; key++) {
for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
++entries;
}
}
entarr = xmalloc(entries * sizeof(struct metaentry *));
i = 0;
for (key = 0; key < HASH_INDEXES; key++) {
for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
entarr[i++] = mentry;
}
}
qsort(entarr, entries, sizeof(struct metaentry *), mentry_cmp_path_q);
fputc('\n', to);
for (key = 0; key < entries; key++) {
mentry = entarr[key];
write_string_url(mentry->path, to);
fputc('\t', to);
write_string_url(mentry->owner, to);
fputc('\t', to);
write_string_url(mentry->group, to);
fputc('\t', to);
fprintf(to, "%.6o", mentry->mode);
fputc('\t', to);
if (mentry->mtimensec != MTIME_NONE) {
gmtime_r(&mentry->mtime, &tm);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%dT%H:%M:%S", &tm);
fputs(tmbuf, to);
fputc('.', to);
fprintf(to, "%.9ldZ", mentry->mtimensec);
}
else {
fputc('0', to);
}
for (i = 0; i < mentry->xattrs; i++) {
fputc('\t', to);
write_string_url(mentry->xattr_names[i], to);
fputc('\t', to);
write_binary_url(mentry->xattr_values[i],
mentry->xattr_lvalues[i], to);
}
fputc('\n', to);
}
free(entarr);
}
/* Stores metaentries to a file */
void
mentries_tofile(const struct metahash *mhash, const char *path, int format)
{
FILE *to;
char const * formatstr;
void(*tofile)(const struct metahash *, FILE *);
switch (format) {
case 0: formatstr = VERSION_0; tofile = mentries_tofile_v0; break;
case 1: formatstr = VERSION_1; tofile = mentries_tofile_v1; break;
default:
msg(MSG_CRITICAL, "Unknown format %d\n", format);
exit(EXIT_FAILURE);
}
to = fopen(path, "w");
if (!to) {
msg(MSG_CRITICAL, "Failed to open %s: %s\n",
path, strerror(errno));
exit(EXIT_FAILURE);
}
write_binary_string(SIGNATURE, SIGNATURELEN, to);
write_binary_string(formatstr, VERSIONLEN, to);
tofile(mhash, to);
fclose(to);
}
/* Creates a metaentry list from a Format 0 file buffer */
static int
mentries_fromfile_v0(struct metahash **mhash, const char *ptr, const char *max)
{
struct metaentry *mentry;
unsigned i;
while (ptr < max) {
if (*ptr == '\0') {
return -1;
}
mentry = mentry_alloc();
mentry->path = read_string(&ptr, max);
mentry->pathlen = strlen(mentry->path);
mentry->owner = read_string(&ptr, max);
mentry->group = read_string(&ptr, max);
mentry->mtime = (time_t)read_int(&ptr, 8, max);
mentry->mtimensec = (time_t)read_int(&ptr, 8, max);
mentry->mode = (mode_t)read_int(&ptr, 2, max);
mentry->xattrs = (unsigned)read_int(&ptr, 4, max);
if (!mentry->xattrs) {
mentry_insert(mentry, *mhash);
continue;
}
mentry->xattr_names = xmalloc(mentry->xattrs * sizeof(char *));
mentry->xattr_lvalues = xmalloc(mentry->xattrs * sizeof(ssize_t));
mentry->xattr_values = xmalloc(mentry->xattrs * sizeof(char *));
for (i = 0; i < mentry->xattrs; i++) {
mentry->xattr_names[i] = read_string(&ptr, max);
mentry->xattr_lvalues[i] = (int)read_int(&ptr, 4, max);
mentry->xattr_values[i] = read_binary_string(
&ptr,
mentry->xattr_lvalues[i],
max
);
}
mentry_insert(mentry, *mhash);
}
return 0;
}
/* Creates a metaentry list from a Format 1 file buffer */
static int
mentries_fromfile_v1(struct metahash **mhash, const char *ptr, const char *max)
{
struct metaentry *mentry;
int i;
char *mtime = NULL, *nsec, *tz;
struct tm tm;
if (read_char(&ptr, max) != '\n')
return -1;
while (ptr < max) {
mentry = mentry_alloc();
if ((mentry->path = read_string_url(&ptr, max)) == NULL)
goto err;
mentry->pathlen = strlen(mentry->path);
if (read_char(&ptr, max) != '\t')
goto err;
mentry->owner = read_string_url(&ptr, max);
if (read_char(&ptr, max) != '\t')
goto err;
mentry->group = read_string_url(&ptr, max);
if (read_char(&ptr, max) != '\t')
goto err;
mentry->mode = read_int_string(&ptr, max, 8);
/* Read the whole mtime string into `mtime`. */
if (read_char(&ptr, max) != '\t')
goto err;
if ((mtime = read_string_url(&ptr, max)) == NULL)
goto err;
if (strcmp(mtime, "0") == 0) {
mentry->mtime = 0;
mentry->mtimensec = MTIME_NONE;
}
else {
/* Get the time_t part of `mtime` into mentry->mtime */
if ((nsec = strptime(mtime, "%Y-%m-%dT%H:%M:%S", &tm)) == NULL)
goto err;
tm.tm_isdst = 0;
mentry->mtime = timegm(&tm);
/* Check if there's a nanosecond portion of `mtime` */
if (*nsec == '.') {
/* There is a decimal point in mtime. Get nanoseconds. */
++nsec;
mentry->mtimensec = strtol(nsec, &tz, 10);
i = tz - nsec;
while (i < 9) {
/* Too few digits for nanosecond precision.
* e.g. "...T10:15:23.5", which we've parsed as
* 5 nanoseconds, but is actually half a second
* or 500000000 nanoseconds.
* Scale to correct value.
*/
mentry->mtimensec *= 10;
++i;
}
while (i > 9) {
/* Too many digits for nanosecond precision.
* Scale to correct value
*/
mentry->mtimensec /= 10;
--i;
}
}
else {
/* No decimal point. Set nanoseconds to zero */
mentry->mtimensec = 0;
tz = nsec;
}
/* Check for a 'Z' (Zulu/UTC) timesone specifier in `mtime`. */
if (*tz == 'Z')
++tz;
/* Check that we've reached the end of `mtime` */
if (*tz != '\0')
goto err;
}
free(mtime);
mtime = NULL;
mentry->xattrs = 0;
mentry->xattr_names = NULL;
mentry->xattr_lvalues = NULL;
mentry->xattr_values = NULL;
while ((i = read_char(&ptr, max)) == '\t') {
mentry->xattr_names = xrealloc(mentry->xattr_names,
(mentry->xattrs + 1) * sizeof(char *));
mentry->xattr_lvalues = xrealloc(mentry->xattr_lvalues,
(mentry->xattrs + 1) * sizeof(ssize_t));
mentry->xattr_values = xrealloc(mentry->xattr_values,
(mentry->xattrs + 1) * sizeof(char *));
mentry->xattr_names[mentry->xattrs] = read_string_url(&ptr, max);
read_char(&ptr, max);
mentry->xattr_values[mentry->xattrs] = read_binary_url(&ptr, max,
&mentry->xattr_lvalues[mentry->xattrs]);
++mentry->xattrs;
}
if (i != '\n')
goto err;
mentry_insert(mentry, *mhash);
}
return 0;
err:
if (mtime)
free(mtime);
mentry_free(mentry);
return -1;
}
/* Creates a metaentry list from a file */
void
mentries_fromfile(struct metahash **mhash, const char *path)
{
char *mmapstart;
char const *ptr;
char const *max;
int fd;
struct stat sbuf;
int i;
if (!(*mhash))
*mhash = mhash_alloc();
fd = open(path, O_RDONLY);
if (fd < 0) {
msg(MSG_CRITICAL, "Failed to open %s: %s\n",
path, strerror(errno));
exit(EXIT_FAILURE);
}
if (fstat(fd, &sbuf)) {
msg(MSG_CRITICAL, "Failed to stat %s: %s\n",
path, strerror(errno));
exit(EXIT_FAILURE);
}
if (sbuf.st_size < (SIGNATURELEN + VERSIONLEN)) {
msg(MSG_CRITICAL, "File %s has an invalid size\n", path);
exit(EXIT_FAILURE);
}
mmapstart = mmap(NULL, (size_t)sbuf.st_size, PROT_READ,
MAP_SHARED, fd, 0);
if (mmapstart == MAP_FAILED) {
msg(MSG_CRITICAL, "Unable to mmap %s: %s\n",
path, strerror(errno));
exit(EXIT_FAILURE);
}
ptr = mmapstart;
max = mmapstart + sbuf.st_size;
if (strncmp(ptr, SIGNATURE, SIGNATURELEN)) {
msg(MSG_CRITICAL, "Invalid signature for file %s\n", path);
goto out;
}
ptr += SIGNATURELEN;
if (strncmp(ptr, VERSION_0, VERSIONLEN) == 0) {
i = mentries_fromfile_v0(mhash, ptr + VERSIONLEN, max);
}
else if (strncmp(ptr, VERSION_1, VERSIONLEN) == 0) {
i = mentries_fromfile_v1(mhash, ptr + VERSIONLEN, max);
}
else {
msg(MSG_CRITICAL, "Invalid version of file %s\n", path);
goto out;
}
if (i != 0) {
msg(MSG_CRITICAL, "Invalid characters in file %s\n", path);
goto out;
}
out:
munmap(mmapstart, sbuf.st_size);
close(fd);
}
/* Searches haystack for an xattr matching xattr number n in needle */
int
mentry_find_xattr(struct metaentry *haystack, struct metaentry *needle,
unsigned n)
{
unsigned i;
for (i = 0; i < haystack->xattrs; i++) {
if (strcmp(haystack->xattr_names[i], needle->xattr_names[n]))
continue;
if (haystack->xattr_lvalues[i] != needle->xattr_lvalues[n])
return -1;
if (bcmp(haystack->xattr_values[i], needle->xattr_values[n],
needle->xattr_lvalues[n])
)
return -1;
return i;
}
return -1;
}
/* Returns zero if all xattrs in left and right match */
static int
mentry_compare_xattr(struct metaentry *left, struct metaentry *right)
{
unsigned i;
if (left->xattrs != right->xattrs)
return 1;
/* Make sure all xattrs in left are found in right and vice versa */
for (i = 0; i < left->xattrs; i++) {
if ( mentry_find_xattr(right, left, i) < 0
|| mentry_find_xattr(left, right, i) < 0
) {
return 1;
}
}
return 0;
}
/* Compares two metaentries and returns an int with a bitmask of differences */
int
mentry_compare(struct metaentry *left, struct metaentry *right, msettings *st)
{
int retval = DIFF_NONE;
if (!left || !right) {
msg(MSG_ERROR, "%s called with empty list\n", __func__);
return -1;
}
if (strcmp(left->path, right->path))
return -1;
if (strcmp(left->owner, right->owner))
retval |= DIFF_OWNER;
if (strcmp(left->group, right->group))
retval |= DIFF_GROUP;
if ((left->mode & 07777) != (right->mode & 07777))
retval |= DIFF_MODE;
if ((left->mode & S_IFMT) != (right->mode & S_IFMT))
retval |= DIFF_TYPE;
if (st->do_mtime > 0
&& left->mtimensec != MTIME_NONE
&& right->mtimensec != MTIME_NONE
&& strcmp(left->path, st->metafile)
&& (left->mtime != right->mtime
|| left->mtimensec != right->mtimensec))
retval |= DIFF_MTIME;
if (mentry_compare_xattr(left, right)) {
retval |= DIFF_XATTR;
return retval;
}
return retval;
}
/* Compares lists of real and stored metadata and calls pfunc for each */
int
mentries_compare(struct metahash *mhashreal,
struct metahash *mhashstored,
void (*pfunc)
(struct metaentry *real, struct metaentry *stored, int cmp),
msettings *st)
{
struct metaentry *real, *stored;
int key;
int cmp, diffs = 0;
if (!mhashreal || !mhashstored) {
msg(MSG_ERROR, "%s called with empty list\n", __func__);
return -1;
}
for (key = 0; key < HASH_INDEXES; key++) {
for (real = mhashreal->bucket[key]; real; real = real->next) {
stored = mentry_find(real->path, mhashstored);
if (!stored)
cmp = DIFF_ADDED;
else
cmp = mentry_compare(real, stored, st);
if (cmp > 0)
++diffs;
pfunc(real, stored, cmp);
}
for (stored = mhashstored->bucket[key]; stored; stored = stored->next) {
real = mentry_find(stored->path, mhashreal);
if (!real) {
++diffs;
pfunc(NULL, stored, DIFF_DELE);
}
}
}
return diffs;
}
/* Dumps given metadata */
void
mentries_dump(struct metahash *mhash, int with_mtime)
{
const struct metaentry *mentry;
char mode[11 + 1] = "";
char date[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = "";
char nsec[1 + 9 + 1] = "";
char zone[1 + 5 + 1] = "";
struct tm cal;
for (int key = 0; key < HASH_INDEXES; key++) {
for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
strmode(mentry->mode, mode);
if (!with_mtime) {
date[0] = '\0';
nsec[0] = '\0';
zone[0] = '\0';
}
else if (mentry->mtimensec == MTIME_NONE) {
snprintf(date, sizeof(date), "\t%19s", "");
snprintf(nsec, sizeof(nsec), "%10s", "");
snprintf(zone, sizeof(zone), "%6s", "");
}
else {
localtime_r(&mentry->mtime, &cal);
strftime(date, sizeof(date), "\t%F %T", &cal);
snprintf(nsec, sizeof(nsec), ".%09ld", mentry->mtimensec);
strftime(zone, sizeof(zone), " %z", &cal);
}
printf("%s\t%s\t%s%s%s%s\t%s%s\n",
mode,
mentry->owner, mentry->group,
date, nsec, zone,
mentry->path, S_ISDIR(mentry->mode) ? "/" : "");
for (unsigned i = 0; i < mentry->xattrs; i++) {
printf("\t\t\t\t%s%s\t%s=",
mentry->path, S_ISDIR(mentry->mode) ? "/" : "",
mentry->xattr_names[i]);
ssize_t p = 0;
for (; p < mentry->xattr_lvalues[i]; p++) {
const char ch = mentry->xattr_values[i][p];
if ((unsigned)(ch - 32) > 126 - 32) {
p = -1;
break;
}
}
if (p >= 0)
printf("\"%.*s\"\n",
(int)mentry->xattr_lvalues[i],
mentry->xattr_values[i]);
else {
printf("0x");
for (p = 0; p < mentry->xattr_lvalues[i]; p++)
printf("%02hhx", (char)mentry->xattr_values[i][p]);
printf("\n");
}
}
}
}
}
|