Mention new hard-link optimization.
[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;
34fada30 296
ebed4c3a 297 filesystem_dev = st.st_dev;
c627d613
AT
298}
299
300
b280a1f4
AT
301static int to_wire_mode(mode_t mode)
302{
b7736c79 303 if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
b280a1f4 304 return (mode & ~(_S_IFMT)) | 0120000;
ebed4c3a 305 return (int) mode;
b280a1f4
AT
306}
307
308static mode_t from_wire_mode(int mode)
309{
b7736c79 310 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
efe3037c 311 return (mode & ~(_S_IFMT)) | _S_IFLNK;
ebed4c3a 312 return (mode_t) mode;
b280a1f4
AT
313}
314
315
ebed4c3a 316static void send_directory(int f, struct file_list *flist, char *dir);
c627d613 317
3a6a366f 318static char *flist_dir;
882e6893 319static int flist_dir_len;
c627d613 320
3ec4dd97 321
d9d6bc52
MP
322/**
323 * Make sure @p flist is big enough to hold at least @p flist->count
324 * entries.
325 **/
326static void flist_expand(struct file_list *flist)
327{
328 if (flist->count >= flist->malloced) {
2e7d1994 329 void *new_ptr;
dbda5fbf 330
d9d6bc52
MP
331 if (flist->malloced < 1000)
332 flist->malloced += 1000;
333 else
334 flist->malloced *= 2;
2e7d1994 335
58cadc86
WD
336 if (flist->files) {
337 new_ptr = realloc_array(flist->files,
338 struct file_struct *,
339 flist->malloced);
340 } else {
341 new_ptr = new_array(struct file_struct *,
342 flist->malloced);
343 }
2e7d1994 344
1d5a1da9 345 if (verbose >= 2) {
1bbd10fe 346 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
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;
364 static DEV64_T rdev; /* just high bytes in p28 onward */
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
WD
377 modtime = 0, mode = 0;
378 rdev = 0, dev = 0;
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)
ebed4c3a 391 flags |= 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) {
398 /* Set both flags so that the test when
399 * writing the data is simpler. */
400 flags |= SAME_RDEV_pre28|SAME_HIGH_RDEV;
401 } else
402 rdev = file->u.rdev;
403 } else
404 rdev = 0;
405 } else if (IS_DEVICE(mode)) {
406 if ((file->u.rdev & ~0xFF) == rdev)
75bc8600 407 flags |= SAME_HIGH_RDEV;
30f337c9 408 else
728d0922 409 rdev = file->u.rdev & ~0xFF;
75bc8600
WD
410 }
411 }
30f337c9 412 if (file->uid == uid)
ebed4c3a 413 flags |= SAME_UID;
1ef00d20 414 else
30f337c9
WD
415 uid = file->uid;
416 if (file->gid == gid)
ebed4c3a 417 flags |= SAME_GID;
1ef00d20 418 else
30f337c9
WD
419 gid = file->gid;
420 if (file->modtime == modtime)
ebed4c3a 421 flags |= SAME_TIME;
1ef00d20 422 else
30f337c9 423 modtime = file->modtime;
92cc9dd7
WD
424 if (file->link_u.idev) {
425 if (file->F_DEV == dev) {
c4b4df4f
WD
426 if (protocol_version >= 28)
427 flags |= SAME_DEV;
728d0922 428 } else
92cc9dd7 429 dev = file->F_DEV;
c4b4df4f
WD
430 flags |= HAS_INODE_DATA;
431 }
ebed4c3a
MP
432
433 for (l1 = 0;
434 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
dbda5fbf 435 l1++) {}
eddd5d12 436 l2 = strlen(fname+l1);
72914a60 437
ebed4c3a
MP
438 if (l1 > 0)
439 flags |= SAME_NAME;
440 if (l2 > 255)
441 flags |= LONG_NAME;
72914a60 442
75bc8600
WD
443 /* We must make sure we don't send a zero flags byte or
444 * the other end will terminate the flist transfer. */
30f337c9 445 if (flags == 0 && !S_ISDIR(mode))
75bc8600
WD
446 flags |= FLAG_DELETE; /* NOTE: no meaning for non-dir */
447 if (protocol_version >= 28) {
448 if ((flags & 0xFF00) || flags == 0) {
449 flags |= EXTENDED_FLAGS;
450 write_byte(f, flags);
451 write_byte(f, flags >> 8);
452 } else
453 write_byte(f, flags);
454 } else {
455 if (flags == 0)
456 flags |= LONG_NAME;
457 write_byte(f, flags);
458 }
72914a60 459 if (flags & SAME_NAME)
ebed4c3a 460 write_byte(f, l1);
72914a60 461 if (flags & LONG_NAME)
ebed4c3a 462 write_int(f, l2);
72914a60 463 else
ebed4c3a
MP
464 write_byte(f, l2);
465 write_buf(f, fname + l1, l2);
72914a60 466
ebed4c3a 467 write_longint(f, file->length);
72914a60 468 if (!(flags & SAME_TIME))
30f337c9 469 write_int(f, modtime);
72914a60 470 if (!(flags & SAME_MODE))
30f337c9 471 write_int(f, to_wire_mode(mode));
72914a60 472 if (preserve_uid && !(flags & SAME_UID)) {
30f337c9
WD
473 add_uid(uid);
474 write_int(f, uid);
72914a60
AT
475 }
476 if (preserve_gid && !(flags & SAME_GID)) {
30f337c9
WD
477 add_gid(gid);
478 write_int(f, gid);
72914a60 479 }
30f337c9 480 if (preserve_devices && IS_DEVICE(mode)) {
0c429465
WD
481 /* If SAME_HIGH_RDEV is off, SAME_RDEV_pre28 is also off.
482 * Also, avoid using "rdev" because it may be incomplete. */
75bc8600 483 if (!(flags & SAME_HIGH_RDEV))
728d0922 484 write_int(f, file->u.rdev);
75bc8600 485 else if (protocol_version >= 28)
728d0922 486 write_byte(f, file->u.rdev);
75bc8600 487 }
c627d613
AT
488
489#if SUPPORT_LINKS
30f337c9 490 if (preserve_links && S_ISLNK(mode)) {
728d0922
WD
491 write_int(f, strlen(file->u.link));
492 write_buf(f, file->u.link, strlen(file->u.link));
72914a60 493 }
c627d613
AT
494#endif
495
dc5ddbcc 496#if SUPPORT_HARD_LINKS
c4b4df4f 497 if (flags & HAS_INODE_DATA) {
d04e9c51 498 if (protocol_version < 26) {
736a6a29 499 /* 32-bit dev_t and ino_t */
30f337c9 500 write_int(f, dev);
92cc9dd7 501 write_int(f, file->F_INODE);
736a6a29
MP
502 } else {
503 /* 64-bit dev_t and ino_t */
c4b4df4f 504 if (!(flags & SAME_DEV))
30f337c9 505 write_longint(f, dev);
92cc9dd7 506 write_longint(f, file->F_INODE);
736a6a29 507 }
72914a60 508 }
dc5ddbcc
AT
509#endif
510
728d0922
WD
511 if (always_checksum) {
512 char *sum;
513 if (S_ISREG(mode))
514 sum = file->u.sum;
515 else if (protocol_version < 28) {
516 /* Prior to 28, we sent a useless set of nulls. */
517 sum = empty_sum;
518 } else
519 sum = NULL;
520 if (sum) {
521 write_buf(f, sum, protocol_version < 21? 2
522 : MD4_SUM_LENGTH);
523 }
ebed4c3a 524 }
182dca5c 525
ebed4c3a 526 strlcpy(lastname, fname, MAXPATHLEN);
eca2adb4
MP
527
528 io_write_phase = "unknown";
182dca5c
AT
529}
530
531
532
7b1a0c19 533void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
182dca5c 534{
5911fee5
WD
535 static time_t modtime;
536 static mode_t mode;
537 static DEV64_T rdev; /* just high bytes in p28 onward */
538 static DEV64_T dev;
539 static uid_t uid;
540 static gid_t gid;
541 static char lastname[MAXPATHLEN];
72914a60 542 char thisname[MAXPATHLEN];
ebed4c3a 543 unsigned int l1 = 0, l2 = 0;
72914a60
AT
544 char *p;
545 struct file_struct *file;
546
5911fee5
WD
547 if (!fptr) {
548 modtime = 0, mode = 0;
549 rdev = 0, dev = 0;
550 uid = 0, gid = 0;
551 *lastname = '\0';
552 return;
553 }
554
72914a60
AT
555 if (flags & SAME_NAME)
556 l1 = read_byte(f);
ebed4c3a 557
72914a60
AT
558 if (flags & LONG_NAME)
559 l2 = read_int(f);
560 else
561 l2 = read_byte(f);
562
58cadc86 563 file = new(struct file_struct);
ebed4c3a
MP
564 if (!file)
565 out_of_memory("receive_file_entry");
566 memset((char *) file, 0, sizeof(*file));
72914a60
AT
567 (*fptr) = file;
568
ebed4c3a
MP
569 if (l2 >= MAXPATHLEN - l1) {
570 rprintf(FERROR,
571 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
d0fd26aa
AT
572 flags, l1, l2, lastname);
573 overflow("receive_file_entry");
574 }
72914a60 575
ebed4c3a
MP
576 strlcpy(thisname, lastname, l1 + 1);
577 read_sbuf(f, &thisname[l1], l2);
578 thisname[l1 + l2] = 0;
72914a60 579
ebed4c3a
MP
580 strlcpy(lastname, thisname, MAXPATHLEN);
581 lastname[MAXPATHLEN - 1] = 0;
72914a60
AT
582
583 clean_fname(thisname);
584
cb13abfe
DD
585 if (sanitize_paths) {
586 sanitize_path(thisname, NULL);
587 }
588
ebed4c3a 589 if ((p = strrchr(thisname, '/'))) {
72914a60
AT
590 static char *lastdir;
591 *p = 0;
b7736c79 592 if (lastdir && strcmp(thisname, lastdir) == 0)
72914a60 593 file->dirname = lastdir;
b7736c79 594 else {
72914a60
AT
595 file->dirname = strdup(thisname);
596 lastdir = file->dirname;
597 }
ebed4c3a 598 file->basename = strdup(p + 1);
72914a60
AT
599 } else {
600 file->dirname = NULL;
601 file->basename = strdup(thisname);
602 }
603
ebed4c3a
MP
604 if (!file->basename)
605 out_of_memory("receive_file_entry 1");
72914a60 606
f3219222 607 file->flags = flags & LIVE_FLAGS;
72914a60 608 file->length = read_longint(f);
1ef00d20 609 if (!(flags & SAME_TIME))
30f337c9
WD
610 modtime = (time_t)read_int(f);
611 file->modtime = modtime;
1ef00d20 612 if (!(flags & SAME_MODE))
30f337c9
WD
613 mode = from_wire_mode(read_int(f));
614 file->mode = mode;
1ef00d20
WD
615
616 if (preserve_uid) {
617 if (!(flags & SAME_UID))
30f337c9
WD
618 uid = (uid_t)read_int(f);
619 file->uid = uid;
1ef00d20
WD
620 }
621 if (preserve_gid) {
622 if (!(flags & SAME_GID))
30f337c9
WD
623 gid = (gid_t)read_int(f);
624 file->gid = gid;
1ef00d20
WD
625 }
626 if (preserve_devices) {
75bc8600 627 if (protocol_version < 28) {
30f337c9
WD
628 if (IS_DEVICE(mode)) {
629 if (!(flags & SAME_RDEV_pre28))
630 rdev = (DEV64_T)read_int(f);
728d0922 631 file->u.rdev = rdev;
75bc8600 632 } else
30f337c9
WD
633 rdev = 0;
634 } else if (IS_DEVICE(mode)) {
635 if (!(flags & SAME_HIGH_RDEV)) {
728d0922
WD
636 file->u.rdev = (DEV64_T)read_int(f);
637 rdev = file->u.rdev & ~0xFF;
30f337c9 638 } else
728d0922 639 file->u.rdev = rdev | (DEV64_T)read_byte(f);
1ef00d20 640 }
b7736c79 641 }
72914a60 642
30f337c9 643 if (preserve_links && S_ISLNK(mode)) {
72914a60 644 int l = read_int(f);
9dd891bb 645 if (l < 0) {
ebed4c3a 646 rprintf(FERROR, "overflow: l=%d\n", l);
9dd891bb
MP
647 overflow("receive_file_entry");
648 }
728d0922 649 if (!(file->u.link = new_array(char, l + 1)))
ebed4c3a 650 out_of_memory("receive_file_entry 2");
728d0922 651 read_sbuf(f, file->u.link, l);
b7736c79 652 if (sanitize_paths)
728d0922 653 sanitize_path(file->u.link, file->dirname);
72914a60 654 }
dc5ddbcc 655#if SUPPORT_HARD_LINKS
92cc9dd7
WD
656 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
657 flags |= HAS_INODE_DATA;
658 if (flags & HAS_INODE_DATA) {
659 if (!(file->link_u.idev = new(struct idev)))
660 out_of_memory("file inode data");
d04e9c51 661 if (protocol_version < 26) {
30f337c9 662 dev = read_int(f);
92cc9dd7 663 file->F_INODE = read_int(f);
736a6a29 664 } else {
c4b4df4f 665 if (!(flags & SAME_DEV))
30f337c9 666 dev = read_longint(f);
92cc9dd7 667 file->F_INODE = read_longint(f);
736a6a29 668 }
92cc9dd7 669 file->F_DEV = dev;
72914a60 670 }
dc5ddbcc 671#endif
ebed4c3a 672
72914a60 673 if (always_checksum) {
7c4f063b 674 char *sum;
fea4db62 675 if (S_ISREG(mode)) {
728d0922 676 sum = file->u.sum = new_array(char, MD4_SUM_LENGTH);
fea4db62
WD
677 if (!sum)
678 out_of_memory("md4 sum");
679 } else if (protocol_version < 28) {
680 /* Prior to 28, we get a useless set of nulls. */
7c4f063b 681 sum = empty_sum;
fea4db62
WD
682 } else
683 sum = NULL;
684 if (sum) {
685 read_buf(f, sum, protocol_version < 21? 2
686 : MD4_SUM_LENGTH);
687 }
72914a60 688 }
ebed4c3a 689
72914a60
AT
690 if (!preserve_perms) {
691 extern int orig_umask;
692 /* set an appropriate set of permissions based on original
fea4db62 693 * permissions and umask. This emulates what GNU cp does */
72914a60
AT
694 file->mode &= ~orig_umask;
695 }
c627d613
AT
696}
697
698
34fada30 699/* determine if a file in a different filesystem should be skipped
0c5f37d9
AT
700 when one_file_system is set. We bascally only want to include
701 the mount points - but they can be hard to find! */
ebed4c3a 702static int skip_filesystem(char *fname, STRUCT_STAT * st)
0c5f37d9 703{
bcacc18b 704 STRUCT_STAT st2;
0c5f37d9
AT
705
706 /* skip all but directories */
ebed4c3a
MP
707 if (!S_ISDIR(st->st_mode))
708 return 1;
0c5f37d9
AT
709
710 /* if its not a subdirectory then allow */
34fada30 711 if (!strrchr(fname, '/'))
ebed4c3a 712 return 0;
0c5f37d9 713
34fada30 714 if (link_stat(fname, &st2) != 0) {
0c5f37d9
AT
715 return 0;
716 }
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];
3ec4dd97 749
882e6893
WD
750 if (strlcpy(cleaned_name, fname, sizeof cleaned_name)
751 >= sizeof cleaned_name - flist_dir_len) {
752 rprintf(FINFO, "skipping overly long name: %s\n", fname);
753 return NULL;
754 }
3ec4dd97 755 clean_fname(cleaned_name);
b7736c79 756 if (sanitize_paths)
cb13abfe 757 sanitize_path(cleaned_name, NULL);
3ec4dd97
AT
758 fname = cleaned_name;
759
ebed4c3a 760 memset(sum, 0, SUM_LENGTH);
3ec4dd97 761
ebed4c3a 762 if (readlink_stat(fname, &st, linkbuf) != 0) {
76e26e10 763 int save_errno = errno;
429f9828 764 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
dbda5fbf 765 /* either symlink pointing nowhere or file that
1b85e3f1
DD
766 * was removed during rsync run; see if excluded
767 * before reporting an error */
429f9828 768 if (check_exclude_file(fname, 0, exclude_level)) {
76e26e10
DD
769 /* file is excluded anyway, ignore silently */
770 return NULL;
771 }
772 }
06c28400 773 io_error |= IOERR_GENERAL;
ea42541f
WD
774 rprintf(FERROR, "readlink %s failed: %s\n",
775 full_fname(fname), strerror(save_errno));
3ec4dd97
AT
776 return NULL;
777 }
c627d613 778
429f9828
WD
779 /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
780 if (exclude_level == NO_EXCLUDES)
ebed4c3a 781 goto skip_excludes;
ac1a0994 782
24d0fcde 783 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
ebed4c3a 784 rprintf(FINFO, "skipping directory %s\n", fname);
3ec4dd97
AT
785 return NULL;
786 }
ebed4c3a 787
0c5f37d9
AT
788 if (one_file_system && st.st_dev != filesystem_dev) {
789 if (skip_filesystem(fname, &st))
790 return NULL;
791 }
ebed4c3a 792
429f9828 793 if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level))
76e26e10
DD
794 return NULL;
795
ebed4c3a 796 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
ac1a0994
AT
797 return NULL;
798
ebed4c3a 799 skip_excludes:
ac1a0994 800
3ec4dd97 801 if (verbose > 2)
429f9828 802 rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level);
ebed4c3a 803
58cadc86 804 file = new(struct file_struct);
ebed4c3a
MP
805 if (!file)
806 out_of_memory("make_file");
807 memset((char *) file, 0, sizeof(*file));
3ec4dd97 808
ebed4c3a 809 if ((p = strrchr(fname, '/'))) {
3ec4dd97
AT
810 static char *lastdir;
811 *p = 0;
b7736c79 812 if (lastdir && strcmp(fname, lastdir) == 0)
3ec4dd97 813 file->dirname = lastdir;
b7736c79 814 else {
a20aa42a 815 file->dirname = strdup(fname);
3ec4dd97
AT
816 lastdir = file->dirname;
817 }
ebed4c3a 818 file->basename = STRDUP(ap, p + 1);
3ec4dd97
AT
819 *p = '/';
820 } else {
821 file->dirname = NULL;
3d382777 822 file->basename = STRDUP(ap, fname);
3ec4dd97 823 }
c627d613 824
3ec4dd97
AT
825 file->modtime = st.st_mtime;
826 file->length = st.st_size;
827 file->mode = st.st_mode;
828 file->uid = st.st_uid;
829 file->gid = st.st_gid;
c4b4df4f 830 if (preserve_hard_links) {
92cc9dd7 831 if (protocol_version < 28 ? S_ISREG(st.st_mode)
c4b4df4f 832 : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
92cc9dd7
WD
833 if (!(file->link_u.idev = new(struct idev)))
834 out_of_memory("file inode data");
835 file->F_DEV = st.st_dev;
836 file->F_INODE = st.st_ino;
c4b4df4f
WD
837 }
838 }
5f78da20 839#ifdef HAVE_STRUCT_STAT_ST_RDEV
728d0922
WD
840 if (IS_DEVICE(st.st_mode))
841 file->u.rdev = st.st_rdev;
c627d613
AT
842#endif
843
844#if SUPPORT_LINKS
b7736c79 845 if (S_ISLNK(st.st_mode))
728d0922 846 file->u.link = STRDUP(ap, linkbuf);
c627d613
AT
847#endif
848
fea4db62 849 if (always_checksum && S_ISREG(st.st_mode)) {
728d0922 850 if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH)))
ebed4c3a 851 out_of_memory("md4 sum");
728d0922 852 file_checksum(fname, file->u.sum, st.st_size);
ebed4c3a 853 }
c627d613 854
882e6893 855 file->basedir = flist_dir;
c627d613 856
3ec4dd97 857 if (!S_ISDIR(st.st_mode))
a800434a 858 stats.total_size += st.st_size;
c627d613 859
3ec4dd97 860 return file;
c627d613
AT
861}
862
863
ebed4c3a 864void send_file_name(int f, struct file_list *flist, char *fname,
1ef00d20 865 int recursive, unsigned short base_flags)
c627d613 866{
ebed4c3a 867 struct file_struct *file;
b7736c79 868 char fbuf[MAXPATHLEN];
429f9828 869 extern int delete_excluded;
ebed4c3a 870
429f9828
WD
871 /* f is set to -1 when calculating deletion file list */
872 file = make_file(fname, &flist->string_area,
873 f == -1 && delete_excluded? SERVER_EXCLUDES
874 : ALL_EXCLUDES);
ebed4c3a
MP
875
876 if (!file)
877 return;
878
db719fb0 879 maybe_emit_filelist_progress(flist);
ebed4c3a 880
d9d6bc52 881 flist_expand(flist);
ebed4c3a 882
64c3523a 883 if (write_batch)
1ef00d20 884 file->flags |= FLAG_DELETE;
ebed4c3a 885
c120ff37 886 if (file->basename[0]) {
ebed4c3a
MP
887 flist->files[flist->count++] = file;
888 send_file_entry(file, f, base_flags);
889 }
890
891 if (S_ISDIR(file->mode) && recursive) {
892 struct exclude_struct **last_exclude_list =
893 local_exclude_list;
882e6893 894 send_directory(f, flist, f_name_to(file, fbuf));
ebed4c3a
MP
895 local_exclude_list = last_exclude_list;
896 return;
897 }
c627d613
AT
898}
899
900
ebed4c3a 901static void send_directory(int f, struct file_list *flist, char *dir)
c627d613 902{
3ec4dd97
AT
903 DIR *d;
904 struct dirent *di;
905 char fname[MAXPATHLEN];
eddd5d12 906 unsigned int offset;
3ec4dd97
AT
907 char *p;
908
909 d = opendir(dir);
910 if (!d) {
06c28400 911 io_error |= IOERR_GENERAL;
ea42541f
WD
912 rprintf(FERROR, "opendir %s failed: %s\n",
913 full_fname(dir), strerror(errno));
3ec4dd97
AT
914 return;
915 }
c627d613 916
eddd5d12
WD
917 offset = strlcpy(fname, dir, MAXPATHLEN);
918 p = fname + offset;
919 if (offset >= MAXPATHLEN || p[-1] != '/') {
920 if (offset >= MAXPATHLEN - 1) {
06c28400 921 io_error |= IOERR_GENERAL;
ea42541f
WD
922 rprintf(FERROR, "skipping long-named directory: %s\n",
923 full_fname(fname));
3ec4dd97
AT
924 closedir(d);
925 return;
926 }
eddd5d12
WD
927 *p++ = '/';
928 offset++;
3ec4dd97 929 }
c627d613 930
3d913675
AT
931 local_exclude_list = NULL;
932
3ec4dd97 933 if (cvs_exclude) {
eddd5d12
WD
934 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
935 < MAXPATHLEN - offset)
57469f6c 936 add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
eddd5d12 937 else {
06c28400 938 io_error |= IOERR_GENERAL;
ebed4c3a
MP
939 rprintf(FINFO,
940 "cannot cvs-exclude in long-named directory %s\n",
ea42541f 941 full_fname(fname));
3ec4dd97 942 }
ebed4c3a
MP
943 }
944
6a7cc46c 945 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 946 char *dname = d_name(di);
6a7cc46c
S
947 if (dname[0] == '.' && (dname[1] == '\0'
948 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 949 continue;
eddd5d12
WD
950 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
951 send_file_name(f, flist, fname, recurse, 0);
952 else {
953 io_error |= IOERR_GENERAL;
954 rprintf(FINFO,
955 "cannot send long-named file %s\n",
956 full_fname(fname));
957 }
3ec4dd97 958 }
6a7cc46c 959 if (errno) {
06c28400 960 io_error |= IOERR_GENERAL;
6a7cc46c 961 rprintf(FERROR, "readdir(%s): (%d) %s\n",
eddd5d12 962 dir, errno, strerror(errno));
6a7cc46c 963 }
c627d613 964
429f9828
WD
965 if (local_exclude_list)
966 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
3d913675 967
3ec4dd97 968 closedir(d);
c627d613
AT
969}
970
971
c4fea82f 972/**
429f9828
WD
973 * The delete_files() function in receiver.c sets f to -1 so that we just
974 * construct the file list in memory without sending it over the wire. It
975 * also has the side-effect of ignoring user-excludes if delete_excluded
976 * is set (so that the delete list includes user-excluded files).
c4fea82f 977 **/
ebed4c3a 978struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 979{
24d0fcde 980 int l;
bcacc18b 981 STRUCT_STAT st;
808c57c3 982 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 983 char lastpath[MAXPATHLEN] = "";
649d65ed 984 struct file_list *flist;
a800434a 985 int64 start_write;
24d0fcde 986 int use_ff_fd = 0;
649d65ed 987
1bbd10fe
DD
988 if (show_filelist_p() && f != -1)
989 start_filelist_progress("building file list");
c627d613 990
a800434a
AT
991 start_write = stats.total_written;
992
3d382777 993 flist = flist_new();
c627d613 994
d6dead6b 995 if (f != -1) {
76c21947 996 io_start_buffering_out(f);
24d0fcde 997 if (filesfrom_fd >= 0) {
808c57c3 998 if (argv[0] && !push_dir(argv[0])) {
ea42541f
WD
999 rprintf(FERROR, "push_dir %s failed: %s\n",
1000 full_fname(argv[0]), strerror(errno));
24d0fcde
WD
1001 exit_cleanup(RERR_FILESELECT);
1002 }
1003 use_ff_fd = 1;
1004 }
d6dead6b
AT
1005 }
1006
24d0fcde 1007 while (1) {
fc638474
DD
1008 char fname2[MAXPATHLEN];
1009 char *fname = fname2;
c627d613 1010
24d0fcde
WD
1011 if (use_ff_fd) {
1012 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1013 break;
1014 sanitize_path(fname, NULL);
1015 } else {
1016 if (argc-- == 0)
1017 break;
1018 strlcpy(fname, *argv++, MAXPATHLEN);
1019 if (sanitize_paths)
1020 sanitize_path(fname, NULL);
1021 }
c627d613 1022
649d65ed 1023 l = strlen(fname);
6931c138
WD
1024 if (fname[l - 1] == '/') {
1025 if (l == 2 && fname[0] == '.') {
1026 /* Turn "./" into just "." rather than "./." */
1027 fname[1] = '\0';
eddd5d12
WD
1028 } else if (l < MAXPATHLEN) {
1029 fname[l++] = '.';
1030 fname[l] = '\0';
53f821f1 1031 }
649d65ed 1032 }
c627d613 1033
ebed4c3a 1034 if (link_stat(fname, &st) != 0) {
f76933b1 1035 if (f != -1) {
06c28400 1036 io_error |= IOERR_GENERAL;
ea42541f
WD
1037 rprintf(FERROR, "link_stat %s failed: %s\n",
1038 full_fname(fname), strerror(errno));
f76933b1 1039 }
649d65ed
AT
1040 continue;
1041 }
c627d613 1042
24d0fcde 1043 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
ebed4c3a 1044 rprintf(FINFO, "skipping directory %s\n", fname);
649d65ed
AT
1045 continue;
1046 }
c627d613 1047
649d65ed 1048 dir = NULL;
808c57c3 1049 olddir[0] = '\0';
649d65ed
AT
1050
1051 if (!relative_paths) {
ebed4c3a 1052 p = strrchr(fname, '/');
649d65ed
AT
1053 if (p) {
1054 *p = 0;
ebed4c3a 1055 if (p == fname)
649d65ed
AT
1056 dir = "/";
1057 else
ebed4c3a
MP
1058 dir = fname;
1059 fname = p + 1;
649d65ed 1060 }
24d0fcde 1061 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
649d65ed
AT
1062 /* this ensures we send the intermediate directories,
1063 thus getting their permissions right */
2154309a 1064 char *lp = lastpath, *fn = fname, *slash = fname;
649d65ed 1065 *p = 0;
2154309a
WD
1066 /* Skip any initial directories in our path that we
1067 * have in common with lastpath. */
1068 while (*fn && *lp == *fn) {
1069 if (*fn == '/')
1070 slash = fn;
1071 lp++, fn++;
1072 }
1073 *p = '/';
1074 if (fn != p || (*lp && *lp != '/')) {
1075 int copy_links_saved = copy_links;
1076 int recurse_saved = recurse;
1077 copy_links = copy_unsafe_links;
1078 /* set recurse to 1 to prevent make_file
1079 * from ignoring directory, but still
1080 * turn off the recursive parameter to
1081 * send_file_name */
1082 recurse = 1;
1083 while ((slash = strchr(slash+1, '/')) != 0) {
1084 *slash = 0;
1085 send_file_name(f, flist, fname, 0, 0);
1086 *slash = '/';
649d65ed 1087 }
2154309a
WD
1088 copy_links = copy_links_saved;
1089 recurse = recurse_saved;
1090 *p = 0;
1091 strlcpy(lastpath, fname, sizeof lastpath);
649d65ed
AT
1092 *p = '/';
1093 }
1094 }
ebed4c3a 1095
649d65ed
AT
1096 if (!*fname)
1097 fname = ".";
ebed4c3a 1098
649d65ed 1099 if (dir && *dir) {
882e6893
WD
1100 static char *lastdir;
1101 static int lastdir_len;
1102
808c57c3 1103 strcpy(olddir, curr_dir); /* can't overflow */
5243c216 1104
808c57c3 1105 if (!push_dir(dir)) {
06c28400 1106 io_error |= IOERR_GENERAL;
ea42541f
WD
1107 rprintf(FERROR, "push_dir %s failed: %s\n",
1108 full_fname(dir), strerror(errno));
649d65ed
AT
1109 continue;
1110 }
5243c216 1111
882e6893
WD
1112 if (lastdir && strcmp(lastdir, dir) == 0) {
1113 flist_dir = lastdir;
1114 flist_dir_len = lastdir_len;
1115 } else {
1116 if (lastdir)
1117 free(lastdir);
1118 flist_dir = lastdir = strdup(dir);
1119 flist_dir_len = lastdir_len = strlen(dir);
1120 }
2bca43f6 1121 }
ebed4c3a 1122
2bca43f6
DD
1123 if (one_file_system)
1124 set_filesystem(fname);
1125
ebed4c3a 1126 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
2bca43f6 1127
808c57c3 1128 if (olddir[0]) {
649d65ed 1129 flist_dir = NULL;
882e6893 1130 flist_dir_len = 0;
808c57c3 1131 if (!pop_dir(olddir)) {
ea42541f
WD
1132 rprintf(FERROR, "pop_dir %s failed: %s\n",
1133 full_fname(dir), strerror(errno));
65417579 1134 exit_cleanup(RERR_FILESELECT);
649d65ed 1135 }
649d65ed 1136 }
649d65ed 1137 }
dc5ddbcc 1138
983b1ed3 1139 if (f != -1) {
ebed4c3a 1140 send_file_entry(NULL, f, 0);
c627d613 1141
983b1ed3
WD
1142 if (show_filelist_p())
1143 finish_filelist_progress(flist);
1144 }
ebed4c3a 1145
827c37f6 1146 clean_flist(flist, 0, 0);
ebed4c3a 1147
785db4ce
WD
1148 if (f != -1) {
1149 /* Now send the uid/gid list. This was introduced in
1150 * protocol version 15 */
649d65ed 1151 send_uid_list(f);
f6c34742 1152
983b1ed3 1153 /* send the io_error flag */
ebed4c3a 1154 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1155
a261989c 1156 io_end_buffering();
a800434a
AT
1157 stats.flist_size = stats.total_written - start_write;
1158 stats.num_files = flist->count;
64c3523a 1159 if (write_batch)
ebed4c3a 1160 write_batch_flist_info(flist->count, flist->files);
d6dead6b
AT
1161 }
1162
17faa41c 1163 if (verbose > 2)
ebed4c3a 1164 rprintf(FINFO, "send_file_list done\n");
17faa41c 1165
649d65ed 1166 return flist;
c627d613
AT
1167}
1168
1169
1170struct file_list *recv_file_list(int f)
1171{
ebed4c3a 1172 struct file_list *flist;
1ef00d20 1173 unsigned short flags;
ebed4c3a
MP
1174 int64 start_read;
1175 extern int list_only;
c627d613 1176
1bbd10fe
DD
1177 if (show_filelist_p())
1178 start_filelist_progress("receiving file list");
c627d613 1179
ebed4c3a 1180 start_read = stats.total_read;
a800434a 1181
58cadc86 1182 flist = new(struct file_list);
ebed4c3a
MP
1183 if (!flist)
1184 goto oom;
c627d613 1185
ebed4c3a
MP
1186 flist->count = 0;
1187 flist->malloced = 1000;
58cadc86 1188 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1189 if (!flist->files)
1190 goto oom;
c627d613
AT
1191
1192
1ef00d20 1193 while ((flags = read_byte(f)) != 0) {
5d2c5c4c 1194 int i = flist->count;
dbda5fbf 1195
d9d6bc52 1196 flist_expand(flist);
c627d613 1197
75bc8600
WD
1198 if (protocol_version >= 28 && (flags & EXTENDED_FLAGS))
1199 flags |= read_byte(f) << 8;
ebed4c3a 1200 receive_file_entry(&flist->files[i], flags, f);
c627d613 1201
ebed4c3a
MP
1202 if (S_ISREG(flist->files[i]->mode))
1203 stats.total_size += flist->files[i]->length;
c627d613 1204
ebed4c3a 1205 flist->count++;
c627d613 1206
db719fb0 1207 maybe_emit_filelist_progress(flist);
1bbd10fe 1208
8018edd3 1209 if (verbose > 2) {
ebed4c3a
MP
1210 rprintf(FINFO, "recv_file_name(%s)\n",
1211 f_name(flist->files[i]));
8018edd3 1212 }
ebed4c3a 1213 }
5911fee5 1214 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
c627d613 1215
ebed4c3a
MP
1216 if (verbose > 2)
1217 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1218
b7736c79 1219 if (show_filelist_p())
1bbd10fe 1220 finish_filelist_progress(flist);
a06d19e3 1221
983b1ed3
WD
1222 clean_flist(flist, relative_paths, 1);
1223
785db4ce
WD
1224 if (f != -1) {
1225 /* Now send the uid/gid list. This was introduced in
1226 * protocol version 15 */
ebed4c3a 1227 recv_uid_list(f, flist);
f6c34742 1228
785db4ce
WD
1229 if (!read_batch) {
1230 /* Recv the io_error flag */
1231 if (lp_ignore_errors(module_id) || ignore_errors)
1232 read_int(f);
1233 else
1234 io_error |= read_int(f);
1235 }
ebed4c3a 1236 }
6ba9279f 1237
ebed4c3a
MP
1238 if (list_only) {
1239 int i;
b7736c79 1240 for (i = 0; i < flist->count; i++)
ebed4c3a 1241 list_file_entry(flist->files[i]);
ebed4c3a 1242 }
f7632fc6 1243
ebed4c3a
MP
1244 if (verbose > 2)
1245 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1246
ebed4c3a
MP
1247 stats.flist_size = stats.total_read - start_read;
1248 stats.num_files = flist->count;
a800434a 1249
ebed4c3a 1250 return flist;
c627d613 1251
ebed4c3a
MP
1252 oom:
1253 out_of_memory("recv_file_list");
1254 return NULL; /* not reached */
c627d613
AT
1255}
1256
1257
fa45cda1 1258int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1259{
fa45cda1
S
1260 struct file_struct *f1 = *file1;
1261 struct file_struct *f2 = *file2;
1262
1263 if (!f1->basename && !f2->basename)
ebed4c3a 1264 return 0;
fa45cda1 1265 if (!f1->basename)
ebed4c3a 1266 return -1;
fa45cda1 1267 if (!f2->basename)
ebed4c3a 1268 return 1;
fa45cda1
S
1269 if (f1->dirname == f2->dirname)
1270 return u_strcmp(f1->basename, f2->basename);
1271 return f_name_cmp(f1, f2);
c627d613
AT
1272}
1273
1274
ebed4c3a 1275int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1276{
ebed4c3a 1277 int low = 0, high = flist->count - 1;
d966ee25 1278
ca23c51a
WD
1279 while (high >= 0 && !flist->files[high]->basename) high--;
1280
1281 if (high < 0)
ebed4c3a 1282 return -1;
d966ee25
AT
1283
1284 while (low != high) {
ebed4c3a 1285 int mid = (low + high) / 2;
b7736c79 1286 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
ebed4c3a
MP
1287 if (ret == 0)
1288 return flist_up(flist, mid);
b7736c79 1289 if (ret > 0)
ebed4c3a 1290 high = mid;
b7736c79 1291 else
ebed4c3a 1292 low = mid + 1;
d966ee25
AT
1293 }
1294
ebed4c3a
MP
1295 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1296 return flist_up(flist, low);
d966ee25 1297 return -1;
c627d613
AT
1298}
1299
1300
3ec4dd97
AT
1301/*
1302 * free up one file
1303 */
66203a98 1304void free_file(struct file_struct *file)
c627d613 1305{
ebed4c3a
MP
1306 if (!file)
1307 return;
1308 if (file->basename)
1309 free(file->basename);
728d0922
WD
1310 if (!IS_DEVICE(file->mode) && file->u.link)
1311 free(file->u.link); /* Handles u.sum too. */
92cc9dd7
WD
1312 if (file->link_u.idev)
1313 free((char*)file->link_u.idev); /* Handles link_u.links too. */
3d382777 1314 *file = null_file;
3ec4dd97 1315}
c627d613 1316
c627d613 1317
3d382777
AT
1318/*
1319 * allocate a new file list
1320 */
1e34e4b7 1321struct file_list *flist_new(void)
3d382777
AT
1322{
1323 struct file_list *flist;
1324
58cadc86 1325 flist = new(struct file_list);
ebed4c3a
MP
1326 if (!flist)
1327 out_of_memory("send_file_list");
3d382777 1328
ebed4c3a 1329 flist->count = 0;
d9d6bc52
MP
1330 flist->malloced = 0;
1331 flist->files = NULL;
1332
3d382777
AT
1333#if ARENA_SIZE > 0
1334 flist->string_area = string_area_new(0);
1335#else
5c66303a 1336 flist->string_area = NULL;
3d382777
AT
1337#endif
1338 return flist;
1339}
ebed4c3a 1340
3ec4dd97
AT
1341/*
1342 * free up all elements in a flist
1343 */
1344void flist_free(struct file_list *flist)
1345{
1346 int i;
ebed4c3a 1347 for (i = 1; i < flist->count; i++) {
3d382777
AT
1348 if (!flist->string_area)
1349 free_file(flist->files[i]);
649d65ed 1350 free(flist->files[i]);
ebed4c3a 1351 }
d9d6bc52
MP
1352 /* FIXME: I don't think we generally need to blank the flist
1353 * since it's about to be freed. This will just cause more
1354 * memory traffic. If you want a freed-memory debugger, you
1355 * know where to get it. */
ebed4c3a
MP
1356 memset((char *) flist->files, 0,
1357 sizeof(flist->files[0]) * flist->count);
3ec4dd97 1358 free(flist->files);
3d382777
AT
1359 if (flist->string_area)
1360 string_area_free(flist->string_area);
ebed4c3a 1361 memset((char *) flist, 0, sizeof(*flist));
3ec4dd97 1362 free(flist);
c627d613
AT
1363}
1364
1365
1366/*
1367 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1368 * duplicate names can cause corruption because of the pipelining
c627d613 1369 */
827c37f6 1370static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1371{
6931c138 1372 int i, prev_i = 0;
c627d613 1373
ebed4c3a 1374 if (!flist || flist->count == 0)
3ec4dd97 1375 return;
3ec4dd97 1376
ebed4c3a
MP
1377 qsort(flist->files, flist->count,
1378 sizeof(flist->files[0]), (int (*)()) file_compare);
1379
827c37f6 1380 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1381 if (flist->files[i]->basename) {
6931c138 1382 prev_i = i;
b91b50c0
WD
1383 break;
1384 }
1385 }
1386 while (++i < flist->count) {
1387 if (!flist->files[i]->basename)
1388 continue;
8018edd3 1389 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
b91b50c0 1390 if (verbose > 1 && !am_server) {
ebed4c3a
MP
1391 rprintf(FINFO,
1392 "removing duplicate name %s from file list %d\n",
8018edd3 1393 f_name(flist->files[i]), i);
b91b50c0 1394 }
6931c138
WD
1395 /* Make sure that if we unduplicate '.', that we don't
1396 * lose track of a user-specified starting point (or
1397 * else deletions will mysteriously fail with -R). */
1398 if (flist->files[i]->flags & FLAG_DELETE)
1399 flist->files[prev_i]->flags |= FLAG_DELETE;
b91b50c0
WD
1400 /* it's not great that the flist knows the semantics of
1401 * the file memory usage, but i'd rather not add a flag
1402 * byte to that struct.
1403 * XXX can i use a bit in the flags field? */
3d382777
AT
1404 if (flist->string_area)
1405 flist->files[i][0] = null_file;
1406 else
1407 free_file(flist->files[i]);
728d0922 1408 } else
6931c138 1409 prev_i = i;
3ec4dd97 1410 }
0199b05f
AT
1411
1412 if (strip_root) {
1413 /* we need to strip off the root directory in the case
1414 of relative paths, but this must be done _after_
1415 the sorting phase */
ebed4c3a 1416 for (i = 0; i < flist->count; i++) {
0199b05f
AT
1417 if (flist->files[i]->dirname &&
1418 flist->files[i]->dirname[0] == '/') {
1419 memmove(&flist->files[i]->dirname[0],
1420 &flist->files[i]->dirname[1],
1421 strlen(flist->files[i]->dirname));
1422 }
ebed4c3a
MP
1423
1424 if (flist->files[i]->dirname &&
0199b05f
AT
1425 !flist->files[i]->dirname[0]) {
1426 flist->files[i]->dirname = NULL;
1427 }
1428 }
1429 }
1430
ebed4c3a
MP
1431 if (verbose <= 3)
1432 return;
0199b05f 1433
ebed4c3a 1434 for (i = 0; i < flist->count; i++) {
1ef00d20
WD
1435 rprintf(FINFO, "[%ld] i=%d %s %s mode=0%o len=%.0f\n",
1436 (long) getpid(), i,
74e708d8
AT
1437 NS(flist->files[i]->dirname),
1438 NS(flist->files[i]->basename),
08a740ff 1439 (int) flist->files[i]->mode,
ebed4c3a 1440 (double) flist->files[i]->length);
0199b05f 1441 }
3ec4dd97
AT
1442}
1443
1444
8018edd3
WD
1445enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1446
1447/* Compare the names of two file_struct entities, just like strcmp()
1448 * would do if it were operating on the joined strings. We assume
1449 * that there are no 0-length strings.
3ec4dd97 1450 */
8018edd3 1451int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1452{
8018edd3
WD
1453 int dif;
1454 const uchar *c1, *c2;
1ef00d20 1455 enum fnc_state state1, state2;
8018edd3
WD
1456
1457 if (!f1 || !f1->basename) {
1458 if (!f2 || !f2->basename)
1459 return 0;
1460 return -1;
1461 }
1462 if (!f2 || !f2->basename)
1463 return 1;
1464
e90b8ace 1465 if (!(c1 = (uchar*)f1->dirname)) {
8018edd3 1466 state1 = fnc_BASE;
e90b8ace 1467 c1 = (uchar*)f1->basename;
728d0922 1468 } else
1ef00d20 1469 state1 = fnc_DIR;
e90b8ace 1470 if (!(c2 = (uchar*)f2->dirname)) {
8018edd3 1471 state2 = fnc_BASE;
e90b8ace 1472 c2 = (uchar*)f2->basename;
728d0922 1473 } else
1ef00d20 1474 state2 = fnc_DIR;
8018edd3
WD
1475
1476 while (1) {
1477 if ((dif = (int)*c1 - (int)*c2) != 0)
1478 break;
1479 if (!*++c1) {
1480 switch (state1) {
1481 case fnc_DIR:
1482 state1 = fnc_SLASH;
e90b8ace 1483 c1 = (uchar*)"/";
8018edd3
WD
1484 break;
1485 case fnc_SLASH:
1486 state1 = fnc_BASE;
e90b8ace 1487 c1 = (uchar*)f1->basename;
8018edd3
WD
1488 break;
1489 case fnc_BASE:
1490 break;
1491 }
1492 }
1493 if (!*++c2) {
1494 switch (state2) {
1495 case fnc_DIR:
1496 state2 = fnc_SLASH;
e90b8ace 1497 c2 = (uchar*)"/";
8018edd3
WD
1498 break;
1499 case fnc_SLASH:
1500 state2 = fnc_BASE;
e90b8ace 1501 c2 = (uchar*)f2->basename;
8018edd3
WD
1502 break;
1503 case fnc_BASE:
1504 if (!*c1)
1505 return 0;
1506 break;
1507 }
1508 }
1509 }
1510
1511 return dif;
1512}
1513
3ec4dd97 1514
8018edd3 1515/* Return a copy of the full filename of a flist entry, using the indicated
882e6893
WD
1516 * buffer. No size-checking is done because we checked the size when creating
1517 * the file_struct entry.
8018edd3 1518 */
882e6893 1519char *f_name_to(struct file_struct *f, char *fbuf)
8018edd3 1520{
ebed4c3a
MP
1521 if (!f || !f->basename)
1522 return NULL;
3ec4dd97 1523
3ec4dd97 1524 if (f->dirname) {
882e6893
WD
1525 int len = strlen(f->dirname);
1526 memcpy(fbuf, f->dirname, len);
1527 fbuf[len] = '/';
1528 strcpy(fbuf + len + 1, f->basename);
8018edd3 1529 } else
882e6893 1530 strcpy(fbuf, f->basename);
b7736c79 1531 return fbuf;
8018edd3 1532}
e03dfae5 1533
3ec4dd97 1534
8018edd3
WD
1535/* Like f_name_to(), but we rotate through 5 static buffers of our own.
1536 */
1537char *f_name(struct file_struct *f)
1538{
1539 static char names[5][MAXPATHLEN];
1540 static unsigned int n;
1541
1542 n = (n + 1) % (sizeof names / sizeof names[0]);
1543
882e6893 1544 return f_name_to(f, names[n]);
c627d613 1545}