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;
94 void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
97 static time_t last_time;
98 static mode_t last_mode;
99 static dev_t last_rdev;
100 static uid_t last_uid;
101 static gid_t last_gid;
102 static char lastname[MAXPATHLEN];
113 fname = f_name(file);
117 if (file->mode == last_mode) flags |= SAME_MODE;
118 if (file->rdev == last_rdev) flags |= SAME_RDEV;
119 if (file->uid == last_uid) flags |= SAME_UID;
120 if (file->gid == last_gid) flags |= SAME_GID;
121 if (file->modtime == last_time) flags |= SAME_TIME;
123 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
124 l2 = strlen(fname) - l1;
126 if (l1 > 0) flags |= SAME_NAME;
127 if (l2 > 255) flags |= LONG_NAME;
129 /* we must make sure we don't send a zero flags byte or the other
130 end will terminate the flist transfer */
131 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
132 if (flags == 0) flags |= LONG_NAME;
135 if (flags & SAME_NAME)
137 if (flags & LONG_NAME)
141 write_buf(f,fname+l1,l2);
143 write_longint(f,file->length);
144 if (!(flags & SAME_TIME))
145 write_int(f,(int)file->modtime);
146 if (!(flags & SAME_MODE))
147 write_int(f,(int)file->mode);
148 if (preserve_uid && !(flags & SAME_UID)) {
150 write_int(f,(int)file->uid);
152 if (preserve_gid && !(flags & SAME_GID)) {
154 write_int(f,(int)file->gid);
156 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
157 write_int(f,(int)file->rdev);
160 if (preserve_links && S_ISLNK(file->mode)) {
161 write_int(f,strlen(file->link));
162 write_buf(f,file->link,strlen(file->link));
166 #if SUPPORT_HARD_LINKS
167 if (preserve_hard_links && S_ISREG(file->mode)) {
168 write_int(f,(int)file->dev);
169 write_int(f,(int)file->inode);
173 if (always_checksum) {
174 write_buf(f,file->sum,csum_length);
177 last_mode = file->mode;
178 last_rdev = file->rdev;
179 last_uid = file->uid;
180 last_gid = file->gid;
181 last_time = file->modtime;
183 strlcpy(lastname,fname,MAXPATHLEN-1);
184 lastname[MAXPATHLEN-1] = 0;
189 static void receive_file_entry(struct file_struct **fptr,
190 unsigned flags,int f)
192 static time_t last_time;
193 static mode_t last_mode;
194 static dev_t last_rdev;
195 static uid_t last_uid;
196 static gid_t last_gid;
197 static char lastname[MAXPATHLEN];
198 char thisname[MAXPATHLEN];
201 struct file_struct *file;
203 if (flags & SAME_NAME)
206 if (flags & LONG_NAME)
211 file = (struct file_struct *)malloc(sizeof(*file));
212 if (!file) out_of_memory("receive_file_entry");
213 bzero((char *)file,sizeof(*file));
216 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
218 strlcpy(thisname,lastname,l1);
219 read_sbuf(f,&thisname[l1],l2);
222 strlcpy(lastname,thisname,MAXPATHLEN-1);
223 lastname[MAXPATHLEN-1] = 0;
225 clean_fname(thisname);
227 if (relative_paths && thisname[0] == '/') {
228 /* strip / off absolute paths in destination */
229 memmove(thisname, thisname+1, strlen(thisname));
230 if (!thisname[0]) strcpy(thisname,".");
233 if ((p = strrchr(thisname,'/'))) {
234 static char *lastdir;
236 if (lastdir && strcmp(thisname, lastdir)==0) {
237 file->dirname = lastdir;
239 file->dirname = strdup(thisname);
240 lastdir = file->dirname;
242 file->basename = strdup(p+1);
244 file->dirname = NULL;
245 file->basename = strdup(thisname);
248 if (!file->basename) out_of_memory("receive_file_entry 1");
252 file->length = read_longint(f);
253 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
254 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
256 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
258 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
259 if (preserve_devices && IS_DEVICE(file->mode))
260 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
262 if (preserve_links && S_ISLNK(file->mode)) {
264 file->link = (char *)malloc(l+1);
265 if (!file->link) out_of_memory("receive_file_entry 2");
266 read_sbuf(f,file->link,l);
269 #if SUPPORT_HARD_LINKS
270 if (preserve_hard_links && S_ISREG(file->mode)) {
271 file->dev = read_int(f);
272 file->inode = read_int(f);
276 if (always_checksum) {
277 file->sum = (char *)malloc(MD4_SUM_LENGTH);
278 if (!file->sum) out_of_memory("md4 sum");
279 read_buf(f,file->sum,csum_length);
282 last_mode = file->mode;
283 last_rdev = file->rdev;
284 last_uid = file->uid;
285 last_gid = file->gid;
286 last_time = file->modtime;
290 /* determine if a file in a different filesstem should be skipped
291 when one_file_system is set. We bascally only want to include
292 the mount points - but they can be hard to find! */
293 static int skip_filesystem(char *fname, STRUCT_STAT *st)
296 char *p = strrchr(fname, '/');
298 /* skip all but directories */
299 if (!S_ISDIR(st->st_mode)) return 1;
301 /* if its not a subdirectory then allow */
305 if (link_stat(fname, &st2)) {
311 return (st2.st_dev != filesystem_dev);
314 static struct file_struct *make_file(char *fname)
316 struct file_struct *file;
318 char sum[SUM_LENGTH];
320 char cleaned_name[MAXPATHLEN];
322 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
323 cleaned_name[MAXPATHLEN-1] = 0;
324 clean_fname(cleaned_name);
325 fname = cleaned_name;
327 bzero(sum,SUM_LENGTH);
329 if (link_stat(fname,&st) != 0) {
331 rprintf(FERROR,"%s: %s\n",
332 fname,strerror(errno));
336 if (S_ISDIR(st.st_mode) && !recurse) {
337 rprintf(FINFO,"skipping directory %s\n",fname);
341 if (one_file_system && st.st_dev != filesystem_dev) {
342 if (skip_filesystem(fname, &st))
346 if (!match_file_name(fname,&st))
350 rprintf(FINFO,"make_file(%s)\n",fname);
352 file = (struct file_struct *)malloc(sizeof(*file));
353 if (!file) out_of_memory("make_file");
354 bzero((char *)file,sizeof(*file));
356 if ((p = strrchr(fname,'/'))) {
357 static char *lastdir;
359 if (lastdir && strcmp(fname, lastdir)==0) {
360 file->dirname = lastdir;
362 file->dirname = strdup(fname);
363 lastdir = file->dirname;
365 file->basename = strdup(p+1);
368 file->dirname = NULL;
369 file->basename = strdup(fname);
372 file->modtime = st.st_mtime;
373 file->length = st.st_size;
374 file->mode = st.st_mode;
375 file->uid = st.st_uid;
376 file->gid = st.st_gid;
377 file->dev = st.st_dev;
378 file->inode = st.st_ino;
380 file->rdev = st.st_rdev;
384 if (S_ISLNK(st.st_mode)) {
386 char lnk[MAXPATHLEN];
387 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
389 rprintf(FERROR,"readlink %s : %s\n",
390 fname,strerror(errno));
394 file->link = strdup(lnk);
398 if (always_checksum) {
399 file->sum = (char *)malloc(MD4_SUM_LENGTH);
400 if (!file->sum) out_of_memory("md4 sum");
401 /* drat. we have to provide a null checksum for non-regular
402 files in order to be compatible with earlier versions
404 if (S_ISREG(st.st_mode)) {
405 file_checksum(fname,file->sum,st.st_size);
407 memset(file->sum, 0, MD4_SUM_LENGTH);
412 static char *lastdir;
413 if (lastdir && strcmp(lastdir, flist_dir)==0) {
414 file->basedir = lastdir;
416 file->basedir = strdup(flist_dir);
417 lastdir = file->basedir;
420 file->basedir = NULL;
423 if (!S_ISDIR(st.st_mode))
424 total_size += st.st_size;
431 static void send_file_name(int f,struct file_list *flist,char *fname,
432 int recursive, unsigned base_flags)
434 struct file_struct *file;
436 file = make_file(fname);
440 if (flist->count >= flist->malloced) {
441 if (flist->malloced < 1000)
442 flist->malloced += 1000;
444 flist->malloced *= 2;
445 flist->files = (struct file_struct **)realloc(flist->files,
446 sizeof(flist->files[0])*
449 out_of_memory("send_file_name");
452 if (strcmp(file->basename,"")) {
453 flist->files[flist->count++] = file;
454 send_file_entry(file,f,base_flags);
457 if (S_ISDIR(file->mode) && recursive) {
458 struct exclude_struct **last_exclude_list = local_exclude_list;
459 send_directory(f,flist,f_name(file));
460 local_exclude_list = last_exclude_list;
467 static void send_directory(int f,struct file_list *flist,char *dir)
471 char fname[MAXPATHLEN];
478 rprintf(FERROR,"opendir(%s): %s\n",
479 dir,strerror(errno));
483 strlcpy(fname,dir,MAXPATHLEN-1);
485 if (fname[l-1] != '/') {
486 if (l == MAXPATHLEN-1) {
488 rprintf(FERROR,"skipping long-named directory %s\n",fname);
492 strlcat(fname,"/", MAXPATHLEN-1);
495 p = fname + strlen(fname);
498 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
499 strcpy(p,".cvsignore");
500 local_exclude_list = make_exclude_list(fname,NULL,0,0);
503 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
507 for (di=readdir(d); di; di=readdir(d)) {
508 char *dname = d_name(di);
509 if (strcmp(dname,".")==0 ||
510 strcmp(dname,"..")==0)
512 strlcpy(p,dname,MAXPATHLEN-(l+1));
513 send_file_name(f,flist,fname,recurse,0);
521 struct file_list *send_file_list(int f,int argc,char *argv[])
526 char lastpath[MAXPATHLEN]="";
527 struct file_list *flist;
529 if (verbose && recurse && !am_server && f != -1) {
530 rprintf(FINFO,"building file list ... ");
534 flist = (struct file_list *)malloc(sizeof(flist[0]));
535 if (!flist) out_of_memory("send_file_list");
538 flist->malloced = 1000;
539 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
541 if (!flist->files) out_of_memory("send_file_list");
544 io_start_buffering(f);
547 for (i=0;i<argc;i++) {
548 char fname2[MAXPATHLEN];
549 char *fname = fname2;
551 strlcpy(fname,argv[i],MAXPATHLEN-1);
554 if (l != 1 && fname[l-1] == '/') {
555 strlcat(fname,".",MAXPATHLEN-1);
558 if (link_stat(fname,&st) != 0) {
560 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
564 if (S_ISDIR(st.st_mode) && !recurse) {
565 rprintf(FINFO,"skipping directory %s\n",fname);
571 if (!relative_paths) {
572 p = strrchr(fname,'/');
581 } else if (f != -1 && (p=strrchr(fname,'/'))) {
582 /* this ensures we send the intermediate directories,
583 thus getting their permissions right */
585 if (strcmp(lastpath,fname)) {
586 strlcpy(lastpath, fname, sizeof(lastpath)-1);
588 for (p=fname+1; (p=strchr(p,'/')); p++) {
589 int copy_links_saved = copy_links;
592 send_file_name(f, flist, fname, 0, 0);
593 copy_links = copy_links_saved;
605 char *olddir = push_dir(dir, 1);
609 rprintf(FERROR,"push_dir %s : %s\n",
610 dir,strerror(errno));
616 set_filesystem(fname);
617 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
619 if (pop_dir(olddir) != 0) {
620 rprintf(FERROR,"pop_dir %s : %s\n",
621 dir,strerror(errno));
628 set_filesystem(fname);
629 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
633 send_file_entry(NULL,f,0);
636 if (verbose && recurse && !am_server && f != -1)
637 rprintf(FINFO,"done\n");
641 /* now send the uid/gid list. This was introduced in protocol
643 if (f != -1 && remote_version >= 15) {
647 /* if protocol version is >= 17 then send the io_error flag */
648 if (f != -1 && remote_version >= 17) {
649 write_int(f, io_error);
657 rprintf(FINFO,"send_file_list done\n");
663 struct file_list *recv_file_list(int f)
665 struct file_list *flist;
668 if (verbose && recurse && !am_server) {
669 rprintf(FINFO,"receiving file list ... ");
673 flist = (struct file_list *)malloc(sizeof(flist[0]));
678 flist->malloced=1000;
679 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
685 for (flags=read_byte(f); flags; flags=read_byte(f)) {
686 int i = flist->count;
688 if (i >= flist->malloced) {
689 if (flist->malloced < 1000)
690 flist->malloced += 1000;
692 flist->malloced *= 2;
693 flist->files =(struct file_struct **)realloc(flist->files,
694 sizeof(flist->files[0])*
700 receive_file_entry(&flist->files[i],flags,f);
702 if (S_ISREG(flist->files[i]->mode))
703 total_size += flist->files[i]->length;
708 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
713 rprintf(FINFO,"received %d names\n",flist->count);
717 if (verbose && recurse && !am_server) {
718 rprintf(FINFO,"done\n");
721 /* now recv the uid/gid list. This was introduced in protocol version 15 */
722 if (f != -1 && remote_version >= 15) {
723 recv_uid_list(f, flist);
726 /* if protocol version is >= 17 then recv the io_error flag */
727 if (f != -1 && remote_version >= 17) {
728 io_error |= read_int(f);
732 rprintf(FINFO,"recv_file_list done\n");
737 out_of_memory("recv_file_list");
738 return NULL; /* not reached */
742 int file_compare(struct file_struct **f1,struct file_struct **f2)
744 if (!(*f1)->basename && !(*f2)->basename) return 0;
745 if (!(*f1)->basename) return -1;
746 if (!(*f2)->basename) return 1;
747 if ((*f1)->dirname == (*f2)->dirname)
748 return strcmp((*f1)->basename, (*f2)->basename);
749 return strcmp(f_name(*f1),f_name(*f2));
753 int flist_find(struct file_list *flist,struct file_struct *f)
755 int low=0,high=flist->count-1;
757 if (flist->count <= 0) return -1;
759 while (low != high) {
760 int mid = (low+high)/2;
761 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
762 if (ret == 0) return flist_up(flist, mid);
770 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
771 return flist_up(flist,low);
779 static void free_file(struct file_struct *file)
782 if (file->basename) free(file->basename);
783 if (file->link) free(file->link);
784 if (file->sum) free(file->sum);
785 bzero((char *)file, sizeof(*file));
790 * free up all elements in a flist
792 void flist_free(struct file_list *flist)
795 for (i=1;i<flist->count;i++) {
796 free_file(flist->files[i]);
797 free(flist->files[i]);
799 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
801 bzero((char *)flist, sizeof(*flist));
807 * This routine ensures we don't have any duplicate names in our file list.
808 * duplicate names can cause corruption because of the pipelining
810 void clean_flist(struct file_list *flist)
814 if (!flist || flist->count == 0)
817 qsort(flist->files,flist->count,
818 sizeof(flist->files[0]),
819 (int (*)())file_compare);
821 for (i=1;i<flist->count;i++) {
822 if (flist->files[i]->basename &&
823 flist->files[i-1]->basename &&
824 strcmp(f_name(flist->files[i]),
825 f_name(flist->files[i-1])) == 0) {
826 if (verbose > 1 && !am_server)
827 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
828 f_name(flist->files[i-1]),i-1);
829 free_file(flist->files[i]);
836 * return the full filename of a flist entry
838 char *f_name(struct file_struct *f)
840 static char names[10][MAXPATHLEN];
844 if (!f || !f->basename) return NULL;
849 slprintf(p, MAXPATHLEN-1, "%s/%s", f->dirname, f->basename);
851 strlcpy(p, f->basename, MAXPATHLEN-1);