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;
26 extern int csum_length;
30 extern int always_checksum;
32 extern int cvs_exclude;
36 extern int one_file_system;
37 extern int make_backups;
38 extern int preserve_links;
39 extern int preserve_hard_links;
40 extern int preserve_perms;
41 extern int preserve_devices;
42 extern int preserve_uid;
43 extern int preserve_gid;
44 extern int preserve_times;
45 extern int relative_paths;
46 extern int copy_links;
47 extern int copy_unsafe_links;
48 extern int remote_version;
51 static char topsrcname[MAXPATHLEN];
53 static struct exclude_struct **local_exclude_list;
55 static void clean_flist(struct file_list *flist, int strip_root);
58 static void list_file_entry(struct file_struct *f)
60 char perms[11] = "----------";
61 char *perm_map = "rwxrwxrwx";
65 /* this can happen if duplicate names were removed */
69 if (f->mode & (1<<i)) perms[9-i] = perm_map[8-i];
71 if (S_ISLNK(f->mode)) perms[0] = 'l';
72 if (S_ISDIR(f->mode)) perms[0] = 'd';
73 if (S_ISBLK(f->mode)) perms[0] = 'b';
74 if (S_ISCHR(f->mode)) perms[0] = 'c';
75 if (S_ISSOCK(f->mode)) perms[0] = 's';
76 if (S_ISFIFO(f->mode)) perms[0] = 'p';
78 if (preserve_links && S_ISLNK(f->mode)) {
79 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
81 (double)f->length, timestring(f->modtime),
84 rprintf(FINFO,"%s %11.0f %s %s\n",
86 (double)f->length, timestring(f->modtime), f_name(f));
91 int readlink_stat(const char *Path, STRUCT_STAT *Buffer, char *Linkbuf)
95 return do_stat(Path, Buffer);
97 if (do_lstat(Path, Buffer) == -1) {
100 if (S_ISLNK(Buffer->st_mode)) {
102 if ((l = readlink(Path,Linkbuf,MAXPATHLEN-1)) == -1) {
106 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
107 unsafe_symlink(Linkbuf, topsrcname)) {
108 return do_stat(Path, Buffer);
113 return do_stat(Path, Buffer);
117 int link_stat(const char *Path, STRUCT_STAT *Buffer)
121 return do_stat(Path, Buffer);
123 return do_lstat(Path, Buffer);
126 return do_stat(Path, Buffer);
131 This function is used to check if a file should be included/excluded
132 from the list of files based on its name and type etc
134 static int match_file_name(char *fname,STRUCT_STAT *st)
136 if (check_exclude(fname,local_exclude_list,st)) {
138 rprintf(FINFO,"excluding file %s\n",fname);
144 /* used by the one_file_system code */
145 static dev_t filesystem_dev;
147 static void set_filesystem(char *fname)
150 if (link_stat(fname,&st) != 0) return;
151 filesystem_dev = st.st_dev;
155 static int to_wire_mode(mode_t mode)
157 if (S_ISLNK(mode) && (S_IFLNK != 0120000)) {
158 return (mode & ~(_S_IFMT)) | 0120000;
163 static mode_t from_wire_mode(int mode)
165 if ((mode & (_S_IFMT)) == 0120000 && (S_IFLNK != 0120000)) {
166 return (mode & ~(_S_IFMT)) | S_IFLNK;
172 static void send_directory(int f,struct file_list *flist,char *dir);
174 static char *flist_dir;
177 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
180 static time_t last_time;
181 static mode_t last_mode;
182 static dev_t last_rdev;
183 static uid_t last_uid;
184 static gid_t last_gid;
185 static char lastname[MAXPATHLEN];
196 fname = f_name(file);
200 if (file->mode == last_mode) flags |= SAME_MODE;
201 if (file->rdev == last_rdev) flags |= SAME_RDEV;
202 if (file->uid == last_uid) flags |= SAME_UID;
203 if (file->gid == last_gid) flags |= SAME_GID;
204 if (file->modtime == last_time) flags |= SAME_TIME;
206 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
207 l2 = strlen(fname) - l1;
209 if (l1 > 0) flags |= SAME_NAME;
210 if (l2 > 255) flags |= LONG_NAME;
212 /* we must make sure we don't send a zero flags byte or the other
213 end will terminate the flist transfer */
214 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
215 if (flags == 0) flags |= LONG_NAME;
218 if (flags & SAME_NAME)
220 if (flags & LONG_NAME)
224 write_buf(f,fname+l1,l2);
226 write_longint(f,file->length);
227 if (!(flags & SAME_TIME))
228 write_int(f,(int)file->modtime);
229 if (!(flags & SAME_MODE))
230 write_int(f,to_wire_mode(file->mode));
231 if (preserve_uid && !(flags & SAME_UID)) {
233 write_int(f,(int)file->uid);
235 if (preserve_gid && !(flags & SAME_GID)) {
237 write_int(f,(int)file->gid);
239 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
240 write_int(f,(int)file->rdev);
243 if (preserve_links && S_ISLNK(file->mode)) {
244 write_int(f,strlen(file->link));
245 write_buf(f,file->link,strlen(file->link));
249 #if SUPPORT_HARD_LINKS
250 if (preserve_hard_links && S_ISREG(file->mode)) {
251 write_int(f,(int)file->dev);
252 write_int(f,(int)file->inode);
256 if (always_checksum) {
257 write_buf(f,file->sum,csum_length);
260 last_mode = file->mode;
261 last_rdev = file->rdev;
262 last_uid = file->uid;
263 last_gid = file->gid;
264 last_time = file->modtime;
266 strlcpy(lastname,fname,MAXPATHLEN);
267 lastname[MAXPATHLEN-1] = 0;
272 static void receive_file_entry(struct file_struct **fptr,
273 unsigned flags,int f)
275 static time_t last_time;
276 static mode_t last_mode;
277 static dev_t last_rdev;
278 static uid_t last_uid;
279 static gid_t last_gid;
280 static char lastname[MAXPATHLEN];
281 char thisname[MAXPATHLEN];
284 struct file_struct *file;
286 if (flags & SAME_NAME)
289 if (flags & LONG_NAME)
294 file = (struct file_struct *)malloc(sizeof(*file));
295 if (!file) out_of_memory("receive_file_entry");
296 memset((char *)file, 0, sizeof(*file));
299 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
301 strlcpy(thisname,lastname,l1+1);
302 read_sbuf(f,&thisname[l1],l2);
305 strlcpy(lastname,thisname,MAXPATHLEN);
306 lastname[MAXPATHLEN-1] = 0;
308 clean_fname(thisname);
310 if ((p = strrchr(thisname,'/'))) {
311 static char *lastdir;
313 if (lastdir && strcmp(thisname, lastdir)==0) {
314 file->dirname = lastdir;
316 file->dirname = strdup(thisname);
317 lastdir = file->dirname;
319 file->basename = strdup(p+1);
321 file->dirname = NULL;
322 file->basename = strdup(thisname);
325 if (!file->basename) out_of_memory("receive_file_entry 1");
329 file->length = read_longint(f);
330 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
331 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
333 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
335 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
336 if (preserve_devices && IS_DEVICE(file->mode))
337 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
339 if (preserve_links && S_ISLNK(file->mode)) {
341 file->link = (char *)malloc(l+1);
342 if (!file->link) out_of_memory("receive_file_entry 2");
343 read_sbuf(f,file->link,l);
346 #if SUPPORT_HARD_LINKS
347 if (preserve_hard_links && S_ISREG(file->mode)) {
348 file->dev = read_int(f);
349 file->inode = read_int(f);
353 if (always_checksum) {
354 file->sum = (char *)malloc(MD4_SUM_LENGTH);
355 if (!file->sum) out_of_memory("md4 sum");
356 read_buf(f,file->sum,csum_length);
359 last_mode = file->mode;
360 last_rdev = file->rdev;
361 last_uid = file->uid;
362 last_gid = file->gid;
363 last_time = file->modtime;
365 if (!preserve_perms) {
366 extern int orig_umask;
367 /* set an appropriate set of permissions based on original
368 permissions and umask. This emulates what GNU cp does */
369 file->mode &= ~orig_umask;
374 /* determine if a file in a different filesstem should be skipped
375 when one_file_system is set. We bascally only want to include
376 the mount points - but they can be hard to find! */
377 static int skip_filesystem(char *fname, STRUCT_STAT *st)
380 char *p = strrchr(fname, '/');
382 /* skip all but directories */
383 if (!S_ISDIR(st->st_mode)) return 1;
385 /* if its not a subdirectory then allow */
389 if (link_stat(fname, &st2)) {
395 return (st2.st_dev != filesystem_dev);
398 static struct file_struct *make_file(char *fname)
400 struct file_struct *file;
402 char sum[SUM_LENGTH];
404 char cleaned_name[MAXPATHLEN];
405 char linkbuf[MAXPATHLEN];
407 strlcpy(cleaned_name, fname, MAXPATHLEN);
408 cleaned_name[MAXPATHLEN-1] = 0;
409 clean_fname(cleaned_name);
410 fname = cleaned_name;
412 memset(sum,0,SUM_LENGTH);
414 if (readlink_stat(fname,&st,linkbuf) != 0) {
416 rprintf(FERROR,"%s: %s\n",
417 fname,strerror(errno));
421 if (S_ISDIR(st.st_mode) && !recurse) {
422 rprintf(FINFO,"skipping directory %s\n",fname);
426 if (one_file_system && st.st_dev != filesystem_dev) {
427 if (skip_filesystem(fname, &st))
431 if (!match_file_name(fname,&st))
435 rprintf(FINFO,"make_file(%s)\n",fname);
437 file = (struct file_struct *)malloc(sizeof(*file));
438 if (!file) out_of_memory("make_file");
439 memset((char *)file,0,sizeof(*file));
441 if ((p = strrchr(fname,'/'))) {
442 static char *lastdir;
444 if (lastdir && strcmp(fname, lastdir)==0) {
445 file->dirname = lastdir;
447 file->dirname = strdup(fname);
448 lastdir = file->dirname;
450 file->basename = strdup(p+1);
453 file->dirname = NULL;
454 file->basename = strdup(fname);
457 file->modtime = st.st_mtime;
458 file->length = st.st_size;
459 file->mode = st.st_mode;
460 file->uid = st.st_uid;
461 file->gid = st.st_gid;
462 file->dev = st.st_dev;
463 file->inode = st.st_ino;
465 file->rdev = st.st_rdev;
469 if (S_ISLNK(st.st_mode)) {
470 file->link = strdup(linkbuf);
474 if (always_checksum) {
475 file->sum = (char *)malloc(MD4_SUM_LENGTH);
476 if (!file->sum) out_of_memory("md4 sum");
477 /* drat. we have to provide a null checksum for non-regular
478 files in order to be compatible with earlier versions
480 if (S_ISREG(st.st_mode)) {
481 file_checksum(fname,file->sum,st.st_size);
483 memset(file->sum, 0, MD4_SUM_LENGTH);
488 static char *lastdir;
489 if (lastdir && strcmp(lastdir, flist_dir)==0) {
490 file->basedir = lastdir;
492 file->basedir = strdup(flist_dir);
493 lastdir = file->basedir;
496 file->basedir = NULL;
499 if (!S_ISDIR(st.st_mode))
500 stats.total_size += st.st_size;
507 void send_file_name(int f,struct file_list *flist,char *fname,
508 int recursive, unsigned base_flags)
510 struct file_struct *file;
512 file = make_file(fname);
516 if (flist->count >= flist->malloced) {
517 if (flist->malloced < 1000)
518 flist->malloced += 1000;
520 flist->malloced *= 2;
521 flist->files = (struct file_struct **)realloc(flist->files,
522 sizeof(flist->files[0])*
525 out_of_memory("send_file_name");
528 if (strcmp(file->basename,"")) {
529 flist->files[flist->count++] = file;
530 send_file_entry(file,f,base_flags);
533 if (S_ISDIR(file->mode) && recursive) {
534 struct exclude_struct **last_exclude_list = local_exclude_list;
535 send_directory(f,flist,f_name(file));
536 local_exclude_list = last_exclude_list;
543 static void send_directory(int f,struct file_list *flist,char *dir)
547 char fname[MAXPATHLEN];
554 rprintf(FERROR,"opendir(%s): %s\n",
555 dir,strerror(errno));
559 strlcpy(fname,dir,MAXPATHLEN);
561 if (fname[l-1] != '/') {
562 if (l == MAXPATHLEN-1) {
564 rprintf(FERROR,"skipping long-named directory %s\n",fname);
568 strlcat(fname,"/", MAXPATHLEN);
571 p = fname + strlen(fname);
573 local_exclude_list = NULL;
576 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
577 strcpy(p,".cvsignore");
578 local_exclude_list = make_exclude_list(fname,NULL,0,0);
581 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
585 for (di=readdir(d); di; di=readdir(d)) {
586 char *dname = d_name(di);
587 if (strcmp(dname,".")==0 ||
588 strcmp(dname,"..")==0)
590 strlcpy(p,dname,MAXPATHLEN-l);
591 send_file_name(f,flist,fname,recurse,0);
594 if (local_exclude_list) {
595 add_exclude_list("!", &local_exclude_list, 0);
603 struct file_list *send_file_list(int f,int argc,char *argv[])
607 char *p,*dir,*olddir;
608 char lastpath[MAXPATHLEN]="";
609 struct file_list *flist;
612 if (verbose && recurse && !am_server && f != -1) {
613 rprintf(FINFO,"building file list ... ");
617 start_write = stats.total_written;
619 flist = (struct file_list *)malloc(sizeof(flist[0]));
620 if (!flist) out_of_memory("send_file_list");
623 flist->malloced = 1000;
624 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
626 if (!flist->files) out_of_memory("send_file_list");
629 io_start_buffering(f);
632 for (i=0;i<argc;i++) {
633 char *fname = topsrcname;
635 strlcpy(fname,argv[i],MAXPATHLEN);
638 if (l != 1 && fname[l-1] == '/') {
639 strlcat(fname,".",MAXPATHLEN);
642 if (link_stat(fname,&st) != 0) {
644 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
648 if (S_ISDIR(st.st_mode) && !recurse) {
649 rprintf(FINFO,"skipping directory %s\n",fname);
656 if (!relative_paths) {
657 p = strrchr(fname,'/');
666 } else if (f != -1 && (p=strrchr(fname,'/'))) {
667 /* this ensures we send the intermediate directories,
668 thus getting their permissions right */
670 if (strcmp(lastpath,fname)) {
671 strlcpy(lastpath, fname, sizeof(lastpath));
673 for (p=fname+1; (p=strchr(p,'/')); p++) {
674 int copy_links_saved = copy_links;
676 copy_links = copy_unsafe_links;
677 send_file_name(f, flist, fname, 0, 0);
678 copy_links = copy_links_saved;
690 olddir = push_dir(dir, 1);
694 rprintf(FERROR,"push_dir %s : %s\n",
695 dir,strerror(errno));
703 set_filesystem(fname);
705 if (!recurse || !send_included_file_names(f,flist))
706 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
708 if (olddir != NULL) {
710 if (pop_dir(olddir) != 0) {
711 rprintf(FERROR,"pop_dir %s : %s\n",
712 dir,strerror(errno));
713 exit_cleanup(RERR_FILESELECT);
718 topsrcname[0] = '\0';
721 send_file_entry(NULL,f,0);
724 if (verbose && recurse && !am_server && f != -1)
725 rprintf(FINFO,"done\n");
727 clean_flist(flist, 0);
729 /* now send the uid/gid list. This was introduced in protocol
731 if (f != -1 && remote_version >= 15) {
735 /* if protocol version is >= 17 then send the io_error flag */
736 if (f != -1 && remote_version >= 17) {
737 write_int(f, io_error);
742 stats.flist_size = stats.total_written - start_write;
743 stats.num_files = flist->count;
747 rprintf(FINFO,"send_file_list done\n");
753 struct file_list *recv_file_list(int f)
755 struct file_list *flist;
758 extern int list_only;
760 if (verbose && recurse && !am_server) {
761 rprintf(FINFO,"receiving file list ... ");
765 start_read = stats.total_read;
767 flist = (struct file_list *)malloc(sizeof(flist[0]));
772 flist->malloced=1000;
773 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
779 for (flags=read_byte(f); flags; flags=read_byte(f)) {
780 int i = flist->count;
782 if (i >= flist->malloced) {
783 if (flist->malloced < 1000)
784 flist->malloced += 1000;
786 flist->malloced *= 2;
787 flist->files =(struct file_struct **)realloc(flist->files,
788 sizeof(flist->files[0])*
794 receive_file_entry(&flist->files[i],flags,f);
796 if (S_ISREG(flist->files[i]->mode))
797 stats.total_size += flist->files[i]->length;
802 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
807 rprintf(FINFO,"received %d names\n",flist->count);
809 clean_flist(flist, relative_paths);
811 if (verbose && recurse && !am_server) {
812 rprintf(FINFO,"done\n");
815 /* now recv the uid/gid list. This was introduced in protocol version 15 */
816 if (f != -1 && remote_version >= 15) {
817 recv_uid_list(f, flist);
820 /* if protocol version is >= 17 then recv the io_error flag */
821 if (f != -1 && remote_version >= 17) {
822 io_error |= read_int(f);
827 for (i=0;i<flist->count;i++) {
828 list_file_entry(flist->files[i]);
834 rprintf(FINFO,"recv_file_list done\n");
836 stats.flist_size = stats.total_read - start_read;
837 stats.num_files = flist->count;
842 out_of_memory("recv_file_list");
843 return NULL; /* not reached */
847 int file_compare(struct file_struct **f1,struct file_struct **f2)
849 if (!(*f1)->basename && !(*f2)->basename) return 0;
850 if (!(*f1)->basename) return -1;
851 if (!(*f2)->basename) return 1;
852 if ((*f1)->dirname == (*f2)->dirname)
853 return u_strcmp((*f1)->basename, (*f2)->basename);
854 return u_strcmp(f_name(*f1),f_name(*f2));
858 int flist_find(struct file_list *flist,struct file_struct *f)
860 int low=0,high=flist->count-1;
862 if (flist->count <= 0) return -1;
864 while (low != high) {
865 int mid = (low+high)/2;
866 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
867 if (ret == 0) return flist_up(flist, mid);
875 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
876 return flist_up(flist,low);
884 static void free_file(struct file_struct *file)
887 if (file->basename) free(file->basename);
888 if (file->link) free(file->link);
889 if (file->sum) free(file->sum);
890 memset((char *)file, 0, sizeof(*file));
895 * free up all elements in a flist
897 void flist_free(struct file_list *flist)
900 for (i=1;i<flist->count;i++) {
901 free_file(flist->files[i]);
902 free(flist->files[i]);
904 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
906 memset((char *)flist, 0, sizeof(*flist));
912 * This routine ensures we don't have any duplicate names in our file list.
913 * duplicate names can cause corruption because of the pipelining
915 static void clean_flist(struct file_list *flist, int strip_root)
919 if (!flist || flist->count == 0)
922 qsort(flist->files,flist->count,
923 sizeof(flist->files[0]),
924 (int (*)())file_compare);
926 for (i=1;i<flist->count;i++) {
927 if (flist->files[i]->basename &&
928 flist->files[i-1]->basename &&
929 strcmp(f_name(flist->files[i]),
930 f_name(flist->files[i-1])) == 0) {
931 if (verbose > 1 && !am_server)
932 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
933 f_name(flist->files[i-1]),i-1);
934 free_file(flist->files[i]);
939 /* we need to strip off the root directory in the case
940 of relative paths, but this must be done _after_
942 for (i=0;i<flist->count;i++) {
943 if (flist->files[i]->dirname &&
944 flist->files[i]->dirname[0] == '/') {
945 memmove(&flist->files[i]->dirname[0],
946 &flist->files[i]->dirname[1],
947 strlen(flist->files[i]->dirname));
950 if (flist->files[i]->dirname &&
951 !flist->files[i]->dirname[0]) {
952 flist->files[i]->dirname = NULL;
958 if (verbose <= 3) return;
960 for (i=0;i<flist->count;i++) {
961 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%d\n",
963 NS(flist->files[i]->dirname),
964 NS(flist->files[i]->basename),
965 flist->files[i]->mode,
966 (int)flist->files[i]->length);
972 * return the full filename of a flist entry
974 char *f_name(struct file_struct *f)
976 static char names[10][MAXPATHLEN];
980 if (!f || !f->basename) return NULL;
985 strlcpy(p, f->dirname, MAXPATHLEN);
986 strlcat(p, "/", MAXPATHLEN);
987 strlcat(p, f->basename, MAXPATHLEN);
989 strlcpy(p, f->basename, MAXPATHLEN);