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