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.
32 extern struct stats stats;
36 extern int always_checksum;
38 extern int cvs_exclude;
42 extern int one_file_system;
43 extern int make_backups;
44 extern int preserve_links;
45 extern int preserve_hard_links;
46 extern int preserve_perms;
47 extern int preserve_devices;
48 extern int preserve_uid;
49 extern int preserve_gid;
50 extern int preserve_times;
51 extern int relative_paths;
52 extern int copy_links;
53 extern int copy_unsafe_links;
54 extern int remote_version;
56 extern int sanitize_paths;
58 extern int read_batch;
59 extern int write_batch;
61 static char topsrcname[MAXPATHLEN];
63 static struct exclude_struct **local_exclude_list;
65 static struct file_struct null_file;
67 static void clean_flist(struct file_list *flist, int strip_root);
70 static int show_build_progress_p(void)
72 extern int do_progress;
74 return do_progress && verbose && recurse && !am_server;
78 * True if we're local, etc, and should emit progress emssages.
80 static void emit_build_progress(const struct file_list *flist)
88 static void finish_build_progress(const struct file_list *flist)
90 if (verbose && recurse && !am_server) {
91 /* This overwrites the progress line, if any. */
92 rprintf(FINFO, RSYNC_NAME ": %d files to consider.\n",
98 static struct string_area *string_area_new(int size)
100 struct string_area *a;
102 if (size <= 0) size = ARENA_SIZE;
103 a = malloc(sizeof(*a));
104 if (!a) out_of_memory("string_area_new");
105 a->current = a->base = malloc(size);
106 if (!a->current) out_of_memory("string_area_new buffer");
107 a->end = a->base + size;
113 static void string_area_free(struct string_area *a)
115 struct string_area *next;
117 for ( ; a ; a = next) {
123 static char *string_area_malloc(struct string_area **ap, int size)
126 struct string_area *a;
128 /* does the request fit into the current space? */
130 if (a->current + size >= a->end) {
131 /* no; get space, move new string_area to front of the list */
132 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
137 /* have space; do the "allocation." */
143 static char *string_area_strdup(struct string_area **ap, const char *src)
145 char* dest = string_area_malloc(ap, strlen(src) + 1);
146 return strcpy(dest, src);
149 static void list_file_entry(struct file_struct *f)
154 /* this can happen if duplicate names were removed */
157 permstring(perms, f->mode);
159 if (preserve_links && S_ISLNK(f->mode)) {
160 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
162 (double)f->length, timestring(f->modtime),
165 rprintf(FINFO,"%s %11.0f %s %s\n",
167 (double)f->length, timestring(f->modtime), f_name(f));
172 int readlink_stat(const char *Path, STRUCT_STAT *Buffer, char *Linkbuf)
176 return do_stat(Path, Buffer);
178 if (do_lstat(Path, Buffer) == -1) {
181 if (S_ISLNK(Buffer->st_mode)) {
183 if ((l = readlink((char *) Path, Linkbuf, MAXPATHLEN-1))== -1) {
187 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
188 unsafe_symlink(Linkbuf, topsrcname)) {
189 return do_stat(Path, Buffer);
194 return do_stat(Path, Buffer);
198 int link_stat(const char *Path, STRUCT_STAT *Buffer)
202 return do_stat(Path, Buffer);
204 return do_lstat(Path, Buffer);
207 return do_stat(Path, Buffer);
212 This function is used to check if a file should be included/excluded
213 from the list of files based on its name and type etc
215 static int check_exclude_file(int f,char *fname,STRUCT_STAT *st)
217 extern int delete_excluded;
219 /* f is set to -1 when calculating deletion file list */
220 if ((f == -1) && delete_excluded) {
223 if (check_exclude(fname,local_exclude_list,st)) {
229 /* used by the one_file_system code */
230 static dev_t filesystem_dev;
232 static void set_filesystem(char *fname)
235 if (link_stat(fname,&st) != 0) return;
236 filesystem_dev = st.st_dev;
240 static int to_wire_mode(mode_t mode)
242 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
243 return (mode & ~(_S_IFMT)) | 0120000;
248 static mode_t from_wire_mode(int mode)
250 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
251 return (mode & ~(_S_IFMT)) | _S_IFLNK;
257 static void send_directory(int f,struct file_list *flist,char *dir);
259 static char *flist_dir;
262 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
265 static time_t last_time;
266 static mode_t last_mode;
267 static dev_t last_rdev;
268 static uid_t last_uid;
269 static gid_t last_gid;
270 static char lastname[MAXPATHLEN];
281 fname = f_name(file);
285 if (file->mode == last_mode) flags |= SAME_MODE;
286 if (file->rdev == last_rdev) flags |= SAME_RDEV;
287 if (file->uid == last_uid) flags |= SAME_UID;
288 if (file->gid == last_gid) flags |= SAME_GID;
289 if (file->modtime == last_time) flags |= SAME_TIME;
291 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
292 l2 = strlen(fname) - l1;
294 if (l1 > 0) flags |= SAME_NAME;
295 if (l2 > 255) flags |= LONG_NAME;
297 /* we must make sure we don't send a zero flags byte or the other
298 end will terminate the flist transfer */
299 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
300 if (flags == 0) flags |= LONG_NAME;
303 if (flags & SAME_NAME)
305 if (flags & LONG_NAME)
309 write_buf(f,fname+l1,l2);
311 write_longint(f,file->length);
312 if (!(flags & SAME_TIME))
313 write_int(f,(int)file->modtime);
314 if (!(flags & SAME_MODE))
315 write_int(f,to_wire_mode(file->mode));
316 if (preserve_uid && !(flags & SAME_UID)) {
318 write_int(f,(int)file->uid);
320 if (preserve_gid && !(flags & SAME_GID)) {
322 write_int(f,(int)file->gid);
324 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
325 write_int(f,(int)file->rdev);
328 if (preserve_links && S_ISLNK(file->mode)) {
329 write_int(f,strlen(file->link));
330 write_buf(f,file->link,strlen(file->link));
334 #if SUPPORT_HARD_LINKS
335 if (preserve_hard_links && S_ISREG(file->mode)) {
336 if (remote_version < 26) {
337 /* 32-bit dev_t and ino_t */
338 write_int(f,(int)file->dev);
339 write_int(f,(int)file->inode);
341 /* 64-bit dev_t and ino_t */
342 write_longint(f, file->dev);
343 write_longint(f, file->inode);
348 if (always_checksum) {
349 if (remote_version < 21) {
350 write_buf(f,file->sum,2);
352 write_buf(f,file->sum,MD4_SUM_LENGTH);
356 last_mode = file->mode;
357 last_rdev = file->rdev;
358 last_uid = file->uid;
359 last_gid = file->gid;
360 last_time = file->modtime;
362 strlcpy(lastname,fname,MAXPATHLEN);
363 lastname[MAXPATHLEN-1] = 0;
368 static void receive_file_entry(struct file_struct **fptr,
369 unsigned flags,int f)
371 static time_t last_time;
372 static mode_t last_mode;
373 static dev_t last_rdev;
374 static uid_t last_uid;
375 static gid_t last_gid;
376 static char lastname[MAXPATHLEN];
377 char thisname[MAXPATHLEN];
380 struct file_struct *file;
382 if (flags & SAME_NAME)
385 if (flags & LONG_NAME)
390 file = (struct file_struct *)malloc(sizeof(*file));
391 if (!file) out_of_memory("receive_file_entry");
392 memset((char *)file, 0, sizeof(*file));
395 if (l2 >= MAXPATHLEN-l1) {
396 rprintf(FERROR,"overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
397 flags, l1, l2, lastname);
398 overflow("receive_file_entry");
401 strlcpy(thisname,lastname,l1+1);
402 read_sbuf(f,&thisname[l1],l2);
405 strlcpy(lastname,thisname,MAXPATHLEN);
406 lastname[MAXPATHLEN-1] = 0;
408 clean_fname(thisname);
410 if (sanitize_paths) {
411 sanitize_path(thisname, NULL);
414 if ((p = strrchr(thisname,'/'))) {
415 static char *lastdir;
417 if (lastdir && strcmp(thisname, lastdir)==0) {
418 file->dirname = lastdir;
420 file->dirname = strdup(thisname);
421 lastdir = file->dirname;
423 file->basename = strdup(p+1);
425 file->dirname = NULL;
426 file->basename = strdup(thisname);
429 if (!file->basename) out_of_memory("receive_file_entry 1");
433 file->length = read_longint(f);
434 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
435 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
437 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
439 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
440 if (preserve_devices && IS_DEVICE(file->mode))
441 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
443 if (preserve_links && S_ISLNK(file->mode)) {
445 file->link = (char *)malloc(l+1);
446 if (!file->link) out_of_memory("receive_file_entry 2");
447 read_sbuf(f,file->link,l);
448 if (sanitize_paths) {
449 sanitize_path(file->link, file->dirname);
453 #if SUPPORT_HARD_LINKS
454 if (preserve_hard_links && S_ISREG(file->mode)) {
455 if (remote_version < 26) {
456 file->dev = read_int(f);
457 file->inode = read_int(f);
459 file->dev = read_longint(f);
460 file->inode = read_longint(f);
465 if (always_checksum) {
466 file->sum = (char *)malloc(MD4_SUM_LENGTH);
467 if (!file->sum) out_of_memory("md4 sum");
468 if (remote_version < 21) {
469 read_buf(f,file->sum,2);
471 read_buf(f,file->sum,MD4_SUM_LENGTH);
475 last_mode = file->mode;
476 last_rdev = file->rdev;
477 last_uid = file->uid;
478 last_gid = file->gid;
479 last_time = file->modtime;
481 if (!preserve_perms) {
482 extern int orig_umask;
483 /* set an appropriate set of permissions based on original
484 permissions and umask. This emulates what GNU cp does */
485 file->mode &= ~orig_umask;
490 /* determine if a file in a different filesstem should be skipped
491 when one_file_system is set. We bascally only want to include
492 the mount points - but they can be hard to find! */
493 static int skip_filesystem(char *fname, STRUCT_STAT *st)
496 char *p = strrchr(fname, '/');
498 /* skip all but directories */
499 if (!S_ISDIR(st->st_mode)) return 1;
501 /* if its not a subdirectory then allow */
505 if (link_stat(fname, &st2)) {
511 return (st2.st_dev != filesystem_dev);
514 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
515 /* IRIX cc cares that the operands to the ternary have the same type. */
516 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
518 /* create a file_struct for a named file */
519 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
522 struct file_struct *file;
524 char sum[SUM_LENGTH];
526 char cleaned_name[MAXPATHLEN];
527 char linkbuf[MAXPATHLEN];
528 extern int module_id;
530 strlcpy(cleaned_name, fname, MAXPATHLEN);
531 cleaned_name[MAXPATHLEN-1] = 0;
532 clean_fname(cleaned_name);
533 if (sanitize_paths) {
534 sanitize_path(cleaned_name, NULL);
536 fname = cleaned_name;
538 memset(sum,0,SUM_LENGTH);
540 if (readlink_stat(fname,&st,linkbuf) != 0) {
541 int save_errno = errno;
542 if ((errno == ENOENT) && copy_links && !noexcludes) {
543 /* symlink pointing nowhere, see if excluded */
544 memset((char *)&st, 0, sizeof(st));
545 if (check_exclude_file(f,fname,&st)) {
546 /* file is excluded anyway, ignore silently */
551 rprintf(FERROR,"readlink %s: %s\n",
552 fname,strerror(save_errno));
556 /* we use noexcludes from backup.c */
557 if (noexcludes) goto skip_excludes;
559 if (S_ISDIR(st.st_mode) && !recurse) {
560 rprintf(FINFO,"skipping directory %s\n",fname);
564 if (one_file_system && st.st_dev != filesystem_dev) {
565 if (skip_filesystem(fname, &st))
569 if (check_exclude_file(f,fname,&st))
573 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
579 rprintf(FINFO,"make_file(%d,%s)\n",f,fname);
581 file = (struct file_struct *)malloc(sizeof(*file));
582 if (!file) out_of_memory("make_file");
583 memset((char *)file,0,sizeof(*file));
585 if ((p = strrchr(fname,'/'))) {
586 static char *lastdir;
588 if (lastdir && strcmp(fname, lastdir)==0) {
589 file->dirname = lastdir;
591 file->dirname = strdup(fname);
592 lastdir = file->dirname;
594 file->basename = STRDUP(ap, p+1);
597 file->dirname = NULL;
598 file->basename = STRDUP(ap, fname);
601 file->modtime = st.st_mtime;
602 file->length = st.st_size;
603 file->mode = st.st_mode;
604 file->uid = st.st_uid;
605 file->gid = st.st_gid;
606 file->dev = st.st_dev;
607 file->inode = st.st_ino;
609 file->rdev = st.st_rdev;
613 if (S_ISLNK(st.st_mode)) {
614 file->link = STRDUP(ap, linkbuf);
618 if (always_checksum) {
619 file->sum = (char *)MALLOC(ap, MD4_SUM_LENGTH);
620 if (!file->sum) out_of_memory("md4 sum");
621 /* drat. we have to provide a null checksum for non-regular
622 files in order to be compatible with earlier versions
624 if (S_ISREG(st.st_mode)) {
625 file_checksum(fname,file->sum,st.st_size);
627 memset(file->sum, 0, MD4_SUM_LENGTH);
632 static char *lastdir;
633 if (lastdir && strcmp(lastdir, flist_dir)==0) {
634 file->basedir = lastdir;
636 file->basedir = strdup(flist_dir);
637 lastdir = file->basedir;
640 file->basedir = NULL;
643 if (!S_ISDIR(st.st_mode))
644 stats.total_size += st.st_size;
651 void send_file_name(int f,struct file_list *flist,char *fname,
652 int recursive, unsigned base_flags)
654 struct file_struct *file;
656 file = make_file(f,fname, &flist->string_area, 0);
660 if (show_build_progress_p() & !(flist->count % 100))
661 emit_build_progress(flist);
663 if (flist->count >= flist->malloced) {
664 if (flist->malloced < 1000)
665 flist->malloced += 1000;
667 flist->malloced *= 2;
668 flist->files = (struct file_struct **)realloc(flist->files,
669 sizeof(flist->files[0])*
672 out_of_memory("send_file_name");
675 if (write_batch) /* dw */
676 file->flags = FLAG_DELETE;
678 if (strcmp(file->basename,"")) {
679 flist->files[flist->count++] = file;
680 send_file_entry(file,f,base_flags);
683 if (S_ISDIR(file->mode) && recursive) {
684 struct exclude_struct **last_exclude_list = local_exclude_list;
685 send_directory(f,flist,f_name(file));
686 local_exclude_list = last_exclude_list;
693 static void send_directory(int f,struct file_list *flist,char *dir)
697 char fname[MAXPATHLEN];
704 rprintf(FERROR,"opendir(%s): %s\n",
705 dir,strerror(errno));
709 strlcpy(fname,dir,MAXPATHLEN);
711 if (fname[l-1] != '/') {
712 if (l == MAXPATHLEN-1) {
714 rprintf(FERROR,"skipping long-named directory %s\n",fname);
718 strlcat(fname,"/", MAXPATHLEN);
721 p = fname + strlen(fname);
723 local_exclude_list = NULL;
726 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
727 strcpy(p,".cvsignore");
728 local_exclude_list = make_exclude_list(fname,NULL,0,0);
731 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
735 for (di=readdir(d); di; di=readdir(d)) {
736 char *dname = d_name(di);
737 if (strcmp(dname,".")==0 ||
738 strcmp(dname,"..")==0)
740 strlcpy(p,dname,MAXPATHLEN-l);
741 send_file_name(f,flist,fname,recurse,0);
744 if (local_exclude_list) {
745 add_exclude_list("!", &local_exclude_list, 0);
754 * I *think* f==-1 means that the list should just be built in memory
755 * and not transmitted. But who can tell? -- mbp
757 struct file_list *send_file_list(int f,int argc,char *argv[])
761 char *p,*dir,*olddir;
762 char lastpath[MAXPATHLEN]="";
763 struct file_list *flist;
766 if (verbose && recurse && !am_server && f != -1) {
767 rprintf(FINFO, RSYNC_NAME ": building file list...\n");
769 rprintf(FINFO, "\n");
773 start_write = stats.total_written;
778 io_start_buffering(f);
781 for (i=0;i<argc;i++) {
782 char *fname = topsrcname;
784 strlcpy(fname,argv[i],MAXPATHLEN);
787 if (l != 1 && fname[l-1] == '/') {
788 if ((l == 2) && (fname[0] == '.')) {
789 /* Turn ./ into just . rather than ./.
790 This was put in to avoid a problem with
791 rsync -aR --delete from ./
792 The send_file_name() below of ./ was
793 mysteriously preventing deletes */
796 strlcat(fname,".",MAXPATHLEN);
800 if (link_stat(fname,&st) != 0) {
803 rprintf(FERROR,"link_stat %s : %s\n",fname,strerror(errno));
808 if (S_ISDIR(st.st_mode) && !recurse) {
809 rprintf(FINFO,"skipping directory %s\n",fname);
816 if (!relative_paths) {
817 p = strrchr(fname,'/');
826 } else if (f != -1 && (p=strrchr(fname,'/'))) {
827 /* this ensures we send the intermediate directories,
828 thus getting their permissions right */
830 if (strcmp(lastpath,fname)) {
831 strlcpy(lastpath, fname, sizeof(lastpath));
833 for (p=fname+1; (p=strchr(p,'/')); p++) {
834 int copy_links_saved = copy_links;
835 int recurse_saved = recurse;
837 copy_links = copy_unsafe_links;
838 /* set recurse to 1 to prevent make_file
839 from ignoring directory, but still
840 turn off the recursive parameter to
843 send_file_name(f, flist, fname, 0, 0);
844 copy_links = copy_links_saved;
845 recurse = recurse_saved;
857 olddir = push_dir(dir, 1);
861 rprintf(FERROR,"push_dir %s : %s\n",
862 dir,strerror(errno));
870 set_filesystem(fname);
872 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
874 if (olddir != NULL) {
876 if (pop_dir(olddir) != 0) {
877 rprintf(FERROR,"pop_dir %s : %s\n",
878 dir,strerror(errno));
879 exit_cleanup(RERR_FILESELECT);
884 topsrcname[0] = '\0';
887 send_file_entry(NULL,f,0);
890 finish_build_progress(flist);
892 clean_flist(flist, 0);
894 /* now send the uid/gid list. This was introduced in protocol
896 if (f != -1 && remote_version >= 15) {
900 /* if protocol version is >= 17 then send the io_error flag */
901 if (f != -1 && remote_version >= 17) {
902 extern int module_id;
903 write_int(f, lp_ignore_errors(module_id)? 0 : io_error);
908 stats.flist_size = stats.total_written - start_write;
909 stats.num_files = flist->count;
910 if (write_batch) /* dw */
911 write_batch_flist_info(flist->count, flist->files);
915 rprintf(FINFO,"send_file_list done\n");
921 struct file_list *recv_file_list(int f)
923 struct file_list *flist;
926 extern int list_only;
928 if (verbose && recurse && !am_server) {
929 rprintf(FINFO,"receiving file list ... ");
933 start_read = stats.total_read;
935 flist = (struct file_list *)malloc(sizeof(flist[0]));
940 flist->malloced=1000;
941 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
947 for (flags=read_byte(f); flags; flags=read_byte(f)) {
948 int i = flist->count;
950 if (i >= flist->malloced) {
951 if (flist->malloced < 1000)
952 flist->malloced += 1000;
954 flist->malloced *= 2;
955 flist->files =(struct file_struct **)realloc(flist->files,
956 sizeof(flist->files[0])*
962 receive_file_entry(&flist->files[i],flags,f);
964 if (S_ISREG(flist->files[i]->mode))
965 stats.total_size += flist->files[i]->length;
970 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
975 rprintf(FINFO,"received %d names\n",flist->count);
977 clean_flist(flist, relative_paths);
979 if (verbose && recurse && !am_server) {
980 rprintf(FINFO,"done\n");
983 /* now recv the uid/gid list. This was introduced in protocol version 15 */
984 if (f != -1 && remote_version >= 15) {
985 recv_uid_list(f, flist);
988 /* if protocol version is >= 17 then recv the io_error flag */
989 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
990 extern int module_id;
991 extern int ignore_errors;
992 if (lp_ignore_errors(module_id) || ignore_errors) {
995 io_error |= read_int(f);
1001 for (i=0;i<flist->count;i++) {
1002 list_file_entry(flist->files[i]);
1008 rprintf(FINFO,"recv_file_list done\n");
1010 stats.flist_size = stats.total_read - start_read;
1011 stats.num_files = flist->count;
1016 out_of_memory("recv_file_list");
1017 return NULL; /* not reached */
1021 int file_compare(struct file_struct **f1,struct file_struct **f2)
1023 if (!(*f1)->basename && !(*f2)->basename) return 0;
1024 if (!(*f1)->basename) return -1;
1025 if (!(*f2)->basename) return 1;
1026 if ((*f1)->dirname == (*f2)->dirname)
1027 return u_strcmp((*f1)->basename, (*f2)->basename);
1028 return u_strcmp(f_name(*f1),f_name(*f2));
1032 int flist_find(struct file_list *flist,struct file_struct *f)
1034 int low=0,high=flist->count-1;
1036 if (flist->count <= 0) return -1;
1038 while (low != high) {
1039 int mid = (low+high)/2;
1040 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1041 if (ret == 0) return flist_up(flist, mid);
1049 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
1050 return flist_up(flist,low);
1058 void free_file(struct file_struct *file)
1061 if (file->basename) free(file->basename);
1062 if (file->link) free(file->link);
1063 if (file->sum) free(file->sum);
1069 * allocate a new file list
1071 struct file_list *flist_new()
1073 struct file_list *flist;
1075 flist = (struct file_list *)malloc(sizeof(flist[0]));
1076 if (!flist) out_of_memory("send_file_list");
1079 flist->malloced = 1000;
1080 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
1082 if (!flist->files) out_of_memory("send_file_list");
1084 flist->string_area = string_area_new(0);
1086 flist->string_area = NULL;
1091 * free up all elements in a flist
1093 void flist_free(struct file_list *flist)
1096 for (i=1;i<flist->count;i++) {
1097 if (!flist->string_area)
1098 free_file(flist->files[i]);
1099 free(flist->files[i]);
1101 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
1103 if (flist->string_area)
1104 string_area_free(flist->string_area);
1105 memset((char *)flist, 0, sizeof(*flist));
1111 * This routine ensures we don't have any duplicate names in our file list.
1112 * duplicate names can cause corruption because of the pipelining
1114 static void clean_flist(struct file_list *flist, int strip_root)
1118 if (!flist || flist->count == 0)
1121 qsort(flist->files,flist->count,
1122 sizeof(flist->files[0]),
1123 (int (*)())file_compare);
1125 for (i=1;i<flist->count;i++) {
1126 if (flist->files[i]->basename &&
1127 flist->files[i-1]->basename &&
1128 strcmp(f_name(flist->files[i]),
1129 f_name(flist->files[i-1])) == 0) {
1130 if (verbose > 1 && !am_server)
1131 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
1132 f_name(flist->files[i-1]),i-1);
1133 /* it's not great that the flist knows the semantics of the
1134 * file memory usage, but i'd rather not add a flag byte
1135 * to that struct. XXX can i use a bit in the flags field? */
1136 if (flist->string_area)
1137 flist->files[i][0] = null_file;
1139 free_file(flist->files[i]);
1143 /* FIXME: There is a bug here when filenames are repeated more
1144 * than once, because we don't handle freed files when doing
1145 * the comparison. */
1148 /* we need to strip off the root directory in the case
1149 of relative paths, but this must be done _after_
1150 the sorting phase */
1151 for (i=0;i<flist->count;i++) {
1152 if (flist->files[i]->dirname &&
1153 flist->files[i]->dirname[0] == '/') {
1154 memmove(&flist->files[i]->dirname[0],
1155 &flist->files[i]->dirname[1],
1156 strlen(flist->files[i]->dirname));
1159 if (flist->files[i]->dirname &&
1160 !flist->files[i]->dirname[0]) {
1161 flist->files[i]->dirname = NULL;
1167 if (verbose <= 3) return;
1169 for (i=0;i<flist->count;i++) {
1170 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%.0f\n",
1172 NS(flist->files[i]->dirname),
1173 NS(flist->files[i]->basename),
1174 (int) flist->files[i]->mode,
1175 (double)flist->files[i]->length);
1181 * return the full filename of a flist entry
1183 char *f_name(struct file_struct *f)
1185 static char names[10][MAXPATHLEN];
1189 if (!f || !f->basename) return NULL;
1194 strlcpy(p, f->dirname, MAXPATHLEN);
1195 strlcat(p, "/", MAXPATHLEN);
1196 strlcat(p, f->basename, MAXPATHLEN);
1198 strlcpy(p, f->basename, MAXPATHLEN);