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