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