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