Changed F_ROOTDIR() to F_PATHNAME().
[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>
ba2133d6 7 * Copyright (C) 2002-2007 Wayne Davison
0f78b815
WD
8 *
9 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
736a6a29 12 *
0f78b815
WD
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
172875cf 17 *
e7c67065
WD
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 21 */
c627d613
AT
22
23#include "rsync.h"
a3e18c76 24#include "rounding.h"
6d4e718f 25#include "io.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;
f14b3ef4 33extern int am_generator;
3ea6e0e7 34extern int inc_recurse;
8715db2c 35extern int do_progress;
c627d613 36extern int always_checksum;
983b1ed3
WD
37extern int module_id;
38extern int ignore_errors;
4836c3ee 39extern int numeric_ids;
a06d19e3 40extern int recurse;
7e037c42 41extern int xfer_dirs;
24d0fcde 42extern int filesfrom_fd;
c627d613 43extern int one_file_system;
88c2190a 44extern int copy_dirlinks;
314f4591 45extern int keep_dirlinks;
1c3344a1 46extern int preserve_acls;
16edf865 47extern int preserve_xattrs;
c627d613 48extern int preserve_links;
dc5ddbcc 49extern int preserve_hard_links;
c627d613 50extern int preserve_devices;
b5c6a6ae 51extern int preserve_specials;
c627d613
AT
52extern int preserve_uid;
53extern int preserve_gid;
6574b4f7 54extern int relative_paths;
24d0fcde 55extern int implied_dirs;
99a957d3 56extern int file_extra_cnt;
53039410
WD
57extern int ignore_perishable;
58extern int non_perishable_cnt;
85aecef6 59extern int prune_empty_dirs;
82306bf6 60extern int copy_links;
b5313607 61extern int copy_unsafe_links;
d04e9c51 62extern int protocol_version;
cb13abfe 63extern int sanitize_paths;
d64e6f42 64extern struct stats stats;
c627d613 65
e1f40891
WD
66extern char curr_dir[MAXPATHLEN];
67
2b7e0f33
WD
68extern struct chmod_mode_struct *chmod_modes;
69
7842418b
WD
70extern struct filter_list_struct filter_list;
71extern struct filter_list_struct server_filter_list;
c627d613 72
06c28400 73int io_error;
7752df41 74int checksum_len;
535737bf 75dev_t filesystem_dev; /* used to implement -x */
82ad07c4 76
c7e6f84f
WD
77struct file_list *cur_flist, *first_flist, *dir_flist;
78int send_dir_ndx = -1, send_dir_depth = 0;
79int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
80int file_total = 0; /* total of all active items over all file-lists */
81int flist_eof = 0; /* all the file-lists are now known */
82
82ad07c4
WD
83/* The tmp_* vars are used as a cache area by make_file() to store data
84 * that the sender doesn't need to remember in its file list. The data
85 * will survive just long enough to be used by send_file_entry(). */
86static dev_t tmp_rdev;
9d155ecd 87#ifdef SUPPORT_HARD_LINKS
9d737ecb 88static int64 tmp_dev, tmp_ino;
9d155ecd 89#endif
a0456b9c 90static char tmp_sum[MAX_DIGEST_LEN];
06c28400 91
a0456b9c 92static char empty_sum[MAX_DIGEST_LEN];
c7565dad 93static int flist_count_offset; /* for --delete --progress */
3d382777 94
827c37f6 95static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
32cbfe7b 96static void output_flist(struct file_list *flist);
0199b05f 97
61dec11a
WD
98void init_flist(void)
99{
96293cf9
WD
100 if (verbose > 4) {
101 rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
6aef83c9 102 (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
96293cf9 103 }
a0456b9c
WD
104 checksum_len = protocol_version < 21 ? 2
105 : protocol_version < 30 ? MD4_DIGEST_LEN
106 : MD5_DIGEST_LEN;
61dec11a
WD
107}
108
1bbd10fe 109static int show_filelist_p(void)
58225000 110{
3ea6e0e7 111 return verbose && xfer_dirs && !am_server && !inc_recurse;
1bbd10fe 112}
ebed4c3a 113
1bbd10fe
DD
114static void start_filelist_progress(char *kind)
115{
ea124cb3 116 rprintf(FCLIENT, "%s ... ", kind);
e5ce3bcf 117 if (verbose > 1 || do_progress)
ea124cb3 118 rprintf(FCLIENT, "\n");
1bbd10fe 119 rflush(FINFO);
58225000
MP
120}
121
53135fe8 122static void emit_filelist_progress(int count)
db719fb0 123{
ea124cb3 124 rprintf(FCLIENT, " %d files...\r", count);
db719fb0
MP
125}
126
53135fe8 127static void maybe_emit_filelist_progress(int count)
58225000 128{
53135fe8
WD
129 if (do_progress && show_filelist_p() && (count % 100) == 0)
130 emit_filelist_progress(count);
58225000
MP
131}
132
1bbd10fe 133static void finish_filelist_progress(const struct file_list *flist)
58225000 134{
1bbd10fe
DD
135 if (do_progress) {
136 /* This overwrites the progress line */
c7b562be
MP
137 rprintf(FINFO, "%d file%sto consider\n",
138 flist->count, flist->count == 1 ? " " : "s ");
b7736c79 139 } else
1bbd10fe 140 rprintf(FINFO, "done\n");
58225000
MP
141}
142
86943126
MP
143void show_flist_stats(void)
144{
145 /* Nothing yet */
146}
147
f7632fc6
AT
148static void list_file_entry(struct file_struct *f)
149{
78d146e8 150 char permbuf[PERMSTRING_SIZE];
96293cf9 151 double len;
f7632fc6 152
96293cf9 153 if (!F_IS_ACTIVE(f)) {
7212be92
DD
154 /* this can happen if duplicate names were removed */
155 return;
e5ce3bcf 156 }
7212be92 157
78d146e8 158 permstring(permbuf, f->mode);
96293cf9 159 len = F_LENGTH(f);
740819ef 160
1c3344a1
WD
161 /* TODO: indicate '+' if the entry has an ACL. */
162
4f5b0756 163#ifdef SUPPORT_LINKS
f7632fc6 164 if (preserve_links && S_ISLNK(f->mode)) {
ebed4c3a 165 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
96293cf9 166 permbuf, len, timestring(f->modtime),
82ad07c4 167 f_name(f, NULL), F_SYMLINK(f));
0d162bd1
WD
168 } else
169#endif
e5ce3bcf 170 {
ebed4c3a 171 rprintf(FINFO, "%s %11.0f %s %s\n",
96293cf9 172 permbuf, len, timestring(f->modtime),
5e4ff5f9 173 f_name(f, NULL));
e5ce3bcf 174 }
f7632fc6
AT
175}
176
cad8f6f9
WD
177/* Stat either a symlink or its referent, depending on the settings of
178 * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
4e5db0ad 179 *
cad8f6f9
WD
180 * If path is the name of a symlink, then the linkbuf buffer (which must hold
181 * MAXPATHLEN chars) will be set to the symlink's target string.
4e5db0ad 182 *
cad8f6f9
WD
183 * The stat structure pointed to by stp will contain information about the
184 * link or the referent as appropriate, if they exist. */
185static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
b5313607 186{
4f5b0756 187#ifdef SUPPORT_LINKS
cad8f6f9 188 if (link_stat(path, stp, copy_dirlinks) < 0)
b5313607 189 return -1;
cad8f6f9
WD
190 if (S_ISLNK(stp->st_mode)) {
191 int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
192 if (llen < 0)
b5313607 193 return -1;
cad8f6f9 194 linkbuf[llen] = '\0';
fc638474
DD
195 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
196 if (verbose > 1) {
dbda5fbf 197 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
0ee6ca98 198 path, linkbuf);
fc638474 199 }
9439c0cb 200 return x_stat(path, stp, NULL);
b5313607
DD
201 }
202 }
203 return 0;
204#else
9439c0cb 205 return x_stat(path, stp, NULL);
b5313607
DD
206#endif
207}
208
cad8f6f9 209int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
82306bf6 210{
4f5b0756 211#ifdef SUPPORT_LINKS
b7736c79 212 if (copy_links)
9439c0cb
WD
213 return x_stat(path, stp, NULL);
214 if (x_lstat(path, stp, NULL) < 0)
314f4591 215 return -1;
cad8f6f9 216 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
314f4591 217 STRUCT_STAT st;
9439c0cb 218 if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
cad8f6f9 219 *stp = st;
314f4591
WD
220 }
221 return 0;
82306bf6 222#else
9439c0cb 223 return x_stat(path, stp, NULL);
82306bf6
AT
224#endif
225}
226
7842418b 227/* This function is used to check if a file should be included/excluded
429f9828 228 * from the list of files based on its name and type etc. The value of
7842418b
WD
229 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
230static int is_excluded(char *fname, int is_dir, int filter_level)
c627d613 231{
7d687932 232#if 0 /* This currently never happens, so avoid a useless compare. */
7842418b 233 if (filter_level == NO_FILTERS)
429f9828
WD
234 return 0;
235#endif
6931c138 236 if (fname) {
429f9828 237 /* never exclude '.', even if somebody does --exclude '*' */
6931c138
WD
238 if (fname[0] == '.' && !fname[1])
239 return 0;
240 /* Handle the -R version of the '.' dir. */
241 if (fname[0] == '/') {
242 int len = strlen(fname);
243 if (fname[len-1] == '.' && fname[len-2] == '/')
244 return 0;
245 }
76e26e10 246 }
7842418b
WD
247 if (server_filter_list.head
248 && check_filter(&server_filter_list, fname, is_dir) < 0)
429f9828 249 return 1;
7842418b 250 if (filter_level != ALL_FILTERS)
429f9828 251 return 0;
7842418b
WD
252 if (filter_list.head
253 && check_filter(&filter_list, fname, is_dir) < 0)
76e26e10 254 return 1;
76e26e10 255 return 0;
c627d613
AT
256}
257
c7e6f84f
WD
258static void send_directory(int f, struct file_list *flist, int ndx,
259 char *fbuf, int len, int flags);
c627d613 260
ef35abb2
WD
261static const char *pathname, *orig_dir;
262static int pathname_len;
c627d613 263
3ec4dd97 264
d9d6bc52
MP
265/**
266 * Make sure @p flist is big enough to hold at least @p flist->count
267 * entries.
268 **/
a85906c7 269void flist_expand(struct file_list *flist)
d9d6bc52 270{
0501f363 271 struct file_struct **new_ptr;
2e7d1994 272
a85906c7
S
273 if (flist->count < flist->malloced)
274 return;
dbda5fbf 275
a85906c7
S
276 if (flist->malloced < FLIST_START)
277 flist->malloced = FLIST_START;
278 else if (flist->malloced >= FLIST_LINEAR)
279 flist->malloced += FLIST_LINEAR;
280 else
281 flist->malloced *= 2;
282
283 /*
284 * In case count jumped or we are starting the list
285 * with a known size just set it.
286 */
287 if (flist->malloced < flist->count)
288 flist->malloced = flist->count;
289
0501f363
WD
290 new_ptr = realloc_array(flist->files, struct file_struct *,
291 flist->malloced);
2e7d1994 292
8c483820 293 if (verbose >= 2 && flist->malloced != FLIST_START) {
ea124cb3 294 rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
a85906c7 295 who_am_i(),
a4a7e64c 296 (double)sizeof flist->files[0] * flist->malloced,
a85906c7 297 (new_ptr == flist->files) ? " not" : "");
d9d6bc52 298 }
a85906c7 299
0501f363 300 flist->files = new_ptr;
a85906c7
S
301
302 if (!flist->files)
303 out_of_memory("flist_expand");
d9d6bc52
MP
304}
305
ef35abb2 306int push_pathname(const char *dir, int len)
c7e6f84f 307{
ef35abb2 308 if (dir == pathname)
c7e6f84f
WD
309 return 1;
310
311 if (!orig_dir)
312 orig_dir = strdup(curr_dir);
313
ef35abb2 314 if (pathname && !pop_dir(orig_dir)) {
c7e6f84f
WD
315 rsyserr(FERROR, errno, "pop_dir %s failed",
316 full_fname(orig_dir));
317 exit_cleanup(RERR_FILESELECT);
318 }
319
320 if (dir && !push_dir(dir, 0)) {
321 io_error |= IOERR_GENERAL;
63d83e94
WD
322 rsyserr(FERROR, errno, "push_dir %s failed in %s",
323 full_fname(dir), curr_dir);
c7e6f84f
WD
324 return 0;
325 }
326
ef35abb2
WD
327 pathname = dir;
328 pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0;
c7e6f84f
WD
329
330 return 1;
331}
332
42c6b139 333static void send_file_entry(int f, struct file_struct *file, int ndx)
c627d613 334{
5911fee5
WD
335 static time_t modtime;
336 static mode_t mode;
1490812a 337 static int64 dev;
4124540d 338 static dev_t rdev;
9c5e91f8 339 static uint32 rdev_major;
5911fee5
WD
340 static uid_t uid;
341 static gid_t gid;
c7e6f84f 342 static char *user_name, *group_name;
5911fee5 343 static char lastname[MAXPATHLEN];
f376e674 344 char fname[MAXPATHLEN];
8cae71eb 345 int first_hlink_ndx = -1;
ebed4c3a 346 int l1, l2;
99a957d3 347 int flags;
72914a60 348
5e4ff5f9 349 f_name(file, fname);
72914a60 350
82ad07c4 351 flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
72914a60 352
30f337c9 353 if (file->mode == mode)
d01d15e0 354 flags |= XMIT_SAME_MODE;
1ef00d20 355 else
30f337c9 356 mode = file->mode;
b5c6a6ae
WD
357 if ((preserve_devices && IS_DEVICE(mode))
358 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 359 if (protocol_version < 28) {
82ad07c4 360 if (tmp_rdev == rdev)
b5c6a6ae
WD
361 flags |= XMIT_SAME_RDEV_pre28;
362 else
82ad07c4 363 rdev = tmp_rdev;
b5c6a6ae 364 } else {
82ad07c4 365 rdev = tmp_rdev;
9c5e91f8
WD
366 if ((uint32)major(rdev) == rdev_major)
367 flags |= XMIT_SAME_RDEV_MAJOR;
84a3efa0 368 else
9c5e91f8 369 rdev_major = major(rdev);
18438f0b
WD
370 if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
371 flags |= XMIT_RDEV_MINOR_8_pre30;
75bc8600 372 }
b5c6a6ae 373 } else if (protocol_version < 28)
b4a09b72 374 rdev = MAKEDEV(0, 0);
82ad07c4 375 if (preserve_uid) {
c7e6f84f 376 if (F_UID(file) == uid && *lastname)
82ad07c4 377 flags |= XMIT_SAME_UID;
c7e6f84f 378 else {
82ad07c4 379 uid = F_UID(file);
c7e6f84f
WD
380 if (preserve_uid && !numeric_ids) {
381 user_name = add_uid(uid);
3ea6e0e7 382 if (inc_recurse && user_name)
c7e6f84f
WD
383 flags |= XMIT_USER_NAME_FOLLOWS;
384 }
385 }
82ad07c4
WD
386 }
387 if (preserve_gid) {
c7e6f84f 388 if (F_GID(file) == gid && *lastname)
82ad07c4 389 flags |= XMIT_SAME_GID;
c7e6f84f 390 else {
82ad07c4 391 gid = F_GID(file);
c7e6f84f
WD
392 if (preserve_gid && !numeric_ids) {
393 group_name = add_gid(gid);
3ea6e0e7 394 if (inc_recurse && group_name)
c7e6f84f
WD
395 flags |= XMIT_GROUP_NAME_FOLLOWS;
396 }
397 }
82ad07c4 398 }
30f337c9 399 if (file->modtime == modtime)
d01d15e0 400 flags |= XMIT_SAME_TIME;
1ef00d20 401 else
30f337c9 402 modtime = file->modtime;
a289addd 403
4f5b0756 404#ifdef SUPPORT_HARD_LINKS
9d737ecb 405 if (tmp_dev != 0) {
8cae71eb 406 if (protocol_version >= 30) {
9d737ecb 407 struct idev_node *np = idev_node(tmp_dev, tmp_ino);
424d3691 408 first_hlink_ndx = (int32)(long)np->data - 1;
8cae71eb 409 if (first_hlink_ndx < 0) {
424d3691 410 np->data = (void*)(long)(ndx + 1);
8cae71eb
WD
411 flags |= XMIT_HLINK_FIRST;
412 }
413 flags |= XMIT_HLINKED;
414 } else {
9d737ecb 415 if (tmp_dev == dev) {
8cae71eb
WD
416 if (protocol_version >= 28)
417 flags |= XMIT_SAME_DEV_pre30;
418 } else
9d737ecb 419 dev = tmp_dev;
8cae71eb
WD
420 flags |= XMIT_HLINKED;
421 }
c4b4df4f 422 }
0d162bd1 423#endif
ebed4c3a
MP
424
425 for (l1 = 0;
3e491682
S
426 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
427 l1++) {}
eddd5d12 428 l2 = strlen(fname+l1);
72914a60 429
ebed4c3a 430 if (l1 > 0)
d01d15e0 431 flags |= XMIT_SAME_NAME;
ebed4c3a 432 if (l2 > 255)
d01d15e0 433 flags |= XMIT_LONG_NAME;
72914a60 434
1aa4caf3
WD
435 /* We must make sure we don't send a zero flag byte or the
436 * other end will terminate the flist transfer. Note that
ee3751c8 437 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
1aa4caf3 438 * it's harmless way to add a bit to the first flag byte. */
75bc8600 439 if (protocol_version >= 28) {
1aa4caf3 440 if (!flags && !S_ISDIR(mode))
ee3751c8 441 flags |= XMIT_TOP_DIR;
1aa4caf3 442 if ((flags & 0xFF00) || !flags) {
d01d15e0 443 flags |= XMIT_EXTENDED_FLAGS;
d521e1c2 444 write_shortint(f, flags);
75bc8600
WD
445 } else
446 write_byte(f, flags);
447 } else {
0a982011 448 if (!(flags & 0xFF))
75c51953 449 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
75bc8600
WD
450 write_byte(f, flags);
451 }
d01d15e0 452 if (flags & XMIT_SAME_NAME)
ebed4c3a 453 write_byte(f, l1);
d01d15e0 454 if (flags & XMIT_LONG_NAME)
f31514ad 455 write_varint30(f, l2);
72914a60 456 else
ebed4c3a
MP
457 write_byte(f, l2);
458 write_buf(f, fname + l1, l2);
72914a60 459
8cae71eb 460 if (first_hlink_ndx >= 0) {
f31514ad 461 write_varint30(f, first_hlink_ndx);
8cae71eb
WD
462 goto the_end;
463 }
464
ba59bd68
WD
465 write_varlong30(f, F_LENGTH(file), 3);
466 if (!(flags & XMIT_SAME_TIME)) {
467 if (protocol_version >= 30)
468 write_varlong(f, modtime, 4);
469 else
470 write_int(f, modtime);
471 }
d01d15e0 472 if (!(flags & XMIT_SAME_MODE))
30f337c9 473 write_int(f, to_wire_mode(mode));
d01d15e0 474 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
18438f0b
WD
475 if (protocol_version < 30)
476 write_int(f, uid);
477 else {
f31514ad 478 write_varint(f, uid);
18438f0b
WD
479 if (flags & XMIT_USER_NAME_FOLLOWS) {
480 int len = strlen(user_name);
481 write_byte(f, len);
482 write_buf(f, user_name, len);
483 }
c7e6f84f 484 }
72914a60 485 }
d01d15e0 486 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
18438f0b
WD
487 if (protocol_version < 30)
488 write_int(f, gid);
489 else {
f31514ad 490 write_varint(f, gid);
18438f0b
WD
491 if (flags & XMIT_GROUP_NAME_FOLLOWS) {
492 int len = strlen(group_name);
493 write_byte(f, len);
494 write_buf(f, group_name, len);
495 }
c7e6f84f 496 }
72914a60 497 }
b5c6a6ae
WD
498 if ((preserve_devices && IS_DEVICE(mode))
499 || (preserve_specials && IS_SPECIAL(mode))) {
9c5e91f8
WD
500 if (protocol_version < 28) {
501 if (!(flags & XMIT_SAME_RDEV_pre28))
502 write_int(f, (int)rdev);
503 } else {
504 if (!(flags & XMIT_SAME_RDEV_MAJOR))
f31514ad 505 write_varint30(f, major(rdev));
18438f0b 506 if (protocol_version >= 30)
f31514ad 507 write_varint(f, minor(rdev));
18438f0b 508 else if (flags & XMIT_RDEV_MINOR_8_pre30)
9c5e91f8
WD
509 write_byte(f, minor(rdev));
510 else
511 write_int(f, minor(rdev));
512 }
75bc8600 513 }
c627d613 514
4f5b0756 515#ifdef SUPPORT_LINKS
30f337c9 516 if (preserve_links && S_ISLNK(mode)) {
82ad07c4
WD
517 const char *sl = F_SYMLINK(file);
518 int len = strlen(sl);
f31514ad 519 write_varint30(f, len);
82ad07c4 520 write_buf(f, sl, len);
72914a60 521 }
c627d613
AT
522#endif
523
4f5b0756 524#ifdef SUPPORT_HARD_LINKS
9d737ecb 525 if (tmp_dev != 0 && protocol_version < 30) {
4124540d 526 if (protocol_version < 26) {
736a6a29 527 /* 32-bit dev_t and ino_t */
26404276 528 write_int(f, (int32)dev);
9d737ecb 529 write_int(f, (int32)tmp_ino);
736a6a29
MP
530 } else {
531 /* 64-bit dev_t and ino_t */
8cae71eb 532 if (!(flags & XMIT_SAME_DEV_pre30))
4124540d 533 write_longint(f, dev);
9d737ecb 534 write_longint(f, tmp_ino);
736a6a29 535 }
72914a60 536 }
dc5ddbcc
AT
537#endif
538
9a4a237e 539 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
4a19c3b2 540 const char *sum;
728d0922 541 if (S_ISREG(mode))
82ad07c4 542 sum = tmp_sum;
9a4a237e 543 else {
728d0922
WD
544 /* Prior to 28, we sent a useless set of nulls. */
545 sum = empty_sum;
728d0922 546 }
7752df41 547 write_buf(f, sum, checksum_len);
ebed4c3a 548 }
182dca5c 549
8cae71eb 550 the_end:
ebed4c3a 551 strlcpy(lastname, fname, MAXPATHLEN);
ca947dea
WD
552
553 if (S_ISREG(mode) || S_ISLNK(mode))
554 stats.total_size += F_LENGTH(file);
182dca5c
AT
555}
556
82ad07c4 557static struct file_struct *recv_file_entry(struct file_list *flist,
99a957d3 558 int flags, int f)
182dca5c 559{
f14b3ef4 560 static int64 modtime;
5911fee5 561 static mode_t mode;
1490812a 562 static int64 dev;
4124540d 563 static dev_t rdev;
9c5e91f8 564 static uint32 rdev_major;
5911fee5
WD
565 static uid_t uid;
566 static gid_t gid;
a289addd 567 static char lastname[MAXPATHLEN], *lastdir;
33ffd7c3 568 static int lastdir_depth, lastdir_len = -1;
42f23f47 569 static unsigned int del_hier_name_len = 0;
649f8742 570 static int in_del_hier = 0;
72914a60 571 char thisname[MAXPATHLEN];
ebed4c3a 572 unsigned int l1 = 0, l2 = 0;
c7e6f84f 573 int alloc_len, basename_len, linkname_len;
99a957d3 574 int extra_len = file_extra_cnt * EXTRA_LEN;
8cae71eb 575 int first_hlink_ndx = -1;
a289addd 576 OFF_T file_length;
c7e6f84f 577 const char *basename;
72914a60 578 struct file_struct *file;
ef35abb2
WD
579 alloc_pool_t *pool;
580 char *bp;
72914a60 581
d01d15e0 582 if (flags & XMIT_SAME_NAME)
72914a60 583 l1 = read_byte(f);
ebed4c3a 584
d01d15e0 585 if (flags & XMIT_LONG_NAME)
f31514ad 586 l2 = read_varint30(f);
72914a60
AT
587 else
588 l2 = read_byte(f);
589
ebed4c3a
MP
590 if (l2 >= MAXPATHLEN - l1) {
591 rprintf(FERROR,
82ad07c4
WD
592 "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
593 flags, l1, l2, lastname, who_am_i());
594 overflow_exit("recv_file_entry");
d0fd26aa 595 }
72914a60 596
ebed4c3a
MP
597 strlcpy(thisname, lastname, l1 + 1);
598 read_sbuf(f, &thisname[l1], l2);
599 thisname[l1 + l2] = 0;
72914a60 600
ebed4c3a 601 strlcpy(lastname, thisname, MAXPATHLEN);
72914a60 602
58b1999e 603 clean_fname(thisname, 0);
72914a60 604
0d162bd1 605 if (sanitize_paths)
a2248aea 606 sanitize_path(thisname, thisname, "", 0, NULL);
cb13abfe 607
a289addd 608 if ((basename = strrchr(thisname, '/')) != NULL) {
c7e6f84f
WD
609 int len = basename++ - thisname;
610 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
611 lastdir = new_array(char, len + 1);
612 memcpy(lastdir, thisname, len);
613 lastdir[len] = '\0';
614 lastdir_len = len;
615 lastdir_depth = count_dir_elements(lastdir);
616 }
617 } else
a289addd 618 basename = thisname;
a289addd 619 basename_len = strlen(basename) + 1; /* count the '\0' */
72914a60 620
8cae71eb
WD
621#ifdef SUPPORT_HARD_LINKS
622 if (protocol_version >= 30
623 && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
624 struct file_struct *first;
f31514ad 625 first_hlink_ndx = read_varint30(f);
8cae71eb
WD
626 if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
627 rprintf(FERROR,
628 "hard-link reference out of range: %d (%d)\n",
629 first_hlink_ndx, flist->count);
630 exit_cleanup(RERR_PROTOCOL);
631 }
632 first = flist->files[first_hlink_ndx];
633 file_length = F_LENGTH(first);
634 modtime = first->modtime;
635 mode = first->mode;
636 if (preserve_uid)
637 uid = F_UID(first);
638 if (preserve_gid)
639 gid = F_GID(first);
640 if ((preserve_devices && IS_DEVICE(mode))
641 || (preserve_specials && IS_SPECIAL(mode))) {
642 uint32 *devp = F_RDEV_P(first);
643 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
057f649f 644 extra_len += 2 * EXTRA_LEN;
8cae71eb
WD
645 }
646 if (preserve_links && S_ISLNK(mode))
647 linkname_len = strlen(F_SYMLINK(first)) + 1;
648 else
649 linkname_len = 0;
650 goto create_object;
651 }
652#endif
653
ba59bd68
WD
654 file_length = read_varlong30(f, 3);
655 if (!(flags & XMIT_SAME_TIME)) {
f14b3ef4
WD
656 if (protocol_version >= 30) {
657 modtime = read_varlong(f, 4);
658#if SIZEOF_TIME_T < SIZEOF_INT64
659 if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) {
660 rprintf(FERROR,
661 "Time value of %s truncated on receiver.\n",
662 lastname);
663 }
664#endif
665 } else
666 modtime = read_int(f);
ba59bd68 667 }
d01d15e0 668 if (!(flags & XMIT_SAME_MODE))
30f337c9 669 mode = from_wire_mode(read_int(f));
1ef00d20 670
ebec5eb6 671 if (chmod_modes && !S_ISLNK(mode))
8bbe41b5
WD
672 mode = tweak_mode(mode, chmod_modes);
673
c7e6f84f 674 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
18438f0b
WD
675 if (protocol_version < 30)
676 uid = (uid_t)read_int(f);
677 else {
f31514ad 678 uid = (uid_t)read_varint(f);
18438f0b
WD
679 if (flags & XMIT_USER_NAME_FOLLOWS)
680 uid = recv_user_name(f, uid);
681 else if (inc_recurse && am_root && !numeric_ids)
682 uid = match_uid(uid);
683 }
c7e6f84f
WD
684 }
685 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
18438f0b
WD
686 if (protocol_version < 30)
687 gid = (gid_t)read_int(f);
688 else {
f31514ad 689 gid = (gid_t)read_varint(f);
18438f0b
WD
690 if (flags & XMIT_GROUP_NAME_FOLLOWS)
691 gid = recv_group_name(f, gid);
692 else if (inc_recurse && (!am_root || !numeric_ids))
693 gid = match_gid(gid);
694 }
c7e6f84f 695 }
a289addd 696
b5c6a6ae
WD
697 if ((preserve_devices && IS_DEVICE(mode))
698 || (preserve_specials && IS_SPECIAL(mode))) {
75bc8600 699 if (protocol_version < 28) {
b5c6a6ae
WD
700 if (!(flags & XMIT_SAME_RDEV_pre28))
701 rdev = (dev_t)read_int(f);
702 } else {
9c5e91f8
WD
703 uint32 rdev_minor;
704 if (!(flags & XMIT_SAME_RDEV_MAJOR))
f31514ad 705 rdev_major = read_varint30(f);
18438f0b 706 if (protocol_version >= 30)
f31514ad 707 rdev_minor = read_varint(f);
18438f0b 708 else if (flags & XMIT_RDEV_MINOR_8_pre30)
9c5e91f8
WD
709 rdev_minor = read_byte(f);
710 else
711 rdev_minor = read_int(f);
b4a09b72 712 rdev = MAKEDEV(rdev_major, rdev_minor);
1ef00d20 713 }
96293cf9
WD
714 extra_len += 2 * EXTRA_LEN;
715 file_length = 0;
b5c6a6ae 716 } else if (protocol_version < 28)
b4a09b72 717 rdev = MAKEDEV(0, 0);
72914a60 718
4f5b0756 719#ifdef SUPPORT_LINKS
30f337c9 720 if (preserve_links && S_ISLNK(mode)) {
f31514ad 721 linkname_len = read_varint30(f) + 1; /* count the '\0' */
a289addd
WD
722 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
723 rprintf(FERROR, "overflow: linkname_len=%d\n",
724 linkname_len - 1);
82ad07c4 725 overflow_exit("recv_file_entry");
9dd891bb 726 }
72914a60 727 }
a289addd 728 else
0d162bd1 729#endif
a289addd 730 linkname_len = 0;
0d162bd1 731
82ad07c4 732#ifdef SUPPORT_HARD_LINKS
8cae71eb 733 create_object:
4785cd43
WD
734 if (preserve_hard_links) {
735 if (protocol_version < 28 && S_ISREG(mode))
c7565dad
WD
736 flags |= XMIT_HLINKED;
737 if (flags & XMIT_HLINKED)
4785cd43 738 extra_len += EXTRA_LEN;
82ad07c4
WD
739 }
740#endif
741
1c3344a1
WD
742#ifdef SUPPORT_ACLS
743 /* We need one or two index int32s when we're preserving ACLs. */
744 if (preserve_acls)
745 extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
746#endif
747
96293cf9
WD
748 if (always_checksum && S_ISREG(mode))
749 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
a289addd 750
96293cf9
WD
751 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
752 extra_len += EXTRA_LEN;
753
a3e18c76
WD
754#if EXTRA_ROUNDING > 0
755 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
756 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
757#endif
758
3ea6e0e7 759 if (inc_recurse && S_ISDIR(mode)) {
c7e6f84f
WD
760 if (one_file_system) {
761 /* Room to save the dir's device for -x */
762 extra_len += 2 * EXTRA_LEN;
763 }
ef35abb2
WD
764 pool = dir_flist->file_pool;
765 } else
766 pool = flist->file_pool;
c7e6f84f
WD
767
768 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
96293cf9 769 + linkname_len;
ef35abb2 770 bp = pool_alloc(pool, alloc_len, "recv_file_entry");
9935066b 771
a3e18c76 772 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
82ad07c4 773 bp += extra_len;
f5db0993 774 file = (struct file_struct *)bp;
96293cf9
WD
775 bp += FILE_STRUCT_LEN;
776
777 memcpy(bp, basename, basename_len);
778 bp += basename_len + linkname_len; /* skip space for symlink too */
a289addd 779
82ad07c4 780#ifdef SUPPORT_HARD_LINKS
c7565dad 781 if (flags & XMIT_HLINKED)
96293cf9 782 file->flags |= FLAG_HLINKED;
82ad07c4 783#endif
f14b3ef4 784 file->modtime = (time_t)modtime;
60af9465 785 file->len32 = (uint32)file_length;
96293cf9
WD
786 if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
787 file->flags |= FLAG_LENGTH64;
788 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
789 }
a289addd 790 file->mode = mode;
82ad07c4 791 if (preserve_uid)
f1482c33 792 F_OWNER(file) = uid;
82ad07c4 793 if (preserve_gid)
f1482c33 794 F_GROUP(file) = gid;
a289addd 795
c7e6f84f
WD
796 if (basename != thisname) {
797 file->dirname = lastdir;
9d737ecb 798 F_DEPTH(file) = lastdir_depth + 1;
f3c3ed44 799 } else
9d737ecb 800 F_DEPTH(file) = 1;
f3c3ed44 801
649f8742 802 if (S_ISDIR(mode)) {
ee3751c8 803 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
9d737ecb 804 F_DEPTH(file)--;
ee3751c8 805 if (flags & XMIT_TOP_DIR) {
75c51953 806 in_del_hier = recurse;
9d737ecb 807 del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
7a16e122 808 if (relative_paths && del_hier_name_len > 2
9cdadbb1
WD
809 && lastname[del_hier_name_len-1] == '.'
810 && lastname[del_hier_name_len-2] == '/')
7a16e122 811 del_hier_name_len -= 2;
82ad07c4 812 file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR;
ee3751c8 813 } else if (in_del_hier) {
f3c3ed44
WD
814 if (!relative_paths || !del_hier_name_len
815 || (l1 >= del_hier_name_len
9cdadbb1 816 && lastname[del_hier_name_len] == '/'))
82ad07c4 817 file->flags |= FLAG_XFER_DIR;
649f8742
WD
818 else
819 in_del_hier = 0;
820 }
821 }
822
b5c6a6ae 823 if ((preserve_devices && IS_DEVICE(mode))
82ad07c4 824 || (preserve_specials && IS_SPECIAL(mode))) {
4785cd43
WD
825 uint32 *devp = F_RDEV_P(file);
826 DEV_MAJOR(devp) = major(rdev);
827 DEV_MINOR(devp) = minor(rdev);
82ad07c4 828 }
a289addd 829
4f5b0756 830#ifdef SUPPORT_LINKS
a289addd 831 if (linkname_len) {
c58c1dc4 832 bp = (char*)file->basename + basename_len;
8cae71eb
WD
833 if (first_hlink_ndx >= 0) {
834 struct file_struct *first = flist->files[first_hlink_ndx];
835 memcpy(bp, F_SYMLINK(first), linkname_len);
836 } else
837 read_sbuf(f, bp, linkname_len - 1);
1a7f3d99 838 if (sanitize_paths)
dcebf78f 839 sanitize_path(bp, bp, "", lastdir_depth, NULL);
a289addd
WD
840 }
841#endif
842
4f5b0756 843#ifdef SUPPORT_HARD_LINKS
c7565dad 844 if (preserve_hard_links && flags & XMIT_HLINKED) {
8cae71eb
WD
845 if (protocol_version >= 30) {
846 F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST
847 ? flist->count : first_hlink_ndx;
736a6a29 848 } else {
9d737ecb
WD
849 static int32 cnt = 0;
850 struct idev_node *np;
851 int64 ino;
852 int32 ndx;
8cae71eb 853 if (protocol_version < 26) {
9d737ecb
WD
854 dev = read_int(f);
855 ino = read_int(f);
8cae71eb
WD
856 } else {
857 if (!(flags & XMIT_SAME_DEV_pre30))
858 dev = read_longint(f);
9d737ecb 859 ino = read_longint(f);
8cae71eb 860 }
9d737ecb 861 np = idev_node(dev, ino);
424d3691 862 ndx = (int32)(long)np->data - 1;
9d737ecb
WD
863 if (ndx < 0) {
864 ndx = cnt++;
424d3691 865 np->data = (void*)(long)cnt;
9d737ecb
WD
866 }
867 F_HL_GNUM(file) = ndx;
736a6a29 868 }
72914a60 869 }
dc5ddbcc 870#endif
ebed4c3a 871
96293cf9
WD
872 if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
873 if (S_ISREG(mode))
874 bp = (char*)F_SUM(file);
82ad07c4 875 else {
fea4db62 876 /* Prior to 28, we get a useless set of nulls. */
82ad07c4 877 bp = tmp_sum;
fea4db62 878 }
8cae71eb
WD
879 if (first_hlink_ndx >= 0) {
880 struct file_struct *first = flist->files[first_hlink_ndx];
881 memcpy(bp, F_SUM(first), checksum_len);
882 } else
883 read_buf(f, bp, checksum_len);
72914a60 884 }
ebed4c3a 885
1c3344a1
WD
886#ifdef SUPPORT_ACLS
887 if (preserve_acls && !S_ISLNK(mode))
888 receive_acl(file, f);
889#endif
16edf865
WD
890#ifdef SUPPORT_XATTRS
891 if (preserve_xattrs)
892 receive_xattr(file, f );
893#endif
1c3344a1 894
ca947dea
WD
895 if (S_ISREG(mode) || S_ISLNK(mode))
896 stats.total_size += file_length;
897
f5db0993 898 return file;
c627d613
AT
899}
900
db719fb0
MP
901/**
902 * Create a file_struct for a named file by reading its stat()
903 * information and performing extensive checks against global
904 * options.
905 *
906 * @return the new file, or NULL if there was an error or this file
907 * should be excluded.
908 *
909 * @todo There is a small optimization opportunity here to avoid
910 * stat()ing the file in some circumstances, which has a certain cost.
911 * We are called immediately after doing readdir(), and so we may
912 * already know the d_type of the file. We could for example avoid
913 * statting directories if we're not recursing, but this is not a very
914 * important case. Some systems may not have d_type.
915 **/
45d8bfe0 916struct file_struct *make_file(const char *fname, struct file_list *flist,
82ad07c4 917 STRUCT_STAT *stp, int flags, int filter_level)
c627d613 918{
a289addd
WD
919 static char *lastdir;
920 static int lastdir_len = -1;
3ec4dd97 921 struct file_struct *file;
bcacc18b 922 STRUCT_STAT st;
1923b1fc 923 char thisname[MAXPATHLEN];
e0870f1d 924 char linkname[MAXPATHLEN];
c7e6f84f 925 int alloc_len, basename_len, linkname_len;
99a957d3 926 int extra_len = file_extra_cnt * EXTRA_LEN;
c7e6f84f
WD
927 const char *basename;
928 char *bp;
9935066b 929
1923b1fc 930 if (strlcpy(thisname, fname, sizeof thisname)
ef35abb2 931 >= sizeof thisname - pathname_len) {
0ee6ca98 932 rprintf(FINFO, "skipping overly long name: %s\n", fname);
882e6893
WD
933 return NULL;
934 }
58b1999e 935 clean_fname(thisname, 0);
b7736c79 936 if (sanitize_paths)
a2248aea 937 sanitize_path(thisname, thisname, "", 0, NULL);
3ec4dd97 938
e4fdf1de 939 if (stp && S_ISDIR(stp->st_mode)) {
ede1f0eb 940 st = *stp; /* Needed for "symlink/." with --relative. */
e4fdf1de
WD
941 *linkname = '\0'; /* make IBM code checker happy */
942 } else if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 943 int save_errno = errno;
a4a7e64c 944 /* See if file is excluded before reporting an error. */
7842418b 945 if (filter_level != NO_FILTERS
60cc01a6 946 && (is_excluded(thisname, 0, filter_level)
53039410
WD
947 || is_excluded(thisname, 1, filter_level))) {
948 if (ignore_perishable && save_errno != ENOENT)
949 non_perishable_cnt++;
a4a7e64c 950 return NULL;
53039410 951 }
a4a7e64c 952 if (save_errno == ENOENT) {
4f5b0756 953#ifdef SUPPORT_LINKS
a4a7e64c 954 /* Avoid "vanished" error if symlink points nowhere. */
9439c0cb 955 if (copy_links && x_lstat(thisname, &st, NULL) == 0
a4a7e64c
WD
956 && S_ISLNK(st.st_mode)) {
957 io_error |= IOERR_GENERAL;
958 rprintf(FERROR, "symlink has no referent: %s\n",
959 full_fname(thisname));
e5ce3bcf
WD
960 } else
961#endif
962 {
a4a7e64c
WD
963 enum logcode c = am_daemon && protocol_version < 28
964 ? FERROR : FINFO;
965 io_error |= IOERR_VANISHED;
966 rprintf(c, "file has vanished: %s\n",
967 full_fname(thisname));
76e26e10 968 }
a4a7e64c 969 } else {
a8726d2a 970 io_error |= IOERR_GENERAL;
d62bcc17
WD
971 rsyserr(FERROR, save_errno, "readlink %s failed",
972 full_fname(thisname));
76e26e10 973 }
3ec4dd97
AT
974 return NULL;
975 }
c627d613 976
7842418b
WD
977 /* backup.c calls us with filter_level set to NO_FILTERS. */
978 if (filter_level == NO_FILTERS)
979 goto skip_filters;
ac1a0994 980
7e037c42 981 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
0ee6ca98 982 rprintf(FINFO, "skipping directory %s\n", thisname);
3ec4dd97
AT
983 return NULL;
984 }
ebed4c3a 985
d521e1c2 986 /* -x only affects directories because we need to avoid recursing
e90cdb8a
WD
987 * into a mount-point directory, not to avoid copying a symlinked
988 * file if -L (or similar) was specified. */
535737bf
WD
989 if (one_file_system && st.st_dev != filesystem_dev
990 && S_ISDIR(st.st_mode)) {
991 if (one_file_system > 1) {
992 if (verbose > 2) {
993 rprintf(FINFO, "skipping mount-point dir %s\n",
994 thisname);
ebec5eb6 995 }
535737bf 996 return NULL;
ebec5eb6 997 }
82ad07c4 998 flags |= FLAG_MOUNT_DIR;
3b173846 999 }
ebed4c3a 1000
53039410
WD
1001 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1002 if (ignore_perishable)
1003 non_perishable_cnt++;
76e26e10 1004 return NULL;
53039410 1005 }
76e26e10 1006
132fcf36 1007 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 1008#ifdef SUPPORT_LINKS
132fcf36
WD
1009 if (!S_ISLNK(st.st_mode))
1010#endif
1011 if (access(thisname, R_OK) != 0)
1012 return NULL;
1013 }
ac1a0994 1014
97b7bff4 1015 skip_filters:
ac1a0994 1016
c7e6f84f
WD
1017 /* Only divert a directory in the main transfer. */
1018 if (flist && flist->prev && S_ISDIR(st.st_mode)
1019 && flags & FLAG_DIVERT_DIRS) {
1020 flist = dir_flist;
1021 /* Room for parent/sibling/next-child info. */
1022 extra_len += 3 * EXTRA_LEN;
1023 }
1024
ea847c62
WD
1025 if (verbose > 2) {
1026 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
0ee6ca98 1027 who_am_i(), thisname, filter_level);
ea847c62 1028 }
ebed4c3a 1029
a289addd 1030 if ((basename = strrchr(thisname, '/')) != NULL) {
c7e6f84f
WD
1031 int len = basename++ - thisname;
1032 if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1033 lastdir = new_array(char, len + 1);
1034 memcpy(lastdir, thisname, len);
1035 lastdir[len] = '\0';
1036 lastdir_len = len;
1037 }
1038 } else
a289addd 1039 basename = thisname;
a289addd 1040 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 1041
4f5b0756 1042#ifdef SUPPORT_LINKS
a289addd
WD
1043 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1044#else
1045 linkname_len = 0;
1046#endif
1047
96293cf9
WD
1048 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1049 extra_len += EXTRA_LEN;
1050
a3e18c76
WD
1051#if EXTRA_ROUNDING > 0
1052 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1053 extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1054#endif
1055
c7e6f84f 1056 alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
96293cf9 1057 + linkname_len;
99aaa6ca
WD
1058 if (flist)
1059 bp = pool_alloc(flist->file_pool, alloc_len, "make_file");
1060 else {
9935066b 1061 if (!(bp = new_array(char, alloc_len)))
99aaa6ca 1062 out_of_memory("make_file");
9935066b
S
1063 }
1064
a3e18c76 1065 memset(bp, 0, extra_len + FILE_STRUCT_LEN);
82ad07c4 1066 bp += extra_len;
a289addd 1067 file = (struct file_struct *)bp;
96293cf9
WD
1068 bp += FILE_STRUCT_LEN;
1069
1070 memcpy(bp, basename, basename_len);
1071 bp += basename_len + linkname_len; /* skip space for symlink too */
82ad07c4
WD
1072
1073#ifdef SUPPORT_HARD_LINKS
c7e6f84f 1074 if (preserve_hard_links && flist && flist->prev) {
82ad07c4
WD
1075 if (protocol_version >= 28
1076 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1077 : S_ISREG(st.st_mode)) {
9d737ecb
WD
1078 tmp_dev = st.st_dev;
1079 tmp_ino = st.st_ino;
82ad07c4 1080 } else
9d737ecb 1081 tmp_dev = 0;
82ad07c4
WD
1082 }
1083#endif
a289addd 1084
96293cf9
WD
1085#ifdef HAVE_STRUCT_STAT_ST_RDEV
1086 if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
1087 tmp_rdev = st.st_rdev;
1088 st.st_size = 0;
1089 }
1090#endif
1091
a289addd 1092 file->flags = flags;
3ec4dd97 1093 file->modtime = st.st_mtime;
60af9465 1094 file->len32 = (uint32)st.st_size;
96293cf9
WD
1095 if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1096 file->flags |= FLAG_LENGTH64;
1097 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
1098 }
8bbe41b5 1099 file->mode = st.st_mode;
82ad07c4 1100 if (preserve_uid)
f1482c33 1101 F_OWNER(file) = st.st_uid;
82ad07c4 1102 if (preserve_gid)
f1482c33 1103 F_GROUP(file) = st.st_gid;
61dec11a 1104
c7e6f84f
WD
1105 if (basename != thisname)
1106 file->dirname = lastdir;
a289addd 1107
4f5b0756 1108#ifdef SUPPORT_LINKS
96293cf9 1109 if (linkname_len) {
c58c1dc4 1110 bp = (char*)file->basename + basename_len;
96293cf9
WD
1111 memcpy(bp, linkname, linkname_len);
1112 }
0d162bd1
WD
1113#endif
1114
82ad07c4
WD
1115 if (always_checksum && am_sender && S_ISREG(st.st_mode))
1116 file_checksum(thisname, tmp_sum, st.st_size);
c627d613 1117
ef35abb2 1118 F_PATHNAME(file) = pathname;
c627d613 1119
23f4587f
WD
1120 /* This code is only used by the receiver when it is building
1121 * a list of files for a delete pass. */
1122 if (keep_dirlinks && linkname_len && flist) {
1123 STRUCT_STAT st2;
f5db0993 1124 int save_mode = file->mode;
8bbe41b5 1125 file->mode = S_IFDIR; /* Find a directory with our name. */
c7e6f84f 1126 if (flist_find(dir_flist, file) >= 0
9439c0cb 1127 && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
23f4587f 1128 file->modtime = st2.st_mtime;
96293cf9 1129 file->len32 = 0;
23f4587f 1130 file->mode = st2.st_mode;
82ad07c4 1131 if (preserve_uid)
f1482c33 1132 F_OWNER(file) = st2.st_uid;
82ad07c4 1133 if (preserve_gid)
f1482c33 1134 F_GROUP(file) = st2.st_gid;
f5db0993
WD
1135 } else
1136 file->mode = save_mode;
23f4587f
WD
1137 }
1138
82ad07c4
WD
1139 if (basename_len == 0+1)
1140 return NULL;
1141
3ea6e0e7 1142 if (inc_recurse && flist == dir_flist) {
c7e6f84f
WD
1143 flist_expand(flist);
1144 flist->files[flist->count++] = file;
1145 }
1146
3ec4dd97 1147 return file;
c627d613
AT
1148}
1149
17026f27 1150/* Only called for temporary file_struct entries created by make_file(). */
82ad07c4
WD
1151void unmake_file(struct file_struct *file)
1152{
99a957d3 1153 int extra_cnt = file_extra_cnt + LEN64_BUMP(file);
a3e18c76
WD
1154#if EXTRA_ROUNDING > 0
1155 if (extra_cnt & EXTRA_ROUNDING)
1156 extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1;
1157#endif
1158 free(REQ_EXTRA(file, extra_cnt));
82ad07c4
WD
1159}
1160
42c6b139 1161static struct file_struct *send_file_name(int f, struct file_list *flist,
ede1f0eb 1162 char *fname, STRUCT_STAT *stp,
c7e6f84f 1163 int flags, int filter_flags)
c627d613 1164{
ebed4c3a 1165 struct file_struct *file;
16edf865 1166#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1c3344a1
WD
1167 statx sx;
1168#endif
ebed4c3a 1169
c7e6f84f 1170 file = make_file(fname, flist, stp, flags, filter_flags);
37802f40 1171 if (!file)
301fb56c 1172 return NULL;
ebed4c3a 1173
ebec5eb6 1174 if (chmod_modes && !S_ISLNK(file->mode))
8bbe41b5
WD
1175 file->mode = tweak_mode(file->mode, chmod_modes);
1176
1c3344a1
WD
1177#ifdef SUPPORT_ACLS
1178 if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) {
1179 sx.st.st_mode = file->mode;
1180 sx.acc_acl = sx.def_acl = NULL;
1181 if (get_acl(fname, &sx) < 0)
1182 return NULL;
1183 }
1184#endif
16edf865
WD
1185#ifdef SUPPORT_XATTRS
1186 if (preserve_xattrs && f >= 0) {
1187 sx.xattr = NULL;
1188 if (get_xattr(fname, &sx) < 0)
1189 return NULL;
1190 }
1191#endif
1c3344a1 1192
53135fe8 1193 maybe_emit_filelist_progress(flist->count + flist_count_offset);
ebed4c3a 1194
d9d6bc52 1195 flist_expand(flist);
82ad07c4 1196 flist->files[flist->count++] = file;
1c3344a1 1197 if (f >= 0) {
42c6b139 1198 send_file_entry(f, file, flist->count - 1);
1c3344a1
WD
1199#ifdef SUPPORT_ACLS
1200 if (preserve_acls && !S_ISLNK(file->mode)) {
1201 send_acl(&sx, f);
1202 free_acl(&sx);
1203 }
16edf865
WD
1204#endif
1205#ifdef SUPPORT_XATTRS
1206 if (preserve_xattrs) {
1207 F_XATTR(file) = send_xattr(&sx, f);
1208 free_xattr(&sx);
1209 }
1c3344a1
WD
1210#endif
1211 }
301fb56c
WD
1212 return file;
1213}
ebed4c3a 1214
301fb56c 1215static void send_if_directory(int f, struct file_list *flist,
56f0c976 1216 struct file_struct *file,
c7e6f84f
WD
1217 char *fbuf, unsigned int ol,
1218 int flags)
301fb56c 1219{
56f0c976 1220 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
301fb56c
WD
1221
1222 if (S_ISDIR(file->mode)
82ad07c4 1223 && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
f1773e09
WD
1224 void *save_filters;
1225 unsigned int len = strlen(fbuf);
1226 if (len > 1 && fbuf[len-1] == '/')
1227 fbuf[--len] = '\0';
1228 if (len >= MAXPATHLEN - 1) {
1229 io_error |= IOERR_GENERAL;
1230 rprintf(FERROR, "skipping long-named directory: %s\n",
1231 full_fname(fbuf));
1232 return;
1233 }
1234 save_filters = push_local_filters(fbuf, len);
c7e6f84f 1235 send_directory(f, flist, -1, fbuf, len, flags);
f1773e09 1236 pop_local_filters(save_filters);
56f0c976
WD
1237 fbuf[ol] = '\0';
1238 if (is_dot_dir)
1239 fbuf[ol-1] = '.';
ebed4c3a 1240 }
c627d613
AT
1241}
1242
c7e6f84f
WD
1243static int file_compare(struct file_struct **file1, struct file_struct **file2)
1244{
1245 return f_name_cmp(*file1, *file2);
1246}
1247
1248/* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end),
1249 * sort them by name, and link them into the tree, setting the appropriate
1250 * parent/child/sibling pointers. */
1251static void add_dirs_to_tree(int parent_ndx, int start, int end)
1252{
1253 int i;
1254 int32 *dp = NULL;
1255 int32 *parent_dp = parent_ndx < 0 ? NULL
1256 : F_DIRNODE_P(dir_flist->files[parent_ndx]);
1257
1258 qsort(dir_flist->files + start, end - start + 1,
1259 sizeof dir_flist->files[0], (int (*)())file_compare);
1260
1261 for (i = start; i <= end; i++) {
1262 struct file_struct *file = dir_flist->files[i];
1263 if (!(file->flags & FLAG_XFER_DIR)
1264 || file->flags & FLAG_MOUNT_DIR)
1265 continue;
1266 if (dp)
1267 DIR_NEXT_SIBLING(dp) = i;
1268 else if (parent_dp)
1269 DIR_FIRST_CHILD(parent_dp) = i;
1270 else
1271 send_dir_ndx = i;
1272 dp = F_DIRNODE_P(file);
1273 DIR_PARENT(dp) = parent_ndx;
1274 DIR_FIRST_CHILD(dp) = -1;
1275 }
1276 if (dp)
1277 DIR_NEXT_SIBLING(dp) = -1;
1278}
1279
301fb56c 1280/* This function is normally called by the sender, but the receiving side also
7b558d7f 1281 * calls it from get_dirlist() with f set to -1 so that we just construct the
301fb56c
WD
1282 * file list in memory without sending it over the wire. Also, get_dirlist()
1283 * might call this with f set to -2, which also indicates that local filter
1284 * rules should be ignored. */
c7e6f84f
WD
1285static void send_directory(int f, struct file_list *flist, int parent_ndx,
1286 char *fbuf, int len, int flags)
c627d613 1287{
3ec4dd97 1288 struct dirent *di;
32cbfe7b 1289 unsigned remainder;
3ec4dd97 1290 char *p;
f1773e09 1291 DIR *d;
c7e6f84f
WD
1292 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1293 int start = divert_dirs ? dir_flist->count : flist->count;
1294 int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
c7e6f84f
WD
1295
1296 assert(flist != NULL);
3ec4dd97 1297
f1773e09 1298 if (!(d = opendir(fbuf))) {
06c28400 1299 io_error |= IOERR_GENERAL;
f1773e09 1300 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1301 return;
1302 }
c627d613 1303
19b2a5d9
WD
1304 p = fbuf + len;
1305 if (len != 1 || *fbuf != '/')
eddd5d12 1306 *p++ = '/';
f1773e09 1307 *p = '\0';
32cbfe7b 1308 remainder = MAXPATHLEN - (p - fbuf);
ebed4c3a 1309
6a7cc46c 1310 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1311 char *dname = d_name(di);
6a7cc46c
S
1312 if (dname[0] == '.' && (dname[1] == '\0'
1313 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1314 continue;
0ee6ca98 1315 if (strlcpy(p, dname, remainder) >= remainder) {
eddd5d12
WD
1316 io_error |= IOERR_GENERAL;
1317 rprintf(FINFO,
1318 "cannot send long-named file %s\n",
f1773e09 1319 full_fname(fbuf));
beaf4954 1320 continue;
eddd5d12 1321 }
0ee6ca98 1322
42c6b139 1323 send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
3ec4dd97 1324 }
32cbfe7b
WD
1325
1326 fbuf[len] = '\0';
1327
6a7cc46c 1328 if (errno) {
06c28400 1329 io_error |= IOERR_GENERAL;
71903f60 1330 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1331 }
c627d613 1332
3ec4dd97 1333 closedir(d);
301fb56c 1334
c7e6f84f
WD
1335 if (f < 0)
1336 return;
1337
1338 if (divert_dirs)
1339 add_dirs_to_tree(parent_ndx, start, dir_flist->count - 1);
1340 else if (recurse) {
301fb56c 1341 int i, end = flist->count - 1;
8cae71eb 1342 /* send_if_directory() bumps flist->count, so use "end". */
301fb56c 1343 for (i = start; i <= end; i++)
c7e6f84f
WD
1344 send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1345 }
1346}
1347
1348void send_extra_file_list(int f, int at_least)
1349{
1350 char fbuf[MAXPATHLEN];
1351 struct file_list *flist;
1352 int64 start_write;
7a2fa0c2 1353 int future_cnt, save_io_error = io_error;
c7e6f84f 1354
25c2a6ac 1355 if (flist_eof)
c7e6f84f
WD
1356 return;
1357
1358 /* Keep sending data until we have the requested number of
1359 * files in the upcoming file-lists. */
7a2fa0c2
WD
1360 if (cur_flist->next) {
1361 flist = first_flist->prev; /* the newest flist */
ef35abb2
WD
1362 future_cnt = flist->count + flist->ndx_start
1363 - cur_flist->next->ndx_start;
7a2fa0c2
WD
1364 } else
1365 future_cnt = 0;
1366 while (future_cnt < at_least) {
c7e6f84f
WD
1367 struct file_struct *file = dir_flist->files[send_dir_ndx];
1368 int32 *dp;
1369 int dlen;
1370
1371 f_name(file, fbuf);
1372 dlen = strlen(fbuf);
1373
ef35abb2
WD
1374 if (F_PATHNAME(file) != pathname) {
1375 if (!push_pathname(F_PATHNAME(file), -1))
c7e6f84f
WD
1376 exit_cleanup(RERR_FILESELECT);
1377 }
1378
1379 flist = flist_new(0, "send_extra_file_list");
4c9d5fef 1380 start_write = stats.total_written;
c7e6f84f 1381
9ae7a2cd 1382 write_ndx(f, NDX_FLIST_OFFSET - send_dir_ndx);
c7e6f84f 1383 change_local_filter_dir(fbuf, dlen, send_dir_depth);
ef35abb2
WD
1384 send_directory(f, flist, send_dir_ndx, fbuf, dlen,
1385 FLAG_DIVERT_DIRS | FLAG_XFER_DIR);
c7e6f84f
WD
1386 write_byte(f, 0);
1387
1388 clean_flist(flist, 0, 0);
1389 file_total += flist->count;
7a2fa0c2 1390 future_cnt += flist->count;
c7e6f84f
WD
1391 stats.flist_size += stats.total_written - start_write;
1392 stats.num_files += flist->count;
1393 if (verbose > 3)
1394 output_flist(flist);
1395
1396 dp = F_DIRNODE_P(file);
1397 if (DIR_FIRST_CHILD(dp) >= 0) {
1398 send_dir_ndx = DIR_FIRST_CHILD(dp);
1399 send_dir_depth++;
1400 } else {
1401 while (DIR_NEXT_SIBLING(dp) < 0) {
1402 if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
9ae7a2cd 1403 write_ndx(f, NDX_FLIST_EOF);
c7e6f84f
WD
1404 flist_eof = 1;
1405 change_local_filter_dir(NULL, 0, 0);
1406 goto finish;
1407 }
1408 send_dir_depth--;
1409 file = dir_flist->files[send_dir_ndx];
1410 dp = F_DIRNODE_P(file);
1411 }
1412 send_dir_ndx = DIR_NEXT_SIBLING(dp);
1413 }
301fb56c 1414 }
c7e6f84f
WD
1415
1416 finish:
1417 if (io_error != save_io_error && !ignore_errors)
1418 send_msg_int(MSG_IO_ERROR, io_error);
c627d613
AT
1419}
1420
ebed4c3a 1421struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1422{
3bb88b43
WD
1423 static const char *lastdir;
1424 static int lastdir_len = -1;
1425 int len, dirlen;
bcacc18b 1426 STRUCT_STAT st;
c7e6f84f 1427 char *p, *dir;
ebed4c3a 1428 char lastpath[MAXPATHLEN] = "";
649d65ed 1429 struct file_list *flist;
31b4d25d 1430 struct timeval start_tv, end_tv;
a800434a 1431 int64 start_write;
24d0fcde 1432 int use_ff_fd = 0;
c7e6f84f 1433 int flags, disable_buffering;
649d65ed 1434
ea124cb3 1435 rprintf(FLOG, "building file list\n");
134f4338 1436 if (show_filelist_p())
1bbd10fe 1437 start_filelist_progress("building file list");
3ea6e0e7 1438 else if (inc_recurse && verbose && !am_server)
98b1689d 1439 rprintf(FCLIENT, "sending incremental file list\n");
c627d613 1440
a800434a 1441 start_write = stats.total_written;
31b4d25d 1442 gettimeofday(&start_tv, NULL);
a800434a 1443
8cae71eb 1444#ifdef SUPPORT_HARD_LINKS
c7e6f84f 1445 if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
8cae71eb
WD
1446 init_hard_links();
1447#endif
1448
c7e6f84f 1449 flist = cur_flist = flist_new(0, "send_file_list");
3ea6e0e7 1450 if (inc_recurse) {
c7e6f84f
WD
1451 dir_flist = flist_new(FLIST_TEMP, "send_file_list");
1452 flags = FLAG_DIVERT_DIRS;
1453 } else {
1454 dir_flist = cur_flist;
1455 flags = 0;
1456 }
c627d613 1457
c7e6f84f 1458 disable_buffering = io_start_buffering_out(f);
134f4338 1459 if (filesfrom_fd >= 0) {
6f3684ff 1460 if (argv[0] && !push_dir(argv[0], 0)) {
63d83e94
WD
1461 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1462 full_fname(argv[0]), curr_dir);
134f4338 1463 exit_cleanup(RERR_FILESELECT);
24d0fcde 1464 }
134f4338 1465 use_ff_fd = 1;
d6dead6b
AT
1466 }
1467
24d0fcde 1468 while (1) {
56f0c976
WD
1469 char fbuf[MAXPATHLEN];
1470 char *fn;
301fb56c 1471 int is_dot_dir;
c627d613 1472
24d0fcde 1473 if (use_ff_fd) {
56f0c976 1474 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
24d0fcde 1475 break;
a2248aea 1476 sanitize_path(fbuf, fbuf, "", 0, NULL);
24d0fcde
WD
1477 } else {
1478 if (argc-- == 0)
1479 break;
56f0c976 1480 strlcpy(fbuf, *argv++, MAXPATHLEN);
24d0fcde 1481 if (sanitize_paths)
a2248aea 1482 sanitize_path(fbuf, fbuf, "", 0, NULL);
24d0fcde 1483 }
c627d613 1484
56f0c976 1485 len = strlen(fbuf);
1902a765
WD
1486 if (relative_paths) {
1487 /* We clean up fbuf below. */
1488 is_dot_dir = 0;
1489 } else if (!len || fbuf[len - 1] == '/') {
56f0c976 1490 if (len == 2 && fbuf[0] == '.') {
6931c138 1491 /* Turn "./" into just "." rather than "./." */
56f0c976 1492 fbuf[1] = '\0';
557a35f5 1493 } else {
56f0c976 1494 if (len + 1 >= MAXPATHLEN)
a1f99493 1495 overflow_exit("send_file_list");
56f0c976
WD
1496 fbuf[len++] = '.';
1497 fbuf[len] = '\0';
53f821f1 1498 }
301fb56c 1499 is_dot_dir = 1;
56f0c976
WD
1500 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1501 && (len == 2 || fbuf[len-3] == '/')) {
1502 if (len + 2 >= MAXPATHLEN)
a1f99493 1503 overflow_exit("send_file_list");
56f0c976
WD
1504 fbuf[len++] = '/';
1505 fbuf[len++] = '.';
1506 fbuf[len] = '\0';
a289f89f 1507 is_dot_dir = 1;
301fb56c 1508 } else {
56f0c976
WD
1509 is_dot_dir = fbuf[len-1] == '.'
1510 && (len == 1 || fbuf[len-2] == '/');
649d65ed 1511 }
c627d613 1512
649d65ed
AT
1513 dir = NULL;
1514
1515 if (!relative_paths) {
56f0c976 1516 p = strrchr(fbuf, '/');
649d65ed 1517 if (p) {
151f59f1 1518 *p = '\0';
56f0c976 1519 if (p == fbuf)
649d65ed
AT
1520 dir = "/";
1521 else
56f0c976
WD
1522 dir = fbuf;
1523 len -= p - fbuf + 1;
1524 fn = p + 1;
1525 } else
1526 fn = fbuf;
1902a765
WD
1527 } else {
1528 if ((p = strstr(fbuf, "/./")) != NULL) {
1529 *p = '\0';
1530 if (p == fbuf)
1531 dir = "/";
1532 else
1533 dir = fbuf;
1534 len -= p - fbuf + 3;
1535 fn = p + 3;
1536 } else
1537 fn = fbuf;
1538 /* Get rid of trailing "/" and "/.". */
1539 while (len) {
ede1f0eb
WD
1540 if (fn[len - 1] == '/') {
1541 is_dot_dir = 1;
1542 if (!--len && !dir) {
1543 len++;
1544 break;
1545 }
1546 }
1902a765
WD
1547 else if (len >= 2 && fn[len - 1] == '.'
1548 && fn[len - 2] == '/') {
ede1f0eb 1549 is_dot_dir = 1;
1902a765
WD
1550 if (!(len -= 2) && !dir) {
1551 len++;
1552 break;
1553 }
1554 } else
1555 break;
1556 }
8c449e62
WD
1557 if (len == 1 && fn[0] == '/')
1558 fn[len++] = '.';
1902a765
WD
1559 fn[len] = '\0';
1560 /* Reject a ".." dir in the active part of the path. */
ede1f0eb
WD
1561 for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
1562 if ((p[2] == '/' || p[2] == '\0')
1563 && (p == fn || p[-1] == '/')) {
1564 rprintf(FERROR,
1565 "found \"..\" dir in relative path: %s\n",
1566 fbuf);
1567 exit_cleanup(RERR_SYNTAX);
1568 }
1902a765
WD
1569 }
1570 }
d2ea5980 1571
56f0c976
WD
1572 if (!*fn) {
1573 len = 1;
1574 fn = ".";
1575 }
d2ea5980 1576
3bb88b43
WD
1577 dirlen = dir ? strlen(dir) : 0;
1578 if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
ef35abb2 1579 if (!push_pathname(dir ? strdup(dir) : NULL, dirlen))
3bb88b43 1580 goto push_error;
ef35abb2
WD
1581 lastdir = pathname;
1582 lastdir_len = pathname_len;
1583 } else if (!push_pathname(lastdir, lastdir_len)) {
3bb88b43
WD
1584 push_error:
1585 io_error |= IOERR_GENERAL;
1586 rsyserr(FERROR, errno, "push_dir %s failed in %s",
1587 full_fname(dir), curr_dir);
1588 continue;
d2ea5980
WD
1589 }
1590
99eba675 1591 if (fn != fbuf)
56f0c976
WD
1592 memmove(fbuf, fn, len + 1);
1593
25c2a6ac
WD
1594 if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
1595 io_error |= IOERR_GENERAL;
1596 rsyserr(FERROR, errno, "link_stat %s failed",
1597 full_fname(fbuf));
1598 continue;
1599 }
1600
1601 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
1602 rprintf(FINFO, "skipping directory %s\n", fbuf);
1603 continue;
1604 }
1605
56f0c976 1606 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
151f59f1
WD
1607 /* Send the implied directories at the start of the
1608 * source spec, so we get their permissions right. */
56f0c976 1609 char *lp = lastpath, *slash = fbuf;
151f59f1 1610 *p = '\0';
2154309a
WD
1611 /* Skip any initial directories in our path that we
1612 * have in common with lastpath. */
56f0c976 1613 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
2154309a
WD
1614 if (*fn == '/')
1615 slash = fn;
2154309a
WD
1616 }
1617 *p = '/';
1618 if (fn != p || (*lp && *lp != '/')) {
0f57446d 1619 int save_copy_links = copy_links;
7e037c42 1620 int save_xfer_dirs = xfer_dirs;
3ea6e0e7 1621 int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0;
88c2190a 1622 copy_links |= copy_unsafe_links;
7e037c42 1623 xfer_dirs = 1;
2154309a 1624 while ((slash = strchr(slash+1, '/')) != 0) {
151f59f1 1625 *slash = '\0';
42c6b139
WD
1626 send_file_name(f, flist, fbuf, NULL,
1627 dir_flags, ALL_FILTERS);
2154309a 1628 *slash = '/';
649d65ed 1629 }
0f57446d 1630 copy_links = save_copy_links;
7e037c42 1631 xfer_dirs = save_xfer_dirs;
151f59f1 1632 *p = '\0';
56f0c976 1633 strlcpy(lastpath, fbuf, sizeof lastpath);
649d65ed
AT
1634 *p = '/';
1635 }
1636 }
ebed4c3a 1637
535737bf
WD
1638 if (one_file_system)
1639 filesystem_dev = st.st_dev;
1640
75c51953 1641 if (recurse || (xfer_dirs && is_dot_dir)) {
42c6b139 1642 struct file_struct *file;
ef35abb2 1643 int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR | flags;
42c6b139
WD
1644 file = send_file_name(f, flist, fbuf, &st,
1645 top_flags, ALL_FILTERS);
3ea6e0e7 1646 if (file && !inc_recurse)
42c6b139
WD
1647 send_if_directory(f, flist, file, fbuf, len, flags);
1648 } else
1649 send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS);
649d65ed 1650 }
dc5ddbcc 1651
134f4338
WD
1652 gettimeofday(&end_tv, NULL);
1653 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1654 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1655 if (stats.flist_buildtime == 0)
1656 stats.flist_buildtime = 1;
1657 start_tv = end_tv;
31b4d25d 1658
8cae71eb
WD
1659 write_byte(f, 0); /* Indicate end of file list */
1660
1661#ifdef SUPPORT_HARD_LINKS
3ea6e0e7 1662 if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
8cae71eb
WD
1663 idev_destroy();
1664#endif
c627d613 1665
134f4338
WD
1666 if (show_filelist_p())
1667 finish_filelist_progress(flist);
31b4d25d 1668
134f4338
WD
1669 gettimeofday(&end_tv, NULL);
1670 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1671 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 1672
c7e6f84f
WD
1673 /* Sort the list without removing any duplicates in non-incremental
1674 * mode. This allows the receiving side to ask for whatever name it
1675 * kept. For incremental mode, the sender also removes duplicates
1676 * in this initial file-list so that it avoids re-sending duplicated
1677 * directories. */
3ea6e0e7 1678 clean_flist(flist, 0, inc_recurse);
c7e6f84f 1679 file_total += flist->count;
ebed4c3a 1680
3ea6e0e7 1681 if (!numeric_ids && !inc_recurse)
82ad07c4 1682 send_uid_list(f);
f6c34742 1683
134f4338 1684 /* send the io_error flag */
c7e6f84f
WD
1685 if (protocol_version < 30)
1686 write_int(f, ignore_errors ? 0 : io_error);
1687 else if (io_error && !ignore_errors)
1688 send_msg_int(MSG_IO_ERROR, io_error);
1689
1690 if (disable_buffering)
1691 io_end_buffering_out();
6ba9279f 1692
134f4338
WD
1693 stats.flist_size = stats.total_written - start_write;
1694 stats.num_files = flist->count;
d6dead6b 1695
cefed3e8 1696 if (verbose > 3)
32cbfe7b 1697 output_flist(flist);
cefed3e8 1698
17faa41c 1699 if (verbose > 2)
ebed4c3a 1700 rprintf(FINFO, "send_file_list done\n");
17faa41c 1701
3ea6e0e7 1702 if (inc_recurse) {
c7e6f84f 1703 add_dirs_to_tree(-1, 0, dir_flist->count - 1);
25c2a6ac
WD
1704 if (send_dir_ndx < 0) {
1705 write_ndx(f, NDX_FLIST_EOF);
1706 flist_eof = 1;
1707 }
1708 else if (file_total == 1) {
c7e6f84f
WD
1709 /* If we're creating incremental file-lists and there
1710 * was just 1 item in the first file-list, send 1 more
1711 * file-list to check if this is a 1-file xfer. */
25c2a6ac 1712 send_extra_file_list(f, 1);
c7e6f84f
WD
1713 }
1714 }
1715
649d65ed 1716 return flist;
c627d613
AT
1717}
1718
c627d613
AT
1719struct file_list *recv_file_list(int f)
1720{
ebed4c3a 1721 struct file_list *flist;
c7e6f84f 1722 int dstart, flags;
ebed4c3a 1723 int64 start_read;
c627d613 1724
98b1689d 1725 if (!first_flist)
c7e6f84f 1726 rprintf(FLOG, "receiving file list\n");
1bbd10fe
DD
1727 if (show_filelist_p())
1728 start_filelist_progress("receiving file list");
3ea6e0e7 1729 else if (inc_recurse && verbose && !am_server && !first_flist)
98b1689d 1730 rprintf(FCLIENT, "receiving incremental file list\n");
c627d613 1731
ebed4c3a 1732 start_read = stats.total_read;
a800434a 1733
c7e6f84f 1734 flist = flist_new(0, "recv_file_list");
c627d613 1735
8cae71eb
WD
1736#ifdef SUPPORT_HARD_LINKS
1737 if (preserve_hard_links && protocol_version < 30)
1738 init_hard_links();
1739#endif
c627d613 1740
3ea6e0e7 1741 if (inc_recurse) {
c7e6f84f
WD
1742 if (flist->ndx_start == 0)
1743 dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
1744 dstart = dir_flist->count;
1745 } else {
1746 dir_flist = flist;
1747 dstart = 0;
1748 }
1749
1ef00d20 1750 while ((flags = read_byte(f)) != 0) {
f5db0993 1751 struct file_struct *file;
dbda5fbf 1752
d9d6bc52 1753 flist_expand(flist);
c627d613 1754
d01d15e0 1755 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1756 flags |= read_byte(f) << 8;
82ad07c4 1757 file = recv_file_entry(flist, flags, f);
c627d613 1758
3ea6e0e7 1759 if (inc_recurse && S_ISDIR(file->mode)) {
c7e6f84f
WD
1760 flist_expand(dir_flist);
1761 dir_flist->files[dir_flist->count++] = file;
1762 }
1763
f5db0993 1764 flist->files[flist->count++] = file;
c627d613 1765
53135fe8 1766 maybe_emit_filelist_progress(flist->count);
1bbd10fe 1767
5e4ff5f9
WD
1768 if (verbose > 2) {
1769 rprintf(FINFO, "recv_file_name(%s)\n",
1770 f_name(file, NULL));
1771 }
ebed4c3a 1772 }
c7e6f84f 1773 file_total += flist->count;
c627d613 1774
ebed4c3a
MP
1775 if (verbose > 2)
1776 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1777
b7736c79 1778 if (show_filelist_p())
1bbd10fe 1779 finish_filelist_progress(flist);
a06d19e3 1780
983b1ed3
WD
1781 clean_flist(flist, relative_paths, 1);
1782
3ea6e0e7 1783 if (inc_recurse) {
c7e6f84f
WD
1784 qsort(dir_flist->files + dstart, dir_flist->count - dstart,
1785 sizeof dir_flist->files[0], (int (*)())file_compare);
1786 } else if (f >= 0)
7b6fa00f 1787 recv_uid_list(f, flist);
f6c34742 1788
c7e6f84f 1789 if (protocol_version < 30) {
b9f592fb 1790 /* Recv the io_error flag */
1f56188f 1791 if (ignore_errors)
b9f592fb
WD
1792 read_int(f);
1793 else
1794 io_error |= read_int(f);
ebed4c3a 1795 }
6ba9279f 1796
cefed3e8 1797 if (verbose > 3)
32cbfe7b 1798 output_flist(flist);
cefed3e8 1799
ebed4c3a
MP
1800 if (list_only) {
1801 int i;
b7736c79 1802 for (i = 0; i < flist->count; i++)
ebed4c3a 1803 list_file_entry(flist->files[i]);
ebed4c3a 1804 }
f7632fc6 1805
ebed4c3a
MP
1806 if (verbose > 2)
1807 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1808
c7e6f84f
WD
1809 stats.flist_size += stats.total_read - start_read;
1810 stats.num_files += flist->count;
a800434a 1811
ebed4c3a 1812 return flist;
c627d613
AT
1813}
1814
c7e6f84f
WD
1815/* This is only used once by the receiver if the very first file-list
1816 * has exactly one item in it. */
1817void recv_additional_file_list(int f)
c627d613 1818{
c7e6f84f 1819 struct file_list *flist;
9ae7a2cd 1820 int ndx = read_ndx(f);
25c2a6ac 1821 if (ndx == NDX_FLIST_EOF) {
c7e6f84f
WD
1822 flist_eof = 1;
1823 change_local_filter_dir(NULL, 0, 0);
1824 } else {
1825 ndx = NDX_FLIST_OFFSET - ndx;
1826 if (ndx < 0 || ndx >= dir_flist->count) {
1827 ndx = NDX_FLIST_OFFSET - ndx;
1828 rprintf(FERROR,
1829 "Invalid dir index: %d (%d - %d)\n",
1830 ndx, NDX_FLIST_OFFSET,
1831 NDX_FLIST_OFFSET - dir_flist->count);
1832 exit_cleanup(RERR_PROTOCOL);
1833 }
1834 flist = recv_file_list(f);
1835 flist->parent_ndx = ndx;
1836 }
c627d613
AT
1837}
1838
f5db0993
WD
1839/* Search for an identically-named item in the file list. Note that the
1840 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 1841int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1842{
f3c3ed44 1843 int low = flist->low, high = flist->high;
207522ae 1844 int diff, mid, mid_up;
f3c3ed44
WD
1845
1846 while (low <= high) {
1847 mid = (low + high) / 2;
96293cf9 1848 if (F_IS_ACTIVE(flist->files[mid]))
c0b134a4 1849 mid_up = mid;
7402d583
WD
1850 else {
1851 /* Scan for the next non-empty entry using the cached
1852 * distance values. If the value isn't fully up-to-
1853 * date, update it. */
9d737ecb 1854 mid_up = mid + F_DEPTH(flist->files[mid]);
96293cf9 1855 if (!F_IS_ACTIVE(flist->files[mid_up])) {
85aecef6 1856 do {
9d737ecb 1857 mid_up += F_DEPTH(flist->files[mid_up]);
96293cf9 1858 } while (!F_IS_ACTIVE(flist->files[mid_up]));
9d737ecb 1859 F_DEPTH(flist->files[mid]) = mid_up - mid;
c0b134a4 1860 }
c0b134a4 1861 if (mid_up > high) {
7402d583
WD
1862 /* If there's nothing left above us, set high to
1863 * a non-empty entry below us and continue. */
96293cf9
WD
1864 high = mid - (int)flist->files[mid]->len32;
1865 if (!F_IS_ACTIVE(flist->files[high])) {
85aecef6 1866 do {
96293cf9
WD
1867 high -= (int)flist->files[high]->len32;
1868 } while (!F_IS_ACTIVE(flist->files[high]));
1869 flist->files[mid]->len32 = mid - high;
7402d583 1870 }
c0b134a4
WD
1871 continue;
1872 }
c0b134a4 1873 }
207522ae
WD
1874 diff = f_name_cmp(flist->files[mid_up], f);
1875 if (diff == 0) {
f5db0993
WD
1876 if (protocol_version < 29
1877 && S_ISDIR(flist->files[mid_up]->mode)
1878 != S_ISDIR(f->mode))
1879 return -1;
f3c3ed44 1880 return mid_up;
f5db0993 1881 }
207522ae 1882 if (diff < 0)
f3c3ed44 1883 low = mid_up + 1;
207522ae
WD
1884 else
1885 high = mid - 1;
d966ee25 1886 }
d966ee25 1887 return -1;
c627d613
AT
1888}
1889
3ec4dd97 1890/*
9935066b
S
1891 * Free up any resources a file_struct has allocated
1892 * and clear the file.
3ec4dd97 1893 */
82ad07c4 1894void clear_file(struct file_struct *file)
c627d613 1895{
a3e18c76
WD
1896 /* The +1 zeros out the first char of the basename. */
1897 memset(file, 0, FILE_STRUCT_LEN + 1);
9d737ecb 1898 /* In an empty entry, F_DEPTH() is an offset to the next non-empty
96293cf9 1899 * entry. Likewise for len32 in the opposite direction. We assume
0679ac4c
WD
1900 * that we're alone for now since flist_find() will adjust the counts
1901 * it runs into that aren't up-to-date. */
9d737ecb 1902 file->len32 = F_DEPTH(file) = 1;
3ec4dd97 1903}
c627d613 1904
4785cd43 1905/* Allocate a new file list. */
c7e6f84f 1906struct file_list *flist_new(int flags, char *msg)
3d382777
AT
1907{
1908 struct file_list *flist;
1909
58cadc86 1910 flist = new(struct file_list);
ebed4c3a 1911 if (!flist)
9935066b 1912 out_of_memory(msg);
3d382777 1913
82ad07c4 1914 memset(flist, 0, sizeof flist[0]);
9935066b 1915
c7e6f84f
WD
1916 if (!(flags & FLIST_TEMP)) {
1917 if (first_flist) {
1918 flist->ndx_start = first_flist->prev->ndx_start
1919 + first_flist->prev->count;
1920 }
1921 /* This is a doubly linked list with prev looping back to
1922 * the end of the list, but the last next pointer is NULL. */
1923 if (!first_flist)
1924 first_flist = cur_flist = flist->prev = flist;
1925 else {
1926 flist->prev = first_flist->prev;
1927 flist->prev->next = first_flist->prev = flist;
1928 }
1929 flist_cnt++;
1930 }
1931
4a19c3b2 1932 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN)))
9935066b
S
1933 out_of_memory(msg);
1934
3d382777
AT
1935 return flist;
1936}
ebed4c3a 1937
4785cd43 1938/* Free up all elements in a flist. */
3ec4dd97
AT
1939void flist_free(struct file_list *flist)
1940{
c7e6f84f
WD
1941 if (!flist->prev)
1942 ; /* Was FLIST_TEMP dir-list. */
1943 else if (flist == flist->prev) {
1944 first_flist = cur_flist = NULL;
1945 file_total = 0;
1946 flist_cnt = 0;
1947 } else {
1948 if (flist == cur_flist)
1949 cur_flist = flist->next;
1950 if (flist == first_flist)
1951 first_flist = first_flist->next;
1952 else {
1953 flist->prev->next = flist->next;
1954 if (!flist->next)
1955 flist->next = first_flist;
1956 }
1957 flist->next->prev = flist->prev;
1958 file_total -= flist->count;
1959 flist_cnt--;
1960 }
1961
9935066b 1962 pool_destroy(flist->file_pool);
3ec4dd97 1963 free(flist->files);
3ec4dd97 1964 free(flist);
c627d613
AT
1965}
1966
c627d613
AT
1967/*
1968 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1969 * duplicate names can cause corruption because of the pipelining
c627d613 1970 */
827c37f6 1971static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1972{
85aecef6 1973 char fbuf[MAXPATHLEN];
6931c138 1974 int i, prev_i = 0;
c627d613 1975
910ee8c9
WD
1976 if (!flist)
1977 return;
1978 if (flist->count == 0) {
1979 flist->high = -1;
3ec4dd97 1980 return;
910ee8c9 1981 }
3ec4dd97 1982
ebed4c3a 1983 qsort(flist->files, flist->count,
a4a7e64c 1984 sizeof flist->files[0], (int (*)())file_compare);
ebed4c3a 1985
827c37f6 1986 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
96293cf9 1987 if (F_IS_ACTIVE(flist->files[i])) {
6931c138 1988 prev_i = i;
b91b50c0
WD
1989 break;
1990 }
1991 }
f3c3ed44 1992 flist->low = prev_i;
b91b50c0 1993 while (++i < flist->count) {
fe1c19dc 1994 int j;
f5db0993
WD
1995 struct file_struct *file = flist->files[i];
1996
96293cf9 1997 if (!F_IS_ACTIVE(file))
b91b50c0 1998 continue;
fe1c19dc
WD
1999 if (f_name_cmp(file, flist->files[prev_i]) == 0)
2000 j = prev_i;
2001 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
2002 int save_mode = file->mode;
2003 /* Make sure that this directory doesn't duplicate a
2004 * non-directory earlier in the list. */
f5db0993 2005 flist->high = prev_i;
fe1c19dc
WD
2006 file->mode = S_IFREG;
2007 j = flist_find(flist, file);
f5db0993 2008 file->mode = save_mode;
fe1c19dc
WD
2009 } else
2010 j = -1;
2011 if (j >= 0) {
2012 struct file_struct *fp = flist->files[j];
2013 int keep, drop;
2014 /* If one is a dir and the other is not, we want to
2015 * keep the dir because it might have contents in the
2016 * list. */
2017 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
2018 if (S_ISDIR(file->mode))
2019 keep = i, drop = j;
2020 else
2021 keep = j, drop = i;
c7e6f84f 2022 } else if (protocol_version < 27)
fe1c19dc 2023 keep = j, drop = i;
c7e6f84f
WD
2024 else
2025 keep = i, drop = j;
b91b50c0 2026 if (verbose > 1 && !am_server) {
ebed4c3a 2027 rprintf(FINFO,
fe1c19dc 2028 "removing duplicate name %s from file list (%d)\n",
85aecef6 2029 f_name(file, fbuf), drop);
b91b50c0 2030 }
9c000f5e
WD
2031 /* Make sure we don't lose track of a user-specified
2032 * top directory. */
0f0b2e66 2033 flist->files[keep]->flags |= flist->files[drop]->flags
82ad07c4 2034 & (FLAG_TOP_DIR|FLAG_XFER_DIR);
fe1c19dc 2035
82ad07c4 2036 clear_file(flist->files[drop]);
9935066b 2037
fe1c19dc
WD
2038 if (keep == i) {
2039 if (flist->low == drop) {
2040 for (j = drop + 1;
96293cf9 2041 j < i && !F_IS_ACTIVE(flist->files[j]);
fe1c19dc
WD
2042 j++) {}
2043 flist->low = j;
2044 }
2045 prev_i = i;
2046 }
728d0922 2047 } else
6931c138 2048 prev_i = i;
3ec4dd97 2049 }
f5db0993 2050 flist->high = no_dups ? prev_i : flist->count - 1;
0199b05f 2051
c0b134a4
WD
2052 if (strip_root) {
2053 /* We need to strip off the leading slashes for relative
2054 * paths, but this must be done _after_ the sorting phase. */
2055 for (i = flist->low; i <= flist->high; i++) {
2056 struct file_struct *file = flist->files[i];
2057
2058 if (!file->dirname)
2059 continue;
2060 while (*file->dirname == '/')
2061 file->dirname++;
2062 if (!*file->dirname)
2063 file->dirname = NULL;
2064 }
2065 }
2066
e6ffb966
WD
2067 if (prune_empty_dirs && no_dups) {
2068 int j, prev_depth = 0;
9c000f5e 2069
e6ffb966 2070 prev_i = 0; /* It's OK that this isn't really true. */
9c000f5e 2071
f5db0993 2072 for (i = flist->low; i <= flist->high; i++) {
e6ffb966 2073 struct file_struct *fp, *file = flist->files[i];
ebed4c3a 2074
9d737ecb 2075 /* This temporarily abuses the F_DEPTH() value for a
e6ffb966
WD
2076 * directory that is in a chain that might get pruned.
2077 * We restore the old value if it gets a reprieve. */
9d737ecb 2078 if (S_ISDIR(file->mode) && F_DEPTH(file)) {
e6ffb966 2079 /* Dump empty dirs when coming back down. */
9d737ecb 2080 for (j = prev_depth; j >= F_DEPTH(file); j--) {
e6ffb966 2081 fp = flist->files[prev_i];
9d737ecb 2082 if (F_DEPTH(fp) >= 0)
e6ffb966 2083 break;
9d737ecb 2084 prev_i = -F_DEPTH(fp)-1;
82ad07c4 2085 clear_file(fp);
9c000f5e 2086 }
9d737ecb 2087 prev_depth = F_DEPTH(file);
85aecef6
WD
2088 if (is_excluded(f_name(file, fbuf), 1,
2089 ALL_FILTERS)) {
e6ffb966
WD
2090 /* Keep dirs through this dir. */
2091 for (j = prev_depth-1; ; j--) {
2092 fp = flist->files[prev_i];
9d737ecb 2093 if (F_DEPTH(fp) >= 0)
e6ffb966 2094 break;
9d737ecb
WD
2095 prev_i = -F_DEPTH(fp)-1;
2096 F_DEPTH(fp) = j;
e6ffb966 2097 }
85aecef6 2098 } else
9d737ecb 2099 F_DEPTH(file) = -prev_i-1;
e6ffb966
WD
2100 prev_i = i;
2101 } else {
2102 /* Keep dirs through this non-dir. */
2103 for (j = prev_depth; ; j--) {
2104 fp = flist->files[prev_i];
9d737ecb 2105 if (F_DEPTH(fp) >= 0)
e6ffb966 2106 break;
9d737ecb
WD
2107 prev_i = -F_DEPTH(fp)-1;
2108 F_DEPTH(fp) = j;
e6ffb966 2109 }
0199b05f 2110 }
9c000f5e 2111 }
ca947dea 2112 /* Dump all remaining empty dirs. */
e6ffb966
WD
2113 while (1) {
2114 struct file_struct *fp = flist->files[prev_i];
9d737ecb 2115 if (F_DEPTH(fp) >= 0)
e6ffb966 2116 break;
9d737ecb 2117 prev_i = -F_DEPTH(fp)-1;
82ad07c4 2118 clear_file(fp);
9c000f5e 2119 }
f5db0993 2120
9c000f5e 2121 for (i = flist->low; i <= flist->high; i++) {
96293cf9 2122 if (F_IS_ACTIVE(flist->files[i]))
9c000f5e
WD
2123 break;
2124 }
2125 flist->low = i;
2126 for (i = flist->high; i >= flist->low; i--) {
96293cf9 2127 if (F_IS_ACTIVE(flist->files[i]))
9c000f5e 2128 break;
0199b05f 2129 }
9c000f5e 2130 flist->high = i;
0199b05f 2131 }
cefed3e8 2132}
0199b05f 2133
32cbfe7b 2134static void output_flist(struct file_list *flist)
cefed3e8 2135{
f3c3ed44 2136 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8 2137 struct file_struct *file;
9d737ecb 2138 const char *root, *dir, *slash, *name, *trail;
32cbfe7b 2139 const char *who = who_am_i();
cefed3e8 2140 int i;
0199b05f 2141
c7e6f84f
WD
2142 rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n",
2143 who, flist->ndx_start, flist->count, flist->low, flist->high);
ebed4c3a 2144 for (i = 0; i < flist->count; i++) {
cefed3e8 2145 file = flist->files[i];
82ad07c4
WD
2146 if ((am_root || am_sender) && preserve_uid) {
2147 snprintf(uidbuf, sizeof uidbuf, " uid=%ld",
2148 (long)F_UID(file));
2149 } else
f05f993e 2150 *uidbuf = '\0';
82ad07c4
WD
2151 if (preserve_gid && F_GID(file) != GID_NONE) {
2152 snprintf(gidbuf, sizeof gidbuf, " gid=%ld",
2153 (long)F_GID(file));
2154 } else
f05f993e 2155 *gidbuf = '\0';
f3c3ed44 2156 if (!am_sender)
9d737ecb 2157 snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
96293cf9 2158 if (F_IS_ACTIVE(file)) {
ef35abb2 2159 root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
96293cf9
WD
2160 if ((dir = file->dirname) == NULL)
2161 dir = slash = "";
2162 else
2163 slash = "/";
c58c1dc4 2164 name = file->basename;
96293cf9
WD
2165 trail = S_ISDIR(file->mode) ? "/" : "";
2166 } else
9d737ecb 2167 root = dir = slash = name = trail = "";
14698a3a 2168 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
9d737ecb 2169 who, i, root, dir, slash, name, trail, (int)file->mode,
96293cf9 2170 (double)F_LENGTH(file), uidbuf, gidbuf, file->flags);
0199b05f 2171 }
3ec4dd97
AT
2172}
2173
8824e2ce 2174enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 2175enum fnc_type { t_PATH, t_ITEM };
8018edd3 2176
2f3cad89 2177/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
2178 * would do if it were operating on the joined strings.
2179 *
2180 * Some differences beginning with protocol_version 29: (1) directory names
2181 * are compared with an assumed trailing slash so that they compare in a
2182 * way that would cause them to sort immediately prior to any content they
2183 * may have; (2) a directory of any name compares after a non-directory of
2184 * any name at the same depth; (3) a directory with name "." compares prior
2185 * to anything else. These changes mean that a directory and a non-dir
2186 * with the same name will not compare as equal (protocol_version >= 29).
2187 *
2188 * The dirname component can be an empty string, but the basename component
2189 * cannot (and never is in the current codebase). The basename component
2190 * may be NULL (for a removed item), in which case it is considered to be
2191 * after any existing item. */
8018edd3 2192int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 2193{
8018edd3
WD
2194 int dif;
2195 const uchar *c1, *c2;
1ef00d20 2196 enum fnc_state state1, state2;
f5db0993
WD
2197 enum fnc_type type1, type2;
2198 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3 2199
96293cf9
WD
2200 if (!f1 || !F_IS_ACTIVE(f1)) {
2201 if (!f2 || !F_IS_ACTIVE(f2))
8018edd3
WD
2202 return 0;
2203 return -1;
2204 }
96293cf9 2205 if (!f2 || !F_IS_ACTIVE(f2))
8018edd3
WD
2206 return 1;
2207
14698a3a
WD
2208 c1 = (uchar*)f1->dirname;
2209 c2 = (uchar*)f2->dirname;
2210 if (c1 == c2)
2211 c1 = c2 = NULL;
2212 if (!c1) {
f5db0993 2213 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
c58c1dc4 2214 c1 = (const uchar*)f1->basename;
f5db0993
WD
2215 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2216 type1 = t_ITEM;
2217 state1 = s_TRAILING;
2218 c1 = (uchar*)"";
2219 } else
2220 state1 = s_BASE;
f5db0993
WD
2221 } else {
2222 type1 = t_path;
8824e2ce 2223 state1 = s_DIR;
f5db0993 2224 }
14698a3a 2225 if (!c2) {
f5db0993 2226 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
c58c1dc4 2227 c2 = (const uchar*)f2->basename;
f5db0993
WD
2228 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2229 type2 = t_ITEM;
2230 state2 = s_TRAILING;
2231 c2 = (uchar*)"";
2232 } else
2233 state2 = s_BASE;
f5db0993
WD
2234 } else {
2235 type2 = t_path;
8824e2ce 2236 state2 = s_DIR;
f5db0993
WD
2237 }
2238
2239 if (type1 != type2)
2240 return type1 == t_PATH ? 1 : -1;
8018edd3 2241
e4fdf1de 2242 do {
f5db0993 2243 if (!*c1) {
8018edd3 2244 switch (state1) {
8824e2ce
WD
2245 case s_DIR:
2246 state1 = s_SLASH;
e90b8ace 2247 c1 = (uchar*)"/";
8018edd3 2248 break;
8824e2ce 2249 case s_SLASH:
f5db0993 2250 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
c58c1dc4 2251 c1 = (const uchar*)f1->basename;
cbb5fa4f
WD
2252 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
2253 type1 = t_ITEM;
2254 state1 = s_TRAILING;
2255 c1 = (uchar*)"";
2256 } else
2257 state1 = s_BASE;
8018edd3 2258 break;
8824e2ce
WD
2259 case s_BASE:
2260 state1 = s_TRAILING;
f5db0993 2261 if (type1 == t_PATH) {
2f3cad89 2262 c1 = (uchar*)"/";
f5db0993
WD
2263 break;
2264 }
2265 /* FALL THROUGH */
8824e2ce 2266 case s_TRAILING:
f5db0993 2267 type1 = t_ITEM;
8018edd3
WD
2268 break;
2269 }
f5db0993
WD
2270 if (*c2 && type1 != type2)
2271 return type1 == t_PATH ? 1 : -1;
8018edd3 2272 }
f5db0993 2273 if (!*c2) {
8018edd3 2274 switch (state2) {
8824e2ce
WD
2275 case s_DIR:
2276 state2 = s_SLASH;
e90b8ace 2277 c2 = (uchar*)"/";
8018edd3 2278 break;
8824e2ce 2279 case s_SLASH:
f5db0993 2280 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
c58c1dc4 2281 c2 = (const uchar*)f2->basename;
cbb5fa4f
WD
2282 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
2283 type2 = t_ITEM;
2284 state2 = s_TRAILING;
2285 c2 = (uchar*)"";
2286 } else
2287 state2 = s_BASE;
8018edd3 2288 break;
8824e2ce 2289 case s_BASE:
8824e2ce 2290 state2 = s_TRAILING;
f5db0993 2291 if (type2 == t_PATH) {
2f3cad89 2292 c2 = (uchar*)"/";
f5db0993
WD
2293 break;
2294 }
2295 /* FALL THROUGH */
8824e2ce 2296 case s_TRAILING:
f5db0993
WD
2297 if (!*c1)
2298 return 0;
2299 type2 = t_ITEM;
8018edd3
WD
2300 break;
2301 }
f5db0993
WD
2302 if (type1 != type2)
2303 return type1 == t_PATH ? 1 : -1;
8018edd3 2304 }
e4fdf1de 2305 } while ((dif = (int)*c1++ - (int)*c2++) == 0);
8018edd3
WD
2306
2307 return dif;
2308}
2309
96293cf9
WD
2310char *f_name_buf(void)
2311{
2312 static char names[5][MAXPATHLEN];
2313 static unsigned int n;
2314
2315 n = (n + 1) % (sizeof names / sizeof names[0]);
2316
2317 return names[n];
2318}
2319
8018edd3 2320/* Return a copy of the full filename of a flist entry, using the indicated
5e4ff5f9
WD
2321 * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
2322 * done because we checked the size when creating the file_struct entry.
8018edd3 2323 */
5e4ff5f9 2324char *f_name(struct file_struct *f, char *fbuf)
8018edd3 2325{
96293cf9 2326 if (!f || !F_IS_ACTIVE(f))
ebed4c3a 2327 return NULL;
3ec4dd97 2328
96293cf9
WD
2329 if (!fbuf)
2330 fbuf = f_name_buf();
5e4ff5f9 2331
3ec4dd97 2332 if (f->dirname) {
882e6893
WD
2333 int len = strlen(f->dirname);
2334 memcpy(fbuf, f->dirname, len);
2335 fbuf[len] = '/';
c58c1dc4 2336 strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
8018edd3 2337 } else
c58c1dc4 2338 strlcpy(fbuf, f->basename, MAXPATHLEN);
0ee6ca98 2339
b7736c79 2340 return fbuf;
8018edd3 2341}
e03dfae5 2342
32cbfe7b
WD
2343/* Do a non-recursive scan of the named directory, possibly ignoring all
2344 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
2345 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
2346 * buffer (the functions we call will append names onto the end, but the old
2347 * dir value will be restored on exit). */
263d3bfb 2348struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
37802f40
WD
2349{
2350 struct file_list *dirlist;
2351 char dirbuf[MAXPATHLEN];
37802f40 2352 int save_recurse = recurse;
1661fe9b 2353 int save_xfer_dirs = xfer_dirs;
37802f40 2354
32cbfe7b
WD
2355 if (dlen < 0) {
2356 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
2357 if (dlen >= MAXPATHLEN)
2358 return NULL;
2359 dirname = dirbuf;
2360 }
37802f40 2361
c7e6f84f 2362 dirlist = flist_new(FLIST_TEMP, "get_dirlist");
32cbfe7b 2363
37802f40 2364 recurse = 0;
1661fe9b 2365 xfer_dirs = 1;
c7e6f84f 2366 send_directory(ignore_filter_rules ? -2 : -1, dirlist, -1, dirname, dlen, 0);
1661fe9b 2367 xfer_dirs = save_xfer_dirs;
37802f40 2368 recurse = save_recurse;
53135fe8
WD
2369 if (do_progress)
2370 flist_count_offset += dirlist->count;
37802f40 2371
564ef546
WD
2372 clean_flist(dirlist, 0, 0);
2373
45478cc7 2374 if (verbose > 3)
32cbfe7b 2375 output_flist(dirlist);
45478cc7 2376
32cbfe7b 2377 return dirlist;
45478cc7 2378}