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