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