Mention the --bwlimit change.
[rsync/rsync.git] / flist.c
CommitLineData
dbda5fbf 1/*
c627d613
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
736a6a29 4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
dbda5fbf 5
c627d613
AT
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
dbda5fbf 10
c627d613
AT
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
dbda5fbf 15
c627d613
AT
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
736a6a29
MP
21/** @file flist.c
22 * Generate and receive file lists
23 *
172875cf
MP
24 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
25 *
736a6a29 26 **/
c627d613
AT
27
28#include "rsync.h"
29
a800434a
AT
30extern struct stats stats;
31
c627d613 32extern int verbose;
1bbd10fe 33extern int do_progress;
f05f993e 34extern int am_root;
c627d613 35extern int am_server;
a8726d2a 36extern int am_daemon;
c627d613 37extern int always_checksum;
983b1ed3
WD
38extern int module_id;
39extern int ignore_errors;
4836c3ee 40extern int numeric_ids;
c627d613
AT
41
42extern int cvs_exclude;
43
a06d19e3 44extern int recurse;
808c57c3 45extern char curr_dir[MAXPATHLEN];
24d0fcde
WD
46extern char *files_from;
47extern int filesfrom_fd;
a06d19e3 48
c627d613 49extern int one_file_system;
c627d613 50extern int preserve_links;
dc5ddbcc 51extern int preserve_hard_links;
c627d613
AT
52extern int preserve_perms;
53extern int preserve_devices;
54extern int preserve_uid;
55extern int preserve_gid;
6574b4f7 56extern int relative_paths;
24d0fcde 57extern int implied_dirs;
82306bf6 58extern int copy_links;
b5313607 59extern int copy_unsafe_links;
d04e9c51 60extern int protocol_version;
cb13abfe 61extern int sanitize_paths;
c627d613 62
6902ed17
MP
63extern int read_batch;
64extern int write_batch;
65
bf6dcd17
WD
66extern struct exclude_list_struct exclude_list;
67extern struct exclude_list_struct server_exclude_list;
68extern struct exclude_list_struct local_exclude_list;
c627d613 69
06c28400
WD
70int io_error;
71
fea4db62 72static char empty_sum[MD4_SUM_LENGTH];
9935066b 73static unsigned int file_struct_len;
3d382777 74
827c37f6 75static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
cefed3e8 76static void output_flist(struct file_list *flist);
0199b05f 77
61dec11a
WD
78void init_flist(void)
79{
1f9ae80a 80 struct file_struct f;
61dec11a 81
1f9ae80a 82 /* Figure out how big the file_struct is without trailing padding */
7cf8e8d0 83 file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
61dec11a
WD
84}
85
86
1bbd10fe 87static int show_filelist_p(void)
58225000 88{
24d0fcde 89 return verbose && (recurse || files_from) && !am_server;
1bbd10fe 90}
ebed4c3a 91
1bbd10fe
DD
92static void start_filelist_progress(char *kind)
93{
94 rprintf(FINFO, "%s ... ", kind);
95 if ((verbose > 1) || do_progress)
96 rprintf(FINFO, "\n");
97 rflush(FINFO);
58225000
MP
98}
99
db719fb0 100
d27cbec5 101static void emit_filelist_progress(const struct file_list *flist)
db719fb0 102{
d27cbec5 103 rprintf(FINFO, " %d files...\r", flist->count);
db719fb0
MP
104}
105
106
d27cbec5 107static void maybe_emit_filelist_progress(const struct file_list *flist)
58225000 108{
d27cbec5
DD
109 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
110 emit_filelist_progress(flist);
58225000
MP
111}
112
113
1bbd10fe 114static void finish_filelist_progress(const struct file_list *flist)
58225000 115{
1bbd10fe
DD
116 if (do_progress) {
117 /* This overwrites the progress line */
c7b562be
MP
118 rprintf(FINFO, "%d file%sto consider\n",
119 flist->count, flist->count == 1 ? " " : "s ");
b7736c79 120 } else
1bbd10fe 121 rprintf(FINFO, "done\n");
58225000
MP
122}
123
86943126
MP
124void show_flist_stats(void)
125{
126 /* Nothing yet */
127}
128
129
f7632fc6
AT
130static void list_file_entry(struct file_struct *f)
131{
740819ef 132 char perms[11];
f7632fc6 133
7212be92
DD
134 if (!f->basename)
135 /* this can happen if duplicate names were removed */
136 return;
137
740819ef
MP
138 permstring(perms, f->mode);
139
0d162bd1 140#if SUPPORT_LINKS
f7632fc6 141 if (preserve_links && S_ISLNK(f->mode)) {
ebed4c3a
MP
142 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
143 perms,
144 (double) f->length, timestring(f->modtime),
728d0922 145 f_name(f), f->u.link);
0d162bd1
WD
146 } else
147#endif
ebed4c3a
MP
148 rprintf(FINFO, "%s %11.0f %s %s\n",
149 perms,
150 (double) f->length, timestring(f->modtime),
151 f_name(f));
f7632fc6
AT
152}
153
154
bd9e9ecc
MP
155/**
156 * Stat either a symlink or its referent, depending on the settings of
157 * copy_links, copy_unsafe_links, etc.
158 *
4e5db0ad
MP
159 * @retval -1 on error
160 *
161 * @retval 0 for success
162 *
163 * @post If @p path is a symlink, then @p linkbuf (of size @c
bd9e9ecc 164 * MAXPATHLEN) contains the symlink target.
4e5db0ad
MP
165 *
166 * @post @p buffer contains information about the link or the
167 * referrent as appropriate, if they exist.
bd9e9ecc 168 **/
b7736c79 169int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
b5313607
DD
170{
171#if SUPPORT_LINKS
b7736c79 172 if (copy_links)
6f2623fd 173 return do_stat(path, buffer);
b7736c79 174 if (do_lstat(path, buffer) == -1)
b5313607 175 return -1;
6f2623fd 176 if (S_ISLNK(buffer->st_mode)) {
b7736c79 177 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
dbda5fbf 178 if (l == -1)
b5313607 179 return -1;
6f2623fd 180 linkbuf[l] = 0;
fc638474
DD
181 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
182 if (verbose > 1) {
dbda5fbf 183 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
fc638474
DD
184 path, linkbuf);
185 }
6f2623fd 186 return do_stat(path, buffer);
b5313607
DD
187 }
188 }
189 return 0;
190#else
6f2623fd 191 return do_stat(path, buffer);
b5313607
DD
192#endif
193}
194
6f2623fd 195int link_stat(const char *path, STRUCT_STAT * buffer)
82306bf6
AT
196{
197#if SUPPORT_LINKS
b7736c79 198 if (copy_links)
6f2623fd 199 return do_stat(path, buffer);
b7736c79 200 return do_lstat(path, buffer);
82306bf6 201#else
6f2623fd 202 return do_stat(path, buffer);
82306bf6
AT
203#endif
204}
205
c627d613 206/*
429f9828
WD
207 * This function is used to check if a file should be included/excluded
208 * from the list of files based on its name and type etc. The value of
209 * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
c627d613 210 */
429f9828 211static int check_exclude_file(char *fname, int is_dir, int exclude_level)
c627d613 212{
9fdb334e
WD
213 int rc;
214
7d687932 215#if 0 /* This currently never happens, so avoid a useless compare. */
429f9828
WD
216 if (exclude_level == NO_EXCLUDES)
217 return 0;
218#endif
6931c138 219 if (fname) {
429f9828 220 /* never exclude '.', even if somebody does --exclude '*' */
6931c138
WD
221 if (fname[0] == '.' && !fname[1])
222 return 0;
223 /* Handle the -R version of the '.' dir. */
224 if (fname[0] == '/') {
225 int len = strlen(fname);
226 if (fname[len-1] == '.' && fname[len-2] == '/')
227 return 0;
228 }
76e26e10 229 }
bf6dcd17 230 if (server_exclude_list.head
9fdb334e 231 && check_exclude(&server_exclude_list, fname, is_dir) < 0)
429f9828
WD
232 return 1;
233 if (exclude_level != ALL_EXCLUDES)
234 return 0;
9fdb334e
WD
235 if (exclude_list.head
236 && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
237 return rc < 0;
bf6dcd17 238 if (local_exclude_list.head
9fdb334e 239 && check_exclude(&local_exclude_list, fname, is_dir) < 0)
76e26e10 240 return 1;
76e26e10 241 return 0;
c627d613
AT
242}
243
244/* used by the one_file_system code */
245static dev_t filesystem_dev;
246
247static void set_filesystem(char *fname)
248{
ebed4c3a
MP
249 STRUCT_STAT st;
250 if (link_stat(fname, &st) != 0)
251 return;
252 filesystem_dev = st.st_dev;
c627d613
AT
253}
254
255
b280a1f4
AT
256static int to_wire_mode(mode_t mode)
257{
0d162bd1 258#if SUPPORT_LINKS
b7736c79 259 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
b280a1f4 260 return (mode & ~(_S_IFMT)) | 0120000;
0d162bd1 261#endif
ebed4c3a 262 return (int) mode;
b280a1f4
AT
263}
264
265static mode_t from_wire_mode(int mode)
266{
b7736c79 267 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
efe3037c 268 return (mode & ~(_S_IFMT)) | _S_IFLNK;
ebed4c3a 269 return (mode_t) mode;
b280a1f4
AT
270}
271
272
ebed4c3a 273static void send_directory(int f, struct file_list *flist, char *dir);
c627d613 274
3a6a366f 275static char *flist_dir;
882e6893 276static int flist_dir_len;
c627d613 277
3ec4dd97 278
d9d6bc52
MP
279/**
280 * Make sure @p flist is big enough to hold at least @p flist->count
281 * entries.
282 **/
a85906c7 283void flist_expand(struct file_list *flist)
d9d6bc52 284{
0501f363 285 struct file_struct **new_ptr;
2e7d1994 286
a85906c7
S
287 if (flist->count < flist->malloced)
288 return;
dbda5fbf 289
a85906c7
S
290 if (flist->malloced < FLIST_START)
291 flist->malloced = FLIST_START;
292 else if (flist->malloced >= FLIST_LINEAR)
293 flist->malloced += FLIST_LINEAR;
294 else
295 flist->malloced *= 2;
296
297 /*
298 * In case count jumped or we are starting the list
299 * with a known size just set it.
300 */
301 if (flist->malloced < flist->count)
302 flist->malloced = flist->count;
303
0501f363
WD
304 new_ptr = realloc_array(flist->files, struct file_struct *,
305 flist->malloced);
2e7d1994 306
a85906c7
S
307 if (verbose >= 2) {
308 rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
309 who_am_i(),
310 (double) sizeof flist->files[0] * flist->malloced,
311 (new_ptr == flist->files) ? " not" : "");
d9d6bc52 312 }
a85906c7 313
0501f363 314 flist->files = new_ptr;
a85906c7
S
315
316 if (!flist->files)
317 out_of_memory("flist_expand");
d9d6bc52
MP
318}
319
7b1a0c19 320void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
c627d613 321{
1ef00d20 322 unsigned short flags;
5911fee5
WD
323 static time_t modtime;
324 static mode_t mode;
4124540d
WD
325 static uint64 dev;
326 static dev_t rdev;
9c5e91f8 327 static uint32 rdev_major;
5911fee5
WD
328 static uid_t uid;
329 static gid_t gid;
330 static char lastname[MAXPATHLEN];
b7736c79 331 char *fname, fbuf[MAXPATHLEN];
ebed4c3a 332 int l1, l2;
72914a60 333
ebed4c3a
MP
334 if (f == -1)
335 return;
72914a60
AT
336
337 if (!file) {
ebed4c3a 338 write_byte(f, 0);
5911fee5 339 modtime = 0, mode = 0;
4124540d 340 dev = 0, rdev = makedev(0, 0);
9c5e91f8 341 rdev_major = 0;
5911fee5
WD
342 uid = 0, gid = 0;
343 *lastname = '\0';
72914a60
AT
344 return;
345 }
346
eca2adb4
MP
347 io_write_phase = "send_file_entry";
348
882e6893 349 fname = f_name_to(file, fbuf);
72914a60
AT
350
351 flags = base_flags;
352
30f337c9 353 if (file->mode == mode)
d01d15e0 354 flags |= XMIT_SAME_MODE;
1ef00d20 355 else
30f337c9 356 mode = file->mode;
75bc8600
WD
357 if (preserve_devices) {
358 if (protocol_version < 28) {
728d0922 359 if (IS_DEVICE(mode)) {
9c5e91f8
WD
360 if (file->u.rdev == rdev)
361 flags |= XMIT_SAME_RDEV_pre28;
362 else
728d0922
WD
363 rdev = file->u.rdev;
364 } else
9c5e91f8 365 rdev = makedev(0, 0);
728d0922 366 } else if (IS_DEVICE(mode)) {
84a3efa0 367 rdev = file->u.rdev;
9c5e91f8
WD
368 if ((uint32)major(rdev) == rdev_major)
369 flags |= XMIT_SAME_RDEV_MAJOR;
84a3efa0 370 else
9c5e91f8
WD
371 rdev_major = major(rdev);
372 if ((uint32)minor(rdev) <= 0xFFu)
373 flags |= XMIT_RDEV_MINOR_IS_SMALL;
75bc8600
WD
374 }
375 }
30f337c9 376 if (file->uid == uid)
d01d15e0 377 flags |= XMIT_SAME_UID;
1ef00d20 378 else
30f337c9
WD
379 uid = file->uid;
380 if (file->gid == gid)
d01d15e0 381 flags |= XMIT_SAME_GID;
1ef00d20 382 else
30f337c9
WD
383 gid = file->gid;
384 if (file->modtime == modtime)
d01d15e0 385 flags |= XMIT_SAME_TIME;
1ef00d20 386 else
30f337c9 387 modtime = file->modtime;
a289addd 388
0d162bd1 389#if SUPPORT_HARD_LINKS
92cc9dd7
WD
390 if (file->link_u.idev) {
391 if (file->F_DEV == dev) {
c4b4df4f 392 if (protocol_version >= 28)
d01d15e0 393 flags |= XMIT_SAME_DEV;
728d0922 394 } else
92cc9dd7 395 dev = file->F_DEV;
d01d15e0 396 flags |= XMIT_HAS_IDEV_DATA;
c4b4df4f 397 }
0d162bd1 398#endif
ebed4c3a
MP
399
400 for (l1 = 0;
3e491682
S
401 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
402 l1++) {}
eddd5d12 403 l2 = strlen(fname+l1);
72914a60 404
ebed4c3a 405 if (l1 > 0)
d01d15e0 406 flags |= XMIT_SAME_NAME;
ebed4c3a 407 if (l2 > 255)
d01d15e0 408 flags |= XMIT_LONG_NAME;
72914a60 409
1aa4caf3
WD
410 /* We must make sure we don't send a zero flag byte or the
411 * other end will terminate the flist transfer. Note that
412 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
413 * it's harmless way to add a bit to the first flag byte. */
75bc8600 414 if (protocol_version >= 28) {
1aa4caf3
WD
415 if (!flags && !S_ISDIR(mode))
416 flags |= XMIT_TOP_DIR;
417 if ((flags & 0xFF00) || !flags) {
d01d15e0 418 flags |= XMIT_EXTENDED_FLAGS;
75bc8600
WD
419 write_byte(f, flags);
420 write_byte(f, flags >> 8);
421 } else
422 write_byte(f, flags);
423 } else {
1aa4caf3
WD
424 if (!(flags & 0xFF) && !S_ISDIR(mode))
425 flags |= XMIT_TOP_DIR;
0a982011 426 if (!(flags & 0xFF))
d01d15e0 427 flags |= XMIT_LONG_NAME;
75bc8600
WD
428 write_byte(f, flags);
429 }
d01d15e0 430 if (flags & XMIT_SAME_NAME)
ebed4c3a 431 write_byte(f, l1);
d01d15e0 432 if (flags & XMIT_LONG_NAME)
ebed4c3a 433 write_int(f, l2);
72914a60 434 else
ebed4c3a
MP
435 write_byte(f, l2);
436 write_buf(f, fname + l1, l2);
72914a60 437
ebed4c3a 438 write_longint(f, file->length);
d01d15e0 439 if (!(flags & XMIT_SAME_TIME))
30f337c9 440 write_int(f, modtime);
d01d15e0 441 if (!(flags & XMIT_SAME_MODE))
30f337c9 442 write_int(f, to_wire_mode(mode));
d01d15e0 443 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
9f7b8c3b
WD
444 if (!numeric_ids)
445 add_uid(uid);
30f337c9 446 write_int(f, uid);
72914a60 447 }
d01d15e0 448 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
9f7b8c3b
WD
449 if (!numeric_ids)
450 add_gid(gid);
30f337c9 451 write_int(f, gid);
72914a60 452 }
30f337c9 453 if (preserve_devices && IS_DEVICE(mode)) {
9c5e91f8
WD
454 if (protocol_version < 28) {
455 if (!(flags & XMIT_SAME_RDEV_pre28))
456 write_int(f, (int)rdev);
457 } else {
458 if (!(flags & XMIT_SAME_RDEV_MAJOR))
459 write_int(f, major(rdev));
460 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
461 write_byte(f, minor(rdev));
462 else
463 write_int(f, minor(rdev));
464 }
75bc8600 465 }
c627d613
AT
466
467#if SUPPORT_LINKS
30f337c9 468 if (preserve_links && S_ISLNK(mode)) {
306ffb8c
WD
469 int len = strlen(file->u.link);
470 write_int(f, len);
471 write_buf(f, file->u.link, len);
72914a60 472 }
c627d613
AT
473#endif
474
dc5ddbcc 475#if SUPPORT_HARD_LINKS
d01d15e0 476 if (flags & XMIT_HAS_IDEV_DATA) {
4124540d 477 if (protocol_version < 26) {
736a6a29 478 /* 32-bit dev_t and ino_t */
30f337c9 479 write_int(f, dev);
92cc9dd7 480 write_int(f, file->F_INODE);
736a6a29
MP
481 } else {
482 /* 64-bit dev_t and ino_t */
4124540d
WD
483 if (!(flags & XMIT_SAME_DEV))
484 write_longint(f, dev);
92cc9dd7 485 write_longint(f, file->F_INODE);
736a6a29 486 }
72914a60 487 }
dc5ddbcc
AT
488#endif
489
728d0922
WD
490 if (always_checksum) {
491 char *sum;
492 if (S_ISREG(mode))
493 sum = file->u.sum;
494 else if (protocol_version < 28) {
495 /* Prior to 28, we sent a useless set of nulls. */
496 sum = empty_sum;
497 } else
498 sum = NULL;
499 if (sum) {
3e491682
S
500 write_buf(f, sum,
501 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
728d0922 502 }
ebed4c3a 503 }
182dca5c 504
ebed4c3a 505 strlcpy(lastname, fname, MAXPATHLEN);
eca2adb4
MP
506
507 io_write_phase = "unknown";
182dca5c
AT
508}
509
510
511
9935066b
S
512void receive_file_entry(struct file_struct **fptr, unsigned short flags,
513 struct file_list *flist, int f)
182dca5c 514{
5911fee5
WD
515 static time_t modtime;
516 static mode_t mode;
4124540d
WD
517 static uint64 dev;
518 static dev_t rdev;
9c5e91f8 519 static uint32 rdev_major;
5911fee5
WD
520 static uid_t uid;
521 static gid_t gid;
a289addd
WD
522 static char lastname[MAXPATHLEN], *lastdir;
523 static int lastdir_len = -1;
72914a60 524 char thisname[MAXPATHLEN];
ebed4c3a 525 unsigned int l1 = 0, l2 = 0;
a1d55ad0 526 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd
WD
527 OFF_T file_length;
528 char *basename, *dirname, *bp;
72914a60
AT
529 struct file_struct *file;
530
5911fee5
WD
531 if (!fptr) {
532 modtime = 0, mode = 0;
4124540d 533 dev = 0, rdev = makedev(0, 0);
9c5e91f8 534 rdev_major = 0;
5911fee5
WD
535 uid = 0, gid = 0;
536 *lastname = '\0';
93272700 537 lastdir_len = -1;
5911fee5
WD
538 return;
539 }
540
d01d15e0 541 if (flags & XMIT_SAME_NAME)
72914a60 542 l1 = read_byte(f);
ebed4c3a 543
d01d15e0 544 if (flags & XMIT_LONG_NAME)
72914a60
AT
545 l2 = read_int(f);
546 else
547 l2 = read_byte(f);
548
ebed4c3a
MP
549 if (l2 >= MAXPATHLEN - l1) {
550 rprintf(FERROR,
551 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
d0fd26aa
AT
552 flags, l1, l2, lastname);
553 overflow("receive_file_entry");
554 }
72914a60 555
ebed4c3a
MP
556 strlcpy(thisname, lastname, l1 + 1);
557 read_sbuf(f, &thisname[l1], l2);
558 thisname[l1 + l2] = 0;
72914a60 559
ebed4c3a 560 strlcpy(lastname, thisname, MAXPATHLEN);
72914a60
AT
561
562 clean_fname(thisname);
563
0d162bd1 564 if (sanitize_paths)
cb13abfe 565 sanitize_path(thisname, NULL);
cb13abfe 566
a289addd
WD
567 if ((basename = strrchr(thisname, '/')) != NULL) {
568 dirname_len = ++basename - thisname; /* counts future '\0' */
569 if (lastdir_len == dirname_len - 1
570 && strncmp(thisname, lastdir, lastdir_len) == 0) {
571 dirname = lastdir;
572 dirname_len = 0; /* indicates no copy is needed */
573 } else
574 dirname = thisname;
72914a60 575 } else {
a289addd
WD
576 basename = thisname;
577 dirname = NULL;
578 dirname_len = 0;
72914a60 579 }
a289addd 580 basename_len = strlen(basename) + 1; /* count the '\0' */
72914a60 581
a289addd 582 file_length = read_longint(f);
d01d15e0 583 if (!(flags & XMIT_SAME_TIME))
30f337c9 584 modtime = (time_t)read_int(f);
d01d15e0 585 if (!(flags & XMIT_SAME_MODE))
30f337c9 586 mode = from_wire_mode(read_int(f));
1ef00d20 587
a289addd
WD
588 if (preserve_uid && !(flags & XMIT_SAME_UID))
589 uid = (uid_t)read_int(f);
590 if (preserve_gid && !(flags & XMIT_SAME_GID))
591 gid = (gid_t)read_int(f);
592
1ef00d20 593 if (preserve_devices) {
75bc8600 594 if (protocol_version < 28) {
30f337c9 595 if (IS_DEVICE(mode)) {
d01d15e0 596 if (!(flags & XMIT_SAME_RDEV_pre28))
9c5e91f8 597 rdev = (dev_t)read_int(f);
75bc8600 598 } else
9c5e91f8 599 rdev = makedev(0, 0);
30f337c9 600 } else if (IS_DEVICE(mode)) {
9c5e91f8
WD
601 uint32 rdev_minor;
602 if (!(flags & XMIT_SAME_RDEV_MAJOR))
603 rdev_major = read_int(f);
604 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
605 rdev_minor = read_byte(f);
606 else
607 rdev_minor = read_int(f);
608 rdev = makedev(rdev_major, rdev_minor);
1ef00d20 609 }
b7736c79 610 }
72914a60 611
0d162bd1 612#if SUPPORT_LINKS
30f337c9 613 if (preserve_links && S_ISLNK(mode)) {
a289addd
WD
614 linkname_len = read_int(f) + 1; /* count the '\0' */
615 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
616 rprintf(FERROR, "overflow: linkname_len=%d\n",
617 linkname_len - 1);
9dd891bb
MP
618 overflow("receive_file_entry");
619 }
72914a60 620 }
a289addd 621 else
0d162bd1 622#endif
a289addd 623 linkname_len = 0;
0d162bd1 624
a289addd
WD
625 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
626
61dec11a 627 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
628 + linkname_len + sum_len;
629 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
630
a289addd 631 file = *fptr = (struct file_struct *)bp;
9935066b 632 memset(bp, 0, file_struct_len);
61dec11a 633 bp += file_struct_len;
a289addd
WD
634
635 file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
636 file->modtime = modtime;
637 file->length = file_length;
638 file->mode = mode;
639 file->uid = uid;
640 file->gid = gid;
641
642 if (dirname_len) {
643 file->dirname = lastdir = bp;
644 lastdir_len = dirname_len - 1;
645 memcpy(bp, dirname, dirname_len - 1);
646 bp += dirname_len;
647 bp[-1] = '\0';
648 } else if (dirname)
649 file->dirname = dirname;
650
651 file->basename = bp;
652 memcpy(bp, basename, basename_len);
653 bp += basename_len;
654
655 if (preserve_devices && IS_DEVICE(mode))
656 file->u.rdev = rdev;
657
658#if SUPPORT_LINKS
659 if (linkname_len) {
660 file->u.link = bp;
661 read_sbuf(f, bp, linkname_len - 1);
662 if (sanitize_paths)
663 sanitize_path(bp, lastdir);
664 bp += linkname_len;
665 }
666#endif
667
668#if SUPPORT_HARD_LINKS
9935066b
S
669 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
670 flags |= XMIT_HAS_IDEV_DATA;
97a67bdf 671 if (flags & XMIT_HAS_IDEV_DATA) {
4124540d
WD
672 uint64 inode;
673 if (protocol_version < 26) {
674 dev = read_int(f);
9935066b 675 inode = read_int(f);
736a6a29 676 } else {
4124540d
WD
677 if (!(flags & XMIT_SAME_DEV))
678 dev = read_longint(f);
9935066b
S
679 inode = read_longint(f);
680 }
681 if (flist->hlink_pool) {
5bf63a11
S
682 file->link_u.idev = pool_talloc(flist->hlink_pool,
683 struct idev, 1, "inode_table");
9935066b
S
684 file->F_INODE = inode;
685 file->F_DEV = dev;
736a6a29 686 }
72914a60 687 }
dc5ddbcc 688#endif
ebed4c3a 689
72914a60 690 if (always_checksum) {
7c4f063b 691 char *sum;
a289addd
WD
692 if (sum_len) {
693 file->u.sum = sum = bp;
694 /*bp += sum_len;*/
fea4db62
WD
695 } else if (protocol_version < 28) {
696 /* Prior to 28, we get a useless set of nulls. */
7c4f063b 697 sum = empty_sum;
fea4db62
WD
698 } else
699 sum = NULL;
700 if (sum) {
3e491682
S
701 read_buf(f, sum,
702 protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
fea4db62 703 }
72914a60 704 }
ebed4c3a 705
72914a60
AT
706 if (!preserve_perms) {
707 extern int orig_umask;
708 /* set an appropriate set of permissions based on original
fea4db62 709 * permissions and umask. This emulates what GNU cp does */
72914a60
AT
710 file->mode &= ~orig_umask;
711 }
c627d613
AT
712}
713
714
db719fb0
MP
715/**
716 * Create a file_struct for a named file by reading its stat()
717 * information and performing extensive checks against global
718 * options.
719 *
720 * @return the new file, or NULL if there was an error or this file
721 * should be excluded.
722 *
723 * @todo There is a small optimization opportunity here to avoid
724 * stat()ing the file in some circumstances, which has a certain cost.
725 * We are called immediately after doing readdir(), and so we may
726 * already know the d_type of the file. We could for example avoid
727 * statting directories if we're not recursing, but this is not a very
728 * important case. Some systems may not have d_type.
729 **/
9935066b
S
730struct file_struct *make_file(char *fname,
731 struct file_list *flist, int exclude_level)
c627d613 732{
a289addd
WD
733 static char *lastdir;
734 static int lastdir_len = -1;
3ec4dd97 735 struct file_struct *file;
bcacc18b 736 STRUCT_STAT st;
3ec4dd97 737 char sum[SUM_LENGTH];
1923b1fc 738 char thisname[MAXPATHLEN];
e0870f1d 739 char linkname[MAXPATHLEN];
a1d55ad0 740 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd 741 char *basename, *dirname, *bp;
4844449a 742 unsigned short flags = 0;
3ec4dd97 743
d508258a 744 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
7de2483f 745 lastdir_len = -1;
9935066b 746
1923b1fc
WD
747 if (strlcpy(thisname, fname, sizeof thisname)
748 >= sizeof thisname - flist_dir_len) {
882e6893
WD
749 rprintf(FINFO, "skipping overly long name: %s\n", fname);
750 return NULL;
751 }
1923b1fc 752 clean_fname(thisname);
b7736c79 753 if (sanitize_paths)
1923b1fc 754 sanitize_path(thisname, NULL);
3ec4dd97 755
ebed4c3a 756 memset(sum, 0, SUM_LENGTH);
3ec4dd97 757
e0870f1d 758 if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 759 int save_errno = errno;
a8726d2a
WD
760 if (errno == ENOENT) {
761 enum logcode c = am_daemon && protocol_version < 28
762 ? FERROR : FINFO;
dbda5fbf 763 /* either symlink pointing nowhere or file that
1b85e3f1
DD
764 * was removed during rsync run; see if excluded
765 * before reporting an error */
a8726d2a
WD
766 if (exclude_level != NO_EXCLUDES
767 && check_exclude_file(thisname, 0, exclude_level)) {
76e26e10
DD
768 /* file is excluded anyway, ignore silently */
769 return NULL;
770 }
a8726d2a
WD
771 io_error |= IOERR_VANISHED;
772 rprintf(c, "file has vanished: %s\n",
773 full_fname(thisname));
774 }
775 else {
776 io_error |= IOERR_GENERAL;
d62bcc17
WD
777 rsyserr(FERROR, save_errno, "readlink %s failed",
778 full_fname(thisname));
76e26e10 779 }
3ec4dd97
AT
780 return NULL;
781 }
c627d613 782
429f9828
WD
783 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
784 if (exclude_level == NO_EXCLUDES)
ebed4c3a 785 goto skip_excludes;
ac1a0994 786
24d0fcde 787 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1923b1fc 788 rprintf(FINFO, "skipping directory %s\n", thisname);
3ec4dd97
AT
789 return NULL;
790 }
ebed4c3a 791
e90cdb8a
WD
792 /* We only care about directories because we need to avoid recursing
793 * into a mount-point directory, not to avoid copying a symlinked
794 * file if -L (or similar) was specified. */
795 if (one_file_system && st.st_dev != filesystem_dev
796 && S_ISDIR(st.st_mode))
4844449a 797 flags |= FLAG_MOUNT_POINT;
ebed4c3a 798
1923b1fc 799 if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
76e26e10
DD
800 return NULL;
801
1923b1fc 802 if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
ac1a0994
AT
803 return NULL;
804
3e491682 805skip_excludes:
ac1a0994 806
ea847c62
WD
807 if (verbose > 2) {
808 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1923b1fc 809 who_am_i(), thisname, exclude_level);
ea847c62 810 }
ebed4c3a 811
a289addd
WD
812 if ((basename = strrchr(thisname, '/')) != NULL) {
813 dirname_len = ++basename - thisname; /* counts future '\0' */
814 if (lastdir_len == dirname_len - 1
815 && strncmp(thisname, lastdir, lastdir_len) == 0) {
816 dirname = lastdir;
817 dirname_len = 0; /* indicates no copy is needed */
818 } else
819 dirname = thisname;
3ec4dd97 820 } else {
a289addd
WD
821 basename = thisname;
822 dirname = NULL;
823 dirname_len = 0;
3ec4dd97 824 }
a289addd 825 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 826
a289addd
WD
827#if SUPPORT_LINKS
828 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
829#else
830 linkname_len = 0;
831#endif
832
a289addd
WD
833 sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
834
61dec11a 835 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
836 + linkname_len + sum_len;
837 if (flist) {
838 bp = pool_alloc(flist->file_pool, alloc_len,
839 "receive_file_entry");
840 } else {
841 if (!(bp = new_array(char, alloc_len)))
842 out_of_memory("receive_file_entry");
843 }
844
a289addd 845 file = (struct file_struct *)bp;
9935066b 846 memset(bp, 0, file_struct_len);
61dec11a 847 bp += file_struct_len;
a289addd
WD
848
849 file->flags = flags;
3ec4dd97
AT
850 file->modtime = st.st_mtime;
851 file->length = st.st_size;
852 file->mode = st.st_mode;
853 file->uid = st.st_uid;
854 file->gid = st.st_gid;
0d162bd1 855
61dec11a 856#if SUPPORT_HARD_LINKS
9935066b
S
857 if (flist && flist->hlink_pool) {
858 if (protocol_version < 28) {
859 if (S_ISREG(st.st_mode))
860 file->link_u.idev = pool_talloc(
861 flist->hlink_pool, struct idev, 1,
862 "inode_table");
863 } else {
864 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
865 file->link_u.idev = pool_talloc(
866 flist->hlink_pool, struct idev, 1,
867 "inode_table");
868 }
869 }
870 if (file->link_u.idev) {
61dec11a
WD
871 file->F_DEV = st.st_dev;
872 file->F_INODE = st.st_ino;
873 }
874#endif
875
a289addd
WD
876 if (dirname_len) {
877 file->dirname = lastdir = bp;
878 lastdir_len = dirname_len - 1;
879 memcpy(bp, dirname, dirname_len - 1);
880 bp += dirname_len;
881 bp[-1] = '\0';
882 } else if (dirname)
883 file->dirname = dirname;
884
885 file->basename = bp;
886 memcpy(bp, basename, basename_len);
887 bp += basename_len;
888
0d162bd1
WD
889#ifdef HAVE_STRUCT_STAT_ST_RDEV
890 if (preserve_devices && IS_DEVICE(st.st_mode))
891 file->u.rdev = st.st_rdev;
892#endif
893
894#if SUPPORT_LINKS
a289addd
WD
895 if (linkname_len) {
896 file->u.link = bp;
897 memcpy(bp, linkname, linkname_len);
898 bp += linkname_len;
899 }
0d162bd1
WD
900#endif
901
a289addd
WD
902 if (sum_len) {
903 file->u.sum = bp;
904 file_checksum(thisname, bp, st.st_size);
905 /*bp += sum_len;*/
ebed4c3a 906 }
c627d613 907
882e6893 908 file->basedir = flist_dir;
c627d613 909
3ec4dd97 910 if (!S_ISDIR(st.st_mode))
a800434a 911 stats.total_size += st.st_size;
c627d613 912
3ec4dd97 913 return file;
c627d613
AT
914}
915
916
ebed4c3a 917void send_file_name(int f, struct file_list *flist, char *fname,
1ef00d20 918 int recursive, unsigned short base_flags)
c627d613 919{
ebed4c3a 920 struct file_struct *file;
b7736c79 921 char fbuf[MAXPATHLEN];
429f9828 922 extern int delete_excluded;
ebed4c3a 923
429f9828 924 /* f is set to -1 when calculating deletion file list */
9935066b
S
925 file = make_file(fname, flist,
926 f == -1 && delete_excluded? SERVER_EXCLUDES : ALL_EXCLUDES);
ebed4c3a
MP
927
928 if (!file)
929 return;
930
db719fb0 931 maybe_emit_filelist_progress(flist);
ebed4c3a 932
d9d6bc52 933 flist_expand(flist);
ebed4c3a 934
64c3523a 935 if (write_batch)
d01d15e0 936 file->flags |= FLAG_TOP_DIR;
ebed4c3a 937
c120ff37 938 if (file->basename[0]) {
ebed4c3a
MP
939 flist->files[flist->count++] = file;
940 send_file_entry(file, f, base_flags);
941 }
942
4844449a
WD
943 if (recursive && S_ISDIR(file->mode)
944 && !(file->flags & FLAG_MOUNT_POINT)) {
bf6dcd17 945 struct exclude_list_struct last_list = local_exclude_list;
8ef81dd4 946 local_exclude_list.head = local_exclude_list.tail = NULL;
882e6893 947 send_directory(f, flist, f_name_to(file, fbuf));
de91e757
WD
948 if (verbose > 2) {
949 rprintf(FINFO, "[%s] popping %sexclude list\n",
950 who_am_i(), local_exclude_list.debug_type);
951 }
8cbf495a 952 clear_exclude_list(&local_exclude_list);
bf6dcd17 953 local_exclude_list = last_list;
ebed4c3a 954 }
c627d613
AT
955}
956
957
ebed4c3a 958static void send_directory(int f, struct file_list *flist, char *dir)
c627d613 959{
3ec4dd97
AT
960 DIR *d;
961 struct dirent *di;
962 char fname[MAXPATHLEN];
eddd5d12 963 unsigned int offset;
3ec4dd97
AT
964 char *p;
965
966 d = opendir(dir);
967 if (!d) {
06c28400 968 io_error |= IOERR_GENERAL;
d62bcc17 969 rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir));
3ec4dd97
AT
970 return;
971 }
c627d613 972
eddd5d12
WD
973 offset = strlcpy(fname, dir, MAXPATHLEN);
974 p = fname + offset;
975 if (offset >= MAXPATHLEN || p[-1] != '/') {
976 if (offset >= MAXPATHLEN - 1) {
06c28400 977 io_error |= IOERR_GENERAL;
ea42541f
WD
978 rprintf(FERROR, "skipping long-named directory: %s\n",
979 full_fname(fname));
3ec4dd97
AT
980 closedir(d);
981 return;
982 }
eddd5d12
WD
983 *p++ = '/';
984 offset++;
3ec4dd97 985 }
c627d613 986
3ec4dd97 987 if (cvs_exclude) {
eddd5d12 988 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
9c5e91f8
WD
989 < MAXPATHLEN - offset) {
990 add_exclude_file(&local_exclude_list, fname,
90a973fe 991 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
9c5e91f8 992 } else {
06c28400 993 io_error |= IOERR_GENERAL;
ebed4c3a
MP
994 rprintf(FINFO,
995 "cannot cvs-exclude in long-named directory %s\n",
ea42541f 996 full_fname(fname));
3ec4dd97 997 }
ebed4c3a
MP
998 }
999
6a7cc46c 1000 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1001 char *dname = d_name(di);
6a7cc46c
S
1002 if (dname[0] == '.' && (dname[1] == '\0'
1003 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1004 continue;
eddd5d12
WD
1005 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
1006 send_file_name(f, flist, fname, recurse, 0);
1007 else {
1008 io_error |= IOERR_GENERAL;
1009 rprintf(FINFO,
1010 "cannot send long-named file %s\n",
1011 full_fname(fname));
1012 }
3ec4dd97 1013 }
6a7cc46c 1014 if (errno) {
06c28400 1015 io_error |= IOERR_GENERAL;
d62bcc17 1016 rsyserr(FERROR, errno, "readdir(%s)", dir);
6a7cc46c 1017 }
c627d613 1018
3ec4dd97 1019 closedir(d);
c627d613
AT
1020}
1021
1022
c4fea82f 1023/**
429f9828
WD
1024 * The delete_files() function in receiver.c sets f to -1 so that we just
1025 * construct the file list in memory without sending it over the wire. It
1026 * also has the side-effect of ignoring user-excludes if delete_excluded
1027 * is set (so that the delete list includes user-excluded files).
c4fea82f 1028 **/
ebed4c3a 1029struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1030{
24d0fcde 1031 int l;
bcacc18b 1032 STRUCT_STAT st;
808c57c3 1033 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 1034 char lastpath[MAXPATHLEN] = "";
649d65ed 1035 struct file_list *flist;
a800434a 1036 int64 start_write;
24d0fcde 1037 int use_ff_fd = 0;
649d65ed 1038
1bbd10fe
DD
1039 if (show_filelist_p() && f != -1)
1040 start_filelist_progress("building file list");
c627d613 1041
a800434a
AT
1042 start_write = stats.total_written;
1043
9935066b
S
1044 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1045 "send_file_list");
c627d613 1046
d6dead6b 1047 if (f != -1) {
76c21947 1048 io_start_buffering_out(f);
24d0fcde 1049 if (filesfrom_fd >= 0) {
808c57c3 1050 if (argv[0] && !push_dir(argv[0])) {
d62bcc17
WD
1051 rsyserr(FERROR, errno, "push_dir %s failed",
1052 full_fname(argv[0]));
24d0fcde
WD
1053 exit_cleanup(RERR_FILESELECT);
1054 }
1055 use_ff_fd = 1;
1056 }
d6dead6b
AT
1057 }
1058
24d0fcde 1059 while (1) {
fc638474
DD
1060 char fname2[MAXPATHLEN];
1061 char *fname = fname2;
c627d613 1062
24d0fcde
WD
1063 if (use_ff_fd) {
1064 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1065 break;
1066 sanitize_path(fname, NULL);
1067 } else {
1068 if (argc-- == 0)
1069 break;
1070 strlcpy(fname, *argv++, MAXPATHLEN);
1071 if (sanitize_paths)
1072 sanitize_path(fname, NULL);
1073 }
c627d613 1074
649d65ed 1075 l = strlen(fname);
6931c138
WD
1076 if (fname[l - 1] == '/') {
1077 if (l == 2 && fname[0] == '.') {
1078 /* Turn "./" into just "." rather than "./." */
1079 fname[1] = '\0';
eddd5d12
WD
1080 } else if (l < MAXPATHLEN) {
1081 fname[l++] = '.';
1082 fname[l] = '\0';
53f821f1 1083 }
649d65ed 1084 }
c627d613 1085
ebed4c3a 1086 if (link_stat(fname, &st) != 0) {
f76933b1 1087 if (f != -1) {
06c28400 1088 io_error |= IOERR_GENERAL;
d62bcc17
WD
1089 rsyserr(FERROR, errno, "link_stat %s failed",
1090 full_fname(fname));
f76933b1 1091 }
649d65ed
AT
1092 continue;
1093 }
c627d613 1094
24d0fcde 1095 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
ebed4c3a 1096 rprintf(FINFO, "skipping directory %s\n", fname);
649d65ed
AT
1097 continue;
1098 }
c627d613 1099
649d65ed 1100 dir = NULL;
808c57c3 1101 olddir[0] = '\0';
649d65ed
AT
1102
1103 if (!relative_paths) {
ebed4c3a 1104 p = strrchr(fname, '/');
649d65ed
AT
1105 if (p) {
1106 *p = 0;
ebed4c3a 1107 if (p == fname)
649d65ed
AT
1108 dir = "/";
1109 else
ebed4c3a
MP
1110 dir = fname;
1111 fname = p + 1;
649d65ed 1112 }
24d0fcde 1113 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
649d65ed
AT
1114 /* this ensures we send the intermediate directories,
1115 thus getting their permissions right */
2154309a 1116 char *lp = lastpath, *fn = fname, *slash = fname;
649d65ed 1117 *p = 0;
2154309a
WD
1118 /* Skip any initial directories in our path that we
1119 * have in common with lastpath. */
1120 while (*fn && *lp == *fn) {
1121 if (*fn == '/')
1122 slash = fn;
1123 lp++, fn++;
1124 }
1125 *p = '/';
1126 if (fn != p || (*lp && *lp != '/')) {
1127 int copy_links_saved = copy_links;
1128 int recurse_saved = recurse;
1129 copy_links = copy_unsafe_links;
1130 /* set recurse to 1 to prevent make_file
1131 * from ignoring directory, but still
1132 * turn off the recursive parameter to
1133 * send_file_name */
1134 recurse = 1;
1135 while ((slash = strchr(slash+1, '/')) != 0) {
1136 *slash = 0;
1137 send_file_name(f, flist, fname, 0, 0);
1138 *slash = '/';
649d65ed 1139 }
2154309a
WD
1140 copy_links = copy_links_saved;
1141 recurse = recurse_saved;
1142 *p = 0;
1143 strlcpy(lastpath, fname, sizeof lastpath);
649d65ed
AT
1144 *p = '/';
1145 }
1146 }
ebed4c3a 1147
649d65ed
AT
1148 if (!*fname)
1149 fname = ".";
ebed4c3a 1150
649d65ed 1151 if (dir && *dir) {
882e6893
WD
1152 static char *lastdir;
1153 static int lastdir_len;
1154
808c57c3 1155 strcpy(olddir, curr_dir); /* can't overflow */
5243c216 1156
808c57c3 1157 if (!push_dir(dir)) {
06c28400 1158 io_error |= IOERR_GENERAL;
d62bcc17
WD
1159 rsyserr(FERROR, errno, "push_dir %s failed",
1160 full_fname(dir));
649d65ed
AT
1161 continue;
1162 }
5243c216 1163
882e6893
WD
1164 if (lastdir && strcmp(lastdir, dir) == 0) {
1165 flist_dir = lastdir;
1166 flist_dir_len = lastdir_len;
1167 } else {
882e6893
WD
1168 flist_dir = lastdir = strdup(dir);
1169 flist_dir_len = lastdir_len = strlen(dir);
1170 }
2bca43f6 1171 }
ebed4c3a 1172
2bca43f6
DD
1173 if (one_file_system)
1174 set_filesystem(fname);
1175
d01d15e0 1176 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
2bca43f6 1177
808c57c3 1178 if (olddir[0]) {
649d65ed 1179 flist_dir = NULL;
882e6893 1180 flist_dir_len = 0;
808c57c3 1181 if (!pop_dir(olddir)) {
d62bcc17
WD
1182 rsyserr(FERROR, errno, "pop_dir %s failed",
1183 full_fname(dir));
65417579 1184 exit_cleanup(RERR_FILESELECT);
649d65ed 1185 }
649d65ed 1186 }
649d65ed 1187 }
dc5ddbcc 1188
983b1ed3 1189 if (f != -1) {
ebed4c3a 1190 send_file_entry(NULL, f, 0);
c627d613 1191
983b1ed3
WD
1192 if (show_filelist_p())
1193 finish_filelist_progress(flist);
1194 }
ebed4c3a 1195
7cf8e8d0 1196 if (flist->hlink_pool) {
9935066b
S
1197 pool_destroy(flist->hlink_pool);
1198 flist->hlink_pool = NULL;
1199 }
1200
827c37f6 1201 clean_flist(flist, 0, 0);
ebed4c3a 1202
785db4ce
WD
1203 if (f != -1) {
1204 /* Now send the uid/gid list. This was introduced in
1205 * protocol version 15 */
649d65ed 1206 send_uid_list(f);
f6c34742 1207
983b1ed3 1208 /* send the io_error flag */
ebed4c3a 1209 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1210
a261989c 1211 io_end_buffering();
a800434a
AT
1212 stats.flist_size = stats.total_written - start_write;
1213 stats.num_files = flist->count;
64c3523a 1214 if (write_batch)
ebed4c3a 1215 write_batch_flist_info(flist->count, flist->files);
d6dead6b
AT
1216 }
1217
cefed3e8
WD
1218 if (verbose > 3)
1219 output_flist(flist);
1220
17faa41c 1221 if (verbose > 2)
ebed4c3a 1222 rprintf(FINFO, "send_file_list done\n");
17faa41c 1223
649d65ed 1224 return flist;
c627d613
AT
1225}
1226
1227
1228struct file_list *recv_file_list(int f)
1229{
ebed4c3a 1230 struct file_list *flist;
1ef00d20 1231 unsigned short flags;
ebed4c3a
MP
1232 int64 start_read;
1233 extern int list_only;
c627d613 1234
1bbd10fe
DD
1235 if (show_filelist_p())
1236 start_filelist_progress("receiving file list");
c627d613 1237
ebed4c3a 1238 start_read = stats.total_read;
a800434a 1239
9935066b 1240 flist = flist_new(WITH_HLINK, "recv_file_list");
c627d613 1241
ebed4c3a
MP
1242 flist->count = 0;
1243 flist->malloced = 1000;
58cadc86 1244 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1245 if (!flist->files)
1246 goto oom;
c627d613
AT
1247
1248
1ef00d20 1249 while ((flags = read_byte(f)) != 0) {
5d2c5c4c 1250 int i = flist->count;
dbda5fbf 1251
d9d6bc52 1252 flist_expand(flist);
c627d613 1253
d01d15e0 1254 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1255 flags |= read_byte(f) << 8;
9935066b 1256 receive_file_entry(&flist->files[i], flags, flist, f);
c627d613 1257
ebed4c3a
MP
1258 if (S_ISREG(flist->files[i]->mode))
1259 stats.total_size += flist->files[i]->length;
c627d613 1260
ebed4c3a 1261 flist->count++;
c627d613 1262
db719fb0 1263 maybe_emit_filelist_progress(flist);
1bbd10fe 1264
8018edd3 1265 if (verbose > 2) {
ebed4c3a
MP
1266 rprintf(FINFO, "recv_file_name(%s)\n",
1267 f_name(flist->files[i]));
8018edd3 1268 }
ebed4c3a 1269 }
9935066b 1270 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
c627d613 1271
ebed4c3a
MP
1272 if (verbose > 2)
1273 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1274
b7736c79 1275 if (show_filelist_p())
1bbd10fe 1276 finish_filelist_progress(flist);
a06d19e3 1277
983b1ed3
WD
1278 clean_flist(flist, relative_paths, 1);
1279
785db4ce
WD
1280 if (f != -1) {
1281 /* Now send the uid/gid list. This was introduced in
1282 * protocol version 15 */
ebed4c3a 1283 recv_uid_list(f, flist);
f6c34742 1284
785db4ce
WD
1285 if (!read_batch) {
1286 /* Recv the io_error flag */
1287 if (lp_ignore_errors(module_id) || ignore_errors)
1288 read_int(f);
1289 else
1290 io_error |= read_int(f);
1291 }
ebed4c3a 1292 }
6ba9279f 1293
cefed3e8
WD
1294 if (verbose > 3)
1295 output_flist(flist);
1296
ebed4c3a
MP
1297 if (list_only) {
1298 int i;
b7736c79 1299 for (i = 0; i < flist->count; i++)
ebed4c3a 1300 list_file_entry(flist->files[i]);
ebed4c3a 1301 }
f7632fc6 1302
ebed4c3a
MP
1303 if (verbose > 2)
1304 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1305
ebed4c3a
MP
1306 stats.flist_size = stats.total_read - start_read;
1307 stats.num_files = flist->count;
a800434a 1308
ebed4c3a 1309 return flist;
c627d613 1310
3e491682 1311oom:
ebed4c3a
MP
1312 out_of_memory("recv_file_list");
1313 return NULL; /* not reached */
c627d613
AT
1314}
1315
1316
fa45cda1 1317int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1318{
fa45cda1
S
1319 struct file_struct *f1 = *file1;
1320 struct file_struct *f2 = *file2;
1321
1322 if (!f1->basename && !f2->basename)
ebed4c3a 1323 return 0;
fa45cda1 1324 if (!f1->basename)
ebed4c3a 1325 return -1;
fa45cda1 1326 if (!f2->basename)
ebed4c3a 1327 return 1;
fa45cda1
S
1328 if (f1->dirname == f2->dirname)
1329 return u_strcmp(f1->basename, f2->basename);
1330 return f_name_cmp(f1, f2);
c627d613
AT
1331}
1332
1333
ebed4c3a 1334int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1335{
ebed4c3a 1336 int low = 0, high = flist->count - 1;
d966ee25 1337
ca23c51a
WD
1338 while (high >= 0 && !flist->files[high]->basename) high--;
1339
1340 if (high < 0)
ebed4c3a 1341 return -1;
d966ee25
AT
1342
1343 while (low != high) {
ebed4c3a 1344 int mid = (low + high) / 2;
b7736c79 1345 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
ebed4c3a
MP
1346 if (ret == 0)
1347 return flist_up(flist, mid);
b7736c79 1348 if (ret > 0)
ebed4c3a 1349 high = mid;
b7736c79 1350 else
ebed4c3a 1351 low = mid + 1;
d966ee25
AT
1352 }
1353
ebed4c3a
MP
1354 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1355 return flist_up(flist, low);
d966ee25 1356 return -1;
c627d613
AT
1357}
1358
3ec4dd97 1359/*
9935066b
S
1360 * Free up any resources a file_struct has allocated
1361 * and clear the file.
3ec4dd97 1362 */
9935066b 1363void clear_file(int i, struct file_list *flist)
c627d613 1364{
9935066b
S
1365 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1366 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1367 memset(flist->files[i], 0, file_struct_len);
3ec4dd97 1368}
c627d613 1369
c627d613 1370
3d382777
AT
1371/*
1372 * allocate a new file list
1373 */
9935066b 1374struct file_list *flist_new(int with_hlink, char *msg)
3d382777
AT
1375{
1376 struct file_list *flist;
1377
58cadc86 1378 flist = new(struct file_list);
ebed4c3a 1379 if (!flist)
9935066b 1380 out_of_memory(msg);
3d382777 1381
9935066b
S
1382 memset(flist, 0, sizeof (struct file_list));
1383
1384 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1385 out_of_memory, POOL_INTERN)))
1386 out_of_memory(msg);
1387
1388#if SUPPORT_HARD_LINKS
1389 if (with_hlink && preserve_hard_links) {
3e491682 1390 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
9935066b
S
1391 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1392 out_of_memory(msg);
1393 }
1394#endif
d9d6bc52 1395
3d382777
AT
1396 return flist;
1397}
ebed4c3a 1398
3ec4dd97
AT
1399/*
1400 * free up all elements in a flist
1401 */
1402void flist_free(struct file_list *flist)
1403{
9935066b
S
1404 pool_destroy(flist->file_pool);
1405 pool_destroy(flist->hlink_pool);
3ec4dd97 1406 free(flist->files);
3ec4dd97 1407 free(flist);
c627d613
AT
1408}
1409
1410
1411/*
1412 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1413 * duplicate names can cause corruption because of the pipelining
c627d613 1414 */
827c37f6 1415static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1416{
6931c138 1417 int i, prev_i = 0;
c627d613 1418
ebed4c3a 1419 if (!flist || flist->count == 0)
3ec4dd97 1420 return;
3ec4dd97 1421
ebed4c3a 1422 qsort(flist->files, flist->count,
3e491682 1423 sizeof flist->files[0], (int (*)()) file_compare);
ebed4c3a 1424
827c37f6 1425 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1426 if (flist->files[i]->basename) {
6931c138 1427 prev_i = i;
b91b50c0
WD
1428 break;
1429 }
1430 }
1431 while (++i < flist->count) {
1432 if (!flist->files[i]->basename)
1433 continue;
8018edd3 1434 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
b91b50c0 1435 if (verbose > 1 && !am_server) {
ebed4c3a
MP
1436 rprintf(FINFO,
1437 "removing duplicate name %s from file list %d\n",
8018edd3 1438 f_name(flist->files[i]), i);
b91b50c0 1439 }
6931c138
WD
1440 /* Make sure that if we unduplicate '.', that we don't
1441 * lose track of a user-specified starting point (or
1442 * else deletions will mysteriously fail with -R). */
d01d15e0
WD
1443 if (flist->files[i]->flags & FLAG_TOP_DIR)
1444 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
9935066b
S
1445
1446 clear_file(i, flist);
728d0922 1447 } else
6931c138 1448 prev_i = i;
3ec4dd97 1449 }
0199b05f
AT
1450
1451 if (strip_root) {
1452 /* we need to strip off the root directory in the case
1453 of relative paths, but this must be done _after_
1454 the sorting phase */
ebed4c3a 1455 for (i = 0; i < flist->count; i++) {
0199b05f
AT
1456 if (flist->files[i]->dirname &&
1457 flist->files[i]->dirname[0] == '/') {
1458 memmove(&flist->files[i]->dirname[0],
1459 &flist->files[i]->dirname[1],
1460 strlen(flist->files[i]->dirname));
1461 }
ebed4c3a
MP
1462
1463 if (flist->files[i]->dirname &&
0199b05f
AT
1464 !flist->files[i]->dirname[0]) {
1465 flist->files[i]->dirname = NULL;
1466 }
1467 }
1468 }
cefed3e8 1469}
0199b05f 1470
cefed3e8
WD
1471static void output_flist(struct file_list *flist)
1472{
1473 char uidbuf[16], gidbuf[16];
1474 struct file_struct *file;
1475 int i;
0199b05f 1476
ebed4c3a 1477 for (i = 0; i < flist->count; i++) {
cefed3e8 1478 file = flist->files[i];
f05f993e
WD
1479 if (am_root && preserve_uid)
1480 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1481 else
1482 *uidbuf = '\0';
cefed3e8 1483 if (preserve_gid && file->gid != GID_NONE)
f05f993e
WD
1484 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1485 else
1486 *gidbuf = '\0';
1487 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1488 who_am_i(), i, NS(file->basedir), NS(file->dirname),
1489 NS(file->basename), (int) file->mode,
1490 (double) file->length, uidbuf, gidbuf);
0199b05f 1491 }
3ec4dd97
AT
1492}
1493
1494
8018edd3
WD
1495enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1496
1497/* Compare the names of two file_struct entities, just like strcmp()
1498 * would do if it were operating on the joined strings. We assume
1499 * that there are no 0-length strings.
3ec4dd97 1500 */
8018edd3 1501int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1502{
8018edd3
WD
1503 int dif;
1504 const uchar *c1, *c2;
1ef00d20 1505 enum fnc_state state1, state2;
8018edd3
WD
1506
1507 if (!f1 || !f1->basename) {
1508 if (!f2 || !f2->basename)
1509 return 0;
1510 return -1;
1511 }
1512 if (!f2 || !f2->basename)
1513 return 1;
1514
e90b8ace 1515 if (!(c1 = (uchar*)f1->dirname)) {
8018edd3 1516 state1 = fnc_BASE;
e90b8ace 1517 c1 = (uchar*)f1->basename;
080ddf58
WD
1518 } else if (!*c1) {
1519 state1 = fnc_SLASH;
1520 c1 = (uchar*)"/";
728d0922 1521 } else
1ef00d20 1522 state1 = fnc_DIR;
e90b8ace 1523 if (!(c2 = (uchar*)f2->dirname)) {
8018edd3 1524 state2 = fnc_BASE;
e90b8ace 1525 c2 = (uchar*)f2->basename;
080ddf58
WD
1526 } else if (!*c2) {
1527 state2 = fnc_SLASH;
1528 c2 = (uchar*)"/";
728d0922 1529 } else
1ef00d20 1530 state2 = fnc_DIR;
8018edd3
WD
1531
1532 while (1) {
1533 if ((dif = (int)*c1 - (int)*c2) != 0)
1534 break;
1535 if (!*++c1) {
1536 switch (state1) {
1537 case fnc_DIR:
1538 state1 = fnc_SLASH;
e90b8ace 1539 c1 = (uchar*)"/";
8018edd3
WD
1540 break;
1541 case fnc_SLASH:
1542 state1 = fnc_BASE;
e90b8ace 1543 c1 = (uchar*)f1->basename;
8018edd3
WD
1544 break;
1545 case fnc_BASE:
1546 break;
1547 }
1548 }
1549 if (!*++c2) {
1550 switch (state2) {
1551 case fnc_DIR:
1552 state2 = fnc_SLASH;
e90b8ace 1553 c2 = (uchar*)"/";
8018edd3
WD
1554 break;
1555 case fnc_SLASH:
1556 state2 = fnc_BASE;
e90b8ace 1557 c2 = (uchar*)f2->basename;
8018edd3
WD
1558 break;
1559 case fnc_BASE:
1560 if (!*c1)
1561 return 0;
1562 break;
1563 }
1564 }
1565 }
1566
1567 return dif;
1568}
1569
3ec4dd97 1570
8018edd3 1571/* Return a copy of the full filename of a flist entry, using the indicated
882e6893
WD
1572 * buffer. No size-checking is done because we checked the size when creating
1573 * the file_struct entry.
8018edd3 1574 */
882e6893 1575char *f_name_to(struct file_struct *f, char *fbuf)
8018edd3 1576{
ebed4c3a
MP
1577 if (!f || !f->basename)
1578 return NULL;
3ec4dd97 1579
3ec4dd97 1580 if (f->dirname) {
882e6893
WD
1581 int len = strlen(f->dirname);
1582 memcpy(fbuf, f->dirname, len);
1583 fbuf[len] = '/';
1584 strcpy(fbuf + len + 1, f->basename);
8018edd3 1585 } else
882e6893 1586 strcpy(fbuf, f->basename);
b7736c79 1587 return fbuf;
8018edd3 1588}
e03dfae5 1589
3ec4dd97 1590
8018edd3
WD
1591/* Like f_name_to(), but we rotate through 5 static buffers of our own.
1592 */
1593char *f_name(struct file_struct *f)
1594{
1595 static char names[5][MAXPATHLEN];
1596 static unsigned int n;
1597
1598 n = (n + 1) % (sizeof names / sizeof names[0]);
1599
882e6893 1600 return f_name_to(f, names[n]);
c627d613 1601}