-
Notifications
You must be signed in to change notification settings - Fork 17
/
flist.c
3260 lines (2912 loc) · 87 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
/*
* Generate and receive file lists.
*
* Copyright (C) 1996 Andrew Tridgell
* Copyright (C) 1996 Paul Mackerras
* Copyright (C) 2001, 2002 Martin Pool <[email protected]>
* Copyright (C) 2002-2018 Wayne Davison
*
* 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, visit the http://fsf.org website.
*/
#include "rsync.h"
#include "ifuncs.h"
#include "rounding.h"
#include "inums.h"
#include "io.h"
extern int am_root;
extern int am_server;
extern int am_daemon;
extern int am_sender;
extern int am_generator;
extern int inc_recurse;
extern int always_checksum;
extern int checksum_type;
extern int module_id;
extern int ignore_errors;
extern int numeric_ids;
extern int quiet;
extern int recurse;
extern int use_qsort;
extern int xfer_dirs;
extern int filesfrom_fd;
extern int one_file_system;
extern int copy_dirlinks;
extern int preserve_uid;
extern int preserve_gid;
extern int preserve_acls;
extern int preserve_xattrs;
extern int preserve_links;
extern int preserve_hard_links;
extern int preserve_devices;
extern int preserve_specials;
extern int delete_during;
extern int missing_args;
extern int eol_nulls;
extern int relative_paths;
extern int implied_dirs;
extern int ignore_perishable;
extern int non_perishable_cnt;
extern int prune_empty_dirs;
extern int copy_links;
extern int copy_unsafe_links;
extern int protocol_version;
extern int sanitize_paths;
extern int munge_symlinks;
extern int use_safe_inc_flist;
extern int need_unsorted_flist;
extern int sender_symlink_iconv;
extern int output_needs_newline;
extern int sender_keeps_checksum;
extern int unsort_ndx;
extern uid_t our_uid;
extern struct stats stats;
extern char *filesfrom_host;
extern char *usermap, *groupmap;
extern char curr_dir[MAXPATHLEN];
extern struct chmod_mode_struct *chmod_modes;
extern filter_rule_list filter_list;
extern filter_rule_list daemon_filter_list;
#ifdef ICONV_OPTION
extern int filesfrom_convert;
extern iconv_t ic_send, ic_recv;
#endif
#define PTR_SIZE (sizeof (struct file_struct *))
int io_error;
int flist_csum_len;
dev_t filesystem_dev; /* used to implement -x */
struct file_list *cur_flist, *first_flist, *dir_flist;
int send_dir_ndx = -1, send_dir_depth = -1;
int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
int file_total = 0; /* total of all active items over all file-lists */
int file_old_total = 0; /* total of active items that will soon be gone */
int flist_eof = 0; /* all the file-lists are now known */
#define NORMAL_NAME 0
#define SLASH_ENDING_NAME 1
#define DOTDIR_NAME 2
#define MISSING_NAME 3
/* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
* internally, even if this platform does not allow files to have 64-bit inums.
* The only exception is if we're on a platform with no 64-bit type at all.
*
* Because we use read_longint() to get these off the wire, if you transfer
* devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
* machine with no 64-bit types then you will get an overflow error.
*
* Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
* to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
* be truncated. But it's a kind of silly thing to do anyhow. */
/* The tmp_* vars are used as a cache area by make_file() to store data
* that the sender doesn't need to remember in its file list. The data
* will survive just long enough to be used by send_file_entry(). */
static dev_t tmp_rdev;
#ifdef SUPPORT_HARD_LINKS
static int64 tmp_dev = -1, tmp_ino;
#endif
static char tmp_sum[MAX_DIGEST_LEN];
static char empty_sum[MAX_DIGEST_LEN];
static int flist_count_offset; /* for --delete --progress */
static int show_filelist_progress;
static void flist_sort_and_clean(struct file_list *flist, int strip_root);
static void output_flist(struct file_list *flist);
void init_flist(void)
{
if (DEBUG_GTE(FLIST, 4)) {
rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
(int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
}
parse_checksum_choice(); /* Sets checksum_type && xfersum_type */
flist_csum_len = csum_len_for_type(checksum_type, 1);
show_filelist_progress = INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse;
}
static void start_filelist_progress(char *kind)
{
if (quiet)
return;
rprintf(FCLIENT, "%s ... ", kind);
output_needs_newline = 1;
rflush(FINFO);
}
static void emit_filelist_progress(int count)
{
if (quiet)
return;
if (output_needs_newline == 2) /* avoid a newline in the middle of this filelist-progress output */
output_needs_newline = 0;
rprintf(FCLIENT, " %d files...\r", count);
output_needs_newline = 2;
}
static void maybe_emit_filelist_progress(int count)
{
if (INFO_GTE(FLIST, 2) && show_filelist_progress && (count % 100) == 0)
emit_filelist_progress(count);
}
static void finish_filelist_progress(const struct file_list *flist)
{
output_needs_newline = 0;
if (INFO_GTE(FLIST, 2)) {
/* This overwrites the progress line */
rprintf(FINFO, "%d file%sto consider\n",
flist->used, flist->used == 1 ? " " : "s ");
} else {
rprintf(FINFO, "done\n");
}
}
void show_flist_stats(void)
{
/* Nothing yet */
}
/* Stat either a symlink or its referent, depending on the settings of
* copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
*
* If path is the name of a symlink, then the linkbuf buffer (which must hold
* MAXPATHLEN chars) will be set to the symlink's target string.
*
* The stat structure pointed to by stp will contain information about the
* link or the referent as appropriate, if they exist. */
static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
{
#ifdef SUPPORT_LINKS
if (link_stat(path, stp, copy_dirlinks) < 0)
return -1;
if (S_ISLNK(stp->st_mode)) {
int llen = bpc_readlink(path, linkbuf, MAXPATHLEN - 1);
if (llen < 0)
return -1;
linkbuf[llen] = '\0';
if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
if (INFO_GTE(SYMSAFE, 1)) {
rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
path, linkbuf);
}
return x_stat(path, stp, NULL);
}
if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
&& strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
llen - SYMLINK_PREFIX_LEN + 1);
}
}
return 0;
#else
return x_stat(path, stp, NULL);
#endif
}
int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
{
#ifdef SUPPORT_LINKS
if (copy_links)
return x_stat(path, stp, NULL);
if (x_lstat(path, stp, NULL) < 0)
return -1;
if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
STRUCT_STAT st;
if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
*stp = st;
}
return 0;
#else
return x_stat(path, stp, NULL);
#endif
}
static inline int path_is_daemon_excluded(char *path, int ignore_filename)
{
if (daemon_filter_list.head) {
char *slash = path;
while ((slash = strchr(slash+1, '/')) != NULL) {
int ret;
*slash = '\0';
ret = check_filter(&daemon_filter_list, FLOG, path, 1);
*slash = '/';
if (ret < 0) {
errno = ENOENT;
return 1;
}
}
if (!ignore_filename
&& check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
errno = ENOENT;
return 1;
}
}
return 0;
}
static inline int is_excluded(const char *fname, int is_dir, int filter_level)
{
return name_is_excluded(fname, is_dir ? NAME_IS_DIR : NAME_IS_FILE, filter_level);
}
static void send_directory(int f, struct file_list *flist,
char *fbuf, int len, int flags);
static const char *pathname, *orig_dir;
static int pathname_len;
/* Make sure flist can hold at least flist->used + extra entries. */
static void flist_expand(struct file_list *flist, int extra)
{
struct file_struct **new_ptr;
if (flist->used + extra <= flist->malloced)
return;
if (flist->malloced < FLIST_START)
flist->malloced = FLIST_START;
else if (flist->malloced >= FLIST_LINEAR)
flist->malloced += FLIST_LINEAR;
else
flist->malloced *= 2;
/* In case count jumped or we are starting the list
* with a known size just set it. */
if (flist->malloced < flist->used + extra)
flist->malloced = flist->used + extra;
new_ptr = realloc_array(flist->files, struct file_struct *,
flist->malloced);
if (DEBUG_GTE(FLIST, 1) && flist->malloced != FLIST_START) {
rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",
who_am_i(),
big_num(sizeof flist->files[0] * flist->malloced),
(new_ptr == flist->files) ? " not" : "");
}
flist->files = new_ptr;
if (!flist->files)
out_of_memory("flist_expand");
}
static void flist_done_allocating(struct file_list *flist)
{
void *ptr = pool_boundary(flist->file_pool, 8*1024);
if (flist->pool_boundary == ptr)
flist->pool_boundary = NULL; /* list didn't use any pool memory */
else
flist->pool_boundary = ptr;
}
/* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
* F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
* with dir == NULL taken to be the starting directory, and dirlen < 0
* indicating that strdup(dir) should be called and then the -dirlen length
* value checked to ensure that it is not daemon-excluded. */
int change_pathname(struct file_struct *file, const char *dir, int dirlen)
{
if (dirlen < 0) {
char *cpy = strdup(dir);
if (*cpy != '/')
change_dir(orig_dir, CD_SKIP_CHDIR);
if (path_is_daemon_excluded(cpy, 0))
goto chdir_error;
dir = cpy;
dirlen = -dirlen;
} else {
if (file) {
if (pathname == F_PATHNAME(file))
return 1;
dir = F_PATHNAME(file);
if (dir)
dirlen = strlen(dir);
} else if (pathname == dir)
return 1;
if (dir && *dir != '/')
change_dir(orig_dir, CD_SKIP_CHDIR);
}
pathname = dir;
pathname_len = dirlen;
if (!dir)
dir = orig_dir;
if (!change_dir(dir, CD_NORMAL)) {
chdir_error:
io_error |= IOERR_GENERAL;
rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
if (dir != orig_dir)
change_dir(orig_dir, CD_NORMAL);
pathname = NULL;
pathname_len = 0;
return 0;
}
return 1;
}
static void send_file_entry(int f, const char *fname, struct file_struct *file,
#ifdef SUPPORT_LINKS
const char *symlink_name, int symlink_len,
#endif
int ndx, int first_ndx)
{
static time_t modtime;
static mode_t mode;
#ifdef SUPPORT_HARD_LINKS
static int64 dev;
#endif
static dev_t rdev;
static uint32 rdev_major;
static uid_t uid;
static gid_t gid;
static const char *user_name, *group_name;
static char lastname[MAXPATHLEN];
#ifdef SUPPORT_HARD_LINKS
int first_hlink_ndx = -1;
#endif
int l1, l2;
int xflags;
/* Initialize starting value of xflags and adjust counts. */
if (S_ISREG(file->mode))
xflags = 0;
else if (S_ISDIR(file->mode)) {
stats.num_dirs++;
if (protocol_version >= 30) {
if (file->flags & FLAG_CONTENT_DIR)
xflags = file->flags & FLAG_TOP_DIR;
else if (file->flags & FLAG_IMPLIED_DIR)
xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
else
xflags = XMIT_NO_CONTENT_DIR;
} else
xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
} else {
if (S_ISLNK(file->mode))
stats.num_symlinks++;
else if (IS_DEVICE(file->mode))
stats.num_devices++;
else if (IS_SPECIAL(file->mode))
stats.num_specials++;
xflags = 0;
}
if (file->mode == mode)
xflags |= XMIT_SAME_MODE;
else
mode = file->mode;
if (preserve_devices && IS_DEVICE(mode)) {
if (protocol_version < 28) {
if (tmp_rdev == rdev)
xflags |= XMIT_SAME_RDEV_pre28;
else
rdev = tmp_rdev;
} else {
rdev = tmp_rdev;
if ((uint32)major(rdev) == rdev_major)
xflags |= XMIT_SAME_RDEV_MAJOR;
else
rdev_major = major(rdev);
if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
xflags |= XMIT_RDEV_MINOR_8_pre30;
}
} else if (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31) {
/* Special files don't need an rdev number, so just make
* the historical transmission of the value efficient. */
if (protocol_version < 28)
xflags |= XMIT_SAME_RDEV_pre28;
else {
rdev = MAKEDEV(major(rdev), 0);
xflags |= XMIT_SAME_RDEV_MAJOR;
if (protocol_version < 30)
xflags |= XMIT_RDEV_MINOR_8_pre30;
}
} else if (protocol_version < 28)
rdev = MAKEDEV(0, 0);
if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
xflags |= XMIT_SAME_UID;
else {
uid = F_OWNER(file);
if (!numeric_ids) {
user_name = add_uid(uid);
if (inc_recurse && user_name)
xflags |= XMIT_USER_NAME_FOLLOWS;
}
}
if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
xflags |= XMIT_SAME_GID;
else {
gid = F_GROUP(file);
if (!numeric_ids) {
group_name = add_gid(gid);
if (inc_recurse && group_name)
xflags |= XMIT_GROUP_NAME_FOLLOWS;
}
}
if (file->modtime == modtime)
xflags |= XMIT_SAME_TIME;
else
modtime = file->modtime;
if (NSEC_BUMP(file) && protocol_version >= 31)
xflags |= XMIT_MOD_NSEC;
#ifdef SUPPORT_HARD_LINKS
if (tmp_dev != -1) {
if (protocol_version >= 30) {
struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
first_hlink_ndx = (int32)(long)np->data - 1;
if (first_hlink_ndx < 0) {
np->data = (void*)(long)(first_ndx + ndx + 1);
xflags |= XMIT_HLINK_FIRST;
}
if (DEBUG_GTE(HLINK, 1)) {
if (first_hlink_ndx >= 0) {
rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
who_am_i(), first_ndx + ndx, first_hlink_ndx,
first_hlink_ndx >= first_ndx ? "" : "un");
} else if (DEBUG_GTE(HLINK, 3)) {
rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n",
who_am_i(), first_ndx + ndx,
big_num(tmp_dev), big_num(tmp_ino));
}
}
} else {
if (tmp_dev == dev) {
if (protocol_version >= 28)
xflags |= XMIT_SAME_DEV_pre30;
} else
dev = tmp_dev;
}
xflags |= XMIT_HLINKED;
}
#endif
for (l1 = 0;
(l1 < 255) && lastname[l1] && (fname[l1] == lastname[l1]);
l1++) {}
l2 = strlen(fname+l1);
if (l1 > 0)
xflags |= XMIT_SAME_NAME;
if (l2 > 255)
xflags |= XMIT_LONG_NAME;
/* We must make sure we don't send a zero flag byte or the
* other end will terminate the flist transfer. Note that
* the use of XMIT_TOP_DIR on a non-dir has no meaning, so
* it's harmless way to add a bit to the first flag byte. */
if (protocol_version >= 28) {
if (!xflags && !S_ISDIR(mode))
xflags |= XMIT_TOP_DIR;
if ((xflags & 0xFF00) || !xflags) {
xflags |= XMIT_EXTENDED_FLAGS;
write_shortint(f, xflags);
} else
write_byte(f, xflags);
} else {
if (!(xflags & 0xFF))
xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
write_byte(f, xflags);
}
if (xflags & XMIT_SAME_NAME)
write_byte(f, l1);
if (xflags & XMIT_LONG_NAME)
write_varint30(f, l2);
else
write_byte(f, l2);
write_buf(f, fname + l1, l2);
#ifdef SUPPORT_HARD_LINKS
if (first_hlink_ndx >= 0) {
write_varint(f, first_hlink_ndx);
if (first_hlink_ndx >= first_ndx)
goto the_end;
}
#endif
write_varlong30(f, F_LENGTH(file), 3);
if (!(xflags & XMIT_SAME_TIME)) {
if (protocol_version >= 30)
write_varlong(f, modtime, 4);
else
write_int(f, modtime);
}
if (xflags & XMIT_MOD_NSEC)
write_varint(f, F_MOD_NSEC(file));
if (!(xflags & XMIT_SAME_MODE))
write_int(f, to_wire_mode(mode));
if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
if (protocol_version < 30)
write_int(f, uid);
else {
write_varint(f, uid);
if (xflags & XMIT_USER_NAME_FOLLOWS) {
int len = strlen(user_name);
write_byte(f, len);
write_buf(f, user_name, len);
}
}
}
if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
if (protocol_version < 30)
write_int(f, gid);
else {
write_varint(f, gid);
if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
int len = strlen(group_name);
write_byte(f, len);
write_buf(f, group_name, len);
}
}
}
if ((preserve_devices && IS_DEVICE(mode))
|| (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
if (protocol_version < 28) {
if (!(xflags & XMIT_SAME_RDEV_pre28))
write_int(f, (int)rdev);
} else {
if (!(xflags & XMIT_SAME_RDEV_MAJOR))
write_varint30(f, major(rdev));
if (protocol_version >= 30)
write_varint(f, minor(rdev));
else if (xflags & XMIT_RDEV_MINOR_8_pre30)
write_byte(f, minor(rdev));
else
write_int(f, minor(rdev));
}
}
#ifdef SUPPORT_LINKS
if (symlink_len) {
write_varint30(f, symlink_len);
write_buf(f, symlink_name, symlink_len);
}
#endif
#ifdef SUPPORT_HARD_LINKS
if (tmp_dev != -1 && protocol_version < 30) {
/* Older protocols expect the dev number to be transmitted
* 1-incremented so that it is never zero. */
if (protocol_version < 26) {
/* 32-bit dev_t and ino_t */
write_int(f, (int32)(dev+1));
write_int(f, (int32)tmp_ino);
} else {
/* 64-bit dev_t and ino_t */
if (!(xflags & XMIT_SAME_DEV_pre30))
write_longint(f, dev+1);
write_longint(f, tmp_ino);
}
}
#endif
if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
const char *sum;
if (S_ISREG(mode))
sum = tmp_sum;
else {
/* Prior to 28, we sent a useless set of nulls. */
sum = empty_sum;
}
write_buf(f, sum, flist_csum_len);
}
#ifdef SUPPORT_HARD_LINKS
the_end:
#endif
strlcpy(lastname, fname, MAXPATHLEN);
if (S_ISREG(mode) || S_ISLNK(mode))
stats.total_size += F_LENGTH(file);
}
static struct file_struct *recv_file_entry(int f, struct file_list *flist, int xflags)
{
static int64 modtime;
static mode_t mode;
#ifdef SUPPORT_HARD_LINKS
static int64 dev;
#endif
static dev_t rdev;
static uint32 rdev_major;
static uid_t uid;
static gid_t gid;
static uint16 gid_flags;
static char lastname[MAXPATHLEN], *lastdir;
static int lastdir_depth, lastdir_len = -1;
static unsigned int del_hier_name_len = 0;
static int in_del_hier = 0;
char thisname[MAXPATHLEN];
unsigned int l1 = 0, l2 = 0;
int alloc_len, basename_len, linkname_len;
int extra_len = file_extra_cnt * EXTRA_LEN;
int first_hlink_ndx = -1;
int64 file_length;
uint32 modtime_nsec;
const char *basename;
struct file_struct *file;
alloc_pool_t *pool;
char *bp;
if (xflags & XMIT_SAME_NAME)
l1 = read_byte(f);
if (xflags & XMIT_LONG_NAME)
l2 = read_varint30(f);
else
l2 = read_byte(f);
if (l2 >= MAXPATHLEN - l1) {
rprintf(FERROR,
"overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
xflags, l1, l2, lastname, who_am_i());
overflow_exit("recv_file_entry");
}
strlcpy(thisname, lastname, l1 + 1);
read_sbuf(f, &thisname[l1], l2);
thisname[l1 + l2] = 0;
/* Abuse basename_len for a moment... */
basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
#ifdef ICONV_OPTION
if (ic_recv != (iconv_t)-1) {
xbuf outbuf, inbuf;
INIT_CONST_XBUF(outbuf, thisname);
INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1);
if (iconvbufs(ic_recv, &inbuf, &outbuf, ICB_INIT) < 0) {
io_error |= IOERR_GENERAL;
rprintf(FERROR_UTF8,
"[%s] cannot convert filename: %s (%s)\n",
who_am_i(), lastname, strerror(errno));
outbuf.len = 0;
}
thisname[outbuf.len] = '\0';
}
#endif
if (*thisname
&& (clean_fname(thisname, CFN_REFUSE_DOT_DOT_DIRS) < 0 || (!relative_paths && *thisname == '/'))) {
rprintf(FERROR, "ABORTING due to unsafe pathname from sender: %s\n", thisname);
exit_cleanup(RERR_PROTOCOL);
}
if (sanitize_paths)
sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
if ((basename = strrchr(thisname, '/')) != NULL) {
int len = basename++ - thisname;
if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
lastdir = new_array(char, len + 1);
memcpy(lastdir, thisname, len);
lastdir[len] = '\0';
lastdir_len = len;
lastdir_depth = count_dir_elements(lastdir);
}
} else
basename = thisname;
basename_len = strlen(basename) + 1; /* count the '\0' */
#ifdef SUPPORT_HARD_LINKS
if (protocol_version >= 30
&& BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
first_hlink_ndx = read_varint(f);
if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
rprintf(FERROR,
"hard-link reference out of range: %d (%d)\n",
first_hlink_ndx, flist->ndx_start + flist->used);
exit_cleanup(RERR_PROTOCOL);
}
if (DEBUG_GTE(HLINK, 1)) {
rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx,
first_hlink_ndx >= flist->ndx_start ? "" : "un");
}
if (first_hlink_ndx >= flist->ndx_start) {
struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
file_length = F_LENGTH(first);
modtime = first->modtime;
modtime_nsec = F_MOD_NSEC(first);
mode = first->mode;
if (preserve_uid)
uid = F_OWNER(first);
if (preserve_gid)
gid = F_GROUP(first);
if (preserve_devices && IS_DEVICE(mode)) {
uint32 *devp = F_RDEV_P(first);
rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
}
if (preserve_links && S_ISLNK(mode))
linkname_len = strlen(F_SYMLINK(first)) + 1;
else
linkname_len = 0;
goto create_object;
}
}
#endif
file_length = read_varlong30(f, 3);
if (!(xflags & XMIT_SAME_TIME)) {
if (protocol_version >= 30) {
modtime = read_varlong(f, 4);
#if SIZEOF_TIME_T < SIZEOF_INT64
if (!am_generator && (int64)(time_t)modtime != modtime) {
rprintf(FERROR_XFER,
"Time value of %s truncated on receiver.\n",
lastname);
}
#endif
} else
modtime = read_int(f);
}
if (xflags & XMIT_MOD_NSEC)
modtime_nsec = read_varint(f);
else
modtime_nsec = 0;
if (!(xflags & XMIT_SAME_MODE))
mode = from_wire_mode(read_int(f));
if (chmod_modes && !S_ISLNK(mode) && mode)
mode = tweak_mode(mode, chmod_modes);
if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
if (protocol_version < 30)
uid = (uid_t)read_int(f);
else {
uid = (uid_t)read_varint(f);
if (xflags & XMIT_USER_NAME_FOLLOWS)
uid = recv_user_name(f, uid);
else if (inc_recurse && am_root && (!numeric_ids || usermap))
uid = match_uid(uid);
}
}
if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
if (protocol_version < 30)
gid = (gid_t)read_int(f);
else {
gid = (gid_t)read_varint(f);
gid_flags = 0;
if (xflags & XMIT_GROUP_NAME_FOLLOWS)
gid = recv_group_name(f, gid, &gid_flags);
else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
gid = match_gid(gid, &gid_flags);
}
}
if ((preserve_devices && IS_DEVICE(mode))
|| (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
if (protocol_version < 28) {
if (!(xflags & XMIT_SAME_RDEV_pre28))
rdev = (dev_t)read_int(f);
} else {
uint32 rdev_minor;
if (!(xflags & XMIT_SAME_RDEV_MAJOR))
rdev_major = read_varint30(f);
if (protocol_version >= 30)
rdev_minor = read_varint(f);
else if (xflags & XMIT_RDEV_MINOR_8_pre30)
rdev_minor = read_byte(f);
else
rdev_minor = read_int(f);
rdev = MAKEDEV(rdev_major, rdev_minor);
}
if (IS_DEVICE(mode))
extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
file_length = 0;
} else if (protocol_version < 28)
rdev = MAKEDEV(0, 0);
#ifdef SUPPORT_LINKS
if (preserve_links && S_ISLNK(mode)) {
linkname_len = read_varint30(f) + 1; /* count the '\0' */
if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
rprintf(FERROR, "overflow: linkname_len=%d\n",
linkname_len - 1);
overflow_exit("recv_file_entry");
}
#ifdef ICONV_OPTION
/* We don't know how much extra room we need to convert
* the as-yet-unread symlink data, so let's hope that a
* double-size buffer is plenty. */
if (sender_symlink_iconv)
linkname_len *= 2;
#endif
if (munge_symlinks)
linkname_len += SYMLINK_PREFIX_LEN;
}
else
#endif
linkname_len = 0;
#ifdef SUPPORT_HARD_LINKS
create_object:
if (preserve_hard_links) {
if (protocol_version < 28 && S_ISREG(mode))
xflags |= XMIT_HLINKED;
if (xflags & XMIT_HLINKED)
extra_len += (inc_recurse+1) * EXTRA_LEN;
}
#endif
#ifdef SUPPORT_ACLS
/* Directories need an extra int32 for the default ACL. */
if (preserve_acls && S_ISDIR(mode))
extra_len += EXTRA_LEN;
#endif
if (always_checksum && S_ISREG(mode))
extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
#if SIZEOF_INT64 >= 8
if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
extra_len += EXTRA_LEN;
#endif
#ifdef CAN_SET_NSEC
if (modtime_nsec)
extra_len += EXTRA_LEN;
#else
(void)modtime_nsec;
#endif
if (file_length < 0) {
rprintf(FERROR, "Offset underflow: file-length is negative\n");
exit_cleanup(RERR_UNSUPPORTED);
}
if (inc_recurse && S_ISDIR(mode)) {
if (one_file_system) {
/* Room to save the dir's device for -x */
extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
}
pool = dir_flist->file_pool;
} else
pool = flist->file_pool;
#if EXTRA_ROUNDING > 0
if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
#endif
alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
+ linkname_len;
bp = pool_alloc(pool, alloc_len, "recv_file_entry");
memset(bp, 0, extra_len + FILE_STRUCT_LEN);
bp += extra_len;
file = (struct file_struct *)bp;
bp += FILE_STRUCT_LEN;
memcpy(bp, basename, basename_len);
#ifdef SUPPORT_HARD_LINKS
if (xflags & XMIT_HLINKED
#ifndef CAN_HARDLINK_SYMLINK
&& !S_ISLNK(mode)
#endif
#ifndef CAN_HARDLINK_SPECIAL
&& !IS_SPECIAL(mode) && !IS_DEVICE(mode)
#endif
)
file->flags |= FLAG_HLINKED;
#endif
file->modtime = (time_t)modtime;
#ifdef CAN_SET_NSEC
if (modtime_nsec) {
file->flags |= FLAG_MOD_NSEC;
OPT_EXTRA(file, 0)->unum = modtime_nsec;
}
#endif
file->len32 = (uint32)file_length;
#if SIZEOF_INT64 >= 8
if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
#if SIZEOF_CAPITAL_OFF_T < 8
rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
exit_cleanup(RERR_UNSUPPORTED);
#else
file->flags |= FLAG_LENGTH64;
OPT_EXTRA(file, NSEC_BUMP(file))->unum = (uint32)(file_length >> 32);
#endif
}
#endif
file->mode = mode;
if (preserve_uid)
F_OWNER(file) = uid;
if (preserve_gid) {
F_GROUP(file) = gid;
file->flags |= gid_flags;
}
if (unsort_ndx)
F_NDX(file) = flist->used + flist->ndx_start;
if (basename != thisname) {
file->dirname = lastdir;
F_DEPTH(file) = lastdir_depth + 1;
} else
F_DEPTH(file) = 1;
if (S_ISDIR(mode)) {
if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
F_DEPTH(file)--;
if (protocol_version >= 30) {
if (!(xflags & XMIT_NO_CONTENT_DIR)) {
if (xflags & XMIT_TOP_DIR)
file->flags |= FLAG_TOP_DIR;
file->flags |= FLAG_CONTENT_DIR;
} else if (xflags & XMIT_TOP_DIR)
file->flags |= FLAG_IMPLIED_DIR;
} else if (xflags & XMIT_TOP_DIR) {
in_del_hier = recurse;
del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
if (relative_paths && del_hier_name_len > 2
&& lastname[del_hier_name_len-1] == '.'
&& lastname[del_hier_name_len-2] == '/')
del_hier_name_len -= 2;
file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
} else if (in_del_hier) {
if (!relative_paths || !del_hier_name_len
|| (l1 >= del_hier_name_len