2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* generate and receive file lists */
24 extern struct stats stats;
28 extern int always_checksum;
30 extern int cvs_exclude;
34 extern int one_file_system;
35 extern int make_backups;
36 extern int preserve_links;
37 extern int preserve_hard_links;
38 extern int preserve_perms;
39 extern int preserve_devices;
40 extern int preserve_uid;
41 extern int preserve_gid;
42 extern int preserve_times;
43 extern int relative_paths;
44 extern int copy_links;
45 extern int copy_unsafe_links;
46 extern int remote_version;
49 static char topsrcname[MAXPATHLEN];
51 static struct exclude_struct **local_exclude_list;
53 static void clean_flist(struct file_list *flist, int strip_root);
56 static void list_file_entry(struct file_struct *f)
58 char perms[11] = "----------";
59 char *perm_map = "rwxrwxrwx";
63 /* this can happen if duplicate names were removed */
67 if (f->mode & (1<<i)) perms[9-i] = perm_map[8-i];
69 if (S_ISLNK(f->mode)) perms[0] = 'l';
70 if (S_ISDIR(f->mode)) perms[0] = 'd';
71 if (S_ISBLK(f->mode)) perms[0] = 'b';
72 if (S_ISCHR(f->mode)) perms[0] = 'c';
73 if (S_ISSOCK(f->mode)) perms[0] = 's';
74 if (S_ISFIFO(f->mode)) perms[0] = 'p';
76 if (preserve_links && S_ISLNK(f->mode)) {
77 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
79 (double)f->length, timestring(f->modtime),
82 rprintf(FINFO,"%s %11.0f %s %s\n",
84 (double)f->length, timestring(f->modtime), f_name(f));
89 int readlink_stat(const char *Path, STRUCT_STAT *Buffer, char *Linkbuf)
93 return do_stat(Path, Buffer);
95 if (do_lstat(Path, Buffer) == -1) {
98 if (S_ISLNK(Buffer->st_mode)) {
100 if ((l = readlink(Path,Linkbuf,MAXPATHLEN-1)) == -1) {
104 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
105 unsafe_symlink(Linkbuf, topsrcname)) {
106 return do_stat(Path, Buffer);
111 return do_stat(Path, Buffer);
115 int link_stat(const char *Path, STRUCT_STAT *Buffer)
119 return do_stat(Path, Buffer);
121 return do_lstat(Path, Buffer);
124 return do_stat(Path, Buffer);
129 This function is used to check if a file should be included/excluded
130 from the list of files based on its name and type etc
132 static int match_file_name(char *fname,STRUCT_STAT *st)
134 if (check_exclude(fname,local_exclude_list,st)) {
136 rprintf(FINFO,"excluding file %s\n",fname);
142 /* used by the one_file_system code */
143 static dev_t filesystem_dev;
145 static void set_filesystem(char *fname)
148 if (link_stat(fname,&st) != 0) return;
149 filesystem_dev = st.st_dev;
153 static int to_wire_mode(mode_t mode)
155 if (S_ISLNK(mode) && (S_IFLNK != 0120000)) {
156 return (mode & ~(_S_IFMT)) | 0120000;
161 static mode_t from_wire_mode(int mode)
163 if ((mode & (_S_IFMT)) == 0120000 && (S_IFLNK != 0120000)) {
164 return (mode & ~(_S_IFMT)) | S_IFLNK;
170 static void send_directory(int f,struct file_list *flist,char *dir);
172 static char *flist_dir;
175 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
178 static time_t last_time;
179 static mode_t last_mode;
180 static dev_t last_rdev;
181 static uid_t last_uid;
182 static gid_t last_gid;
183 static char lastname[MAXPATHLEN];
194 fname = f_name(file);
198 if (file->mode == last_mode) flags |= SAME_MODE;
199 if (file->rdev == last_rdev) flags |= SAME_RDEV;
200 if (file->uid == last_uid) flags |= SAME_UID;
201 if (file->gid == last_gid) flags |= SAME_GID;
202 if (file->modtime == last_time) flags |= SAME_TIME;
204 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
205 l2 = strlen(fname) - l1;
207 if (l1 > 0) flags |= SAME_NAME;
208 if (l2 > 255) flags |= LONG_NAME;
210 /* we must make sure we don't send a zero flags byte or the other
211 end will terminate the flist transfer */
212 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
213 if (flags == 0) flags |= LONG_NAME;
216 if (flags & SAME_NAME)
218 if (flags & LONG_NAME)
222 write_buf(f,fname+l1,l2);
224 write_longint(f,file->length);
225 if (!(flags & SAME_TIME))
226 write_int(f,(int)file->modtime);
227 if (!(flags & SAME_MODE))
228 write_int(f,to_wire_mode(file->mode));
229 if (preserve_uid && !(flags & SAME_UID)) {
231 write_int(f,(int)file->uid);
233 if (preserve_gid && !(flags & SAME_GID)) {
235 write_int(f,(int)file->gid);
237 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
238 write_int(f,(int)file->rdev);
241 if (preserve_links && S_ISLNK(file->mode)) {
242 write_int(f,strlen(file->link));
243 write_buf(f,file->link,strlen(file->link));
247 #if SUPPORT_HARD_LINKS
248 if (preserve_hard_links && S_ISREG(file->mode)) {
249 write_int(f,(int)file->dev);
250 write_int(f,(int)file->inode);
254 if (always_checksum) {
255 if (remote_version < 21) {
256 write_buf(f,file->sum,2);
258 write_buf(f,file->sum,MD4_SUM_LENGTH);
262 last_mode = file->mode;
263 last_rdev = file->rdev;
264 last_uid = file->uid;
265 last_gid = file->gid;
266 last_time = file->modtime;
268 strlcpy(lastname,fname,MAXPATHLEN);
269 lastname[MAXPATHLEN-1] = 0;
274 static void receive_file_entry(struct file_struct **fptr,
275 unsigned flags,int f)
277 static time_t last_time;
278 static mode_t last_mode;
279 static dev_t last_rdev;
280 static uid_t last_uid;
281 static gid_t last_gid;
282 static char lastname[MAXPATHLEN];
283 char thisname[MAXPATHLEN];
286 struct file_struct *file;
288 if (flags & SAME_NAME)
291 if (flags & LONG_NAME)
296 file = (struct file_struct *)malloc(sizeof(*file));
297 if (!file) out_of_memory("receive_file_entry");
298 memset((char *)file, 0, sizeof(*file));
301 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
303 strlcpy(thisname,lastname,l1+1);
304 read_sbuf(f,&thisname[l1],l2);
307 strlcpy(lastname,thisname,MAXPATHLEN);
308 lastname[MAXPATHLEN-1] = 0;
310 clean_fname(thisname);
312 if ((p = strrchr(thisname,'/'))) {
313 static char *lastdir;
315 if (lastdir && strcmp(thisname, lastdir)==0) {
316 file->dirname = lastdir;
318 file->dirname = strdup(thisname);
319 lastdir = file->dirname;
321 file->basename = strdup(p+1);
323 file->dirname = NULL;
324 file->basename = strdup(thisname);
327 if (!file->basename) out_of_memory("receive_file_entry 1");
331 file->length = read_longint(f);
332 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
333 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
335 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
337 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
338 if (preserve_devices && IS_DEVICE(file->mode))
339 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
341 if (preserve_links && S_ISLNK(file->mode)) {
343 file->link = (char *)malloc(l+1);
344 if (!file->link) out_of_memory("receive_file_entry 2");
345 read_sbuf(f,file->link,l);
348 #if SUPPORT_HARD_LINKS
349 if (preserve_hard_links && S_ISREG(file->mode)) {
350 file->dev = read_int(f);
351 file->inode = read_int(f);
355 if (always_checksum) {
356 file->sum = (char *)malloc(MD4_SUM_LENGTH);
357 if (!file->sum) out_of_memory("md4 sum");
358 if (remote_version < 21) {
359 read_buf(f,file->sum,2);
361 read_buf(f,file->sum,MD4_SUM_LENGTH);
365 last_mode = file->mode;
366 last_rdev = file->rdev;
367 last_uid = file->uid;
368 last_gid = file->gid;
369 last_time = file->modtime;
371 if (!preserve_perms) {
372 extern int orig_umask;
373 /* set an appropriate set of permissions based on original
374 permissions and umask. This emulates what GNU cp does */
375 file->mode &= ~orig_umask;
380 /* determine if a file in a different filesstem should be skipped
381 when one_file_system is set. We bascally only want to include
382 the mount points - but they can be hard to find! */
383 static int skip_filesystem(char *fname, STRUCT_STAT *st)
386 char *p = strrchr(fname, '/');
388 /* skip all but directories */
389 if (!S_ISDIR(st->st_mode)) return 1;
391 /* if its not a subdirectory then allow */
395 if (link_stat(fname, &st2)) {
401 return (st2.st_dev != filesystem_dev);
404 static struct file_struct *make_file(int f, char *fname)
406 struct file_struct *file;
408 char sum[SUM_LENGTH];
410 char cleaned_name[MAXPATHLEN];
411 char linkbuf[MAXPATHLEN];
412 extern int delete_excluded;
414 strlcpy(cleaned_name, fname, MAXPATHLEN);
415 cleaned_name[MAXPATHLEN-1] = 0;
416 clean_fname(cleaned_name);
417 fname = cleaned_name;
419 memset(sum,0,SUM_LENGTH);
421 if (readlink_stat(fname,&st,linkbuf) != 0) {
423 rprintf(FERROR,"%s: %s\n",
424 fname,strerror(errno));
428 if (S_ISDIR(st.st_mode) && !recurse) {
429 rprintf(FINFO,"skipping directory %s\n",fname);
433 if (one_file_system && st.st_dev != filesystem_dev) {
434 if (skip_filesystem(fname, &st))
438 /* f is set to -1 when calculating deletion file list */
439 if (((f != -1) || !delete_excluded) && !match_file_name(fname,&st))
443 rprintf(FINFO,"make_file(%d,%s)\n",f,fname);
445 file = (struct file_struct *)malloc(sizeof(*file));
446 if (!file) out_of_memory("make_file");
447 memset((char *)file,0,sizeof(*file));
449 if ((p = strrchr(fname,'/'))) {
450 static char *lastdir;
452 if (lastdir && strcmp(fname, lastdir)==0) {
453 file->dirname = lastdir;
455 file->dirname = strdup(fname);
456 lastdir = file->dirname;
458 file->basename = strdup(p+1);
461 file->dirname = NULL;
462 file->basename = strdup(fname);
465 file->modtime = st.st_mtime;
466 file->length = st.st_size;
467 file->mode = st.st_mode;
468 file->uid = st.st_uid;
469 file->gid = st.st_gid;
470 file->dev = st.st_dev;
471 file->inode = st.st_ino;
473 file->rdev = st.st_rdev;
477 if (S_ISLNK(st.st_mode)) {
478 file->link = strdup(linkbuf);
482 if (always_checksum) {
483 file->sum = (char *)malloc(MD4_SUM_LENGTH);
484 if (!file->sum) out_of_memory("md4 sum");
485 /* drat. we have to provide a null checksum for non-regular
486 files in order to be compatible with earlier versions
488 if (S_ISREG(st.st_mode)) {
489 file_checksum(fname,file->sum,st.st_size);
491 memset(file->sum, 0, MD4_SUM_LENGTH);
496 static char *lastdir;
497 if (lastdir && strcmp(lastdir, flist_dir)==0) {
498 file->basedir = lastdir;
500 file->basedir = strdup(flist_dir);
501 lastdir = file->basedir;
504 file->basedir = NULL;
507 if (!S_ISDIR(st.st_mode))
508 stats.total_size += st.st_size;
515 void send_file_name(int f,struct file_list *flist,char *fname,
516 int recursive, unsigned base_flags)
518 struct file_struct *file;
520 file = make_file(f,fname);
524 if (flist->count >= flist->malloced) {
525 if (flist->malloced < 1000)
526 flist->malloced += 1000;
528 flist->malloced *= 2;
529 flist->files = (struct file_struct **)realloc(flist->files,
530 sizeof(flist->files[0])*
533 out_of_memory("send_file_name");
536 if (strcmp(file->basename,"")) {
537 flist->files[flist->count++] = file;
538 send_file_entry(file,f,base_flags);
541 if (S_ISDIR(file->mode) && recursive) {
542 struct exclude_struct **last_exclude_list = local_exclude_list;
543 send_directory(f,flist,f_name(file));
544 local_exclude_list = last_exclude_list;
551 static void send_directory(int f,struct file_list *flist,char *dir)
555 char fname[MAXPATHLEN];
562 rprintf(FERROR,"opendir(%s): %s\n",
563 dir,strerror(errno));
567 strlcpy(fname,dir,MAXPATHLEN);
569 if (fname[l-1] != '/') {
570 if (l == MAXPATHLEN-1) {
572 rprintf(FERROR,"skipping long-named directory %s\n",fname);
576 strlcat(fname,"/", MAXPATHLEN);
579 p = fname + strlen(fname);
581 local_exclude_list = NULL;
584 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
585 strcpy(p,".cvsignore");
586 local_exclude_list = make_exclude_list(fname,NULL,0,0);
589 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
593 for (di=readdir(d); di; di=readdir(d)) {
594 char *dname = d_name(di);
595 if (strcmp(dname,".")==0 ||
596 strcmp(dname,"..")==0)
598 strlcpy(p,dname,MAXPATHLEN-l);
599 send_file_name(f,flist,fname,recurse,0);
602 if (local_exclude_list) {
603 add_exclude_list("!", &local_exclude_list, 0);
611 struct file_list *send_file_list(int f,int argc,char *argv[])
615 char *p,*dir,*olddir;
616 char lastpath[MAXPATHLEN]="";
617 struct file_list *flist;
620 if (verbose && recurse && !am_server && f != -1) {
621 rprintf(FINFO,"building file list ... ");
625 start_write = stats.total_written;
627 flist = (struct file_list *)malloc(sizeof(flist[0]));
628 if (!flist) out_of_memory("send_file_list");
631 flist->malloced = 1000;
632 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
634 if (!flist->files) out_of_memory("send_file_list");
637 io_start_buffering(f);
640 for (i=0;i<argc;i++) {
641 char *fname = topsrcname;
643 strlcpy(fname,argv[i],MAXPATHLEN);
646 if (l != 1 && fname[l-1] == '/') {
647 if ((l == 2) && (fname[0] == '.')) {
648 /* Turn ./ into just . rather than ./.
649 This was put in to avoid a problem with
650 rsync -aR --delete from ./
651 The send_file_name() below of ./ was
652 mysteriously preventing deletes */
655 strlcat(fname,".",MAXPATHLEN);
659 if (link_stat(fname,&st) != 0) {
661 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
665 if (S_ISDIR(st.st_mode) && !recurse) {
666 rprintf(FINFO,"skipping directory %s\n",fname);
673 if (!relative_paths) {
674 p = strrchr(fname,'/');
683 } else if (f != -1 && (p=strrchr(fname,'/'))) {
684 /* this ensures we send the intermediate directories,
685 thus getting their permissions right */
687 if (strcmp(lastpath,fname)) {
688 strlcpy(lastpath, fname, sizeof(lastpath));
690 for (p=fname+1; (p=strchr(p,'/')); p++) {
691 int copy_links_saved = copy_links;
692 int recurse_saved = recurse;
694 copy_links = copy_unsafe_links;
695 /* set recurse to 1 to prevent make_file
696 from ignoring directory, but still
697 turn off the recursive parameter to
700 send_file_name(f, flist, fname, 0, 0);
701 copy_links = copy_links_saved;
702 recurse = recurse_saved;
714 olddir = push_dir(dir, 1);
718 rprintf(FERROR,"push_dir %s : %s\n",
719 dir,strerror(errno));
727 set_filesystem(fname);
729 if (!recurse || !send_included_file_names(f,flist))
730 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
732 if (olddir != NULL) {
734 if (pop_dir(olddir) != 0) {
735 rprintf(FERROR,"pop_dir %s : %s\n",
736 dir,strerror(errno));
737 exit_cleanup(RERR_FILESELECT);
742 topsrcname[0] = '\0';
745 send_file_entry(NULL,f,0);
748 if (verbose && recurse && !am_server && f != -1)
749 rprintf(FINFO,"done\n");
751 clean_flist(flist, 0);
753 /* now send the uid/gid list. This was introduced in protocol
755 if (f != -1 && remote_version >= 15) {
759 /* if protocol version is >= 17 then send the io_error flag */
760 if (f != -1 && remote_version >= 17) {
761 write_int(f, io_error);
766 stats.flist_size = stats.total_written - start_write;
767 stats.num_files = flist->count;
771 rprintf(FINFO,"send_file_list done\n");
777 struct file_list *recv_file_list(int f)
779 struct file_list *flist;
782 extern int list_only;
784 if (verbose && recurse && !am_server) {
785 rprintf(FINFO,"receiving file list ... ");
789 start_read = stats.total_read;
791 flist = (struct file_list *)malloc(sizeof(flist[0]));
796 flist->malloced=1000;
797 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
803 for (flags=read_byte(f); flags; flags=read_byte(f)) {
804 int i = flist->count;
806 if (i >= flist->malloced) {
807 if (flist->malloced < 1000)
808 flist->malloced += 1000;
810 flist->malloced *= 2;
811 flist->files =(struct file_struct **)realloc(flist->files,
812 sizeof(flist->files[0])*
818 receive_file_entry(&flist->files[i],flags,f);
820 if (S_ISREG(flist->files[i]->mode))
821 stats.total_size += flist->files[i]->length;
826 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
831 rprintf(FINFO,"received %d names\n",flist->count);
833 clean_flist(flist, relative_paths);
835 if (verbose && recurse && !am_server) {
836 rprintf(FINFO,"done\n");
839 /* now recv the uid/gid list. This was introduced in protocol version 15 */
840 if (f != -1 && remote_version >= 15) {
841 recv_uid_list(f, flist);
844 /* if protocol version is >= 17 then recv the io_error flag */
845 if (f != -1 && remote_version >= 17) {
846 io_error |= read_int(f);
851 for (i=0;i<flist->count;i++) {
852 list_file_entry(flist->files[i]);
858 rprintf(FINFO,"recv_file_list done\n");
860 stats.flist_size = stats.total_read - start_read;
861 stats.num_files = flist->count;
866 out_of_memory("recv_file_list");
867 return NULL; /* not reached */
871 int file_compare(struct file_struct **f1,struct file_struct **f2)
873 if (!(*f1)->basename && !(*f2)->basename) return 0;
874 if (!(*f1)->basename) return -1;
875 if (!(*f2)->basename) return 1;
876 if ((*f1)->dirname == (*f2)->dirname)
877 return u_strcmp((*f1)->basename, (*f2)->basename);
878 return u_strcmp(f_name(*f1),f_name(*f2));
882 int flist_find(struct file_list *flist,struct file_struct *f)
884 int low=0,high=flist->count-1;
886 if (flist->count <= 0) return -1;
888 while (low != high) {
889 int mid = (low+high)/2;
890 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
891 if (ret == 0) return flist_up(flist, mid);
899 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
900 return flist_up(flist,low);
908 static void free_file(struct file_struct *file)
911 if (file->basename) free(file->basename);
912 if (file->link) free(file->link);
913 if (file->sum) free(file->sum);
914 memset((char *)file, 0, sizeof(*file));
919 * free up all elements in a flist
921 void flist_free(struct file_list *flist)
924 for (i=1;i<flist->count;i++) {
925 free_file(flist->files[i]);
926 free(flist->files[i]);
928 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
930 memset((char *)flist, 0, sizeof(*flist));
936 * This routine ensures we don't have any duplicate names in our file list.
937 * duplicate names can cause corruption because of the pipelining
939 static void clean_flist(struct file_list *flist, int strip_root)
943 if (!flist || flist->count == 0)
946 qsort(flist->files,flist->count,
947 sizeof(flist->files[0]),
948 (int (*)())file_compare);
950 for (i=1;i<flist->count;i++) {
951 if (flist->files[i]->basename &&
952 flist->files[i-1]->basename &&
953 strcmp(f_name(flist->files[i]),
954 f_name(flist->files[i-1])) == 0) {
955 if (verbose > 1 && !am_server)
956 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
957 f_name(flist->files[i-1]),i-1);
958 free_file(flist->files[i]);
963 /* we need to strip off the root directory in the case
964 of relative paths, but this must be done _after_
966 for (i=0;i<flist->count;i++) {
967 if (flist->files[i]->dirname &&
968 flist->files[i]->dirname[0] == '/') {
969 memmove(&flist->files[i]->dirname[0],
970 &flist->files[i]->dirname[1],
971 strlen(flist->files[i]->dirname));
974 if (flist->files[i]->dirname &&
975 !flist->files[i]->dirname[0]) {
976 flist->files[i]->dirname = NULL;
982 if (verbose <= 3) return;
984 for (i=0;i<flist->count;i++) {
985 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%d\n",
987 NS(flist->files[i]->dirname),
988 NS(flist->files[i]->basename),
989 flist->files[i]->mode,
990 (int)flist->files[i]->length);
996 * return the full filename of a flist entry
998 char *f_name(struct file_struct *f)
1000 static char names[10][MAXPATHLEN];
1004 if (!f || !f->basename) return NULL;
1009 strlcpy(p, f->dirname, MAXPATHLEN);
1010 strlcat(p, "/", MAXPATHLEN);
1011 strlcat(p, f->basename, MAXPATHLEN);
1013 strlcpy(p, f->basename, MAXPATHLEN);