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