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 char **local_exclude_list;
51 int link_stat(const char *Path, struct stat *Buffer)
55 return stat(Path, Buffer);
57 return lstat(Path, Buffer);
60 return 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)) {
72 fprintf(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;
93 static void clean_fname(char *name)
104 if ((p=strstr(name,"/./"))) {
112 if ((p=strstr(name,"//"))) {
120 if (strncmp(p=name,"./",2) == 0) {
128 if (l > 1 && p[l-1] == '/') {
137 void send_file_entry(struct file_struct *file,int f)
140 static time_t last_time;
141 static mode_t last_mode;
142 static dev_t last_rdev;
143 static uid_t last_uid;
144 static gid_t last_gid;
145 static char lastname[MAXPATHLEN];
156 fname = f_name(file);
160 if (file->mode == last_mode) flags |= SAME_MODE;
161 if (file->rdev == last_rdev) flags |= SAME_RDEV;
162 if (file->uid == last_uid) flags |= SAME_UID;
163 if (file->gid == last_gid) flags |= SAME_GID;
164 if (file->modtime == last_time) flags |= SAME_TIME;
166 for (l1=0;lastname[l1] && fname[l1] == lastname[l1];l1++) ;
167 l2 = strlen(fname) - l1;
169 if (l1 > 0) flags |= SAME_NAME;
170 if (l2 > 255) flags |= LONG_NAME;
173 if (flags & SAME_NAME)
175 if (flags & LONG_NAME)
179 write_buf(f,fname+l1,l2);
181 write_longint(f,file->length);
182 if (!(flags & SAME_TIME))
183 write_int(f,(int)file->modtime);
184 if (!(flags & SAME_MODE))
185 write_int(f,(int)file->mode);
186 if (preserve_uid && !(flags & SAME_UID)) {
188 write_int(f,(int)file->uid);
190 if (preserve_gid && !(flags & SAME_GID)) {
192 write_int(f,(int)file->gid);
194 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
195 write_int(f,(int)file->rdev);
198 if (preserve_links && S_ISLNK(file->mode)) {
199 write_int(f,strlen(file->link));
200 write_buf(f,file->link,strlen(file->link));
204 #if SUPPORT_HARD_LINKS
205 if (preserve_hard_links && S_ISREG(file->mode)) {
206 write_int(f,(int)file->dev);
207 write_int(f,(int)file->inode);
211 if (always_checksum) {
212 write_buf(f,file->sum,csum_length);
215 last_mode = file->mode;
216 last_rdev = file->rdev;
217 last_uid = file->uid;
218 last_gid = file->gid;
219 last_time = file->modtime;
221 strncpy(lastname,fname,MAXPATHLEN-1);
222 lastname[MAXPATHLEN-1] = 0;
227 void receive_file_entry(struct file_struct **fptr,
228 unsigned char flags,int f)
230 static time_t last_time;
231 static mode_t last_mode;
232 static dev_t last_rdev;
233 static uid_t last_uid;
234 static gid_t last_gid;
235 static char lastname[MAXPATHLEN];
236 char thisname[MAXPATHLEN];
239 struct file_struct *file;
241 if (flags & SAME_NAME)
244 if (flags & LONG_NAME)
249 file = (struct file_struct *)malloc(sizeof(*file));
250 if (!file) out_of_memory("receive_file_entry");
251 bzero((char *)file,sizeof(*file));
254 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
256 strncpy(thisname,lastname,l1);
257 read_sbuf(f,&thisname[l1],l2);
260 strncpy(lastname,thisname,MAXPATHLEN-1);
261 lastname[MAXPATHLEN-1] = 0;
263 clean_fname(thisname);
265 if ((p = strrchr(thisname,'/'))) {
266 static char *lastdir;
268 if (lastdir && strcmp(thisname, lastdir)==0) {
269 file->dirname = lastdir;
271 file->dirname = strdup(thisname);
272 lastdir = file->dirname;
274 file->basename = strdup(p+1);
276 file->dirname = NULL;
277 file->basename = strdup(thisname);
280 if (!file->basename) out_of_memory("receive_file_entry 1");
283 file->length = read_longint(f);
284 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
285 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
287 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
289 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
290 if (preserve_devices && IS_DEVICE(file->mode))
291 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
293 if (preserve_links && S_ISLNK(file->mode)) {
295 file->link = (char *)malloc(l+1);
296 if (!file->link) out_of_memory("receive_file_entry 2");
297 read_sbuf(f,file->link,l);
300 #if SUPPORT_HARD_LINKS
301 if (preserve_hard_links && S_ISREG(file->mode)) {
302 file->dev = read_int(f);
303 file->inode = read_int(f);
307 if (always_checksum) {
308 file->sum = (char *)malloc(MD4_SUM_LENGTH);
309 if (!file->sum) out_of_memory("md4 sum");
310 read_buf(f,file->sum,csum_length);
313 last_mode = file->mode;
314 last_rdev = file->rdev;
315 last_uid = file->uid;
316 last_gid = file->gid;
317 last_time = file->modtime;
321 /* determine if a file in a different filesstem should be skipped
322 when one_file_system is set. We bascally only want to include
323 the mount points - but they can be hard to find! */
324 static int skip_filesystem(char *fname, struct stat *st)
327 char *p = strrchr(fname, '/');
329 /* skip all but directories */
330 if (!S_ISDIR(st->st_mode)) return 1;
332 /* if its not a subdirectory then allow */
336 if (link_stat(fname, &st2)) {
342 return (st2.st_dev != filesystem_dev);
345 static struct file_struct *make_file(char *fname)
347 struct file_struct *file;
349 char sum[SUM_LENGTH];
351 char cleaned_name[MAXPATHLEN];
353 strncpy(cleaned_name, fname, MAXPATHLEN-1);
354 cleaned_name[MAXPATHLEN-1] = 0;
355 clean_fname(cleaned_name);
356 fname = cleaned_name;
358 bzero(sum,SUM_LENGTH);
360 if (link_stat(fname,&st) != 0) {
362 fprintf(FERROR,"%s: %s\n",
363 fname,strerror(errno));
367 if (S_ISDIR(st.st_mode) && !recurse) {
368 fprintf(FINFO,"skipping directory %s\n",fname);
372 if (one_file_system && st.st_dev != filesystem_dev) {
373 if (skip_filesystem(fname, &st))
377 if (!match_file_name(fname,&st))
381 fprintf(FINFO,"make_file(%s)\n",fname);
383 file = (struct file_struct *)malloc(sizeof(*file));
384 if (!file) out_of_memory("make_file");
385 bzero((char *)file,sizeof(*file));
387 if ((p = strrchr(fname,'/'))) {
388 static char *lastdir;
390 if (lastdir && strcmp(fname, lastdir)==0) {
391 file->dirname = lastdir;
393 file->dirname = strdup(fname);
394 lastdir = file->dirname;
396 file->basename = strdup(p+1);
399 file->dirname = NULL;
400 file->basename = strdup(fname);
403 file->modtime = st.st_mtime;
404 file->length = st.st_size;
405 file->mode = st.st_mode;
406 file->uid = st.st_uid;
407 file->gid = st.st_gid;
408 file->dev = st.st_dev;
409 file->inode = st.st_ino;
411 file->rdev = st.st_rdev;
415 if (S_ISLNK(st.st_mode)) {
417 char lnk[MAXPATHLEN];
418 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
420 fprintf(FERROR,"readlink %s : %s\n",
421 fname,strerror(errno));
425 file->link = strdup(lnk);
429 if (always_checksum && S_ISREG(st.st_mode)) {
430 file->sum = (char *)malloc(MD4_SUM_LENGTH);
431 if (!file->sum) out_of_memory("md4 sum");
432 file_checksum(fname,file->sum,st.st_size);
436 static char *lastdir;
437 if (lastdir && strcmp(lastdir, flist_dir)==0) {
438 file->basedir = lastdir;
440 file->basedir = strdup(flist_dir);
441 lastdir = file->basedir;
444 file->basedir = NULL;
447 if (!S_ISDIR(st.st_mode))
448 total_size += st.st_size;
455 static void send_file_name(int f,struct file_list *flist,char *fname,
458 struct file_struct *file;
460 file = make_file(fname);
464 if (flist->count >= flist->malloced) {
465 if (flist->malloced < 1000)
466 flist->malloced += 1000;
468 flist->malloced *= 2;
469 flist->files = (struct file_struct **)realloc(flist->files,
470 sizeof(flist->files[0])*
473 out_of_memory("send_file_name");
476 if (strcmp(file->basename,"")) {
477 flist->files[flist->count++] = file;
478 send_file_entry(file,f);
481 if (S_ISDIR(file->mode) && recursive) {
482 char **last_exclude_list = local_exclude_list;
483 send_directory(f,flist,f_name(file));
484 local_exclude_list = last_exclude_list;
491 static void send_directory(int f,struct file_list *flist,char *dir)
495 char fname[MAXPATHLEN];
502 fprintf(FERROR,"%s: %s\n",
503 dir,strerror(errno));
507 strncpy(fname,dir,MAXPATHLEN-1);
508 fname[MAXPATHLEN-1]=0;
510 if (fname[l-1] != '/') {
511 if (l == MAXPATHLEN-1) {
513 fprintf(FERROR,"skipping long-named directory %s\n",fname);
520 p = fname + strlen(fname);
523 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
524 strcpy(p,".cvsignore");
525 local_exclude_list = make_exclude_list(fname,NULL,0);
528 fprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
532 for (di=readdir(d); di; di=readdir(d)) {
533 if (strcmp(di->d_name,".")==0 ||
534 strcmp(di->d_name,"..")==0)
536 strncpy(p,di->d_name,MAXPATHLEN-(l+1));
537 send_file_name(f,flist,fname,recurse);
545 struct file_list *send_file_list(int f,int argc,char *argv[])
550 char dbuf[MAXPATHLEN];
551 char lastpath[MAXPATHLEN]="";
552 struct file_list *flist;
554 if (verbose && recurse && !am_server && f != -1) {
555 fprintf(FINFO,"building file list ... ");
559 flist = (struct file_list *)malloc(sizeof(flist[0]));
560 if (!flist) out_of_memory("send_file_list");
563 flist->malloced = 1000;
564 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
566 if (!flist->files) out_of_memory("send_file_list");
568 for (i=0;i<argc;i++) {
569 char fname2[MAXPATHLEN];
570 char *fname = fname2;
572 strncpy(fname,argv[i],MAXPATHLEN-1);
573 fname[MAXPATHLEN-1] = 0;
576 if (l != 1 && fname[l-1] == '/') {
580 if (link_stat(fname,&st) != 0) {
582 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
586 if (S_ISDIR(st.st_mode) && !recurse) {
587 fprintf(FINFO,"skipping directory %s\n",fname);
593 if (!relative_paths) {
594 p = strrchr(fname,'/');
603 } else if (f != -1 && (p=strrchr(fname,'/'))) {
604 /* this ensures we send the intermediate directories,
605 thus getting their permissions right */
607 if (strcmp(lastpath,fname)) {
608 strcpy(lastpath, fname);
610 for (p=fname+1; (p=strchr(p,'/')); p++) {
612 send_file_name(f, flist, fname, 0);
624 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
625 fprintf(FERROR,"getwd : %s\n",strerror(errno));
628 if (chdir(dir) != 0) {
630 fprintf(FERROR,"chdir %s : %s\n",
631 dir,strerror(errno));
636 set_filesystem(fname);
637 send_file_name(f,flist,fname,recurse);
639 if (chdir(dbuf) != 0) {
640 fprintf(FERROR,"chdir %s : %s\n",
641 dbuf,strerror(errno));
648 set_filesystem(fname);
649 send_file_name(f,flist,fname,recurse);
653 send_file_entry(NULL,f);
657 if (verbose && recurse && !am_server && f != -1)
658 fprintf(FINFO,"done\n");
662 /* now send the uid/gid list. This was introduced in protocol
664 if (f != -1 && remote_version >= 15) {
668 /* if protocol version is >= 17 then send the io_error flag */
669 if (f != -1 && remote_version >= 17) {
670 write_int(f, io_error);
674 fprintf(FINFO,"send_file_list done\n");
680 struct file_list *recv_file_list(int f)
682 struct file_list *flist;
685 if (verbose && recurse && !am_server) {
686 fprintf(FINFO,"receiving file list ... ");
690 flist = (struct file_list *)malloc(sizeof(flist[0]));
695 flist->malloced=1000;
696 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
702 for (flags=read_byte(f); flags; flags=read_byte(f)) {
703 int i = flist->count;
705 if (i >= flist->malloced) {
706 if (flist->malloced < 1000)
707 flist->malloced += 1000;
709 flist->malloced *= 2;
710 flist->files =(struct file_struct **)realloc(flist->files,
711 sizeof(flist->files[0])*
717 receive_file_entry(&flist->files[i],flags,f);
719 if (S_ISREG(flist->files[i]->mode))
720 total_size += flist->files[i]->length;
725 fprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
730 fprintf(FINFO,"received %d names\n",flist->count);
734 if (verbose && recurse && !am_server) {
735 fprintf(FINFO,"done\n");
738 /* now recv the uid/gid list. This was introduced in protocol version 15 */
739 if (f != -1 && remote_version >= 15) {
740 recv_uid_list(f, flist);
743 /* if protocol version is >= 17 then recv the io_error flag */
744 if (f != -1 && remote_version >= 17) {
745 io_error |= read_int(f);
749 fprintf(FINFO,"recv_file_list done\n");
754 out_of_memory("recv_file_list");
755 return NULL; /* not reached */
759 int file_compare(struct file_struct **f1,struct file_struct **f2)
761 if (!(*f1)->basename && !(*f2)->basename) return 0;
762 if (!(*f1)->basename) return -1;
763 if (!(*f2)->basename) return 1;
764 if ((*f1)->dirname == (*f2)->dirname)
765 return strcmp((*f1)->basename, (*f2)->basename);
766 return strcmp(f_name(*f1),f_name(*f2));
770 int flist_find(struct file_list *flist,struct file_struct *f)
772 int low=0,high=flist->count-1;
774 if (flist->count <= 0) return -1;
776 while (low != high) {
777 int mid = (low+high)/2;
778 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
779 if (ret == 0) return flist_up(flist, mid);
787 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
788 return flist_up(flist,low);
796 static void free_file(struct file_struct *file)
799 if (file->basename) free(file->basename);
800 if (file->link) free(file->link);
801 if (file->sum) free(file->sum);
802 bzero((char *)file, sizeof(*file));
807 * free up all elements in a flist
809 void flist_free(struct file_list *flist)
812 for (i=1;i<flist->count;i++) {
813 free_file(flist->files[i]);
814 free(flist->files[i]);
816 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
818 bzero((char *)flist, sizeof(*flist));
824 * This routine ensures we don't have any duplicate names in our file list.
825 * duplicate names can cause corruption because of the pipelining
827 void clean_flist(struct file_list *flist)
831 if (!flist || flist->count == 0)
834 qsort(flist->files,flist->count,
835 sizeof(flist->files[0]),
836 (int (*)())file_compare);
838 for (i=1;i<flist->count;i++) {
839 if (flist->files[i]->basename &&
840 flist->files[i-1]->basename &&
841 strcmp(f_name(flist->files[i]),
842 f_name(flist->files[i-1])) == 0) {
843 if (verbose > 1 && !am_server)
844 fprintf(FINFO,"removing duplicate name %s from file list %d\n",
845 f_name(flist->files[i-1]),i-1);
846 free_file(flist->files[i]);
853 * return the full filename of a flist entry
855 char *f_name(struct file_struct *f)
857 static char names[10][MAXPATHLEN];
861 if (!f || !f->basename) return NULL;
866 sprintf(p, "%s/%s", f->dirname, f->basename);
868 strcpy(p, f->basename);