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