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