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