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