Fixed a thinko in the calling of recv_add_gid().
[rsync/rsync.git] / flist.c
CommitLineData
dbda5fbf 1/*
0f78b815
WD
2 * Generate and receive file lists.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
ba2133d6 7 * Copyright (C) 2002-2007 Wayne Davison
0f78b815
WD
8 *
9 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
736a6a29 12 *
0f78b815
WD
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
172875cf 17 *
e7c67065
WD
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 21 */
c627d613
AT
22
23#include "rsync.h"
a3e18c76 24#include "rounding.h"
6d4e718f 25#include "io.h"
c627d613
AT
26
27extern int verbose;
34e18ecd 28extern int list_only;
f05f993e 29extern int am_root;
c627d613 30extern int am_server;
a8726d2a 31extern int am_daemon;
56194bcd 32extern int am_sender;
f14b3ef4 33extern int am_generator;
3ea6e0e7 34extern int inc_recurse;
8715db2c 35extern int do_progress;
c627d613 36extern int always_checksum;
983b1ed3
WD
37extern int module_id;
38extern int ignore_errors;
4836c3ee 39extern int numeric_ids;
a06d19e3 40extern int recurse;
7e037c42 41extern int xfer_dirs;
24d0fcde 42extern int filesfrom_fd;
c627d613 43extern int one_file_system;
88c2190a 44extern int copy_dirlinks;
314f4591 45extern int keep_dirlinks;
1c3344a1 46extern int preserve_acls;
16edf865 47extern int preserve_xattrs;
c627d613 48extern int preserve_links;
dc5ddbcc 49extern int preserve_hard_links;
c627d613 50extern int preserve_devices;
b5c6a6ae 51extern int preserve_specials;
c627d613
AT
52extern int preserve_uid;
53extern int preserve_gid;
6574b4f7 54extern int relative_paths;
24d0fcde 55extern int implied_dirs;
99a957d3 56extern int file_extra_cnt;
53039410
WD
57extern int ignore_perishable;
58extern int non_perishable_cnt;
85aecef6 59extern int prune_empty_dirs;
82306bf6 60extern int copy_links;
b5313607 61extern int copy_unsafe_links;
d04e9c51 62extern int protocol_version;
cb13abfe 63extern int sanitize_paths;
d64e6f42 64extern struct stats stats;
c627d613 65
e1f40891
WD
66extern char curr_dir[MAXPATHLEN];
67
2b7e0f33
WD
68extern struct chmod_mode_struct *chmod_modes;
69
7842418b
WD
70extern struct filter_list_struct filter_list;
71extern struct filter_list_struct server_filter_list;
c627d613 72
332cf6df
WD
73#ifdef ICONV_OPTION
74extern int ic_ndx;
75extern int need_unsorted_flist;
76extern iconv_t ic_send, ic_recv;
77#endif
78
06c28400 79int io_error;
7752df41 80int checksum_len;
535737bf 81dev_t filesystem_dev; /* used to implement -x */
82ad07c4 82
c7e6f84f
WD
83struct file_list *cur_flist, *first_flist, *dir_flist;
84int send_dir_ndx = -1, send_dir_depth = 0;
85int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
86int file_total = 0; /* total of all active items over all file-lists */
87int flist_eof = 0; /* all the file-lists are now known */
88
82ad07c4
WD
89/* The tmp_* vars are used as a cache area by make_file() to store data
90 * that the sender doesn't need to remember in its file list. The data
91 * will survive just long enough to be used by send_file_entry(). */
92static dev_t tmp_rdev;
9d155ecd 93#ifdef SUPPORT_HARD_LINKS
9d737ecb 94static int64 tmp_dev, tmp_ino;
9d155ecd 95#endif
a0456b9c 96static char tmp_sum[MAX_DIGEST_LEN];
06c28400 97
a0456b9c 98static char empty_sum[MAX_DIGEST_LEN];
c7565dad 99static int flist_count_offset; /* for --delete --progress */
3d382777 100
a6e7b978 101static void clean_flist(struct file_list *flist, int strip_root);
32cbfe7b 102static void output_flist(struct file_list *flist);
0199b05f 103
61dec11a
WD
104void init_flist(void)
105{
96293cf9
WD
106 if (verbose > 4) {
107 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
6aef83c9 108 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
96293cf9 109 }
a0456b9c
WD
110 checksum_len = protocol_version < 21 ? 2
111 : protocol_version < 30 ? MD4_DIGEST_LEN
112 : MD5_DIGEST_LEN;
61dec11a
WD
113}
114
1bbd10fe 115static int show_filelist_p(void)
58225000 116{
3ea6e0e7 117 return verbose && xfer_dirs && !am_server && !inc_recurse;
1bbd10fe 118}
ebed4c3a 119
1bbd10fe
DD
120static void start_filelist_progress(char *kind)
121{
ea124cb3 122 rprintf(FCLIENT, "%s ... ", kind);
e5ce3bcf 123 if (verbose > 1 || do_progress)
ea124cb3 124 rprintf(FCLIENT, "\n");
1bbd10fe 125 rflush(FINFO);
58225000
MP
126}
127
53135fe8 128static void emit_filelist_progress(int count)
db719fb0 129{
ea124cb3 130 rprintf(FCLIENT, " %d files...\r", count);
db719fb0
MP
131}
132
53135fe8 133static void maybe_emit_filelist_progress(int count)
58225000 134{
53135fe8
WD
135 if (do_progress && show_filelist_p() && (count % 100) == 0)
136 emit_filelist_progress(count);
58225000
MP
137}
138
1bbd10fe 139static void finish_filelist_progress(const struct file_list *flist)
58225000 140{
1bbd10fe
DD
141 if (do_progress) {
142 /* This overwrites the progress line */
c7b562be
MP
143 rprintf(FINFO, "%d file%sto consider\n",
144 flist->count, flist->count == 1 ? " " : "s ");
b7736c79 145 } else
1bbd10fe 146 rprintf(FINFO, "done\n");
58225000
MP
147}
148
86943126
MP
149void show_flist_stats(void)
150{
151 /* Nothing yet */
152}
153
f7632fc6
AT
154static void list_file_entry(struct file_struct *f)
155{
78d146e8 156 char permbuf[PERMSTRING_SIZE];
96293cf9 157 double len;
f7632fc6 158
96293cf9 159 if (!F_IS_ACTIVE(f)) {
7212be92
DD
160 /* this can happen if duplicate names were removed */
161 return;
e5ce3bcf 162 }
7212be92 163
78d146e8 164 permstring(permbuf, f->mode);
96293cf9 165 len = F_LENGTH(f);
740819ef 166
1c3344a1
WD
167 /* TODO: indicate '+' if the entry has an ACL. */
168
4f5b0756 169#ifdef SUPPORT_LINKS
f7632fc6 170 if (preserve_links && S_ISLNK(f->mode)) {
ebed4c3a 171 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
96293cf9 172 permbuf, len, timestring(f->modtime),
82ad07c4 173 f_name(f, NULL), F_SYMLINK(f));
0d162bd1
WD
174 } else
175#endif
e5ce3bcf 176 {
ebed4c3a 177 rprintf(FINFO, "%s %11.0f %s %s\n",
96293cf9 178 permbuf, len, timestring(f->modtime),
5e4ff5f9 179 f_name(f, NULL));
e5ce3bcf 180 }
f7632fc6
AT
181}
182
cad8f6f9
WD
183/* Stat either a symlink or its referent, depending on the settings of
184 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
4e5db0ad 185 *
cad8f6f9
WD
186 * If path is the name of a symlink, then the linkbuf buffer (which must hold
187 * MAXPATHLEN chars) will be set to the symlink's target string.
4e5db0ad 188 *
cad8f6f9
WD
189 * The stat structure pointed to by stp will contain information about the
190 * link or the referent as appropriate, if they exist. */
191static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
b5313607 192{
4f5b0756 193#ifdef SUPPORT_LINKS
cad8f6f9 194 if (link_stat(path, stp, copy_dirlinks) < 0)
b5313607 195 return -1;
cad8f6f9
WD
196 if (S_ISLNK(stp->st_mode)) {
197 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
198 if (llen < 0)
b5313607 199 return -1;
cad8f6f9 200 linkbuf[llen] = '\0';
fc638474
DD
201 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
202 if (verbose > 1) {
dbda5fbf 203 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
0ee6ca98 204 path, linkbuf);
fc638474 205 }
9439c0cb 206 return x_stat(path, stp, NULL);
b5313607
DD
207 }
208 }
209 return 0;
210#else
9439c0cb 211 return x_stat(path, stp, NULL);
b5313607
DD
212#endif
213}
214
cad8f6f9 215int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
82306bf6 216{
4f5b0756 217#ifdef SUPPORT_LINKS
b7736c79 218 if (copy_links)
9439c0cb
WD
219 return x_stat(path, stp, NULL);
220 if (x_lstat(path, stp, NULL) < 0)
314f4591 221 return -1;
cad8f6f9 222 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
314f4591 223 STRUCT_STAT st;
9439c0cb 224 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
cad8f6f9 225 *stp = st;
314f4591
WD
226 }
227 return 0;
82306bf6 228#else
9439c0cb 229 return x_stat(path, stp, NULL);
82306bf6
AT
230#endif
231}
232
7842418b 233/* This function is used to check if a file should be included/excluded
429f9828 234 * from the list of files based on its name and type etc. The value of
7842418b
WD
235 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
236static int is_excluded(char *fname, int is_dir, int filter_level)
c627d613 237{
7d687932 238#if 0 /* This currently never happens, so avoid a useless compare. */
7842418b 239 if (filter_level == NO_FILTERS)
429f9828
WD
240 return 0;
241#endif
6931c138 242 if (fname) {
429f9828 243 /* never exclude '.', even if somebody does --exclude '*' */
6931c138
WD
244 if (fname[0] == '.' && !fname[1])
245 return 0;
246 /* Handle the -R version of the '.' dir. */
247 if (fname[0] == '/') {
248 int len = strlen(fname);
249 if (fname[len-1] == '.' && fname[len-2] == '/')
250 return 0;
251 }
76e26e10 252 }
7842418b
WD
253 if (server_filter_list.head
254 && check_filter(&server_filter_list, fname, is_dir) < 0)
429f9828 255 return 1;
7842418b 256 if (filter_level != ALL_FILTERS)
429f9828 257 return 0;
7842418b
WD
258 if (filter_list.head
259 && check_filter(&filter_list, fname, is_dir) < 0)
76e26e10 260 return 1;
76e26e10 261 return 0;
c627d613
AT
262}
263
a6e7b978 264static void send_directory(int f, struct file_list *flist,
c7e6f84f 265 char *fbuf, int len, int flags);
c627d613 266
ef35abb2
WD
267static const char *pathname, *orig_dir;
268static int pathname_len;
c627d613 269
3ec4dd97 270
d9d6bc52
MP
271/**
272 * Make sure @p flist is big enough to hold at least @p flist->count
273 * entries.
274 **/
a85906c7 275void flist_expand(struct file_list *flist)
d9d6bc52 276{
0501f363 277 struct file_struct **new_ptr;
2e7d1994 278
a85906c7
S
279 if (flist->count < flist->malloced)
280 return;
dbda5fbf 281
a85906c7
S
282 if (flist->malloced < FLIST_START)
283 flist->malloced = FLIST_START;
284 else if (flist->malloced >= FLIST_LINEAR)
285 flist->malloced += FLIST_LINEAR;
286 else
287 flist->malloced *= 2;
288
a6e7b978
WD
289 /* In case count jumped or we are starting the list
290 * with a known size just set it. */
a85906c7
S
291 if (flist->malloced < flist->count)
292 flist->malloced = flist->count;
293
a6e7b978
WD
294#ifdef ICONV_OPTION
295 if (inc_recurse && flist == dir_flist && need_unsorted_flist) {
296 flist->sorted = realloc_array(flist->sorted, struct file_struct *,
297 flist->malloced);
298 }
299#endif
300
0501f363
WD
301 new_ptr = realloc_array(flist->files, struct file_struct *,
302 flist->malloced);
2e7d1994 303
8c483820 304 if (verbose >= 2 && flist->malloced != FLIST_START) {
ea124cb3 305 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
a85906c7 306 who_am_i(),
a4a7e64c 307 (double)sizeof flist->files[0] * flist->malloced,
a85906c7 308 (new_ptr == flist->files) ? " not" : "");
d9d6bc52 309 }
a85906c7 310
0501f363 311 flist->files = new_ptr;
a85906c7
S
312
313 if (!flist->files)
314 out_of_memory("flist_expand");
d9d6bc52
MP
315}
316
ef35abb2 317int push_pathname(const char *dir, int len)
c7e6f84f 318{
ef35abb2 319 if (dir == pathname)
c7e6f84f
WD
320 return 1;
321
322 if (!orig_dir)
323 orig_dir = strdup(curr_dir);
324
ef35abb2 325 if (pathname && !pop_dir(orig_dir)) {
c7e6f84f
WD
326 rsyserr(FERROR, errno, "pop_dir %s failed",
327 full_fname(orig_dir));
328 exit_cleanup(RERR_FILESELECT);
329 }
330
331 if (dir && !push_dir(dir, 0)) {
332 io_error |= IOERR_GENERAL;
63d83e94
WD
333 rsyserr(FERROR, errno, "push_dir %s failed in %s",
334 full_fname(dir), curr_dir);
c7e6f84f
WD
335 return 0;
336 }
337
ef35abb2
WD
338 pathname = dir;
339 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
c7e6f84f
WD
340
341 return 1;
342}
343
42c6b139 344static void send_file_entry(int f, struct file_struct *file, int ndx)
c627d613 345{
5911fee5
WD
346 static time_t modtime;
347 static mode_t mode;
1490812a 348 static int64 dev;
4124540d 349 static dev_t rdev;
9c5e91f8 350 static uint32 rdev_major;
5911fee5
WD
351 static uid_t uid;
352 static gid_t gid;
c7e6f84f 353 static char *user_name, *group_name;
5911fee5 354 static char lastname[MAXPATHLEN];
f376e674 355 char fname[MAXPATHLEN];
8cae71eb 356 int first_hlink_ndx = -1;
ebed4c3a 357 int l1, l2;
99a957d3 358 int flags;
72914a60 359
332cf6df
WD
360#ifdef ICONV_OPTION
361 if (ic_send != (iconv_t)-1) {
362 ICONV_CONST char *ibuf;
363 char *obuf = fname;
364 size_t ocnt = MAXPATHLEN, icnt;
365
366 iconv(ic_send, NULL,0, NULL,0);
367 if ((ibuf = (ICONV_CONST char *)file->dirname) != NULL) {
368 icnt = strlen(ibuf);
369 ocnt--; /* pre-subtract the space for the '/' */
370 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1)
371 goto convert_error;
372 *obuf++ = '/';
373 }
374
375 ibuf = (ICONV_CONST char *)file->basename;
376 icnt = strlen(ibuf);
377 if (iconv(ic_send, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
378 convert_error:
379 io_error |= IOERR_GENERAL;
380 rprintf(FINFO,
381 "[%s] cannot convert filename: %s (%s)\n",
382 who_am_i(), f_name(file, fname), strerror(errno));
383 return;
384 }
385 *obuf = '\0';
386 } else
387#endif
388 f_name(file, fname);
72914a60 389
82ad07c4 390 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
72914a60 391
30f337c9 392 if (file->mode == mode)
d01d15e0 393 flags |= XMIT_SAME_MODE;
1ef00d20 394 else
30f337c9 395 mode = file->mode;
b5c6a6ae
WD
396 if ((preserve_devices && IS_DEVICE(mode))
397 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 398 if (protocol_version < 28) {
82ad07c4 399 if (tmp_rdev == rdev)
b5c6a6ae
WD
400 flags |= XMIT_SAME_RDEV_pre28;
401 else
82ad07c4 402 rdev = tmp_rdev;
b5c6a6ae 403 } else {
82ad07c4 404 rdev = tmp_rdev;
9c5e91f8
WD
405 if ((uint32)major(rdev) == rdev_major)
406 flags |= XMIT_SAME_RDEV_MAJOR;
84a3efa0 407 else
9c5e91f8 408 rdev_major = major(rdev);
18438f0b
WD
409 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
410 flags |= XMIT_RDEV_MINOR_8_pre30;
75bc8600 411 }
b5c6a6ae 412 } else if (protocol_version < 28)
b4a09b72 413 rdev = MAKEDEV(0, 0);
82ad07c4 414 if (preserve_uid) {
858d45f1 415 if ((uid_t)F_OWNER(file) == uid && *lastname)
82ad07c4 416 flags |= XMIT_SAME_UID;
c7e6f84f 417 else {
1564cd5a 418 uid = F_OWNER(file);
c7e6f84f
WD
419 if (preserve_uid && !numeric_ids) {
420 user_name = add_uid(uid);
3ea6e0e7 421 if (inc_recurse && user_name)
c7e6f84f
WD
422 flags |= XMIT_USER_NAME_FOLLOWS;
423 }
424 }
82ad07c4
WD
425 }
426 if (preserve_gid) {
858d45f1 427 if ((gid_t)F_GROUP(file) == gid && *lastname)
82ad07c4 428 flags |= XMIT_SAME_GID;
c7e6f84f 429 else {
1564cd5a 430 gid = F_GROUP(file);
c7e6f84f
WD
431 if (preserve_gid && !numeric_ids) {
432 group_name = add_gid(gid);
3ea6e0e7 433 if (inc_recurse && group_name)
c7e6f84f
WD
434 flags |= XMIT_GROUP_NAME_FOLLOWS;
435 }
436 }
82ad07c4 437 }
30f337c9 438 if (file->modtime == modtime)
d01d15e0 439 flags |= XMIT_SAME_TIME;
1ef00d20 440 else
30f337c9 441 modtime = file->modtime;
a289addd 442
4f5b0756 443#ifdef SUPPORT_HARD_LINKS
9d737ecb 444 if (tmp_dev != 0) {
8cae71eb 445 if (protocol_version >= 30) {
9d737ecb 446 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
424d3691 447 first_hlink_ndx = (int32)(long)np->data - 1;
8cae71eb 448 if (first_hlink_ndx < 0) {
424d3691 449 np->data = (void*)(long)(ndx + 1);
8cae71eb
WD
450 flags |= XMIT_HLINK_FIRST;
451 }
452 flags |= XMIT_HLINKED;
453 } else {
9d737ecb 454 if (tmp_dev == dev) {
8cae71eb
WD
455 if (protocol_version >= 28)
456 flags |= XMIT_SAME_DEV_pre30;
457 } else
9d737ecb 458 dev = tmp_dev;
8cae71eb
WD
459 flags |= XMIT_HLINKED;
460 }
c4b4df4f 461 }
0d162bd1 462#endif
ebed4c3a
MP
463
464 for (l1 = 0;
3e491682
S
465 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
466 l1++) {}
eddd5d12 467 l2 = strlen(fname+l1);
72914a60 468
ebed4c3a 469 if (l1 > 0)
d01d15e0 470 flags |= XMIT_SAME_NAME;
ebed4c3a 471 if (l2 > 255)
d01d15e0 472 flags |= XMIT_LONG_NAME;
72914a60 473
1aa4caf3
WD
474 /* We must make sure we don't send a zero flag byte or the
475 * other end will terminate the flist transfer. Note that
ee3751c8 476 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
1aa4caf3 477 * it's harmless way to add a bit to the first flag byte. */
75bc8600 478 if (protocol_version >= 28) {
1aa4caf3 479 if (!flags && !S_ISDIR(mode))
ee3751c8 480 flags |= XMIT_TOP_DIR;
1aa4caf3 481 if ((flags & 0xFF00) || !flags) {
d01d15e0 482 flags |= XMIT_EXTENDED_FLAGS;
d521e1c2 483 write_shortint(f, flags);
75bc8600
WD
484 } else
485 write_byte(f, flags);
486 } else {
0a982011 487 if (!(flags & 0xFF))
75c51953 488 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
75bc8600
WD
489 write_byte(f, flags);
490 }
d01d15e0 491 if (flags & XMIT_SAME_NAME)
ebed4c3a 492 write_byte(f, l1);
d01d15e0 493 if (flags & XMIT_LONG_NAME)
f31514ad 494 write_varint30(f, l2);
72914a60 495 else
ebed4c3a
MP
496 write_byte(f, l2);
497 write_buf(f, fname + l1, l2);
72914a60 498
8cae71eb 499 if (first_hlink_ndx >= 0) {
f31514ad 500 write_varint30(f, first_hlink_ndx);
8cae71eb
WD
501 goto the_end;
502 }
503
ba59bd68
WD
504 write_varlong30(f, F_LENGTH(file), 3);
505 if (!(flags & XMIT_SAME_TIME)) {
506 if (protocol_version >= 30)
507 write_varlong(f, modtime, 4);
508 else
509 write_int(f, modtime);
510 }
d01d15e0 511 if (!(flags & XMIT_SAME_MODE))
30f337c9 512 write_int(f, to_wire_mode(mode));
d01d15e0 513 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
18438f0b
WD
514 if (protocol_version < 30)
515 write_int(f, uid);
516 else {
f31514ad 517 write_varint(f, uid);
18438f0b
WD
518 if (flags & XMIT_USER_NAME_FOLLOWS) {
519 int len = strlen(user_name);
520 write_byte(f, len);
521 write_buf(f, user_name, len);
522 }
c7e6f84f 523 }
72914a60 524 }
d01d15e0 525 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
18438f0b
WD
526 if (protocol_version < 30)
527 write_int(f, gid);
528 else {
f31514ad 529 write_varint(f, gid);
18438f0b
WD
530 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
531 int len = strlen(group_name);
532 write_byte(f, len);
533 write_buf(f, group_name, len);
534 }
c7e6f84f 535 }
72914a60 536 }
b5c6a6ae
WD
537 if ((preserve_devices && IS_DEVICE(mode))
538 || (preserve_specials && IS_SPECIAL(mode))) {
9c5e91f8
WD
539 if (protocol_version < 28) {
540 if (!(flags & XMIT_SAME_RDEV_pre28))
541 write_int(f, (int)rdev);
542 } else {
543 if (!(flags & XMIT_SAME_RDEV_MAJOR))
f31514ad 544 write_varint30(f, major(rdev));
18438f0b 545 if (protocol_version >= 30)
f31514ad 546 write_varint(f, minor(rdev));
18438f0b 547 else if (flags & XMIT_RDEV_MINOR_8_pre30)
9c5e91f8
WD
548 write_byte(f, minor(rdev));
549 else
550 write_int(f, minor(rdev));
551 }
75bc8600 552 }
c627d613 553
4f5b0756 554#ifdef SUPPORT_LINKS
30f337c9 555 if (preserve_links && S_ISLNK(mode)) {
82ad07c4
WD
556 const char *sl = F_SYMLINK(file);
557 int len = strlen(sl);
f31514ad 558 write_varint30(f, len);
82ad07c4 559 write_buf(f, sl, len);
72914a60 560 }
c627d613
AT
561#endif
562
4f5b0756 563#ifdef SUPPORT_HARD_LINKS
9d737ecb 564 if (tmp_dev != 0 && protocol_version < 30) {
4124540d 565 if (protocol_version < 26) {
736a6a29 566 /* 32-bit dev_t and ino_t */
26404276 567 write_int(f, (int32)dev);
9d737ecb 568 write_int(f, (int32)tmp_ino);
736a6a29
MP
569 } else {
570 /* 64-bit dev_t and ino_t */
8cae71eb 571 if (!(flags & XMIT_SAME_DEV_pre30))
4124540d 572 write_longint(f, dev);
9d737ecb 573 write_longint(f, tmp_ino);
736a6a29 574 }
72914a60 575 }
dc5ddbcc
AT
576#endif
577
9a4a237e 578 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
4a19c3b2 579 const char *sum;
728d0922 580 if (S_ISREG(mode))
82ad07c4 581 sum = tmp_sum;
9a4a237e 582 else {
728d0922
WD
583 /* Prior to 28, we sent a useless set of nulls. */
584 sum = empty_sum;
728d0922 585 }
7752df41 586 write_buf(f, sum, checksum_len);
ebed4c3a 587 }
182dca5c 588
8cae71eb 589 the_end:
ebed4c3a 590 strlcpy(lastname, fname, MAXPATHLEN);
ca947dea
WD
591
592 if (S_ISREG(mode) || S_ISLNK(mode))
593 stats.total_size += F_LENGTH(file);
182dca5c
AT
594}
595
82ad07c4 596static struct file_struct *recv_file_entry(struct file_list *flist,
1564cd5a 597 int xflags, int f)
182dca5c 598{
f14b3ef4 599 static int64 modtime;
5911fee5 600 static mode_t mode;
1490812a 601 static int64 dev;
4124540d 602 static dev_t rdev;
9c5e91f8 603 static uint32 rdev_major;
5911fee5
WD
604 static uid_t uid;
605 static gid_t gid;
cf1b4969 606 static uint16 gid_flags;
a289addd 607 static char lastname[MAXPATHLEN], *lastdir;
33ffd7c3 608 static int lastdir_depth, lastdir_len = -1;
42f23f47 609 static unsigned int del_hier_name_len = 0;
649f8742 610 static int in_del_hier = 0;
72914a60 611 char thisname[MAXPATHLEN];
ebed4c3a 612 unsigned int l1 = 0, l2 = 0;
c7e6f84f 613 int alloc_len, basename_len, linkname_len;
99a957d3 614 int extra_len = file_extra_cnt * EXTRA_LEN;
8cae71eb 615 int first_hlink_ndx = -1;
a289addd 616 OFF_T file_length;
c7e6f84f 617 const char *basename;
72914a60 618 struct file_struct *file;
ef35abb2
WD
619 alloc_pool_t *pool;
620 char *bp;
72914a60 621
1564cd5a 622 if (xflags & XMIT_SAME_NAME)
72914a60 623 l1 = read_byte(f);
ebed4c3a 624
1564cd5a 625 if (xflags & XMIT_LONG_NAME)
f31514ad 626 l2 = read_varint30(f);
72914a60
AT
627 else
628 l2 = read_byte(f);
629
ebed4c3a
MP
630 if (l2 >= MAXPATHLEN - l1) {
631 rprintf(FERROR,
1564cd5a
WD
632 "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
633 xflags, l1, l2, lastname, who_am_i());
82ad07c4 634 overflow_exit("recv_file_entry");
d0fd26aa 635 }
72914a60 636
ebed4c3a
MP
637 strlcpy(thisname, lastname, l1 + 1);
638 read_sbuf(f, &thisname[l1], l2);
639 thisname[l1 + l2] = 0;
72914a60 640
332cf6df
WD
641 /* Abuse basename_len for a moment... */
642 basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
643
644#ifdef ICONV_OPTION
645 if (ic_recv != (iconv_t)-1) {
646 char *obuf = thisname, *ibuf = lastname;
647 size_t ocnt = MAXPATHLEN, icnt = basename_len;
648
649 if (icnt >= MAXPATHLEN) {
650 errno = E2BIG;
651 goto convert_error;
652 }
653
654 iconv(ic_recv, NULL,0, NULL,0);
655 if (iconv(ic_recv, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
656 convert_error:
657 io_error |= IOERR_GENERAL;
658 rprintf(FINFO,
659 "[%s] cannot convert filename: %s (%s)\n",
660 who_am_i(), lastname, strerror(errno));
661 obuf = thisname;
662 }
663 *obuf = '\0';
664 }
665#endif
72914a60 666
58b1999e 667 clean_fname(thisname, 0);
72914a60 668
0d162bd1 669 if (sanitize_paths)
a2248aea 670 sanitize_path(thisname, thisname, "", 0, NULL);
cb13abfe 671
a289addd 672 if ((basename = strrchr(thisname, '/')) != NULL) {
c7e6f84f
WD
673 int len = basename++ - thisname;
674 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
675 lastdir = new_array(char, len + 1);
676 memcpy(lastdir, thisname, len);
677 lastdir[len] = '\0';
678 lastdir_len = len;
679 lastdir_depth = count_dir_elements(lastdir);
680 }
681 } else
a289addd 682 basename = thisname;
a289addd 683 basename_len = strlen(basename) + 1; /* count the '\0' */
72914a60 684
8cae71eb
WD
685#ifdef SUPPORT_HARD_LINKS
686 if (protocol_version >= 30
1564cd5a 687 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
8cae71eb 688 struct file_struct *first;
f31514ad 689 first_hlink_ndx = read_varint30(f);
8cae71eb
WD
690 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
691 rprintf(FERROR,
692 "hard-link reference out of range: %d (%d)\n",
693 first_hlink_ndx, flist->count);
694 exit_cleanup(RERR_PROTOCOL);
695 }
696 first = flist->files[first_hlink_ndx];
697 file_length = F_LENGTH(first);
698 modtime = first->modtime;
699 mode = first->mode;
700 if (preserve_uid)
1564cd5a 701 uid = F_OWNER(first);
8cae71eb 702 if (preserve_gid)
1564cd5a 703 gid = F_GROUP(first);
8cae71eb
WD
704 if ((preserve_devices && IS_DEVICE(mode))
705 || (preserve_specials && IS_SPECIAL(mode))) {
706 uint32 *devp = F_RDEV_P(first);
707 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
057f649f 708 extra_len += 2 * EXTRA_LEN;
8cae71eb
WD
709 }
710 if (preserve_links && S_ISLNK(mode))
711 linkname_len = strlen(F_SYMLINK(first)) + 1;
712 else
713 linkname_len = 0;
714 goto create_object;
715 }
716#endif
717
ba59bd68 718 file_length = read_varlong30(f, 3);
1564cd5a 719 if (!(xflags & XMIT_SAME_TIME)) {
f14b3ef4
WD
720 if (protocol_version >= 30) {
721 modtime = read_varlong(f, 4);
722#if SIZEOF_TIME_T < SIZEOF_INT64
723 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
724 rprintf(FERROR,
725 "Time value of %s truncated on receiver.\n",
726 lastname);
727 }
728#endif
729 } else
730 modtime = read_int(f);
ba59bd68 731 }
1564cd5a 732 if (!(xflags & XMIT_SAME_MODE))
30f337c9 733 mode = from_wire_mode(read_int(f));
1ef00d20 734
ebec5eb6 735 if (chmod_modes && !S_ISLNK(mode))
8bbe41b5
WD
736 mode = tweak_mode(mode, chmod_modes);
737
1564cd5a 738 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
18438f0b
WD
739 if (protocol_version < 30)
740 uid = (uid_t)read_int(f);
741 else {
f31514ad 742 uid = (uid_t)read_varint(f);
1564cd5a 743 if (xflags & XMIT_USER_NAME_FOLLOWS)
18438f0b
WD
744 uid = recv_user_name(f, uid);
745 else if (inc_recurse && am_root && !numeric_ids)
746 uid = match_uid(uid);
747 }
c7e6f84f 748 }
1564cd5a 749 if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
18438f0b
WD
750 if (protocol_version < 30)
751 gid = (gid_t)read_int(f);
752 else {
f31514ad 753 gid = (gid_t)read_varint(f);
cf1b4969 754 gid_flags = 0;
1564cd5a 755 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
cf1b4969 756 gid = recv_group_name(f, gid, &gid_flags);
18438f0b 757 else if (inc_recurse && (!am_root || !numeric_ids))
cf1b4969 758 gid = match_gid(gid, &gid_flags);
18438f0b 759 }
c7e6f84f 760 }
a289addd 761
b5c6a6ae
WD
762 if ((preserve_devices && IS_DEVICE(mode))
763 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 764 if (protocol_version < 28) {
1564cd5a 765 if (!(xflags & XMIT_SAME_RDEV_pre28))
b5c6a6ae
WD
766 rdev = (dev_t)read_int(f);
767 } else {
9c5e91f8 768 uint32 rdev_minor;
1564cd5a 769 if (!(xflags & XMIT_SAME_RDEV_MAJOR))
f31514ad 770 rdev_major = read_varint30(f);
18438f0b 771 if (protocol_version >= 30)
f31514ad 772 rdev_minor = read_varint(f);
1564cd5a 773 else if (xflags & XMIT_RDEV_MINOR_8_pre30)
9c5e91f8
WD
774 rdev_minor = read_byte(f);
775 else
776 rdev_minor = read_int(f);
b4a09b72 777 rdev = MAKEDEV(rdev_major, rdev_minor);
1ef00d20 778 }
96293cf9
WD
779 extra_len += 2 * EXTRA_LEN;
780 file_length = 0;
b5c6a6ae 781 } else if (protocol_version < 28)
b4a09b72 782 rdev = MAKEDEV(0, 0);
72914a60 783
4f5b0756 784#ifdef SUPPORT_LINKS
30f337c9 785 if (preserve_links && S_ISLNK(mode)) {
f31514ad 786 linkname_len = read_varint30(f) + 1; /* count the '\0' */
a289addd
WD
787 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
788 rprintf(FERROR, "overflow: linkname_len=%d\n",
789 linkname_len - 1);
82ad07c4 790 overflow_exit("recv_file_entry");
9dd891bb 791 }
72914a60 792 }
a289addd 793 else
0d162bd1 794#endif
a289addd 795 linkname_len = 0;
0d162bd1 796
82ad07c4 797#ifdef SUPPORT_HARD_LINKS
8cae71eb 798 create_object:
4785cd43
WD
799 if (preserve_hard_links) {
800 if (protocol_version < 28 && S_ISREG(mode))
1564cd5a 801 xflags |= XMIT_HLINKED;
cf1b4969 802 if (xflags & XMIT_HLINKED)
4785cd43 803 extra_len += EXTRA_LEN;
82ad07c4
WD
804 }
805#endif
806
1c3344a1
WD
807#ifdef SUPPORT_ACLS
808 /* We need one or two index int32s when we're preserving ACLs. */
809 if (preserve_acls)
810 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
811#endif
812
96293cf9
WD
813 if (always_checksum && S_ISREG(mode))
814 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
a289addd 815
96293cf9
WD
816 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
817 extra_len += EXTRA_LEN;
818
a3e18c76
WD
819#if EXTRA_ROUNDING > 0
820 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
821 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
822#endif
823
3ea6e0e7 824 if (inc_recurse && S_ISDIR(mode)) {
c7e6f84f
WD
825 if (one_file_system) {
826 /* Room to save the dir's device for -x */
827 extra_len += 2 * EXTRA_LEN;
828 }
ef35abb2
WD
829 pool = dir_flist->file_pool;
830 } else
831 pool = flist->file_pool;
c7e6f84f
WD
832
833 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
96293cf9 834 + linkname_len;
ef35abb2 835 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
9935066b 836
a3e18c76 837 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
82ad07c4 838 bp += extra_len;
f5db0993 839 file = (struct file_struct *)bp;
96293cf9
WD
840 bp += FILE_STRUCT_LEN;
841
842 memcpy(bp, basename, basename_len);
843 bp += basename_len + linkname_len; /* skip space for symlink too */
a289addd 844
cf1b4969
WD
845#ifdef SUPPORT_HARD_LINKS
846 if (xflags & XMIT_HLINKED)
847 file->flags |= FLAG_HLINKED;
848#endif
f14b3ef4 849 file->modtime = (time_t)modtime;
60af9465 850 file->len32 = (uint32)file_length;
96293cf9
WD
851 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
852 file->flags |= FLAG_LENGTH64;
853 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
854 }
a289addd 855 file->mode = mode;
82ad07c4 856 if (preserve_uid)
f1482c33 857 F_OWNER(file) = uid;
cf1b4969 858 if (preserve_gid) {
f1482c33 859 F_GROUP(file) = gid;
cf1b4969
WD
860 file->flags |= gid_flags;
861 }
332cf6df
WD
862#ifdef ICONV_OPTION
863 if (ic_ndx)
864 F_NDX(file) = flist->count + flist->ndx_start;
865#endif
a289addd 866
c7e6f84f
WD
867 if (basename != thisname) {
868 file->dirname = lastdir;
9d737ecb 869 F_DEPTH(file) = lastdir_depth + 1;
f3c3ed44 870 } else
9d737ecb 871 F_DEPTH(file) = 1;
f3c3ed44 872
649f8742 873 if (S_ISDIR(mode)) {
ee3751c8 874 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
9d737ecb 875 F_DEPTH(file)--;
1564cd5a 876 if (xflags & XMIT_TOP_DIR) {
75c51953 877 in_del_hier = recurse;
9d737ecb 878 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
7a16e122 879 if (relative_paths && del_hier_name_len > 2
9cdadbb1
WD
880 && lastname[del_hier_name_len-1] == '.'
881 && lastname[del_hier_name_len-2] == '/')
7a16e122 882 del_hier_name_len -= 2;
82ad07c4 883 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
ee3751c8 884 } else if (in_del_hier) {
f3c3ed44
WD
885 if (!relative_paths || !del_hier_name_len
886 || (l1 >= del_hier_name_len
9cdadbb1 887 && lastname[del_hier_name_len] == '/'))
82ad07c4 888 file->flags |= FLAG_XFER_DIR;
649f8742
WD
889 else
890 in_del_hier = 0;
891 }
892 }
893
b5c6a6ae 894 if ((preserve_devices && IS_DEVICE(mode))
82ad07c4 895 || (preserve_specials && IS_SPECIAL(mode))) {
4785cd43
WD
896 uint32 *devp = F_RDEV_P(file);
897 DEV_MAJOR(devp) = major(rdev);
898 DEV_MINOR(devp) = minor(rdev);
82ad07c4 899 }
a289addd 900
4f5b0756 901#ifdef SUPPORT_LINKS
a289addd 902 if (linkname_len) {
c58c1dc4 903 bp = (char*)file->basename + basename_len;
8cae71eb
WD
904 if (first_hlink_ndx >= 0) {
905 struct file_struct *first = flist->files[first_hlink_ndx];
906 memcpy(bp, F_SYMLINK(first), linkname_len);
907 } else
908 read_sbuf(f, bp, linkname_len - 1);
1a7f3d99 909 if (sanitize_paths)
dcebf78f 910 sanitize_path(bp, bp, "", lastdir_depth, NULL);
a289addd
WD
911 }
912#endif
913
4f5b0756 914#ifdef SUPPORT_HARD_LINKS
1564cd5a 915 if (preserve_hard_links && xflags & XMIT_HLINKED) {
8cae71eb 916 if (protocol_version >= 30) {
1564cd5a 917 F_HL_GNUM(file) = xflags & XMIT_HLINK_FIRST
8cae71eb 918 ? flist->count : first_hlink_ndx;
736a6a29 919 } else {
9d737ecb
WD
920 static int32 cnt = 0;
921 struct idev_node *np;
922 int64 ino;
923 int32 ndx;
8cae71eb 924 if (protocol_version < 26) {
9d737ecb
WD
925 dev = read_int(f);
926 ino = read_int(f);
8cae71eb 927 } else {
1564cd5a 928 if (!(xflags & XMIT_SAME_DEV_pre30))
8cae71eb 929 dev = read_longint(f);
9d737ecb 930 ino = read_longint(f);
8cae71eb 931 }
9d737ecb 932 np = idev_node(dev, ino);
424d3691 933 ndx = (int32)(long)np->data - 1;
9d737ecb
WD
934 if (ndx < 0) {
935 ndx = cnt++;
424d3691 936 np->data = (void*)(long)cnt;
9d737ecb
WD
937 }
938 F_HL_GNUM(file) = ndx;
736a6a29 939 }
72914a60 940 }
dc5ddbcc 941#endif
ebed4c3a 942
96293cf9
WD
943 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
944 if (S_ISREG(mode))
945 bp = (char*)F_SUM(file);
82ad07c4 946 else {
fea4db62 947 /* Prior to 28, we get a useless set of nulls. */
82ad07c4 948 bp = tmp_sum;
fea4db62 949 }
8cae71eb
WD
950 if (first_hlink_ndx >= 0) {
951 struct file_struct *first = flist->files[first_hlink_ndx];
952 memcpy(bp, F_SUM(first), checksum_len);
953 } else
954 read_buf(f, bp, checksum_len);
72914a60 955 }
ebed4c3a 956
1c3344a1
WD
957#ifdef SUPPORT_ACLS
958 if (preserve_acls && !S_ISLNK(mode))
959 receive_acl(file, f);
960#endif
16edf865
WD
961#ifdef SUPPORT_XATTRS
962 if (preserve_xattrs)
963 receive_xattr(file, f );
964#endif
1c3344a1 965
ca947dea
WD
966 if (S_ISREG(mode) || S_ISLNK(mode))
967 stats.total_size += file_length;
968
f5db0993 969 return file;
c627d613
AT
970}
971
db719fb0
MP
972/**
973 * Create a file_struct for a named file by reading its stat()
974 * information and performing extensive checks against global
975 * options.
976 *
977 * @return the new file, or NULL if there was an error or this file
978 * should be excluded.
979 *
980 * @todo There is a small optimization opportunity here to avoid
981 * stat()ing the file in some circumstances, which has a certain cost.
982 * We are called immediately after doing readdir(), and so we may
983 * already know the d_type of the file. We could for example avoid
984 * statting directories if we're not recursing, but this is not a very
985 * important case. Some systems may not have d_type.
986 **/
45d8bfe0 987struct file_struct *make_file(const char *fname, struct file_list *flist,
82ad07c4 988 STRUCT_STAT *stp, int flags, int filter_level)
c627d613 989{
a289addd
WD
990 static char *lastdir;
991 static int lastdir_len = -1;
3ec4dd97 992 struct file_struct *file;
bcacc18b 993 STRUCT_STAT st;
1923b1fc 994 char thisname[MAXPATHLEN];
e0870f1d 995 char linkname[MAXPATHLEN];
c7e6f84f 996 int alloc_len, basename_len, linkname_len;
99a957d3 997 int extra_len = file_extra_cnt * EXTRA_LEN;
c7e6f84f
WD
998 const char *basename;
999 char *bp;
9935066b 1000
1923b1fc 1001 if (strlcpy(thisname, fname, sizeof thisname)
ef35abb2 1002 >= sizeof thisname - pathname_len) {
0ee6ca98 1003 rprintf(FINFO, "skipping overly long name: %s\n", fname);
882e6893
WD
1004 return NULL;
1005 }
58b1999e 1006 clean_fname(thisname, 0);
b7736c79 1007 if (sanitize_paths)
a2248aea 1008 sanitize_path(thisname, thisname, "", 0, NULL);
3ec4dd97 1009
e4fdf1de 1010 if (stp && S_ISDIR(stp->st_mode)) {
ede1f0eb 1011 st = *stp; /* Needed for "symlink/." with --relative. */
e4fdf1de
WD
1012 *linkname = '\0'; /* make IBM code checker happy */
1013 } else if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 1014 int save_errno = errno;
a4a7e64c 1015 /* See if file is excluded before reporting an error. */
7842418b 1016 if (filter_level != NO_FILTERS
60cc01a6 1017 && (is_excluded(thisname, 0, filter_level)
53039410
WD
1018 || is_excluded(thisname, 1, filter_level))) {
1019 if (ignore_perishable && save_errno != ENOENT)
1020 non_perishable_cnt++;
a4a7e64c 1021 return NULL;
53039410 1022 }
a4a7e64c 1023 if (save_errno == ENOENT) {
4f5b0756 1024#ifdef SUPPORT_LINKS
a4a7e64c 1025 /* Avoid "vanished" error if symlink points nowhere. */
9439c0cb 1026 if (copy_links && x_lstat(thisname, &st, NULL) == 0
a4a7e64c
WD
1027 && S_ISLNK(st.st_mode)) {
1028 io_error |= IOERR_GENERAL;
1029 rprintf(FERROR, "symlink has no referent: %s\n",
1030 full_fname(thisname));
e5ce3bcf
WD
1031 } else
1032#endif
1033 {
a4a7e64c
WD
1034 enum logcode c = am_daemon && protocol_version < 28
1035 ? FERROR : FINFO;
1036 io_error |= IOERR_VANISHED;
1037 rprintf(c, "file has vanished: %s\n",
1038 full_fname(thisname));
76e26e10 1039 }
a4a7e64c 1040 } else {
a8726d2a 1041 io_error |= IOERR_GENERAL;
d62bcc17
WD
1042 rsyserr(FERROR, save_errno, "readlink %s failed",
1043 full_fname(thisname));
76e26e10 1044 }
3ec4dd97
AT
1045 return NULL;
1046 }
c627d613 1047
7842418b
WD
1048 /* backup.c calls us with filter_level set to NO_FILTERS. */
1049 if (filter_level == NO_FILTERS)
1050 goto skip_filters;
ac1a0994 1051
7e037c42 1052 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
0ee6ca98 1053 rprintf(FINFO, "skipping directory %s\n", thisname);
3ec4dd97
AT
1054 return NULL;
1055 }
ebed4c3a 1056
d521e1c2 1057 /* -x only affects directories because we need to avoid recursing
e90cdb8a
WD
1058 * into a mount-point directory, not to avoid copying a symlinked
1059 * file if -L (or similar) was specified. */
535737bf
WD
1060 if (one_file_system && st.st_dev != filesystem_dev
1061 && S_ISDIR(st.st_mode)) {
1062 if (one_file_system > 1) {
1063 if (verbose > 2) {
1064 rprintf(FINFO, "skipping mount-point dir %s\n",
1065 thisname);
ebec5eb6 1066 }
535737bf 1067 return NULL;
ebec5eb6 1068 }
82ad07c4 1069 flags |= FLAG_MOUNT_DIR;
3b173846 1070 }
ebed4c3a 1071
53039410
WD
1072 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1073 if (ignore_perishable)
1074 non_perishable_cnt++;
76e26e10 1075 return NULL;
53039410 1076 }
76e26e10 1077
132fcf36 1078 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 1079#ifdef SUPPORT_LINKS
132fcf36
WD
1080 if (!S_ISLNK(st.st_mode))
1081#endif
1082 if (access(thisname, R_OK) != 0)
1083 return NULL;
1084 }
ac1a0994 1085
97b7bff4 1086 skip_filters:
ac1a0994 1087
c7e6f84f
WD
1088 /* Only divert a directory in the main transfer. */
1089 if (flist && flist->prev && S_ISDIR(st.st_mode)
1090 && flags & FLAG_DIVERT_DIRS) {
1091 flist = dir_flist;
1092 /* Room for parent/sibling/next-child info. */
1093 extra_len += 3 * EXTRA_LEN;
1094 }
1095
ea847c62
WD
1096 if (verbose > 2) {
1097 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
0ee6ca98 1098 who_am_i(), thisname, filter_level);
ea847c62 1099 }
ebed4c3a 1100
a289addd 1101 if ((basename = strrchr(thisname, '/')) != NULL) {
c7e6f84f
WD
1102 int len = basename++ - thisname;
1103 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1104 lastdir = new_array(char, len + 1);
1105 memcpy(lastdir, thisname, len);
1106 lastdir[len] = '\0';
1107 lastdir_len = len;
1108 }
1109 } else
a289addd 1110 basename = thisname;
a289addd 1111 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 1112
4f5b0756 1113#ifdef SUPPORT_LINKS
a289addd
WD
1114 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1115#else
1116 linkname_len = 0;
1117#endif
1118
96293cf9
WD
1119 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1120 extra_len += EXTRA_LEN;
1121
a3e18c76
WD
1122#if EXTRA_ROUNDING > 0
1123 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1124 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1125#endif
1126
c7e6f84f 1127 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
96293cf9 1128 + linkname_len;
99aaa6ca
WD
1129 if (flist)
1130 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
1131 else {
9935066b 1132 if (!(bp = new_array(char, alloc_len)))
99aaa6ca 1133 out_of_memory("make_file");
9935066b
S
1134 }
1135
a3e18c76 1136 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
82ad07c4 1137 bp += extra_len;
a289addd 1138 file = (struct file_struct *)bp;
96293cf9
WD
1139 bp += FILE_STRUCT_LEN;
1140
1141 memcpy(bp, basename, basename_len);
1142 bp += basename_len + linkname_len; /* skip space for symlink too */
82ad07c4
WD
1143
1144#ifdef SUPPORT_HARD_LINKS
c7e6f84f 1145 if (preserve_hard_links && flist && flist->prev) {
82ad07c4
WD
1146 if (protocol_version >= 28
1147 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1148 : S_ISREG(st.st_mode)) {
9d737ecb
WD
1149 tmp_dev = st.st_dev;
1150 tmp_ino = st.st_ino;
82ad07c4 1151 } else
9d737ecb 1152 tmp_dev = 0;
82ad07c4
WD
1153 }
1154#endif
a289addd 1155
96293cf9
WD
1156#ifdef HAVE_STRUCT_STAT_ST_RDEV
1157 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1158 tmp_rdev = st.st_rdev;
1159 st.st_size = 0;
1160 }
1161#endif
1162
a289addd 1163 file->flags = flags;
3ec4dd97 1164 file->modtime = st.st_mtime;
60af9465 1165 file->len32 = (uint32)st.st_size;
96293cf9
WD
1166 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1167 file->flags |= FLAG_LENGTH64;
1168 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1169 }
8bbe41b5 1170 file->mode = st.st_mode;
82ad07c4 1171 if (preserve_uid)
f1482c33 1172 F_OWNER(file) = st.st_uid;
82ad07c4 1173 if (preserve_gid)
f1482c33 1174 F_GROUP(file) = st.st_gid;
61dec11a 1175
c7e6f84f
WD
1176 if (basename != thisname)
1177 file->dirname = lastdir;
a289addd 1178
4f5b0756 1179#ifdef SUPPORT_LINKS
96293cf9 1180 if (linkname_len) {
c58c1dc4 1181 bp = (char*)file->basename + basename_len;
96293cf9
WD
1182 memcpy(bp, linkname, linkname_len);
1183 }
0d162bd1
WD
1184#endif
1185
82ad07c4
WD
1186 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1187 file_checksum(thisname, tmp_sum, st.st_size);
c627d613 1188
ef35abb2 1189 F_PATHNAME(file) = pathname;
c627d613 1190
23f4587f
WD
1191 /* This code is only used by the receiver when it is building
1192 * a list of files for a delete pass. */
1193 if (keep_dirlinks && linkname_len && flist) {
1194 STRUCT_STAT st2;
f5db0993 1195 int save_mode = file->mode;
8bbe41b5 1196 file->mode = S_IFDIR; /* Find a directory with our name. */
c7e6f84f 1197 if (flist_find(dir_flist, file) >= 0
9439c0cb 1198 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
23f4587f 1199 file->modtime = st2.st_mtime;
96293cf9 1200 file->len32 = 0;
23f4587f 1201 file->mode = st2.st_mode;
82ad07c4 1202 if (preserve_uid)
f1482c33 1203 F_OWNER(file) = st2.st_uid;
82ad07c4 1204 if (preserve_gid)
f1482c33 1205 F_GROUP(file) = st2.st_gid;
f5db0993
WD
1206 } else
1207 file->mode = save_mode;
23f4587f
WD
1208 }
1209
82ad07c4
WD
1210 if (basename_len == 0+1)
1211 return NULL;
1212
3ea6e0e7 1213 if (inc_recurse && flist == dir_flist) {
76e754a0 1214 flist_expand(dir_flist);
a6e7b978
WD
1215#ifdef ICONV_OPTION
1216 if (ic_ndx)
1217 F_NDX(file) = dir_flist->count;
a6e7b978
WD
1218 if (need_unsorted_flist)
1219 dir_flist->sorted[dir_flist->count] = file;
76e754a0 1220#endif
a6e7b978 1221 dir_flist->files[dir_flist->count++] = file;
c7e6f84f
WD
1222 }
1223
3ec4dd97 1224 return file;
c627d613
AT
1225}
1226
17026f27 1227/* Only called for temporary file_struct entries created by make_file(). */
82ad07c4
WD
1228void unmake_file(struct file_struct *file)
1229{
99a957d3 1230 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
a3e18c76
WD
1231#if EXTRA_ROUNDING > 0
1232 if (extra_cnt & EXTRA_ROUNDING)
1233 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1234#endif
1235 free(REQ_EXTRA(file, extra_cnt));
82ad07c4
WD
1236}
1237
42c6b139 1238static struct file_struct *send_file_name(int f, struct file_list *flist,
ede1f0eb 1239 char *fname, STRUCT_STAT *stp,
c7e6f84f 1240 int flags, int filter_flags)
c627d613 1241{
ebed4c3a 1242 struct file_struct *file;
16edf865 1243#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1c3344a1
WD
1244 statx sx;
1245#endif
ebed4c3a 1246
c7e6f84f 1247 file = make_file(fname, flist, stp, flags, filter_flags);
37802f40 1248 if (!file)
301fb56c 1249 return NULL;
ebed4c3a 1250
ebec5eb6 1251 if (chmod_modes && !S_ISLNK(file->mode))
8bbe41b5
WD
1252 file->mode = tweak_mode(file->mode, chmod_modes);
1253
1c3344a1
WD
1254#ifdef SUPPORT_ACLS
1255 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1256 sx.st.st_mode = file->mode;
1257 sx.acc_acl = sx.def_acl = NULL;
1258 if (get_acl(fname, &sx) < 0)
1259 return NULL;
1260 }
1261#endif
16edf865
WD
1262#ifdef SUPPORT_XATTRS
1263 if (preserve_xattrs && f >= 0) {
1264 sx.xattr = NULL;
1265 if (get_xattr(fname, &sx) < 0)
1266 return NULL;
1267 }
1268#endif
1c3344a1 1269
53135fe8 1270 maybe_emit_filelist_progress(flist->count + flist_count_offset);
ebed4c3a 1271
d9d6bc52 1272 flist_expand(flist);
82ad07c4 1273 flist->files[flist->count++] = file;
1c3344a1 1274 if (f >= 0) {
42c6b139 1275 send_file_entry(f, file, flist->count - 1);
1c3344a1
WD
1276#ifdef SUPPORT_ACLS
1277 if (preserve_acls && !S_ISLNK(file->mode)) {
1278 send_acl(&sx, f);
1279 free_acl(&sx);
1280 }
16edf865
WD
1281#endif
1282#ifdef SUPPORT_XATTRS
1283 if (preserve_xattrs) {
1284 F_XATTR(file) = send_xattr(&sx, f);
1285 free_xattr(&sx);
1286 }
1c3344a1
WD
1287#endif
1288 }
301fb56c
WD
1289 return file;
1290}
ebed4c3a 1291
301fb56c 1292static void send_if_directory(int f, struct file_list *flist,
56f0c976 1293 struct file_struct *file,
c7e6f84f
WD
1294 char *fbuf, unsigned int ol,
1295 int flags)
301fb56c 1296{
56f0c976 1297 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
301fb56c
WD
1298
1299 if (S_ISDIR(file->mode)
82ad07c4 1300 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
f1773e09
WD
1301 void *save_filters;
1302 unsigned int len = strlen(fbuf);
1303 if (len > 1 && fbuf[len-1] == '/')
1304 fbuf[--len] = '\0';
1305 if (len >= MAXPATHLEN - 1) {
1306 io_error |= IOERR_GENERAL;
1307 rprintf(FERROR, "skipping long-named directory: %s\n",
1308 full_fname(fbuf));
1309 return;
1310 }
1311 save_filters = push_local_filters(fbuf, len);
a6e7b978 1312 send_directory(f, flist, fbuf, len, flags);
f1773e09 1313 pop_local_filters(save_filters);
56f0c976
WD
1314 fbuf[ol] = '\0';
1315 if (is_dot_dir)
1316 fbuf[ol-1] = '.';
ebed4c3a 1317 }
c627d613
AT
1318}
1319
c7e6f84f
WD
1320static int file_compare(struct file_struct **file1, struct file_struct **file2)
1321{
1322 return f_name_cmp(*file1, *file2);
1323}
1324
1325/* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end),
1326 * sort them by name, and link them into the tree, setting the appropriate
1327 * parent/child/sibling pointers. */
1328static void add_dirs_to_tree(int parent_ndx, int start, int end)
1329{
1330 int i;
1331 int32 *dp = NULL;
1332 int32 *parent_dp = parent_ndx < 0 ? NULL
a6e7b978 1333 : F_DIRNODE_P(dir_flist->sorted[parent_ndx]);
c7e6f84f 1334
a6e7b978
WD
1335 qsort(dir_flist->sorted + start, end - start + 1,
1336 sizeof dir_flist->sorted[0], (int (*)())file_compare);
c7e6f84f
WD
1337
1338 for (i = start; i <= end; i++) {
a6e7b978 1339 struct file_struct *file = dir_flist->sorted[i];
c7e6f84f
WD
1340 if (!(file->flags & FLAG_XFER_DIR)
1341 || file->flags & FLAG_MOUNT_DIR)
1342 continue;
1343 if (dp)
1344 DIR_NEXT_SIBLING(dp) = i;
1345 else if (parent_dp)
1346 DIR_FIRST_CHILD(parent_dp) = i;
1347 else
1348 send_dir_ndx = i;
1349 dp = F_DIRNODE_P(file);
1350 DIR_PARENT(dp) = parent_ndx;
1351 DIR_FIRST_CHILD(dp) = -1;
1352 }
1353 if (dp)
1354 DIR_NEXT_SIBLING(dp) = -1;
1355}
1356
301fb56c 1357/* This function is normally called by the sender, but the receiving side also
7b558d7f 1358 * calls it from get_dirlist() with f set to -1 so that we just construct the
301fb56c
WD
1359 * file list in memory without sending it over the wire. Also, get_dirlist()
1360 * might call this with f set to -2, which also indicates that local filter
1361 * rules should be ignored. */
a6e7b978
WD
1362static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1363 int flags)
c627d613 1364{
3ec4dd97 1365 struct dirent *di;
32cbfe7b 1366 unsigned remainder;
3ec4dd97 1367 char *p;
f1773e09 1368 DIR *d;
c7e6f84f 1369 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
a6e7b978 1370 int start = flist->count;
c7e6f84f 1371 int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
c7e6f84f
WD
1372
1373 assert(flist != NULL);
3ec4dd97 1374
f1773e09 1375 if (!(d = opendir(fbuf))) {
06c28400 1376 io_error |= IOERR_GENERAL;
f1773e09 1377 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1378 return;
1379 }
c627d613 1380
19b2a5d9
WD
1381 p = fbuf + len;
1382 if (len != 1 || *fbuf != '/')
eddd5d12 1383 *p++ = '/';
f1773e09 1384 *p = '\0';
32cbfe7b 1385 remainder = MAXPATHLEN - (p - fbuf);
ebed4c3a 1386
6a7cc46c 1387 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1388 char *dname = d_name(di);
6a7cc46c
S
1389 if (dname[0] == '.' && (dname[1] == '\0'
1390 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1391 continue;
0ee6ca98 1392 if (strlcpy(p, dname, remainder) >= remainder) {
eddd5d12
WD
1393 io_error |= IOERR_GENERAL;
1394 rprintf(FINFO,
1395 "cannot send long-named file %s\n",
f1773e09 1396 full_fname(fbuf));
beaf4954 1397 continue;
eddd5d12 1398 }
0ee6ca98 1399
42c6b139 1400 send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
3ec4dd97 1401 }
32cbfe7b
WD
1402
1403 fbuf[len] = '\0';
1404
6a7cc46c 1405 if (errno) {
06c28400 1406 io_error |= IOERR_GENERAL;
71903f60 1407 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1408 }
c627d613 1409
3ec4dd97 1410 closedir(d);
301fb56c 1411
a6e7b978 1412 if (f >= 0 && recurse && !divert_dirs) {
301fb56c 1413 int i, end = flist->count - 1;
8cae71eb 1414 /* send_if_directory() bumps flist->count, so use "end". */
301fb56c 1415 for (i = start; i <= end; i++)
c7e6f84f
WD
1416 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1417 }
1418}
1419
a6e7b978 1420static void send1extra(int f, struct file_struct *file, struct file_list *flist)
c7e6f84f
WD
1421{
1422 char fbuf[MAXPATHLEN];
a6e7b978
WD
1423 int dlen;
1424
1425 f_name(file, fbuf);
1426 dlen = strlen(fbuf);
1427
1428 if (F_PATHNAME(file) != pathname) {
1429 if (!push_pathname(F_PATHNAME(file), -1))
1430 exit_cleanup(RERR_FILESELECT);
1431 }
1432
1433 change_local_filter_dir(fbuf, dlen, send_dir_depth);
1434
1435 send_directory(f, flist, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
1436}
1437
1438void send_extra_file_list(int f, int at_least)
1439{
c7e6f84f
WD
1440 struct file_list *flist;
1441 int64 start_write;
7a2fa0c2 1442 int future_cnt, save_io_error = io_error;
c7e6f84f 1443
25c2a6ac 1444 if (flist_eof)
c7e6f84f
WD
1445 return;
1446
1447 /* Keep sending data until we have the requested number of
1448 * files in the upcoming file-lists. */
7a2fa0c2
WD
1449 if (cur_flist->next) {
1450 flist = first_flist->prev; /* the newest flist */
ef35abb2
WD
1451 future_cnt = flist->count + flist->ndx_start
1452 - cur_flist->next->ndx_start;
7a2fa0c2
WD
1453 } else
1454 future_cnt = 0;
1455 while (future_cnt < at_least) {
a6e7b978
WD
1456 struct file_struct *file = dir_flist->sorted[send_dir_ndx];
1457 int start = dir_flist->count;
c7e6f84f 1458 int32 *dp;
c7e6f84f
WD
1459
1460 flist = flist_new(0, "send_extra_file_list");
4c9d5fef 1461 start_write = stats.total_written;
c7e6f84f 1462
a6e7b978
WD
1463 /* If this is the first of a set of duplicate dirs, we must
1464 * send all the dirs together in a single file-list. We must
1465 * also send the index of the last dir in the header. */
1466 if (file->flags & FLAG_DUPLICATE) {
1467 int dir_ndx, end_ndx = send_dir_ndx;
1468 struct file_struct *fp = file;
1469
1470 while (1) {
1471 dp = F_DIRNODE_P(fp);
1472 end_ndx = DIR_NEXT_SIBLING(dp);
1473 fp = dir_flist->sorted[end_ndx];
1474 if (!(fp->flags & FLAG_DUPLICATE))
1475 break;
1476 }
c7e6f84f 1477
332cf6df 1478#ifdef ICONV_OPTION
a6e7b978
WD
1479 if (ic_ndx)
1480 dir_ndx = F_NDX(fp);
1481 else
1482#endif
1483 dir_ndx = end_ndx;
1484 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1485
1486 while (1) {
1487 send1extra(f, file, flist);
1488 if (send_dir_ndx == end_ndx)
1489 break;
1490 dp = F_DIRNODE_P(file);
1491 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1492 file = dir_flist->sorted[send_dir_ndx];
1493 }
1494 } else {
1495 int dir_ndx;
1496#ifdef ICONV_OPTION
1497 if (ic_ndx)
1498 dir_ndx = F_NDX(file);
1499 else
1500#endif
1501 dir_ndx = send_dir_ndx;
1502 write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
1503
1504 send1extra(f, file, flist);
1505 }
1506 write_byte(f, 0);
1507
76e754a0 1508#ifdef ICONV_OPTION
332cf6df 1509 if (!need_unsorted_flist)
76e754a0 1510#endif
a6e7b978
WD
1511 dir_flist->sorted = dir_flist->files;
1512 add_dirs_to_tree(send_dir_ndx, start, dir_flist->count - 1);
1513
1514#ifdef ICONV_OPTION
1515 if (need_unsorted_flist) {
1516 if (inc_recurse) {
1517 if (!(flist->sorted = new_array(struct file_struct *, flist->count)))
1518 out_of_memory("send_extra_file_list");
1519 memcpy(flist->sorted, flist->files,
1520 flist->count * sizeof (struct file_struct*));
1521 clean_flist(flist, 0);
1522 } else
1523 flist->sorted = flist->files;
1524 } else
332cf6df
WD
1525#endif
1526 {
1527 flist->sorted = flist->files;
a6e7b978 1528 clean_flist(flist, 0);
332cf6df 1529 }
a6e7b978 1530
c7e6f84f 1531 file_total += flist->count;
7a2fa0c2 1532 future_cnt += flist->count;
c7e6f84f
WD
1533 stats.flist_size += stats.total_written - start_write;
1534 stats.num_files += flist->count;
1535 if (verbose > 3)
1536 output_flist(flist);
1537
1538 dp = F_DIRNODE_P(file);
1539 if (DIR_FIRST_CHILD(dp) >= 0) {
1540 send_dir_ndx = DIR_FIRST_CHILD(dp);
1541 send_dir_depth++;
1542 } else {
1543 while (DIR_NEXT_SIBLING(dp) < 0) {
1544 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
9ae7a2cd 1545 write_ndx(f, NDX_FLIST_EOF);
c7e6f84f
WD
1546 flist_eof = 1;
1547 change_local_filter_dir(NULL, 0, 0);
1548 goto finish;
1549 }
1550 send_dir_depth--;
a6e7b978 1551 file = dir_flist->sorted[send_dir_ndx];
c7e6f84f
WD
1552 dp = F_DIRNODE_P(file);
1553 }
1554 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1555 }
301fb56c 1556 }
c7e6f84f
WD
1557
1558 finish:
1559 if (io_error != save_io_error && !ignore_errors)
1560 send_msg_int(MSG_IO_ERROR, io_error);
c627d613
AT
1561}
1562
ebed4c3a 1563struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1564{
3bb88b43
WD
1565 static const char *lastdir;
1566 static int lastdir_len = -1;
1567 int len, dirlen;
bcacc18b 1568 STRUCT_STAT st;
c7e6f84f 1569 char *p, *dir;
ebed4c3a 1570 char lastpath[MAXPATHLEN] = "";
649d65ed 1571 struct file_list *flist;
31b4d25d 1572 struct timeval start_tv, end_tv;
a800434a 1573 int64 start_write;
24d0fcde 1574 int use_ff_fd = 0;
c7e6f84f 1575 int flags, disable_buffering;
649d65ed 1576
ea124cb3 1577 rprintf(FLOG, "building file list\n");
134f4338 1578 if (show_filelist_p())
1bbd10fe 1579 start_filelist_progress("building file list");
3ea6e0e7 1580 else if (inc_recurse && verbose && !am_server)
98b1689d 1581 rprintf(FCLIENT, "sending incremental file list\n");
c627d613 1582
a800434a 1583 start_write = stats.total_written;
31b4d25d 1584 gettimeofday(&start_tv, NULL);
a800434a 1585
8cae71eb 1586#ifdef SUPPORT_HARD_LINKS
c7e6f84f 1587 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
8cae71eb
WD
1588 init_hard_links();
1589#endif
1590
c7e6f84f 1591 flist = cur_flist = flist_new(0, "send_file_list");
3ea6e0e7 1592 if (inc_recurse) {
c7e6f84f
WD
1593 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1594 flags = FLAG_DIVERT_DIRS;
1595 } else {
1596 dir_flist = cur_flist;
1597 flags = 0;
1598 }
c627d613 1599
c7e6f84f 1600 disable_buffering = io_start_buffering_out(f);
134f4338 1601 if (filesfrom_fd >= 0) {
6f3684ff 1602 if (argv[0] && !push_dir(argv[0], 0)) {
63d83e94
WD
1603 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1604 full_fname(argv[0]), curr_dir);
134f4338 1605 exit_cleanup(RERR_FILESELECT);
24d0fcde 1606 }
134f4338 1607 use_ff_fd = 1;
d6dead6b
AT
1608 }
1609
24d0fcde 1610 while (1) {
56f0c976
WD
1611 char fbuf[MAXPATHLEN];
1612 char *fn;
301fb56c 1613 int is_dot_dir;
c627d613 1614
24d0fcde 1615 if (use_ff_fd) {
56f0c976 1616 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
24d0fcde 1617 break;
a2248aea 1618 sanitize_path(fbuf, fbuf, "", 0, NULL);
24d0fcde
WD
1619 } else {
1620 if (argc-- == 0)
1621 break;
56f0c976 1622 strlcpy(fbuf, *argv++, MAXPATHLEN);
24d0fcde 1623 if (sanitize_paths)
a2248aea 1624 sanitize_path(fbuf, fbuf, "", 0, NULL);
24d0fcde 1625 }
c627d613 1626
56f0c976 1627 len = strlen(fbuf);
1902a765
WD
1628 if (relative_paths) {
1629 /* We clean up fbuf below. */
1630 is_dot_dir = 0;
1631 } else if (!len || fbuf[len - 1] == '/') {
56f0c976 1632 if (len == 2 && fbuf[0] == '.') {
6931c138 1633 /* Turn "./" into just "." rather than "./." */
56f0c976 1634 fbuf[1] = '\0';
557a35f5 1635 } else {
56f0c976 1636 if (len + 1 >= MAXPATHLEN)
a1f99493 1637 overflow_exit("send_file_list");
56f0c976
WD
1638 fbuf[len++] = '.';
1639 fbuf[len] = '\0';
53f821f1 1640 }
301fb56c 1641 is_dot_dir = 1;
56f0c976
WD
1642 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1643 && (len == 2 || fbuf[len-3] == '/')) {
1644 if (len + 2 >= MAXPATHLEN)
a1f99493 1645 overflow_exit("send_file_list");
56f0c976
WD
1646 fbuf[len++] = '/';
1647 fbuf[len++] = '.';
1648 fbuf[len] = '\0';
a289f89f 1649 is_dot_dir = 1;
301fb56c 1650 } else {
56f0c976
WD
1651 is_dot_dir = fbuf[len-1] == '.'
1652 && (len == 1 || fbuf[len-2] == '/');
649d65ed 1653 }
c627d613 1654
649d65ed
AT
1655 dir = NULL;
1656
1657 if (!relative_paths) {
56f0c976 1658 p = strrchr(fbuf, '/');
649d65ed 1659 if (p) {
151f59f1 1660 *p = '\0';
56f0c976 1661 if (p == fbuf)
649d65ed
AT
1662 dir = "/";
1663 else
56f0c976
WD
1664 dir = fbuf;
1665 len -= p - fbuf + 1;
1666 fn = p + 1;
1667 } else
1668 fn = fbuf;
1902a765
WD
1669 } else {
1670 if ((p = strstr(fbuf, "/./")) != NULL) {
1671 *p = '\0';
1672 if (p == fbuf)
1673 dir = "/";
1674 else
1675 dir = fbuf;
1676 len -= p - fbuf + 3;
1677 fn = p + 3;
1678 } else
1679 fn = fbuf;
1680 /* Get rid of trailing "/" and "/.". */
1681 while (len) {
ede1f0eb
WD
1682 if (fn[len - 1] == '/') {
1683 is_dot_dir = 1;
1684 if (!--len && !dir) {
1685 len++;
1686 break;
1687 }
1688 }
1902a765
WD
1689 else if (len >= 2 && fn[len - 1] == '.'
1690 && fn[len - 2] == '/') {
ede1f0eb 1691 is_dot_dir = 1;
1902a765
WD
1692 if (!(len -= 2) && !dir) {
1693 len++;
1694 break;
1695 }
1696 } else
1697 break;
1698 }
8c449e62
WD
1699 if (len == 1 && fn[0] == '/')
1700 fn[len++] = '.';
1902a765
WD
1701 fn[len] = '\0';
1702 /* Reject a ".." dir in the active part of the path. */
ede1f0eb
WD
1703 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1704 if ((p[2] == '/' || p[2] == '\0')
1705 && (p == fn || p[-1] == '/')) {
1706 rprintf(FERROR,
1707 "found \"..\" dir in relative path: %s\n",
1708 fbuf);
1709 exit_cleanup(RERR_SYNTAX);
1710 }
1902a765
WD
1711 }
1712 }
d2ea5980 1713
56f0c976
WD
1714 if (!*fn) {
1715 len = 1;
1716 fn = ".";
1717 }
d2ea5980 1718
3bb88b43
WD
1719 dirlen = dir ? strlen(dir) : 0;
1720 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
ef35abb2 1721 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
3bb88b43 1722 goto push_error;
ef35abb2
WD
1723 lastdir = pathname;
1724 lastdir_len = pathname_len;
1725 } else if (!push_pathname(lastdir, lastdir_len)) {
3bb88b43
WD
1726 push_error:
1727 io_error |= IOERR_GENERAL;
1728 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1729 full_fname(dir), curr_dir);
1730 continue;
d2ea5980
WD
1731 }
1732
99eba675 1733 if (fn != fbuf)
56f0c976
WD
1734 memmove(fbuf, fn, len + 1);
1735
25c2a6ac
WD
1736 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1737 io_error |= IOERR_GENERAL;
1738 rsyserr(FERROR, errno, "link_stat %s failed",
1739 full_fname(fbuf));
1740 continue;
1741 }
1742
1743 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1744 rprintf(FINFO, "skipping directory %s\n", fbuf);
1745 continue;
1746 }
1747
56f0c976 1748 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
151f59f1
WD
1749 /* Send the implied directories at the start of the
1750 * source spec, so we get their permissions right. */
56f0c976 1751 char *lp = lastpath, *slash = fbuf;
151f59f1 1752 *p = '\0';
2154309a
WD
1753 /* Skip any initial directories in our path that we
1754 * have in common with lastpath. */
56f0c976 1755 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
2154309a
WD
1756 if (*fn == '/')
1757 slash = fn;
2154309a
WD
1758 }
1759 *p = '/';
1760 if (fn != p || (*lp && *lp != '/')) {
0f57446d 1761 int save_copy_links = copy_links;
7e037c42 1762 int save_xfer_dirs = xfer_dirs;
3ea6e0e7 1763 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
88c2190a 1764 copy_links |= copy_unsafe_links;
7e037c42 1765 xfer_dirs = 1;
2154309a 1766 while ((slash = strchr(slash+1, '/')) != 0) {
151f59f1 1767 *slash = '\0';
42c6b139
WD
1768 send_file_name(f, flist, fbuf, NULL,
1769 dir_flags, ALL_FILTERS);
2154309a 1770 *slash = '/';
649d65ed 1771 }
0f57446d 1772 copy_links = save_copy_links;
7e037c42 1773 xfer_dirs = save_xfer_dirs;
151f59f1 1774 *p = '\0';
56f0c976 1775 strlcpy(lastpath, fbuf, sizeof lastpath);
649d65ed
AT
1776 *p = '/';
1777 }
1778 }
ebed4c3a 1779
535737bf
WD
1780 if (one_file_system)
1781 filesystem_dev = st.st_dev;
1782
75c51953 1783 if (recurse || (xfer_dirs && is_dot_dir)) {
42c6b139 1784 struct file_struct *file;
ef35abb2 1785 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR | flags;
42c6b139
WD
1786 file = send_file_name(f, flist, fbuf, &st,
1787 top_flags, ALL_FILTERS);
3ea6e0e7 1788 if (file && !inc_recurse)
42c6b139
WD
1789 send_if_directory(f, flist, file, fbuf, len, flags);
1790 } else
1791 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
649d65ed 1792 }
dc5ddbcc 1793
134f4338
WD
1794 gettimeofday(&end_tv, NULL);
1795 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1796 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1797 if (stats.flist_buildtime == 0)
1798 stats.flist_buildtime = 1;
1799 start_tv = end_tv;
31b4d25d 1800
8cae71eb
WD
1801 write_byte(f, 0); /* Indicate end of file list */
1802
1803#ifdef SUPPORT_HARD_LINKS
3ea6e0e7 1804 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
8cae71eb
WD
1805 idev_destroy();
1806#endif
c627d613 1807
134f4338
WD
1808 if (show_filelist_p())
1809 finish_filelist_progress(flist);
31b4d25d 1810
134f4338
WD
1811 gettimeofday(&end_tv, NULL);
1812 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1813 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 1814
332cf6df
WD
1815 /* When converting names, both sides keep an unsorted file-list array
1816 * because the names will differ on the sending and receiving sides
1817 * (both sides will use the unsorted index number for each item). */
1818
a6e7b978
WD
1819 /* Sort the list without removing any duplicates. This allows the
1820 * receiving side to ask for whatever name it kept. For incremental
1821 * recursion mode, the sender marks duplicate dirs so that it can
1822 * send them together in a single file-list. */
332cf6df
WD
1823#ifdef ICONV_OPTION
1824 if (need_unsorted_flist) {
1825 if (inc_recurse) {
1826 if (!(flist->sorted = new_array(struct file_struct *, flist->count)))
a6e7b978 1827 out_of_memory("send_file_list");
332cf6df
WD
1828 memcpy(flist->sorted, flist->files,
1829 flist->count * sizeof (struct file_struct*));
a6e7b978
WD
1830 clean_flist(flist, 0);
1831 } else
1832 flist->sorted = flist->files;
332cf6df
WD
1833 } else
1834#endif
1835 {
1836 flist->sorted = flist->files;
a6e7b978 1837 clean_flist(flist, 0);
332cf6df 1838 }
c7e6f84f 1839 file_total += flist->count;
ebed4c3a 1840
3ea6e0e7 1841 if (!numeric_ids && !inc_recurse)
1564cd5a 1842 send_id_list(f);
f6c34742 1843
134f4338 1844 /* send the io_error flag */
c7e6f84f
WD
1845 if (protocol_version < 30)
1846 write_int(f, ignore_errors ? 0 : io_error);
1847 else if (io_error && !ignore_errors)
1848 send_msg_int(MSG_IO_ERROR, io_error);
1849
1850 if (disable_buffering)
1851 io_end_buffering_out();
6ba9279f 1852
134f4338
WD
1853 stats.flist_size = stats.total_written - start_write;
1854 stats.num_files = flist->count;
d6dead6b 1855
cefed3e8 1856 if (verbose > 3)
32cbfe7b 1857 output_flist(flist);
cefed3e8 1858
17faa41c 1859 if (verbose > 2)
ebed4c3a 1860 rprintf(FINFO, "send_file_list done\n");
17faa41c 1861
3ea6e0e7 1862 if (inc_recurse) {
76e754a0 1863#ifdef ICONV_OPTION
a6e7b978 1864 if (!need_unsorted_flist)
76e754a0 1865#endif
a6e7b978 1866 dir_flist->sorted = dir_flist->files;
c7e6f84f 1867 add_dirs_to_tree(-1, 0, dir_flist->count - 1);
25c2a6ac
WD
1868 if (send_dir_ndx < 0) {
1869 write_ndx(f, NDX_FLIST_EOF);
1870 flist_eof = 1;
1871 }
1872 else if (file_total == 1) {
c7e6f84f
WD
1873 /* If we're creating incremental file-lists and there
1874 * was just 1 item in the first file-list, send 1 more
1875 * file-list to check if this is a 1-file xfer. */
25c2a6ac 1876 send_extra_file_list(f, 1);
c7e6f84f
WD
1877 }
1878 }
1879
649d65ed 1880 return flist;
c627d613
AT
1881}
1882
c627d613
AT
1883struct file_list *recv_file_list(int f)
1884{
ebed4c3a 1885 struct file_list *flist;
c7e6f84f 1886 int dstart, flags;
ebed4c3a 1887 int64 start_read;
c627d613 1888
98b1689d 1889 if (!first_flist)
c7e6f84f 1890 rprintf(FLOG, "receiving file list\n");
1bbd10fe
DD
1891 if (show_filelist_p())
1892 start_filelist_progress("receiving file list");
3ea6e0e7 1893 else if (inc_recurse && verbose && !am_server && !first_flist)
98b1689d 1894 rprintf(FCLIENT, "receiving incremental file list\n");
c627d613 1895
ebed4c3a 1896 start_read = stats.total_read;
a800434a 1897
c7e6f84f 1898 flist = flist_new(0, "recv_file_list");
c627d613 1899
8cae71eb
WD
1900#ifdef SUPPORT_HARD_LINKS
1901 if (preserve_hard_links && protocol_version < 30)
1902 init_hard_links();
1903#endif
c627d613 1904
3ea6e0e7 1905 if (inc_recurse) {
c7e6f84f
WD
1906 if (flist->ndx_start == 0)
1907 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1908 dstart = dir_flist->count;
1909 } else {
1910 dir_flist = flist;
1911 dstart = 0;
1912 }
1913
1ef00d20 1914 while ((flags = read_byte(f)) != 0) {
f5db0993 1915 struct file_struct *file;
dbda5fbf 1916
d9d6bc52 1917 flist_expand(flist);
c627d613 1918
d01d15e0 1919 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1920 flags |= read_byte(f) << 8;
82ad07c4 1921 file = recv_file_entry(flist, flags, f);
c627d613 1922
3ea6e0e7 1923 if (inc_recurse && S_ISDIR(file->mode)) {
c7e6f84f 1924 flist_expand(dir_flist);
76e754a0 1925#ifdef ICONV_OPTION
a6e7b978
WD
1926 if (need_unsorted_flist)
1927 dir_flist->sorted[dir_flist->count] = file;
76e754a0 1928#endif
c7e6f84f
WD
1929 dir_flist->files[dir_flist->count++] = file;
1930 }
1931
f5db0993 1932 flist->files[flist->count++] = file;
c627d613 1933
53135fe8 1934 maybe_emit_filelist_progress(flist->count);
1bbd10fe 1935
5e4ff5f9
WD
1936 if (verbose > 2) {
1937 rprintf(FINFO, "recv_file_name(%s)\n",
1938 f_name(file, NULL));
1939 }
ebed4c3a 1940 }
c7e6f84f 1941 file_total += flist->count;
c627d613 1942
ebed4c3a
MP
1943 if (verbose > 2)
1944 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1945
b7736c79 1946 if (show_filelist_p())
1bbd10fe 1947 finish_filelist_progress(flist);
a06d19e3 1948
332cf6df
WD
1949#ifdef ICONV_OPTION
1950 if (need_unsorted_flist) {
1951 /* Create an extra array of index pointers that we can sort for
1952 * the generator's use (for wading through the files in sorted
1953 * order and for calling flist_find()). We keep the "files"
1954 * list unsorted for our exchange of index numbers with the
1955 * other side (since their names may not sort the same). */
1956 if (!(flist->sorted = new_array(struct file_struct *, flist->count)))
1957 out_of_memory("recv_file_list");
1958 memcpy(flist->sorted, flist->files,
1959 flist->count * sizeof (struct file_struct*));
a6e7b978
WD
1960 if (inc_recurse) {
1961 qsort(dir_flist->sorted + dstart, dir_flist->count - dstart,
1962 sizeof (struct file_struct*), (int (*)())file_compare);
1963 }
332cf6df
WD
1964 } else
1965#endif
a6e7b978 1966 {
332cf6df 1967 flist->sorted = flist->files;
a6e7b978
WD
1968 if (inc_recurse) {
1969 dir_flist->sorted = dir_flist->files;
1970 qsort(dir_flist->sorted + dstart, dir_flist->count - dstart,
1971 sizeof (struct file_struct*), (int (*)())file_compare);
1972 }
1973 }
332cf6df 1974
a6e7b978 1975 if (!inc_recurse && f >= 0)
1564cd5a 1976 recv_id_list(f, flist);
f6c34742 1977
a6e7b978
WD
1978 clean_flist(flist, relative_paths);
1979
c7e6f84f 1980 if (protocol_version < 30) {
b9f592fb 1981 /* Recv the io_error flag */
1f56188f 1982 if (ignore_errors)
b9f592fb
WD
1983 read_int(f);
1984 else
1985 io_error |= read_int(f);
ebed4c3a 1986 }
6ba9279f 1987
cefed3e8 1988 if (verbose > 3)
32cbfe7b 1989 output_flist(flist);
cefed3e8 1990
ebed4c3a
MP
1991 if (list_only) {
1992 int i;
b7736c79 1993 for (i = 0; i < flist->count; i++)
ebed4c3a 1994 list_file_entry(flist->files[i]);
ebed4c3a 1995 }
f7632fc6 1996
ebed4c3a
MP
1997 if (verbose > 2)
1998 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1999
c7e6f84f
WD
2000 stats.flist_size += stats.total_read - start_read;
2001 stats.num_files += flist->count;
a800434a 2002
ebed4c3a 2003 return flist;
c627d613
AT
2004}
2005
c7e6f84f
WD
2006/* This is only used once by the receiver if the very first file-list
2007 * has exactly one item in it. */
2008void recv_additional_file_list(int f)
c627d613 2009{
c7e6f84f 2010 struct file_list *flist;
9ae7a2cd 2011 int ndx = read_ndx(f);
25c2a6ac 2012 if (ndx == NDX_FLIST_EOF) {
c7e6f84f
WD
2013 flist_eof = 1;
2014 change_local_filter_dir(NULL, 0, 0);
2015 } else {
2016 ndx = NDX_FLIST_OFFSET - ndx;
2017 if (ndx < 0 || ndx >= dir_flist->count) {
2018 ndx = NDX_FLIST_OFFSET - ndx;
2019 rprintf(FERROR,
2020 "Invalid dir index: %d (%d - %d)\n",
2021 ndx, NDX_FLIST_OFFSET,
2022 NDX_FLIST_OFFSET - dir_flist->count);
2023 exit_cleanup(RERR_PROTOCOL);
2024 }
2025 flist = recv_file_list(f);
2026 flist->parent_ndx = ndx;
2027 }
c627d613
AT
2028}
2029
f5db0993
WD
2030/* Search for an identically-named item in the file list. Note that the
2031 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 2032int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 2033{
f3c3ed44 2034 int low = flist->low, high = flist->high;
207522ae 2035 int diff, mid, mid_up;
f3c3ed44
WD
2036
2037 while (low <= high) {
2038 mid = (low + high) / 2;
332cf6df 2039 if (F_IS_ACTIVE(flist->sorted[mid]))
c0b134a4 2040 mid_up = mid;
7402d583
WD
2041 else {
2042 /* Scan for the next non-empty entry using the cached
2043 * distance values. If the value isn't fully up-to-
2044 * date, update it. */
332cf6df
WD
2045 mid_up = mid + F_DEPTH(flist->sorted[mid]);
2046 if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
85aecef6 2047 do {
332cf6df
WD
2048 mid_up += F_DEPTH(flist->sorted[mid_up]);
2049 } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2050 F_DEPTH(flist->sorted[mid]) = mid_up - mid;
c0b134a4 2051 }
c0b134a4 2052 if (mid_up > high) {
7402d583
WD
2053 /* If there's nothing left above us, set high to
2054 * a non-empty entry below us and continue. */
332cf6df
WD
2055 high = mid - (int)flist->sorted[mid]->len32;
2056 if (!F_IS_ACTIVE(flist->sorted[high])) {
85aecef6 2057 do {
332cf6df
WD
2058 high -= (int)flist->sorted[high]->len32;
2059 } while (!F_IS_ACTIVE(flist->sorted[high]));
2060 flist->sorted[mid]->len32 = mid - high;
7402d583 2061 }
c0b134a4
WD
2062 continue;
2063 }
c0b134a4 2064 }
332cf6df 2065 diff = f_name_cmp(flist->sorted[mid_up], f);
207522ae 2066 if (diff == 0) {
f5db0993 2067 if (protocol_version < 29
332cf6df 2068 && S_ISDIR(flist->sorted[mid_up]->mode)
f5db0993
WD
2069 != S_ISDIR(f->mode))
2070 return -1;
f3c3ed44 2071 return mid_up;
f5db0993 2072 }
207522ae 2073 if (diff < 0)
f3c3ed44 2074 low = mid_up + 1;
207522ae
WD
2075 else
2076 high = mid - 1;
d966ee25 2077 }
d966ee25 2078 return -1;
c627d613
AT
2079}
2080
3ec4dd97 2081/*
9935066b
S
2082 * Free up any resources a file_struct has allocated
2083 * and clear the file.
3ec4dd97 2084 */
82ad07c4 2085void clear_file(struct file_struct *file)
c627d613 2086{
a3e18c76
WD
2087 /* The +1 zeros out the first char of the basename. */
2088 memset(file, 0, FILE_STRUCT_LEN + 1);
9d737ecb 2089 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
96293cf9 2090 * entry. Likewise for len32 in the opposite direction. We assume
0679ac4c
WD
2091 * that we're alone for now since flist_find() will adjust the counts
2092 * it runs into that aren't up-to-date. */
9d737ecb 2093 file->len32 = F_DEPTH(file) = 1;
3ec4dd97 2094}
c627d613 2095
4785cd43 2096/* Allocate a new file list. */
c7e6f84f 2097struct file_list *flist_new(int flags, char *msg)
3d382777
AT
2098{
2099 struct file_list *flist;
2100
58cadc86 2101 flist = new(struct file_list);
ebed4c3a 2102 if (!flist)
9935066b 2103 out_of_memory(msg);
3d382777 2104
82ad07c4 2105 memset(flist, 0, sizeof flist[0]);
9935066b 2106
c7e6f84f
WD
2107 if (!(flags & FLIST_TEMP)) {
2108 if (first_flist) {
2109 flist->ndx_start = first_flist->prev->ndx_start
2110 + first_flist->prev->count;
2111 }
2112 /* This is a doubly linked list with prev looping back to
2113 * the end of the list, but the last next pointer is NULL. */
2114 if (!first_flist)
2115 first_flist = cur_flist = flist->prev = flist;
2116 else {
2117 flist->prev = first_flist->prev;
2118 flist->prev->next = first_flist->prev = flist;
2119 }
2120 flist_cnt++;
2121 }
2122
4a19c3b2 2123 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
9935066b
S
2124 out_of_memory(msg);
2125
3d382777
AT
2126 return flist;
2127}
ebed4c3a 2128
4785cd43 2129/* Free up all elements in a flist. */
3ec4dd97
AT
2130void flist_free(struct file_list *flist)
2131{
c7e6f84f
WD
2132 if (!flist->prev)
2133 ; /* Was FLIST_TEMP dir-list. */
2134 else if (flist == flist->prev) {
2135 first_flist = cur_flist = NULL;
2136 file_total = 0;
2137 flist_cnt = 0;
2138 } else {
2139 if (flist == cur_flist)
2140 cur_flist = flist->next;
2141 if (flist == first_flist)
2142 first_flist = first_flist->next;
2143 else {
2144 flist->prev->next = flist->next;
2145 if (!flist->next)
2146 flist->next = first_flist;
2147 }
2148 flist->next->prev = flist->prev;
2149 file_total -= flist->count;
2150 flist_cnt--;
2151 }
2152
9935066b 2153 pool_destroy(flist->file_pool);
332cf6df
WD
2154 if (flist->sorted && flist->sorted != flist->files)
2155 free(flist->sorted);
3ec4dd97 2156 free(flist->files);
3ec4dd97 2157 free(flist);
c627d613
AT
2158}
2159
a6e7b978
WD
2160/* This routine ensures we don't have any duplicate names in our file list.
2161 * duplicate names can cause corruption because of the pipelining. */
2162static void clean_flist(struct file_list *flist, int strip_root)
c627d613 2163{
85aecef6 2164 char fbuf[MAXPATHLEN];
a6e7b978 2165 int i, prev_i;
c627d613 2166
910ee8c9
WD
2167 if (!flist)
2168 return;
2169 if (flist->count == 0) {
2170 flist->high = -1;
3ec4dd97 2171 return;
910ee8c9 2172 }
3ec4dd97 2173
332cf6df
WD
2174 qsort(flist->sorted, flist->count,
2175 sizeof flist->sorted[0], (int (*)())file_compare);
ebed4c3a 2176
a6e7b978
WD
2177 if (!am_sender || inc_recurse) {
2178 for (i = prev_i = 0; i < flist->count; i++) {
2179 if (F_IS_ACTIVE(flist->sorted[i])) {
2180 prev_i = i;
2181 break;
2182 }
b91b50c0 2183 }
a6e7b978
WD
2184 flist->low = prev_i;
2185 } else {
2186 i = prev_i = flist->count - 1;
2187 flist->low = 0;
b91b50c0 2188 }
a6e7b978 2189
b91b50c0 2190 while (++i < flist->count) {
fe1c19dc 2191 int j;
332cf6df 2192 struct file_struct *file = flist->sorted[i];
f5db0993 2193
96293cf9 2194 if (!F_IS_ACTIVE(file))
b91b50c0 2195 continue;
332cf6df 2196 if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
fe1c19dc
WD
2197 j = prev_i;
2198 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
2199 int save_mode = file->mode;
2200 /* Make sure that this directory doesn't duplicate a
2201 * non-directory earlier in the list. */
f5db0993 2202 flist->high = prev_i;
fe1c19dc
WD
2203 file->mode = S_IFREG;
2204 j = flist_find(flist, file);
f5db0993 2205 file->mode = save_mode;
fe1c19dc
WD
2206 } else
2207 j = -1;
2208 if (j >= 0) {
332cf6df 2209 struct file_struct *fp = flist->sorted[j];
fe1c19dc
WD
2210 int keep, drop;
2211 /* If one is a dir and the other is not, we want to
2212 * keep the dir because it might have contents in the
2213 * list. */
2214 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
2215 if (S_ISDIR(file->mode))
2216 keep = i, drop = j;
2217 else
2218 keep = j, drop = i;
c7e6f84f 2219 } else if (protocol_version < 27)
fe1c19dc 2220 keep = j, drop = i;
c7e6f84f
WD
2221 else
2222 keep = i, drop = j;
fe1c19dc 2223
a6e7b978
WD
2224 if (am_sender)
2225 flist->sorted[drop]->flags |= FLAG_DUPLICATE;
2226 else {
2227 if (verbose > 1) {
2228 rprintf(FINFO,
2229 "removing duplicate name %s from file list (%d)\n",
2230 f_name(file, fbuf), drop);
2231 }
2232 /* Make sure we don't lose track of a user-specified
2233 * top directory. */
2234 flist->sorted[keep]->flags |= flist->sorted[drop]->flags
2235 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
2236
2237 clear_file(flist->sorted[drop]);
2238 }
9935066b 2239
fe1c19dc
WD
2240 if (keep == i) {
2241 if (flist->low == drop) {
2242 for (j = drop + 1;
332cf6df 2243 j < i && !F_IS_ACTIVE(flist->sorted[j]);
fe1c19dc
WD
2244 j++) {}
2245 flist->low = j;
2246 }
2247 prev_i = i;
2248 }
728d0922 2249 } else
6931c138 2250 prev_i = i;
3ec4dd97 2251 }
a6e7b978 2252 flist->high = prev_i;
0199b05f 2253
c0b134a4
WD
2254 if (strip_root) {
2255 /* We need to strip off the leading slashes for relative
2256 * paths, but this must be done _after_ the sorting phase. */
2257 for (i = flist->low; i <= flist->high; i++) {
332cf6df 2258 struct file_struct *file = flist->sorted[i];
c0b134a4
WD
2259
2260 if (!file->dirname)
2261 continue;
2262 while (*file->dirname == '/')
2263 file->dirname++;
2264 if (!*file->dirname)
2265 file->dirname = NULL;
2266 }
2267 }
2268
a6e7b978 2269 if (prune_empty_dirs && !am_sender) {
e6ffb966 2270 int j, prev_depth = 0;
9c000f5e 2271
e6ffb966 2272 prev_i = 0; /* It's OK that this isn't really true. */
9c000f5e 2273
f5db0993 2274 for (i = flist->low; i <= flist->high; i++) {
332cf6df 2275 struct file_struct *fp, *file = flist->sorted[i];
ebed4c3a 2276
9d737ecb 2277 /* This temporarily abuses the F_DEPTH() value for a
e6ffb966
WD
2278 * directory that is in a chain that might get pruned.
2279 * We restore the old value if it gets a reprieve. */
9d737ecb 2280 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
e6ffb966 2281 /* Dump empty dirs when coming back down. */
9d737ecb 2282 for (j = prev_depth; j >= F_DEPTH(file); j--) {
332cf6df 2283 fp = flist->sorted[prev_i];
9d737ecb 2284 if (F_DEPTH(fp) >= 0)
e6ffb966 2285 break;
9d737ecb 2286 prev_i = -F_DEPTH(fp)-1;
82ad07c4 2287 clear_file(fp);
9c000f5e 2288 }
9d737ecb 2289 prev_depth = F_DEPTH(file);
85aecef6
WD
2290 if (is_excluded(f_name(file, fbuf), 1,
2291 ALL_FILTERS)) {
e6ffb966
WD
2292 /* Keep dirs through this dir. */
2293 for (j = prev_depth-1; ; j--) {
332cf6df 2294 fp = flist->sorted[prev_i];
9d737ecb 2295 if (F_DEPTH(fp) >= 0)
e6ffb966 2296 break;
9d737ecb
WD
2297 prev_i = -F_DEPTH(fp)-1;
2298 F_DEPTH(fp) = j;
e6ffb966 2299 }
85aecef6 2300 } else
9d737ecb 2301 F_DEPTH(file) = -prev_i-1;
e6ffb966
WD
2302 prev_i = i;
2303 } else {
2304 /* Keep dirs through this non-dir. */
2305 for (j = prev_depth; ; j--) {
332cf6df 2306 fp = flist->sorted[prev_i];
9d737ecb 2307 if (F_DEPTH(fp) >= 0)
e6ffb966 2308 break;
9d737ecb
WD
2309 prev_i = -F_DEPTH(fp)-1;
2310 F_DEPTH(fp) = j;
e6ffb966 2311 }
0199b05f 2312 }
9c000f5e 2313 }
ca947dea 2314 /* Dump all remaining empty dirs. */
e6ffb966 2315 while (1) {
332cf6df 2316 struct file_struct *fp = flist->sorted[prev_i];
9d737ecb 2317 if (F_DEPTH(fp) >= 0)
e6ffb966 2318 break;
9d737ecb 2319 prev_i = -F_DEPTH(fp)-1;
82ad07c4 2320 clear_file(fp);
9c000f5e 2321 }
f5db0993 2322
9c000f5e 2323 for (i = flist->low; i <= flist->high; i++) {
332cf6df 2324 if (F_IS_ACTIVE(flist->sorted[i]))
9c000f5e
WD
2325 break;
2326 }
2327 flist->low = i;
2328 for (i = flist->high; i >= flist->low; i--) {
332cf6df 2329 if (F_IS_ACTIVE(flist->sorted[i]))
9c000f5e 2330 break;
0199b05f 2331 }
9c000f5e 2332 flist->high = i;
0199b05f 2333 }
cefed3e8 2334}
0199b05f 2335
32cbfe7b 2336static void output_flist(struct file_list *flist)
cefed3e8 2337{
f3c3ed44 2338 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8 2339 struct file_struct *file;
9d737ecb 2340 const char *root, *dir, *slash, *name, *trail;
32cbfe7b 2341 const char *who = who_am_i();
cefed3e8 2342 int i;
0199b05f 2343
c7e6f84f
WD
2344 rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n",
2345 who, flist->ndx_start, flist->count, flist->low, flist->high);
ebed4c3a 2346 for (i = 0; i < flist->count; i++) {
a6e7b978 2347 file = flist->sorted[i];
82ad07c4 2348 if ((am_root || am_sender) && preserve_uid) {
1564cd5a
WD
2349 snprintf(uidbuf, sizeof uidbuf, " uid=%u",
2350 F_OWNER(file));
82ad07c4 2351 } else
f05f993e 2352 *uidbuf = '\0';
1564cd5a
WD
2353 if (preserve_gid) {
2354 static char parens[] = "(\0)\0\0\0";
2355 char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
2356 snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
2357 pp, F_GROUP(file), pp + 2);
82ad07c4 2358 } else
f05f993e 2359 *gidbuf = '\0';
f3c3ed44 2360 if (!am_sender)
9d737ecb 2361 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
96293cf9 2362 if (F_IS_ACTIVE(file)) {
ef35abb2 2363 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
96293cf9
WD
2364 if ((dir = file->dirname) == NULL)
2365 dir = slash = "";
2366 else
2367 slash = "/";
c58c1dc4 2368 name = file->basename;
96293cf9
WD
2369 trail = S_ISDIR(file->mode) ? "/" : "";
2370 } else
9d737ecb 2371 root = dir = slash = name = trail = "";
14698a3a 2372 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
9d737ecb 2373 who, i, root, dir, slash, name, trail, (int)file->mode,
96293cf9 2374 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
0199b05f 2375 }
3ec4dd97
AT
2376}
2377
8824e2ce 2378enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 2379enum fnc_type { t_PATH, t_ITEM };
8018edd3 2380
2f3cad89 2381/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
2382 * would do if it were operating on the joined strings.
2383 *
2384 * Some differences beginning with protocol_version 29: (1) directory names
2385 * are compared with an assumed trailing slash so that they compare in a
2386 * way that would cause them to sort immediately prior to any content they
2387 * may have; (2) a directory of any name compares after a non-directory of
2388 * any name at the same depth; (3) a directory with name "." compares prior
2389 * to anything else. These changes mean that a directory and a non-dir
2390 * with the same name will not compare as equal (protocol_version >= 29).
2391 *
2392 * The dirname component can be an empty string, but the basename component
2393 * cannot (and never is in the current codebase). The basename component
2394 * may be NULL (for a removed item), in which case it is considered to be
2395 * after any existing item. */
8018edd3 2396int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 2397{
8018edd3
WD
2398 int dif;
2399 const uchar *c1, *c2;
1ef00d20 2400 enum fnc_state state1, state2;
f5db0993
WD
2401 enum fnc_type type1, type2;
2402 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3 2403
96293cf9
WD
2404 if (!f1 || !F_IS_ACTIVE(f1)) {
2405 if (!f2 || !F_IS_ACTIVE(f2))
8018edd3
WD
2406 return 0;
2407 return -1;
2408 }
96293cf9 2409 if (!f2 || !F_IS_ACTIVE(f2))
8018edd3
WD
2410 return 1;
2411
14698a3a
WD
2412 c1 = (uchar*)f1->dirname;
2413 c2 = (uchar*)f2->dirname;
2414 if (c1 == c2)
2415 c1 = c2 = NULL;
2416 if (!c1) {
f5db0993 2417 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
c58c1dc4 2418 c1 = (const uchar*)f1->basename;
f5db0993
WD
2419 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2420 type1 = t_ITEM;
2421 state1 = s_TRAILING;
2422 c1 = (uchar*)"";
2423 } else
2424 state1 = s_BASE;
f5db0993
WD
2425 } else {
2426 type1 = t_path;
8824e2ce 2427 state1 = s_DIR;
f5db0993 2428 }
14698a3a 2429 if (!c2) {
f5db0993 2430 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
c58c1dc4 2431 c2 = (const uchar*)f2->basename;
f5db0993
WD
2432 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2433 type2 = t_ITEM;
2434 state2 = s_TRAILING;
2435 c2 = (uchar*)"";
2436 } else
2437 state2 = s_BASE;
f5db0993
WD
2438 } else {
2439 type2 = t_path;
8824e2ce 2440 state2 = s_DIR;
f5db0993
WD
2441 }
2442
2443 if (type1 != type2)
2444 return type1 == t_PATH ? 1 : -1;
8018edd3 2445
e4fdf1de 2446 do {
f5db0993 2447 if (!*c1) {
8018edd3 2448 switch (state1) {
8824e2ce
WD
2449 case s_DIR:
2450 state1 = s_SLASH;
e90b8ace 2451 c1 = (uchar*)"/";
8018edd3 2452 break;
8824e2ce 2453 case s_SLASH:
f5db0993 2454 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
c58c1dc4 2455 c1 = (const uchar*)f1->basename;
cbb5fa4f
WD
2456 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2457 type1 = t_ITEM;
2458 state1 = s_TRAILING;
2459 c1 = (uchar*)"";
2460 } else
2461 state1 = s_BASE;
8018edd3 2462 break;
8824e2ce
WD
2463 case s_BASE:
2464 state1 = s_TRAILING;
f5db0993 2465 if (type1 == t_PATH) {
2f3cad89 2466 c1 = (uchar*)"/";
f5db0993
WD
2467 break;
2468 }
2469 /* FALL THROUGH */
8824e2ce 2470 case s_TRAILING:
f5db0993 2471 type1 = t_ITEM;
8018edd3
WD
2472 break;
2473 }
f5db0993
WD
2474 if (*c2 && type1 != type2)
2475 return type1 == t_PATH ? 1 : -1;
8018edd3 2476 }
f5db0993 2477 if (!*c2) {
8018edd3 2478 switch (state2) {
8824e2ce
WD
2479 case s_DIR:
2480 state2 = s_SLASH;
e90b8ace 2481 c2 = (uchar*)"/";
8018edd3 2482 break;
8824e2ce 2483 case s_SLASH:
f5db0993 2484 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
c58c1dc4 2485 c2 = (const uchar*)f2->basename;
cbb5fa4f
WD
2486 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2487 type2 = t_ITEM;
2488 state2 = s_TRAILING;
2489 c2 = (uchar*)"";
2490 } else
2491 state2 = s_BASE;
8018edd3 2492 break;
8824e2ce 2493 case s_BASE:
8824e2ce 2494 state2 = s_TRAILING;
f5db0993 2495 if (type2 == t_PATH) {
2f3cad89 2496 c2 = (uchar*)"/";
f5db0993
WD
2497 break;
2498 }
2499 /* FALL THROUGH */
8824e2ce 2500 case s_TRAILING:
f5db0993
WD
2501 if (!*c1)
2502 return 0;
2503 type2 = t_ITEM;
8018edd3
WD
2504 break;
2505 }
f5db0993
WD
2506 if (type1 != type2)
2507 return type1 == t_PATH ? 1 : -1;
8018edd3 2508 }
e4fdf1de 2509 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
8018edd3
WD
2510
2511 return dif;
2512}
2513
96293cf9
WD
2514char *f_name_buf(void)
2515{
2516 static char names[5][MAXPATHLEN];
2517 static unsigned int n;
2518
2519 n = (n + 1) % (sizeof names / sizeof names[0]);
2520
2521 return names[n];
2522}
2523
8018edd3 2524/* Return a copy of the full filename of a flist entry, using the indicated
5e4ff5f9
WD
2525 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2526 * done because we checked the size when creating the file_struct entry.
8018edd3 2527 */
5e4ff5f9 2528char *f_name(struct file_struct *f, char *fbuf)
8018edd3 2529{
96293cf9 2530 if (!f || !F_IS_ACTIVE(f))
ebed4c3a 2531 return NULL;
3ec4dd97 2532
96293cf9
WD
2533 if (!fbuf)
2534 fbuf = f_name_buf();
5e4ff5f9 2535
3ec4dd97 2536 if (f->dirname) {
882e6893
WD
2537 int len = strlen(f->dirname);
2538 memcpy(fbuf, f->dirname, len);
2539 fbuf[len] = '/';
c58c1dc4 2540 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
8018edd3 2541 } else
c58c1dc4 2542 strlcpy(fbuf, f->basename, MAXPATHLEN);
0ee6ca98 2543
b7736c79 2544 return fbuf;
8018edd3 2545}
e03dfae5 2546
32cbfe7b
WD
2547/* Do a non-recursive scan of the named directory, possibly ignoring all
2548 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2549 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2550 * buffer (the functions we call will append names onto the end, but the old
2551 * dir value will be restored on exit). */
263d3bfb 2552struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
37802f40
WD
2553{
2554 struct file_list *dirlist;
2555 char dirbuf[MAXPATHLEN];
37802f40 2556 int save_recurse = recurse;
1661fe9b 2557 int save_xfer_dirs = xfer_dirs;
37802f40 2558
32cbfe7b
WD
2559 if (dlen < 0) {
2560 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2561 if (dlen >= MAXPATHLEN)
2562 return NULL;
2563 dirname = dirbuf;
2564 }
37802f40 2565
c7e6f84f 2566 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
32cbfe7b 2567
37802f40 2568 recurse = 0;
1661fe9b 2569 xfer_dirs = 1;
a6e7b978 2570 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
1661fe9b 2571 xfer_dirs = save_xfer_dirs;
37802f40 2572 recurse = save_recurse;
53135fe8
WD
2573 if (do_progress)
2574 flist_count_offset += dirlist->count;
37802f40 2575
332cf6df 2576 dirlist->sorted = dirlist->files;
a6e7b978 2577 clean_flist(dirlist, 0);
564ef546 2578
45478cc7 2579 if (verbose > 3)
32cbfe7b 2580 output_flist(dirlist);
45478cc7 2581
32cbfe7b 2582 return dirlist;
45478cc7 2583}