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