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