Changed --skip-empty-dirs (-k) to --prune-empty-dirs (-m).
[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;
9c000f5e 56extern int skip_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;
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;
9c000f5e 79static int max_dir_depth = 0;
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{
740819ef 134 char perms[11];
f7632fc6 135
e5ce3bcf 136 if (!f->basename) {
7212be92
DD
137 /* this can happen if duplicate names were removed */
138 return;
e5ce3bcf 139 }
7212be92 140
740819ef
MP
141 permstring(perms, f->mode);
142
4f5b0756 143#ifdef SUPPORT_LINKS
f7632fc6 144 if (preserve_links && S_ISLNK(f->mode)) {
ebed4c3a
MP
145 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
146 perms,
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
MP
152 rprintf(FINFO, "%s %11.0f %s %s\n",
153 perms,
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;
9c000f5e
WD
638 if (lastdir_depth >= max_dir_depth)
639 max_dir_depth = lastdir_depth + 1;
f3c3ed44
WD
640 } else if (dirname) {
641 file->dirname = dirname; /* we're reusing lastname */
642 file->dir.depth = lastdir_depth + 1;
643 } else
644 file->dir.depth = 1;
645
649f8742 646 if (S_ISDIR(mode)) {
ee3751c8 647 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
f3c3ed44 648 file->dir.depth--;
ee3751c8 649 if (flags & XMIT_TOP_DIR) {
75c51953 650 in_del_hier = recurse;
f3c3ed44 651 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
7a16e122 652 if (relative_paths && del_hier_name_len > 2
9cdadbb1
WD
653 && lastname[del_hier_name_len-1] == '.'
654 && lastname[del_hier_name_len-2] == '/')
7a16e122 655 del_hier_name_len -= 2;
ee3751c8
WD
656 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
657 } else if (in_del_hier) {
f3c3ed44
WD
658 if (!relative_paths || !del_hier_name_len
659 || (l1 >= del_hier_name_len
9cdadbb1 660 && lastname[del_hier_name_len] == '/'))
ee3751c8 661 file->flags |= FLAG_DEL_HERE;
649f8742
WD
662 else
663 in_del_hier = 0;
664 }
665 }
666
a289addd
WD
667 file->basename = bp;
668 memcpy(bp, basename, basename_len);
669 bp += basename_len;
670
b5c6a6ae
WD
671 if ((preserve_devices && IS_DEVICE(mode))
672 || (preserve_specials && IS_SPECIAL(mode)))
a289addd
WD
673 file->u.rdev = rdev;
674
4f5b0756 675#ifdef SUPPORT_LINKS
a289addd
WD
676 if (linkname_len) {
677 file->u.link = bp;
678 read_sbuf(f, bp, linkname_len - 1);
679 if (sanitize_paths)
33ffd7c3 680 sanitize_path(bp, bp, "", lastdir_depth);
a289addd
WD
681 bp += linkname_len;
682 }
683#endif
684
4f5b0756 685#ifdef SUPPORT_HARD_LINKS
9935066b
S
686 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
687 flags |= XMIT_HAS_IDEV_DATA;
97a67bdf 688 if (flags & XMIT_HAS_IDEV_DATA) {
1490812a 689 int64 inode;
4124540d
WD
690 if (protocol_version < 26) {
691 dev = read_int(f);
9935066b 692 inode = read_int(f);
736a6a29 693 } else {
4124540d
WD
694 if (!(flags & XMIT_SAME_DEV))
695 dev = read_longint(f);
9935066b
S
696 inode = read_longint(f);
697 }
698 if (flist->hlink_pool) {
5bf63a11
S
699 file->link_u.idev = pool_talloc(flist->hlink_pool,
700 struct idev, 1, "inode_table");
9935066b
S
701 file->F_INODE = inode;
702 file->F_DEV = dev;
736a6a29 703 }
72914a60 704 }
dc5ddbcc 705#endif
ebed4c3a 706
9a4a237e 707 if (always_checksum && (sum_len || protocol_version < 28)) {
7c4f063b 708 char *sum;
a289addd
WD
709 if (sum_len) {
710 file->u.sum = sum = bp;
711 /*bp += sum_len;*/
9a4a237e 712 } else {
fea4db62 713 /* Prior to 28, we get a useless set of nulls. */
7c4f063b 714 sum = empty_sum;
fea4db62 715 }
7752df41 716 read_buf(f, sum, checksum_len);
72914a60 717 }
ebed4c3a 718
72914a60 719 if (!preserve_perms) {
72914a60 720 /* set an appropriate set of permissions based on original
fea4db62 721 * permissions and umask. This emulates what GNU cp does */
72914a60
AT
722 file->mode &= ~orig_umask;
723 }
f5db0993
WD
724
725 return file;
c627d613
AT
726}
727
db719fb0
MP
728/**
729 * Create a file_struct for a named file by reading its stat()
730 * information and performing extensive checks against global
731 * options.
732 *
733 * @return the new file, or NULL if there was an error or this file
734 * should be excluded.
735 *
736 * @todo There is a small optimization opportunity here to avoid
737 * stat()ing the file in some circumstances, which has a certain cost.
738 * We are called immediately after doing readdir(), and so we may
739 * already know the d_type of the file. We could for example avoid
740 * statting directories if we're not recursing, but this is not a very
741 * important case. Some systems may not have d_type.
742 **/
314f4591 743struct file_struct *make_file(char *fname, struct file_list *flist,
96b87581
WD
744 STRUCT_STAT *stp, unsigned short flags,
745 int filter_level)
c627d613 746{
a289addd
WD
747 static char *lastdir;
748 static int lastdir_len = -1;
3ec4dd97 749 struct file_struct *file;
bcacc18b 750 STRUCT_STAT st;
3ec4dd97 751 char sum[SUM_LENGTH];
1923b1fc 752 char thisname[MAXPATHLEN];
e0870f1d 753 char linkname[MAXPATHLEN];
a1d55ad0 754 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd 755 char *basename, *dirname, *bp;
3ec4dd97 756
d508258a 757 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
7de2483f 758 lastdir_len = -1;
9935066b 759
1923b1fc
WD
760 if (strlcpy(thisname, fname, sizeof thisname)
761 >= sizeof thisname - flist_dir_len) {
0ee6ca98 762 rprintf(FINFO, "skipping overly long name: %s\n", fname);
882e6893
WD
763 return NULL;
764 }
58b1999e 765 clean_fname(thisname, 0);
b7736c79 766 if (sanitize_paths)
33ffd7c3 767 sanitize_path(thisname, thisname, "", 0);
3ec4dd97 768
ebed4c3a 769 memset(sum, 0, SUM_LENGTH);
3ec4dd97 770
ede1f0eb
WD
771 if (stp && S_ISDIR(stp->st_mode))
772 st = *stp; /* Needed for "symlink/." with --relative. */
773 else if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 774 int save_errno = errno;
a4a7e64c 775 /* See if file is excluded before reporting an error. */
7842418b
WD
776 if (filter_level != NO_FILTERS
777 && is_excluded(thisname, 0, filter_level))
a4a7e64c
WD
778 return NULL;
779 if (save_errno == ENOENT) {
4f5b0756 780#ifdef SUPPORT_LINKS
a4a7e64c
WD
781 /* Avoid "vanished" error if symlink points nowhere. */
782 if (copy_links && do_lstat(thisname, &st) == 0
783 && S_ISLNK(st.st_mode)) {
784 io_error |= IOERR_GENERAL;
785 rprintf(FERROR, "symlink has no referent: %s\n",
786 full_fname(thisname));
e5ce3bcf
WD
787 } else
788#endif
789 {
a4a7e64c
WD
790 enum logcode c = am_daemon && protocol_version < 28
791 ? FERROR : FINFO;
792 io_error |= IOERR_VANISHED;
793 rprintf(c, "file has vanished: %s\n",
794 full_fname(thisname));
76e26e10 795 }
a4a7e64c 796 } else {
a8726d2a 797 io_error |= IOERR_GENERAL;
d62bcc17
WD
798 rsyserr(FERROR, save_errno, "readlink %s failed",
799 full_fname(thisname));
76e26e10 800 }
3ec4dd97
AT
801 return NULL;
802 }
c627d613 803
7842418b
WD
804 /* backup.c calls us with filter_level set to NO_FILTERS. */
805 if (filter_level == NO_FILTERS)
806 goto skip_filters;
ac1a0994 807
7e037c42 808 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
0ee6ca98 809 rprintf(FINFO, "skipping directory %s\n", thisname);
3ec4dd97
AT
810 return NULL;
811 }
ebed4c3a 812
e90cdb8a
WD
813 /* We only care about directories because we need to avoid recursing
814 * into a mount-point directory, not to avoid copying a symlinked
815 * file if -L (or similar) was specified. */
535737bf
WD
816 if (one_file_system && st.st_dev != filesystem_dev
817 && S_ISDIR(st.st_mode)) {
818 if (one_file_system > 1) {
819 if (verbose > 2) {
820 rprintf(FINFO, "skipping mount-point dir %s\n",
821 thisname);
ebec5eb6 822 }
535737bf 823 return NULL;
ebec5eb6 824 }
535737bf 825 flags |= FLAG_MOUNT_POINT;
3b173846 826 }
ebed4c3a 827
7842418b 828 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
76e26e10
DD
829 return NULL;
830
132fcf36 831 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 832#ifdef SUPPORT_LINKS
132fcf36
WD
833 if (!S_ISLNK(st.st_mode))
834#endif
835 if (access(thisname, R_OK) != 0)
836 return NULL;
837 }
ac1a0994 838
97b7bff4 839 skip_filters:
ac1a0994 840
ea847c62
WD
841 if (verbose > 2) {
842 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
0ee6ca98 843 who_am_i(), thisname, filter_level);
ea847c62 844 }
ebed4c3a 845
a289addd
WD
846 if ((basename = strrchr(thisname, '/')) != NULL) {
847 dirname_len = ++basename - thisname; /* counts future '\0' */
848 if (lastdir_len == dirname_len - 1
849 && strncmp(thisname, lastdir, lastdir_len) == 0) {
850 dirname = lastdir;
851 dirname_len = 0; /* indicates no copy is needed */
852 } else
853 dirname = thisname;
3ec4dd97 854 } else {
a289addd
WD
855 basename = thisname;
856 dirname = NULL;
857 dirname_len = 0;
3ec4dd97 858 }
a289addd 859 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 860
4f5b0756 861#ifdef SUPPORT_LINKS
a289addd
WD
862 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
863#else
864 linkname_len = 0;
865#endif
866
902f03d1
WD
867 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
868 ? MD4_SUM_LENGTH : 0;
a289addd 869
61dec11a 870 alloc_len = file_struct_len + dirname_len + basename_len
9935066b 871 + linkname_len + sum_len;
99aaa6ca
WD
872 if (flist)
873 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
874 else {
9935066b 875 if (!(bp = new_array(char, alloc_len)))
99aaa6ca 876 out_of_memory("make_file");
9935066b
S
877 }
878
a289addd 879 file = (struct file_struct *)bp;
9935066b 880 memset(bp, 0, file_struct_len);
61dec11a 881 bp += file_struct_len;
a289addd
WD
882
883 file->flags = flags;
3ec4dd97
AT
884 file->modtime = st.st_mtime;
885 file->length = st.st_size;
8bbe41b5 886 file->mode = st.st_mode;
7b6fa00f
WD
887 file->uid = st.st_uid;
888 file->gid = st.st_gid;
0d162bd1 889
4f5b0756 890#ifdef SUPPORT_HARD_LINKS
9935066b
S
891 if (flist && flist->hlink_pool) {
892 if (protocol_version < 28) {
893 if (S_ISREG(st.st_mode))
894 file->link_u.idev = pool_talloc(
895 flist->hlink_pool, struct idev, 1,
896 "inode_table");
897 } else {
898 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
899 file->link_u.idev = pool_talloc(
900 flist->hlink_pool, struct idev, 1,
901 "inode_table");
902 }
903 }
904 if (file->link_u.idev) {
61dec11a
WD
905 file->F_DEV = st.st_dev;
906 file->F_INODE = st.st_ino;
907 }
908#endif
909
a289addd
WD
910 if (dirname_len) {
911 file->dirname = lastdir = bp;
912 lastdir_len = dirname_len - 1;
913 memcpy(bp, dirname, dirname_len - 1);
914 bp += dirname_len;
915 bp[-1] = '\0';
916 } else if (dirname)
917 file->dirname = dirname;
918
919 file->basename = bp;
920 memcpy(bp, basename, basename_len);
921 bp += basename_len;
922
4f5b0756 923#ifdef HAVE_STRUCT_STAT_ST_RDEV
b5c6a6ae
WD
924 if ((preserve_devices && IS_DEVICE(st.st_mode))
925 || (preserve_specials && IS_SPECIAL(st.st_mode)))
0d162bd1
WD
926 file->u.rdev = st.st_rdev;
927#endif
928
4f5b0756 929#ifdef SUPPORT_LINKS
a289addd
WD
930 if (linkname_len) {
931 file->u.link = bp;
932 memcpy(bp, linkname, linkname_len);
933 bp += linkname_len;
934 }
0d162bd1
WD
935#endif
936
a289addd
WD
937 if (sum_len) {
938 file->u.sum = bp;
939 file_checksum(thisname, bp, st.st_size);
940 /*bp += sum_len;*/
ebed4c3a 941 }
c627d613 942
f3c3ed44 943 file->dir.root = flist_dir;
c627d613 944
23f4587f
WD
945 /* This code is only used by the receiver when it is building
946 * a list of files for a delete pass. */
947 if (keep_dirlinks && linkname_len && flist) {
948 STRUCT_STAT st2;
f5db0993 949 int save_mode = file->mode;
8bbe41b5 950 file->mode = S_IFDIR; /* Find a directory with our name. */
d64e6f42 951 if (flist_find(the_file_list, file) >= 0
23f4587f
WD
952 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
953 file->modtime = st2.st_mtime;
954 file->length = st2.st_size;
955 file->mode = st2.st_mode;
7b6fa00f
WD
956 file->uid = st2.st_uid;
957 file->gid = st2.st_gid;
23f4587f 958 file->u.link = NULL;
f5db0993
WD
959 } else
960 file->mode = save_mode;
23f4587f
WD
961 }
962
d17190df 963 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
a800434a 964 stats.total_size += st.st_size;
c627d613 965
3ec4dd97 966 return file;
c627d613
AT
967}
968
301fb56c 969static struct file_struct *send_file_name(int f, struct file_list *flist,
ede1f0eb 970 char *fname, STRUCT_STAT *stp,
96b87581 971 unsigned short flags)
c627d613 972{
ebed4c3a
MP
973 struct file_struct *file;
974
96b87581 975 file = make_file(fname, flist, stp, flags,
ede1f0eb 976 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
37802f40 977 if (!file)
301fb56c 978 return NULL;
ebed4c3a 979
ebec5eb6 980 if (chmod_modes && !S_ISLNK(file->mode))
8bbe41b5
WD
981 file->mode = tweak_mode(file->mode, chmod_modes);
982
53135fe8 983 maybe_emit_filelist_progress(flist->count + flist_count_offset);
ebed4c3a 984
d9d6bc52 985 flist_expand(flist);
ebed4c3a 986
c120ff37 987 if (file->basename[0]) {
ebed4c3a 988 flist->files[flist->count++] = file;
96b87581 989 send_file_entry(file, f);
ebed4c3a 990 }
301fb56c
WD
991 return file;
992}
ebed4c3a 993
301fb56c 994static void send_if_directory(int f, struct file_list *flist,
56f0c976
WD
995 struct file_struct *file,
996 char *fbuf, unsigned int ol)
301fb56c 997{
56f0c976 998 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
301fb56c
WD
999
1000 if (S_ISDIR(file->mode)
5e4ff5f9 1001 && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) {
f1773e09
WD
1002 void *save_filters;
1003 unsigned int len = strlen(fbuf);
1004 if (len > 1 && fbuf[len-1] == '/')
1005 fbuf[--len] = '\0';
1006 if (len >= MAXPATHLEN - 1) {
1007 io_error |= IOERR_GENERAL;
1008 rprintf(FERROR, "skipping long-named directory: %s\n",
1009 full_fname(fbuf));
1010 return;
1011 }
1012 save_filters = push_local_filters(fbuf, len);
1013 send_directory(f, flist, fbuf, len);
1014 pop_local_filters(save_filters);
56f0c976
WD
1015 fbuf[ol] = '\0';
1016 if (is_dot_dir)
1017 fbuf[ol-1] = '.';
ebed4c3a 1018 }
c627d613
AT
1019}
1020
301fb56c 1021/* This function is normally called by the sender, but the receiving side also
7b558d7f 1022 * calls it from get_dirlist() with f set to -1 so that we just construct the
301fb56c
WD
1023 * file list in memory without sending it over the wire. Also, get_dirlist()
1024 * might call this with f set to -2, which also indicates that local filter
1025 * rules should be ignored. */
f1773e09 1026static void send_directory(int f, struct file_list *flist,
32cbfe7b 1027 char *fbuf, int len)
c627d613 1028{
3ec4dd97 1029 struct dirent *di;
32cbfe7b 1030 unsigned remainder;
3ec4dd97 1031 char *p;
f1773e09 1032 DIR *d;
301fb56c 1033 int start = flist->count;
3ec4dd97 1034
f1773e09 1035 if (!(d = opendir(fbuf))) {
06c28400 1036 io_error |= IOERR_GENERAL;
f1773e09 1037 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1038 return;
1039 }
c627d613 1040
19b2a5d9
WD
1041 p = fbuf + len;
1042 if (len != 1 || *fbuf != '/')
eddd5d12 1043 *p++ = '/';
f1773e09 1044 *p = '\0';
32cbfe7b 1045 remainder = MAXPATHLEN - (p - fbuf);
ebed4c3a 1046
6a7cc46c 1047 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1048 char *dname = d_name(di);
6a7cc46c
S
1049 if (dname[0] == '.' && (dname[1] == '\0'
1050 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1051 continue;
0ee6ca98 1052 if (strlcpy(p, dname, remainder) >= remainder) {
eddd5d12
WD
1053 io_error |= IOERR_GENERAL;
1054 rprintf(FINFO,
1055 "cannot send long-named file %s\n",
f1773e09 1056 full_fname(fbuf));
beaf4954 1057 continue;
eddd5d12 1058 }
0ee6ca98 1059
ede1f0eb 1060 send_file_name(f, flist, fbuf, NULL, 0);
3ec4dd97 1061 }
32cbfe7b
WD
1062
1063 fbuf[len] = '\0';
1064
6a7cc46c 1065 if (errno) {
06c28400 1066 io_error |= IOERR_GENERAL;
71903f60 1067 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1068 }
c627d613 1069
3ec4dd97 1070 closedir(d);
301fb56c
WD
1071
1072 if (recurse) {
1073 int i, end = flist->count - 1;
1074 for (i = start; i <= end; i++)
56f0c976 1075 send_if_directory(f, flist, flist->files[i], fbuf, len);
301fb56c 1076 }
c627d613
AT
1077}
1078
ebed4c3a 1079struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1080{
56f0c976 1081 int len;
bcacc18b 1082 STRUCT_STAT st;
808c57c3 1083 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 1084 char lastpath[MAXPATHLEN] = "";
649d65ed 1085 struct file_list *flist;
31b4d25d 1086 struct timeval start_tv, end_tv;
a800434a 1087 int64 start_write;
24d0fcde 1088 int use_ff_fd = 0;
649d65ed 1089
134f4338 1090 if (show_filelist_p())
1bbd10fe 1091 start_filelist_progress("building file list");
c627d613 1092
a800434a 1093 start_write = stats.total_written;
31b4d25d 1094 gettimeofday(&start_tv, NULL);
a800434a 1095
134f4338 1096 flist = flist_new(WITH_HLINK, "send_file_list");
c627d613 1097
134f4338
WD
1098 io_start_buffering_out();
1099 if (filesfrom_fd >= 0) {
1100 if (argv[0] && !push_dir(argv[0])) {
1101 rsyserr(FERROR, errno, "push_dir %s failed",
1102 full_fname(argv[0]));
1103 exit_cleanup(RERR_FILESELECT);
24d0fcde 1104 }
134f4338 1105 use_ff_fd = 1;
d6dead6b
AT
1106 }
1107
24d0fcde 1108 while (1) {
56f0c976
WD
1109 char fbuf[MAXPATHLEN];
1110 char *fn;
301fb56c 1111 int is_dot_dir;
c627d613 1112
24d0fcde 1113 if (use_ff_fd) {
56f0c976 1114 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
24d0fcde 1115 break;
56f0c976 1116 sanitize_path(fbuf, fbuf, "", 0);
24d0fcde
WD
1117 } else {
1118 if (argc-- == 0)
1119 break;
56f0c976 1120 strlcpy(fbuf, *argv++, MAXPATHLEN);
24d0fcde 1121 if (sanitize_paths)
56f0c976 1122 sanitize_path(fbuf, fbuf, "", 0);
24d0fcde 1123 }
c627d613 1124
56f0c976 1125 len = strlen(fbuf);
1902a765
WD
1126 if (relative_paths) {
1127 /* We clean up fbuf below. */
1128 is_dot_dir = 0;
1129 } else if (!len || fbuf[len - 1] == '/') {
56f0c976 1130 if (len == 2 && fbuf[0] == '.') {
6931c138 1131 /* Turn "./" into just "." rather than "./." */
56f0c976 1132 fbuf[1] = '\0';
557a35f5 1133 } else {
56f0c976 1134 if (len + 1 >= MAXPATHLEN)
a1f99493 1135 overflow_exit("send_file_list");
56f0c976
WD
1136 fbuf[len++] = '.';
1137 fbuf[len] = '\0';
53f821f1 1138 }
301fb56c 1139 is_dot_dir = 1;
56f0c976
WD
1140 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1141 && (len == 2 || fbuf[len-3] == '/')) {
1142 if (len + 2 >= MAXPATHLEN)
a1f99493 1143 overflow_exit("send_file_list");
56f0c976
WD
1144 fbuf[len++] = '/';
1145 fbuf[len++] = '.';
1146 fbuf[len] = '\0';
a289f89f 1147 is_dot_dir = 1;
301fb56c 1148 } else {
56f0c976
WD
1149 is_dot_dir = fbuf[len-1] == '.'
1150 && (len == 1 || fbuf[len-2] == '/');
649d65ed 1151 }
c627d613 1152
56f0c976 1153 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
134f4338
WD
1154 io_error |= IOERR_GENERAL;
1155 rsyserr(FERROR, errno, "link_stat %s failed",
56f0c976 1156 full_fname(fbuf));
649d65ed
AT
1157 continue;
1158 }
c627d613 1159
7e037c42 1160 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
0ee6ca98 1161 rprintf(FINFO, "skipping directory %s\n", fbuf);
649d65ed
AT
1162 continue;
1163 }
c627d613 1164
649d65ed 1165 dir = NULL;
808c57c3 1166 olddir[0] = '\0';
649d65ed
AT
1167
1168 if (!relative_paths) {
56f0c976 1169 p = strrchr(fbuf, '/');
649d65ed 1170 if (p) {
151f59f1 1171 *p = '\0';
56f0c976 1172 if (p == fbuf)
649d65ed
AT
1173 dir = "/";
1174 else
56f0c976
WD
1175 dir = fbuf;
1176 len -= p - fbuf + 1;
1177 fn = p + 1;
1178 } else
1179 fn = fbuf;
1902a765
WD
1180 } else {
1181 if ((p = strstr(fbuf, "/./")) != NULL) {
1182 *p = '\0';
1183 if (p == fbuf)
1184 dir = "/";
1185 else
1186 dir = fbuf;
1187 len -= p - fbuf + 3;
1188 fn = p + 3;
1189 } else
1190 fn = fbuf;
1191 /* Get rid of trailing "/" and "/.". */
1192 while (len) {
ede1f0eb
WD
1193 if (fn[len - 1] == '/') {
1194 is_dot_dir = 1;
1195 if (!--len && !dir) {
1196 len++;
1197 break;
1198 }
1199 }
1902a765
WD
1200 else if (len >= 2 && fn[len - 1] == '.'
1201 && fn[len - 2] == '/') {
ede1f0eb 1202 is_dot_dir = 1;
1902a765
WD
1203 if (!(len -= 2) && !dir) {
1204 len++;
1205 break;
1206 }
1207 } else
1208 break;
1209 }
1210 fn[len] = '\0';
1211 /* Reject a ".." dir in the active part of the path. */
ede1f0eb
WD
1212 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1213 if ((p[2] == '/' || p[2] == '\0')
1214 && (p == fn || p[-1] == '/')) {
1215 rprintf(FERROR,
1216 "found \"..\" dir in relative path: %s\n",
1217 fbuf);
1218 exit_cleanup(RERR_SYNTAX);
1219 }
1902a765
WD
1220 }
1221 }
d2ea5980 1222
56f0c976
WD
1223 if (!*fn) {
1224 len = 1;
1225 fn = ".";
1226 }
d2ea5980
WD
1227
1228 if (dir && *dir) {
1229 static char *lastdir;
1230 static int lastdir_len;
1231
8ad5cea3 1232 strlcpy(olddir, curr_dir, sizeof olddir);
d2ea5980
WD
1233
1234 if (!push_dir(dir)) {
1235 io_error |= IOERR_GENERAL;
1236 rsyserr(FERROR, errno, "push_dir %s failed",
1237 full_fname(dir));
1238 continue;
1239 }
1240
1241 if (lastdir && strcmp(lastdir, dir) == 0) {
1242 flist_dir = lastdir;
1243 flist_dir_len = lastdir_len;
1244 } else {
1245 flist_dir = lastdir = strdup(dir);
1246 flist_dir_len = lastdir_len = strlen(dir);
1247 }
1248 }
1249
56f0c976
WD
1250 if (fn != fbuf)
1251 memmove(fbuf, fn, len + 1);
1252
1253 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
151f59f1
WD
1254 /* Send the implied directories at the start of the
1255 * source spec, so we get their permissions right. */
56f0c976 1256 char *lp = lastpath, *slash = fbuf;
151f59f1 1257 *p = '\0';
2154309a
WD
1258 /* Skip any initial directories in our path that we
1259 * have in common with lastpath. */
56f0c976 1260 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
2154309a
WD
1261 if (*fn == '/')
1262 slash = fn;
2154309a
WD
1263 }
1264 *p = '/';
1265 if (fn != p || (*lp && *lp != '/')) {
0f57446d 1266 int save_copy_links = copy_links;
7e037c42 1267 int save_xfer_dirs = xfer_dirs;
2154309a 1268 copy_links = copy_unsafe_links;
7e037c42 1269 xfer_dirs = 1;
2154309a 1270 while ((slash = strchr(slash+1, '/')) != 0) {
151f59f1 1271 *slash = '\0';
ede1f0eb 1272 send_file_name(f, flist, fbuf, NULL, 0);
2154309a 1273 *slash = '/';
649d65ed 1274 }
0f57446d 1275 copy_links = save_copy_links;
7e037c42 1276 xfer_dirs = save_xfer_dirs;
151f59f1 1277 *p = '\0';
56f0c976 1278 strlcpy(lastpath, fbuf, sizeof lastpath);
649d65ed
AT
1279 *p = '/';
1280 }
1281 }
ebed4c3a 1282
535737bf
WD
1283 if (one_file_system)
1284 filesystem_dev = st.st_dev;
1285
75c51953
WD
1286 if (recurse || (xfer_dirs && is_dot_dir)) {
1287 struct file_struct *file;
96b87581 1288 file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR);
ede1f0eb 1289 if (file)
56f0c976 1290 send_if_directory(f, flist, file, fbuf, len);
75c51953 1291 } else
ede1f0eb 1292 send_file_name(f, flist, fbuf, &st, 0);
2bca43f6 1293
808c57c3 1294 if (olddir[0]) {
649d65ed 1295 flist_dir = NULL;
882e6893 1296 flist_dir_len = 0;
808c57c3 1297 if (!pop_dir(olddir)) {
d62bcc17 1298 rsyserr(FERROR, errno, "pop_dir %s failed",
92cdc393 1299 full_fname(olddir));
65417579 1300 exit_cleanup(RERR_FILESELECT);
649d65ed 1301 }
649d65ed 1302 }
649d65ed 1303 }
dc5ddbcc 1304
134f4338
WD
1305 gettimeofday(&end_tv, NULL);
1306 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1307 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1308 if (stats.flist_buildtime == 0)
1309 stats.flist_buildtime = 1;
1310 start_tv = end_tv;
31b4d25d 1311
96b87581 1312 send_file_entry(NULL, f);
c627d613 1313
134f4338
WD
1314 if (show_filelist_p())
1315 finish_filelist_progress(flist);
31b4d25d 1316
134f4338
WD
1317 gettimeofday(&end_tv, NULL);
1318 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1319 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 1320
7cf8e8d0 1321 if (flist->hlink_pool) {
9935066b
S
1322 pool_destroy(flist->hlink_pool);
1323 flist->hlink_pool = NULL;
1324 }
1325
f5db0993
WD
1326 /* Sort the list without removing any duplicates. This allows the
1327 * receiving side to ask for any name they like, which gives us the
1328 * flexibility to change the way we unduplicate names in the future
1329 * without causing a compatibility problem with older versions. */
827c37f6 1330 clean_flist(flist, 0, 0);
ebed4c3a 1331
134f4338 1332 send_uid_list(f);
f6c34742 1333
134f4338
WD
1334 /* send the io_error flag */
1335 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1336
134f4338
WD
1337 io_end_buffering();
1338 stats.flist_size = stats.total_written - start_write;
1339 stats.num_files = flist->count;
d6dead6b 1340
cefed3e8 1341 if (verbose > 3)
32cbfe7b 1342 output_flist(flist);
cefed3e8 1343
17faa41c 1344 if (verbose > 2)
ebed4c3a 1345 rprintf(FINFO, "send_file_list done\n");
17faa41c 1346
649d65ed 1347 return flist;
c627d613
AT
1348}
1349
c627d613
AT
1350struct file_list *recv_file_list(int f)
1351{
ebed4c3a 1352 struct file_list *flist;
1ef00d20 1353 unsigned short flags;
ebed4c3a 1354 int64 start_read;
c627d613 1355
1bbd10fe
DD
1356 if (show_filelist_p())
1357 start_filelist_progress("receiving file list");
c627d613 1358
ebed4c3a 1359 start_read = stats.total_read;
a800434a 1360
9935066b 1361 flist = flist_new(WITH_HLINK, "recv_file_list");
c627d613 1362
ebed4c3a
MP
1363 flist->count = 0;
1364 flist->malloced = 1000;
58cadc86 1365 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1366 if (!flist->files)
1367 goto oom;
c627d613 1368
1ef00d20 1369 while ((flags = read_byte(f)) != 0) {
f5db0993 1370 struct file_struct *file;
dbda5fbf 1371
d9d6bc52 1372 flist_expand(flist);
c627d613 1373
d01d15e0 1374 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1375 flags |= read_byte(f) << 8;
f5db0993 1376 file = receive_file_entry(flist, flags, f);
c627d613 1377
f5db0993
WD
1378 if (S_ISREG(file->mode))
1379 stats.total_size += file->length;
c627d613 1380
f5db0993 1381 flist->files[flist->count++] = file;
c627d613 1382
53135fe8 1383 maybe_emit_filelist_progress(flist->count);
1bbd10fe 1384
5e4ff5f9
WD
1385 if (verbose > 2) {
1386 rprintf(FINFO, "recv_file_name(%s)\n",
1387 f_name(file, NULL));
1388 }
ebed4c3a 1389 }
f5db0993 1390 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
c627d613 1391
ebed4c3a
MP
1392 if (verbose > 2)
1393 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1394
b7736c79 1395 if (show_filelist_p())
1bbd10fe 1396 finish_filelist_progress(flist);
a06d19e3 1397
983b1ed3
WD
1398 clean_flist(flist, relative_paths, 1);
1399
37802f40 1400 if (f >= 0) {
7b6fa00f 1401 recv_uid_list(f, flist);
f6c34742 1402
b9f592fb
WD
1403 /* Recv the io_error flag */
1404 if (lp_ignore_errors(module_id) || ignore_errors)
1405 read_int(f);
1406 else
1407 io_error |= read_int(f);
ebed4c3a 1408 }
6ba9279f 1409
cefed3e8 1410 if (verbose > 3)
32cbfe7b 1411 output_flist(flist);
cefed3e8 1412
ebed4c3a
MP
1413 if (list_only) {
1414 int i;
b7736c79 1415 for (i = 0; i < flist->count; i++)
ebed4c3a 1416 list_file_entry(flist->files[i]);
ebed4c3a 1417 }
f7632fc6 1418
ebed4c3a
MP
1419 if (verbose > 2)
1420 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1421
ebed4c3a
MP
1422 stats.flist_size = stats.total_read - start_read;
1423 stats.num_files = flist->count;
a800434a 1424
ebed4c3a 1425 return flist;
c627d613 1426
97b7bff4 1427 oom:
ebed4c3a
MP
1428 out_of_memory("recv_file_list");
1429 return NULL; /* not reached */
c627d613
AT
1430}
1431
14698a3a 1432static int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1433{
14698a3a 1434 return f_name_cmp(*file1, *file2);
c627d613
AT
1435}
1436
f5db0993
WD
1437/* Search for an identically-named item in the file list. Note that the
1438 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 1439int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1440{
f3c3ed44
WD
1441 int low = flist->low, high = flist->high;
1442 int ret, mid, mid_up;
1443
1444 while (low <= high) {
1445 mid = (low + high) / 2;
c0b134a4
WD
1446 if (flist->files[mid]->basename)
1447 mid_up = mid;
7402d583
WD
1448 else {
1449 /* Scan for the next non-empty entry using the cached
1450 * distance values. If the value isn't fully up-to-
1451 * date, update it. */
1452 while (1) {
1453 mid_up = mid + flist->files[mid]->dir.depth;
1454 if (flist->files[mid_up]->basename)
1455 break;
1456 flist->files[mid]->dir.depth
1457 += flist->files[mid_up]->dir.depth;
c0b134a4 1458 }
c0b134a4 1459 if (mid_up > high) {
7402d583
WD
1460 /* If there's nothing left above us, set high to
1461 * a non-empty entry below us and continue. */
1462 while (1) {
1463 high = mid - flist->files[mid]->length;
1464 if (flist->files[high]->basename)
1465 break;
1466 flist->files[mid]->length
1467 += flist->files[high]->length;
1468 }
c0b134a4
WD
1469 continue;
1470 }
c0b134a4
WD
1471 }
1472 ret = f_name_cmp(flist->files[mid_up], f);
f5db0993
WD
1473 if (ret == 0) {
1474 if (protocol_version < 29
1475 && S_ISDIR(flist->files[mid_up]->mode)
1476 != S_ISDIR(f->mode))
1477 return -1;
f3c3ed44 1478 return mid_up;
f5db0993 1479 }
b7736c79 1480 if (ret > 0)
f3c3ed44 1481 high = mid - 1;
b7736c79 1482 else
f3c3ed44 1483 low = mid_up + 1;
d966ee25 1484 }
d966ee25 1485 return -1;
c627d613
AT
1486}
1487
3ec4dd97 1488/*
9935066b
S
1489 * Free up any resources a file_struct has allocated
1490 * and clear the file.
3ec4dd97 1491 */
9935066b 1492void clear_file(int i, struct file_list *flist)
c627d613 1493{
7402d583
WD
1494 struct file_struct *file = flist->files[i];
1495 if (flist->hlink_pool && file->link_u.idev)
1496 pool_free(flist->hlink_pool, 0, file->link_u.idev);
1497 memset(file, 0, file_struct_len);
1498 /* In an empty entry, dir.depth is an offset to the next non-empty
1499 * entry. Likewise for length in the opposite direction. We assume
1500 * that we're alone for now since flist_find() will collate adjacent
1501 * items for any entries that are encountered during the find. */
1502 file->length = file->dir.depth = 1;
3ec4dd97 1503}
c627d613 1504
3d382777
AT
1505/*
1506 * allocate a new file list
1507 */
9935066b 1508struct file_list *flist_new(int with_hlink, char *msg)
3d382777
AT
1509{
1510 struct file_list *flist;
1511
58cadc86 1512 flist = new(struct file_list);
ebed4c3a 1513 if (!flist)
9935066b 1514 out_of_memory(msg);
3d382777 1515
9935066b
S
1516 memset(flist, 0, sizeof (struct file_list));
1517
1518 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1519 out_of_memory, POOL_INTERN)))
1520 out_of_memory(msg);
1521
4f5b0756 1522#ifdef SUPPORT_HARD_LINKS
9935066b 1523 if (with_hlink && preserve_hard_links) {
3e491682 1524 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
9935066b
S
1525 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1526 out_of_memory(msg);
1527 }
1528#endif
d9d6bc52 1529
3d382777
AT
1530 return flist;
1531}
ebed4c3a 1532
3ec4dd97
AT
1533/*
1534 * free up all elements in a flist
1535 */
1536void flist_free(struct file_list *flist)
1537{
9935066b
S
1538 pool_destroy(flist->file_pool);
1539 pool_destroy(flist->hlink_pool);
3ec4dd97 1540 free(flist->files);
3ec4dd97 1541 free(flist);
c627d613
AT
1542}
1543
c627d613
AT
1544/*
1545 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1546 * duplicate names can cause corruption because of the pipelining
c627d613 1547 */
827c37f6 1548static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1549{
6931c138 1550 int i, prev_i = 0;
c627d613 1551
910ee8c9
WD
1552 if (!flist)
1553 return;
1554 if (flist->count == 0) {
1555 flist->high = -1;
3ec4dd97 1556 return;
910ee8c9 1557 }
3ec4dd97 1558
ebed4c3a 1559 qsort(flist->files, flist->count,
a4a7e64c 1560 sizeof flist->files[0], (int (*)())file_compare);
ebed4c3a 1561
827c37f6 1562 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1563 if (flist->files[i]->basename) {
6931c138 1564 prev_i = i;
b91b50c0
WD
1565 break;
1566 }
1567 }
f3c3ed44 1568 flist->low = prev_i;
b91b50c0 1569 while (++i < flist->count) {
fe1c19dc 1570 int j;
f5db0993
WD
1571 struct file_struct *file = flist->files[i];
1572
1573 if (!file->basename)
b91b50c0 1574 continue;
fe1c19dc
WD
1575 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1576 j = prev_i;
1577 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
1578 int save_mode = file->mode;
1579 /* Make sure that this directory doesn't duplicate a
1580 * non-directory earlier in the list. */
f5db0993 1581 flist->high = prev_i;
fe1c19dc
WD
1582 file->mode = S_IFREG;
1583 j = flist_find(flist, file);
f5db0993 1584 file->mode = save_mode;
fe1c19dc
WD
1585 } else
1586 j = -1;
1587 if (j >= 0) {
1588 struct file_struct *fp = flist->files[j];
1589 int keep, drop;
1590 /* If one is a dir and the other is not, we want to
1591 * keep the dir because it might have contents in the
1592 * list. */
1593 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1594 if (S_ISDIR(file->mode))
1595 keep = i, drop = j;
1596 else
1597 keep = j, drop = i;
1598 } else
1599 keep = j, drop = i;
b91b50c0 1600 if (verbose > 1 && !am_server) {
ebed4c3a 1601 rprintf(FINFO,
fe1c19dc 1602 "removing duplicate name %s from file list (%d)\n",
5e4ff5f9 1603 f_name(file, NULL), drop);
b91b50c0 1604 }
9c000f5e
WD
1605 /* Make sure we don't lose track of a user-specified
1606 * top directory. */
0f0b2e66
WD
1607 flist->files[keep]->flags |= flist->files[drop]->flags
1608 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
fe1c19dc
WD
1609
1610 clear_file(drop, flist);
9935066b 1611
fe1c19dc
WD
1612 if (keep == i) {
1613 if (flist->low == drop) {
1614 for (j = drop + 1;
1615 j < i && !flist->files[j]->basename;
1616 j++) {}
1617 flist->low = j;
1618 }
1619 prev_i = i;
1620 }
728d0922 1621 } else
6931c138 1622 prev_i = i;
3ec4dd97 1623 }
f5db0993 1624 flist->high = no_dups ? prev_i : flist->count - 1;
0199b05f 1625
c0b134a4
WD
1626 if (strip_root) {
1627 /* We need to strip off the leading slashes for relative
1628 * paths, but this must be done _after_ the sorting phase. */
1629 for (i = flist->low; i <= flist->high; i++) {
1630 struct file_struct *file = flist->files[i];
1631
1632 if (!file->dirname)
1633 continue;
1634 while (*file->dirname == '/')
1635 file->dirname++;
1636 if (!*file->dirname)
1637 file->dirname = NULL;
1638 }
1639 }
1640
9c000f5e
WD
1641 if (skip_empty_dirs && no_dups && max_dir_depth) {
1642 int j, cur_depth = 0;
1643 int *maybe_dirs = new_array(int, max_dir_depth);
1644
1645 maybe_dirs[0] = -1;
1646
f5db0993
WD
1647 for (i = flist->low; i <= flist->high; i++) {
1648 struct file_struct *file = flist->files[i];
ebed4c3a 1649
9c000f5e
WD
1650 if (S_ISDIR(file->mode) && file->dir.depth) {
1651 j = cur_depth;
1652 cur_depth = file->dir.depth - 1;
1653 for ( ; j >= cur_depth; j--) {
1654 if (maybe_dirs[j] < 0)
1655 continue;
1656 clear_file(maybe_dirs[j], flist);
1657 }
1658 maybe_dirs[cur_depth] = i;
1659 } else if (maybe_dirs[cur_depth] >= 0) {
1660 for (j = 0; j <= cur_depth; j++)
1661 maybe_dirs[j] = -1;
0199b05f 1662 }
9c000f5e
WD
1663 }
1664 for (j = cur_depth; j >= 0; j--) {
1665 if (maybe_dirs[j] < 0)
1666 continue;
1667 clear_file(maybe_dirs[j], flist);
1668 }
f5db0993 1669
9c000f5e
WD
1670 for (i = flist->low; i <= flist->high; i++) {
1671 if (flist->files[i]->basename)
1672 break;
1673 }
1674 flist->low = i;
1675 for (i = flist->high; i >= flist->low; i--) {
1676 if (flist->files[i]->basename)
1677 break;
0199b05f 1678 }
9c000f5e 1679 flist->high = i;
0199b05f 1680 }
cefed3e8 1681}
0199b05f 1682
32cbfe7b 1683static void output_flist(struct file_list *flist)
cefed3e8 1684{
f3c3ed44 1685 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8 1686 struct file_struct *file;
32cbfe7b 1687 const char *who = who_am_i();
cefed3e8 1688 int i;
0199b05f 1689
ebed4c3a 1690 for (i = 0; i < flist->count; i++) {
cefed3e8 1691 file = flist->files[i];
56194bcd 1692 if ((am_root || am_sender) && preserve_uid)
7b6fa00f 1693 sprintf(uidbuf, " uid=%ld", (long)file->uid);
f05f993e
WD
1694 else
1695 *uidbuf = '\0';
7b6fa00f
WD
1696 if (preserve_gid && file->gid != GID_NONE)
1697 sprintf(gidbuf, " gid=%ld", (long)file->gid);
f05f993e
WD
1698 else
1699 *gidbuf = '\0';
f3c3ed44
WD
1700 if (!am_sender)
1701 sprintf(depthbuf, "%d", file->dir.depth);
14698a3a 1702 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
32cbfe7b 1703 who, i, am_sender ? NS(file->dir.root) : depthbuf,
0ee6ca98 1704 file->dirname ? file->dirname : "",
14698a3a
WD
1705 file->dirname ? "/" : "", NS(file->basename),
1706 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
f3c3ed44 1707 (double)file->length, uidbuf, gidbuf, file->flags);
0199b05f 1708 }
3ec4dd97
AT
1709}
1710
8824e2ce 1711enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 1712enum fnc_type { t_PATH, t_ITEM };
8018edd3 1713
2f3cad89 1714/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
1715 * would do if it were operating on the joined strings.
1716 *
1717 * Some differences beginning with protocol_version 29: (1) directory names
1718 * are compared with an assumed trailing slash so that they compare in a
1719 * way that would cause them to sort immediately prior to any content they
1720 * may have; (2) a directory of any name compares after a non-directory of
1721 * any name at the same depth; (3) a directory with name "." compares prior
1722 * to anything else. These changes mean that a directory and a non-dir
1723 * with the same name will not compare as equal (protocol_version >= 29).
1724 *
1725 * The dirname component can be an empty string, but the basename component
1726 * cannot (and never is in the current codebase). The basename component
1727 * may be NULL (for a removed item), in which case it is considered to be
1728 * after any existing item. */
8018edd3 1729int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1730{
8018edd3
WD
1731 int dif;
1732 const uchar *c1, *c2;
1ef00d20 1733 enum fnc_state state1, state2;
f5db0993
WD
1734 enum fnc_type type1, type2;
1735 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3
WD
1736
1737 if (!f1 || !f1->basename) {
1738 if (!f2 || !f2->basename)
1739 return 0;
1740 return -1;
1741 }
1742 if (!f2 || !f2->basename)
1743 return 1;
1744
14698a3a
WD
1745 c1 = (uchar*)f1->dirname;
1746 c2 = (uchar*)f2->dirname;
1747 if (c1 == c2)
1748 c1 = c2 = NULL;
1749 if (!c1) {
f5db0993 1750 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1751 c1 = (uchar*)f1->basename;
f5db0993
WD
1752 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1753 type1 = t_ITEM;
1754 state1 = s_TRAILING;
1755 c1 = (uchar*)"";
1756 } else
1757 state1 = s_BASE;
080ddf58 1758 } else if (!*c1) {
f5db0993 1759 type1 = t_path;
8824e2ce 1760 state1 = s_SLASH;
080ddf58 1761 c1 = (uchar*)"/";
f5db0993
WD
1762 } else {
1763 type1 = t_path;
8824e2ce 1764 state1 = s_DIR;
f5db0993 1765 }
14698a3a 1766 if (!c2) {
f5db0993 1767 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1768 c2 = (uchar*)f2->basename;
f5db0993
WD
1769 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1770 type2 = t_ITEM;
1771 state2 = s_TRAILING;
1772 c2 = (uchar*)"";
1773 } else
1774 state2 = s_BASE;
080ddf58 1775 } else if (!*c2) {
f5db0993 1776 type2 = t_path;
8824e2ce 1777 state2 = s_SLASH;
080ddf58 1778 c2 = (uchar*)"/";
f5db0993
WD
1779 } else {
1780 type2 = t_path;
8824e2ce 1781 state2 = s_DIR;
f5db0993
WD
1782 }
1783
1784 if (type1 != type2)
1785 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1786
1787 while (1) {
f5db0993 1788 if ((dif = (int)*c1++ - (int)*c2++) != 0)
8018edd3 1789 break;
f5db0993 1790 if (!*c1) {
8018edd3 1791 switch (state1) {
8824e2ce
WD
1792 case s_DIR:
1793 state1 = s_SLASH;
e90b8ace 1794 c1 = (uchar*)"/";
8018edd3 1795 break;
8824e2ce 1796 case s_SLASH:
f5db0993 1797 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1798 c1 = (uchar*)f1->basename;
cbb5fa4f
WD
1799 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1800 type1 = t_ITEM;
1801 state1 = s_TRAILING;
1802 c1 = (uchar*)"";
1803 } else
1804 state1 = s_BASE;
8018edd3 1805 break;
8824e2ce
WD
1806 case s_BASE:
1807 state1 = s_TRAILING;
f5db0993 1808 if (type1 == t_PATH) {
2f3cad89 1809 c1 = (uchar*)"/";
f5db0993
WD
1810 break;
1811 }
1812 /* FALL THROUGH */
8824e2ce 1813 case s_TRAILING:
f5db0993 1814 type1 = t_ITEM;
8018edd3
WD
1815 break;
1816 }
f5db0993
WD
1817 if (*c2 && type1 != type2)
1818 return type1 == t_PATH ? 1 : -1;
8018edd3 1819 }
f5db0993 1820 if (!*c2) {
8018edd3 1821 switch (state2) {
8824e2ce
WD
1822 case s_DIR:
1823 state2 = s_SLASH;
e90b8ace 1824 c2 = (uchar*)"/";
8018edd3 1825 break;
8824e2ce 1826 case s_SLASH:
f5db0993 1827 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1828 c2 = (uchar*)f2->basename;
cbb5fa4f
WD
1829 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1830 type2 = t_ITEM;
1831 state2 = s_TRAILING;
1832 c2 = (uchar*)"";
1833 } else
1834 state2 = s_BASE;
8018edd3 1835 break;
8824e2ce 1836 case s_BASE:
8824e2ce 1837 state2 = s_TRAILING;
f5db0993 1838 if (type2 == t_PATH) {
2f3cad89 1839 c2 = (uchar*)"/";
f5db0993
WD
1840 break;
1841 }
1842 /* FALL THROUGH */
8824e2ce 1843 case s_TRAILING:
f5db0993
WD
1844 if (!*c1)
1845 return 0;
1846 type2 = t_ITEM;
8018edd3
WD
1847 break;
1848 }
f5db0993
WD
1849 if (type1 != type2)
1850 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1851 }
1852 }
1853
1854 return dif;
1855}
1856
8018edd3 1857/* Return a copy of the full filename of a flist entry, using the indicated
5e4ff5f9
WD
1858 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
1859 * done because we checked the size when creating the file_struct entry.
8018edd3 1860 */
5e4ff5f9 1861char *f_name(struct file_struct *f, char *fbuf)
8018edd3 1862{
ebed4c3a
MP
1863 if (!f || !f->basename)
1864 return NULL;
3ec4dd97 1865
5e4ff5f9
WD
1866 if (!fbuf) {
1867 static char names[5][MAXPATHLEN];
1868 static unsigned int n;
1869
1870 n = (n + 1) % (sizeof names / sizeof names[0]);
1871
1872 fbuf = names[n];
1873 }
1874
3ec4dd97 1875 if (f->dirname) {
882e6893
WD
1876 int len = strlen(f->dirname);
1877 memcpy(fbuf, f->dirname, len);
1878 fbuf[len] = '/';
1879 strcpy(fbuf + len + 1, f->basename);
8018edd3 1880 } else
882e6893 1881 strcpy(fbuf, f->basename);
0ee6ca98 1882
b7736c79 1883 return fbuf;
8018edd3 1884}
e03dfae5 1885
32cbfe7b
WD
1886/* Do a non-recursive scan of the named directory, possibly ignoring all
1887 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1888 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1889 * buffer (the functions we call will append names onto the end, but the old
1890 * dir value will be restored on exit). */
1891struct file_list *get_dirlist(char *dirname, int dlen,
1892 int ignore_filter_rules)
37802f40
WD
1893{
1894 struct file_list *dirlist;
1895 char dirbuf[MAXPATHLEN];
37802f40
WD
1896 int save_recurse = recurse;
1897
32cbfe7b
WD
1898 if (dlen < 0) {
1899 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1900 if (dlen >= MAXPATHLEN)
1901 return NULL;
1902 dirname = dirbuf;
1903 }
37802f40
WD
1904
1905 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
32cbfe7b 1906
37802f40 1907 recurse = 0;
32cbfe7b 1908 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
37802f40 1909 recurse = save_recurse;
53135fe8
WD
1910 if (do_progress)
1911 flist_count_offset += dirlist->count;
37802f40 1912
564ef546
WD
1913 clean_flist(dirlist, 0, 0);
1914
45478cc7 1915 if (verbose > 3)
32cbfe7b 1916 output_flist(dirlist);
45478cc7 1917
32cbfe7b 1918 return dirlist;
45478cc7 1919}