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);
55 static void list_file_entry(struct file_struct *f)
57 char perms[11] = "----------";
58 char *perm_map = "rwxrwxrwx";
62 if (f->mode & (1<<i)) perms[9-i] = perm_map[8-i];
64 if (S_ISLNK(f->mode)) perms[0] = 'l';
65 if (S_ISDIR(f->mode)) perms[0] = 'd';
66 if (S_ISBLK(f->mode)) perms[0] = 'b';
67 if (S_ISCHR(f->mode)) perms[0] = 'c';
68 if (S_ISSOCK(f->mode)) perms[0] = 's';
69 if (S_ISFIFO(f->mode)) perms[0] = 'p';
71 if (preserve_links && S_ISLNK(f->mode)) {
72 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
74 (double)f->length, timestring(f->modtime),
77 rprintf(FINFO,"%s %11.0f %s %s\n",
79 (double)f->length, timestring(f->modtime), f_name(f));
84 int link_stat(const char *Path, STRUCT_STAT *Buffer)
88 return do_stat(Path, Buffer);
90 return do_lstat(Path, Buffer);
93 return do_stat(Path, Buffer);
98 This function is used to check if a file should be included/excluded
99 from the list of files based on its name and type etc
101 static int match_file_name(char *fname,STRUCT_STAT *st)
103 if (check_exclude(fname,local_exclude_list,st)) {
105 rprintf(FINFO,"excluding file %s\n",fname);
111 /* used by the one_file_system code */
112 static dev_t filesystem_dev;
114 static void set_filesystem(char *fname)
117 if (link_stat(fname,&st) != 0) return;
118 filesystem_dev = st.st_dev;
122 static int to_wire_mode(mode_t mode)
124 if (S_ISLNK(mode) && (S_IFLNK != 0120000)) {
125 return (mode & ~(_S_IFMT)) | 0120000;
130 static mode_t from_wire_mode(int mode)
132 if ((mode & (_S_IFMT)) == 0120000 && (S_IFLNK != 0120000)) {
133 return (mode & ~(_S_IFMT)) | S_IFLNK;
139 static void send_directory(int f,struct file_list *flist,char *dir);
141 static char *flist_dir;
144 static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
147 static time_t last_time;
148 static mode_t last_mode;
149 static dev_t last_rdev;
150 static uid_t last_uid;
151 static gid_t last_gid;
152 static char lastname[MAXPATHLEN];
163 fname = f_name(file);
167 if (file->mode == last_mode) flags |= SAME_MODE;
168 if (file->rdev == last_rdev) flags |= SAME_RDEV;
169 if (file->uid == last_uid) flags |= SAME_UID;
170 if (file->gid == last_gid) flags |= SAME_GID;
171 if (file->modtime == last_time) flags |= SAME_TIME;
173 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
174 l2 = strlen(fname) - l1;
176 if (l1 > 0) flags |= SAME_NAME;
177 if (l2 > 255) flags |= LONG_NAME;
179 /* we must make sure we don't send a zero flags byte or the other
180 end will terminate the flist transfer */
181 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
182 if (flags == 0) flags |= LONG_NAME;
185 if (flags & SAME_NAME)
187 if (flags & LONG_NAME)
191 write_buf(f,fname+l1,l2);
193 write_longint(f,file->length);
194 if (!(flags & SAME_TIME))
195 write_int(f,(int)file->modtime);
196 if (!(flags & SAME_MODE))
197 write_int(f,to_wire_mode(file->mode));
198 if (preserve_uid && !(flags & SAME_UID)) {
200 write_int(f,(int)file->uid);
202 if (preserve_gid && !(flags & SAME_GID)) {
204 write_int(f,(int)file->gid);
206 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
207 write_int(f,(int)file->rdev);
210 if (preserve_links && S_ISLNK(file->mode)) {
211 write_int(f,strlen(file->link));
212 write_buf(f,file->link,strlen(file->link));
216 #if SUPPORT_HARD_LINKS
217 if (preserve_hard_links && S_ISREG(file->mode)) {
218 write_int(f,(int)file->dev);
219 write_int(f,(int)file->inode);
223 if (always_checksum) {
224 write_buf(f,file->sum,csum_length);
227 last_mode = file->mode;
228 last_rdev = file->rdev;
229 last_uid = file->uid;
230 last_gid = file->gid;
231 last_time = file->modtime;
233 strlcpy(lastname,fname,MAXPATHLEN-1);
234 lastname[MAXPATHLEN-1] = 0;
239 static void receive_file_entry(struct file_struct **fptr,
240 unsigned flags,int f)
242 static time_t last_time;
243 static mode_t last_mode;
244 static dev_t last_rdev;
245 static uid_t last_uid;
246 static gid_t last_gid;
247 static char lastname[MAXPATHLEN];
248 char thisname[MAXPATHLEN];
251 struct file_struct *file;
253 if (flags & SAME_NAME)
256 if (flags & LONG_NAME)
261 file = (struct file_struct *)malloc(sizeof(*file));
262 if (!file) out_of_memory("receive_file_entry");
263 memset((char *)file, 0, sizeof(*file));
266 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
268 strlcpy(thisname,lastname,l1);
269 read_sbuf(f,&thisname[l1],l2);
272 strlcpy(lastname,thisname,MAXPATHLEN-1);
273 lastname[MAXPATHLEN-1] = 0;
275 clean_fname(thisname);
277 if ((p = strrchr(thisname,'/'))) {
278 static char *lastdir;
280 if (lastdir && strcmp(thisname, lastdir)==0) {
281 file->dirname = lastdir;
283 file->dirname = strdup(thisname);
284 lastdir = file->dirname;
286 file->basename = strdup(p+1);
288 file->dirname = NULL;
289 file->basename = strdup(thisname);
292 if (!file->basename) out_of_memory("receive_file_entry 1");
296 file->length = read_longint(f);
297 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
298 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
300 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
302 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
303 if (preserve_devices && IS_DEVICE(file->mode))
304 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
306 if (preserve_links && S_ISLNK(file->mode)) {
308 file->link = (char *)malloc(l+1);
309 if (!file->link) out_of_memory("receive_file_entry 2");
310 read_sbuf(f,file->link,l);
313 #if SUPPORT_HARD_LINKS
314 if (preserve_hard_links && S_ISREG(file->mode)) {
315 file->dev = read_int(f);
316 file->inode = read_int(f);
320 if (always_checksum) {
321 file->sum = (char *)malloc(MD4_SUM_LENGTH);
322 if (!file->sum) out_of_memory("md4 sum");
323 read_buf(f,file->sum,csum_length);
326 last_mode = file->mode;
327 last_rdev = file->rdev;
328 last_uid = file->uid;
329 last_gid = file->gid;
330 last_time = file->modtime;
332 if (!preserve_perms) {
333 extern int orig_umask;
334 /* set an appropriate set of permissions based on original
335 permissions and umask. This emulates what GNU cp does */
336 file->mode &= ~orig_umask;
341 /* determine if a file in a different filesstem should be skipped
342 when one_file_system is set. We bascally only want to include
343 the mount points - but they can be hard to find! */
344 static int skip_filesystem(char *fname, STRUCT_STAT *st)
347 char *p = strrchr(fname, '/');
349 /* skip all but directories */
350 if (!S_ISDIR(st->st_mode)) return 1;
352 /* if its not a subdirectory then allow */
356 if (link_stat(fname, &st2)) {
362 return (st2.st_dev != filesystem_dev);
365 static struct file_struct *make_file(char *fname)
367 struct file_struct *file;
369 char sum[SUM_LENGTH];
371 char cleaned_name[MAXPATHLEN];
373 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
374 cleaned_name[MAXPATHLEN-1] = 0;
375 clean_fname(cleaned_name);
376 fname = cleaned_name;
378 memset(sum,0,SUM_LENGTH);
380 if (link_stat(fname,&st) != 0) {
382 rprintf(FERROR,"%s: %s\n",
383 fname,strerror(errno));
387 if (S_ISDIR(st.st_mode) && !recurse) {
388 rprintf(FINFO,"skipping directory %s\n",fname);
392 if (one_file_system && st.st_dev != filesystem_dev) {
393 if (skip_filesystem(fname, &st))
397 if (!match_file_name(fname,&st))
401 rprintf(FINFO,"make_file(%s)\n",fname);
403 file = (struct file_struct *)malloc(sizeof(*file));
404 if (!file) out_of_memory("make_file");
405 memset((char *)file,0,sizeof(*file));
407 if ((p = strrchr(fname,'/'))) {
408 static char *lastdir;
410 if (lastdir && strcmp(fname, lastdir)==0) {
411 file->dirname = lastdir;
413 file->dirname = strdup(fname);
414 lastdir = file->dirname;
416 file->basename = strdup(p+1);
419 file->dirname = NULL;
420 file->basename = strdup(fname);
423 file->modtime = st.st_mtime;
424 file->length = st.st_size;
425 file->mode = st.st_mode;
426 file->uid = st.st_uid;
427 file->gid = st.st_gid;
428 file->dev = st.st_dev;
429 file->inode = st.st_ino;
431 file->rdev = st.st_rdev;
435 if (S_ISLNK(st.st_mode)) {
437 char lnk[MAXPATHLEN];
438 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
440 rprintf(FERROR,"readlink %s : %s\n",
441 fname,strerror(errno));
445 file->link = strdup(lnk);
449 if (always_checksum) {
450 file->sum = (char *)malloc(MD4_SUM_LENGTH);
451 if (!file->sum) out_of_memory("md4 sum");
452 /* drat. we have to provide a null checksum for non-regular
453 files in order to be compatible with earlier versions
455 if (S_ISREG(st.st_mode)) {
456 file_checksum(fname,file->sum,st.st_size);
458 memset(file->sum, 0, MD4_SUM_LENGTH);
463 static char *lastdir;
464 if (lastdir && strcmp(lastdir, flist_dir)==0) {
465 file->basedir = lastdir;
467 file->basedir = strdup(flist_dir);
468 lastdir = file->basedir;
471 file->basedir = NULL;
474 if (!S_ISDIR(st.st_mode))
475 stats.total_size += st.st_size;
482 void send_file_name(int f,struct file_list *flist,char *fname,
483 int recursive, unsigned base_flags)
485 struct file_struct *file;
487 file = make_file(fname);
491 if (flist->count >= flist->malloced) {
492 if (flist->malloced < 1000)
493 flist->malloced += 1000;
495 flist->malloced *= 2;
496 flist->files = (struct file_struct **)realloc(flist->files,
497 sizeof(flist->files[0])*
500 out_of_memory("send_file_name");
503 if (strcmp(file->basename,"")) {
504 flist->files[flist->count++] = file;
505 send_file_entry(file,f,base_flags);
508 if (S_ISDIR(file->mode) && recursive) {
509 struct exclude_struct **last_exclude_list = local_exclude_list;
510 send_directory(f,flist,f_name(file));
511 local_exclude_list = last_exclude_list;
518 static void send_directory(int f,struct file_list *flist,char *dir)
522 char fname[MAXPATHLEN];
529 rprintf(FERROR,"opendir(%s): %s\n",
530 dir,strerror(errno));
534 strlcpy(fname,dir,MAXPATHLEN-1);
536 if (fname[l-1] != '/') {
537 if (l == MAXPATHLEN-1) {
539 rprintf(FERROR,"skipping long-named directory %s\n",fname);
543 strlcat(fname,"/", MAXPATHLEN-1);
546 p = fname + strlen(fname);
548 local_exclude_list = NULL;
551 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
552 strcpy(p,".cvsignore");
553 local_exclude_list = make_exclude_list(fname,NULL,0,0);
556 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
560 for (di=readdir(d); di; di=readdir(d)) {
561 char *dname = d_name(di);
562 if (strcmp(dname,".")==0 ||
563 strcmp(dname,"..")==0)
565 strlcpy(p,dname,MAXPATHLEN-(l+1));
566 send_file_name(f,flist,fname,recurse,0);
569 if (local_exclude_list) {
570 add_exclude_list("!", &local_exclude_list, 0);
578 struct file_list *send_file_list(int f,int argc,char *argv[])
582 char *p,*dir,*olddir;
583 char lastpath[MAXPATHLEN]="";
584 struct file_list *flist;
587 if (verbose && recurse && !am_server && f != -1) {
588 rprintf(FINFO,"building file list ... ");
592 start_write = stats.total_written;
594 flist = (struct file_list *)malloc(sizeof(flist[0]));
595 if (!flist) out_of_memory("send_file_list");
598 flist->malloced = 1000;
599 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
601 if (!flist->files) out_of_memory("send_file_list");
604 io_start_buffering(f);
607 for (i=0;i<argc;i++) {
608 char fname2[MAXPATHLEN];
609 char *fname = fname2;
611 strlcpy(fname,argv[i],MAXPATHLEN-1);
614 if (l != 1 && fname[l-1] == '/') {
615 strlcat(fname,".",MAXPATHLEN-1);
618 if (link_stat(fname,&st) != 0) {
620 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
624 if (S_ISDIR(st.st_mode) && !recurse) {
625 rprintf(FINFO,"skipping directory %s\n",fname);
632 if (!relative_paths) {
633 p = strrchr(fname,'/');
642 } else if (f != -1 && (p=strrchr(fname,'/'))) {
643 /* this ensures we send the intermediate directories,
644 thus getting their permissions right */
646 if (strcmp(lastpath,fname)) {
647 strlcpy(lastpath, fname, sizeof(lastpath)-1);
649 for (p=fname+1; (p=strchr(p,'/')); p++) {
650 int copy_links_saved = copy_links;
653 send_file_name(f, flist, fname, 0, 0);
654 copy_links = copy_links_saved;
666 olddir = push_dir(dir, 1);
670 rprintf(FERROR,"push_dir %s : %s\n",
671 dir,strerror(errno));
679 set_filesystem(fname);
681 if (!recurse || !send_included_file_names(f,flist))
682 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
684 if (olddir != NULL) {
686 if (pop_dir(olddir) != 0) {
687 rprintf(FERROR,"pop_dir %s : %s\n",
688 dir,strerror(errno));
689 exit_cleanup(RERR_FILESELECT);
695 send_file_entry(NULL,f,0);
698 if (verbose && recurse && !am_server && f != -1)
699 rprintf(FINFO,"done\n");
701 clean_flist(flist, 0);
703 /* now send the uid/gid list. This was introduced in protocol
705 if (f != -1 && remote_version >= 15) {
709 /* if protocol version is >= 17 then send the io_error flag */
710 if (f != -1 && remote_version >= 17) {
711 write_int(f, io_error);
716 stats.flist_size = stats.total_written - start_write;
717 stats.num_files = flist->count;
721 rprintf(FINFO,"send_file_list done\n");
727 struct file_list *recv_file_list(int f)
729 struct file_list *flist;
732 extern int list_only;
734 if (verbose && recurse && !am_server) {
735 rprintf(FINFO,"receiving file list ... ");
739 start_read = stats.total_read;
741 flist = (struct file_list *)malloc(sizeof(flist[0]));
746 flist->malloced=1000;
747 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
753 for (flags=read_byte(f); flags; flags=read_byte(f)) {
754 int i = flist->count;
756 if (i >= flist->malloced) {
757 if (flist->malloced < 1000)
758 flist->malloced += 1000;
760 flist->malloced *= 2;
761 flist->files =(struct file_struct **)realloc(flist->files,
762 sizeof(flist->files[0])*
768 receive_file_entry(&flist->files[i],flags,f);
770 if (S_ISREG(flist->files[i]->mode))
771 stats.total_size += flist->files[i]->length;
776 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
781 rprintf(FINFO,"received %d names\n",flist->count);
783 clean_flist(flist, relative_paths);
785 if (verbose && recurse && !am_server) {
786 rprintf(FINFO,"done\n");
789 /* now recv the uid/gid list. This was introduced in protocol version 15 */
790 if (f != -1 && remote_version >= 15) {
791 recv_uid_list(f, flist);
794 /* if protocol version is >= 17 then recv the io_error flag */
795 if (f != -1 && remote_version >= 17) {
796 io_error |= read_int(f);
801 for (i=0;i<flist->count;i++) {
802 list_file_entry(flist->files[i]);
808 rprintf(FINFO,"recv_file_list done\n");
810 stats.flist_size = stats.total_read - start_read;
811 stats.num_files = flist->count;
816 out_of_memory("recv_file_list");
817 return NULL; /* not reached */
821 int file_compare(struct file_struct **f1,struct file_struct **f2)
823 if (!(*f1)->basename && !(*f2)->basename) return 0;
824 if (!(*f1)->basename) return -1;
825 if (!(*f2)->basename) return 1;
826 if ((*f1)->dirname == (*f2)->dirname)
827 return u_strcmp((*f1)->basename, (*f2)->basename);
828 return u_strcmp(f_name(*f1),f_name(*f2));
832 int flist_find(struct file_list *flist,struct file_struct *f)
834 int low=0,high=flist->count-1;
836 if (flist->count <= 0) return -1;
838 while (low != high) {
839 int mid = (low+high)/2;
840 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
841 if (ret == 0) return flist_up(flist, mid);
849 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
850 return flist_up(flist,low);
858 static void free_file(struct file_struct *file)
861 if (file->basename) free(file->basename);
862 if (file->link) free(file->link);
863 if (file->sum) free(file->sum);
864 memset((char *)file, 0, sizeof(*file));
869 * free up all elements in a flist
871 void flist_free(struct file_list *flist)
874 for (i=1;i<flist->count;i++) {
875 free_file(flist->files[i]);
876 free(flist->files[i]);
878 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
880 memset((char *)flist, 0, sizeof(*flist));
886 * This routine ensures we don't have any duplicate names in our file list.
887 * duplicate names can cause corruption because of the pipelining
889 static void clean_flist(struct file_list *flist, int strip_root)
893 if (!flist || flist->count == 0)
896 qsort(flist->files,flist->count,
897 sizeof(flist->files[0]),
898 (int (*)())file_compare);
900 for (i=1;i<flist->count;i++) {
901 if (flist->files[i]->basename &&
902 flist->files[i-1]->basename &&
903 strcmp(f_name(flist->files[i]),
904 f_name(flist->files[i-1])) == 0) {
905 if (verbose > 1 && !am_server)
906 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
907 f_name(flist->files[i-1]),i-1);
908 free_file(flist->files[i]);
913 /* we need to strip off the root directory in the case
914 of relative paths, but this must be done _after_
916 for (i=0;i<flist->count;i++) {
917 if (flist->files[i]->dirname &&
918 flist->files[i]->dirname[0] == '/') {
919 memmove(&flist->files[i]->dirname[0],
920 &flist->files[i]->dirname[1],
921 strlen(flist->files[i]->dirname));
924 if (flist->files[i]->dirname &&
925 !flist->files[i]->dirname[0]) {
926 flist->files[i]->dirname = NULL;
932 if (verbose <= 3) return;
934 for (i=0;i<flist->count;i++) {
935 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%d\n",
937 NS(flist->files[i]->dirname),
938 NS(flist->files[i]->basename),
939 flist->files[i]->mode,
940 (int)flist->files[i]->length);
946 * return the full filename of a flist entry
948 char *f_name(struct file_struct *f)
950 static char names[10][MAXPATHLEN];
954 if (!f || !f->basename) return NULL;
959 strlcpy(p, f->dirname, MAXPATHLEN-1);
960 strlcat(p, "/", MAXPATHLEN-1);
961 strlcat(p, f->basename, MAXPATHLEN-1);
963 strlcpy(p, f->basename, MAXPATHLEN-1);