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