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