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