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