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