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