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