Sort the dir-list returned by get_dirlist() and the dir-list that
[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;
34e18ecd
WD
33extern int dry_run;
34extern int list_only;
f05f993e 35extern int am_root;
c627d613 36extern int am_server;
a8726d2a 37extern int am_daemon;
56194bcd 38extern int am_sender;
8715db2c 39extern int do_progress;
c627d613 40extern int always_checksum;
983b1ed3
WD
41extern int module_id;
42extern int ignore_errors;
4836c3ee 43extern int numeric_ids;
a06d19e3 44extern int recurse;
7e037c42 45extern int xfer_dirs;
24d0fcde 46extern int filesfrom_fd;
c627d613 47extern int one_file_system;
314f4591 48extern int keep_dirlinks;
c627d613 49extern int preserve_links;
dc5ddbcc 50extern int preserve_hard_links;
c627d613
AT
51extern int preserve_perms;
52extern int preserve_devices;
53extern int preserve_uid;
54extern int preserve_gid;
6574b4f7 55extern int relative_paths;
24d0fcde 56extern int implied_dirs;
82306bf6 57extern int copy_links;
b5313607 58extern int copy_unsafe_links;
d04e9c51 59extern int protocol_version;
cb13abfe 60extern int sanitize_paths;
649f8742 61extern int max_delete;
34e18ecd 62extern int force_delete;
314f4591 63extern int orig_umask;
34e18ecd 64extern int make_backups;
e1f40891 65extern unsigned int curr_dir_len;
74481777 66extern char *log_format;
34e18ecd
WD
67extern char *backup_dir;
68extern char *backup_suffix;
69extern int backup_suffix_len;
c627d613 70
e1f40891
WD
71extern char curr_dir[MAXPATHLEN];
72
7842418b
WD
73extern struct filter_list_struct filter_list;
74extern struct filter_list_struct server_filter_list;
c627d613 75
06c28400
WD
76int io_error;
77
fea4db62 78static char empty_sum[MD4_SUM_LENGTH];
9935066b 79static unsigned int file_struct_len;
bf0c5bec 80static struct file_list *received_flist, *sorting_flist;
45478cc7 81static dev_t filesystem_dev; /* used to implement -x */
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
4f5b0756 149#ifdef 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 181{
4f5b0756 182#ifdef 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 207{
4f5b0756 208#ifdef 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{
4f5b0756 257#ifdef 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
37802f40 334 if (f < 0)
ebed4c3a 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
4f5b0756 389#ifdef 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 466
4f5b0756 467#ifdef 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
4f5b0756 475#ifdef 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
f5db0993
WD
512static struct file_struct *receive_file_entry(struct file_list *flist,
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;
f5db0993 541 return NULL;
5911fee5
WD
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
4f5b0756 615#ifdef 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
f5db0993 634 file = (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
4f5b0756 683#ifdef SUPPORT_LINKS
a289addd
WD
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
4f5b0756 693#ifdef 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 }
f5db0993
WD
736
737 return file;
c627d613
AT
738}
739
740
db719fb0
MP
741/**
742 * Create a file_struct for a named file by reading its stat()
743 * information and performing extensive checks against global
744 * options.
745 *
746 * @return the new file, or NULL if there was an error or this file
747 * should be excluded.
748 *
749 * @todo There is a small optimization opportunity here to avoid
750 * stat()ing the file in some circumstances, which has a certain cost.
751 * We are called immediately after doing readdir(), and so we may
752 * already know the d_type of the file. We could for example avoid
753 * statting directories if we're not recursing, but this is not a very
754 * important case. Some systems may not have d_type.
755 **/
314f4591 756struct file_struct *make_file(char *fname, struct file_list *flist,
7842418b 757 int filter_level)
c627d613 758{
a289addd
WD
759 static char *lastdir;
760 static int lastdir_len = -1;
3ec4dd97 761 struct file_struct *file;
bcacc18b 762 STRUCT_STAT st;
3ec4dd97 763 char sum[SUM_LENGTH];
1923b1fc 764 char thisname[MAXPATHLEN];
e0870f1d 765 char linkname[MAXPATHLEN];
a1d55ad0 766 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd 767 char *basename, *dirname, *bp;
4844449a 768 unsigned short flags = 0;
3ec4dd97 769
d508258a 770 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
7de2483f 771 lastdir_len = -1;
9935066b 772
1923b1fc
WD
773 if (strlcpy(thisname, fname, sizeof thisname)
774 >= sizeof thisname - flist_dir_len) {
71903f60
WD
775 rprintf(FINFO, "skipping overly long name: %s\n",
776 safe_fname(fname));
882e6893
WD
777 return NULL;
778 }
58b1999e 779 clean_fname(thisname, 0);
b7736c79 780 if (sanitize_paths)
33ffd7c3 781 sanitize_path(thisname, thisname, "", 0);
3ec4dd97 782
ebed4c3a 783 memset(sum, 0, SUM_LENGTH);
3ec4dd97 784
e0870f1d 785 if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 786 int save_errno = errno;
a4a7e64c 787 /* See if file is excluded before reporting an error. */
7842418b
WD
788 if (filter_level != NO_FILTERS
789 && is_excluded(thisname, 0, filter_level))
a4a7e64c
WD
790 return NULL;
791 if (save_errno == ENOENT) {
4f5b0756 792#ifdef SUPPORT_LINKS
a4a7e64c
WD
793 /* Avoid "vanished" error if symlink points nowhere. */
794 if (copy_links && do_lstat(thisname, &st) == 0
795 && S_ISLNK(st.st_mode)) {
796 io_error |= IOERR_GENERAL;
797 rprintf(FERROR, "symlink has no referent: %s\n",
798 full_fname(thisname));
e5ce3bcf
WD
799 } else
800#endif
801 {
a4a7e64c
WD
802 enum logcode c = am_daemon && protocol_version < 28
803 ? FERROR : FINFO;
804 io_error |= IOERR_VANISHED;
805 rprintf(c, "file has vanished: %s\n",
806 full_fname(thisname));
76e26e10 807 }
a4a7e64c 808 } else {
a8726d2a 809 io_error |= IOERR_GENERAL;
d62bcc17
WD
810 rsyserr(FERROR, save_errno, "readlink %s failed",
811 full_fname(thisname));
76e26e10 812 }
3ec4dd97
AT
813 return NULL;
814 }
c627d613 815
7842418b
WD
816 /* backup.c calls us with filter_level set to NO_FILTERS. */
817 if (filter_level == NO_FILTERS)
818 goto skip_filters;
ac1a0994 819
7e037c42 820 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
71903f60 821 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
3ec4dd97
AT
822 return NULL;
823 }
ebed4c3a 824
e90cdb8a
WD
825 /* We only care about directories because we need to avoid recursing
826 * into a mount-point directory, not to avoid copying a symlinked
827 * file if -L (or similar) was specified. */
828 if (one_file_system && st.st_dev != filesystem_dev
829 && S_ISDIR(st.st_mode))
4844449a 830 flags |= FLAG_MOUNT_POINT;
ebed4c3a 831
7842418b 832 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
76e26e10
DD
833 return NULL;
834
132fcf36 835 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 836#ifdef SUPPORT_LINKS
132fcf36
WD
837 if (!S_ISLNK(st.st_mode))
838#endif
839 if (access(thisname, R_OK) != 0)
840 return NULL;
841 }
ac1a0994 842
7842418b 843skip_filters:
ac1a0994 844
ea847c62
WD
845 if (verbose > 2) {
846 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
71903f60 847 who_am_i(), safe_fname(thisname), filter_level);
ea847c62 848 }
ebed4c3a 849
a289addd
WD
850 if ((basename = strrchr(thisname, '/')) != NULL) {
851 dirname_len = ++basename - thisname; /* counts future '\0' */
852 if (lastdir_len == dirname_len - 1
853 && strncmp(thisname, lastdir, lastdir_len) == 0) {
854 dirname = lastdir;
855 dirname_len = 0; /* indicates no copy is needed */
856 } else
857 dirname = thisname;
3ec4dd97 858 } else {
a289addd
WD
859 basename = thisname;
860 dirname = NULL;
861 dirname_len = 0;
3ec4dd97 862 }
a289addd 863 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 864
4f5b0756 865#ifdef SUPPORT_LINKS
a289addd
WD
866 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
867#else
868 linkname_len = 0;
869#endif
870
a289addd
WD
871 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
872
61dec11a 873 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
874 + linkname_len + sum_len;
875 if (flist) {
876 bp = pool_alloc(flist->file_pool, alloc_len,
877 "receive_file_entry");
878 } else {
879 if (!(bp = new_array(char, alloc_len)))
880 out_of_memory("receive_file_entry");
881 }
882
a289addd 883 file = (struct file_struct *)bp;
9935066b 884 memset(bp, 0, file_struct_len);
61dec11a 885 bp += file_struct_len;
a289addd
WD
886
887 file->flags = flags;
3ec4dd97
AT
888 file->modtime = st.st_mtime;
889 file->length = st.st_size;
890 file->mode = st.st_mode;
891 file->uid = st.st_uid;
892 file->gid = st.st_gid;
0d162bd1 893
4f5b0756 894#ifdef SUPPORT_HARD_LINKS
9935066b
S
895 if (flist && flist->hlink_pool) {
896 if (protocol_version < 28) {
897 if (S_ISREG(st.st_mode))
898 file->link_u.idev = pool_talloc(
899 flist->hlink_pool, struct idev, 1,
900 "inode_table");
901 } else {
902 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
903 file->link_u.idev = pool_talloc(
904 flist->hlink_pool, struct idev, 1,
905 "inode_table");
906 }
907 }
908 if (file->link_u.idev) {
61dec11a
WD
909 file->F_DEV = st.st_dev;
910 file->F_INODE = st.st_ino;
911 }
912#endif
913
a289addd
WD
914 if (dirname_len) {
915 file->dirname = lastdir = bp;
916 lastdir_len = dirname_len - 1;
917 memcpy(bp, dirname, dirname_len - 1);
918 bp += dirname_len;
919 bp[-1] = '\0';
920 } else if (dirname)
921 file->dirname = dirname;
922
923 file->basename = bp;
924 memcpy(bp, basename, basename_len);
925 bp += basename_len;
926
4f5b0756 927#ifdef HAVE_STRUCT_STAT_ST_RDEV
0d162bd1
WD
928 if (preserve_devices && IS_DEVICE(st.st_mode))
929 file->u.rdev = st.st_rdev;
930#endif
931
4f5b0756 932#ifdef SUPPORT_LINKS
a289addd
WD
933 if (linkname_len) {
934 file->u.link = bp;
935 memcpy(bp, linkname, linkname_len);
936 bp += linkname_len;
937 }
0d162bd1
WD
938#endif
939
a289addd
WD
940 if (sum_len) {
941 file->u.sum = bp;
942 file_checksum(thisname, bp, st.st_size);
943 /*bp += sum_len;*/
ebed4c3a 944 }
c627d613 945
f3c3ed44 946 file->dir.root = flist_dir;
c627d613 947
23f4587f
WD
948 /* This code is only used by the receiver when it is building
949 * a list of files for a delete pass. */
950 if (keep_dirlinks && linkname_len && flist) {
951 STRUCT_STAT st2;
f5db0993
WD
952 int save_mode = file->mode;
953 file->mode = S_IFDIR; /* find a directory w/our name */
954 if (flist_find(received_flist, file) >= 0
23f4587f
WD
955 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
956 file->modtime = st2.st_mtime;
957 file->length = st2.st_size;
958 file->mode = st2.st_mode;
959 file->uid = st2.st_uid;
960 file->gid = st2.st_gid;
961 file->u.link = NULL;
f5db0993
WD
962 } else
963 file->mode = save_mode;
23f4587f
WD
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
37802f40
WD
979 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
980 if (!file)
ebed4c3a
MP
981 return;
982
db719fb0 983 maybe_emit_filelist_progress(flist);
ebed4c3a 984
d9d6bc52 985 flist_expand(flist);
ebed4c3a 986
c120ff37 987 if (file->basename[0]) {
ebed4c3a
MP
988 flist->files[flist->count++] = file;
989 send_file_entry(file, f, base_flags);
990 }
991
4844449a 992 if (recursive && S_ISDIR(file->mode)
f1773e09
WD
993 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
994 void *save_filters;
995 unsigned int len = strlen(fbuf);
996 if (len > 1 && fbuf[len-1] == '/')
997 fbuf[--len] = '\0';
998 if (len >= MAXPATHLEN - 1) {
999 io_error |= IOERR_GENERAL;
1000 rprintf(FERROR, "skipping long-named directory: %s\n",
1001 full_fname(fbuf));
1002 return;
1003 }
1004 save_filters = push_local_filters(fbuf, len);
1005 send_directory(f, flist, fbuf, len);
1006 pop_local_filters(save_filters);
ebed4c3a 1007 }
c627d613
AT
1008}
1009
1010
9f125ea7 1011/* Note that the "recurse" value either contains -1, for infinite recursion,
134f4338
WD
1012 * or a number >= 0 indicating how many levels of recursion we will allow.
1013 * This function is normally called by the sender, but the receiving side
1014 * also calls it from delete_in_dir() with f set to -1 so that we just
37802f40
WD
1015 * construct the file list in memory without sending it over the wire. Also,
1016 * get_dirlist() calls this with f set to -2, which indicates that local
1017 * filter rules should be ignored. */
f1773e09 1018static void send_directory(int f, struct file_list *flist,
19b2a5d9 1019 char *fbuf, unsigned int len)
c627d613 1020{
3ec4dd97 1021 struct dirent *di;
3ec4dd97 1022 char *p;
f1773e09 1023 DIR *d;
3ec4dd97 1024
f1773e09 1025 if (!(d = opendir(fbuf))) {
06c28400 1026 io_error |= IOERR_GENERAL;
f1773e09 1027 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1028 return;
1029 }
c627d613 1030
19b2a5d9
WD
1031 p = fbuf + len;
1032 if (len != 1 || *fbuf != '/')
eddd5d12 1033 *p++ = '/';
f1773e09 1034 *p = '\0';
ebed4c3a 1035
6a7cc46c 1036 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1037 char *dname = d_name(di);
6a7cc46c
S
1038 if (dname[0] == '.' && (dname[1] == '\0'
1039 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1040 continue;
19b2a5d9 1041 if (strlcpy(p, dname, MAXPATHLEN - len) < MAXPATHLEN - len) {
9f125ea7 1042 int do_subdirs = recurse >= 1 ? recurse-- : recurse;
f1773e09 1043 send_file_name(f, flist, fbuf, do_subdirs, 0);
0f57446d 1044 } else {
eddd5d12
WD
1045 io_error |= IOERR_GENERAL;
1046 rprintf(FINFO,
1047 "cannot send long-named file %s\n",
f1773e09 1048 full_fname(fbuf));
eddd5d12 1049 }
3ec4dd97 1050 }
6a7cc46c 1051 if (errno) {
06c28400 1052 io_error |= IOERR_GENERAL;
f1773e09 1053 *p = '\0';
71903f60 1054 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1055 }
c627d613 1056
3ec4dd97 1057 closedir(d);
c627d613
AT
1058}
1059
1060
ebed4c3a 1061struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1062{
24d0fcde 1063 int l;
bcacc18b 1064 STRUCT_STAT st;
808c57c3 1065 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 1066 char lastpath[MAXPATHLEN] = "";
649d65ed 1067 struct file_list *flist;
31b4d25d 1068 struct timeval start_tv, end_tv;
a800434a 1069 int64 start_write;
24d0fcde 1070 int use_ff_fd = 0;
649d65ed 1071
134f4338 1072 if (show_filelist_p())
1bbd10fe 1073 start_filelist_progress("building file list");
c627d613 1074
a800434a 1075 start_write = stats.total_written;
31b4d25d 1076 gettimeofday(&start_tv, NULL);
a800434a 1077
134f4338 1078 flist = flist_new(WITH_HLINK, "send_file_list");
c627d613 1079
134f4338
WD
1080 io_start_buffering_out();
1081 if (filesfrom_fd >= 0) {
1082 if (argv[0] && !push_dir(argv[0])) {
1083 rsyserr(FERROR, errno, "push_dir %s failed",
1084 full_fname(argv[0]));
1085 exit_cleanup(RERR_FILESELECT);
24d0fcde 1086 }
134f4338 1087 use_ff_fd = 1;
d6dead6b
AT
1088 }
1089
24d0fcde 1090 while (1) {
fc638474
DD
1091 char fname2[MAXPATHLEN];
1092 char *fname = fname2;
9f125ea7 1093 int do_subdirs;
c627d613 1094
24d0fcde
WD
1095 if (use_ff_fd) {
1096 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1097 break;
33ffd7c3 1098 sanitize_path(fname, fname, "", 0);
24d0fcde
WD
1099 } else {
1100 if (argc-- == 0)
1101 break;
1102 strlcpy(fname, *argv++, MAXPATHLEN);
1103 if (sanitize_paths)
33ffd7c3 1104 sanitize_path(fname, fname, "", 0);
24d0fcde 1105 }
c627d613 1106
649d65ed 1107 l = strlen(fname);
9f125ea7 1108 if (!l || fname[l - 1] == '/') {
6931c138
WD
1109 if (l == 2 && fname[0] == '.') {
1110 /* Turn "./" into just "." rather than "./." */
1111 fname[1] = '\0';
eddd5d12
WD
1112 } else if (l < MAXPATHLEN) {
1113 fname[l++] = '.';
1114 fname[l] = '\0';
53f821f1 1115 }
649d65ed 1116 }
45478cc7 1117 if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) {
7e037c42 1118 if (!recurse && xfer_dirs)
9f125ea7
WD
1119 recurse = 1; /* allow one level */
1120 } else if (recurse > 0)
1121 recurse = 0;
c627d613 1122
314f4591 1123 if (link_stat(fname, &st, keep_dirlinks) != 0) {
134f4338
WD
1124 io_error |= IOERR_GENERAL;
1125 rsyserr(FERROR, errno, "link_stat %s failed",
1126 full_fname(fname));
649d65ed
AT
1127 continue;
1128 }
c627d613 1129
7e037c42 1130 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
71903f60
WD
1131 rprintf(FINFO, "skipping directory %s\n",
1132 safe_fname(fname));
649d65ed
AT
1133 continue;
1134 }
c627d613 1135
649d65ed 1136 dir = NULL;
808c57c3 1137 olddir[0] = '\0';
649d65ed
AT
1138
1139 if (!relative_paths) {
ebed4c3a 1140 p = strrchr(fname, '/');
649d65ed
AT
1141 if (p) {
1142 *p = 0;
ebed4c3a 1143 if (p == fname)
649d65ed
AT
1144 dir = "/";
1145 else
ebed4c3a
MP
1146 dir = fname;
1147 fname = p + 1;
649d65ed 1148 }
134f4338 1149 } else if (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
649d65ed
AT
1150 /* this ensures we send the intermediate directories,
1151 thus getting their permissions right */
2154309a 1152 char *lp = lastpath, *fn = fname, *slash = fname;
649d65ed 1153 *p = 0;
2154309a
WD
1154 /* Skip any initial directories in our path that we
1155 * have in common with lastpath. */
1156 while (*fn && *lp == *fn) {
1157 if (*fn == '/')
1158 slash = fn;
1159 lp++, fn++;
1160 }
1161 *p = '/';
1162 if (fn != p || (*lp && *lp != '/')) {
0f57446d 1163 int save_copy_links = copy_links;
7e037c42 1164 int save_xfer_dirs = xfer_dirs;
2154309a 1165 copy_links = copy_unsafe_links;
7e037c42 1166 xfer_dirs = 1;
2154309a
WD
1167 while ((slash = strchr(slash+1, '/')) != 0) {
1168 *slash = 0;
1169 send_file_name(f, flist, fname, 0, 0);
1170 *slash = '/';
649d65ed 1171 }
0f57446d 1172 copy_links = save_copy_links;
7e037c42 1173 xfer_dirs = save_xfer_dirs;
2154309a
WD
1174 *p = 0;
1175 strlcpy(lastpath, fname, sizeof lastpath);
649d65ed
AT
1176 *p = '/';
1177 }
1178 }
ebed4c3a 1179
649d65ed
AT
1180 if (!*fname)
1181 fname = ".";
ebed4c3a 1182
649d65ed 1183 if (dir && *dir) {
882e6893
WD
1184 static char *lastdir;
1185 static int lastdir_len;
1186
808c57c3 1187 strcpy(olddir, curr_dir); /* can't overflow */
5243c216 1188
808c57c3 1189 if (!push_dir(dir)) {
06c28400 1190 io_error |= IOERR_GENERAL;
d62bcc17
WD
1191 rsyserr(FERROR, errno, "push_dir %s failed",
1192 full_fname(dir));
649d65ed
AT
1193 continue;
1194 }
5243c216 1195
882e6893
WD
1196 if (lastdir && strcmp(lastdir, dir) == 0) {
1197 flist_dir = lastdir;
1198 flist_dir_len = lastdir_len;
1199 } else {
882e6893
WD
1200 flist_dir = lastdir = strdup(dir);
1201 flist_dir_len = lastdir_len = strlen(dir);
1202 }
2bca43f6 1203 }
ebed4c3a 1204
2bca43f6 1205 if (one_file_system)
45478cc7 1206 filesystem_dev = st.st_dev;
2bca43f6 1207
9f125ea7 1208 do_subdirs = recurse >= 1 ? recurse-- : recurse;
ee3751c8 1209 send_file_name(f, flist, fname, do_subdirs, XMIT_TOP_DIR);
2bca43f6 1210
808c57c3 1211 if (olddir[0]) {
649d65ed 1212 flist_dir = NULL;
882e6893 1213 flist_dir_len = 0;
808c57c3 1214 if (!pop_dir(olddir)) {
d62bcc17
WD
1215 rsyserr(FERROR, errno, "pop_dir %s failed",
1216 full_fname(dir));
65417579 1217 exit_cleanup(RERR_FILESELECT);
649d65ed 1218 }
649d65ed 1219 }
649d65ed 1220 }
dc5ddbcc 1221
134f4338
WD
1222 gettimeofday(&end_tv, NULL);
1223 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1224 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1225 if (stats.flist_buildtime == 0)
1226 stats.flist_buildtime = 1;
1227 start_tv = end_tv;
31b4d25d 1228
134f4338 1229 send_file_entry(NULL, f, 0);
c627d613 1230
134f4338
WD
1231 if (show_filelist_p())
1232 finish_filelist_progress(flist);
31b4d25d 1233
134f4338
WD
1234 gettimeofday(&end_tv, NULL);
1235 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1236 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 1237
7cf8e8d0 1238 if (flist->hlink_pool) {
9935066b
S
1239 pool_destroy(flist->hlink_pool);
1240 flist->hlink_pool = NULL;
1241 }
1242
f5db0993
WD
1243 /* Sort the list without removing any duplicates. This allows the
1244 * receiving side to ask for any name they like, which gives us the
1245 * flexibility to change the way we unduplicate names in the future
1246 * without causing a compatibility problem with older versions. */
827c37f6 1247 clean_flist(flist, 0, 0);
ebed4c3a 1248
134f4338
WD
1249 /* Now send the uid/gid list. This was introduced in
1250 * protocol version 15 */
1251 send_uid_list(f);
f6c34742 1252
134f4338
WD
1253 /* send the io_error flag */
1254 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1255
134f4338
WD
1256 io_end_buffering();
1257 stats.flist_size = stats.total_written - start_write;
1258 stats.num_files = flist->count;
d6dead6b 1259
cefed3e8 1260 if (verbose > 3)
134f4338 1261 output_flist(flist, who_am_i());
cefed3e8 1262
17faa41c 1263 if (verbose > 2)
ebed4c3a 1264 rprintf(FINFO, "send_file_list done\n");
17faa41c 1265
649d65ed 1266 return flist;
c627d613
AT
1267}
1268
1269
1270struct file_list *recv_file_list(int f)
1271{
ebed4c3a 1272 struct file_list *flist;
1ef00d20 1273 unsigned short flags;
ebed4c3a 1274 int64 start_read;
c627d613 1275
1bbd10fe
DD
1276 if (show_filelist_p())
1277 start_filelist_progress("receiving file list");
c627d613 1278
ebed4c3a 1279 start_read = stats.total_read;
a800434a 1280
9935066b 1281 flist = flist_new(WITH_HLINK, "recv_file_list");
23f4587f 1282 received_flist = flist;
c627d613 1283
ebed4c3a
MP
1284 flist->count = 0;
1285 flist->malloced = 1000;
58cadc86 1286 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1287 if (!flist->files)
1288 goto oom;
c627d613
AT
1289
1290
1ef00d20 1291 while ((flags = read_byte(f)) != 0) {
f5db0993 1292 struct file_struct *file;
dbda5fbf 1293
d9d6bc52 1294 flist_expand(flist);
c627d613 1295
d01d15e0 1296 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1297 flags |= read_byte(f) << 8;
f5db0993 1298 file = receive_file_entry(flist, flags, f);
c627d613 1299
f5db0993
WD
1300 if (S_ISREG(file->mode))
1301 stats.total_size += file->length;
c627d613 1302
f5db0993 1303 flist->files[flist->count++] = file;
c627d613 1304
db719fb0 1305 maybe_emit_filelist_progress(flist);
1bbd10fe 1306
8018edd3 1307 if (verbose > 2) {
ebed4c3a 1308 rprintf(FINFO, "recv_file_name(%s)\n",
f5db0993 1309 safe_fname(f_name(file)));
8018edd3 1310 }
ebed4c3a 1311 }
f5db0993 1312 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
c627d613 1313
ebed4c3a
MP
1314 if (verbose > 2)
1315 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1316
b7736c79 1317 if (show_filelist_p())
1bbd10fe 1318 finish_filelist_progress(flist);
a06d19e3 1319
983b1ed3
WD
1320 clean_flist(flist, relative_paths, 1);
1321
37802f40 1322 if (f >= 0) {
785db4ce
WD
1323 /* Now send the uid/gid list. This was introduced in
1324 * protocol version 15 */
ebed4c3a 1325 recv_uid_list(f, flist);
f6c34742 1326
b9f592fb
WD
1327 /* Recv the io_error flag */
1328 if (lp_ignore_errors(module_id) || ignore_errors)
1329 read_int(f);
1330 else
1331 io_error |= read_int(f);
ebed4c3a 1332 }
6ba9279f 1333
cefed3e8 1334 if (verbose > 3)
48ea74bf 1335 output_flist(flist, who_am_i());
cefed3e8 1336
ebed4c3a
MP
1337 if (list_only) {
1338 int i;
b7736c79 1339 for (i = 0; i < flist->count; i++)
ebed4c3a 1340 list_file_entry(flist->files[i]);
ebed4c3a 1341 }
f7632fc6 1342
ebed4c3a
MP
1343 if (verbose > 2)
1344 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1345
ebed4c3a
MP
1346 stats.flist_size = stats.total_read - start_read;
1347 stats.num_files = flist->count;
a800434a 1348
ebed4c3a 1349 return flist;
c627d613 1350
3e491682 1351oom:
ebed4c3a
MP
1352 out_of_memory("recv_file_list");
1353 return NULL; /* not reached */
c627d613
AT
1354}
1355
1356
14698a3a 1357static int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1358{
14698a3a 1359 return f_name_cmp(*file1, *file2);
c627d613
AT
1360}
1361
1362
f5db0993
WD
1363/* Search for an identically-named item in the file list. Note that the
1364 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 1365int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1366{
f3c3ed44
WD
1367 int low = flist->low, high = flist->high;
1368 int ret, mid, mid_up;
1369
1370 while (low <= high) {
1371 mid = (low + high) / 2;
1372 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1373 if (mid_up <= high)
14698a3a 1374 ret = f_name_cmp(flist->files[mid_up], f);
f3c3ed44
WD
1375 else
1376 ret = 1;
f5db0993
WD
1377 if (ret == 0) {
1378 if (protocol_version < 29
1379 && S_ISDIR(flist->files[mid_up]->mode)
1380 != S_ISDIR(f->mode))
1381 return -1;
f3c3ed44 1382 return mid_up;
f5db0993 1383 }
b7736c79 1384 if (ret > 0)
f3c3ed44 1385 high = mid - 1;
b7736c79 1386 else
f3c3ed44 1387 low = mid_up + 1;
d966ee25 1388 }
d966ee25 1389 return -1;
c627d613
AT
1390}
1391
f5db0993 1392
3ec4dd97 1393/*
9935066b
S
1394 * Free up any resources a file_struct has allocated
1395 * and clear the file.
3ec4dd97 1396 */
9935066b 1397void clear_file(int i, struct file_list *flist)
c627d613 1398{
9935066b
S
1399 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1400 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1401 memset(flist->files[i], 0, file_struct_len);
3ec4dd97 1402}
c627d613 1403
c627d613 1404
3d382777
AT
1405/*
1406 * allocate a new file list
1407 */
9935066b 1408struct file_list *flist_new(int with_hlink, char *msg)
3d382777
AT
1409{
1410 struct file_list *flist;
1411
58cadc86 1412 flist = new(struct file_list);
ebed4c3a 1413 if (!flist)
9935066b 1414 out_of_memory(msg);
3d382777 1415
9935066b
S
1416 memset(flist, 0, sizeof (struct file_list));
1417
1418 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1419 out_of_memory, POOL_INTERN)))
1420 out_of_memory(msg);
1421
4f5b0756 1422#ifdef SUPPORT_HARD_LINKS
9935066b 1423 if (with_hlink && preserve_hard_links) {
3e491682 1424 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
9935066b
S
1425 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1426 out_of_memory(msg);
1427 }
1428#endif
d9d6bc52 1429
3d382777
AT
1430 return flist;
1431}
ebed4c3a 1432
3ec4dd97
AT
1433/*
1434 * free up all elements in a flist
1435 */
1436void flist_free(struct file_list *flist)
1437{
9935066b
S
1438 pool_destroy(flist->file_pool);
1439 pool_destroy(flist->hlink_pool);
3ec4dd97 1440 free(flist->files);
3ec4dd97 1441 free(flist);
c627d613
AT
1442}
1443
1444
1445/*
1446 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1447 * duplicate names can cause corruption because of the pipelining
c627d613 1448 */
827c37f6 1449static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1450{
6931c138 1451 int i, prev_i = 0;
c627d613 1452
ebed4c3a 1453 if (!flist || flist->count == 0)
3ec4dd97 1454 return;
3ec4dd97 1455
bf0c5bec 1456 sorting_flist = flist;
ebed4c3a 1457 qsort(flist->files, flist->count,
a4a7e64c 1458 sizeof flist->files[0], (int (*)())file_compare);
bf0c5bec 1459 sorting_flist = NULL;
ebed4c3a 1460
827c37f6 1461 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1462 if (flist->files[i]->basename) {
6931c138 1463 prev_i = i;
b91b50c0
WD
1464 break;
1465 }
1466 }
f3c3ed44 1467 flist->low = prev_i;
b91b50c0 1468 while (++i < flist->count) {
fe1c19dc 1469 int j;
f5db0993
WD
1470 struct file_struct *file = flist->files[i];
1471
1472 if (!file->basename)
b91b50c0 1473 continue;
fe1c19dc
WD
1474 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1475 j = prev_i;
1476 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
1477 int save_mode = file->mode;
1478 /* Make sure that this directory doesn't duplicate a
1479 * non-directory earlier in the list. */
f5db0993 1480 flist->high = prev_i;
fe1c19dc
WD
1481 file->mode = S_IFREG;
1482 j = flist_find(flist, file);
f5db0993 1483 file->mode = save_mode;
fe1c19dc
WD
1484 } else
1485 j = -1;
1486 if (j >= 0) {
1487 struct file_struct *fp = flist->files[j];
1488 int keep, drop;
1489 /* If one is a dir and the other is not, we want to
1490 * keep the dir because it might have contents in the
1491 * list. */
1492 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1493 if (S_ISDIR(file->mode))
1494 keep = i, drop = j;
1495 else
1496 keep = j, drop = i;
1497 } else
1498 keep = j, drop = i;
b91b50c0 1499 if (verbose > 1 && !am_server) {
ebed4c3a 1500 rprintf(FINFO,
fe1c19dc
WD
1501 "removing duplicate name %s from file list (%d)\n",
1502 safe_fname(f_name(file)), drop);
b91b50c0 1503 }
6931c138 1504 /* Make sure that if we unduplicate '.', that we don't
ee3751c8 1505 * lose track of a user-specified top directory. */
fe1c19dc
WD
1506 if (flist->files[drop]->flags & FLAG_TOP_DIR)
1507 flist->files[keep]->flags |= FLAG_TOP_DIR;
1508
1509 clear_file(drop, flist);
9935066b 1510
fe1c19dc
WD
1511 if (keep == i) {
1512 if (flist->low == drop) {
1513 for (j = drop + 1;
1514 j < i && !flist->files[j]->basename;
1515 j++) {}
1516 flist->low = j;
1517 }
1518 prev_i = i;
1519 }
728d0922 1520 } else
6931c138 1521 prev_i = i;
3ec4dd97 1522 }
f5db0993 1523 flist->high = no_dups ? prev_i : flist->count - 1;
0199b05f
AT
1524
1525 if (strip_root) {
f5db0993
WD
1526 /* We need to strip off the leading slashes for relative
1527 * paths, but this must be done _after_ the sorting phase. */
1528 for (i = flist->low; i <= flist->high; i++) {
1529 struct file_struct *file = flist->files[i];
ebed4c3a 1530
f5db0993
WD
1531 if (!file->dirname)
1532 continue;
1533 if (*file->dirname == '/') {
1534 char *s = file->dirname + 1;
1535 while (*s == '/') s++;
1536 memmove(file->dirname, s, strlen(s) + 1);
0199b05f 1537 }
f5db0993
WD
1538
1539 if (!*file->dirname)
1540 file->dirname = NULL;
0199b05f
AT
1541 }
1542 }
cefed3e8 1543}
0199b05f 1544
48ea74bf 1545static void output_flist(struct file_list *flist, const char *whose_list)
cefed3e8 1546{
f3c3ed44 1547 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8
WD
1548 struct file_struct *file;
1549 int i;
0199b05f 1550
ebed4c3a 1551 for (i = 0; i < flist->count; i++) {
cefed3e8 1552 file = flist->files[i];
56194bcd 1553 if ((am_root || am_sender) && preserve_uid)
f05f993e
WD
1554 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1555 else
1556 *uidbuf = '\0';
cefed3e8 1557 if (preserve_gid && file->gid != GID_NONE)
f05f993e
WD
1558 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1559 else
1560 *gidbuf = '\0';
f3c3ed44
WD
1561 if (!am_sender)
1562 sprintf(depthbuf, "%d", file->dir.depth);
14698a3a 1563 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
f3c3ed44 1564 whose_list, i, am_sender ? NS(file->dir.root) : depthbuf,
71903f60 1565 file->dirname ? safe_fname(file->dirname) : "",
14698a3a
WD
1566 file->dirname ? "/" : "", NS(file->basename),
1567 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
f3c3ed44 1568 (double)file->length, uidbuf, gidbuf, file->flags);
0199b05f 1569 }
3ec4dd97
AT
1570}
1571
1572
8824e2ce 1573enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 1574enum fnc_type { t_PATH, t_ITEM };
8018edd3 1575
2f3cad89 1576/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
1577 * would do if it were operating on the joined strings.
1578 *
1579 * Some differences beginning with protocol_version 29: (1) directory names
1580 * are compared with an assumed trailing slash so that they compare in a
1581 * way that would cause them to sort immediately prior to any content they
1582 * may have; (2) a directory of any name compares after a non-directory of
1583 * any name at the same depth; (3) a directory with name "." compares prior
1584 * to anything else. These changes mean that a directory and a non-dir
1585 * with the same name will not compare as equal (protocol_version >= 29).
1586 *
1587 * The dirname component can be an empty string, but the basename component
1588 * cannot (and never is in the current codebase). The basename component
1589 * may be NULL (for a removed item), in which case it is considered to be
1590 * after any existing item. */
8018edd3 1591int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1592{
8018edd3
WD
1593 int dif;
1594 const uchar *c1, *c2;
1ef00d20 1595 enum fnc_state state1, state2;
f5db0993
WD
1596 enum fnc_type type1, type2;
1597 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3
WD
1598
1599 if (!f1 || !f1->basename) {
1600 if (!f2 || !f2->basename)
1601 return 0;
1602 return -1;
1603 }
1604 if (!f2 || !f2->basename)
1605 return 1;
1606
14698a3a
WD
1607 c1 = (uchar*)f1->dirname;
1608 c2 = (uchar*)f2->dirname;
1609 if (c1 == c2)
1610 c1 = c2 = NULL;
1611 if (!c1) {
f5db0993 1612 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1613 c1 = (uchar*)f1->basename;
f5db0993
WD
1614 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1615 type1 = t_ITEM;
1616 state1 = s_TRAILING;
1617 c1 = (uchar*)"";
1618 } else
1619 state1 = s_BASE;
080ddf58 1620 } else if (!*c1) {
f5db0993 1621 type1 = t_path;
8824e2ce 1622 state1 = s_SLASH;
080ddf58 1623 c1 = (uchar*)"/";
f5db0993
WD
1624 } else {
1625 type1 = t_path;
8824e2ce 1626 state1 = s_DIR;
f5db0993 1627 }
14698a3a 1628 if (!c2) {
f5db0993 1629 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1630 c2 = (uchar*)f2->basename;
f5db0993
WD
1631 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1632 type2 = t_ITEM;
1633 state2 = s_TRAILING;
1634 c2 = (uchar*)"";
1635 } else
1636 state2 = s_BASE;
080ddf58 1637 } else if (!*c2) {
f5db0993 1638 type2 = t_path;
8824e2ce 1639 state2 = s_SLASH;
080ddf58 1640 c2 = (uchar*)"/";
f5db0993
WD
1641 } else {
1642 type2 = t_path;
8824e2ce 1643 state2 = s_DIR;
f5db0993
WD
1644 }
1645
1646 if (type1 != type2)
1647 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1648
1649 while (1) {
f5db0993 1650 if ((dif = (int)*c1++ - (int)*c2++) != 0)
8018edd3 1651 break;
f5db0993 1652 if (!*c1) {
8018edd3 1653 switch (state1) {
8824e2ce
WD
1654 case s_DIR:
1655 state1 = s_SLASH;
e90b8ace 1656 c1 = (uchar*)"/";
8018edd3 1657 break;
8824e2ce 1658 case s_SLASH:
f5db0993 1659 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
8824e2ce 1660 state1 = s_BASE;
e90b8ace 1661 c1 = (uchar*)f1->basename;
8018edd3 1662 break;
8824e2ce
WD
1663 case s_BASE:
1664 state1 = s_TRAILING;
f5db0993 1665 if (type1 == t_PATH) {
2f3cad89 1666 c1 = (uchar*)"/";
f5db0993
WD
1667 break;
1668 }
1669 /* FALL THROUGH */
8824e2ce 1670 case s_TRAILING:
f5db0993 1671 type1 = t_ITEM;
8018edd3
WD
1672 break;
1673 }
f5db0993
WD
1674 if (*c2 && type1 != type2)
1675 return type1 == t_PATH ? 1 : -1;
8018edd3 1676 }
f5db0993 1677 if (!*c2) {
8018edd3 1678 switch (state2) {
8824e2ce 1679 case s_DIR:
bf0c5bec
WD
1680 if (state1 == s_SLASH && sorting_flist) {
1681 int j;
1682 /* Optimize for future comparisons. */
1683 for (j = 0;
1684 j < sorting_flist->count;
1685 j++) {
1686 struct file_struct *fp
1687 = sorting_flist->files[j];
1688 if (fp->dirname == f2->dirname)
1689 fp->dirname = f1->dirname;
1690 }
1691 }
8824e2ce 1692 state2 = s_SLASH;
e90b8ace 1693 c2 = (uchar*)"/";
8018edd3 1694 break;
8824e2ce 1695 case s_SLASH:
f5db0993 1696 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
8824e2ce 1697 state2 = s_BASE;
e90b8ace 1698 c2 = (uchar*)f2->basename;
8018edd3 1699 break;
8824e2ce 1700 case s_BASE:
8824e2ce 1701 state2 = s_TRAILING;
f5db0993 1702 if (type2 == t_PATH) {
2f3cad89 1703 c2 = (uchar*)"/";
f5db0993
WD
1704 break;
1705 }
1706 /* FALL THROUGH */
8824e2ce 1707 case s_TRAILING:
f5db0993
WD
1708 if (!*c1)
1709 return 0;
1710 type2 = t_ITEM;
8018edd3
WD
1711 break;
1712 }
f5db0993
WD
1713 if (type1 != type2)
1714 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1715 }
1716 }
1717
1718 return dif;
1719}
1720
3ec4dd97 1721
8018edd3 1722/* Return a copy of the full filename of a flist entry, using the indicated
882e6893
WD
1723 * buffer. No size-checking is done because we checked the size when creating
1724 * the file_struct entry.
8018edd3 1725 */
882e6893 1726char *f_name_to(struct file_struct *f, char *fbuf)
8018edd3 1727{
ebed4c3a
MP
1728 if (!f || !f->basename)
1729 return NULL;
3ec4dd97 1730
3ec4dd97 1731 if (f->dirname) {
882e6893
WD
1732 int len = strlen(f->dirname);
1733 memcpy(fbuf, f->dirname, len);
1734 fbuf[len] = '/';
1735 strcpy(fbuf + len + 1, f->basename);
8018edd3 1736 } else
882e6893 1737 strcpy(fbuf, f->basename);
b7736c79 1738 return fbuf;
8018edd3 1739}
e03dfae5 1740
3ec4dd97 1741
45478cc7 1742/* Like f_name_to(), but we rotate through 5 static buffers of our own. */
8018edd3
WD
1743char *f_name(struct file_struct *f)
1744{
1745 static char names[5][MAXPATHLEN];
1746 static unsigned int n;
1747
1748 n = (n + 1) % (sizeof names / sizeof names[0]);
1749
882e6893 1750 return f_name_to(f, names[n]);
c627d613 1751}
649f8742 1752
45478cc7 1753
37802f40
WD
1754struct file_list *get_dirlist(const char *dirname, int ignore_filter_rules)
1755{
1756 struct file_list *dirlist;
1757 char dirbuf[MAXPATHLEN];
1758 int dlen;
1759 int save_recurse = recurse;
1760
1761 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1762 if (dlen >= MAXPATHLEN)
1763 return NULL;
1764
1765 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
1766 recurse = 0;
1767 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirbuf, dlen);
1768 recurse = save_recurse;
1769
564ef546
WD
1770 clean_flist(dirlist, 0, 0);
1771
37802f40
WD
1772 return dirlist;
1773}
1774
45478cc7 1775
34e18ecd
WD
1776static int deletion_count = 0; /* used to implement --max-delete */
1777
1778static int is_backup_file(char *fn)
1779{
1780 int k = strlen(fn) - backup_suffix_len;
1781 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
1782}
1783
1784
1785/* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
1786 * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
1787 * is not set in the flags. */
1788int delete_file(char *fname, int mode, int flags)
1789{
1790 struct file_list *dirlist;
1791 char buf[MAXPATHLEN];
1792 int j, zap_dir, ok;
1793
1794 if (max_delete && deletion_count >= max_delete)
1795 return -1;
1796
1797 if (!S_ISDIR(mode)) {
1798 if (make_backups && (backup_dir || !is_backup_file(fname)))
1799 ok = make_backup(fname);
1800 else
1801 ok = robust_unlink(fname) == 0;
1802 if (ok) {
1803 if ((verbose || log_format) && !(flags & DEL_TERSE))
1804 log_delete(fname, mode);
1805 deletion_count++;
1806 return 0;
1807 }
1808 if (errno == ENOENT)
1809 return 0;
1810 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
1811 full_fname(fname));
1812 return -1;
1813 }
1814
1815 zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
1816 && !(flags & DEL_NO_RECURSE);
1bad11c7
WD
1817 if (dry_run && zap_dir) {
1818 ok = 0;
34e18ecd 1819 errno = ENOTEMPTY;
1bad11c7 1820 } else if (make_backups && !backup_dir && !is_backup_file(fname)
94dcbf83 1821 && !(flags & DEL_FORCE_RECURSE))
1bad11c7
WD
1822 ok = make_backup(fname);
1823 else
1824 ok = do_rmdir(fname) == 0;
1825 if (ok) {
34e18ecd
WD
1826 if ((verbose || log_format) && !(flags & DEL_TERSE))
1827 log_delete(fname, mode);
1828 deletion_count++;
1829 return 0;
1830 }
1831 if (errno == ENOENT)
1832 return 0;
1833 if (!zap_dir || (errno != ENOTEMPTY && errno != EEXIST)) {
1834 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
1835 full_fname(fname));
1836 return -1;
1837 }
94dcbf83 1838 flags |= DEL_FORCE_RECURSE;
34e18ecd 1839
34e18ecd 1840 dirlist = get_dirlist(fname, 0);
564ef546 1841 for (j = dirlist->count; j--; ) {
34e18ecd
WD
1842 struct file_struct *fp = dirlist->files[j];
1843 f_name_to(fp, buf);
e1ad7fe6 1844 if (delete_file(buf, fp->mode, flags & ~DEL_TERSE) != 0) {
34e18ecd
WD
1845 flist_free(dirlist);
1846 return -1;
1847 }
1848 }
1849 flist_free(dirlist);
1850
1851 if (max_delete && deletion_count >= max_delete)
1852 return -1;
1853
1bad11c7 1854 if (do_rmdir(fname) == 0) {
e1ad7fe6
WD
1855 if ((verbose || log_format) && !(flags & DEL_TERSE))
1856 log_delete(fname, mode);
1857 deletion_count++;
1858 } else if (errno != ENOTEMPTY && errno != ENOENT) {
34e18ecd
WD
1859 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
1860 full_fname(fname));
1861 return -1;
1862 }
1863
34e18ecd
WD
1864 return 0;
1865}
1866
1867
1868/* If an item in dir_list is not found in full_list, delete it from the
1869 * filesystem. */
1870static void delete_missing(struct file_list *full_list,
1871 struct file_list *dir_list, const char *dirname)
1872{
1873 char fbuf[MAXPATHLEN];
1874 int i;
1875
1876 if (max_delete && deletion_count >= max_delete)
1877 return;
1878
1879 if (verbose > 2)
1880 rprintf(FINFO, "delete_missing(%s)\n", safe_fname(dirname));
1881
1882 for (i = dir_list->count; i--; ) {
1883 if (!dir_list->files[i]->basename)
1884 continue;
1885 if (flist_find(full_list, dir_list->files[i]) < 0) {
1886 char *fn = f_name_to(dir_list->files[i], fbuf);
1887 int mode = dir_list->files[i]->mode;
94dcbf83 1888 if (delete_file(fn, mode, DEL_FORCE_RECURSE) < 0)
34e18ecd
WD
1889 break;
1890 }
1891 }
1892}
1893
1894
ee3751c8
WD
1895/* This function is used to implement per-directory deletion, and
1896 * is used by all the --delete-WHEN options. Note that the fbuf
1897 * pointer must point to a MAXPATHLEN buffer with the name of the
1898 * directory in it (the functions we call will append names onto
1899 * the end, but the old dir value will be restored on exit). */
1900void delete_in_dir(struct file_list *flist, char *fbuf,
1901 struct file_struct *file)
649f8742 1902{
14698a3a
WD
1903 static int min_depth = MAXPATHLEN, cur_depth = -1;
1904 static void *filt_array[MAXPATHLEN/2+1];
45478cc7
WD
1905 struct file_list *dir_list;
1906 STRUCT_STAT st;
ee3751c8 1907 int dlen;
19b2a5d9
WD
1908
1909 if (!flist) {
14698a3a
WD
1910 while (cur_depth >= min_depth)
1911 pop_local_filters(filt_array[cur_depth--]);
1912 min_depth = MAXPATHLEN;
1913 cur_depth = -1;
19b2a5d9
WD
1914 return;
1915 }
ee3751c8 1916 if (file->dir.depth >= MAXPATHLEN/2+1)
14698a3a 1917 return; /* Impossible... */
649f8742
WD
1918
1919 if (max_delete && deletion_count >= max_delete)
1920 return;
1921
1922 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
f1773e09 1923 rprintf(FINFO,
4ccfd96c 1924 "IO error encountered -- skipping file deletion\n");
649f8742
WD
1925 max_delete = -1; /* avoid duplicating the above warning */
1926 return;
1927 }
1928
ee3751c8 1929 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
14698a3a 1930 pop_local_filters(filt_array[cur_depth--]);
ee3751c8 1931 cur_depth = file->dir.depth;
14698a3a
WD
1932 if (min_depth > cur_depth)
1933 min_depth = cur_depth;
ee3751c8 1934 dlen = strlen(fbuf);
14698a3a 1935 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
f1773e09 1936
14698a3a 1937 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
776b9d1c
WD
1938 return;
1939
ee3751c8 1940 if (one_file_system && file->flags & FLAG_TOP_DIR)
776b9d1c
WD
1941 filesystem_dev = st.st_dev;
1942
45478cc7 1943 dir_list = flist_new(WITHOUT_HLINK, "delete_in_dir");
f1773e09
WD
1944
1945 recurse = 0;
19b2a5d9 1946 send_directory(-1, dir_list, fbuf, dlen);
f1773e09 1947 recurse = -1;
14698a3a 1948 fbuf[dlen] = '\0';
45478cc7 1949
564ef546
WD
1950 clean_flist(dir_list, 0, 0);
1951
45478cc7
WD
1952 if (verbose > 3)
1953 output_flist(dir_list, "delete");
1954
14698a3a 1955 delete_missing(flist, dir_list, fbuf);
45478cc7
WD
1956
1957 flist_free(dir_list);
1958}