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