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