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