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