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