Allow deletions to occur in directories transferred with the
[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
9a4a237e 483 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
728d0922 484 char *sum;
9a4a237e 485 int slen = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
728d0922
WD
486 if (S_ISREG(mode))
487 sum = file->u.sum;
9a4a237e 488 else {
728d0922
WD
489 /* Prior to 28, we sent a useless set of nulls. */
490 sum = empty_sum;
728d0922 491 }
9a4a237e 492 write_buf(f, sum, slen);
ebed4c3a 493 }
182dca5c 494
ebed4c3a 495 strlcpy(lastname, fname, MAXPATHLEN);
eca2adb4
MP
496
497 io_write_phase = "unknown";
182dca5c
AT
498}
499
500
501
f5db0993
WD
502static struct file_struct *receive_file_entry(struct file_list *flist,
503 unsigned short flags, int f)
182dca5c 504{
5911fee5
WD
505 static time_t modtime;
506 static mode_t mode;
1490812a 507 static int64 dev;
4124540d 508 static dev_t rdev;
9c5e91f8 509 static uint32 rdev_major;
5911fee5
WD
510 static uid_t uid;
511 static gid_t gid;
a289addd 512 static char lastname[MAXPATHLEN], *lastdir;
33ffd7c3 513 static int lastdir_depth, lastdir_len = -1;
42f23f47 514 static unsigned int del_hier_name_len = 0;
649f8742 515 static int in_del_hier = 0;
72914a60 516 char thisname[MAXPATHLEN];
ebed4c3a 517 unsigned int l1 = 0, l2 = 0;
a1d55ad0 518 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd
WD
519 OFF_T file_length;
520 char *basename, *dirname, *bp;
72914a60
AT
521 struct file_struct *file;
522
f3c3ed44 523 if (!flist) {
5911fee5 524 modtime = 0, mode = 0;
4124540d 525 dev = 0, rdev = makedev(0, 0);
9c5e91f8 526 rdev_major = 0;
5911fee5
WD
527 uid = 0, gid = 0;
528 *lastname = '\0';
3db859e8 529 lastdir_len = -1;
649f8742 530 in_del_hier = 0;
f5db0993 531 return NULL;
5911fee5
WD
532 }
533
d01d15e0 534 if (flags & XMIT_SAME_NAME)
72914a60 535 l1 = read_byte(f);
ebed4c3a 536
d01d15e0 537 if (flags & XMIT_LONG_NAME)
72914a60
AT
538 l2 = read_int(f);
539 else
540 l2 = read_byte(f);
541
ebed4c3a
MP
542 if (l2 >= MAXPATHLEN - l1) {
543 rprintf(FERROR,
544 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
71903f60 545 flags, l1, l2, safe_fname(lastname));
a1f99493 546 overflow_exit("receive_file_entry");
d0fd26aa 547 }
72914a60 548
ebed4c3a
MP
549 strlcpy(thisname, lastname, l1 + 1);
550 read_sbuf(f, &thisname[l1], l2);
551 thisname[l1 + l2] = 0;
72914a60 552
ebed4c3a 553 strlcpy(lastname, thisname, MAXPATHLEN);
72914a60 554
58b1999e 555 clean_fname(thisname, 0);
72914a60 556
0d162bd1 557 if (sanitize_paths)
33ffd7c3 558 sanitize_path(thisname, thisname, "", 0);
cb13abfe 559
a289addd
WD
560 if ((basename = strrchr(thisname, '/')) != NULL) {
561 dirname_len = ++basename - thisname; /* counts future '\0' */
562 if (lastdir_len == dirname_len - 1
563 && strncmp(thisname, lastdir, lastdir_len) == 0) {
564 dirname = lastdir;
565 dirname_len = 0; /* indicates no copy is needed */
566 } else
567 dirname = thisname;
72914a60 568 } else {
a289addd
WD
569 basename = thisname;
570 dirname = NULL;
571 dirname_len = 0;
72914a60 572 }
a289addd 573 basename_len = strlen(basename) + 1; /* count the '\0' */
72914a60 574
a289addd 575 file_length = read_longint(f);
d01d15e0 576 if (!(flags & XMIT_SAME_TIME))
30f337c9 577 modtime = (time_t)read_int(f);
d01d15e0 578 if (!(flags & XMIT_SAME_MODE))
30f337c9 579 mode = from_wire_mode(read_int(f));
1ef00d20 580
a289addd
WD
581 if (preserve_uid && !(flags & XMIT_SAME_UID))
582 uid = (uid_t)read_int(f);
583 if (preserve_gid && !(flags & XMIT_SAME_GID))
584 gid = (gid_t)read_int(f);
585
1ef00d20 586 if (preserve_devices) {
75bc8600 587 if (protocol_version < 28) {
30f337c9 588 if (IS_DEVICE(mode)) {
d01d15e0 589 if (!(flags & XMIT_SAME_RDEV_pre28))
9c5e91f8 590 rdev = (dev_t)read_int(f);
75bc8600 591 } else
9c5e91f8 592 rdev = makedev(0, 0);
30f337c9 593 } else if (IS_DEVICE(mode)) {
9c5e91f8
WD
594 uint32 rdev_minor;
595 if (!(flags & XMIT_SAME_RDEV_MAJOR))
596 rdev_major = read_int(f);
597 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
598 rdev_minor = read_byte(f);
599 else
600 rdev_minor = read_int(f);
601 rdev = makedev(rdev_major, rdev_minor);
1ef00d20 602 }
b7736c79 603 }
72914a60 604
4f5b0756 605#ifdef SUPPORT_LINKS
30f337c9 606 if (preserve_links && S_ISLNK(mode)) {
a289addd
WD
607 linkname_len = read_int(f) + 1; /* count the '\0' */
608 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
609 rprintf(FERROR, "overflow: linkname_len=%d\n",
610 linkname_len - 1);
a1f99493 611 overflow_exit("receive_file_entry");
9dd891bb 612 }
72914a60 613 }
a289addd 614 else
0d162bd1 615#endif
a289addd 616 linkname_len = 0;
0d162bd1 617
a289addd
WD
618 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
619
61dec11a 620 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
621 + linkname_len + sum_len;
622 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
623
f5db0993 624 file = (struct file_struct *)bp;
9935066b 625 memset(bp, 0, file_struct_len);
61dec11a 626 bp += file_struct_len;
a289addd 627
649f8742 628 file->flags = 0;
a289addd
WD
629 file->modtime = modtime;
630 file->length = file_length;
631 file->mode = mode;
632 file->uid = uid;
633 file->gid = gid;
634
f3c3ed44
WD
635 if (dirname_len) {
636 file->dirname = lastdir = bp;
637 lastdir_len = dirname_len - 1;
638 memcpy(bp, dirname, dirname_len - 1);
639 bp += dirname_len;
640 bp[-1] = '\0';
641 lastdir_depth = count_dir_elements(lastdir);
642 file->dir.depth = lastdir_depth + 1;
643 } else if (dirname) {
644 file->dirname = dirname; /* we're reusing lastname */
645 file->dir.depth = lastdir_depth + 1;
646 } else
647 file->dir.depth = 1;
648
649f8742 649 if (S_ISDIR(mode)) {
ee3751c8 650 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
f3c3ed44 651 file->dir.depth--;
ee3751c8 652 if (flags & XMIT_TOP_DIR) {
649f8742 653 in_del_hier = 1;
f3c3ed44 654 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
7a16e122
WD
655 if (relative_paths && del_hier_name_len > 2
656 && basename_len == 1+1 && *basename == '.')
657 del_hier_name_len -= 2;
ee3751c8
WD
658 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
659 } else if (in_del_hier) {
f3c3ed44
WD
660 if (!relative_paths || !del_hier_name_len
661 || (l1 >= del_hier_name_len
662 && thisname[del_hier_name_len] == '/'))
ee3751c8 663 file->flags |= FLAG_DEL_HERE;
649f8742
WD
664 else
665 in_del_hier = 0;
666 }
667 }
668
a289addd
WD
669 file->basename = bp;
670 memcpy(bp, basename, basename_len);
671 bp += basename_len;
672
673 if (preserve_devices && IS_DEVICE(mode))
674 file->u.rdev = rdev;
675
4f5b0756 676#ifdef SUPPORT_LINKS
a289addd
WD
677 if (linkname_len) {
678 file->u.link = bp;
679 read_sbuf(f, bp, linkname_len - 1);
680 if (sanitize_paths)
33ffd7c3 681 sanitize_path(bp, bp, "", lastdir_depth);
a289addd
WD
682 bp += linkname_len;
683 }
684#endif
685
4f5b0756 686#ifdef SUPPORT_HARD_LINKS
9935066b
S
687 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
688 flags |= XMIT_HAS_IDEV_DATA;
97a67bdf 689 if (flags & XMIT_HAS_IDEV_DATA) {
1490812a 690 int64 inode;
4124540d
WD
691 if (protocol_version < 26) {
692 dev = read_int(f);
9935066b 693 inode = read_int(f);
736a6a29 694 } else {
4124540d
WD
695 if (!(flags & XMIT_SAME_DEV))
696 dev = read_longint(f);
9935066b
S
697 inode = read_longint(f);
698 }
699 if (flist->hlink_pool) {
5bf63a11
S
700 file->link_u.idev = pool_talloc(flist->hlink_pool,
701 struct idev, 1, "inode_table");
9935066b
S
702 file->F_INODE = inode;
703 file->F_DEV = dev;
736a6a29 704 }
72914a60 705 }
dc5ddbcc 706#endif
ebed4c3a 707
9a4a237e 708 if (always_checksum && (sum_len || protocol_version < 28)) {
7c4f063b 709 char *sum;
9a4a237e 710 int slen = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
a289addd
WD
711 if (sum_len) {
712 file->u.sum = sum = bp;
713 /*bp += sum_len;*/
9a4a237e 714 } else {
fea4db62 715 /* Prior to 28, we get a useless set of nulls. */
7c4f063b 716 sum = empty_sum;
fea4db62 717 }
9a4a237e 718 read_buf(f, sum, slen);
72914a60 719 }
ebed4c3a 720
72914a60 721 if (!preserve_perms) {
72914a60 722 /* set an appropriate set of permissions based on original
fea4db62 723 * permissions and umask. This emulates what GNU cp does */
72914a60
AT
724 file->mode &= ~orig_umask;
725 }
f5db0993
WD
726
727 return file;
c627d613
AT
728}
729
730
db719fb0
MP
731/**
732 * Create a file_struct for a named file by reading its stat()
733 * information and performing extensive checks against global
734 * options.
735 *
736 * @return the new file, or NULL if there was an error or this file
737 * should be excluded.
738 *
739 * @todo There is a small optimization opportunity here to avoid
740 * stat()ing the file in some circumstances, which has a certain cost.
741 * We are called immediately after doing readdir(), and so we may
742 * already know the d_type of the file. We could for example avoid
743 * statting directories if we're not recursing, but this is not a very
744 * important case. Some systems may not have d_type.
745 **/
314f4591 746struct file_struct *make_file(char *fname, struct file_list *flist,
7842418b 747 int filter_level)
c627d613 748{
a289addd
WD
749 static char *lastdir;
750 static int lastdir_len = -1;
3ec4dd97 751 struct file_struct *file;
bcacc18b 752 STRUCT_STAT st;
3ec4dd97 753 char sum[SUM_LENGTH];
1923b1fc 754 char thisname[MAXPATHLEN];
e0870f1d 755 char linkname[MAXPATHLEN];
a1d55ad0 756 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd 757 char *basename, *dirname, *bp;
4844449a 758 unsigned short flags = 0;
3ec4dd97 759
d508258a 760 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
7de2483f 761 lastdir_len = -1;
9935066b 762
1923b1fc
WD
763 if (strlcpy(thisname, fname, sizeof thisname)
764 >= sizeof thisname - flist_dir_len) {
71903f60
WD
765 rprintf(FINFO, "skipping overly long name: %s\n",
766 safe_fname(fname));
882e6893
WD
767 return NULL;
768 }
58b1999e 769 clean_fname(thisname, 0);
b7736c79 770 if (sanitize_paths)
33ffd7c3 771 sanitize_path(thisname, thisname, "", 0);
3ec4dd97 772
ebed4c3a 773 memset(sum, 0, SUM_LENGTH);
3ec4dd97 774
e0870f1d 775 if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 776 int save_errno = errno;
a4a7e64c 777 /* See if file is excluded before reporting an error. */
7842418b
WD
778 if (filter_level != NO_FILTERS
779 && is_excluded(thisname, 0, filter_level))
a4a7e64c
WD
780 return NULL;
781 if (save_errno == ENOENT) {
4f5b0756 782#ifdef SUPPORT_LINKS
a4a7e64c
WD
783 /* Avoid "vanished" error if symlink points nowhere. */
784 if (copy_links && do_lstat(thisname, &st) == 0
785 && S_ISLNK(st.st_mode)) {
786 io_error |= IOERR_GENERAL;
787 rprintf(FERROR, "symlink has no referent: %s\n",
788 full_fname(thisname));
e5ce3bcf
WD
789 } else
790#endif
791 {
a4a7e64c
WD
792 enum logcode c = am_daemon && protocol_version < 28
793 ? FERROR : FINFO;
794 io_error |= IOERR_VANISHED;
795 rprintf(c, "file has vanished: %s\n",
796 full_fname(thisname));
76e26e10 797 }
a4a7e64c 798 } else {
a8726d2a 799 io_error |= IOERR_GENERAL;
d62bcc17
WD
800 rsyserr(FERROR, save_errno, "readlink %s failed",
801 full_fname(thisname));
76e26e10 802 }
3ec4dd97
AT
803 return NULL;
804 }
c627d613 805
7842418b
WD
806 /* backup.c calls us with filter_level set to NO_FILTERS. */
807 if (filter_level == NO_FILTERS)
808 goto skip_filters;
ac1a0994 809
7e037c42 810 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
71903f60 811 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
3ec4dd97
AT
812 return NULL;
813 }
ebed4c3a 814
e90cdb8a
WD
815 /* We only care about directories because we need to avoid recursing
816 * into a mount-point directory, not to avoid copying a symlinked
817 * file if -L (or similar) was specified. */
818 if (one_file_system && st.st_dev != filesystem_dev
819 && S_ISDIR(st.st_mode))
4844449a 820 flags |= FLAG_MOUNT_POINT;
ebed4c3a 821
7842418b 822 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
76e26e10
DD
823 return NULL;
824
132fcf36 825 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 826#ifdef SUPPORT_LINKS
132fcf36
WD
827 if (!S_ISLNK(st.st_mode))
828#endif
829 if (access(thisname, R_OK) != 0)
830 return NULL;
831 }
ac1a0994 832
7842418b 833skip_filters:
ac1a0994 834
ea847c62
WD
835 if (verbose > 2) {
836 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
71903f60 837 who_am_i(), safe_fname(thisname), filter_level);
ea847c62 838 }
ebed4c3a 839
a289addd
WD
840 if ((basename = strrchr(thisname, '/')) != NULL) {
841 dirname_len = ++basename - thisname; /* counts future '\0' */
842 if (lastdir_len == dirname_len - 1
843 && strncmp(thisname, lastdir, lastdir_len) == 0) {
844 dirname = lastdir;
845 dirname_len = 0; /* indicates no copy is needed */
846 } else
847 dirname = thisname;
3ec4dd97 848 } else {
a289addd
WD
849 basename = thisname;
850 dirname = NULL;
851 dirname_len = 0;
3ec4dd97 852 }
a289addd 853 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 854
4f5b0756 855#ifdef SUPPORT_LINKS
a289addd
WD
856 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
857#else
858 linkname_len = 0;
859#endif
860
a289addd
WD
861 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
862
61dec11a 863 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
864 + linkname_len + sum_len;
865 if (flist) {
866 bp = pool_alloc(flist->file_pool, alloc_len,
867 "receive_file_entry");
868 } else {
869 if (!(bp = new_array(char, alloc_len)))
870 out_of_memory("receive_file_entry");
871 }
872
a289addd 873 file = (struct file_struct *)bp;
9935066b 874 memset(bp, 0, file_struct_len);
61dec11a 875 bp += file_struct_len;
a289addd
WD
876
877 file->flags = flags;
3ec4dd97
AT
878 file->modtime = st.st_mtime;
879 file->length = st.st_size;
880 file->mode = st.st_mode;
881 file->uid = st.st_uid;
882 file->gid = st.st_gid;
0d162bd1 883
4f5b0756 884#ifdef SUPPORT_HARD_LINKS
9935066b
S
885 if (flist && flist->hlink_pool) {
886 if (protocol_version < 28) {
887 if (S_ISREG(st.st_mode))
888 file->link_u.idev = pool_talloc(
889 flist->hlink_pool, struct idev, 1,
890 "inode_table");
891 } else {
892 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
893 file->link_u.idev = pool_talloc(
894 flist->hlink_pool, struct idev, 1,
895 "inode_table");
896 }
897 }
898 if (file->link_u.idev) {
61dec11a
WD
899 file->F_DEV = st.st_dev;
900 file->F_INODE = st.st_ino;
901 }
902#endif
903
a289addd
WD
904 if (dirname_len) {
905 file->dirname = lastdir = bp;
906 lastdir_len = dirname_len - 1;
907 memcpy(bp, dirname, dirname_len - 1);
908 bp += dirname_len;
909 bp[-1] = '\0';
910 } else if (dirname)
911 file->dirname = dirname;
912
913 file->basename = bp;
914 memcpy(bp, basename, basename_len);
915 bp += basename_len;
916
4f5b0756 917#ifdef HAVE_STRUCT_STAT_ST_RDEV
0d162bd1
WD
918 if (preserve_devices && IS_DEVICE(st.st_mode))
919 file->u.rdev = st.st_rdev;
920#endif
921
4f5b0756 922#ifdef SUPPORT_LINKS
a289addd
WD
923 if (linkname_len) {
924 file->u.link = bp;
925 memcpy(bp, linkname, linkname_len);
926 bp += linkname_len;
927 }
0d162bd1
WD
928#endif
929
a289addd
WD
930 if (sum_len) {
931 file->u.sum = bp;
932 file_checksum(thisname, bp, st.st_size);
933 /*bp += sum_len;*/
ebed4c3a 934 }
c627d613 935
f3c3ed44 936 file->dir.root = flist_dir;
c627d613 937
23f4587f
WD
938 /* This code is only used by the receiver when it is building
939 * a list of files for a delete pass. */
940 if (keep_dirlinks && linkname_len && flist) {
941 STRUCT_STAT st2;
f5db0993
WD
942 int save_mode = file->mode;
943 file->mode = S_IFDIR; /* find a directory w/our name */
d64e6f42 944 if (flist_find(the_file_list, file) >= 0
23f4587f
WD
945 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
946 file->modtime = st2.st_mtime;
947 file->length = st2.st_size;
948 file->mode = st2.st_mode;
949 file->uid = st2.st_uid;
950 file->gid = st2.st_gid;
951 file->u.link = NULL;
f5db0993
WD
952 } else
953 file->mode = save_mode;
23f4587f
WD
954 }
955
d17190df 956 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
a800434a 957 stats.total_size += st.st_size;
c627d613 958
3ec4dd97 959 return file;
c627d613
AT
960}
961
962
301fb56c
WD
963static struct file_struct *send_file_name(int f, struct file_list *flist,
964 char *fname, unsigned short base_flags)
c627d613 965{
ebed4c3a
MP
966 struct file_struct *file;
967
37802f40
WD
968 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
969 if (!file)
301fb56c 970 return NULL;
ebed4c3a 971
53135fe8 972 maybe_emit_filelist_progress(flist->count + flist_count_offset);
ebed4c3a 973
d9d6bc52 974 flist_expand(flist);
ebed4c3a 975
c120ff37 976 if (file->basename[0]) {
ebed4c3a
MP
977 flist->files[flist->count++] = file;
978 send_file_entry(file, f, base_flags);
979 }
301fb56c
WD
980 return file;
981}
ebed4c3a 982
301fb56c
WD
983static void send_if_directory(int f, struct file_list *flist,
984 struct file_struct *file)
985{
986 char fbuf[MAXPATHLEN];
987
988 if (S_ISDIR(file->mode)
f1773e09
WD
989 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
990 void *save_filters;
991 unsigned int len = strlen(fbuf);
992 if (len > 1 && fbuf[len-1] == '/')
993 fbuf[--len] = '\0';
994 if (len >= MAXPATHLEN - 1) {
995 io_error |= IOERR_GENERAL;
996 rprintf(FERROR, "skipping long-named directory: %s\n",
997 full_fname(fbuf));
998 return;
999 }
1000 save_filters = push_local_filters(fbuf, len);
1001 send_directory(f, flist, fbuf, len);
1002 pop_local_filters(save_filters);
ebed4c3a 1003 }
c627d613
AT
1004}
1005
1006
301fb56c 1007/* This function is normally called by the sender, but the receiving side also
7b558d7f 1008 * calls it from get_dirlist() with f set to -1 so that we just construct the
301fb56c
WD
1009 * file list in memory without sending it over the wire. Also, get_dirlist()
1010 * might call this with f set to -2, which also indicates that local filter
1011 * rules should be ignored. */
f1773e09 1012static void send_directory(int f, struct file_list *flist,
32cbfe7b 1013 char *fbuf, int len)
c627d613 1014{
3ec4dd97 1015 struct dirent *di;
32cbfe7b 1016 unsigned remainder;
3ec4dd97 1017 char *p;
f1773e09 1018 DIR *d;
301fb56c 1019 int start = flist->count;
3ec4dd97 1020
f1773e09 1021 if (!(d = opendir(fbuf))) {
06c28400 1022 io_error |= IOERR_GENERAL;
f1773e09 1023 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1024 return;
1025 }
c627d613 1026
19b2a5d9
WD
1027 p = fbuf + len;
1028 if (len != 1 || *fbuf != '/')
eddd5d12 1029 *p++ = '/';
f1773e09 1030 *p = '\0';
32cbfe7b 1031 remainder = MAXPATHLEN - (p - fbuf);
ebed4c3a 1032
6a7cc46c 1033 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1034 char *dname = d_name(di);
6a7cc46c
S
1035 if (dname[0] == '.' && (dname[1] == '\0'
1036 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1037 continue;
301fb56c
WD
1038 if (strlcpy(p, dname, remainder) < remainder)
1039 send_file_name(f, flist, fbuf, 0);
1040 else {
eddd5d12
WD
1041 io_error |= IOERR_GENERAL;
1042 rprintf(FINFO,
1043 "cannot send long-named file %s\n",
f1773e09 1044 full_fname(fbuf));
eddd5d12 1045 }
3ec4dd97 1046 }
32cbfe7b
WD
1047
1048 fbuf[len] = '\0';
1049
6a7cc46c 1050 if (errno) {
06c28400 1051 io_error |= IOERR_GENERAL;
71903f60 1052 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1053 }
c627d613 1054
3ec4dd97 1055 closedir(d);
301fb56c
WD
1056
1057 if (recurse) {
1058 int i, end = flist->count - 1;
1059 for (i = start; i <= end; i++)
1060 send_if_directory(f, flist, flist->files[i]);
1061 }
c627d613
AT
1062}
1063
1064
ebed4c3a 1065struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1066{
24d0fcde 1067 int l;
bcacc18b 1068 STRUCT_STAT st;
808c57c3 1069 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 1070 char lastpath[MAXPATHLEN] = "";
649d65ed 1071 struct file_list *flist;
31b4d25d 1072 struct timeval start_tv, end_tv;
a800434a 1073 int64 start_write;
24d0fcde 1074 int use_ff_fd = 0;
649d65ed 1075
134f4338 1076 if (show_filelist_p())
1bbd10fe 1077 start_filelist_progress("building file list");
c627d613 1078
a800434a 1079 start_write = stats.total_written;
31b4d25d 1080 gettimeofday(&start_tv, NULL);
a800434a 1081
134f4338 1082 flist = flist_new(WITH_HLINK, "send_file_list");
c627d613 1083
134f4338
WD
1084 io_start_buffering_out();
1085 if (filesfrom_fd >= 0) {
1086 if (argv[0] && !push_dir(argv[0])) {
1087 rsyserr(FERROR, errno, "push_dir %s failed",
1088 full_fname(argv[0]));
1089 exit_cleanup(RERR_FILESELECT);
24d0fcde 1090 }
134f4338 1091 use_ff_fd = 1;
d6dead6b
AT
1092 }
1093
24d0fcde 1094 while (1) {
301fb56c 1095 struct file_struct *file;
fc638474
DD
1096 char fname2[MAXPATHLEN];
1097 char *fname = fname2;
301fb56c 1098 int is_dot_dir;
c627d613 1099
24d0fcde
WD
1100 if (use_ff_fd) {
1101 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1102 break;
33ffd7c3 1103 sanitize_path(fname, fname, "", 0);
24d0fcde
WD
1104 } else {
1105 if (argc-- == 0)
1106 break;
1107 strlcpy(fname, *argv++, MAXPATHLEN);
1108 if (sanitize_paths)
33ffd7c3 1109 sanitize_path(fname, fname, "", 0);
24d0fcde 1110 }
c627d613 1111
649d65ed 1112 l = strlen(fname);
9f125ea7 1113 if (!l || fname[l - 1] == '/') {
6931c138
WD
1114 if (l == 2 && fname[0] == '.') {
1115 /* Turn "./" into just "." rather than "./." */
1116 fname[1] = '\0';
557a35f5
WD
1117 } else {
1118 if (l + 1 >= MAXPATHLEN)
a1f99493 1119 overflow_exit("send_file_list");
eddd5d12
WD
1120 fname[l++] = '.';
1121 fname[l] = '\0';
53f821f1 1122 }
301fb56c 1123 is_dot_dir = 1;
a289f89f
WD
1124 } else if (l > 1 && fname[l-1] == '.' && fname[l-2] == '.'
1125 && (l == 2 || fname[l-3] == '/')) {
1126 if (l + 2 >= MAXPATHLEN)
a1f99493 1127 overflow_exit("send_file_list");
a289f89f
WD
1128 fname[l++] = '/';
1129 fname[l++] = '.';
1130 fname[l] = '\0';
1131 is_dot_dir = 1;
301fb56c
WD
1132 } else {
1133 is_dot_dir = fname[l-1] == '.'
1134 && (l == 1 || fname[l-2] == '/');
649d65ed 1135 }
c627d613 1136
314f4591 1137 if (link_stat(fname, &st, keep_dirlinks) != 0) {
134f4338
WD
1138 io_error |= IOERR_GENERAL;
1139 rsyserr(FERROR, errno, "link_stat %s failed",
1140 full_fname(fname));
649d65ed
AT
1141 continue;
1142 }
c627d613 1143
7e037c42 1144 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
71903f60
WD
1145 rprintf(FINFO, "skipping directory %s\n",
1146 safe_fname(fname));
649d65ed
AT
1147 continue;
1148 }
c627d613 1149
649d65ed 1150 dir = NULL;
808c57c3 1151 olddir[0] = '\0';
649d65ed
AT
1152
1153 if (!relative_paths) {
ebed4c3a 1154 p = strrchr(fname, '/');
649d65ed 1155 if (p) {
151f59f1 1156 *p = '\0';
ebed4c3a 1157 if (p == fname)
649d65ed
AT
1158 dir = "/";
1159 else
ebed4c3a
MP
1160 dir = fname;
1161 fname = p + 1;
649d65ed 1162 }
d2ea5980
WD
1163 } else if ((p = strstr(fname, "/./")) != NULL) {
1164 *p = '\0';
1165 if (p == fname)
1166 dir = "/";
1167 else
1168 dir = fname;
1169 fname = p + 3;
1170 }
1171
1172 if (!*fname)
1173 fname = ".";
1174
1175 if (dir && *dir) {
1176 static char *lastdir;
1177 static int lastdir_len;
1178
8ad5cea3 1179 strlcpy(olddir, curr_dir, sizeof olddir);
d2ea5980
WD
1180
1181 if (!push_dir(dir)) {
1182 io_error |= IOERR_GENERAL;
1183 rsyserr(FERROR, errno, "push_dir %s failed",
1184 full_fname(dir));
1185 continue;
1186 }
1187
1188 if (lastdir && strcmp(lastdir, dir) == 0) {
1189 flist_dir = lastdir;
1190 flist_dir_len = lastdir_len;
1191 } else {
1192 flist_dir = lastdir = strdup(dir);
1193 flist_dir_len = lastdir_len = strlen(dir);
1194 }
1195 }
1196
1197 if (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
151f59f1
WD
1198 /* Send the implied directories at the start of the
1199 * source spec, so we get their permissions right. */
2154309a 1200 char *lp = lastpath, *fn = fname, *slash = fname;
151f59f1 1201 *p = '\0';
2154309a
WD
1202 /* Skip any initial directories in our path that we
1203 * have in common with lastpath. */
1204 while (*fn && *lp == *fn) {
1205 if (*fn == '/')
1206 slash = fn;
1207 lp++, fn++;
1208 }
1209 *p = '/';
1210 if (fn != p || (*lp && *lp != '/')) {
0f57446d 1211 int save_copy_links = copy_links;
7e037c42 1212 int save_xfer_dirs = xfer_dirs;
2154309a 1213 copy_links = copy_unsafe_links;
7e037c42 1214 xfer_dirs = 1;
2154309a 1215 while ((slash = strchr(slash+1, '/')) != 0) {
151f59f1 1216 *slash = '\0';
301fb56c 1217 send_file_name(f, flist, fname, 0);
2154309a 1218 *slash = '/';
649d65ed 1219 }
0f57446d 1220 copy_links = save_copy_links;
7e037c42 1221 xfer_dirs = save_xfer_dirs;
151f59f1 1222 *p = '\0';
2154309a 1223 strlcpy(lastpath, fname, sizeof lastpath);
649d65ed
AT
1224 *p = '/';
1225 }
1226 }
ebed4c3a 1227
2bca43f6 1228 if (one_file_system)
45478cc7 1229 filesystem_dev = st.st_dev;
2bca43f6 1230
301fb56c
WD
1231 if ((file = send_file_name(f, flist, fname, XMIT_TOP_DIR))) {
1232 if (recurse || (xfer_dirs && is_dot_dir))
1233 send_if_directory(f, flist, file);
1234 }
2bca43f6 1235
808c57c3 1236 if (olddir[0]) {
649d65ed 1237 flist_dir = NULL;
882e6893 1238 flist_dir_len = 0;
808c57c3 1239 if (!pop_dir(olddir)) {
d62bcc17 1240 rsyserr(FERROR, errno, "pop_dir %s failed",
92cdc393 1241 full_fname(olddir));
65417579 1242 exit_cleanup(RERR_FILESELECT);
649d65ed 1243 }
649d65ed 1244 }
649d65ed 1245 }
dc5ddbcc 1246
134f4338
WD
1247 gettimeofday(&end_tv, NULL);
1248 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1249 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1250 if (stats.flist_buildtime == 0)
1251 stats.flist_buildtime = 1;
1252 start_tv = end_tv;
31b4d25d 1253
134f4338 1254 send_file_entry(NULL, f, 0);
c627d613 1255
134f4338
WD
1256 if (show_filelist_p())
1257 finish_filelist_progress(flist);
31b4d25d 1258
134f4338
WD
1259 gettimeofday(&end_tv, NULL);
1260 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1261 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 1262
7cf8e8d0 1263 if (flist->hlink_pool) {
9935066b
S
1264 pool_destroy(flist->hlink_pool);
1265 flist->hlink_pool = NULL;
1266 }
1267
f5db0993
WD
1268 /* Sort the list without removing any duplicates. This allows the
1269 * receiving side to ask for any name they like, which gives us the
1270 * flexibility to change the way we unduplicate names in the future
1271 * without causing a compatibility problem with older versions. */
827c37f6 1272 clean_flist(flist, 0, 0);
ebed4c3a 1273
134f4338
WD
1274 /* Now send the uid/gid list. This was introduced in
1275 * protocol version 15 */
1276 send_uid_list(f);
f6c34742 1277
134f4338
WD
1278 /* send the io_error flag */
1279 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1280
134f4338
WD
1281 io_end_buffering();
1282 stats.flist_size = stats.total_written - start_write;
1283 stats.num_files = flist->count;
d6dead6b 1284
cefed3e8 1285 if (verbose > 3)
32cbfe7b 1286 output_flist(flist);
cefed3e8 1287
17faa41c 1288 if (verbose > 2)
ebed4c3a 1289 rprintf(FINFO, "send_file_list done\n");
17faa41c 1290
649d65ed 1291 return flist;
c627d613
AT
1292}
1293
1294
1295struct file_list *recv_file_list(int f)
1296{
ebed4c3a 1297 struct file_list *flist;
1ef00d20 1298 unsigned short flags;
ebed4c3a 1299 int64 start_read;
c627d613 1300
1bbd10fe
DD
1301 if (show_filelist_p())
1302 start_filelist_progress("receiving file list");
c627d613 1303
ebed4c3a 1304 start_read = stats.total_read;
a800434a 1305
9935066b 1306 flist = flist_new(WITH_HLINK, "recv_file_list");
c627d613 1307
ebed4c3a
MP
1308 flist->count = 0;
1309 flist->malloced = 1000;
58cadc86 1310 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1311 if (!flist->files)
1312 goto oom;
c627d613
AT
1313
1314
1ef00d20 1315 while ((flags = read_byte(f)) != 0) {
f5db0993 1316 struct file_struct *file;
dbda5fbf 1317
d9d6bc52 1318 flist_expand(flist);
c627d613 1319
d01d15e0 1320 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1321 flags |= read_byte(f) << 8;
f5db0993 1322 file = receive_file_entry(flist, flags, f);
c627d613 1323
f5db0993
WD
1324 if (S_ISREG(file->mode))
1325 stats.total_size += file->length;
c627d613 1326
f5db0993 1327 flist->files[flist->count++] = file;
c627d613 1328
53135fe8 1329 maybe_emit_filelist_progress(flist->count);
1bbd10fe 1330
8018edd3 1331 if (verbose > 2) {
ebed4c3a 1332 rprintf(FINFO, "recv_file_name(%s)\n",
f5db0993 1333 safe_fname(f_name(file)));
8018edd3 1334 }
ebed4c3a 1335 }
f5db0993 1336 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
c627d613 1337
ebed4c3a
MP
1338 if (verbose > 2)
1339 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1340
b7736c79 1341 if (show_filelist_p())
1bbd10fe 1342 finish_filelist_progress(flist);
a06d19e3 1343
983b1ed3
WD
1344 clean_flist(flist, relative_paths, 1);
1345
37802f40 1346 if (f >= 0) {
785db4ce
WD
1347 /* Now send the uid/gid list. This was introduced in
1348 * protocol version 15 */
ebed4c3a 1349 recv_uid_list(f, flist);
f6c34742 1350
b9f592fb
WD
1351 /* Recv the io_error flag */
1352 if (lp_ignore_errors(module_id) || ignore_errors)
1353 read_int(f);
1354 else
1355 io_error |= read_int(f);
ebed4c3a 1356 }
6ba9279f 1357
cefed3e8 1358 if (verbose > 3)
32cbfe7b 1359 output_flist(flist);
cefed3e8 1360
ebed4c3a
MP
1361 if (list_only) {
1362 int i;
b7736c79 1363 for (i = 0; i < flist->count; i++)
ebed4c3a 1364 list_file_entry(flist->files[i]);
ebed4c3a 1365 }
f7632fc6 1366
ebed4c3a
MP
1367 if (verbose > 2)
1368 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1369
ebed4c3a
MP
1370 stats.flist_size = stats.total_read - start_read;
1371 stats.num_files = flist->count;
a800434a 1372
ebed4c3a 1373 return flist;
c627d613 1374
3e491682 1375oom:
ebed4c3a
MP
1376 out_of_memory("recv_file_list");
1377 return NULL; /* not reached */
c627d613
AT
1378}
1379
1380
14698a3a 1381static int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1382{
14698a3a 1383 return f_name_cmp(*file1, *file2);
c627d613
AT
1384}
1385
1386
f5db0993
WD
1387/* Search for an identically-named item in the file list. Note that the
1388 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 1389int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1390{
f3c3ed44
WD
1391 int low = flist->low, high = flist->high;
1392 int ret, mid, mid_up;
1393
1394 while (low <= high) {
1395 mid = (low + high) / 2;
1396 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1397 if (mid_up <= high)
14698a3a 1398 ret = f_name_cmp(flist->files[mid_up], f);
f3c3ed44
WD
1399 else
1400 ret = 1;
f5db0993
WD
1401 if (ret == 0) {
1402 if (protocol_version < 29
1403 && S_ISDIR(flist->files[mid_up]->mode)
1404 != S_ISDIR(f->mode))
1405 return -1;
f3c3ed44 1406 return mid_up;
f5db0993 1407 }
b7736c79 1408 if (ret > 0)
f3c3ed44 1409 high = mid - 1;
b7736c79 1410 else
f3c3ed44 1411 low = mid_up + 1;
d966ee25 1412 }
d966ee25 1413 return -1;
c627d613
AT
1414}
1415
f5db0993 1416
3ec4dd97 1417/*
9935066b
S
1418 * Free up any resources a file_struct has allocated
1419 * and clear the file.
3ec4dd97 1420 */
9935066b 1421void clear_file(int i, struct file_list *flist)
c627d613 1422{
9935066b
S
1423 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1424 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1425 memset(flist->files[i], 0, file_struct_len);
3ec4dd97 1426}
c627d613 1427
c627d613 1428
3d382777
AT
1429/*
1430 * allocate a new file list
1431 */
9935066b 1432struct file_list *flist_new(int with_hlink, char *msg)
3d382777
AT
1433{
1434 struct file_list *flist;
1435
58cadc86 1436 flist = new(struct file_list);
ebed4c3a 1437 if (!flist)
9935066b 1438 out_of_memory(msg);
3d382777 1439
9935066b
S
1440 memset(flist, 0, sizeof (struct file_list));
1441
1442 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1443 out_of_memory, POOL_INTERN)))
1444 out_of_memory(msg);
1445
4f5b0756 1446#ifdef SUPPORT_HARD_LINKS
9935066b 1447 if (with_hlink && preserve_hard_links) {
3e491682 1448 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
9935066b
S
1449 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1450 out_of_memory(msg);
1451 }
1452#endif
d9d6bc52 1453
3d382777
AT
1454 return flist;
1455}
ebed4c3a 1456
3ec4dd97
AT
1457/*
1458 * free up all elements in a flist
1459 */
1460void flist_free(struct file_list *flist)
1461{
9935066b
S
1462 pool_destroy(flist->file_pool);
1463 pool_destroy(flist->hlink_pool);
3ec4dd97 1464 free(flist->files);
3ec4dd97 1465 free(flist);
c627d613
AT
1466}
1467
1468
1469/*
1470 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1471 * duplicate names can cause corruption because of the pipelining
c627d613 1472 */
827c37f6 1473static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1474{
6931c138 1475 int i, prev_i = 0;
c627d613 1476
910ee8c9
WD
1477 if (!flist)
1478 return;
1479 if (flist->count == 0) {
1480 flist->high = -1;
3ec4dd97 1481 return;
910ee8c9 1482 }
3ec4dd97 1483
bf0c5bec 1484 sorting_flist = flist;
ebed4c3a 1485 qsort(flist->files, flist->count,
a4a7e64c 1486 sizeof flist->files[0], (int (*)())file_compare);
bf0c5bec 1487 sorting_flist = NULL;
ebed4c3a 1488
827c37f6 1489 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1490 if (flist->files[i]->basename) {
6931c138 1491 prev_i = i;
b91b50c0
WD
1492 break;
1493 }
1494 }
f3c3ed44 1495 flist->low = prev_i;
b91b50c0 1496 while (++i < flist->count) {
fe1c19dc 1497 int j;
f5db0993
WD
1498 struct file_struct *file = flist->files[i];
1499
1500 if (!file->basename)
b91b50c0 1501 continue;
fe1c19dc
WD
1502 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1503 j = prev_i;
1504 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
1505 int save_mode = file->mode;
1506 /* Make sure that this directory doesn't duplicate a
1507 * non-directory earlier in the list. */
f5db0993 1508 flist->high = prev_i;
fe1c19dc
WD
1509 file->mode = S_IFREG;
1510 j = flist_find(flist, file);
f5db0993 1511 file->mode = save_mode;
fe1c19dc
WD
1512 } else
1513 j = -1;
1514 if (j >= 0) {
1515 struct file_struct *fp = flist->files[j];
1516 int keep, drop;
1517 /* If one is a dir and the other is not, we want to
1518 * keep the dir because it might have contents in the
1519 * list. */
1520 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1521 if (S_ISDIR(file->mode))
1522 keep = i, drop = j;
1523 else
1524 keep = j, drop = i;
1525 } else
1526 keep = j, drop = i;
b91b50c0 1527 if (verbose > 1 && !am_server) {
ebed4c3a 1528 rprintf(FINFO,
fe1c19dc
WD
1529 "removing duplicate name %s from file list (%d)\n",
1530 safe_fname(f_name(file)), drop);
b91b50c0 1531 }
6931c138 1532 /* Make sure that if we unduplicate '.', that we don't
ee3751c8 1533 * lose track of a user-specified top directory. */
0f0b2e66
WD
1534 flist->files[keep]->flags |= flist->files[drop]->flags
1535 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
fe1c19dc
WD
1536
1537 clear_file(drop, flist);
9935066b 1538
fe1c19dc
WD
1539 if (keep == i) {
1540 if (flist->low == drop) {
1541 for (j = drop + 1;
1542 j < i && !flist->files[j]->basename;
1543 j++) {}
1544 flist->low = j;
1545 }
1546 prev_i = i;
1547 }
728d0922 1548 } else
6931c138 1549 prev_i = i;
3ec4dd97 1550 }
f5db0993 1551 flist->high = no_dups ? prev_i : flist->count - 1;
0199b05f
AT
1552
1553 if (strip_root) {
f5db0993
WD
1554 /* We need to strip off the leading slashes for relative
1555 * paths, but this must be done _after_ the sorting phase. */
1556 for (i = flist->low; i <= flist->high; i++) {
1557 struct file_struct *file = flist->files[i];
ebed4c3a 1558
f5db0993
WD
1559 if (!file->dirname)
1560 continue;
1561 if (*file->dirname == '/') {
1562 char *s = file->dirname + 1;
1563 while (*s == '/') s++;
1564 memmove(file->dirname, s, strlen(s) + 1);
0199b05f 1565 }
f5db0993
WD
1566
1567 if (!*file->dirname)
1568 file->dirname = NULL;
0199b05f
AT
1569 }
1570 }
cefed3e8 1571}
0199b05f 1572
32cbfe7b
WD
1573
1574static void output_flist(struct file_list *flist)
cefed3e8 1575{
f3c3ed44 1576 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8 1577 struct file_struct *file;
32cbfe7b 1578 const char *who = who_am_i();
cefed3e8 1579 int i;
0199b05f 1580
ebed4c3a 1581 for (i = 0; i < flist->count; i++) {
cefed3e8 1582 file = flist->files[i];
56194bcd 1583 if ((am_root || am_sender) && preserve_uid)
f05f993e
WD
1584 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1585 else
1586 *uidbuf = '\0';
cefed3e8 1587 if (preserve_gid && file->gid != GID_NONE)
f05f993e
WD
1588 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1589 else
1590 *gidbuf = '\0';
f3c3ed44
WD
1591 if (!am_sender)
1592 sprintf(depthbuf, "%d", file->dir.depth);
14698a3a 1593 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
32cbfe7b 1594 who, i, am_sender ? NS(file->dir.root) : depthbuf,
71903f60 1595 file->dirname ? safe_fname(file->dirname) : "",
14698a3a
WD
1596 file->dirname ? "/" : "", NS(file->basename),
1597 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
f3c3ed44 1598 (double)file->length, uidbuf, gidbuf, file->flags);
0199b05f 1599 }
3ec4dd97
AT
1600}
1601
1602
8824e2ce 1603enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 1604enum fnc_type { t_PATH, t_ITEM };
8018edd3 1605
2f3cad89 1606/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
1607 * would do if it were operating on the joined strings.
1608 *
1609 * Some differences beginning with protocol_version 29: (1) directory names
1610 * are compared with an assumed trailing slash so that they compare in a
1611 * way that would cause them to sort immediately prior to any content they
1612 * may have; (2) a directory of any name compares after a non-directory of
1613 * any name at the same depth; (3) a directory with name "." compares prior
1614 * to anything else. These changes mean that a directory and a non-dir
1615 * with the same name will not compare as equal (protocol_version >= 29).
1616 *
1617 * The dirname component can be an empty string, but the basename component
1618 * cannot (and never is in the current codebase). The basename component
1619 * may be NULL (for a removed item), in which case it is considered to be
1620 * after any existing item. */
8018edd3 1621int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1622{
8018edd3
WD
1623 int dif;
1624 const uchar *c1, *c2;
1ef00d20 1625 enum fnc_state state1, state2;
f5db0993
WD
1626 enum fnc_type type1, type2;
1627 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3
WD
1628
1629 if (!f1 || !f1->basename) {
1630 if (!f2 || !f2->basename)
1631 return 0;
1632 return -1;
1633 }
1634 if (!f2 || !f2->basename)
1635 return 1;
1636
14698a3a
WD
1637 c1 = (uchar*)f1->dirname;
1638 c2 = (uchar*)f2->dirname;
1639 if (c1 == c2)
1640 c1 = c2 = NULL;
1641 if (!c1) {
f5db0993 1642 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1643 c1 = (uchar*)f1->basename;
f5db0993
WD
1644 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1645 type1 = t_ITEM;
1646 state1 = s_TRAILING;
1647 c1 = (uchar*)"";
1648 } else
1649 state1 = s_BASE;
080ddf58 1650 } else if (!*c1) {
f5db0993 1651 type1 = t_path;
8824e2ce 1652 state1 = s_SLASH;
080ddf58 1653 c1 = (uchar*)"/";
f5db0993
WD
1654 } else {
1655 type1 = t_path;
8824e2ce 1656 state1 = s_DIR;
f5db0993 1657 }
14698a3a 1658 if (!c2) {
f5db0993 1659 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1660 c2 = (uchar*)f2->basename;
f5db0993
WD
1661 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1662 type2 = t_ITEM;
1663 state2 = s_TRAILING;
1664 c2 = (uchar*)"";
1665 } else
1666 state2 = s_BASE;
080ddf58 1667 } else if (!*c2) {
f5db0993 1668 type2 = t_path;
8824e2ce 1669 state2 = s_SLASH;
080ddf58 1670 c2 = (uchar*)"/";
f5db0993
WD
1671 } else {
1672 type2 = t_path;
8824e2ce 1673 state2 = s_DIR;
f5db0993
WD
1674 }
1675
1676 if (type1 != type2)
1677 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1678
1679 while (1) {
f5db0993 1680 if ((dif = (int)*c1++ - (int)*c2++) != 0)
8018edd3 1681 break;
f5db0993 1682 if (!*c1) {
8018edd3 1683 switch (state1) {
8824e2ce
WD
1684 case s_DIR:
1685 state1 = s_SLASH;
e90b8ace 1686 c1 = (uchar*)"/";
8018edd3 1687 break;
8824e2ce 1688 case s_SLASH:
f5db0993 1689 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1690 c1 = (uchar*)f1->basename;
cbb5fa4f
WD
1691 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1692 type1 = t_ITEM;
1693 state1 = s_TRAILING;
1694 c1 = (uchar*)"";
1695 } else
1696 state1 = s_BASE;
8018edd3 1697 break;
8824e2ce
WD
1698 case s_BASE:
1699 state1 = s_TRAILING;
f5db0993 1700 if (type1 == t_PATH) {
2f3cad89 1701 c1 = (uchar*)"/";
f5db0993
WD
1702 break;
1703 }
1704 /* FALL THROUGH */
8824e2ce 1705 case s_TRAILING:
f5db0993 1706 type1 = t_ITEM;
8018edd3
WD
1707 break;
1708 }
f5db0993
WD
1709 if (*c2 && type1 != type2)
1710 return type1 == t_PATH ? 1 : -1;
8018edd3 1711 }
f5db0993 1712 if (!*c2) {
8018edd3 1713 switch (state2) {
8824e2ce
WD
1714 case s_DIR:
1715 state2 = s_SLASH;
e90b8ace 1716 c2 = (uchar*)"/";
8018edd3 1717 break;
8824e2ce 1718 case s_SLASH:
f5db0993 1719 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1720 c2 = (uchar*)f2->basename;
cbb5fa4f
WD
1721 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1722 type2 = t_ITEM;
1723 state2 = s_TRAILING;
1724 c2 = (uchar*)"";
1725 } else
1726 state2 = s_BASE;
8018edd3 1727 break;
8824e2ce 1728 case s_BASE:
8824e2ce 1729 state2 = s_TRAILING;
f5db0993 1730 if (type2 == t_PATH) {
2f3cad89 1731 c2 = (uchar*)"/";
f5db0993
WD
1732 break;
1733 }
1734 /* FALL THROUGH */
8824e2ce 1735 case s_TRAILING:
f5db0993
WD
1736 if (!*c1)
1737 return 0;
1738 type2 = t_ITEM;
8018edd3
WD
1739 break;
1740 }
f5db0993
WD
1741 if (type1 != type2)
1742 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1743 }
1744 }
1745
1746 return dif;
1747}
1748
3ec4dd97 1749
8018edd3 1750/* Return a copy of the full filename of a flist entry, using the indicated
882e6893
WD
1751 * buffer. No size-checking is done because we checked the size when creating
1752 * the file_struct entry.
8018edd3 1753 */
882e6893 1754char *f_name_to(struct file_struct *f, char *fbuf)
8018edd3 1755{
ebed4c3a
MP
1756 if (!f || !f->basename)
1757 return NULL;
3ec4dd97 1758
3ec4dd97 1759 if (f->dirname) {
882e6893
WD
1760 int len = strlen(f->dirname);
1761 memcpy(fbuf, f->dirname, len);
1762 fbuf[len] = '/';
1763 strcpy(fbuf + len + 1, f->basename);
8018edd3 1764 } else
882e6893 1765 strcpy(fbuf, f->basename);
b7736c79 1766 return fbuf;
8018edd3 1767}
e03dfae5 1768
3ec4dd97 1769
45478cc7 1770/* Like f_name_to(), but we rotate through 5 static buffers of our own. */
8018edd3
WD
1771char *f_name(struct file_struct *f)
1772{
1773 static char names[5][MAXPATHLEN];
1774 static unsigned int n;
1775
1776 n = (n + 1) % (sizeof names / sizeof names[0]);
1777
882e6893 1778 return f_name_to(f, names[n]);
c627d613 1779}
649f8742 1780
45478cc7 1781
32cbfe7b
WD
1782/* Do a non-recursive scan of the named directory, possibly ignoring all
1783 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1784 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1785 * buffer (the functions we call will append names onto the end, but the old
1786 * dir value will be restored on exit). */
1787struct file_list *get_dirlist(char *dirname, int dlen,
1788 int ignore_filter_rules)
37802f40
WD
1789{
1790 struct file_list *dirlist;
1791 char dirbuf[MAXPATHLEN];
37802f40
WD
1792 int save_recurse = recurse;
1793
32cbfe7b
WD
1794 if (dlen < 0) {
1795 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1796 if (dlen >= MAXPATHLEN)
1797 return NULL;
1798 dirname = dirbuf;
1799 }
37802f40
WD
1800
1801 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
32cbfe7b 1802
37802f40 1803 recurse = 0;
32cbfe7b 1804 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
37802f40 1805 recurse = save_recurse;
53135fe8
WD
1806 if (do_progress)
1807 flist_count_offset += dirlist->count;
37802f40 1808
564ef546
WD
1809 clean_flist(dirlist, 0, 0);
1810
45478cc7 1811 if (verbose > 3)
32cbfe7b 1812 output_flist(dirlist);
45478cc7 1813
32cbfe7b 1814 return dirlist;
45478cc7 1815}