2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
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.
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.
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.
22 * Generate and receive file lists
24 * @todo Get rid of the string_area optimization. Efficiently
25 * allocating blocks is the responsibility of the system's malloc
26 * library, not of rsync.
28 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
34 extern struct stats stats;
38 extern int always_checksum;
40 extern int cvs_exclude;
44 extern int one_file_system;
45 extern int make_backups;
46 extern int preserve_links;
47 extern int preserve_hard_links;
48 extern int preserve_perms;
49 extern int preserve_devices;
50 extern int preserve_uid;
51 extern int preserve_gid;
52 extern int preserve_times;
53 extern int relative_paths;
54 extern int copy_links;
55 extern int copy_unsafe_links;
56 extern int remote_version;
58 extern int sanitize_paths;
60 extern int read_batch;
61 extern int write_batch;
63 static char topsrcname[MAXPATHLEN];
65 static struct exclude_struct **local_exclude_list;
67 static struct file_struct null_file;
69 static void clean_flist(struct file_list *flist, int strip_root);
72 static int show_build_progress_p(void)
74 extern int do_progress;
76 return do_progress && verbose && recurse && !am_server;
80 * True if we're local, etc, and should emit progress emssages.
82 static void emit_build_progress(const struct file_list *flist)
90 static void finish_build_progress(const struct file_list *flist)
92 if (verbose && recurse && !am_server) {
93 /* This overwrites the progress line, if any. */
94 rprintf(FINFO, RSYNC_NAME ": %d files to consider.\n",
100 static struct string_area *string_area_new(int size)
102 struct string_area *a;
104 if (size <= 0) size = ARENA_SIZE;
105 a = malloc(sizeof(*a));
106 if (!a) out_of_memory("string_area_new");
107 a->current = a->base = malloc(size);
108 if (!a->current) out_of_memory("string_area_new buffer");
109 a->end = a->base + size;
115 static void string_area_free(struct string_area *a)
117 struct string_area *next;
119 for ( ; a ; a = next) {
125 static char *string_area_malloc(struct string_area **ap, int size)
128 struct string_area *a;
130 /* does the request fit into the current space? */
132 if (a->current + size >= a->end) {
133 /* no; get space, move new string_area to front of the list */
134 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
139 /* have space; do the "allocation." */
145 static char *string_area_strdup(struct string_area **ap, const char *src)
147 char* dest = string_area_malloc(ap, strlen(src) + 1);
148 return strcpy(dest, src);
151 static void list_file_entry(struct file_struct *f)
156 /* this can happen if duplicate names were removed */
159 permstring(perms, f->mode);
161 if (preserve_links && S_ISLNK(f->mode)) {
162 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
164 (double)f->length, timestring(f->modtime),
167 rprintf(FINFO,"%s %11.0f %s %s\n",
169 (double)f->length, timestring(f->modtime), f_name(f));
174 int readlink_stat(const char *Path, STRUCT_STAT *Buffer, char *Linkbuf)
178 return do_stat(Path, Buffer);
180 if (do_lstat(Path, Buffer) == -1) {
183 if (S_ISLNK(Buffer->st_mode)) {
185 if ((l = readlink((char *) Path, Linkbuf, MAXPATHLEN-1))== -1) {
189 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
190 unsafe_symlink(Linkbuf, topsrcname)) {
191 return do_stat(Path, Buffer);
196 return do_stat(Path, Buffer);
200 int link_stat(const char *Path, STRUCT_STAT *Buffer)
204 return do_stat(Path, Buffer);
206 return do_lstat(Path, Buffer);
209 return do_stat(Path, Buffer);
214 This function is used to check if a file should be included/excluded
215 from the list of files based on its name and type etc
217 static int check_exclude_file(int f,char *fname,STRUCT_STAT *st)
219 extern int delete_excluded;
221 /* f is set to -1 when calculating deletion file list */
222 if ((f == -1) && delete_excluded) {
225 if (check_exclude(fname,local_exclude_list,st)) {
231 /* used by the one_file_system code */
232 static dev_t filesystem_dev;
234 static void set_filesystem(char *fname)
237 if (link_stat(fname,&st) != 0) return;
238 filesystem_dev = st.st_dev;
242 static int to_wire_mode(mode_t mode)
244 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
245 return (mode & ~(_S_IFMT)) | 0120000;
250 static mode_t from_wire_mode(int mode)
252 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
253 return (mode & ~(_S_IFMT)) | _S_IFLNK;
259 static void send_directory(int f,struct file_list *flist,char *dir);
261 static char *flist_dir;
264 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
267 static time_t last_time;
268 static mode_t last_mode;
269 static dev_t last_rdev;
270 static uid_t last_uid;
271 static gid_t last_gid;
272 static char lastname[MAXPATHLEN];
283 fname = f_name(file);
287 if (file->mode == last_mode) flags |= SAME_MODE;
288 if (file->rdev == last_rdev) flags |= SAME_RDEV;
289 if (file->uid == last_uid) flags |= SAME_UID;
290 if (file->gid == last_gid) flags |= SAME_GID;
291 if (file->modtime == last_time) flags |= SAME_TIME;
293 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
294 l2 = strlen(fname) - l1;
296 if (l1 > 0) flags |= SAME_NAME;
297 if (l2 > 255) flags |= LONG_NAME;
299 /* we must make sure we don't send a zero flags byte or the other
300 end will terminate the flist transfer */
301 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
302 if (flags == 0) flags |= LONG_NAME;
305 if (flags & SAME_NAME)
307 if (flags & LONG_NAME)
311 write_buf(f,fname+l1,l2);
313 write_longint(f,file->length);
314 if (!(flags & SAME_TIME))
315 write_int(f,(int)file->modtime);
316 if (!(flags & SAME_MODE))
317 write_int(f,to_wire_mode(file->mode));
318 if (preserve_uid && !(flags & SAME_UID)) {
320 write_int(f,(int)file->uid);
322 if (preserve_gid && !(flags & SAME_GID)) {
324 write_int(f,(int)file->gid);
326 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
327 write_int(f,(int)file->rdev);
330 if (preserve_links && S_ISLNK(file->mode)) {
331 write_int(f,strlen(file->link));
332 write_buf(f,file->link,strlen(file->link));
336 #if SUPPORT_HARD_LINKS
337 if (preserve_hard_links && S_ISREG(file->mode)) {
338 if (remote_version < 26) {
339 /* 32-bit dev_t and ino_t */
340 write_int(f,(int)file->dev);
341 write_int(f,(int)file->inode);
343 /* 64-bit dev_t and ino_t */
344 write_longint(f, file->dev);
345 write_longint(f, file->inode);
350 if (always_checksum) {
351 if (remote_version < 21) {
352 write_buf(f,file->sum,2);
354 write_buf(f,file->sum,MD4_SUM_LENGTH);
358 last_mode = file->mode;
359 last_rdev = file->rdev;
360 last_uid = file->uid;
361 last_gid = file->gid;
362 last_time = file->modtime;
364 strlcpy(lastname,fname,MAXPATHLEN);
365 lastname[MAXPATHLEN-1] = 0;
370 static void receive_file_entry(struct file_struct **fptr,
371 unsigned flags,int f)
373 static time_t last_time;
374 static mode_t last_mode;
375 static dev_t last_rdev;
376 static uid_t last_uid;
377 static gid_t last_gid;
378 static char lastname[MAXPATHLEN];
379 char thisname[MAXPATHLEN];
380 unsigned int l1=0,l2=0;
382 struct file_struct *file;
384 if (flags & SAME_NAME)
387 if (flags & LONG_NAME)
392 file = (struct file_struct *)malloc(sizeof(*file));
393 if (!file) out_of_memory("receive_file_entry");
394 memset((char *)file, 0, sizeof(*file));
397 if (l2 >= MAXPATHLEN-l1) {
398 rprintf(FERROR,"overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
399 flags, l1, l2, lastname);
400 overflow("receive_file_entry");
403 strlcpy(thisname,lastname,l1+1);
404 read_sbuf(f,&thisname[l1],l2);
407 strlcpy(lastname,thisname,MAXPATHLEN);
408 lastname[MAXPATHLEN-1] = 0;
410 clean_fname(thisname);
412 if (sanitize_paths) {
413 sanitize_path(thisname, NULL);
416 if ((p = strrchr(thisname,'/'))) {
417 static char *lastdir;
419 if (lastdir && strcmp(thisname, lastdir)==0) {
420 file->dirname = lastdir;
422 file->dirname = strdup(thisname);
423 lastdir = file->dirname;
425 file->basename = strdup(p+1);
427 file->dirname = NULL;
428 file->basename = strdup(thisname);
431 if (!file->basename) out_of_memory("receive_file_entry 1");
435 file->length = read_longint(f);
436 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
437 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
439 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
441 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
442 if (preserve_devices && IS_DEVICE(file->mode))
443 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
445 if (preserve_links && S_ISLNK(file->mode)) {
448 rprintf(FERROR,"overflow: l=%d\n", l);
449 overflow("receive_file_entry");
451 file->link = (char *)malloc(l+1);
452 if (!file->link) out_of_memory("receive_file_entry 2");
453 read_sbuf(f,file->link,l);
454 if (sanitize_paths) {
455 sanitize_path(file->link, file->dirname);
459 #if SUPPORT_HARD_LINKS
460 if (preserve_hard_links && S_ISREG(file->mode)) {
461 if (remote_version < 26) {
462 file->dev = read_int(f);
463 file->inode = read_int(f);
465 file->dev = read_longint(f);
466 file->inode = read_longint(f);
471 if (always_checksum) {
472 file->sum = (char *)malloc(MD4_SUM_LENGTH);
473 if (!file->sum) out_of_memory("md4 sum");
474 if (remote_version < 21) {
475 read_buf(f,file->sum,2);
477 read_buf(f,file->sum,MD4_SUM_LENGTH);
481 last_mode = file->mode;
482 last_rdev = file->rdev;
483 last_uid = file->uid;
484 last_gid = file->gid;
485 last_time = file->modtime;
487 if (!preserve_perms) {
488 extern int orig_umask;
489 /* set an appropriate set of permissions based on original
490 permissions and umask. This emulates what GNU cp does */
491 file->mode &= ~orig_umask;
496 /* determine if a file in a different filesstem should be skipped
497 when one_file_system is set. We bascally only want to include
498 the mount points - but they can be hard to find! */
499 static int skip_filesystem(char *fname, STRUCT_STAT *st)
502 char *p = strrchr(fname, '/');
504 /* skip all but directories */
505 if (!S_ISDIR(st->st_mode)) return 1;
507 /* if its not a subdirectory then allow */
511 if (link_stat(fname, &st2)) {
517 return (st2.st_dev != filesystem_dev);
520 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
521 /* IRIX cc cares that the operands to the ternary have the same type. */
522 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
524 /* create a file_struct for a named file */
525 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
528 struct file_struct *file;
530 char sum[SUM_LENGTH];
532 char cleaned_name[MAXPATHLEN];
533 char linkbuf[MAXPATHLEN];
534 extern int module_id;
536 strlcpy(cleaned_name, fname, MAXPATHLEN);
537 cleaned_name[MAXPATHLEN-1] = 0;
538 clean_fname(cleaned_name);
539 if (sanitize_paths) {
540 sanitize_path(cleaned_name, NULL);
542 fname = cleaned_name;
544 memset(sum,0,SUM_LENGTH);
546 if (readlink_stat(fname,&st,linkbuf) != 0) {
547 int save_errno = errno;
548 if ((errno == ENOENT) && copy_links && !noexcludes) {
549 /* symlink pointing nowhere, see if excluded */
550 memset((char *)&st, 0, sizeof(st));
551 if (check_exclude_file(f,fname,&st)) {
552 /* file is excluded anyway, ignore silently */
557 rprintf(FERROR,"readlink %s: %s\n",
558 fname,strerror(save_errno));
562 /* we use noexcludes from backup.c */
563 if (noexcludes) goto skip_excludes;
565 if (S_ISDIR(st.st_mode) && !recurse) {
566 rprintf(FINFO,"skipping directory %s\n",fname);
570 if (one_file_system && st.st_dev != filesystem_dev) {
571 if (skip_filesystem(fname, &st))
575 if (check_exclude_file(f,fname,&st))
579 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
585 rprintf(FINFO,"make_file(%d,%s)\n",f,fname);
587 file = (struct file_struct *)malloc(sizeof(*file));
588 if (!file) out_of_memory("make_file");
589 memset((char *)file,0,sizeof(*file));
591 if ((p = strrchr(fname,'/'))) {
592 static char *lastdir;
594 if (lastdir && strcmp(fname, lastdir)==0) {
595 file->dirname = lastdir;
597 file->dirname = strdup(fname);
598 lastdir = file->dirname;
600 file->basename = STRDUP(ap, p+1);
603 file->dirname = NULL;
604 file->basename = STRDUP(ap, fname);
607 file->modtime = st.st_mtime;
608 file->length = st.st_size;
609 file->mode = st.st_mode;
610 file->uid = st.st_uid;
611 file->gid = st.st_gid;
612 file->dev = st.st_dev;
613 file->inode = st.st_ino;
615 file->rdev = st.st_rdev;
619 if (S_ISLNK(st.st_mode)) {
620 file->link = STRDUP(ap, linkbuf);
624 if (always_checksum) {
625 file->sum = (char *)MALLOC(ap, MD4_SUM_LENGTH);
626 if (!file->sum) out_of_memory("md4 sum");
627 /* drat. we have to provide a null checksum for non-regular
628 files in order to be compatible with earlier versions
630 if (S_ISREG(st.st_mode)) {
631 file_checksum(fname,file->sum,st.st_size);
633 memset(file->sum, 0, MD4_SUM_LENGTH);
638 static char *lastdir;
639 if (lastdir && strcmp(lastdir, flist_dir)==0) {
640 file->basedir = lastdir;
642 file->basedir = strdup(flist_dir);
643 lastdir = file->basedir;
646 file->basedir = NULL;
649 if (!S_ISDIR(st.st_mode))
650 stats.total_size += st.st_size;
657 void send_file_name(int f,struct file_list *flist,char *fname,
658 int recursive, unsigned base_flags)
660 struct file_struct *file;
662 file = make_file(f,fname, &flist->string_area, 0);
666 if (show_build_progress_p() & !(flist->count % 100))
667 emit_build_progress(flist);
669 if (flist->count >= flist->malloced) {
670 if (flist->malloced < 1000)
671 flist->malloced += 1000;
673 flist->malloced *= 2;
674 flist->files = (struct file_struct **)realloc(flist->files,
675 sizeof(flist->files[0])*
678 out_of_memory("send_file_name");
681 if (write_batch) /* dw */
682 file->flags = FLAG_DELETE;
684 if (strcmp(file->basename,"")) {
685 flist->files[flist->count++] = file;
686 send_file_entry(file,f,base_flags);
689 if (S_ISDIR(file->mode) && recursive) {
690 struct exclude_struct **last_exclude_list = local_exclude_list;
691 send_directory(f,flist,f_name(file));
692 local_exclude_list = last_exclude_list;
699 static void send_directory(int f,struct file_list *flist,char *dir)
703 char fname[MAXPATHLEN];
710 rprintf(FERROR,"opendir(%s): %s\n",
711 dir,strerror(errno));
715 strlcpy(fname,dir,MAXPATHLEN);
717 if (fname[l-1] != '/') {
718 if (l == MAXPATHLEN-1) {
720 rprintf(FERROR,"skipping long-named directory %s\n",fname);
724 strlcat(fname,"/", MAXPATHLEN);
727 p = fname + strlen(fname);
729 local_exclude_list = NULL;
732 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
733 strcpy(p,".cvsignore");
734 local_exclude_list = make_exclude_list(fname,NULL,0,0);
737 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
741 for (di=readdir(d); di; di=readdir(d)) {
742 char *dname = d_name(di);
743 if (strcmp(dname,".")==0 ||
744 strcmp(dname,"..")==0)
746 strlcpy(p,dname,MAXPATHLEN-l);
747 send_file_name(f,flist,fname,recurse,0);
750 if (local_exclude_list) {
751 add_exclude_list("!", &local_exclude_list, 0);
760 * I *think* f==-1 means that the list should just be built in memory
761 * and not transmitted. But who can tell? -- mbp
763 struct file_list *send_file_list(int f,int argc,char *argv[])
767 char *p,*dir,*olddir;
768 char lastpath[MAXPATHLEN]="";
769 struct file_list *flist;
772 if (verbose && recurse && !am_server && f != -1) {
773 rprintf(FINFO, RSYNC_NAME ": building file list...\n");
775 rprintf(FINFO, "\n");
779 start_write = stats.total_written;
784 io_start_buffering(f);
787 for (i=0;i<argc;i++) {
788 char *fname = topsrcname;
790 strlcpy(fname,argv[i],MAXPATHLEN);
793 if (l != 1 && fname[l-1] == '/') {
794 if ((l == 2) && (fname[0] == '.')) {
795 /* Turn ./ into just . rather than ./.
796 This was put in to avoid a problem with
797 rsync -aR --delete from ./
798 The send_file_name() below of ./ was
799 mysteriously preventing deletes */
802 strlcat(fname,".",MAXPATHLEN);
806 if (link_stat(fname,&st) != 0) {
809 rprintf(FERROR,"link_stat %s : %s\n",fname,strerror(errno));
814 if (S_ISDIR(st.st_mode) && !recurse) {
815 rprintf(FINFO,"skipping directory %s\n",fname);
822 if (!relative_paths) {
823 p = strrchr(fname,'/');
832 } else if (f != -1 && (p=strrchr(fname,'/'))) {
833 /* this ensures we send the intermediate directories,
834 thus getting their permissions right */
836 if (strcmp(lastpath,fname)) {
837 strlcpy(lastpath, fname, sizeof(lastpath));
839 for (p=fname+1; (p=strchr(p,'/')); p++) {
840 int copy_links_saved = copy_links;
841 int recurse_saved = recurse;
843 copy_links = copy_unsafe_links;
844 /* set recurse to 1 to prevent make_file
845 from ignoring directory, but still
846 turn off the recursive parameter to
849 send_file_name(f, flist, fname, 0, 0);
850 copy_links = copy_links_saved;
851 recurse = recurse_saved;
863 olddir = push_dir(dir, 1);
867 rprintf(FERROR,"push_dir %s : %s\n",
868 dir,strerror(errno));
876 set_filesystem(fname);
878 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
880 if (olddir != NULL) {
882 if (pop_dir(olddir) != 0) {
883 rprintf(FERROR,"pop_dir %s : %s\n",
884 dir,strerror(errno));
885 exit_cleanup(RERR_FILESELECT);
890 topsrcname[0] = '\0';
893 send_file_entry(NULL,f,0);
896 finish_build_progress(flist);
898 clean_flist(flist, 0);
900 /* now send the uid/gid list. This was introduced in protocol
902 if (f != -1 && remote_version >= 15) {
906 /* if protocol version is >= 17 then send the io_error flag */
907 if (f != -1 && remote_version >= 17) {
908 extern int module_id;
909 write_int(f, lp_ignore_errors(module_id)? 0 : io_error);
914 stats.flist_size = stats.total_written - start_write;
915 stats.num_files = flist->count;
916 if (write_batch) /* dw */
917 write_batch_flist_info(flist->count, flist->files);
921 rprintf(FINFO,"send_file_list done\n");
927 struct file_list *recv_file_list(int f)
929 struct file_list *flist;
932 extern int list_only;
934 if (verbose && recurse && !am_server) {
935 rprintf(FINFO,"receiving file list ... ");
939 start_read = stats.total_read;
941 flist = (struct file_list *)malloc(sizeof(flist[0]));
946 flist->malloced=1000;
947 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
953 for (flags=read_byte(f); flags; flags=read_byte(f)) {
954 int i = flist->count;
956 if (i >= flist->malloced) {
957 if (flist->malloced < 1000)
958 flist->malloced += 1000;
960 flist->malloced *= 2;
961 flist->files =(struct file_struct **)realloc(flist->files,
962 sizeof(flist->files[0])*
968 receive_file_entry(&flist->files[i],flags,f);
970 if (S_ISREG(flist->files[i]->mode))
971 stats.total_size += flist->files[i]->length;
976 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
981 rprintf(FINFO,"received %d names\n",flist->count);
983 clean_flist(flist, relative_paths);
985 if (verbose && recurse && !am_server) {
986 rprintf(FINFO,"done\n");
989 /* now recv the uid/gid list. This was introduced in protocol version 15 */
990 if (f != -1 && remote_version >= 15) {
991 recv_uid_list(f, flist);
994 /* if protocol version is >= 17 then recv the io_error flag */
995 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
996 extern int module_id;
997 extern int ignore_errors;
998 if (lp_ignore_errors(module_id) || ignore_errors) {
1001 io_error |= read_int(f);
1007 for (i=0;i<flist->count;i++) {
1008 list_file_entry(flist->files[i]);
1014 rprintf(FINFO,"recv_file_list done\n");
1016 stats.flist_size = stats.total_read - start_read;
1017 stats.num_files = flist->count;
1022 out_of_memory("recv_file_list");
1023 return NULL; /* not reached */
1028 * XXX: This is currently the hottest function while building the file
1029 * list, because building f_name()s every time is expensive.
1031 int file_compare(struct file_struct **f1,struct file_struct **f2)
1033 if (!(*f1)->basename && !(*f2)->basename) return 0;
1034 if (!(*f1)->basename) return -1;
1035 if (!(*f2)->basename) return 1;
1036 if ((*f1)->dirname == (*f2)->dirname)
1037 return u_strcmp((*f1)->basename, (*f2)->basename);
1038 return u_strcmp(f_name(*f1),f_name(*f2));
1042 int flist_find(struct file_list *flist,struct file_struct *f)
1044 int low=0,high=flist->count-1;
1046 if (flist->count <= 0) return -1;
1048 while (low != high) {
1049 int mid = (low+high)/2;
1050 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1051 if (ret == 0) return flist_up(flist, mid);
1059 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
1060 return flist_up(flist,low);
1068 void free_file(struct file_struct *file)
1071 if (file->basename) free(file->basename);
1072 if (file->link) free(file->link);
1073 if (file->sum) free(file->sum);
1079 * allocate a new file list
1081 struct file_list *flist_new()
1083 struct file_list *flist;
1085 flist = (struct file_list *)malloc(sizeof(flist[0]));
1086 if (!flist) out_of_memory("send_file_list");
1089 flist->malloced = 1000;
1090 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
1092 if (!flist->files) out_of_memory("send_file_list");
1094 flist->string_area = string_area_new(0);
1096 flist->string_area = NULL;
1101 * free up all elements in a flist
1103 void flist_free(struct file_list *flist)
1106 for (i=1;i<flist->count;i++) {
1107 if (!flist->string_area)
1108 free_file(flist->files[i]);
1109 free(flist->files[i]);
1111 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
1113 if (flist->string_area)
1114 string_area_free(flist->string_area);
1115 memset((char *)flist, 0, sizeof(*flist));
1121 * This routine ensures we don't have any duplicate names in our file list.
1122 * duplicate names can cause corruption because of the pipelining
1124 static void clean_flist(struct file_list *flist, int strip_root)
1128 if (!flist || flist->count == 0)
1131 qsort(flist->files,flist->count,
1132 sizeof(flist->files[0]),
1133 (int (*)())file_compare);
1135 for (i=1;i<flist->count;i++) {
1136 if (flist->files[i]->basename &&
1137 flist->files[i-1]->basename &&
1138 strcmp(f_name(flist->files[i]),
1139 f_name(flist->files[i-1])) == 0) {
1140 if (verbose > 1 && !am_server)
1141 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
1142 f_name(flist->files[i-1]),i-1);
1143 /* it's not great that the flist knows the semantics of the
1144 * file memory usage, but i'd rather not add a flag byte
1145 * to that struct. XXX can i use a bit in the flags field? */
1146 if (flist->string_area)
1147 flist->files[i][0] = null_file;
1149 free_file(flist->files[i]);
1153 /* FIXME: There is a bug here when filenames are repeated more
1154 * than once, because we don't handle freed files when doing
1155 * the comparison. */
1158 /* we need to strip off the root directory in the case
1159 of relative paths, but this must be done _after_
1160 the sorting phase */
1161 for (i=0;i<flist->count;i++) {
1162 if (flist->files[i]->dirname &&
1163 flist->files[i]->dirname[0] == '/') {
1164 memmove(&flist->files[i]->dirname[0],
1165 &flist->files[i]->dirname[1],
1166 strlen(flist->files[i]->dirname));
1169 if (flist->files[i]->dirname &&
1170 !flist->files[i]->dirname[0]) {
1171 flist->files[i]->dirname = NULL;
1177 if (verbose <= 3) return;
1179 for (i=0;i<flist->count;i++) {
1180 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%.0f\n",
1182 NS(flist->files[i]->dirname),
1183 NS(flist->files[i]->basename),
1184 (int) flist->files[i]->mode,
1185 (double)flist->files[i]->length);
1191 * return the full filename of a flist entry
1193 * This function is too expensive at the moment, because it copies
1194 * strings when often we only want to compare them. In any case,
1195 * using strlcat is silly because it will walk the string repeatedly.
1197 char *f_name(struct file_struct *f)
1199 static char names[10][MAXPATHLEN];
1203 if (!f || !f->basename) return NULL;
1210 off = strlcpy(p, f->dirname, MAXPATHLEN);
1211 off += strlcpy(p+off, "/", MAXPATHLEN-off);
1212 off += strlcpy(p+off, f->basename, MAXPATHLEN-off);
1214 strlcpy(p, f->basename, MAXPATHLEN);