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