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 int link_stat(const char *Path, STRUCT_STAT *Buffer)
56 return do_stat(Path, Buffer);
58 return do_lstat(Path, Buffer);
61 return do_stat(Path, Buffer);
66 This function is used to check if a file should be included/excluded
67 from the list of files based on its name and type etc
69 static int match_file_name(char *fname,STRUCT_STAT *st)
71 if (check_exclude(fname,local_exclude_list,st)) {
73 rprintf(FINFO,"excluding file %s\n",fname);
79 /* used by the one_file_system code */
80 static dev_t filesystem_dev;
82 static void set_filesystem(char *fname)
85 if (link_stat(fname,&st) != 0) return;
86 filesystem_dev = st.st_dev;
90 static void send_directory(int f,struct file_list *flist,char *dir);
92 static char *flist_dir;
95 void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
98 static time_t last_time;
99 static mode_t last_mode;
100 static dev_t last_rdev;
101 static uid_t last_uid;
102 static gid_t last_gid;
103 static char lastname[MAXPATHLEN];
114 fname = f_name(file);
118 if (file->mode == last_mode) flags |= SAME_MODE;
119 if (file->rdev == last_rdev) flags |= SAME_RDEV;
120 if (file->uid == last_uid) flags |= SAME_UID;
121 if (file->gid == last_gid) flags |= SAME_GID;
122 if (file->modtime == last_time) flags |= SAME_TIME;
124 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
125 l2 = strlen(fname) - l1;
127 if (l1 > 0) flags |= SAME_NAME;
128 if (l2 > 255) flags |= LONG_NAME;
130 /* we must make sure we don't send a zero flags byte or the other
131 end will terminate the flist transfer */
132 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
133 if (flags == 0) flags |= LONG_NAME;
136 if (flags & SAME_NAME)
138 if (flags & LONG_NAME)
142 write_buf(f,fname+l1,l2);
144 write_longint(f,file->length);
145 if (!(flags & SAME_TIME))
146 write_int(f,(int)file->modtime);
147 if (!(flags & SAME_MODE))
148 write_int(f,(int)file->mode);
149 if (preserve_uid && !(flags & SAME_UID)) {
151 write_int(f,(int)file->uid);
153 if (preserve_gid && !(flags & SAME_GID)) {
155 write_int(f,(int)file->gid);
157 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
158 write_int(f,(int)file->rdev);
161 if (preserve_links && S_ISLNK(file->mode)) {
162 write_int(f,strlen(file->link));
163 write_buf(f,file->link,strlen(file->link));
167 #if SUPPORT_HARD_LINKS
168 if (preserve_hard_links && S_ISREG(file->mode)) {
169 write_int(f,(int)file->dev);
170 write_int(f,(int)file->inode);
174 if (always_checksum) {
175 write_buf(f,file->sum,csum_length);
178 last_mode = file->mode;
179 last_rdev = file->rdev;
180 last_uid = file->uid;
181 last_gid = file->gid;
182 last_time = file->modtime;
184 strlcpy(lastname,fname,MAXPATHLEN-1);
185 lastname[MAXPATHLEN-1] = 0;
190 static void receive_file_entry(struct file_struct **fptr,
191 unsigned flags,int f)
193 static time_t last_time;
194 static mode_t last_mode;
195 static dev_t last_rdev;
196 static uid_t last_uid;
197 static gid_t last_gid;
198 static char lastname[MAXPATHLEN];
199 char thisname[MAXPATHLEN];
202 struct file_struct *file;
204 if (flags & SAME_NAME)
207 if (flags & LONG_NAME)
212 file = (struct file_struct *)malloc(sizeof(*file));
213 if (!file) out_of_memory("receive_file_entry");
214 memset((char *)file, 0, sizeof(*file));
217 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
219 strlcpy(thisname,lastname,l1);
220 read_sbuf(f,&thisname[l1],l2);
223 strlcpy(lastname,thisname,MAXPATHLEN-1);
224 lastname[MAXPATHLEN-1] = 0;
226 clean_fname(thisname);
228 if (relative_paths && thisname[0] == '/') {
229 /* strip / off absolute paths in destination */
230 memmove(thisname, thisname+1, strlen(thisname));
231 if (!thisname[0]) strcpy(thisname,".");
234 if ((p = strrchr(thisname,'/'))) {
235 static char *lastdir;
237 if (lastdir && strcmp(thisname, lastdir)==0) {
238 file->dirname = lastdir;
240 file->dirname = strdup(thisname);
241 lastdir = file->dirname;
243 file->basename = strdup(p+1);
245 file->dirname = NULL;
246 file->basename = strdup(thisname);
249 if (!file->basename) out_of_memory("receive_file_entry 1");
253 file->length = read_longint(f);
254 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
255 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
257 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
259 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
260 if (preserve_devices && IS_DEVICE(file->mode))
261 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
263 if (preserve_links && S_ISLNK(file->mode)) {
265 file->link = (char *)malloc(l+1);
266 if (!file->link) out_of_memory("receive_file_entry 2");
267 read_sbuf(f,file->link,l);
270 #if SUPPORT_HARD_LINKS
271 if (preserve_hard_links && S_ISREG(file->mode)) {
272 file->dev = read_int(f);
273 file->inode = read_int(f);
277 if (always_checksum) {
278 file->sum = (char *)malloc(MD4_SUM_LENGTH);
279 if (!file->sum) out_of_memory("md4 sum");
280 read_buf(f,file->sum,csum_length);
283 last_mode = file->mode;
284 last_rdev = file->rdev;
285 last_uid = file->uid;
286 last_gid = file->gid;
287 last_time = file->modtime;
289 if (!preserve_perms) {
290 extern int orig_umask;
291 /* set an appropriate set of permissions based on original
292 permissions and umask. This emulates what GNU cp does */
293 file->mode &= ~orig_umask;
298 /* determine if a file in a different filesstem should be skipped
299 when one_file_system is set. We bascally only want to include
300 the mount points - but they can be hard to find! */
301 static int skip_filesystem(char *fname, STRUCT_STAT *st)
304 char *p = strrchr(fname, '/');
306 /* skip all but directories */
307 if (!S_ISDIR(st->st_mode)) return 1;
309 /* if its not a subdirectory then allow */
313 if (link_stat(fname, &st2)) {
319 return (st2.st_dev != filesystem_dev);
322 static struct file_struct *make_file(char *fname)
324 struct file_struct *file;
326 char sum[SUM_LENGTH];
328 char cleaned_name[MAXPATHLEN];
330 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
331 cleaned_name[MAXPATHLEN-1] = 0;
332 clean_fname(cleaned_name);
333 fname = cleaned_name;
335 memset(sum,0,SUM_LENGTH);
337 if (link_stat(fname,&st) != 0) {
339 rprintf(FERROR,"%s: %s\n",
340 fname,strerror(errno));
344 if (S_ISDIR(st.st_mode) && !recurse) {
345 rprintf(FINFO,"skipping directory %s\n",fname);
349 if (one_file_system && st.st_dev != filesystem_dev) {
350 if (skip_filesystem(fname, &st))
354 if (!match_file_name(fname,&st))
358 rprintf(FINFO,"make_file(%s)\n",fname);
360 file = (struct file_struct *)malloc(sizeof(*file));
361 if (!file) out_of_memory("make_file");
362 memset((char *)file,0,sizeof(*file));
364 if ((p = strrchr(fname,'/'))) {
365 static char *lastdir;
367 if (lastdir && strcmp(fname, lastdir)==0) {
368 file->dirname = lastdir;
370 file->dirname = strdup(fname);
371 lastdir = file->dirname;
373 file->basename = strdup(p+1);
376 file->dirname = NULL;
377 file->basename = strdup(fname);
380 file->modtime = st.st_mtime;
381 file->length = st.st_size;
382 file->mode = st.st_mode;
383 file->uid = st.st_uid;
384 file->gid = st.st_gid;
385 file->dev = st.st_dev;
386 file->inode = st.st_ino;
388 file->rdev = st.st_rdev;
392 if (S_ISLNK(st.st_mode)) {
394 char lnk[MAXPATHLEN];
395 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
397 rprintf(FERROR,"readlink %s : %s\n",
398 fname,strerror(errno));
402 file->link = strdup(lnk);
406 if (always_checksum) {
407 file->sum = (char *)malloc(MD4_SUM_LENGTH);
408 if (!file->sum) out_of_memory("md4 sum");
409 /* drat. we have to provide a null checksum for non-regular
410 files in order to be compatible with earlier versions
412 if (S_ISREG(st.st_mode)) {
413 file_checksum(fname,file->sum,st.st_size);
415 memset(file->sum, 0, MD4_SUM_LENGTH);
420 static char *lastdir;
421 if (lastdir && strcmp(lastdir, flist_dir)==0) {
422 file->basedir = lastdir;
424 file->basedir = strdup(flist_dir);
425 lastdir = file->basedir;
428 file->basedir = NULL;
431 if (!S_ISDIR(st.st_mode))
432 stats.total_size += st.st_size;
439 static void send_file_name(int f,struct file_list *flist,char *fname,
440 int recursive, unsigned base_flags)
442 struct file_struct *file;
444 file = make_file(fname);
448 if (flist->count >= flist->malloced) {
449 if (flist->malloced < 1000)
450 flist->malloced += 1000;
452 flist->malloced *= 2;
453 flist->files = (struct file_struct **)realloc(flist->files,
454 sizeof(flist->files[0])*
457 out_of_memory("send_file_name");
460 if (strcmp(file->basename,"")) {
461 flist->files[flist->count++] = file;
462 send_file_entry(file,f,base_flags);
465 if (S_ISDIR(file->mode) && recursive) {
466 struct exclude_struct **last_exclude_list = local_exclude_list;
467 send_directory(f,flist,f_name(file));
468 local_exclude_list = last_exclude_list;
475 static void send_directory(int f,struct file_list *flist,char *dir)
479 char fname[MAXPATHLEN];
486 rprintf(FERROR,"opendir(%s): %s\n",
487 dir,strerror(errno));
491 strlcpy(fname,dir,MAXPATHLEN-1);
493 if (fname[l-1] != '/') {
494 if (l == MAXPATHLEN-1) {
496 rprintf(FERROR,"skipping long-named directory %s\n",fname);
500 strlcat(fname,"/", MAXPATHLEN-1);
503 p = fname + strlen(fname);
506 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
507 strcpy(p,".cvsignore");
508 local_exclude_list = make_exclude_list(fname,NULL,0,0);
511 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
515 for (di=readdir(d); di; di=readdir(d)) {
516 char *dname = d_name(di);
517 if (strcmp(dname,".")==0 ||
518 strcmp(dname,"..")==0)
520 strlcpy(p,dname,MAXPATHLEN-(l+1));
521 send_file_name(f,flist,fname,recurse,0);
529 struct file_list *send_file_list(int f,int argc,char *argv[])
534 char lastpath[MAXPATHLEN]="";
535 struct file_list *flist;
538 if (verbose && recurse && !am_server && f != -1) {
539 rprintf(FINFO,"building file list ... ");
543 start_write = stats.total_written;
545 flist = (struct file_list *)malloc(sizeof(flist[0]));
546 if (!flist) out_of_memory("send_file_list");
549 flist->malloced = 1000;
550 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
552 if (!flist->files) out_of_memory("send_file_list");
555 io_start_buffering(f);
558 for (i=0;i<argc;i++) {
559 char fname2[MAXPATHLEN];
560 char *fname = fname2;
562 strlcpy(fname,argv[i],MAXPATHLEN-1);
565 if (l != 1 && fname[l-1] == '/') {
566 strlcat(fname,".",MAXPATHLEN-1);
569 if (link_stat(fname,&st) != 0) {
571 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
575 if (S_ISDIR(st.st_mode) && !recurse) {
576 rprintf(FINFO,"skipping directory %s\n",fname);
582 if (!relative_paths) {
583 p = strrchr(fname,'/');
592 } else if (f != -1 && (p=strrchr(fname,'/'))) {
593 /* this ensures we send the intermediate directories,
594 thus getting their permissions right */
596 if (strcmp(lastpath,fname)) {
597 strlcpy(lastpath, fname, sizeof(lastpath)-1);
599 for (p=fname+1; (p=strchr(p,'/')); p++) {
600 int copy_links_saved = copy_links;
603 send_file_name(f, flist, fname, 0, 0);
604 copy_links = copy_links_saved;
616 char *olddir = push_dir(dir, 1);
620 rprintf(FERROR,"push_dir %s : %s\n",
621 dir,strerror(errno));
627 set_filesystem(fname);
628 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
630 if (pop_dir(olddir) != 0) {
631 rprintf(FERROR,"pop_dir %s : %s\n",
632 dir,strerror(errno));
639 set_filesystem(fname);
640 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
644 send_file_entry(NULL,f,0);
647 if (verbose && recurse && !am_server && f != -1)
648 rprintf(FINFO,"done\n");
652 /* now send the uid/gid list. This was introduced in protocol
654 if (f != -1 && remote_version >= 15) {
658 /* if protocol version is >= 17 then send the io_error flag */
659 if (f != -1 && remote_version >= 17) {
660 write_int(f, io_error);
665 stats.flist_size = stats.total_written - start_write;
666 stats.num_files = flist->count;
670 rprintf(FINFO,"send_file_list done\n");
676 struct file_list *recv_file_list(int f)
678 struct file_list *flist;
682 if (verbose && recurse && !am_server) {
683 rprintf(FINFO,"receiving file list ... ");
687 start_read = stats.total_read;
689 flist = (struct file_list *)malloc(sizeof(flist[0]));
694 flist->malloced=1000;
695 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
701 for (flags=read_byte(f); flags; flags=read_byte(f)) {
702 int i = flist->count;
704 if (i >= flist->malloced) {
705 if (flist->malloced < 1000)
706 flist->malloced += 1000;
708 flist->malloced *= 2;
709 flist->files =(struct file_struct **)realloc(flist->files,
710 sizeof(flist->files[0])*
716 receive_file_entry(&flist->files[i],flags,f);
718 if (S_ISREG(flist->files[i]->mode))
719 stats.total_size += flist->files[i]->length;
724 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
729 rprintf(FINFO,"received %d names\n",flist->count);
733 if (verbose && recurse && !am_server) {
734 rprintf(FINFO,"done\n");
737 /* now recv the uid/gid list. This was introduced in protocol version 15 */
738 if (f != -1 && remote_version >= 15) {
739 recv_uid_list(f, flist);
742 /* if protocol version is >= 17 then recv the io_error flag */
743 if (f != -1 && remote_version >= 17) {
744 io_error |= read_int(f);
748 rprintf(FINFO,"recv_file_list done\n");
750 stats.flist_size = stats.total_read - start_read;
751 stats.num_files = flist->count;
756 out_of_memory("recv_file_list");
757 return NULL; /* not reached */
761 int file_compare(struct file_struct **f1,struct file_struct **f2)
763 if (!(*f1)->basename && !(*f2)->basename) return 0;
764 if (!(*f1)->basename) return -1;
765 if (!(*f2)->basename) return 1;
766 if ((*f1)->dirname == (*f2)->dirname)
767 return u_strcmp((*f1)->basename, (*f2)->basename);
768 return u_strcmp(f_name(*f1),f_name(*f2));
772 int flist_find(struct file_list *flist,struct file_struct *f)
774 int low=0,high=flist->count-1;
776 if (flist->count <= 0) return -1;
778 while (low != high) {
779 int mid = (low+high)/2;
780 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
781 if (ret == 0) return flist_up(flist, mid);
789 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
790 return flist_up(flist,low);
798 static void free_file(struct file_struct *file)
801 if (file->basename) free(file->basename);
802 if (file->link) free(file->link);
803 if (file->sum) free(file->sum);
804 memset((char *)file, 0, sizeof(*file));
809 * free up all elements in a flist
811 void flist_free(struct file_list *flist)
814 for (i=1;i<flist->count;i++) {
815 free_file(flist->files[i]);
816 free(flist->files[i]);
818 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
820 memset((char *)flist, 0, sizeof(*flist));
826 * This routine ensures we don't have any duplicate names in our file list.
827 * duplicate names can cause corruption because of the pipelining
829 void clean_flist(struct file_list *flist)
833 if (!flist || flist->count == 0)
836 qsort(flist->files,flist->count,
837 sizeof(flist->files[0]),
838 (int (*)())file_compare);
840 for (i=1;i<flist->count;i++) {
841 if (flist->files[i]->basename &&
842 flist->files[i-1]->basename &&
843 strcmp(f_name(flist->files[i]),
844 f_name(flist->files[i-1])) == 0) {
845 if (verbose > 1 && !am_server)
846 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
847 f_name(flist->files[i-1]),i-1);
848 free_file(flist->files[i]);
855 * return the full filename of a flist entry
857 char *f_name(struct file_struct *f)
859 static char names[10][MAXPATHLEN];
863 if (!f || !f->basename) return NULL;
868 slprintf(p, MAXPATHLEN-1, "%s/%s", f->dirname, f->basename);
870 strlcpy(p, f->basename, MAXPATHLEN-1);