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