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