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