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