-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathflist.c
1579 lines (1369 loc) · 35.9 KB
/
flist.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
/*
* Copyright (c) 2019 Kristaps Dzonsons <[email protected]>
* Copyright (c) 2019 Florian Obser <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/types.h>
#include COMPAT_MAJOR_MINOR_H
#include <sys/param.h>
#include <sys/stat.h>
#ifdef __sun
# include <sys/mkdev.h>
#endif
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#if HAVE_FTS
# include <fts.h>
#endif
#include <limits.h>
#include <inttypes.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
/*
* We allocate our file list in chunk sizes so as not to do it one by
* one.
* Preferably we get one or two allocation.
*/
#define FLIST_CHUNK_SIZE (1024)
/*
* These flags are part of the rsync protocol.
* They are sent as the first byte for a file transmission and encode
* information that affects subsequent transmissions.
*/
#define FLIST_TOP_LEVEL 0x0001 /* needed for remote --delete */
#define FLIST_MODE_SAME 0x0002 /* mode is repeat */
#define FLIST_RDEV_SAME 0x0004 /* rdev is repeat */
#define FLIST_UID_SAME 0x0008 /* uid is repeat */
#define FLIST_GID_SAME 0x0010 /* gid is repeat */
#define FLIST_NAME_SAME 0x0020 /* name is repeat */
#define FLIST_NAME_LONG 0x0040 /* name >255 bytes */
#define FLIST_TIME_SAME 0x0080 /* time is repeat */
/*
* Required way to sort a filename list.
*/
static int
flist_cmp(const void *p1, const void *p2)
{
const struct flist *f1 = p1, *f2 = p2;
return strcmp(f1->wpath, f2->wpath);
}
/*
* Deduplicate our file list (which may be zero-length).
* Returns zero on failure, non-zero on success.
*/
static int
flist_dedupe(struct flist **fl, size_t *sz)
{
size_t i, j;
struct flist *new;
struct flist *f, *fnext;
if (*sz == 0)
return 1;
/* Create a new buffer, "new", and copy. */
new = calloc(*sz, sizeof(struct flist));
if (new == NULL) {
ERR("calloc");
return 0;
}
for (i = j = 0; i < *sz - 1; i++) {
f = &(*fl)[i];
fnext = &(*fl)[i + 1];
if (strcmp(f->wpath, fnext->wpath)) {
new[j++] = *f;
continue;
}
/*
* Our working (destination) paths are the same.
* If the actual file is the same (as given on the
* command-line), then we can just discard the first.
* Otherwise, we need to bail out: it means we have two
* different files with the relative path on the
* destination side.
*/
if (strcmp(f->path, fnext->path) == 0) {
new[j++] = *f;
i++;
WARNX("%s: duplicate path: %s",
f->wpath, f->path);
free(fnext->path);
free(fnext->link);
fnext->path = fnext->link = NULL;
continue;
}
ERRX("%s: duplicate working path for "
"possibly different file: %s, %s",
f->wpath, f->path, fnext->path);
free(new);
return 0;
}
/* Don't forget the last entry. */
if (i == *sz - 1)
new[j++] = (*fl)[i];
/*
* Reassign to the deduplicated array.
* If we started out with *sz > 0, which we check for at the
* beginning, then we'll always continue having *sz > 0.
*/
free(*fl);
*fl = new;
*sz = j;
assert(*sz);
return 1;
}
/*
* We're now going to find our top-level directories.
* This only applies to recursive mode.
* If we have the first element as the ".", then that's the "top
* directory" of our transfer.
* Otherwise, mark up all top-level directories in the set.
* XXX: the FLIST_TOP_LEVEL flag should indicate what is and what isn't
* a top-level directory, but I'm not sure if GPL rsync(1) respects it
* the same way.
*/
static void
flist_topdirs(struct sess *sess, struct flist *fl, size_t flsz)
{
size_t i;
const char *cp;
if (!sess->opts->recursive)
return;
if (flsz && strcmp(fl[0].wpath, ".")) {
for (i = 0; i < flsz; i++) {
if (!S_ISDIR(fl[i].st.mode))
continue;
cp = strchr(fl[i].wpath, '/');
if (cp != NULL && cp[1] != '\0')
continue;
fl[i].st.flags |= FLSTAT_TOP_DIR;
LOG4("%s: top-level", fl[i].wpath);
}
} else if (flsz) {
fl[0].st.flags |= FLSTAT_TOP_DIR;
LOG4("%s: top-level", fl[0].wpath);
}
}
/*
* Filter through the fts() file information.
* We want directories (pre-order), regular files, and symlinks.
* Everything else is skipped and possibly warned about.
* Return zero to skip, non-zero to examine.
*/
static int
flist_fts_check(struct sess *sess, FTSENT *ent)
{
if (ent->fts_info == FTS_F ||
ent->fts_info == FTS_D ||
ent->fts_info == FTS_SL ||
ent->fts_info == FTS_SLNONE)
return 1;
if (ent->fts_info == FTS_DC) {
WARNX("%s: directory cycle", ent->fts_path);
} else if (ent->fts_info == FTS_DNR) {
errno = ent->fts_errno;
WARN("%s: unreadable directory", ent->fts_path);
} else if (ent->fts_info == FTS_DOT) {
WARNX("%s: skipping dot-file", ent->fts_path);
} else if (ent->fts_info == FTS_ERR) {
errno = ent->fts_errno;
WARN("%s", ent->fts_path);
} else if (ent->fts_info == FTS_DEFAULT) {
if ((sess->opts->devices && (S_ISBLK(ent->fts_statp->st_mode) ||
S_ISCHR(ent->fts_statp->st_mode))) ||
(sess->opts->specials &&
(S_ISFIFO(ent->fts_statp->st_mode) ||
S_ISSOCK(ent->fts_statp->st_mode)))) {
return 1;
}
WARNX("%s: skipping special", ent->fts_path);
} else if (ent->fts_info == FTS_NS) {
errno = ent->fts_errno;
WARN("%s: could not stat", ent->fts_path);
}
return 0;
}
/*
* Copy necessary elements in "st" into the fields of "f".
*/
static void
flist_copy_stat(struct flist *f, const struct stat *st)
{
f->st.mode = st->st_mode;
f->st.uid = st->st_uid;
f->st.gid = st->st_gid;
f->st.size = st->st_size;
f->st.mtime = st->st_mtime;
f->st.rdev = st->st_rdev;
}
void
flist_free(struct flist *f, size_t sz)
{
size_t i;
if (f == NULL)
return;
for (i = 0; i < sz; i++) {
free(f[i].path);
free(f[i].link);
}
free(f);
}
/*
* Serialise our file list (which may be zero-length) to the wire.
* Makes sure that the receiver isn't going to block on sending us
* return messages on the log channel.
* Return zero on failure, non-zero on success.
*/
int
flist_send(struct sess *sess, int fdin, int fdout, const struct flist *fl,
size_t flsz)
{
size_t i, sz, gidsz = 0, uidsz = 0;
uint8_t flag;
const struct flist *f;
const char *fn;
struct ident *gids = NULL, *uids = NULL;
int rc = 0;
/* Double-check that we've no pending multiplexed data. */
LOG2("sending file metadata list: %zu", flsz);
for (i = 0; i < flsz; i++) {
f = &fl[i];
fn = f->wpath;
sz = strlen(f->wpath);
assert(sz > 0);
assert(sz < INT32_MAX);
/*
* If applicable, unclog the read buffer.
* This happens when the receiver has a lot of log
* messages and all we're doing is sending our file list
* without checking for messages.
*/
if (sess->mplex_reads &&
io_read_check(fdin) &&
!io_read_flush(sess, fdin)) {
ERRX1("io_read_flush");
goto out;
}
/*
* For ease, make all of our filenames be "long"
* regardless their actual length.
* This also makes sure that we don't transmit a zero
* byte unintentionally.
*/
flag = FLIST_NAME_LONG;
if ((FLSTAT_TOP_DIR & f->st.flags))
flag |= FLIST_TOP_LEVEL;
LOG3("%s: sending file metadata: "
"size %jd, mtime %jd, mode %o",
fn, (intmax_t)f->st.size,
(intmax_t)f->st.mtime, f->st.mode);
/* Now write to the wire. */
/* FIXME: buffer this. */
if (!io_write_byte(sess, fdout, flag)) {
ERRX1("io_write_byte");
goto out;
} else if (!io_write_int(sess, fdout, sz)) {
ERRX1("io_write_int");
goto out;
} else if (!io_write_buf(sess, fdout, fn, sz)) {
ERRX1("io_write_buf");
goto out;
} else if (!io_write_long(sess, fdout, f->st.size)) {
ERRX1("io_write_long");
goto out;
} else if (!io_write_uint(sess, fdout, (uint32_t)f->st.mtime)) {
ERRX1("io_write_uint");
goto out;
} else if (!io_write_uint(sess, fdout, f->st.mode)) {
ERRX1("io_write_uint");
goto out;
}
/* Conditional part: uid. */
if (sess->opts->preserve_uids) {
if (!io_write_uint(sess, fdout, f->st.uid)) {
ERRX1("io_write_uint");
goto out;
}
if (!idents_add(0, &uids, &uidsz, f->st.uid)) {
ERRX1("idents_add");
goto out;
}
}
/* Conditional part: gid. */
if (sess->opts->preserve_gids) {
if (!io_write_uint(sess, fdout, f->st.gid)) {
ERRX1("io_write_uint");
goto out;
}
if (!idents_add(1, &gids, &gidsz, f->st.gid)) {
ERRX1("idents_add");
goto out;
}
}
/* Conditional part: devices & special files. */
if ((sess->opts->devices && (S_ISBLK(f->st.mode) ||
S_ISCHR(f->st.mode))) ||
(sess->opts->specials && (S_ISFIFO(f->st.mode) ||
S_ISSOCK(f->st.mode)))) {
if (!io_write_int(sess, fdout, f->st.rdev)) {
ERRX1("io_write_int");
goto out;
}
}
/* Conditional part: link. */
if (S_ISLNK(f->st.mode) &&
sess->opts->preserve_links) {
fn = f->link;
sz = strlen(f->link);
assert(sz < INT32_MAX);
if (!io_write_int(sess, fdout, sz)) {
ERRX1("io_write_int");
goto out;
}
if (!io_write_buf(sess, fdout, fn, sz)) {
ERRX1("io_write_buf");
goto out;
}
}
if (S_ISREG(f->st.mode))
sess->total_size += f->st.size;
}
/* Signal end of file list. */
if (!io_write_byte(sess, fdout, 0)) {
ERRX1("io_write_byte");
goto out;
}
/* Conditionally write identifier lists. */
if (sess->opts->preserve_uids && !sess->opts->numeric_ids) {
LOG2("sending uid list: %zu", uidsz);
if (!idents_send(sess, fdout, uids, uidsz)) {
ERRX1("idents_send");
goto out;
}
}
if (sess->opts->preserve_gids && !sess->opts->numeric_ids) {
LOG2("sending gid list: %zu", gidsz);
if (!idents_send(sess, fdout, gids, gidsz)) {
ERRX1("idents_send");
goto out;
}
}
rc = 1;
out:
idents_free(gids, gidsz);
idents_free(uids, uidsz);
return rc;
}
/*
* Read the filename of a file list.
* This is the most expensive part of the file list transfer, so a lot
* of attention has gone into transmitting as little as possible.
* Micro-optimisation, but whatever.
* Fills in "f" with the full path on success.
* Returns zero on failure, non-zero on success.
*/
static int
flist_recv_name(struct sess *sess, int fd, struct flist *f, uint8_t flags,
char last[PATH_MAX])
{
uint8_t bval;
size_t partial = 0;
size_t pathlen = 0, len;
/*
* Read our filename.
* If we have FLIST_NAME_SAME, we inherit some of the last
* transmitted name.
* If we have FLIST_NAME_LONG, then the string length is greater
* than byte-size.
*/
if (flags & FLIST_NAME_SAME) {
if (!io_read_byte(sess, fd, &bval)) {
ERRX1("io_read_byte");
return 0;
}
partial = bval;
}
/* Get the (possibly-remaining) filename length. */
if (flags & FLIST_NAME_LONG) {
if (!io_read_size(sess, fd, &pathlen)) {
ERRX1("io_read_size");
return 0;
}
} else {
if (!io_read_byte(sess, fd, &bval)) {
ERRX1("io_read_byte");
return 0;
}
pathlen = bval;
}
/* Allocate our full filename length. */
/* FIXME: maximum pathname length. */
if ((len = pathlen + partial) == 0) {
ERRX("security violation: zero-length pathname");
return 0;
}
if ((f->path = malloc(len + 1)) == NULL) {
ERR("malloc");
return 0;
}
f->path[len] = '\0';
if (flags & FLIST_NAME_SAME)
memcpy(f->path, last, partial);
if (!io_read_buf(sess, fd, f->path + partial, pathlen)) {
ERRX1("io_read_buf");
return 0;
}
if (f->path[0] == '/') {
ERRX("security violation: absolute pathname: %s",
f->path);
return 0;
}
if (strstr(f->path, "/../") != NULL ||
(len > 2 && strcmp(f->path + len - 3, "/..") == 0) ||
(len > 2 && strncmp(f->path, "../", 3) == 0) ||
strcmp(f->path, "..") == 0) {
ERRX("%s: security violation: backtracking pathname",
f->path);
return 0;
}
/* Record our last path and construct our filename. */
strlcpy(last, f->path, PATH_MAX);
f->wpath = f->path;
return 1;
}
/*
* Reallocate a file list in chunks of FLIST_CHUNK_SIZE;
* Returns zero on failure, non-zero on success.
*/
static int
flist_realloc(struct flist **fl, size_t *sz, size_t *max)
{
void *pp;
if (*sz + 1 <= *max) {
(*sz)++;
return 1;
}
pp = recallocarray(*fl, *max,
*max + FLIST_CHUNK_SIZE, sizeof(struct flist));
if (pp == NULL) {
ERR("recallocarray");
return 0;
}
*fl = pp;
*max += FLIST_CHUNK_SIZE;
(*sz)++;
return 1;
}
/*
* Copy a regular or symbolic link file "path" into "f".
* This handles the correct path creation and symbolic linking.
* Returns zero on failure, non-zero on success.
*/
static int
flist_append(struct flist *f, struct stat *st, const char *path)
{
/*
* Copy the full path for local addressing and transmit
* only the filename part for the receiver.
*/
if ((f->path = strdup(path)) == NULL) {
ERR("strdup");
return 0;
}
if ((f->wpath = strrchr(f->path, '/')) == NULL)
f->wpath = f->path;
else
f->wpath++;
/*
* On the receiving end, we'll strip out all bits on the
* mode except for the file permissions.
* No need to warn about it here.
*/
flist_copy_stat(f, st);
/* Optionally copy link information. */
if (S_ISLNK(st->st_mode)) {
f->link = symlink_read(f->path);
if (f->link == NULL) {
ERRX1("symlink_read");
return 0;
}
}
return 1;
}
/*
* Receive a file list from the wire, filling in length "sz" (which may
* possibly be zero) and list "flp" on success.
* Return zero on failure, non-zero on success.
*/
int
flist_recv(struct sess *sess, int fd, struct flist **flp, size_t *sz)
{
struct flist *fl = NULL;
struct flist *ff;
const struct flist *fflast = NULL;
size_t flsz = 0, flmax = 0, lsz, gidsz = 0, uidsz = 0;
uint8_t flag;
char last[PATH_MAX];
int64_t lval; /* temporary values... */
int32_t ival;
uint32_t uival;
struct ident *gids = NULL, *uids = NULL;
last[0] = '\0';
for (;;) {
if (!io_read_byte(sess, fd, &flag)) {
ERRX1("io_read_byte");
goto out;
} else if (flag == 0)
break;
if (!flist_realloc(&fl, &flsz, &flmax)) {
ERRX1("flist_realloc");
goto out;
}
ff = &fl[flsz - 1];
fflast = flsz > 1 ? &fl[flsz - 2] : NULL;
/* Filename first. */
if (!flist_recv_name(sess, fd, ff, flag, last)) {
ERRX1("flist_recv_name");
goto out;
}
/* Read the file size. */
if (!io_read_long(sess, fd, &lval)) {
ERRX1("io_read_long");
goto out;
}
ff->st.size = lval;
/* Read the modification time. */
if (!(flag & FLIST_TIME_SAME)) {
if (!io_read_uint(sess, fd, &uival)) {
ERRX1("io_read_uint");
goto out;
}
ff->st.mtime = uival; /* beyond 2038 */
} else if (fflast == NULL) {
ff->st.mtime = 0;
} else
ff->st.mtime = fflast->st.mtime;
/* Read the file mode. */
if (!(flag & FLIST_MODE_SAME)) {
if (!io_read_uint(sess, fd, &uival)) {
ERRX1("io_read_uint");
goto out;
}
ff->st.mode = uival;
} else if (fflast == NULL) {
ff->st.mode = 0;
} else
ff->st.mode = fflast->st.mode;
/* Conditional part: uid. */
if (sess->opts->preserve_uids) {
if (!(flag & FLIST_UID_SAME)) {
if (!io_read_uint(sess, fd, &uival)) {
ERRX1("io_read_int");
goto out;
}
ff->st.uid = uival;
} else if (fflast == NULL) {
ff->st.uid = 0;
} else
ff->st.uid = fflast->st.uid;
}
/* Conditional part: gid. */
if (sess->opts->preserve_gids) {
if (!(flag & FLIST_GID_SAME)) {
if (!io_read_uint(sess, fd, &uival)) {
ERRX1("io_read_uint");
goto out;
}
ff->st.gid = uival;
} else if (fflast == NULL) {
ff->st.gid = 0;
} else
ff->st.gid = fflast->st.gid;
}
/* Conditional part: devices & special files. */
if ((sess->opts->devices && (S_ISBLK(ff->st.mode) ||
S_ISCHR(ff->st.mode))) ||
(sess->opts->specials && (S_ISFIFO(ff->st.mode) ||
S_ISSOCK(ff->st.mode)))) {
if (!(flag & FLIST_RDEV_SAME)) {
if (!io_read_int(sess, fd, &ival)) {
ERRX1("io_read_int");
goto out;
}
ff->st.rdev = ival;
} else if (fflast == NULL) {
ff->st.rdev = 0;
} else
ff->st.rdev = fflast->st.rdev;
}
/* Conditional part: link. */
if (S_ISLNK(ff->st.mode) &&
sess->opts->preserve_links) {
if (!io_read_size(sess, fd, &lsz)) {
ERRX1("io_read_size");
goto out;
} else if (lsz == 0) {
ERRX("empty link name");
goto out;
}
ff->link = calloc(lsz + 1, 1);
if (ff->link == NULL) {
ERR("calloc");
goto out;
}
if (!io_read_buf(sess, fd, ff->link, lsz)) {
ERRX1("io_read_buf");
goto out;
}
}
LOG3("%s: received file metadata: "
"size %jd, mtime %jd, mode %o, rdev (%d, %d)",
ff->path, (intmax_t)ff->st.size,
(intmax_t)ff->st.mtime, ff->st.mode,
major(ff->st.rdev), minor(ff->st.rdev));
if (S_ISREG(ff->st.mode))
sess->total_size += ff->st.size;
}
/* Conditionally read the user/group list. */
if (sess->opts->preserve_uids && !sess->opts->numeric_ids) {
if (!idents_recv(sess, fd, &uids, &uidsz)) {
ERRX1("idents_recv");
goto out;
}
LOG2("received uid list: %zu", uidsz);
}
if (sess->opts->preserve_gids && !sess->opts->numeric_ids) {
if (!idents_recv(sess, fd, &gids, &gidsz)) {
ERRX1("idents_recv");
goto out;
}
LOG2("received gid list: %zu", gidsz);
}
/* Remember to order the received list. */
LOG2("received file metadata list: %zu", flsz);
qsort(fl, flsz, sizeof(struct flist), flist_cmp);
flist_topdirs(sess, fl, flsz);
*sz = flsz;
*flp = fl;
/* Conditionally remap and reassign identifiers. */
if (sess->opts->preserve_uids && !sess->opts->numeric_ids) {
idents_remap(sess, 0, uids, uidsz);
idents_assign_uid(sess, fl, flsz, uids, uidsz);
}
if (sess->opts->preserve_gids && !sess->opts->numeric_ids) {
idents_remap(sess, 1, gids, gidsz);
idents_assign_gid(sess, fl, flsz, gids, gidsz);
}
idents_free(gids, gidsz);
idents_free(uids, uidsz);
return 1;
out:
flist_free(fl, flsz);
idents_free(gids, gidsz);
idents_free(uids, uidsz);
*sz = 0;
*flp = NULL;
return 0;
}
/*
* Generate a flist possibly-recursively given a file root, which may
* also be a regular file or symlink.
* On success, augments the generated list in "flp" of length "sz".
* Returns zero on failure, non-zero on success.
*/
static int
flist_gen_dirent(struct sess *sess, char *root, struct flist **fl, size_t *sz,
size_t *max)
{
char *cargv[2], *cp;
int rc = 0, flag;
FTS *fts;
FTSENT *ent;
struct flist *f;
size_t i, flsz = 0, nxdev = 0, stripdir;
dev_t *newxdev, *xdev = NULL;
struct stat st;
cargv[0] = root;
cargv[1] = NULL;
/*
* If we're a file, then revert to the same actions we use for
* the non-recursive scan.
*/
if (lstat(root, &st) == -1) {
ERR("%s: lstat", root);
return 0;
} else if (S_ISREG(st.st_mode)) {
/* filter files */
if (rules_match(root, 0) == -1) {
WARNX("%s: skipping excluded file", root);
return 1;
}
if (!flist_realloc(fl, sz, max)) {
ERRX1("flist_realloc");
return 0;
}
f = &(*fl)[(*sz) - 1];
assert(f != NULL);
if (!flist_append(f, &st, root)) {
ERRX1("flist_append");
return 0;
}
return 1;
} else if (S_ISLNK(st.st_mode)) {
if (!sess->opts->preserve_links) {
WARNX("%s: skipping symlink", root);
return 1;
}
/* filter files */
if (rules_match(root, 0) == -1) {
WARNX("%s: skipping excluded symlink", root);
return 1;
}
if (!flist_realloc(fl, sz, max)) {
ERRX1("flist_realloc");
return 0;
}
f = &(*fl)[(*sz) - 1];
assert(f != NULL);
if (!flist_append(f, &st, root)) {
ERRX1("flist_append");
return 0;
}
return 1;
} else if (!S_ISDIR(st.st_mode)) {
WARNX("%s: skipping special", root);
return 1;
}
/*
* If we end with a slash, it means that we're not supposed to
* copy the directory part itself---only the contents.
* So set "stripdir" to be what we take out.
*/
stripdir = strlen(root);
assert(stripdir > 0);
if (root[stripdir - 1] != '/')
stripdir = 0;
/*
* If we're not stripping anything, then see if we need to strip
* out the leading material in the path up to and including the
* last directory component.
*/
if (stripdir == 0)
if ((cp = strrchr(root, '/')) != NULL)
stripdir = cp - root + 1;
/*
* If we're recursive, then we need to take down all of the
* files and directory components, so use fts(3).
* Copying the information file-by-file into the flstat.
* We'll make sense of it in flist_send.
*/
if ((fts = fts_open(cargv, FTS_PHYSICAL, NULL)) == NULL) {
ERR("fts_open");
return 0;
}
errno = 0;
while ((ent = fts_read(fts)) != NULL) {
if (!flist_fts_check(sess, ent)) {
errno = 0;
continue;
}
/* We don't allow symlinks without -l. */
assert(ent->fts_statp != NULL);
if (S_ISLNK(ent->fts_statp->st_mode) &&
!sess->opts->preserve_links) {
WARNX("%s: skipping symlink", ent->fts_path);
continue;
}
/*
* If rsync is told to avoid crossing a filesystem
* boundary when recursing, then replace all mount point
* directories with empty directories. The latter is
* prevented by telling rsync multiple times to avoid
* crossing a filesystem boundary when recursing.
* Replacing mount point directories is tricky. We need
* to sort out which directories to include. As such,
* keep track of unique device inodes, and use these for
* comparison.
*/
if (sess->opts->one_file_system &&
ent->fts_statp->st_dev != st.st_dev) {
if (sess->opts->one_file_system > 1 ||
!S_ISDIR(ent->fts_statp->st_mode))
continue;
flag = 0;
for (i = 0; i < nxdev; i++)
if (xdev[i] == ent->fts_statp->st_dev) {
flag = 1;
break;
}
if (flag)
continue;
if ((newxdev = reallocarray(xdev, nxdev + 1,
sizeof(dev_t))) == NULL) {
ERRX1("reallocarray");
goto out;
}
xdev = newxdev;
xdev[nxdev] = ent->fts_statp->st_dev;
nxdev++;
}
/* filter files */
if (rules_match(ent->fts_path + stripdir,
(ent->fts_info == FTS_D)) == -1) {
WARNX("%s: skipping excluded file",
ent->fts_path + stripdir);
fts_set(fts, ent, FTS_SKIP);
continue;
}
/* Allocate a new file entry. */
if (!flist_realloc(fl, sz, max)) {
ERRX1("flist_realloc");
goto out;
}
flsz++;
f = &(*fl)[*sz - 1];
/* Our path defaults to "." for the root. */
if (ent->fts_path[stripdir] == '\0') {
if (asprintf(&f->path, "%s.", ent->fts_path) == -1) {
ERR("asprintf");
f->path = NULL;
goto out;
}
} else {
if ((f->path = strdup(ent->fts_path)) == NULL) {
ERR("strdup");
goto out;
}
}
f->wpath = f->path + stripdir;
flist_copy_stat(f, ent->fts_statp);
/* Optionally copy link information. */
if (S_ISLNK(ent->fts_statp->st_mode)) {
f->link = symlink_read(ent->fts_accpath);
if (f->link == NULL) {