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