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 void send_directory(int f,struct file_list *flist,char *dir);
94 static char *flist_dir;
97 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
100 static time_t last_time;
101 static mode_t last_mode;
102 static dev_t last_rdev;
103 static uid_t last_uid;
104 static gid_t last_gid;
105 static char lastname[MAXPATHLEN];
116 fname = f_name(file);
120 if (file->mode == last_mode) flags |= SAME_MODE;
121 if (file->rdev == last_rdev) flags |= SAME_RDEV;
122 if (file->uid == last_uid) flags |= SAME_UID;
123 if (file->gid == last_gid) flags |= SAME_GID;
124 if (file->modtime == last_time) flags |= SAME_TIME;
126 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
127 l2 = strlen(fname) - l1;
129 if (l1 > 0) flags |= SAME_NAME;
130 if (l2 > 255) flags |= LONG_NAME;
132 /* we must make sure we don't send a zero flags byte or the other
133 end will terminate the flist transfer */
134 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
135 if (flags == 0) flags |= LONG_NAME;
138 if (flags & SAME_NAME)
140 if (flags & LONG_NAME)
144 write_buf(f,fname+l1,l2);
146 write_longint(f,file->length);
147 if (!(flags & SAME_TIME))
148 write_int(f,(int)file->modtime);
149 if (!(flags & SAME_MODE))
150 write_int(f,(int)file->mode);
151 if (preserve_uid && !(flags & SAME_UID)) {
153 write_int(f,(int)file->uid);
155 if (preserve_gid && !(flags & SAME_GID)) {
157 write_int(f,(int)file->gid);
159 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
160 write_int(f,(int)file->rdev);
163 if (preserve_links && S_ISLNK(file->mode)) {
164 write_int(f,strlen(file->link));
165 write_buf(f,file->link,strlen(file->link));
169 #if SUPPORT_HARD_LINKS
170 if (preserve_hard_links && S_ISREG(file->mode)) {
171 write_int(f,(int)file->dev);
172 write_int(f,(int)file->inode);
176 if (always_checksum) {
177 write_buf(f,file->sum,csum_length);
180 last_mode = file->mode;
181 last_rdev = file->rdev;
182 last_uid = file->uid;
183 last_gid = file->gid;
184 last_time = file->modtime;
186 strlcpy(lastname,fname,MAXPATHLEN-1);
187 lastname[MAXPATHLEN-1] = 0;
192 static void receive_file_entry(struct file_struct **fptr,
193 unsigned flags,int f)
195 static time_t last_time;
196 static mode_t last_mode;
197 static dev_t last_rdev;
198 static uid_t last_uid;
199 static gid_t last_gid;
200 static char lastname[MAXPATHLEN];
201 char thisname[MAXPATHLEN];
204 struct file_struct *file;
206 if (flags & SAME_NAME)
209 if (flags & LONG_NAME)
214 file = (struct file_struct *)malloc(sizeof(*file));
215 if (!file) out_of_memory("receive_file_entry");
216 memset((char *)file, 0, sizeof(*file));
219 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
221 strlcpy(thisname,lastname,l1);
222 read_sbuf(f,&thisname[l1],l2);
225 strlcpy(lastname,thisname,MAXPATHLEN-1);
226 lastname[MAXPATHLEN-1] = 0;
228 clean_fname(thisname);
230 if ((p = strrchr(thisname,'/'))) {
231 static char *lastdir;
233 if (lastdir && strcmp(thisname, lastdir)==0) {
234 file->dirname = lastdir;
236 file->dirname = strdup(thisname);
237 lastdir = file->dirname;
239 file->basename = strdup(p+1);
241 file->dirname = NULL;
242 file->basename = strdup(thisname);
245 if (!file->basename) out_of_memory("receive_file_entry 1");
249 file->length = read_longint(f);
250 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
251 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
253 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
255 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
256 if (preserve_devices && IS_DEVICE(file->mode))
257 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
259 if (preserve_links && S_ISLNK(file->mode)) {
261 file->link = (char *)malloc(l+1);
262 if (!file->link) out_of_memory("receive_file_entry 2");
263 read_sbuf(f,file->link,l);
266 #if SUPPORT_HARD_LINKS
267 if (preserve_hard_links && S_ISREG(file->mode)) {
268 file->dev = read_int(f);
269 file->inode = read_int(f);
273 if (always_checksum) {
274 file->sum = (char *)malloc(MD4_SUM_LENGTH);
275 if (!file->sum) out_of_memory("md4 sum");
276 read_buf(f,file->sum,csum_length);
279 last_mode = file->mode;
280 last_rdev = file->rdev;
281 last_uid = file->uid;
282 last_gid = file->gid;
283 last_time = file->modtime;
285 if (!preserve_perms) {
286 extern int orig_umask;
287 /* set an appropriate set of permissions based on original
288 permissions and umask. This emulates what GNU cp does */
289 file->mode &= ~orig_umask;
294 /* determine if a file in a different filesstem should be skipped
295 when one_file_system is set. We bascally only want to include
296 the mount points - but they can be hard to find! */
297 static int skip_filesystem(char *fname, STRUCT_STAT *st)
300 char *p = strrchr(fname, '/');
302 /* skip all but directories */
303 if (!S_ISDIR(st->st_mode)) return 1;
305 /* if its not a subdirectory then allow */
309 if (link_stat(fname, &st2)) {
315 return (st2.st_dev != filesystem_dev);
318 static struct file_struct *make_file(char *fname)
320 struct file_struct *file;
322 char sum[SUM_LENGTH];
324 char cleaned_name[MAXPATHLEN];
326 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
327 cleaned_name[MAXPATHLEN-1] = 0;
328 clean_fname(cleaned_name);
329 fname = cleaned_name;
331 memset(sum,0,SUM_LENGTH);
333 if (link_stat(fname,&st) != 0) {
335 rprintf(FERROR,"%s: %s\n",
336 fname,strerror(errno));
340 if (S_ISDIR(st.st_mode) && !recurse) {
341 rprintf(FINFO,"skipping directory %s\n",fname);
345 if (one_file_system && st.st_dev != filesystem_dev) {
346 if (skip_filesystem(fname, &st))
350 if (!match_file_name(fname,&st))
354 rprintf(FINFO,"make_file(%s)\n",fname);
356 file = (struct file_struct *)malloc(sizeof(*file));
357 if (!file) out_of_memory("make_file");
358 memset((char *)file,0,sizeof(*file));
360 if ((p = strrchr(fname,'/'))) {
361 static char *lastdir;
363 if (lastdir && strcmp(fname, lastdir)==0) {
364 file->dirname = lastdir;
366 file->dirname = strdup(fname);
367 lastdir = file->dirname;
369 file->basename = strdup(p+1);
372 file->dirname = NULL;
373 file->basename = strdup(fname);
376 file->modtime = st.st_mtime;
377 file->length = st.st_size;
378 file->mode = st.st_mode;
379 file->uid = st.st_uid;
380 file->gid = st.st_gid;
381 file->dev = st.st_dev;
382 file->inode = st.st_ino;
384 file->rdev = st.st_rdev;
388 if (S_ISLNK(st.st_mode)) {
390 char lnk[MAXPATHLEN];
391 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
393 rprintf(FERROR,"readlink %s : %s\n",
394 fname,strerror(errno));
398 file->link = strdup(lnk);
402 if (always_checksum) {
403 file->sum = (char *)malloc(MD4_SUM_LENGTH);
404 if (!file->sum) out_of_memory("md4 sum");
405 /* drat. we have to provide a null checksum for non-regular
406 files in order to be compatible with earlier versions
408 if (S_ISREG(st.st_mode)) {
409 file_checksum(fname,file->sum,st.st_size);
411 memset(file->sum, 0, MD4_SUM_LENGTH);
416 static char *lastdir;
417 if (lastdir && strcmp(lastdir, flist_dir)==0) {
418 file->basedir = lastdir;
420 file->basedir = strdup(flist_dir);
421 lastdir = file->basedir;
424 file->basedir = NULL;
427 if (!S_ISDIR(st.st_mode))
428 stats.total_size += st.st_size;
435 static void send_file_name(int f,struct file_list *flist,char *fname,
436 int recursive, unsigned base_flags)
438 struct file_struct *file;
440 file = make_file(fname);
444 if (flist->count >= flist->malloced) {
445 if (flist->malloced < 1000)
446 flist->malloced += 1000;
448 flist->malloced *= 2;
449 flist->files = (struct file_struct **)realloc(flist->files,
450 sizeof(flist->files[0])*
453 out_of_memory("send_file_name");
456 if (strcmp(file->basename,"")) {
457 flist->files[flist->count++] = file;
458 send_file_entry(file,f,base_flags);
461 if (S_ISDIR(file->mode) && recursive) {
462 struct exclude_struct **last_exclude_list = local_exclude_list;
463 send_directory(f,flist,f_name(file));
464 local_exclude_list = last_exclude_list;
471 static void send_directory(int f,struct file_list *flist,char *dir)
475 char fname[MAXPATHLEN];
482 rprintf(FERROR,"opendir(%s): %s\n",
483 dir,strerror(errno));
487 strlcpy(fname,dir,MAXPATHLEN-1);
489 if (fname[l-1] != '/') {
490 if (l == MAXPATHLEN-1) {
492 rprintf(FERROR,"skipping long-named directory %s\n",fname);
496 strlcat(fname,"/", MAXPATHLEN-1);
499 p = fname + strlen(fname);
501 local_exclude_list = NULL;
504 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
505 strcpy(p,".cvsignore");
506 local_exclude_list = make_exclude_list(fname,NULL,0,0);
509 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
513 for (di=readdir(d); di; di=readdir(d)) {
514 char *dname = d_name(di);
515 if (strcmp(dname,".")==0 ||
516 strcmp(dname,"..")==0)
518 strlcpy(p,dname,MAXPATHLEN-(l+1));
519 send_file_name(f,flist,fname,recurse,0);
522 if (local_exclude_list) {
523 add_exclude_list("!", &local_exclude_list, 0);
531 struct file_list *send_file_list(int f,int argc,char *argv[])
536 char lastpath[MAXPATHLEN]="";
537 struct file_list *flist;
540 if (verbose && recurse && !am_server && f != -1) {
541 rprintf(FINFO,"building file list ... ");
545 start_write = stats.total_written;
547 flist = (struct file_list *)malloc(sizeof(flist[0]));
548 if (!flist) out_of_memory("send_file_list");
551 flist->malloced = 1000;
552 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
554 if (!flist->files) out_of_memory("send_file_list");
557 io_start_buffering(f);
560 for (i=0;i<argc;i++) {
561 char fname2[MAXPATHLEN];
562 char *fname = fname2;
564 strlcpy(fname,argv[i],MAXPATHLEN-1);
567 if (l != 1 && fname[l-1] == '/') {
568 strlcat(fname,".",MAXPATHLEN-1);
571 if (link_stat(fname,&st) != 0) {
573 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
577 if (S_ISDIR(st.st_mode) && !recurse) {
578 rprintf(FINFO,"skipping directory %s\n",fname);
584 if (!relative_paths) {
585 p = strrchr(fname,'/');
594 } else if (f != -1 && (p=strrchr(fname,'/'))) {
595 /* this ensures we send the intermediate directories,
596 thus getting their permissions right */
598 if (strcmp(lastpath,fname)) {
599 strlcpy(lastpath, fname, sizeof(lastpath)-1);
601 for (p=fname+1; (p=strchr(p,'/')); p++) {
602 int copy_links_saved = copy_links;
605 send_file_name(f, flist, fname, 0, 0);
606 copy_links = copy_links_saved;
618 char *olddir = push_dir(dir, 1);
622 rprintf(FERROR,"push_dir %s : %s\n",
623 dir,strerror(errno));
629 set_filesystem(fname);
630 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
632 if (pop_dir(olddir) != 0) {
633 rprintf(FERROR,"pop_dir %s : %s\n",
634 dir,strerror(errno));
641 set_filesystem(fname);
642 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
646 send_file_entry(NULL,f,0);
649 if (verbose && recurse && !am_server && f != -1)
650 rprintf(FINFO,"done\n");
652 clean_flist(flist, 0);
654 /* now send the uid/gid list. This was introduced in protocol
656 if (f != -1 && remote_version >= 15) {
660 /* if protocol version is >= 17 then send the io_error flag */
661 if (f != -1 && remote_version >= 17) {
662 write_int(f, io_error);
667 stats.flist_size = stats.total_written - start_write;
668 stats.num_files = flist->count;
672 rprintf(FINFO,"send_file_list done\n");
678 struct file_list *recv_file_list(int f)
680 struct file_list *flist;
684 if (verbose && recurse && !am_server) {
685 rprintf(FINFO,"receiving file list ... ");
689 start_read = stats.total_read;
691 flist = (struct file_list *)malloc(sizeof(flist[0]));
696 flist->malloced=1000;
697 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
703 for (flags=read_byte(f); flags; flags=read_byte(f)) {
704 int i = flist->count;
706 if (i >= flist->malloced) {
707 if (flist->malloced < 1000)
708 flist->malloced += 1000;
710 flist->malloced *= 2;
711 flist->files =(struct file_struct **)realloc(flist->files,
712 sizeof(flist->files[0])*
718 receive_file_entry(&flist->files[i],flags,f);
720 if (S_ISREG(flist->files[i]->mode))
721 stats.total_size += flist->files[i]->length;
726 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
731 rprintf(FINFO,"received %d names\n",flist->count);
733 clean_flist(flist, relative_paths);
735 if (verbose && recurse && !am_server) {
736 rprintf(FINFO,"done\n");
739 /* now recv the uid/gid list. This was introduced in protocol version 15 */
740 if (f != -1 && remote_version >= 15) {
741 recv_uid_list(f, flist);
744 /* if protocol version is >= 17 then recv the io_error flag */
745 if (f != -1 && remote_version >= 17) {
746 io_error |= read_int(f);
750 rprintf(FINFO,"recv_file_list done\n");
752 stats.flist_size = stats.total_read - start_read;
753 stats.num_files = flist->count;
758 out_of_memory("recv_file_list");
759 return NULL; /* not reached */
763 int file_compare(struct file_struct **f1,struct file_struct **f2)
765 if (!(*f1)->basename && !(*f2)->basename) return 0;
766 if (!(*f1)->basename) return -1;
767 if (!(*f2)->basename) return 1;
768 if ((*f1)->dirname == (*f2)->dirname)
769 return u_strcmp((*f1)->basename, (*f2)->basename);
770 return u_strcmp(f_name(*f1),f_name(*f2));
774 int flist_find(struct file_list *flist,struct file_struct *f)
776 int low=0,high=flist->count-1;
778 if (flist->count <= 0) return -1;
780 while (low != high) {
781 int mid = (low+high)/2;
782 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
783 if (ret == 0) return flist_up(flist, mid);
791 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
792 return flist_up(flist,low);
800 static void free_file(struct file_struct *file)
803 if (file->basename) free(file->basename);
804 if (file->link) free(file->link);
805 if (file->sum) free(file->sum);
806 memset((char *)file, 0, sizeof(*file));
811 * free up all elements in a flist
813 void flist_free(struct file_list *flist)
816 for (i=1;i<flist->count;i++) {
817 free_file(flist->files[i]);
818 free(flist->files[i]);
820 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
822 memset((char *)flist, 0, sizeof(*flist));
828 * This routine ensures we don't have any duplicate names in our file list.
829 * duplicate names can cause corruption because of the pipelining
831 static void clean_flist(struct file_list *flist, int strip_root)
835 if (!flist || flist->count == 0)
838 qsort(flist->files,flist->count,
839 sizeof(flist->files[0]),
840 (int (*)())file_compare);
842 for (i=1;i<flist->count;i++) {
843 if (flist->files[i]->basename &&
844 flist->files[i-1]->basename &&
845 strcmp(f_name(flist->files[i]),
846 f_name(flist->files[i-1])) == 0) {
847 if (verbose > 1 && !am_server)
848 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
849 f_name(flist->files[i-1]),i-1);
850 free_file(flist->files[i]);
855 /* we need to strip off the root directory in the case
856 of relative paths, but this must be done _after_
858 for (i=0;i<flist->count;i++) {
859 if (flist->files[i]->dirname &&
860 flist->files[i]->dirname[0] == '/') {
861 memmove(&flist->files[i]->dirname[0],
862 &flist->files[i]->dirname[1],
863 strlen(flist->files[i]->dirname));
866 if (flist->files[i]->dirname &&
867 !flist->files[i]->dirname[0]) {
868 flist->files[i]->dirname = NULL;
874 if (verbose <= 3) return;
876 for (i=0;i<flist->count;i++) {
877 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%d\n",
879 flist->files[i]->dirname,
880 flist->files[i]->basename,
881 flist->files[i]->mode,
882 flist->files[i]->length);
888 * return the full filename of a flist entry
890 char *f_name(struct file_struct *f)
892 static char names[10][MAXPATHLEN];
896 if (!f || !f->basename) return NULL;
901 slprintf(p, MAXPATHLEN-1, "%s/%s", f->dirname, f->basename);
903 strlcpy(p, f->basename, MAXPATHLEN-1);