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