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