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