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 remote_version;
50 static struct exclude_struct **local_exclude_list;
52 static void clean_flist(struct file_list *flist, int strip_root);
54 int link_stat(const char *Path, STRUCT_STAT *Buffer)
58 return do_stat(Path, Buffer);
60 return do_lstat(Path, Buffer);
63 return do_stat(Path, Buffer);
68 This function is used to check if a file should be included/excluded
69 from the list of files based on its name and type etc
71 static int match_file_name(char *fname,STRUCT_STAT *st)
73 if (check_exclude(fname,local_exclude_list,st)) {
75 rprintf(FINFO,"excluding file %s\n",fname);
81 /* used by the one_file_system code */
82 static dev_t filesystem_dev;
84 static void set_filesystem(char *fname)
87 if (link_stat(fname,&st) != 0) return;
88 filesystem_dev = st.st_dev;
92 static int to_wire_mode(mode_t mode)
94 if (S_ISLNK(mode) && (S_IFLNK != 0120000)) {
95 return (mode & ~(_S_IFMT)) | 0120000;
100 static mode_t from_wire_mode(int mode)
102 if ((mode & (_S_IFMT)) == 0120000 && (S_IFLNK != 0120000)) {
103 return (mode & ~(_S_IFMT)) | S_IFLNK;
109 static void send_directory(int f,struct file_list *flist,char *dir);
111 static char *flist_dir;
114 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
117 static time_t last_time;
118 static mode_t last_mode;
119 static dev_t last_rdev;
120 static uid_t last_uid;
121 static gid_t last_gid;
122 static char lastname[MAXPATHLEN];
133 fname = f_name(file);
137 if (file->mode == last_mode) flags |= SAME_MODE;
138 if (file->rdev == last_rdev) flags |= SAME_RDEV;
139 if (file->uid == last_uid) flags |= SAME_UID;
140 if (file->gid == last_gid) flags |= SAME_GID;
141 if (file->modtime == last_time) flags |= SAME_TIME;
143 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
144 l2 = strlen(fname) - l1;
146 if (l1 > 0) flags |= SAME_NAME;
147 if (l2 > 255) flags |= LONG_NAME;
149 /* we must make sure we don't send a zero flags byte or the other
150 end will terminate the flist transfer */
151 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
152 if (flags == 0) flags |= LONG_NAME;
155 if (flags & SAME_NAME)
157 if (flags & LONG_NAME)
161 write_buf(f,fname+l1,l2);
163 write_longint(f,file->length);
164 if (!(flags & SAME_TIME))
165 write_int(f,(int)file->modtime);
166 if (!(flags & SAME_MODE))
167 write_int(f,to_wire_mode(file->mode));
168 if (preserve_uid && !(flags & SAME_UID)) {
170 write_int(f,(int)file->uid);
172 if (preserve_gid && !(flags & SAME_GID)) {
174 write_int(f,(int)file->gid);
176 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
177 write_int(f,(int)file->rdev);
180 if (preserve_links && S_ISLNK(file->mode)) {
181 write_int(f,strlen(file->link));
182 write_buf(f,file->link,strlen(file->link));
186 #if SUPPORT_HARD_LINKS
187 if (preserve_hard_links && S_ISREG(file->mode)) {
188 write_int(f,(int)file->dev);
189 write_int(f,(int)file->inode);
193 if (always_checksum) {
194 write_buf(f,file->sum,csum_length);
197 last_mode = file->mode;
198 last_rdev = file->rdev;
199 last_uid = file->uid;
200 last_gid = file->gid;
201 last_time = file->modtime;
203 strlcpy(lastname,fname,MAXPATHLEN-1);
204 lastname[MAXPATHLEN-1] = 0;
209 static void receive_file_entry(struct file_struct **fptr,
210 unsigned flags,int f)
212 static time_t last_time;
213 static mode_t last_mode;
214 static dev_t last_rdev;
215 static uid_t last_uid;
216 static gid_t last_gid;
217 static char lastname[MAXPATHLEN];
218 char thisname[MAXPATHLEN];
221 struct file_struct *file;
223 if (flags & SAME_NAME)
226 if (flags & LONG_NAME)
231 file = (struct file_struct *)malloc(sizeof(*file));
232 if (!file) out_of_memory("receive_file_entry");
233 memset((char *)file, 0, sizeof(*file));
236 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
238 strlcpy(thisname,lastname,l1);
239 read_sbuf(f,&thisname[l1],l2);
242 strlcpy(lastname,thisname,MAXPATHLEN-1);
243 lastname[MAXPATHLEN-1] = 0;
245 clean_fname(thisname);
247 if ((p = strrchr(thisname,'/'))) {
248 static char *lastdir;
250 if (lastdir && strcmp(thisname, lastdir)==0) {
251 file->dirname = lastdir;
253 file->dirname = strdup(thisname);
254 lastdir = file->dirname;
256 file->basename = strdup(p+1);
258 file->dirname = NULL;
259 file->basename = strdup(thisname);
262 if (!file->basename) out_of_memory("receive_file_entry 1");
266 file->length = read_longint(f);
267 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
268 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
270 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
272 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
273 if (preserve_devices && IS_DEVICE(file->mode))
274 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
276 if (preserve_links && S_ISLNK(file->mode)) {
278 file->link = (char *)malloc(l+1);
279 if (!file->link) out_of_memory("receive_file_entry 2");
280 read_sbuf(f,file->link,l);
283 #if SUPPORT_HARD_LINKS
284 if (preserve_hard_links && S_ISREG(file->mode)) {
285 file->dev = read_int(f);
286 file->inode = read_int(f);
290 if (always_checksum) {
291 file->sum = (char *)malloc(MD4_SUM_LENGTH);
292 if (!file->sum) out_of_memory("md4 sum");
293 read_buf(f,file->sum,csum_length);
296 last_mode = file->mode;
297 last_rdev = file->rdev;
298 last_uid = file->uid;
299 last_gid = file->gid;
300 last_time = file->modtime;
302 if (!preserve_perms) {
303 extern int orig_umask;
304 /* set an appropriate set of permissions based on original
305 permissions and umask. This emulates what GNU cp does */
306 file->mode &= ~orig_umask;
311 /* determine if a file in a different filesstem should be skipped
312 when one_file_system is set. We bascally only want to include
313 the mount points - but they can be hard to find! */
314 static int skip_filesystem(char *fname, STRUCT_STAT *st)
317 char *p = strrchr(fname, '/');
319 /* skip all but directories */
320 if (!S_ISDIR(st->st_mode)) return 1;
322 /* if its not a subdirectory then allow */
326 if (link_stat(fname, &st2)) {
332 return (st2.st_dev != filesystem_dev);
335 static struct file_struct *make_file(char *fname)
337 struct file_struct *file;
339 char sum[SUM_LENGTH];
341 char cleaned_name[MAXPATHLEN];
343 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
344 cleaned_name[MAXPATHLEN-1] = 0;
345 clean_fname(cleaned_name);
346 fname = cleaned_name;
348 memset(sum,0,SUM_LENGTH);
350 if (link_stat(fname,&st) != 0) {
352 rprintf(FERROR,"%s: %s\n",
353 fname,strerror(errno));
357 if (S_ISDIR(st.st_mode) && !recurse) {
358 rprintf(FINFO,"skipping directory %s\n",fname);
362 if (one_file_system && st.st_dev != filesystem_dev) {
363 if (skip_filesystem(fname, &st))
367 if (!match_file_name(fname,&st))
371 rprintf(FINFO,"make_file(%s)\n",fname);
373 file = (struct file_struct *)malloc(sizeof(*file));
374 if (!file) out_of_memory("make_file");
375 memset((char *)file,0,sizeof(*file));
377 if ((p = strrchr(fname,'/'))) {
378 static char *lastdir;
380 if (lastdir && strcmp(fname, lastdir)==0) {
381 file->dirname = lastdir;
383 file->dirname = strdup(fname);
384 lastdir = file->dirname;
386 file->basename = strdup(p+1);
389 file->dirname = NULL;
390 file->basename = strdup(fname);
393 file->modtime = st.st_mtime;
394 file->length = st.st_size;
395 file->mode = st.st_mode;
396 file->uid = st.st_uid;
397 file->gid = st.st_gid;
398 file->dev = st.st_dev;
399 file->inode = st.st_ino;
401 file->rdev = st.st_rdev;
405 if (S_ISLNK(st.st_mode)) {
407 char lnk[MAXPATHLEN];
408 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
410 rprintf(FERROR,"readlink %s : %s\n",
411 fname,strerror(errno));
415 file->link = strdup(lnk);
419 if (always_checksum) {
420 file->sum = (char *)malloc(MD4_SUM_LENGTH);
421 if (!file->sum) out_of_memory("md4 sum");
422 /* drat. we have to provide a null checksum for non-regular
423 files in order to be compatible with earlier versions
425 if (S_ISREG(st.st_mode)) {
426 file_checksum(fname,file->sum,st.st_size);
428 memset(file->sum, 0, MD4_SUM_LENGTH);
433 static char *lastdir;
434 if (lastdir && strcmp(lastdir, flist_dir)==0) {
435 file->basedir = lastdir;
437 file->basedir = strdup(flist_dir);
438 lastdir = file->basedir;
441 file->basedir = NULL;
444 if (!S_ISDIR(st.st_mode))
445 stats.total_size += st.st_size;
452 void send_file_name(int f,struct file_list *flist,char *fname,
453 int recursive, unsigned base_flags)
455 struct file_struct *file;
457 file = make_file(fname);
461 if (flist->count >= flist->malloced) {
462 if (flist->malloced < 1000)
463 flist->malloced += 1000;
465 flist->malloced *= 2;
466 flist->files = (struct file_struct **)realloc(flist->files,
467 sizeof(flist->files[0])*
470 out_of_memory("send_file_name");
473 if (strcmp(file->basename,"")) {
474 flist->files[flist->count++] = file;
475 send_file_entry(file,f,base_flags);
478 if (S_ISDIR(file->mode) && recursive) {
479 struct exclude_struct **last_exclude_list = local_exclude_list;
480 send_directory(f,flist,f_name(file));
481 local_exclude_list = last_exclude_list;
488 static void send_directory(int f,struct file_list *flist,char *dir)
492 char fname[MAXPATHLEN];
499 rprintf(FERROR,"opendir(%s): %s\n",
500 dir,strerror(errno));
504 strlcpy(fname,dir,MAXPATHLEN-1);
506 if (fname[l-1] != '/') {
507 if (l == MAXPATHLEN-1) {
509 rprintf(FERROR,"skipping long-named directory %s\n",fname);
513 strlcat(fname,"/", MAXPATHLEN-1);
516 p = fname + strlen(fname);
518 local_exclude_list = NULL;
521 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
522 strcpy(p,".cvsignore");
523 local_exclude_list = make_exclude_list(fname,NULL,0,0);
526 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
530 for (di=readdir(d); di; di=readdir(d)) {
531 char *dname = d_name(di);
532 if (strcmp(dname,".")==0 ||
533 strcmp(dname,"..")==0)
535 strlcpy(p,dname,MAXPATHLEN-(l+1));
536 send_file_name(f,flist,fname,recurse,0);
539 if (local_exclude_list) {
540 add_exclude_list("!", &local_exclude_list, 0);
548 struct file_list *send_file_list(int f,int argc,char *argv[])
552 char *p,*dir,*olddir;
553 char lastpath[MAXPATHLEN]="";
554 struct file_list *flist;
557 if (verbose && recurse && !am_server && f != -1) {
558 rprintf(FINFO,"building file list ... ");
562 start_write = stats.total_written;
564 flist = (struct file_list *)malloc(sizeof(flist[0]));
565 if (!flist) out_of_memory("send_file_list");
568 flist->malloced = 1000;
569 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
571 if (!flist->files) out_of_memory("send_file_list");
574 io_start_buffering(f);
577 for (i=0;i<argc;i++) {
578 char fname2[MAXPATHLEN];
579 char *fname = fname2;
581 strlcpy(fname,argv[i],MAXPATHLEN-1);
584 if (l != 1 && fname[l-1] == '/') {
585 strlcat(fname,".",MAXPATHLEN-1);
588 if (link_stat(fname,&st) != 0) {
590 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
594 if (S_ISDIR(st.st_mode) && !recurse) {
595 rprintf(FINFO,"skipping directory %s\n",fname);
602 if (!relative_paths) {
603 p = strrchr(fname,'/');
612 } else if (f != -1 && (p=strrchr(fname,'/'))) {
613 /* this ensures we send the intermediate directories,
614 thus getting their permissions right */
616 if (strcmp(lastpath,fname)) {
617 strlcpy(lastpath, fname, sizeof(lastpath)-1);
619 for (p=fname+1; (p=strchr(p,'/')); p++) {
620 int copy_links_saved = copy_links;
623 send_file_name(f, flist, fname, 0, 0);
624 copy_links = copy_links_saved;
636 olddir = push_dir(dir, 1);
640 rprintf(FERROR,"push_dir %s : %s\n",
641 dir,strerror(errno));
649 set_filesystem(fname);
651 if (!recurse || !send_included_file_names(f,flist))
652 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
654 if (olddir != NULL) {
656 if (pop_dir(olddir) != 0) {
657 rprintf(FERROR,"pop_dir %s : %s\n",
658 dir,strerror(errno));
665 send_file_entry(NULL,f,0);
668 if (verbose && recurse && !am_server && f != -1)
669 rprintf(FINFO,"done\n");
671 clean_flist(flist, 0);
673 /* now send the uid/gid list. This was introduced in protocol
675 if (f != -1 && remote_version >= 15) {
679 /* if protocol version is >= 17 then send the io_error flag */
680 if (f != -1 && remote_version >= 17) {
681 write_int(f, io_error);
686 stats.flist_size = stats.total_written - start_write;
687 stats.num_files = flist->count;
691 rprintf(FINFO,"send_file_list done\n");
697 struct file_list *recv_file_list(int f)
699 struct file_list *flist;
703 if (verbose && recurse && !am_server) {
704 rprintf(FINFO,"receiving file list ... ");
708 start_read = stats.total_read;
710 flist = (struct file_list *)malloc(sizeof(flist[0]));
715 flist->malloced=1000;
716 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
722 for (flags=read_byte(f); flags; flags=read_byte(f)) {
723 int i = flist->count;
725 if (i >= flist->malloced) {
726 if (flist->malloced < 1000)
727 flist->malloced += 1000;
729 flist->malloced *= 2;
730 flist->files =(struct file_struct **)realloc(flist->files,
731 sizeof(flist->files[0])*
737 receive_file_entry(&flist->files[i],flags,f);
739 if (S_ISREG(flist->files[i]->mode))
740 stats.total_size += flist->files[i]->length;
745 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
750 rprintf(FINFO,"received %d names\n",flist->count);
752 clean_flist(flist, relative_paths);
754 if (verbose && recurse && !am_server) {
755 rprintf(FINFO,"done\n");
758 /* now recv the uid/gid list. This was introduced in protocol version 15 */
759 if (f != -1 && remote_version >= 15) {
760 recv_uid_list(f, flist);
763 /* if protocol version is >= 17 then recv the io_error flag */
764 if (f != -1 && remote_version >= 17) {
765 io_error |= read_int(f);
769 rprintf(FINFO,"recv_file_list done\n");
771 stats.flist_size = stats.total_read - start_read;
772 stats.num_files = flist->count;
777 out_of_memory("recv_file_list");
778 return NULL; /* not reached */
782 int file_compare(struct file_struct **f1,struct file_struct **f2)
784 if (!(*f1)->basename && !(*f2)->basename) return 0;
785 if (!(*f1)->basename) return -1;
786 if (!(*f2)->basename) return 1;
787 if ((*f1)->dirname == (*f2)->dirname)
788 return u_strcmp((*f1)->basename, (*f2)->basename);
789 return u_strcmp(f_name(*f1),f_name(*f2));
793 int flist_find(struct file_list *flist,struct file_struct *f)
795 int low=0,high=flist->count-1;
797 if (flist->count <= 0) return -1;
799 while (low != high) {
800 int mid = (low+high)/2;
801 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
802 if (ret == 0) return flist_up(flist, mid);
810 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
811 return flist_up(flist,low);
819 static void free_file(struct file_struct *file)
822 if (file->basename) free(file->basename);
823 if (file->link) free(file->link);
824 if (file->sum) free(file->sum);
825 memset((char *)file, 0, sizeof(*file));
830 * free up all elements in a flist
832 void flist_free(struct file_list *flist)
835 for (i=1;i<flist->count;i++) {
836 free_file(flist->files[i]);
837 free(flist->files[i]);
839 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
841 memset((char *)flist, 0, sizeof(*flist));
847 * This routine ensures we don't have any duplicate names in our file list.
848 * duplicate names can cause corruption because of the pipelining
850 static void clean_flist(struct file_list *flist, int strip_root)
854 if (!flist || flist->count == 0)
857 qsort(flist->files,flist->count,
858 sizeof(flist->files[0]),
859 (int (*)())file_compare);
861 for (i=1;i<flist->count;i++) {
862 if (flist->files[i]->basename &&
863 flist->files[i-1]->basename &&
864 strcmp(f_name(flist->files[i]),
865 f_name(flist->files[i-1])) == 0) {
866 if (verbose > 1 && !am_server)
867 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
868 f_name(flist->files[i-1]),i-1);
869 free_file(flist->files[i]);
874 /* we need to strip off the root directory in the case
875 of relative paths, but this must be done _after_
877 for (i=0;i<flist->count;i++) {
878 if (flist->files[i]->dirname &&
879 flist->files[i]->dirname[0] == '/') {
880 memmove(&flist->files[i]->dirname[0],
881 &flist->files[i]->dirname[1],
882 strlen(flist->files[i]->dirname));
885 if (flist->files[i]->dirname &&
886 !flist->files[i]->dirname[0]) {
887 flist->files[i]->dirname = NULL;
893 if (verbose <= 3) return;
895 for (i=0;i<flist->count;i++) {
896 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%d\n",
898 flist->files[i]->dirname,
899 flist->files[i]->basename,
900 flist->files[i]->mode,
901 flist->files[i]->length);
907 * return the full filename of a flist entry
909 char *f_name(struct file_struct *f)
911 static char names[10][MAXPATHLEN];
915 if (!f || !f->basename) return NULL;
920 strlcpy(p, f->dirname, MAXPATHLEN-1);
921 strlcat(p, "/", MAXPATHLEN-1);
922 strlcat(p, f->basename, MAXPATHLEN-1);
924 strlcpy(p, f->basename, MAXPATHLEN-1);