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