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