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 int csum_length;
28 extern int always_checksum;
29 extern int64 total_size;
31 extern int cvs_exclude;
35 extern int one_file_system;
36 extern int make_backups;
37 extern int preserve_links;
38 extern int preserve_hard_links;
39 extern int preserve_perms;
40 extern int preserve_devices;
41 extern int preserve_uid;
42 extern int preserve_gid;
43 extern int preserve_times;
44 extern int relative_paths;
45 extern int copy_links;
46 extern int remote_version;
49 static struct exclude_struct **local_exclude_list;
51 int link_stat(const char *Path, STRUCT_STAT *Buffer)
55 return do_stat(Path, Buffer);
57 return do_lstat(Path, Buffer);
60 return do_stat(Path, Buffer);
65 This function is used to check if a file should be included/excluded
66 from the list of files based on its name and type etc
68 static int match_file_name(char *fname,STRUCT_STAT *st)
70 if (check_exclude(fname,local_exclude_list,st)) {
72 rprintf(FINFO,"excluding file %s\n",fname);
78 /* used by the one_file_system code */
79 static dev_t filesystem_dev;
81 static void set_filesystem(char *fname)
84 if (link_stat(fname,&st) != 0) return;
85 filesystem_dev = st.st_dev;
89 static void send_directory(int f,struct file_list *flist,char *dir);
91 static char *flist_dir;
93 static void clean_fname(char *name)
104 if ((p=strstr(name,"/./"))) {
112 if ((p=strstr(name,"//"))) {
120 if (strncmp(p=name,"./",2) == 0) {
128 if (l > 1 && p[l-1] == '/') {
137 void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
140 static time_t last_time;
141 static mode_t last_mode;
142 static dev_t last_rdev;
143 static uid_t last_uid;
144 static gid_t last_gid;
145 static char lastname[MAXPATHLEN];
156 fname = f_name(file);
160 if (file->mode == last_mode) flags |= SAME_MODE;
161 if (file->rdev == last_rdev) flags |= SAME_RDEV;
162 if (file->uid == last_uid) flags |= SAME_UID;
163 if (file->gid == last_gid) flags |= SAME_GID;
164 if (file->modtime == last_time) flags |= SAME_TIME;
166 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
167 l2 = strlen(fname) - l1;
169 if (l1 > 0) flags |= SAME_NAME;
170 if (l2 > 255) flags |= LONG_NAME;
172 /* we must make sure we don't send a zero flags byte or the other
173 end will terminate the flist transfer */
174 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
175 if (flags == 0) flags |= LONG_NAME;
178 if (flags & SAME_NAME)
180 if (flags & LONG_NAME)
184 write_buf(f,fname+l1,l2);
186 write_longint(f,file->length);
187 if (!(flags & SAME_TIME))
188 write_int(f,(int)file->modtime);
189 if (!(flags & SAME_MODE))
190 write_int(f,(int)file->mode);
191 if (preserve_uid && !(flags & SAME_UID)) {
193 write_int(f,(int)file->uid);
195 if (preserve_gid && !(flags & SAME_GID)) {
197 write_int(f,(int)file->gid);
199 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
200 write_int(f,(int)file->rdev);
203 if (preserve_links && S_ISLNK(file->mode)) {
204 write_int(f,strlen(file->link));
205 write_buf(f,file->link,strlen(file->link));
209 #if SUPPORT_HARD_LINKS
210 if (preserve_hard_links && S_ISREG(file->mode)) {
211 write_int(f,(int)file->dev);
212 write_int(f,(int)file->inode);
216 if (always_checksum) {
217 write_buf(f,file->sum,csum_length);
220 last_mode = file->mode;
221 last_rdev = file->rdev;
222 last_uid = file->uid;
223 last_gid = file->gid;
224 last_time = file->modtime;
226 strlcpy(lastname,fname,MAXPATHLEN-1);
227 lastname[MAXPATHLEN-1] = 0;
232 static void receive_file_entry(struct file_struct **fptr,
233 unsigned flags,int f)
235 static time_t last_time;
236 static mode_t last_mode;
237 static dev_t last_rdev;
238 static uid_t last_uid;
239 static gid_t last_gid;
240 static char lastname[MAXPATHLEN];
241 char thisname[MAXPATHLEN];
244 struct file_struct *file;
246 if (flags & SAME_NAME)
249 if (flags & LONG_NAME)
254 file = (struct file_struct *)malloc(sizeof(*file));
255 if (!file) out_of_memory("receive_file_entry");
256 bzero((char *)file,sizeof(*file));
259 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
261 strlcpy(thisname,lastname,l1);
262 read_sbuf(f,&thisname[l1],l2);
265 strlcpy(lastname,thisname,MAXPATHLEN-1);
266 lastname[MAXPATHLEN-1] = 0;
268 clean_fname(thisname);
270 if (relative_paths && thisname[0] == '/') {
271 /* strip / off absolute paths in destination */
272 memmove(thisname, thisname+1, strlen(thisname));
273 if (!thisname[0]) strcpy(thisname,".");
276 if ((p = strrchr(thisname,'/'))) {
277 static char *lastdir;
279 if (lastdir && strcmp(thisname, lastdir)==0) {
280 file->dirname = lastdir;
282 file->dirname = strdup(thisname);
283 lastdir = file->dirname;
285 file->basename = strdup(p+1);
287 file->dirname = NULL;
288 file->basename = strdup(thisname);
291 if (!file->basename) out_of_memory("receive_file_entry 1");
295 file->length = read_longint(f);
296 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
297 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
299 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
301 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
302 if (preserve_devices && IS_DEVICE(file->mode))
303 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
305 if (preserve_links && S_ISLNK(file->mode)) {
307 file->link = (char *)malloc(l+1);
308 if (!file->link) out_of_memory("receive_file_entry 2");
309 read_sbuf(f,file->link,l);
312 #if SUPPORT_HARD_LINKS
313 if (preserve_hard_links && S_ISREG(file->mode)) {
314 file->dev = read_int(f);
315 file->inode = read_int(f);
319 if (always_checksum) {
320 file->sum = (char *)malloc(MD4_SUM_LENGTH);
321 if (!file->sum) out_of_memory("md4 sum");
322 read_buf(f,file->sum,csum_length);
325 last_mode = file->mode;
326 last_rdev = file->rdev;
327 last_uid = file->uid;
328 last_gid = file->gid;
329 last_time = file->modtime;
333 /* determine if a file in a different filesstem should be skipped
334 when one_file_system is set. We bascally only want to include
335 the mount points - but they can be hard to find! */
336 static int skip_filesystem(char *fname, STRUCT_STAT *st)
339 char *p = strrchr(fname, '/');
341 /* skip all but directories */
342 if (!S_ISDIR(st->st_mode)) return 1;
344 /* if its not a subdirectory then allow */
348 if (link_stat(fname, &st2)) {
354 return (st2.st_dev != filesystem_dev);
357 static struct file_struct *make_file(char *fname)
359 struct file_struct *file;
361 char sum[SUM_LENGTH];
363 char cleaned_name[MAXPATHLEN];
365 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
366 cleaned_name[MAXPATHLEN-1] = 0;
367 clean_fname(cleaned_name);
368 fname = cleaned_name;
370 bzero(sum,SUM_LENGTH);
372 if (link_stat(fname,&st) != 0) {
374 rprintf(FERROR,"%s: %s\n",
375 fname,strerror(errno));
379 if (S_ISDIR(st.st_mode) && !recurse) {
380 rprintf(FINFO,"skipping directory %s\n",fname);
384 if (one_file_system && st.st_dev != filesystem_dev) {
385 if (skip_filesystem(fname, &st))
389 if (!match_file_name(fname,&st))
393 rprintf(FINFO,"make_file(%s)\n",fname);
395 file = (struct file_struct *)malloc(sizeof(*file));
396 if (!file) out_of_memory("make_file");
397 bzero((char *)file,sizeof(*file));
399 if ((p = strrchr(fname,'/'))) {
400 static char *lastdir;
402 if (lastdir && strcmp(fname, lastdir)==0) {
403 file->dirname = lastdir;
405 file->dirname = strdup(fname);
406 lastdir = file->dirname;
408 file->basename = strdup(p+1);
411 file->dirname = NULL;
412 file->basename = strdup(fname);
415 file->modtime = st.st_mtime;
416 file->length = st.st_size;
417 file->mode = st.st_mode;
418 file->uid = st.st_uid;
419 file->gid = st.st_gid;
420 file->dev = st.st_dev;
421 file->inode = st.st_ino;
423 file->rdev = st.st_rdev;
427 if (S_ISLNK(st.st_mode)) {
429 char lnk[MAXPATHLEN];
430 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
432 rprintf(FERROR,"readlink %s : %s\n",
433 fname,strerror(errno));
437 file->link = strdup(lnk);
441 if (always_checksum) {
442 file->sum = (char *)malloc(MD4_SUM_LENGTH);
443 if (!file->sum) out_of_memory("md4 sum");
444 /* drat. we have to provide a null checksum for non-regular
445 files in order to be compatible with earlier versions
447 if (S_ISREG(st.st_mode)) {
448 file_checksum(fname,file->sum,st.st_size);
450 memset(file->sum, 0, MD4_SUM_LENGTH);
455 static char *lastdir;
456 if (lastdir && strcmp(lastdir, flist_dir)==0) {
457 file->basedir = lastdir;
459 file->basedir = strdup(flist_dir);
460 lastdir = file->basedir;
463 file->basedir = NULL;
466 if (!S_ISDIR(st.st_mode))
467 total_size += st.st_size;
474 static void send_file_name(int f,struct file_list *flist,char *fname,
475 int recursive, unsigned base_flags)
477 struct file_struct *file;
479 file = make_file(fname);
483 if (flist->count >= flist->malloced) {
484 if (flist->malloced < 1000)
485 flist->malloced += 1000;
487 flist->malloced *= 2;
488 flist->files = (struct file_struct **)realloc(flist->files,
489 sizeof(flist->files[0])*
492 out_of_memory("send_file_name");
495 if (strcmp(file->basename,"")) {
496 flist->files[flist->count++] = file;
497 send_file_entry(file,f,base_flags);
500 if (S_ISDIR(file->mode) && recursive) {
501 struct exclude_struct **last_exclude_list = local_exclude_list;
502 send_directory(f,flist,f_name(file));
503 local_exclude_list = last_exclude_list;
510 static void send_directory(int f,struct file_list *flist,char *dir)
514 char fname[MAXPATHLEN];
521 rprintf(FERROR,"opendir(%s): %s\n",
522 dir,strerror(errno));
526 strlcpy(fname,dir,MAXPATHLEN-1);
528 if (fname[l-1] != '/') {
529 if (l == MAXPATHLEN-1) {
531 rprintf(FERROR,"skipping long-named directory %s\n",fname);
535 strlcat(fname,"/", MAXPATHLEN-1);
538 p = fname + strlen(fname);
541 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
542 strcpy(p,".cvsignore");
543 local_exclude_list = make_exclude_list(fname,NULL,0,0);
546 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
550 for (di=readdir(d); di; di=readdir(d)) {
551 char *dname = d_name(di);
552 if (strcmp(dname,".")==0 ||
553 strcmp(dname,"..")==0)
555 strlcpy(p,dname,MAXPATHLEN-(l+1));
556 send_file_name(f,flist,fname,recurse,0);
564 struct file_list *send_file_list(int f,int argc,char *argv[])
569 char dbuf[MAXPATHLEN];
570 char lastpath[MAXPATHLEN]="";
571 struct file_list *flist;
573 if (verbose && recurse && !am_server && f != -1) {
574 rprintf(FINFO,"building file list ... ");
578 flist = (struct file_list *)malloc(sizeof(flist[0]));
579 if (!flist) out_of_memory("send_file_list");
582 flist->malloced = 1000;
583 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
585 if (!flist->files) out_of_memory("send_file_list");
588 io_start_buffering(f);
591 for (i=0;i<argc;i++) {
592 char fname2[MAXPATHLEN];
593 char *fname = fname2;
595 strlcpy(fname,argv[i],MAXPATHLEN-1);
598 if (l != 1 && fname[l-1] == '/') {
599 strlcat(fname,".",MAXPATHLEN-1);
602 if (link_stat(fname,&st) != 0) {
604 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
608 if (S_ISDIR(st.st_mode) && !recurse) {
609 rprintf(FINFO,"skipping directory %s\n",fname);
615 if (!relative_paths) {
616 p = strrchr(fname,'/');
625 } else if (f != -1 && (p=strrchr(fname,'/'))) {
626 /* this ensures we send the intermediate directories,
627 thus getting their permissions right */
629 if (strcmp(lastpath,fname)) {
630 strlcpy(lastpath, fname, sizeof(lastpath)-1);
632 for (p=fname+1; (p=strchr(p,'/')); p++) {
633 int copy_links_saved = copy_links;
636 send_file_name(f, flist, fname, 0, 0);
637 copy_links = copy_links_saved;
649 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
650 rprintf(FERROR,"getwd : %s\n",strerror(errno));
653 if (chdir(dir) != 0) {
655 rprintf(FERROR,"chdir %s : %s\n",
656 dir,strerror(errno));
661 set_filesystem(fname);
662 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
664 if (chdir(dbuf) != 0) {
665 rprintf(FERROR,"chdir %s : %s\n",
666 dbuf,strerror(errno));
673 set_filesystem(fname);
674 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
678 send_file_entry(NULL,f,0);
681 if (verbose && recurse && !am_server && f != -1)
682 rprintf(FINFO,"done\n");
686 /* now send the uid/gid list. This was introduced in protocol
688 if (f != -1 && remote_version >= 15) {
692 /* if protocol version is >= 17 then send the io_error flag */
693 if (f != -1 && remote_version >= 17) {
694 write_int(f, io_error);
702 rprintf(FINFO,"send_file_list done\n");
708 struct file_list *recv_file_list(int f)
710 struct file_list *flist;
713 if (verbose && recurse && !am_server) {
714 rprintf(FINFO,"receiving file list ... ");
718 flist = (struct file_list *)malloc(sizeof(flist[0]));
723 flist->malloced=1000;
724 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
730 for (flags=read_byte(f); flags; flags=read_byte(f)) {
731 int i = flist->count;
733 if (i >= flist->malloced) {
734 if (flist->malloced < 1000)
735 flist->malloced += 1000;
737 flist->malloced *= 2;
738 flist->files =(struct file_struct **)realloc(flist->files,
739 sizeof(flist->files[0])*
745 receive_file_entry(&flist->files[i],flags,f);
747 if (S_ISREG(flist->files[i]->mode))
748 total_size += flist->files[i]->length;
753 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
758 rprintf(FINFO,"received %d names\n",flist->count);
762 if (verbose && recurse && !am_server) {
763 rprintf(FINFO,"done\n");
766 /* now recv the uid/gid list. This was introduced in protocol version 15 */
767 if (f != -1 && remote_version >= 15) {
768 recv_uid_list(f, flist);
771 /* if protocol version is >= 17 then recv the io_error flag */
772 if (f != -1 && remote_version >= 17) {
773 io_error |= read_int(f);
777 rprintf(FINFO,"recv_file_list done\n");
782 out_of_memory("recv_file_list");
783 return NULL; /* not reached */
787 int file_compare(struct file_struct **f1,struct file_struct **f2)
789 if (!(*f1)->basename && !(*f2)->basename) return 0;
790 if (!(*f1)->basename) return -1;
791 if (!(*f2)->basename) return 1;
792 if ((*f1)->dirname == (*f2)->dirname)
793 return strcmp((*f1)->basename, (*f2)->basename);
794 return strcmp(f_name(*f1),f_name(*f2));
798 int flist_find(struct file_list *flist,struct file_struct *f)
800 int low=0,high=flist->count-1;
802 if (flist->count <= 0) return -1;
804 while (low != high) {
805 int mid = (low+high)/2;
806 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
807 if (ret == 0) return flist_up(flist, mid);
815 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
816 return flist_up(flist,low);
824 static void free_file(struct file_struct *file)
827 if (file->basename) free(file->basename);
828 if (file->link) free(file->link);
829 if (file->sum) free(file->sum);
830 bzero((char *)file, sizeof(*file));
835 * free up all elements in a flist
837 void flist_free(struct file_list *flist)
840 for (i=1;i<flist->count;i++) {
841 free_file(flist->files[i]);
842 free(flist->files[i]);
844 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
846 bzero((char *)flist, sizeof(*flist));
852 * This routine ensures we don't have any duplicate names in our file list.
853 * duplicate names can cause corruption because of the pipelining
855 void clean_flist(struct file_list *flist)
859 if (!flist || flist->count == 0)
862 qsort(flist->files,flist->count,
863 sizeof(flist->files[0]),
864 (int (*)())file_compare);
866 for (i=1;i<flist->count;i++) {
867 if (flist->files[i]->basename &&
868 flist->files[i-1]->basename &&
869 strcmp(f_name(flist->files[i]),
870 f_name(flist->files[i-1])) == 0) {
871 if (verbose > 1 && !am_server)
872 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
873 f_name(flist->files[i-1]),i-1);
874 free_file(flist->files[i]);
881 * return the full filename of a flist entry
883 char *f_name(struct file_struct *f)
885 static char names[10][MAXPATHLEN];
889 if (!f || !f->basename) return NULL;
894 slprintf(p, MAXPATHLEN-1, "%s/%s", f->dirname, f->basename);
896 strlcpy(p, f->basename, MAXPATHLEN-1);