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