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