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