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