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