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