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 off_t 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;
48 static char **local_exclude_list;
50 int link_stat(const char *Path, struct stat *Buffer)
54 return stat(Path, Buffer);
56 return lstat(Path, Buffer);
59 return stat(Path, Buffer);
64 This function is used to check if a file should be included/excluded
65 from the list of files based on its name and type etc
67 static int match_file_name(char *fname,struct stat *st)
69 if (check_exclude(fname,local_exclude_list)) {
71 fprintf(FERROR,"excluding file %s\n",fname);
77 /* used by the one_file_system code */
78 static dev_t filesystem_dev;
80 static void set_filesystem(char *fname)
83 if (link_stat(fname,&st) != 0) return;
84 filesystem_dev = st.st_dev;
88 static void send_directory(int f,struct file_list *flist,char *dir);
90 static char *flist_dir;
92 static void clean_fname(char *name)
103 if ((p=strstr(name,"/./"))) {
111 if ((p=strstr(name,"//"))) {
119 if (strncmp(p=name,"./",2) == 0) {
127 if (l > 1 && p[l-1] == '/') {
136 void send_file_entry(struct file_struct *file,int f)
139 static time_t last_time;
140 static mode_t last_mode;
141 static dev_t last_rdev;
142 static uid_t last_uid;
143 static gid_t last_gid;
144 static char lastname[MAXPATHLEN];
155 fname = f_name(file);
159 if (file->mode == last_mode) flags |= SAME_MODE;
160 if (file->rdev == last_rdev) flags |= SAME_RDEV;
161 if (file->uid == last_uid) flags |= SAME_UID;
162 if (file->gid == last_gid) flags |= SAME_GID;
163 if (file->modtime == last_time) flags |= SAME_TIME;
165 for (l1=0;lastname[l1] && fname[l1] == lastname[l1];l1++) ;
166 l2 = strlen(fname) - l1;
168 if (l1 > 0) flags |= SAME_NAME;
169 if (l2 > 255) flags |= LONG_NAME;
172 if (flags & SAME_NAME)
174 if (flags & LONG_NAME)
178 write_buf(f,fname+l1,l2);
180 write_longint(f,file->length);
181 if (!(flags & SAME_TIME))
182 write_int(f,(int)file->modtime);
183 if (!(flags & SAME_MODE))
184 write_int(f,(int)file->mode);
185 if (preserve_uid && !(flags & SAME_UID)) {
187 write_int(f,(int)file->uid);
189 if (preserve_gid && !(flags & SAME_GID)) {
191 write_int(f,(int)file->gid);
193 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
194 write_int(f,(int)file->rdev);
197 if (preserve_links && S_ISLNK(file->mode)) {
198 write_int(f,strlen(file->link));
199 write_buf(f,file->link,strlen(file->link));
203 #if SUPPORT_HARD_LINKS
204 if (preserve_hard_links && S_ISREG(file->mode)) {
205 write_int(f,(int)file->dev);
206 write_int(f,(int)file->inode);
210 if (always_checksum) {
211 write_buf(f,file->sum,csum_length);
214 last_mode = file->mode;
215 last_rdev = file->rdev;
216 last_uid = file->uid;
217 last_gid = file->gid;
218 last_time = file->modtime;
220 strncpy(lastname,fname,MAXPATHLEN-1);
221 lastname[MAXPATHLEN-1] = 0;
226 void receive_file_entry(struct file_struct **fptr,
227 unsigned char flags,int f)
229 static time_t last_time;
230 static mode_t last_mode;
231 static dev_t last_rdev;
232 static uid_t last_uid;
233 static gid_t last_gid;
234 static char lastname[MAXPATHLEN];
235 char thisname[MAXPATHLEN];
238 struct file_struct *file;
240 if (flags & SAME_NAME)
243 if (flags & LONG_NAME)
248 file = (struct file_struct *)malloc(sizeof(*file));
249 if (!file) out_of_memory("receive_file_entry");
250 bzero((char *)file,sizeof(*file));
253 strncpy(thisname,lastname,l1);
254 read_buf(f,&thisname[l1],l2);
257 strncpy(lastname,thisname,MAXPATHLEN-1);
258 lastname[MAXPATHLEN-1] = 0;
260 clean_fname(thisname);
262 if ((p = strrchr(thisname,'/'))) {
263 static char *lastdir;
265 if (lastdir && strcmp(thisname, lastdir)==0) {
266 file->dirname = lastdir;
268 file->dirname = strdup(thisname);
269 lastdir = file->dirname;
271 file->basename = strdup(p+1);
273 file->dirname = NULL;
274 file->basename = strdup(thisname);
277 if (!file->basename) out_of_memory("receive_file_entry 1");
280 file->length = read_longint(f);
281 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
282 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
284 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
286 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
287 if (preserve_devices && IS_DEVICE(file->mode))
288 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
290 if (preserve_links && S_ISLNK(file->mode)) {
292 file->link = (char *)malloc(l+1);
293 if (!file->link) out_of_memory("receive_file_entry 2");
294 read_buf(f,file->link,l);
298 #if SUPPORT_HARD_LINKS
299 if (preserve_hard_links && S_ISREG(file->mode)) {
300 file->dev = read_int(f);
301 file->inode = read_int(f);
305 if (always_checksum) {
306 file->sum = (char *)malloc(MD4_SUM_LENGTH);
307 if (!file->sum) out_of_memory("md4 sum");
308 read_buf(f,file->sum,csum_length);
311 last_mode = file->mode;
312 last_rdev = file->rdev;
313 last_uid = file->uid;
314 last_gid = file->gid;
315 last_time = file->modtime;
320 static struct file_struct *make_file(char *fname)
322 struct file_struct *file;
324 char sum[SUM_LENGTH];
326 char cleaned_name[MAXPATHLEN];
328 strncpy(cleaned_name, fname, MAXPATHLEN-1);
329 cleaned_name[MAXPATHLEN-1] = 0;
330 clean_fname(cleaned_name);
331 fname = cleaned_name;
333 bzero(sum,SUM_LENGTH);
335 if (link_stat(fname,&st) != 0) {
336 fprintf(FERROR,"%s: %s\n",
337 fname,strerror(errno));
341 if (S_ISDIR(st.st_mode) && !recurse) {
342 fprintf(FERROR,"skipping directory %s\n",fname);
346 if (one_file_system && st.st_dev != filesystem_dev)
349 if (!match_file_name(fname,&st))
353 fprintf(FERROR,"make_file(%s)\n",fname);
355 file = (struct file_struct *)malloc(sizeof(*file));
356 if (!file) out_of_memory("make_file");
357 bzero((char *)file,sizeof(*file));
359 if ((p = strrchr(fname,'/'))) {
360 static char *lastdir;
362 if (lastdir && strcmp(fname, lastdir)==0) {
363 file->dirname = lastdir;
365 file->dirname = strdup(fname);
366 lastdir = file->dirname;
368 file->basename = strdup(p+1);
371 file->dirname = NULL;
372 file->basename = strdup(fname);
375 file->modtime = st.st_mtime;
376 file->length = st.st_size;
377 file->mode = st.st_mode;
378 file->uid = st.st_uid;
379 file->gid = st.st_gid;
380 file->dev = st.st_dev;
381 file->inode = st.st_ino;
383 file->rdev = st.st_rdev;
387 if (S_ISLNK(st.st_mode)) {
389 char lnk[MAXPATHLEN];
390 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
391 fprintf(FERROR,"readlink %s : %s\n",
392 fname,strerror(errno));
396 file->link = strdup(lnk);
400 if (always_checksum && S_ISREG(st.st_mode)) {
401 file->sum = (char *)malloc(MD4_SUM_LENGTH);
402 if (!file->sum) out_of_memory("md4 sum");
403 file_checksum(fname,file->sum,st.st_size);
407 static char *lastdir;
408 if (lastdir && strcmp(lastdir, flist_dir)==0) {
409 file->basedir = lastdir;
411 file->basedir = strdup(flist_dir);
412 lastdir = file->basedir;
415 file->basedir = NULL;
418 if (!S_ISDIR(st.st_mode))
419 total_size += st.st_size;
426 static void send_file_name(int f,struct file_list *flist,char *fname,
429 struct file_struct *file;
431 file = make_file(fname);
435 if (flist->count >= flist->malloced) {
436 if (flist->malloced < 1000)
437 flist->malloced += 1000;
439 flist->malloced *= 2;
440 flist->files = (struct file_struct **)realloc(flist->files,
441 sizeof(flist->files[0])*
444 out_of_memory("send_file_name");
447 if (strcmp(file->basename,"")) {
448 flist->files[flist->count++] = file;
449 send_file_entry(file,f);
452 if (S_ISDIR(file->mode) && recursive) {
453 char **last_exclude_list = local_exclude_list;
454 send_directory(f,flist,f_name(file));
455 local_exclude_list = last_exclude_list;
462 static void send_directory(int f,struct file_list *flist,char *dir)
466 char fname[MAXPATHLEN];
472 fprintf(FERROR,"%s: %s\n",
473 dir,strerror(errno));
477 strncpy(fname,dir,MAXPATHLEN-1);
478 fname[MAXPATHLEN-1]=0;
480 if (fname[l-1] != '/') {
481 if (l == MAXPATHLEN-1) {
482 fprintf(FERROR,"skipping long-named directory %s\n",fname);
489 p = fname + strlen(fname);
492 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
493 strcpy(p,".cvsignore");
494 local_exclude_list = make_exclude_list(fname,NULL,0);
496 fprintf(FERROR,"cannot cvs-exclude in long-named directory %s\n",fname);
500 for (di=readdir(d); di; di=readdir(d)) {
501 if (strcmp(di->d_name,".")==0 ||
502 strcmp(di->d_name,"..")==0)
504 strncpy(p,di->d_name,MAXPATHLEN-(l+1));
505 send_file_name(f,flist,fname,recurse);
513 struct file_list *send_file_list(int f,int argc,char *argv[])
518 char dbuf[MAXPATHLEN];
519 char lastpath[MAXPATHLEN]="";
520 struct file_list *flist;
522 if (verbose && recurse && !am_server && f != -1) {
523 fprintf(FINFO,"building file list ... ");
527 flist = (struct file_list *)malloc(sizeof(flist[0]));
528 if (!flist) out_of_memory("send_file_list");
531 flist->malloced = 1000;
532 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
534 if (!flist->files) out_of_memory("send_file_list");
536 for (i=0;i<argc;i++) {
537 char fname2[MAXPATHLEN];
538 char *fname = fname2;
540 strncpy(fname,argv[i],MAXPATHLEN-1);
541 fname[MAXPATHLEN-1] = 0;
544 if (l != 1 && fname[l-1] == '/') {
548 if (link_stat(fname,&st) != 0) {
549 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
553 if (S_ISDIR(st.st_mode) && !recurse) {
554 fprintf(FERROR,"skipping directory %s\n",fname);
560 if (!relative_paths) {
561 p = strrchr(fname,'/');
570 } else if (f != -1 && (p=strrchr(fname,'/'))) {
571 /* this ensures we send the intermediate directories,
572 thus getting their permissions right */
574 if (strcmp(lastpath,fname)) {
575 strcpy(lastpath, fname);
577 for (p=fname+1; (p=strchr(p,'/')); p++) {
579 send_file_name(f, flist, fname, 0);
591 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
592 fprintf(FERROR,"getwd : %s\n",strerror(errno));
595 if (chdir(dir) != 0) {
596 fprintf(FERROR,"chdir %s : %s\n",
597 dir,strerror(errno));
602 set_filesystem(fname);
603 send_file_name(f,flist,fname,recurse);
605 if (chdir(dbuf) != 0) {
606 fprintf(FERROR,"chdir %s : %s\n",
607 dbuf,strerror(errno));
614 set_filesystem(fname);
615 send_file_name(f,flist,fname,recurse);
619 send_file_entry(NULL,f);
623 if (verbose && recurse && !am_server && f != -1)
624 fprintf(FINFO,"done\n");
628 /* now send the uid/gid list. This was introduced in protocol
630 if (f != -1 && remote_version >= 15) {
638 struct file_list *recv_file_list(int f)
640 struct file_list *flist;
643 if (verbose && recurse && !am_server) {
644 fprintf(FINFO,"receiving file list ... ");
648 flist = (struct file_list *)malloc(sizeof(flist[0]));
653 flist->malloced=1000;
654 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
660 for (flags=read_byte(f); flags; flags=read_byte(f)) {
661 int i = flist->count;
663 if (i >= flist->malloced) {
664 if (flist->malloced < 1000)
665 flist->malloced += 1000;
667 flist->malloced *= 2;
668 flist->files =(struct file_struct **)realloc(flist->files,
669 sizeof(flist->files[0])*
675 receive_file_entry(&flist->files[i],flags,f);
677 if (S_ISREG(flist->files[i]->mode))
678 total_size += flist->files[i]->length;
683 fprintf(FERROR,"recv_file_name(%s)\n",f_name(flist->files[i]));
688 fprintf(FERROR,"received %d names\n",flist->count);
692 if (verbose && recurse && !am_server) {
693 fprintf(FINFO,"done\n");
696 /* now recv the uid/gid list. This was introduced in protocol version 15 */
697 if (f != -1 && remote_version >= 15) {
698 recv_uid_list(f, flist);
704 out_of_memory("recv_file_list");
705 return NULL; /* not reached */
709 int file_compare(struct file_struct **f1,struct file_struct **f2)
711 if (!(*f1)->basename && !(*f2)->basename) return 0;
712 if (!(*f1)->basename) return -1;
713 if (!(*f2)->basename) return 1;
714 if ((*f1)->dirname == (*f2)->dirname)
715 return strcmp((*f1)->basename, (*f2)->basename);
716 return strcmp(f_name(*f1),f_name(*f2));
720 int flist_find(struct file_list *flist,struct file_struct *f)
722 int low=0,high=flist->count-1;
724 if (flist->count <= 0) return -1;
726 while (low != high) {
727 int mid = (low+high)/2;
728 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
729 if (ret == 0) return flist_up(flist, mid);
737 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
738 return flist_up(flist,low);
746 static void free_file(struct file_struct *file)
749 if (file->basename) free(file->basename);
750 if (file->link) free(file->link);
751 if (file->sum) free(file->sum);
752 bzero((char *)file, sizeof(*file));
757 * free up all elements in a flist
759 void flist_free(struct file_list *flist)
762 for (i=1;i<flist->count;i++) {
763 free_file(flist->files[i]);
764 free(flist->files[i]);
766 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
768 bzero((char *)flist, sizeof(*flist));
774 * This routine ensures we don't have any duplicate names in our file list.
775 * duplicate names can cause corruption because of the pipelining
777 void clean_flist(struct file_list *flist)
781 if (!flist || flist->count == 0)
784 qsort(flist->files,flist->count,
785 sizeof(flist->files[0]),
786 (int (*)())file_compare);
788 for (i=1;i<flist->count;i++) {
789 if (flist->files[i]->basename &&
790 flist->files[i-1]->basename &&
791 strcmp(f_name(flist->files[i]),
792 f_name(flist->files[i-1])) == 0) {
793 if (verbose > 1 && !am_server)
794 fprintf(FERROR,"removing duplicate name %s from file list %d\n",
795 f_name(flist->files[i-1]),i-1);
796 free_file(flist->files[i]);
803 * return the full filename of a flist entry
805 char *f_name(struct file_struct *f)
807 static char names[10][MAXPATHLEN];
811 if (!f || !f->basename) return NULL;
816 sprintf(p, "%s/%s", f->dirname, f->basename);
818 strcpy(p, f->basename);