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