Restored write_ndx_and_attrs() and made it public.
[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>
7 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
736a6a29 13 *
0f78b815
WD
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
172875cf 18 *
e7c67065
WD
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 22 */
c627d613
AT
23
24#include "rsync.h"
25
26extern int verbose;
34e18ecd 27extern int list_only;
f05f993e 28extern int am_root;
c627d613 29extern int am_server;
a8726d2a 30extern int am_daemon;
56194bcd 31extern int am_sender;
8715db2c 32extern int do_progress;
c627d613 33extern int always_checksum;
983b1ed3
WD
34extern int module_id;
35extern int ignore_errors;
4836c3ee 36extern int numeric_ids;
a06d19e3 37extern int recurse;
7e037c42 38extern int xfer_dirs;
24d0fcde 39extern int filesfrom_fd;
c627d613 40extern int one_file_system;
88c2190a 41extern int copy_dirlinks;
314f4591 42extern int keep_dirlinks;
c627d613 43extern int preserve_links;
dc5ddbcc 44extern int preserve_hard_links;
c627d613 45extern int preserve_devices;
b5c6a6ae 46extern int preserve_specials;
c627d613
AT
47extern int preserve_uid;
48extern int preserve_gid;
6574b4f7 49extern int relative_paths;
24d0fcde 50extern int implied_dirs;
53039410
WD
51extern int ignore_perishable;
52extern int non_perishable_cnt;
85aecef6 53extern int prune_empty_dirs;
82306bf6 54extern int copy_links;
b5313607 55extern int copy_unsafe_links;
d04e9c51 56extern int protocol_version;
cb13abfe 57extern int sanitize_paths;
d64e6f42
WD
58extern struct stats stats;
59extern struct file_list *the_file_list;
c627d613 60
e1f40891
WD
61extern char curr_dir[MAXPATHLEN];
62
2b7e0f33
WD
63extern struct chmod_mode_struct *chmod_modes;
64
7842418b
WD
65extern struct filter_list_struct filter_list;
66extern struct filter_list_struct server_filter_list;
c627d613 67
06c28400 68int io_error;
7752df41 69int checksum_len;
535737bf 70dev_t filesystem_dev; /* used to implement -x */
5e58e3f9 71unsigned int file_struct_len;
06c28400 72
fea4db62 73static char empty_sum[MD4_SUM_LENGTH];
53135fe8 74static int flist_count_offset;
3d382777 75
827c37f6 76static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
32cbfe7b 77static void output_flist(struct file_list *flist);
0199b05f 78
61dec11a
WD
79void init_flist(void)
80{
1f9ae80a 81 struct file_struct f;
61dec11a 82
1f9ae80a 83 /* Figure out how big the file_struct is without trailing padding */
7cf8e8d0 84 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
7752df41 85 checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
61dec11a
WD
86}
87
1bbd10fe 88static int show_filelist_p(void)
58225000 89{
7e037c42 90 return verbose && xfer_dirs && !am_server;
1bbd10fe 91}
ebed4c3a 92
1bbd10fe
DD
93static void start_filelist_progress(char *kind)
94{
ea124cb3 95 rprintf(FCLIENT, "%s ... ", kind);
e5ce3bcf 96 if (verbose > 1 || do_progress)
ea124cb3 97 rprintf(FCLIENT, "\n");
1bbd10fe 98 rflush(FINFO);
58225000
MP
99}
100
53135fe8 101static void emit_filelist_progress(int count)
db719fb0 102{
ea124cb3 103 rprintf(FCLIENT, " %d files...\r", count);
db719fb0
MP
104}
105
53135fe8 106static void maybe_emit_filelist_progress(int count)
58225000 107{
53135fe8
WD
108 if (do_progress && show_filelist_p() && (count % 100) == 0)
109 emit_filelist_progress(count);
58225000
MP
110}
111
1bbd10fe 112static void finish_filelist_progress(const struct file_list *flist)
58225000 113{
1bbd10fe
DD
114 if (do_progress) {
115 /* This overwrites the progress line */
c7b562be
MP
116 rprintf(FINFO, "%d file%sto consider\n",
117 flist->count, flist->count == 1 ? " " : "s ");
b7736c79 118 } else
1bbd10fe 119 rprintf(FINFO, "done\n");
58225000
MP
120}
121
86943126
MP
122void show_flist_stats(void)
123{
124 /* Nothing yet */
125}
126
f7632fc6
AT
127static void list_file_entry(struct file_struct *f)
128{
78d146e8 129 char permbuf[PERMSTRING_SIZE];
f7632fc6 130
e5ce3bcf 131 if (!f->basename) {
7212be92
DD
132 /* this can happen if duplicate names were removed */
133 return;
e5ce3bcf 134 }
7212be92 135
78d146e8 136 permstring(permbuf, f->mode);
740819ef 137
4f5b0756 138#ifdef SUPPORT_LINKS
f7632fc6 139 if (preserve_links && S_ISLNK(f->mode)) {
ebed4c3a 140 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
78d146e8 141 permbuf,
a4a7e64c 142 (double)f->length, timestring(f->modtime),
5e4ff5f9 143 f_name(f, NULL), f->u.link);
0d162bd1
WD
144 } else
145#endif
e5ce3bcf 146 {
ebed4c3a 147 rprintf(FINFO, "%s %11.0f %s %s\n",
78d146e8 148 permbuf,
a4a7e64c 149 (double)f->length, timestring(f->modtime),
5e4ff5f9 150 f_name(f, NULL));
e5ce3bcf 151 }
f7632fc6
AT
152}
153
cad8f6f9
WD
154/* Stat either a symlink or its referent, depending on the settings of
155 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
4e5db0ad 156 *
cad8f6f9
WD
157 * If path is the name of a symlink, then the linkbuf buffer (which must hold
158 * MAXPATHLEN chars) will be set to the symlink's target string.
4e5db0ad 159 *
cad8f6f9
WD
160 * The stat structure pointed to by stp will contain information about the
161 * link or the referent as appropriate, if they exist. */
162static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
b5313607 163{
4f5b0756 164#ifdef SUPPORT_LINKS
cad8f6f9 165 if (link_stat(path, stp, copy_dirlinks) < 0)
b5313607 166 return -1;
cad8f6f9
WD
167 if (S_ISLNK(stp->st_mode)) {
168 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
169 if (llen < 0)
b5313607 170 return -1;
cad8f6f9 171 linkbuf[llen] = '\0';
fc638474
DD
172 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
173 if (verbose > 1) {
dbda5fbf 174 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
0ee6ca98 175 path, linkbuf);
fc638474 176 }
1a7f3d99 177 return do_stat(path, stp);
b5313607
DD
178 }
179 }
180 return 0;
181#else
cad8f6f9 182 return do_stat(path, stp);
b5313607
DD
183#endif
184}
185
cad8f6f9 186int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
82306bf6 187{
4f5b0756 188#ifdef SUPPORT_LINKS
b7736c79 189 if (copy_links)
1a7f3d99 190 return do_stat(path, stp);
cad8f6f9 191 if (do_lstat(path, stp) < 0)
314f4591 192 return -1;
cad8f6f9 193 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
314f4591 194 STRUCT_STAT st;
1a7f3d99 195 if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
cad8f6f9 196 *stp = st;
314f4591
WD
197 }
198 return 0;
82306bf6 199#else
cad8f6f9 200 return do_stat(path, stp);
82306bf6
AT
201#endif
202}
203
7842418b 204/* This function is used to check if a file should be included/excluded
429f9828 205 * from the list of files based on its name and type etc. The value of
7842418b
WD
206 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
207static int is_excluded(char *fname, int is_dir, int filter_level)
c627d613 208{
7d687932 209#if 0 /* This currently never happens, so avoid a useless compare. */
7842418b 210 if (filter_level == NO_FILTERS)
429f9828
WD
211 return 0;
212#endif
6931c138 213 if (fname) {
429f9828 214 /* never exclude '.', even if somebody does --exclude '*' */
6931c138
WD
215 if (fname[0] == '.' && !fname[1])
216 return 0;
217 /* Handle the -R version of the '.' dir. */
218 if (fname[0] == '/') {
219 int len = strlen(fname);
220 if (fname[len-1] == '.' && fname[len-2] == '/')
221 return 0;
222 }
76e26e10 223 }
7842418b
WD
224 if (server_filter_list.head
225 && check_filter(&server_filter_list, fname, is_dir) < 0)
429f9828 226 return 1;
7842418b 227 if (filter_level != ALL_FILTERS)
429f9828 228 return 0;
7842418b
WD
229 if (filter_list.head
230 && check_filter(&filter_list, fname, is_dir) < 0)
76e26e10 231 return 1;
76e26e10 232 return 0;
c627d613
AT
233}
234
b280a1f4
AT
235static int to_wire_mode(mode_t mode)
236{
4f5b0756 237#ifdef SUPPORT_LINKS
e4fdf1de
WD
238#if _S_IFLNK != 0120000
239 if (S_ISLNK(mode))
b280a1f4 240 return (mode & ~(_S_IFMT)) | 0120000;
0d162bd1 241#endif
e4fdf1de
WD
242#endif
243 return mode;
b280a1f4
AT
244}
245
246static mode_t from_wire_mode(int mode)
247{
e4fdf1de
WD
248#if _S_IFLNK != 0120000
249 if ((mode & (_S_IFMT)) == 0120000)
efe3037c 250 return (mode & ~(_S_IFMT)) | _S_IFLNK;
e4fdf1de
WD
251#endif
252 return mode;
b280a1f4
AT
253}
254
f1773e09 255static void send_directory(int f, struct file_list *flist,
32cbfe7b 256 char *fbuf, int len);
c627d613 257
cd87e2f5 258static const char *flist_dir;
882e6893 259static int flist_dir_len;
c627d613 260
3ec4dd97 261
d9d6bc52
MP
262/**
263 * Make sure @p flist is big enough to hold at least @p flist->count
264 * entries.
265 **/
a85906c7 266void flist_expand(struct file_list *flist)
d9d6bc52 267{
0501f363 268 struct file_struct **new_ptr;
2e7d1994 269
a85906c7
S
270 if (flist->count < flist->malloced)
271 return;
dbda5fbf 272
a85906c7
S
273 if (flist->malloced < FLIST_START)
274 flist->malloced = FLIST_START;
275 else if (flist->malloced >= FLIST_LINEAR)
276 flist->malloced += FLIST_LINEAR;
277 else
278 flist->malloced *= 2;
279
280 /*
281 * In case count jumped or we are starting the list
282 * with a known size just set it.
283 */
284 if (flist->malloced < flist->count)
285 flist->malloced = flist->count;
286
0501f363
WD
287 new_ptr = realloc_array(flist->files, struct file_struct *,
288 flist->malloced);
2e7d1994 289
8c483820 290 if (verbose >= 2 && flist->malloced != FLIST_START) {
ea124cb3 291 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
a85906c7 292 who_am_i(),
a4a7e64c 293 (double)sizeof flist->files[0] * flist->malloced,
a85906c7 294 (new_ptr == flist->files) ? " not" : "");
d9d6bc52 295 }
a85906c7 296
0501f363 297 flist->files = new_ptr;
a85906c7
S
298
299 if (!flist->files)
300 out_of_memory("flist_expand");
d9d6bc52
MP
301}
302
96b87581 303static void send_file_entry(struct file_struct *file, int f)
c627d613 304{
1ef00d20 305 unsigned short flags;
5911fee5
WD
306 static time_t modtime;
307 static mode_t mode;
1490812a 308 static int64 dev;
4124540d 309 static dev_t rdev;
9c5e91f8 310 static uint32 rdev_major;
5911fee5
WD
311 static uid_t uid;
312 static gid_t gid;
313 static char lastname[MAXPATHLEN];
f376e674 314 char fname[MAXPATHLEN];
ebed4c3a 315 int l1, l2;
72914a60 316
37802f40 317 if (f < 0)
ebed4c3a 318 return;
72914a60
AT
319
320 if (!file) {
ebed4c3a 321 write_byte(f, 0);
5911fee5 322 modtime = 0, mode = 0;
b4a09b72 323 dev = 0, rdev = MAKEDEV(0, 0);
9c5e91f8 324 rdev_major = 0;
5911fee5
WD
325 uid = 0, gid = 0;
326 *lastname = '\0';
72914a60
AT
327 return;
328 }
329
5e4ff5f9 330 f_name(file, fname);
72914a60 331
96b87581 332 flags = file->flags & XMIT_TOP_DIR;
72914a60 333
30f337c9 334 if (file->mode == mode)
d01d15e0 335 flags |= XMIT_SAME_MODE;
1ef00d20 336 else
30f337c9 337 mode = file->mode;
b5c6a6ae
WD
338 if ((preserve_devices && IS_DEVICE(mode))
339 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 340 if (protocol_version < 28) {
b5c6a6ae
WD
341 if (file->u.rdev == rdev)
342 flags |= XMIT_SAME_RDEV_pre28;
343 else
344 rdev = file->u.rdev;
345 } else {
84a3efa0 346 rdev = file->u.rdev;
9c5e91f8
WD
347 if ((uint32)major(rdev) == rdev_major)
348 flags |= XMIT_SAME_RDEV_MAJOR;
84a3efa0 349 else
9c5e91f8
WD
350 rdev_major = major(rdev);
351 if ((uint32)minor(rdev) <= 0xFFu)
352 flags |= XMIT_RDEV_MINOR_IS_SMALL;
75bc8600 353 }
b5c6a6ae 354 } else if (protocol_version < 28)
b4a09b72 355 rdev = MAKEDEV(0, 0);
7b6fa00f 356 if (file->uid == uid)
d01d15e0 357 flags |= XMIT_SAME_UID;
1ef00d20 358 else
7b6fa00f
WD
359 uid = file->uid;
360 if (file->gid == gid)
d01d15e0 361 flags |= XMIT_SAME_GID;
1ef00d20 362 else
7b6fa00f 363 gid = file->gid;
30f337c9 364 if (file->modtime == modtime)
d01d15e0 365 flags |= XMIT_SAME_TIME;
1ef00d20 366 else
30f337c9 367 modtime = file->modtime;
a289addd 368
4f5b0756 369#ifdef SUPPORT_HARD_LINKS
92cc9dd7
WD
370 if (file->link_u.idev) {
371 if (file->F_DEV == dev) {
c4b4df4f 372 if (protocol_version >= 28)
d01d15e0 373 flags |= XMIT_SAME_DEV;
728d0922 374 } else
92cc9dd7 375 dev = file->F_DEV;
d01d15e0 376 flags |= XMIT_HAS_IDEV_DATA;
c4b4df4f 377 }
0d162bd1 378#endif
ebed4c3a
MP
379
380 for (l1 = 0;
3e491682
S
381 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
382 l1++) {}
eddd5d12 383 l2 = strlen(fname+l1);
72914a60 384
ebed4c3a 385 if (l1 > 0)
d01d15e0 386 flags |= XMIT_SAME_NAME;
ebed4c3a 387 if (l2 > 255)
d01d15e0 388 flags |= XMIT_LONG_NAME;
72914a60 389
1aa4caf3
WD
390 /* We must make sure we don't send a zero flag byte or the
391 * other end will terminate the flist transfer. Note that
ee3751c8 392 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
1aa4caf3 393 * it's harmless way to add a bit to the first flag byte. */
75bc8600 394 if (protocol_version >= 28) {
1aa4caf3 395 if (!flags && !S_ISDIR(mode))
ee3751c8 396 flags |= XMIT_TOP_DIR;
1aa4caf3 397 if ((flags & 0xFF00) || !flags) {
d01d15e0 398 flags |= XMIT_EXTENDED_FLAGS;
d521e1c2 399 write_shortint(f, flags);
75bc8600
WD
400 } else
401 write_byte(f, flags);
402 } else {
0a982011 403 if (!(flags & 0xFF))
75c51953 404 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
75bc8600
WD
405 write_byte(f, flags);
406 }
d01d15e0 407 if (flags & XMIT_SAME_NAME)
ebed4c3a 408 write_byte(f, l1);
d01d15e0 409 if (flags & XMIT_LONG_NAME)
ebed4c3a 410 write_int(f, l2);
72914a60 411 else
ebed4c3a
MP
412 write_byte(f, l2);
413 write_buf(f, fname + l1, l2);
72914a60 414
ebed4c3a 415 write_longint(f, file->length);
d01d15e0 416 if (!(flags & XMIT_SAME_TIME))
30f337c9 417 write_int(f, modtime);
d01d15e0 418 if (!(flags & XMIT_SAME_MODE))
30f337c9 419 write_int(f, to_wire_mode(mode));
d01d15e0 420 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
9f7b8c3b
WD
421 if (!numeric_ids)
422 add_uid(uid);
30f337c9 423 write_int(f, uid);
72914a60 424 }
d01d15e0 425 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
9f7b8c3b
WD
426 if (!numeric_ids)
427 add_gid(gid);
30f337c9 428 write_int(f, gid);
72914a60 429 }
b5c6a6ae
WD
430 if ((preserve_devices && IS_DEVICE(mode))
431 || (preserve_specials && IS_SPECIAL(mode))) {
9c5e91f8
WD
432 if (protocol_version < 28) {
433 if (!(flags & XMIT_SAME_RDEV_pre28))
434 write_int(f, (int)rdev);
435 } else {
436 if (!(flags & XMIT_SAME_RDEV_MAJOR))
437 write_int(f, major(rdev));
438 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
439 write_byte(f, minor(rdev));
440 else
441 write_int(f, minor(rdev));
442 }
75bc8600 443 }
c627d613 444
4f5b0756 445#ifdef SUPPORT_LINKS
30f337c9 446 if (preserve_links && S_ISLNK(mode)) {
306ffb8c
WD
447 int len = strlen(file->u.link);
448 write_int(f, len);
449 write_buf(f, file->u.link, len);
72914a60 450 }
c627d613
AT
451#endif
452
4f5b0756 453#ifdef SUPPORT_HARD_LINKS
e4fdf1de 454 if (file->link_u.idev) {
4124540d 455 if (protocol_version < 26) {
736a6a29 456 /* 32-bit dev_t and ino_t */
26404276
WD
457 write_int(f, (int32)dev);
458 write_int(f, (int32)file->F_INODE);
736a6a29
MP
459 } else {
460 /* 64-bit dev_t and ino_t */
4124540d
WD
461 if (!(flags & XMIT_SAME_DEV))
462 write_longint(f, dev);
92cc9dd7 463 write_longint(f, file->F_INODE);
736a6a29 464 }
72914a60 465 }
dc5ddbcc
AT
466#endif
467
9a4a237e 468 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
4a19c3b2 469 const char *sum;
728d0922
WD
470 if (S_ISREG(mode))
471 sum = file->u.sum;
9a4a237e 472 else {
728d0922
WD
473 /* Prior to 28, we sent a useless set of nulls. */
474 sum = empty_sum;
728d0922 475 }
7752df41 476 write_buf(f, sum, checksum_len);
ebed4c3a 477 }
182dca5c 478
ebed4c3a 479 strlcpy(lastname, fname, MAXPATHLEN);
182dca5c
AT
480}
481
f5db0993
WD
482static struct file_struct *receive_file_entry(struct file_list *flist,
483 unsigned short flags, int f)
182dca5c 484{
5911fee5
WD
485 static time_t modtime;
486 static mode_t mode;
1490812a 487 static int64 dev;
4124540d 488 static dev_t rdev;
9c5e91f8 489 static uint32 rdev_major;
5911fee5
WD
490 static uid_t uid;
491 static gid_t gid;
a289addd 492 static char lastname[MAXPATHLEN], *lastdir;
33ffd7c3 493 static int lastdir_depth, lastdir_len = -1;
42f23f47 494 static unsigned int del_hier_name_len = 0;
649f8742 495 static int in_del_hier = 0;
72914a60 496 char thisname[MAXPATHLEN];
ebed4c3a 497 unsigned int l1 = 0, l2 = 0;
a1d55ad0 498 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd
WD
499 OFF_T file_length;
500 char *basename, *dirname, *bp;
72914a60
AT
501 struct file_struct *file;
502
f3c3ed44 503 if (!flist) {
5911fee5 504 modtime = 0, mode = 0;
b4a09b72 505 dev = 0, rdev = MAKEDEV(0, 0);
9c5e91f8 506 rdev_major = 0;
5911fee5
WD
507 uid = 0, gid = 0;
508 *lastname = '\0';
3db859e8 509 lastdir_len = -1;
649f8742 510 in_del_hier = 0;
f5db0993 511 return NULL;
5911fee5
WD
512 }
513
d01d15e0 514 if (flags & XMIT_SAME_NAME)
72914a60 515 l1 = read_byte(f);
ebed4c3a 516
d01d15e0 517 if (flags & XMIT_LONG_NAME)
72914a60
AT
518 l2 = read_int(f);
519 else
520 l2 = read_byte(f);
521
ebed4c3a
MP
522 if (l2 >= MAXPATHLEN - l1) {
523 rprintf(FERROR,
524 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
0ee6ca98 525 flags, l1, l2, lastname);
a1f99493 526 overflow_exit("receive_file_entry");
d0fd26aa 527 }
72914a60 528
ebed4c3a
MP
529 strlcpy(thisname, lastname, l1 + 1);
530 read_sbuf(f, &thisname[l1], l2);
531 thisname[l1 + l2] = 0;
72914a60 532
ebed4c3a 533 strlcpy(lastname, thisname, MAXPATHLEN);
72914a60 534
58b1999e 535 clean_fname(thisname, 0);
72914a60 536
0d162bd1 537 if (sanitize_paths)
a2248aea 538 sanitize_path(thisname, thisname, "", 0, NULL);
cb13abfe 539
a289addd
WD
540 if ((basename = strrchr(thisname, '/')) != NULL) {
541 dirname_len = ++basename - thisname; /* counts future '\0' */
542 if (lastdir_len == dirname_len - 1
543 && strncmp(thisname, lastdir, lastdir_len) == 0) {
544 dirname = lastdir;
545 dirname_len = 0; /* indicates no copy is needed */
546 } else
547 dirname = thisname;
72914a60 548 } else {
a289addd
WD
549 basename = thisname;
550 dirname = NULL;
551 dirname_len = 0;
72914a60 552 }
a289addd 553 basename_len = strlen(basename) + 1; /* count the '\0' */
72914a60 554
a289addd 555 file_length = read_longint(f);
d01d15e0 556 if (!(flags & XMIT_SAME_TIME))
30f337c9 557 modtime = (time_t)read_int(f);
d01d15e0 558 if (!(flags & XMIT_SAME_MODE))
30f337c9 559 mode = from_wire_mode(read_int(f));
1ef00d20 560
ebec5eb6 561 if (chmod_modes && !S_ISLNK(mode))
8bbe41b5
WD
562 mode = tweak_mode(mode, chmod_modes);
563
a289addd
WD
564 if (preserve_uid && !(flags & XMIT_SAME_UID))
565 uid = (uid_t)read_int(f);
566 if (preserve_gid && !(flags & XMIT_SAME_GID))
567 gid = (gid_t)read_int(f);
568
b5c6a6ae
WD
569 if ((preserve_devices && IS_DEVICE(mode))
570 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 571 if (protocol_version < 28) {
b5c6a6ae
WD
572 if (!(flags & XMIT_SAME_RDEV_pre28))
573 rdev = (dev_t)read_int(f);
574 } else {
9c5e91f8
WD
575 uint32 rdev_minor;
576 if (!(flags & XMIT_SAME_RDEV_MAJOR))
577 rdev_major = read_int(f);
578 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
579 rdev_minor = read_byte(f);
580 else
581 rdev_minor = read_int(f);
b4a09b72 582 rdev = MAKEDEV(rdev_major, rdev_minor);
1ef00d20 583 }
b5c6a6ae 584 } else if (protocol_version < 28)
b4a09b72 585 rdev = MAKEDEV(0, 0);
72914a60 586
4f5b0756 587#ifdef SUPPORT_LINKS
30f337c9 588 if (preserve_links && S_ISLNK(mode)) {
a289addd
WD
589 linkname_len = read_int(f) + 1; /* count the '\0' */
590 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
591 rprintf(FERROR, "overflow: linkname_len=%d\n",
592 linkname_len - 1);
a1f99493 593 overflow_exit("receive_file_entry");
9dd891bb 594 }
72914a60 595 }
a289addd 596 else
0d162bd1 597#endif
a289addd 598 linkname_len = 0;
0d162bd1 599
a289addd
WD
600 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
601
61dec11a 602 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
603 + linkname_len + sum_len;
604 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
605
f5db0993 606 file = (struct file_struct *)bp;
9935066b 607 memset(bp, 0, file_struct_len);
61dec11a 608 bp += file_struct_len;
a289addd 609
a289addd
WD
610 file->modtime = modtime;
611 file->length = file_length;
612 file->mode = mode;
7b6fa00f
WD
613 file->uid = uid;
614 file->gid = gid;
a289addd 615
f3c3ed44
WD
616 if (dirname_len) {
617 file->dirname = lastdir = bp;
618 lastdir_len = dirname_len - 1;
619 memcpy(bp, dirname, dirname_len - 1);
620 bp += dirname_len;
621 bp[-1] = '\0';
622 lastdir_depth = count_dir_elements(lastdir);
623 file->dir.depth = lastdir_depth + 1;
624 } else if (dirname) {
625 file->dirname = dirname; /* we're reusing lastname */
626 file->dir.depth = lastdir_depth + 1;
627 } else
628 file->dir.depth = 1;
629
649f8742 630 if (S_ISDIR(mode)) {
ee3751c8 631 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
f3c3ed44 632 file->dir.depth--;
ee3751c8 633 if (flags & XMIT_TOP_DIR) {
75c51953 634 in_del_hier = recurse;
f3c3ed44 635 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
7a16e122 636 if (relative_paths && del_hier_name_len > 2
9cdadbb1
WD
637 && lastname[del_hier_name_len-1] == '.'
638 && lastname[del_hier_name_len-2] == '/')
7a16e122 639 del_hier_name_len -= 2;
ee3751c8
WD
640 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
641 } else if (in_del_hier) {
f3c3ed44
WD
642 if (!relative_paths || !del_hier_name_len
643 || (l1 >= del_hier_name_len
9cdadbb1 644 && lastname[del_hier_name_len] == '/'))
ee3751c8 645 file->flags |= FLAG_DEL_HERE;
649f8742
WD
646 else
647 in_del_hier = 0;
648 }
649 }
650
a289addd
WD
651 file->basename = bp;
652 memcpy(bp, basename, basename_len);
653 bp += basename_len;
654
b5c6a6ae
WD
655 if ((preserve_devices && IS_DEVICE(mode))
656 || (preserve_specials && IS_SPECIAL(mode)))
a289addd
WD
657 file->u.rdev = rdev;
658
4f5b0756 659#ifdef SUPPORT_LINKS
a289addd
WD
660 if (linkname_len) {
661 file->u.link = bp;
662 read_sbuf(f, bp, linkname_len - 1);
1a7f3d99 663 if (sanitize_paths)
dcebf78f 664 sanitize_path(bp, bp, "", lastdir_depth, NULL);
a289addd
WD
665 bp += linkname_len;
666 }
667#endif
668
4f5b0756 669#ifdef SUPPORT_HARD_LINKS
9935066b
S
670 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
671 flags |= XMIT_HAS_IDEV_DATA;
97a67bdf 672 if (flags & XMIT_HAS_IDEV_DATA) {
1490812a 673 int64 inode;
4124540d
WD
674 if (protocol_version < 26) {
675 dev = read_int(f);
9935066b 676 inode = read_int(f);
736a6a29 677 } else {
4124540d
WD
678 if (!(flags & XMIT_SAME_DEV))
679 dev = read_longint(f);
9935066b
S
680 inode = read_longint(f);
681 }
682 if (flist->hlink_pool) {
5bf63a11
S
683 file->link_u.idev = pool_talloc(flist->hlink_pool,
684 struct idev, 1, "inode_table");
9935066b
S
685 file->F_INODE = inode;
686 file->F_DEV = dev;
736a6a29 687 }
72914a60 688 }
dc5ddbcc 689#endif
ebed4c3a 690
9a4a237e 691 if (always_checksum && (sum_len || protocol_version < 28)) {
7c4f063b 692 char *sum;
a289addd
WD
693 if (sum_len) {
694 file->u.sum = sum = bp;
695 /*bp += sum_len;*/
9a4a237e 696 } else {
fea4db62 697 /* Prior to 28, we get a useless set of nulls. */
7c4f063b 698 sum = empty_sum;
fea4db62 699 }
7752df41 700 read_buf(f, sum, checksum_len);
72914a60 701 }
ebed4c3a 702
f5db0993 703 return file;
c627d613
AT
704}
705
db719fb0
MP
706/**
707 * Create a file_struct for a named file by reading its stat()
708 * information and performing extensive checks against global
709 * options.
710 *
711 * @return the new file, or NULL if there was an error or this file
712 * should be excluded.
713 *
714 * @todo There is a small optimization opportunity here to avoid
715 * stat()ing the file in some circumstances, which has a certain cost.
716 * We are called immediately after doing readdir(), and so we may
717 * already know the d_type of the file. We could for example avoid
718 * statting directories if we're not recursing, but this is not a very
719 * important case. Some systems may not have d_type.
720 **/
45d8bfe0 721struct file_struct *make_file(const char *fname, struct file_list *flist,
96b87581
WD
722 STRUCT_STAT *stp, unsigned short flags,
723 int filter_level)
c627d613 724{
a289addd
WD
725 static char *lastdir;
726 static int lastdir_len = -1;
3ec4dd97 727 struct file_struct *file;
bcacc18b 728 STRUCT_STAT st;
3ec4dd97 729 char sum[SUM_LENGTH];
1923b1fc 730 char thisname[MAXPATHLEN];
e0870f1d 731 char linkname[MAXPATHLEN];
a1d55ad0 732 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd 733 char *basename, *dirname, *bp;
3ec4dd97 734
d508258a 735 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
7de2483f 736 lastdir_len = -1;
9935066b 737
1923b1fc
WD
738 if (strlcpy(thisname, fname, sizeof thisname)
739 >= sizeof thisname - flist_dir_len) {
0ee6ca98 740 rprintf(FINFO, "skipping overly long name: %s\n", fname);
882e6893
WD
741 return NULL;
742 }
58b1999e 743 clean_fname(thisname, 0);
b7736c79 744 if (sanitize_paths)
a2248aea 745 sanitize_path(thisname, thisname, "", 0, NULL);
3ec4dd97 746
ebed4c3a 747 memset(sum, 0, SUM_LENGTH);
3ec4dd97 748
e4fdf1de 749 if (stp && S_ISDIR(stp->st_mode)) {
ede1f0eb 750 st = *stp; /* Needed for "symlink/." with --relative. */
e4fdf1de
WD
751 *linkname = '\0'; /* make IBM code checker happy */
752 } else if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 753 int save_errno = errno;
a4a7e64c 754 /* See if file is excluded before reporting an error. */
7842418b 755 if (filter_level != NO_FILTERS
60cc01a6 756 && (is_excluded(thisname, 0, filter_level)
53039410
WD
757 || is_excluded(thisname, 1, filter_level))) {
758 if (ignore_perishable && save_errno != ENOENT)
759 non_perishable_cnt++;
a4a7e64c 760 return NULL;
53039410 761 }
a4a7e64c 762 if (save_errno == ENOENT) {
4f5b0756 763#ifdef SUPPORT_LINKS
a4a7e64c
WD
764 /* Avoid "vanished" error if symlink points nowhere. */
765 if (copy_links && do_lstat(thisname, &st) == 0
766 && S_ISLNK(st.st_mode)) {
767 io_error |= IOERR_GENERAL;
768 rprintf(FERROR, "symlink has no referent: %s\n",
769 full_fname(thisname));
e5ce3bcf
WD
770 } else
771#endif
772 {
a4a7e64c
WD
773 enum logcode c = am_daemon && protocol_version < 28
774 ? FERROR : FINFO;
775 io_error |= IOERR_VANISHED;
776 rprintf(c, "file has vanished: %s\n",
777 full_fname(thisname));
76e26e10 778 }
a4a7e64c 779 } else {
a8726d2a 780 io_error |= IOERR_GENERAL;
d62bcc17
WD
781 rsyserr(FERROR, save_errno, "readlink %s failed",
782 full_fname(thisname));
76e26e10 783 }
3ec4dd97
AT
784 return NULL;
785 }
c627d613 786
7842418b
WD
787 /* backup.c calls us with filter_level set to NO_FILTERS. */
788 if (filter_level == NO_FILTERS)
789 goto skip_filters;
ac1a0994 790
7e037c42 791 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
0ee6ca98 792 rprintf(FINFO, "skipping directory %s\n", thisname);
3ec4dd97
AT
793 return NULL;
794 }
ebed4c3a 795
d521e1c2 796 /* -x only affects directories because we need to avoid recursing
e90cdb8a
WD
797 * into a mount-point directory, not to avoid copying a symlinked
798 * file if -L (or similar) was specified. */
535737bf
WD
799 if (one_file_system && st.st_dev != filesystem_dev
800 && S_ISDIR(st.st_mode)) {
801 if (one_file_system > 1) {
802 if (verbose > 2) {
803 rprintf(FINFO, "skipping mount-point dir %s\n",
804 thisname);
ebec5eb6 805 }
535737bf 806 return NULL;
ebec5eb6 807 }
535737bf 808 flags |= FLAG_MOUNT_POINT;
3b173846 809 }
ebed4c3a 810
53039410
WD
811 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
812 if (ignore_perishable)
813 non_perishable_cnt++;
76e26e10 814 return NULL;
53039410 815 }
76e26e10 816
132fcf36 817 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 818#ifdef SUPPORT_LINKS
132fcf36
WD
819 if (!S_ISLNK(st.st_mode))
820#endif
821 if (access(thisname, R_OK) != 0)
822 return NULL;
823 }
ac1a0994 824
97b7bff4 825 skip_filters:
ac1a0994 826
ea847c62
WD
827 if (verbose > 2) {
828 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
0ee6ca98 829 who_am_i(), thisname, filter_level);
ea847c62 830 }
ebed4c3a 831
a289addd
WD
832 if ((basename = strrchr(thisname, '/')) != NULL) {
833 dirname_len = ++basename - thisname; /* counts future '\0' */
834 if (lastdir_len == dirname_len - 1
835 && strncmp(thisname, lastdir, lastdir_len) == 0) {
836 dirname = lastdir;
837 dirname_len = 0; /* indicates no copy is needed */
838 } else
839 dirname = thisname;
3ec4dd97 840 } else {
a289addd
WD
841 basename = thisname;
842 dirname = NULL;
843 dirname_len = 0;
3ec4dd97 844 }
a289addd 845 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 846
4f5b0756 847#ifdef SUPPORT_LINKS
a289addd
WD
848 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
849#else
850 linkname_len = 0;
851#endif
852
902f03d1
WD
853 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
854 ? MD4_SUM_LENGTH : 0;
a289addd 855
61dec11a 856 alloc_len = file_struct_len + dirname_len + basename_len
cad8f6f9 857 + linkname_len + sum_len;
99aaa6ca
WD
858 if (flist)
859 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
860 else {
9935066b 861 if (!(bp = new_array(char, alloc_len)))
99aaa6ca 862 out_of_memory("make_file");
9935066b
S
863 }
864
a289addd 865 file = (struct file_struct *)bp;
9935066b 866 memset(bp, 0, file_struct_len);
61dec11a 867 bp += file_struct_len;
a289addd
WD
868
869 file->flags = flags;
3ec4dd97
AT
870 file->modtime = st.st_mtime;
871 file->length = st.st_size;
8bbe41b5 872 file->mode = st.st_mode;
7b6fa00f
WD
873 file->uid = st.st_uid;
874 file->gid = st.st_gid;
0d162bd1 875
4f5b0756 876#ifdef SUPPORT_HARD_LINKS
9935066b
S
877 if (flist && flist->hlink_pool) {
878 if (protocol_version < 28) {
879 if (S_ISREG(st.st_mode))
880 file->link_u.idev = pool_talloc(
881 flist->hlink_pool, struct idev, 1,
882 "inode_table");
883 } else {
884 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
885 file->link_u.idev = pool_talloc(
886 flist->hlink_pool, struct idev, 1,
887 "inode_table");
888 }
889 }
890 if (file->link_u.idev) {
61dec11a
WD
891 file->F_DEV = st.st_dev;
892 file->F_INODE = st.st_ino;
893 }
894#endif
895
a289addd
WD
896 if (dirname_len) {
897 file->dirname = lastdir = bp;
898 lastdir_len = dirname_len - 1;
899 memcpy(bp, dirname, dirname_len - 1);
900 bp += dirname_len;
901 bp[-1] = '\0';
902 } else if (dirname)
903 file->dirname = dirname;
904
905 file->basename = bp;
906 memcpy(bp, basename, basename_len);
907 bp += basename_len;
908
4f5b0756 909#ifdef HAVE_STRUCT_STAT_ST_RDEV
b5c6a6ae
WD
910 if ((preserve_devices && IS_DEVICE(st.st_mode))
911 || (preserve_specials && IS_SPECIAL(st.st_mode)))
0d162bd1
WD
912 file->u.rdev = st.st_rdev;
913#endif
914
4f5b0756 915#ifdef SUPPORT_LINKS
a289addd
WD
916 if (linkname_len) {
917 file->u.link = bp;
918 memcpy(bp, linkname, linkname_len);
919 bp += linkname_len;
920 }
0d162bd1
WD
921#endif
922
a289addd
WD
923 if (sum_len) {
924 file->u.sum = bp;
925 file_checksum(thisname, bp, st.st_size);
926 /*bp += sum_len;*/
ebed4c3a 927 }
c627d613 928
f3c3ed44 929 file->dir.root = flist_dir;
c627d613 930
23f4587f
WD
931 /* This code is only used by the receiver when it is building
932 * a list of files for a delete pass. */
933 if (keep_dirlinks && linkname_len && flist) {
934 STRUCT_STAT st2;
f5db0993 935 int save_mode = file->mode;
8bbe41b5 936 file->mode = S_IFDIR; /* Find a directory with our name. */
d64e6f42 937 if (flist_find(the_file_list, file) >= 0
1a7f3d99 938 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
23f4587f
WD
939 file->modtime = st2.st_mtime;
940 file->length = st2.st_size;
941 file->mode = st2.st_mode;
7b6fa00f
WD
942 file->uid = st2.st_uid;
943 file->gid = st2.st_gid;
23f4587f 944 file->u.link = NULL;
f5db0993
WD
945 } else
946 file->mode = save_mode;
23f4587f
WD
947 }
948
d17190df 949 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
a800434a 950 stats.total_size += st.st_size;
c627d613 951
3ec4dd97 952 return file;
c627d613
AT
953}
954
301fb56c 955static struct file_struct *send_file_name(int f, struct file_list *flist,
ede1f0eb 956 char *fname, STRUCT_STAT *stp,
96b87581 957 unsigned short flags)
c627d613 958{
ebed4c3a
MP
959 struct file_struct *file;
960
96b87581 961 file = make_file(fname, flist, stp, flags,
ede1f0eb 962 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
37802f40 963 if (!file)
301fb56c 964 return NULL;
ebed4c3a 965
ebec5eb6 966 if (chmod_modes && !S_ISLNK(file->mode))
8bbe41b5
WD
967 file->mode = tweak_mode(file->mode, chmod_modes);
968
53135fe8 969 maybe_emit_filelist_progress(flist->count + flist_count_offset);
ebed4c3a 970
d9d6bc52 971 flist_expand(flist);
ebed4c3a 972
c120ff37 973 if (file->basename[0]) {
ebed4c3a 974 flist->files[flist->count++] = file;
96b87581 975 send_file_entry(file, f);
ebed4c3a 976 }
301fb56c
WD
977 return file;
978}
ebed4c3a 979
301fb56c 980static void send_if_directory(int f, struct file_list *flist,
56f0c976
WD
981 struct file_struct *file,
982 char *fbuf, unsigned int ol)
301fb56c 983{
56f0c976 984 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
301fb56c
WD
985
986 if (S_ISDIR(file->mode)
5e4ff5f9 987 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
f1773e09
WD
988 void *save_filters;
989 unsigned int len = strlen(fbuf);
990 if (len > 1 && fbuf[len-1] == '/')
991 fbuf[--len] = '\0';
992 if (len >= MAXPATHLEN - 1) {
993 io_error |= IOERR_GENERAL;
994 rprintf(FERROR, "skipping long-named directory: %s\n",
995 full_fname(fbuf));
996 return;
997 }
998 save_filters = push_local_filters(fbuf, len);
999 send_directory(f, flist, fbuf, len);
1000 pop_local_filters(save_filters);
56f0c976
WD
1001 fbuf[ol] = '\0';
1002 if (is_dot_dir)
1003 fbuf[ol-1] = '.';
ebed4c3a 1004 }
c627d613
AT
1005}
1006
301fb56c 1007/* This function is normally called by the sender, but the receiving side also
7b558d7f 1008 * calls it from get_dirlist() with f set to -1 so that we just construct the
301fb56c
WD
1009 * file list in memory without sending it over the wire. Also, get_dirlist()
1010 * might call this with f set to -2, which also indicates that local filter
1011 * rules should be ignored. */
f1773e09 1012static void send_directory(int f, struct file_list *flist,
32cbfe7b 1013 char *fbuf, int len)
c627d613 1014{
3ec4dd97 1015 struct dirent *di;
32cbfe7b 1016 unsigned remainder;
3ec4dd97 1017 char *p;
f1773e09 1018 DIR *d;
301fb56c 1019 int start = flist->count;
3ec4dd97 1020
f1773e09 1021 if (!(d = opendir(fbuf))) {
06c28400 1022 io_error |= IOERR_GENERAL;
f1773e09 1023 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1024 return;
1025 }
c627d613 1026
19b2a5d9
WD
1027 p = fbuf + len;
1028 if (len != 1 || *fbuf != '/')
eddd5d12 1029 *p++ = '/';
f1773e09 1030 *p = '\0';
32cbfe7b 1031 remainder = MAXPATHLEN - (p - fbuf);
ebed4c3a 1032
6a7cc46c 1033 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1034 char *dname = d_name(di);
6a7cc46c
S
1035 if (dname[0] == '.' && (dname[1] == '\0'
1036 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1037 continue;
0ee6ca98 1038 if (strlcpy(p, dname, remainder) >= remainder) {
eddd5d12
WD
1039 io_error |= IOERR_GENERAL;
1040 rprintf(FINFO,
1041 "cannot send long-named file %s\n",
f1773e09 1042 full_fname(fbuf));
beaf4954 1043 continue;
eddd5d12 1044 }
0ee6ca98 1045
ede1f0eb 1046 send_file_name(f, flist, fbuf, NULL, 0);
3ec4dd97 1047 }
32cbfe7b
WD
1048
1049 fbuf[len] = '\0';
1050
6a7cc46c 1051 if (errno) {
06c28400 1052 io_error |= IOERR_GENERAL;
71903f60 1053 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1054 }
c627d613 1055
3ec4dd97 1056 closedir(d);
301fb56c
WD
1057
1058 if (recurse) {
1059 int i, end = flist->count - 1;
1060 for (i = start; i <= end; i++)
56f0c976 1061 send_if_directory(f, flist, flist->files[i], fbuf, len);
301fb56c 1062 }
c627d613
AT
1063}
1064
ebed4c3a 1065struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1066{
56f0c976 1067 int len;
bcacc18b 1068 STRUCT_STAT st;
808c57c3 1069 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 1070 char lastpath[MAXPATHLEN] = "";
649d65ed 1071 struct file_list *flist;
31b4d25d 1072 struct timeval start_tv, end_tv;
a800434a 1073 int64 start_write;
24d0fcde 1074 int use_ff_fd = 0;
649d65ed 1075
ea124cb3 1076 rprintf(FLOG, "building file list\n");
134f4338 1077 if (show_filelist_p())
1bbd10fe 1078 start_filelist_progress("building file list");
c627d613 1079
a800434a 1080 start_write = stats.total_written;
31b4d25d 1081 gettimeofday(&start_tv, NULL);
a800434a 1082
134f4338 1083 flist = flist_new(WITH_HLINK, "send_file_list");
c627d613 1084
134f4338
WD
1085 io_start_buffering_out();
1086 if (filesfrom_fd >= 0) {
6f3684ff 1087 if (argv[0] && !push_dir(argv[0], 0)) {
134f4338
WD
1088 rsyserr(FERROR, errno, "push_dir %s failed",
1089 full_fname(argv[0]));
1090 exit_cleanup(RERR_FILESELECT);
24d0fcde 1091 }
134f4338 1092 use_ff_fd = 1;
d6dead6b
AT
1093 }
1094
24d0fcde 1095 while (1) {
56f0c976
WD
1096 char fbuf[MAXPATHLEN];
1097 char *fn;
301fb56c 1098 int is_dot_dir;
c627d613 1099
24d0fcde 1100 if (use_ff_fd) {
56f0c976 1101 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
24d0fcde 1102 break;
a2248aea 1103 sanitize_path(fbuf, fbuf, "", 0, NULL);
24d0fcde
WD
1104 } else {
1105 if (argc-- == 0)
1106 break;
56f0c976 1107 strlcpy(fbuf, *argv++, MAXPATHLEN);
24d0fcde 1108 if (sanitize_paths)
a2248aea 1109 sanitize_path(fbuf, fbuf, "", 0, NULL);
24d0fcde 1110 }
c627d613 1111
56f0c976 1112 len = strlen(fbuf);
1902a765
WD
1113 if (relative_paths) {
1114 /* We clean up fbuf below. */
1115 is_dot_dir = 0;
1116 } else if (!len || fbuf[len - 1] == '/') {
56f0c976 1117 if (len == 2 && fbuf[0] == '.') {
6931c138 1118 /* Turn "./" into just "." rather than "./." */
56f0c976 1119 fbuf[1] = '\0';
557a35f5 1120 } else {
56f0c976 1121 if (len + 1 >= MAXPATHLEN)
a1f99493 1122 overflow_exit("send_file_list");
56f0c976
WD
1123 fbuf[len++] = '.';
1124 fbuf[len] = '\0';
53f821f1 1125 }
301fb56c 1126 is_dot_dir = 1;
56f0c976
WD
1127 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1128 && (len == 2 || fbuf[len-3] == '/')) {
1129 if (len + 2 >= MAXPATHLEN)
a1f99493 1130 overflow_exit("send_file_list");
56f0c976
WD
1131 fbuf[len++] = '/';
1132 fbuf[len++] = '.';
1133 fbuf[len] = '\0';
a289f89f 1134 is_dot_dir = 1;
301fb56c 1135 } else {
56f0c976
WD
1136 is_dot_dir = fbuf[len-1] == '.'
1137 && (len == 1 || fbuf[len-2] == '/');
649d65ed 1138 }
c627d613 1139
88c2190a 1140 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
134f4338
WD
1141 io_error |= IOERR_GENERAL;
1142 rsyserr(FERROR, errno, "link_stat %s failed",
56f0c976 1143 full_fname(fbuf));
649d65ed
AT
1144 continue;
1145 }
c627d613 1146
7e037c42 1147 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
0ee6ca98 1148 rprintf(FINFO, "skipping directory %s\n", fbuf);
649d65ed
AT
1149 continue;
1150 }
c627d613 1151
649d65ed 1152 dir = NULL;
808c57c3 1153 olddir[0] = '\0';
649d65ed
AT
1154
1155 if (!relative_paths) {
56f0c976 1156 p = strrchr(fbuf, '/');
649d65ed 1157 if (p) {
151f59f1 1158 *p = '\0';
56f0c976 1159 if (p == fbuf)
649d65ed
AT
1160 dir = "/";
1161 else
56f0c976
WD
1162 dir = fbuf;
1163 len -= p - fbuf + 1;
1164 fn = p + 1;
1165 } else
1166 fn = fbuf;
1902a765
WD
1167 } else {
1168 if ((p = strstr(fbuf, "/./")) != NULL) {
1169 *p = '\0';
1170 if (p == fbuf)
1171 dir = "/";
1172 else
1173 dir = fbuf;
1174 len -= p - fbuf + 3;
1175 fn = p + 3;
1176 } else
1177 fn = fbuf;
1178 /* Get rid of trailing "/" and "/.". */
1179 while (len) {
ede1f0eb
WD
1180 if (fn[len - 1] == '/') {
1181 is_dot_dir = 1;
1182 if (!--len && !dir) {
1183 len++;
1184 break;
1185 }
1186 }
1902a765
WD
1187 else if (len >= 2 && fn[len - 1] == '.'
1188 && fn[len - 2] == '/') {
ede1f0eb 1189 is_dot_dir = 1;
1902a765
WD
1190 if (!(len -= 2) && !dir) {
1191 len++;
1192 break;
1193 }
1194 } else
1195 break;
1196 }
8c449e62
WD
1197 if (len == 1 && fn[0] == '/')
1198 fn[len++] = '.';
1902a765
WD
1199 fn[len] = '\0';
1200 /* Reject a ".." dir in the active part of the path. */
ede1f0eb
WD
1201 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1202 if ((p[2] == '/' || p[2] == '\0')
1203 && (p == fn || p[-1] == '/')) {
1204 rprintf(FERROR,
1205 "found \"..\" dir in relative path: %s\n",
1206 fbuf);
1207 exit_cleanup(RERR_SYNTAX);
1208 }
1902a765
WD
1209 }
1210 }
d2ea5980 1211
56f0c976
WD
1212 if (!*fn) {
1213 len = 1;
1214 fn = ".";
1215 }
d2ea5980
WD
1216
1217 if (dir && *dir) {
cd87e2f5 1218 static const char *lastdir;
d2ea5980
WD
1219 static int lastdir_len;
1220
8ad5cea3 1221 strlcpy(olddir, curr_dir, sizeof olddir);
d2ea5980 1222
6f3684ff 1223 if (!push_dir(dir, 0)) {
d2ea5980
WD
1224 io_error |= IOERR_GENERAL;
1225 rsyserr(FERROR, errno, "push_dir %s failed",
1226 full_fname(dir));
1227 continue;
1228 }
1229
1230 if (lastdir && strcmp(lastdir, dir) == 0) {
1231 flist_dir = lastdir;
1232 flist_dir_len = lastdir_len;
1233 } else {
1234 flist_dir = lastdir = strdup(dir);
1235 flist_dir_len = lastdir_len = strlen(dir);
1236 }
1237 }
1238
99eba675 1239 if (fn != fbuf)
56f0c976
WD
1240 memmove(fbuf, fn, len + 1);
1241
1242 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
151f59f1
WD
1243 /* Send the implied directories at the start of the
1244 * source spec, so we get their permissions right. */
56f0c976 1245 char *lp = lastpath, *slash = fbuf;
151f59f1 1246 *p = '\0';
2154309a
WD
1247 /* Skip any initial directories in our path that we
1248 * have in common with lastpath. */
56f0c976 1249 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
2154309a
WD
1250 if (*fn == '/')
1251 slash = fn;
2154309a
WD
1252 }
1253 *p = '/';
1254 if (fn != p || (*lp && *lp != '/')) {
0f57446d 1255 int save_copy_links = copy_links;
7e037c42 1256 int save_xfer_dirs = xfer_dirs;
88c2190a 1257 copy_links |= copy_unsafe_links;
7e037c42 1258 xfer_dirs = 1;
2154309a 1259 while ((slash = strchr(slash+1, '/')) != 0) {
151f59f1 1260 *slash = '\0';
ede1f0eb 1261 send_file_name(f, flist, fbuf, NULL, 0);
2154309a 1262 *slash = '/';
649d65ed 1263 }
0f57446d 1264 copy_links = save_copy_links;
7e037c42 1265 xfer_dirs = save_xfer_dirs;
151f59f1 1266 *p = '\0';
56f0c976 1267 strlcpy(lastpath, fbuf, sizeof lastpath);
649d65ed
AT
1268 *p = '/';
1269 }
1270 }
ebed4c3a 1271
535737bf
WD
1272 if (one_file_system)
1273 filesystem_dev = st.st_dev;
1274
75c51953
WD
1275 if (recurse || (xfer_dirs && is_dot_dir)) {
1276 struct file_struct *file;
96b87581 1277 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
ede1f0eb 1278 if (file)
56f0c976 1279 send_if_directory(f, flist, file, fbuf, len);
75c51953 1280 } else
ede1f0eb 1281 send_file_name(f, flist, fbuf, &st, 0);
2bca43f6 1282
808c57c3 1283 if (olddir[0]) {
649d65ed 1284 flist_dir = NULL;
882e6893 1285 flist_dir_len = 0;
808c57c3 1286 if (!pop_dir(olddir)) {
d62bcc17 1287 rsyserr(FERROR, errno, "pop_dir %s failed",
92cdc393 1288 full_fname(olddir));
65417579 1289 exit_cleanup(RERR_FILESELECT);
649d65ed 1290 }
649d65ed 1291 }
649d65ed 1292 }
dc5ddbcc 1293
134f4338
WD
1294 gettimeofday(&end_tv, NULL);
1295 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1296 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1297 if (stats.flist_buildtime == 0)
1298 stats.flist_buildtime = 1;
1299 start_tv = end_tv;
31b4d25d 1300
96b87581 1301 send_file_entry(NULL, f);
c627d613 1302
134f4338
WD
1303 if (show_filelist_p())
1304 finish_filelist_progress(flist);
31b4d25d 1305
134f4338
WD
1306 gettimeofday(&end_tv, NULL);
1307 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1308 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 1309
7cf8e8d0 1310 if (flist->hlink_pool) {
9935066b
S
1311 pool_destroy(flist->hlink_pool);
1312 flist->hlink_pool = NULL;
1313 }
1314
f5db0993
WD
1315 /* Sort the list without removing any duplicates. This allows the
1316 * receiving side to ask for any name they like, which gives us the
1317 * flexibility to change the way we unduplicate names in the future
1318 * without causing a compatibility problem with older versions. */
827c37f6 1319 clean_flist(flist, 0, 0);
ebed4c3a 1320
134f4338 1321 send_uid_list(f);
f6c34742 1322
134f4338
WD
1323 /* send the io_error flag */
1324 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1325
134f4338
WD
1326 io_end_buffering();
1327 stats.flist_size = stats.total_written - start_write;
1328 stats.num_files = flist->count;
d6dead6b 1329
cefed3e8 1330 if (verbose > 3)
32cbfe7b 1331 output_flist(flist);
cefed3e8 1332
17faa41c 1333 if (verbose > 2)
ebed4c3a 1334 rprintf(FINFO, "send_file_list done\n");
17faa41c 1335
649d65ed 1336 return flist;
c627d613
AT
1337}
1338
c627d613
AT
1339struct file_list *recv_file_list(int f)
1340{
ebed4c3a 1341 struct file_list *flist;
1ef00d20 1342 unsigned short flags;
ebed4c3a 1343 int64 start_read;
c627d613 1344
ea124cb3 1345 rprintf(FLOG, "receiving file list\n");
1bbd10fe
DD
1346 if (show_filelist_p())
1347 start_filelist_progress("receiving file list");
c627d613 1348
ebed4c3a 1349 start_read = stats.total_read;
a800434a 1350
9935066b 1351 flist = flist_new(WITH_HLINK, "recv_file_list");
c627d613 1352
ebed4c3a
MP
1353 flist->count = 0;
1354 flist->malloced = 1000;
58cadc86 1355 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1356 if (!flist->files)
1357 goto oom;
c627d613 1358
1ef00d20 1359 while ((flags = read_byte(f)) != 0) {
f5db0993 1360 struct file_struct *file;
dbda5fbf 1361
d9d6bc52 1362 flist_expand(flist);
c627d613 1363
d01d15e0 1364 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1365 flags |= read_byte(f) << 8;
f5db0993 1366 file = receive_file_entry(flist, flags, f);
c627d613 1367
5e7b826a 1368 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
f5db0993 1369 stats.total_size += file->length;
c627d613 1370
f5db0993 1371 flist->files[flist->count++] = file;
c627d613 1372
53135fe8 1373 maybe_emit_filelist_progress(flist->count);
1bbd10fe 1374
5e4ff5f9
WD
1375 if (verbose > 2) {
1376 rprintf(FINFO, "recv_file_name(%s)\n",
1377 f_name(file, NULL));
1378 }
ebed4c3a 1379 }
f5db0993 1380 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
c627d613 1381
ebed4c3a
MP
1382 if (verbose > 2)
1383 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1384
b7736c79 1385 if (show_filelist_p())
1bbd10fe 1386 finish_filelist_progress(flist);
a06d19e3 1387
983b1ed3
WD
1388 clean_flist(flist, relative_paths, 1);
1389
37802f40 1390 if (f >= 0) {
7b6fa00f 1391 recv_uid_list(f, flist);
f6c34742 1392
b9f592fb
WD
1393 /* Recv the io_error flag */
1394 if (lp_ignore_errors(module_id) || ignore_errors)
1395 read_int(f);
1396 else
1397 io_error |= read_int(f);
ebed4c3a 1398 }
6ba9279f 1399
cefed3e8 1400 if (verbose > 3)
32cbfe7b 1401 output_flist(flist);
cefed3e8 1402
ebed4c3a
MP
1403 if (list_only) {
1404 int i;
b7736c79 1405 for (i = 0; i < flist->count; i++)
ebed4c3a 1406 list_file_entry(flist->files[i]);
ebed4c3a 1407 }
f7632fc6 1408
ebed4c3a
MP
1409 if (verbose > 2)
1410 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1411
ebed4c3a
MP
1412 stats.flist_size = stats.total_read - start_read;
1413 stats.num_files = flist->count;
a800434a 1414
ebed4c3a 1415 return flist;
c627d613 1416
97b7bff4 1417 oom:
ebed4c3a
MP
1418 out_of_memory("recv_file_list");
1419 return NULL; /* not reached */
c627d613
AT
1420}
1421
14698a3a 1422static int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1423{
14698a3a 1424 return f_name_cmp(*file1, *file2);
c627d613
AT
1425}
1426
f5db0993
WD
1427/* Search for an identically-named item in the file list. Note that the
1428 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 1429int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1430{
f3c3ed44 1431 int low = flist->low, high = flist->high;
207522ae 1432 int diff, mid, mid_up;
f3c3ed44
WD
1433
1434 while (low <= high) {
1435 mid = (low + high) / 2;
c0b134a4
WD
1436 if (flist->files[mid]->basename)
1437 mid_up = mid;
7402d583
WD
1438 else {
1439 /* Scan for the next non-empty entry using the cached
1440 * distance values. If the value isn't fully up-to-
1441 * date, update it. */
85aecef6
WD
1442 mid_up = mid + flist->files[mid]->dir.depth;
1443 if (!flist->files[mid_up]->basename) {
1444 do {
1445 mid_up += flist->files[mid_up]->dir.depth;
1446 } while (!flist->files[mid_up]->basename);
1447 flist->files[mid]->dir.depth = mid_up - mid;
c0b134a4 1448 }
c0b134a4 1449 if (mid_up > high) {
7402d583
WD
1450 /* If there's nothing left above us, set high to
1451 * a non-empty entry below us and continue. */
26404276 1452 high = mid - (int)flist->files[mid]->length;
85aecef6
WD
1453 if (!flist->files[high]->basename) {
1454 do {
26404276 1455 high -= (int)flist->files[high]->length;
85aecef6
WD
1456 } while (!flist->files[high]->basename);
1457 flist->files[mid]->length = mid - high;
7402d583 1458 }
c0b134a4
WD
1459 continue;
1460 }
c0b134a4 1461 }
207522ae
WD
1462 diff = f_name_cmp(flist->files[mid_up], f);
1463 if (diff == 0) {
f5db0993
WD
1464 if (protocol_version < 29
1465 && S_ISDIR(flist->files[mid_up]->mode)
1466 != S_ISDIR(f->mode))
1467 return -1;
f3c3ed44 1468 return mid_up;
f5db0993 1469 }
207522ae 1470 if (diff < 0)
f3c3ed44 1471 low = mid_up + 1;
207522ae
WD
1472 else
1473 high = mid - 1;
d966ee25 1474 }
d966ee25 1475 return -1;
c627d613
AT
1476}
1477
3ec4dd97 1478/*
9935066b
S
1479 * Free up any resources a file_struct has allocated
1480 * and clear the file.
3ec4dd97 1481 */
e6ffb966 1482void clear_file(struct file_struct *file, struct file_list *flist)
c627d613 1483{
7402d583
WD
1484 if (flist->hlink_pool && file->link_u.idev)
1485 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1486 memset(file, 0, file_struct_len);
1487 /* In an empty entry, dir.depth is an offset to the next non-empty
1488 * entry. Likewise for length in the opposite direction. We assume
0679ac4c
WD
1489 * that we're alone for now since flist_find() will adjust the counts
1490 * it runs into that aren't up-to-date. */
7402d583 1491 file->length = file->dir.depth = 1;
3ec4dd97 1492}
c627d613 1493
3d382777
AT
1494/*
1495 * allocate a new file list
1496 */
9935066b 1497struct file_list *flist_new(int with_hlink, char *msg)
3d382777
AT
1498{
1499 struct file_list *flist;
1500
58cadc86 1501 flist = new(struct file_list);
ebed4c3a 1502 if (!flist)
9935066b 1503 out_of_memory(msg);
3d382777 1504
9935066b
S
1505 memset(flist, 0, sizeof (struct file_list));
1506
4a19c3b2 1507 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
9935066b
S
1508 out_of_memory(msg);
1509
4f5b0756 1510#ifdef SUPPORT_HARD_LINKS
9935066b 1511 if (with_hlink && preserve_hard_links) {
3e491682 1512 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
9935066b
S
1513 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1514 out_of_memory(msg);
1515 }
1516#endif
d9d6bc52 1517
3d382777
AT
1518 return flist;
1519}
ebed4c3a 1520
3ec4dd97
AT
1521/*
1522 * free up all elements in a flist
1523 */
1524void flist_free(struct file_list *flist)
1525{
9935066b
S
1526 pool_destroy(flist->file_pool);
1527 pool_destroy(flist->hlink_pool);
3ec4dd97 1528 free(flist->files);
3ec4dd97 1529 free(flist);
c627d613
AT
1530}
1531
c627d613
AT
1532/*
1533 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1534 * duplicate names can cause corruption because of the pipelining
c627d613 1535 */
827c37f6 1536static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1537{
85aecef6 1538 char fbuf[MAXPATHLEN];
6931c138 1539 int i, prev_i = 0;
c627d613 1540
910ee8c9
WD
1541 if (!flist)
1542 return;
1543 if (flist->count == 0) {
1544 flist->high = -1;
3ec4dd97 1545 return;
910ee8c9 1546 }
3ec4dd97 1547
ebed4c3a 1548 qsort(flist->files, flist->count,
a4a7e64c 1549 sizeof flist->files[0], (int (*)())file_compare);
ebed4c3a 1550
827c37f6 1551 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1552 if (flist->files[i]->basename) {
6931c138 1553 prev_i = i;
b91b50c0
WD
1554 break;
1555 }
1556 }
f3c3ed44 1557 flist->low = prev_i;
b91b50c0 1558 while (++i < flist->count) {
fe1c19dc 1559 int j;
f5db0993
WD
1560 struct file_struct *file = flist->files[i];
1561
1562 if (!file->basename)
b91b50c0 1563 continue;
fe1c19dc
WD
1564 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1565 j = prev_i;
1566 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
1567 int save_mode = file->mode;
1568 /* Make sure that this directory doesn't duplicate a
1569 * non-directory earlier in the list. */
f5db0993 1570 flist->high = prev_i;
fe1c19dc
WD
1571 file->mode = S_IFREG;
1572 j = flist_find(flist, file);
f5db0993 1573 file->mode = save_mode;
fe1c19dc
WD
1574 } else
1575 j = -1;
1576 if (j >= 0) {
1577 struct file_struct *fp = flist->files[j];
1578 int keep, drop;
1579 /* If one is a dir and the other is not, we want to
1580 * keep the dir because it might have contents in the
1581 * list. */
1582 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1583 if (S_ISDIR(file->mode))
1584 keep = i, drop = j;
1585 else
1586 keep = j, drop = i;
1587 } else
1588 keep = j, drop = i;
b91b50c0 1589 if (verbose > 1 && !am_server) {
ebed4c3a 1590 rprintf(FINFO,
fe1c19dc 1591 "removing duplicate name %s from file list (%d)\n",
85aecef6 1592 f_name(file, fbuf), drop);
b91b50c0 1593 }
9c000f5e
WD
1594 /* Make sure we don't lose track of a user-specified
1595 * top directory. */
0f0b2e66
WD
1596 flist->files[keep]->flags |= flist->files[drop]->flags
1597 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
fe1c19dc 1598
e6ffb966 1599 clear_file(flist->files[drop], flist);
9935066b 1600
fe1c19dc
WD
1601 if (keep == i) {
1602 if (flist->low == drop) {
1603 for (j = drop + 1;
1604 j < i && !flist->files[j]->basename;
1605 j++) {}
1606 flist->low = j;
1607 }
1608 prev_i = i;
1609 }
728d0922 1610 } else
6931c138 1611 prev_i = i;
3ec4dd97 1612 }
f5db0993 1613 flist->high = no_dups ? prev_i : flist->count - 1;
0199b05f 1614
c0b134a4
WD
1615 if (strip_root) {
1616 /* We need to strip off the leading slashes for relative
1617 * paths, but this must be done _after_ the sorting phase. */
1618 for (i = flist->low; i <= flist->high; i++) {
1619 struct file_struct *file = flist->files[i];
1620
1621 if (!file->dirname)
1622 continue;
1623 while (*file->dirname == '/')
1624 file->dirname++;
1625 if (!*file->dirname)
1626 file->dirname = NULL;
1627 }
1628 }
1629
e6ffb966
WD
1630 if (prune_empty_dirs && no_dups) {
1631 int j, prev_depth = 0;
9c000f5e 1632
e6ffb966 1633 prev_i = 0; /* It's OK that this isn't really true. */
9c000f5e 1634
f5db0993 1635 for (i = flist->low; i <= flist->high; i++) {
e6ffb966 1636 struct file_struct *fp, *file = flist->files[i];
ebed4c3a 1637
e6ffb966
WD
1638 /* This temporarily abuses the dir.depth value for a
1639 * directory that is in a chain that might get pruned.
1640 * We restore the old value if it gets a reprieve. */
9c000f5e 1641 if (S_ISDIR(file->mode) && file->dir.depth) {
e6ffb966
WD
1642 /* Dump empty dirs when coming back down. */
1643 for (j = prev_depth; j >= file->dir.depth; j--) {
1644 fp = flist->files[prev_i];
1645 if (fp->dir.depth >= 0)
1646 break;
1647 prev_i = -fp->dir.depth-1;
1648 clear_file(fp, flist);
9c000f5e 1649 }
e6ffb966 1650 prev_depth = file->dir.depth;
85aecef6
WD
1651 if (is_excluded(f_name(file, fbuf), 1,
1652 ALL_FILTERS)) {
e6ffb966
WD
1653 /* Keep dirs through this dir. */
1654 for (j = prev_depth-1; ; j--) {
1655 fp = flist->files[prev_i];
1656 if (fp->dir.depth >= 0)
1657 break;
1658 prev_i = -fp->dir.depth-1;
1659 fp->dir.depth = j;
1660 }
85aecef6 1661 } else
e6ffb966
WD
1662 file->dir.depth = -prev_i-1;
1663 prev_i = i;
1664 } else {
1665 /* Keep dirs through this non-dir. */
1666 for (j = prev_depth; ; j--) {
1667 fp = flist->files[prev_i];
1668 if (fp->dir.depth >= 0)
1669 break;
1670 prev_i = -fp->dir.depth-1;
1671 fp->dir.depth = j;
1672 }
0199b05f 1673 }
9c000f5e 1674 }
e6ffb966
WD
1675 /* Dump empty all remaining empty dirs. */
1676 while (1) {
1677 struct file_struct *fp = flist->files[prev_i];
1678 if (fp->dir.depth >= 0)
1679 break;
1680 prev_i = -fp->dir.depth-1;
1681 clear_file(fp, flist);
9c000f5e 1682 }
f5db0993 1683
9c000f5e
WD
1684 for (i = flist->low; i <= flist->high; i++) {
1685 if (flist->files[i]->basename)
1686 break;
1687 }
1688 flist->low = i;
1689 for (i = flist->high; i >= flist->low; i--) {
1690 if (flist->files[i]->basename)
1691 break;
0199b05f 1692 }
9c000f5e 1693 flist->high = i;
0199b05f 1694 }
cefed3e8 1695}
0199b05f 1696
32cbfe7b 1697static void output_flist(struct file_list *flist)
cefed3e8 1698{
f3c3ed44 1699 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8 1700 struct file_struct *file;
32cbfe7b 1701 const char *who = who_am_i();
cefed3e8 1702 int i;
0199b05f 1703
ebed4c3a 1704 for (i = 0; i < flist->count; i++) {
cefed3e8 1705 file = flist->files[i];
56194bcd 1706 if ((am_root || am_sender) && preserve_uid)
cabd60fd 1707 snprintf(uidbuf, sizeof uidbuf, " uid=%ld", (long)file->uid);
f05f993e
WD
1708 else
1709 *uidbuf = '\0';
7b6fa00f 1710 if (preserve_gid && file->gid != GID_NONE)
cabd60fd 1711 snprintf(gidbuf, sizeof gidbuf, " gid=%ld", (long)file->gid);
f05f993e
WD
1712 else
1713 *gidbuf = '\0';
f3c3ed44 1714 if (!am_sender)
cabd60fd 1715 snprintf(depthbuf, sizeof depthbuf, "%d", file->dir.depth);
14698a3a 1716 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
32cbfe7b 1717 who, i, am_sender ? NS(file->dir.root) : depthbuf,
0ee6ca98 1718 file->dirname ? file->dirname : "",
14698a3a
WD
1719 file->dirname ? "/" : "", NS(file->basename),
1720 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
f3c3ed44 1721 (double)file->length, uidbuf, gidbuf, file->flags);
0199b05f 1722 }
3ec4dd97
AT
1723}
1724
8824e2ce 1725enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 1726enum fnc_type { t_PATH, t_ITEM };
8018edd3 1727
2f3cad89 1728/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
1729 * would do if it were operating on the joined strings.
1730 *
1731 * Some differences beginning with protocol_version 29: (1) directory names
1732 * are compared with an assumed trailing slash so that they compare in a
1733 * way that would cause them to sort immediately prior to any content they
1734 * may have; (2) a directory of any name compares after a non-directory of
1735 * any name at the same depth; (3) a directory with name "." compares prior
1736 * to anything else. These changes mean that a directory and a non-dir
1737 * with the same name will not compare as equal (protocol_version >= 29).
1738 *
1739 * The dirname component can be an empty string, but the basename component
1740 * cannot (and never is in the current codebase). The basename component
1741 * may be NULL (for a removed item), in which case it is considered to be
1742 * after any existing item. */
8018edd3 1743int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1744{
8018edd3
WD
1745 int dif;
1746 const uchar *c1, *c2;
1ef00d20 1747 enum fnc_state state1, state2;
f5db0993
WD
1748 enum fnc_type type1, type2;
1749 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3
WD
1750
1751 if (!f1 || !f1->basename) {
1752 if (!f2 || !f2->basename)
1753 return 0;
1754 return -1;
1755 }
1756 if (!f2 || !f2->basename)
1757 return 1;
1758
14698a3a
WD
1759 c1 = (uchar*)f1->dirname;
1760 c2 = (uchar*)f2->dirname;
1761 if (c1 == c2)
1762 c1 = c2 = NULL;
1763 if (!c1) {
f5db0993 1764 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1765 c1 = (uchar*)f1->basename;
f5db0993
WD
1766 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1767 type1 = t_ITEM;
1768 state1 = s_TRAILING;
1769 c1 = (uchar*)"";
1770 } else
1771 state1 = s_BASE;
f5db0993
WD
1772 } else {
1773 type1 = t_path;
8824e2ce 1774 state1 = s_DIR;
f5db0993 1775 }
14698a3a 1776 if (!c2) {
f5db0993 1777 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1778 c2 = (uchar*)f2->basename;
f5db0993
WD
1779 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1780 type2 = t_ITEM;
1781 state2 = s_TRAILING;
1782 c2 = (uchar*)"";
1783 } else
1784 state2 = s_BASE;
f5db0993
WD
1785 } else {
1786 type2 = t_path;
8824e2ce 1787 state2 = s_DIR;
f5db0993
WD
1788 }
1789
1790 if (type1 != type2)
1791 return type1 == t_PATH ? 1 : -1;
8018edd3 1792
e4fdf1de 1793 do {
f5db0993 1794 if (!*c1) {
8018edd3 1795 switch (state1) {
8824e2ce
WD
1796 case s_DIR:
1797 state1 = s_SLASH;
e90b8ace 1798 c1 = (uchar*)"/";
8018edd3 1799 break;
8824e2ce 1800 case s_SLASH:
f5db0993 1801 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1802 c1 = (uchar*)f1->basename;
cbb5fa4f
WD
1803 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1804 type1 = t_ITEM;
1805 state1 = s_TRAILING;
1806 c1 = (uchar*)"";
1807 } else
1808 state1 = s_BASE;
8018edd3 1809 break;
8824e2ce
WD
1810 case s_BASE:
1811 state1 = s_TRAILING;
f5db0993 1812 if (type1 == t_PATH) {
2f3cad89 1813 c1 = (uchar*)"/";
f5db0993
WD
1814 break;
1815 }
1816 /* FALL THROUGH */
8824e2ce 1817 case s_TRAILING:
f5db0993 1818 type1 = t_ITEM;
8018edd3
WD
1819 break;
1820 }
f5db0993
WD
1821 if (*c2 && type1 != type2)
1822 return type1 == t_PATH ? 1 : -1;
8018edd3 1823 }
f5db0993 1824 if (!*c2) {
8018edd3 1825 switch (state2) {
8824e2ce
WD
1826 case s_DIR:
1827 state2 = s_SLASH;
e90b8ace 1828 c2 = (uchar*)"/";
8018edd3 1829 break;
8824e2ce 1830 case s_SLASH:
f5db0993 1831 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1832 c2 = (uchar*)f2->basename;
cbb5fa4f
WD
1833 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1834 type2 = t_ITEM;
1835 state2 = s_TRAILING;
1836 c2 = (uchar*)"";
1837 } else
1838 state2 = s_BASE;
8018edd3 1839 break;
8824e2ce 1840 case s_BASE:
8824e2ce 1841 state2 = s_TRAILING;
f5db0993 1842 if (type2 == t_PATH) {
2f3cad89 1843 c2 = (uchar*)"/";
f5db0993
WD
1844 break;
1845 }
1846 /* FALL THROUGH */
8824e2ce 1847 case s_TRAILING:
f5db0993
WD
1848 if (!*c1)
1849 return 0;
1850 type2 = t_ITEM;
8018edd3
WD
1851 break;
1852 }
f5db0993
WD
1853 if (type1 != type2)
1854 return type1 == t_PATH ? 1 : -1;
8018edd3 1855 }
e4fdf1de 1856 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
8018edd3
WD
1857
1858 return dif;
1859}
1860
8018edd3 1861/* Return a copy of the full filename of a flist entry, using the indicated
5e4ff5f9
WD
1862 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1863 * done because we checked the size when creating the file_struct entry.
8018edd3 1864 */
5e4ff5f9 1865char *f_name(struct file_struct *f, char *fbuf)
8018edd3 1866{
ebed4c3a
MP
1867 if (!f || !f->basename)
1868 return NULL;
3ec4dd97 1869
5e4ff5f9
WD
1870 if (!fbuf) {
1871 static char names[5][MAXPATHLEN];
1872 static unsigned int n;
1873
1874 n = (n + 1) % (sizeof names / sizeof names[0]);
1875
1876 fbuf = names[n];
1877 }
1878
3ec4dd97 1879 if (f->dirname) {
882e6893
WD
1880 int len = strlen(f->dirname);
1881 memcpy(fbuf, f->dirname, len);
1882 fbuf[len] = '/';
cabd60fd 1883 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
8018edd3 1884 } else
cabd60fd 1885 strlcpy(fbuf, f->basename, MAXPATHLEN);
0ee6ca98 1886
b7736c79 1887 return fbuf;
8018edd3 1888}
e03dfae5 1889
32cbfe7b
WD
1890/* Do a non-recursive scan of the named directory, possibly ignoring all
1891 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1892 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1893 * buffer (the functions we call will append names onto the end, but the old
1894 * dir value will be restored on exit). */
1895struct file_list *get_dirlist(char *dirname, int dlen,
1896 int ignore_filter_rules)
37802f40
WD
1897{
1898 struct file_list *dirlist;
1899 char dirbuf[MAXPATHLEN];
37802f40 1900 int save_recurse = recurse;
1661fe9b 1901 int save_xfer_dirs = xfer_dirs;
37802f40 1902
32cbfe7b
WD
1903 if (dlen < 0) {
1904 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1905 if (dlen >= MAXPATHLEN)
1906 return NULL;
1907 dirname = dirbuf;
1908 }
37802f40
WD
1909
1910 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
32cbfe7b 1911
37802f40 1912 recurse = 0;
1661fe9b 1913 xfer_dirs = 1;
32cbfe7b 1914 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
1661fe9b 1915 xfer_dirs = save_xfer_dirs;
37802f40 1916 recurse = save_recurse;
53135fe8
WD
1917 if (do_progress)
1918 flist_count_offset += dirlist->count;
37802f40 1919
564ef546
WD
1920 clean_flist(dirlist, 0, 0);
1921
45478cc7 1922 if (verbose > 3)
32cbfe7b 1923 output_flist(dirlist);
45478cc7 1924
32cbfe7b 1925 return dirlist;
45478cc7 1926}