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