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;
46 static char **local_exclude_list = NULL;
48 static void clean_fname(char *name);
52 This function is used to check if a file should be included/excluded
53 from the list of files based on its name and type etc
55 static int match_file_name(char *fname,struct stat *st)
57 if (check_exclude(fname,local_exclude_list)) {
59 fprintf(FERROR,"excluding file %s\n",fname);
65 /* used by the one_file_system code */
66 static dev_t filesystem_dev;
68 static void set_filesystem(char *fname)
71 if (lstat(fname,&st) != 0) return;
72 filesystem_dev = st.st_dev;
76 static void send_directory(int f,struct file_list *flist,char *dir);
78 static char *flist_dir = NULL;
80 extern void (*send_file_entry)(struct file_struct *file,int f);
81 extern void (*receive_file_entry)(struct file_struct *file,
82 unsigned char flags,int f);
85 void send_file_entry_v11(struct file_struct *file,int f)
88 static time_t last_time=0;
89 static mode_t last_mode=0;
90 static dev_t last_rdev=0;
91 static uid_t last_uid=0;
92 static gid_t last_gid=0;
93 static char lastname[MAXPATHLEN]="";
105 if (file->mode == last_mode) flags |= SAME_MODE;
106 if (file->rdev == last_rdev) flags |= SAME_RDEV;
107 if (file->uid == last_uid) flags |= SAME_UID;
108 if (file->gid == last_gid) flags |= SAME_GID;
109 if (file->modtime == last_time) flags |= SAME_TIME;
111 for (l1=0;lastname[l1] && file->name[l1] == lastname[l1];l1++) ;
112 l2 = strlen(file->name) - l1;
114 if (l1 > 0) flags |= SAME_NAME;
115 if (l2 > 255) flags |= LONG_NAME;
118 if (flags & SAME_NAME)
120 if (flags & LONG_NAME)
124 write_buf(f,file->name+l1,l2);
126 write_int(f,(int)file->length);
127 if (!(flags & SAME_TIME))
128 write_int(f,(int)file->modtime);
129 if (!(flags & SAME_MODE))
130 write_int(f,(int)file->mode);
131 if (preserve_uid && !(flags & SAME_UID))
132 write_int(f,(int)file->uid);
133 if (preserve_gid && !(flags & SAME_GID))
134 write_int(f,(int)file->gid);
135 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
136 write_int(f,(int)file->rdev);
139 if (preserve_links && S_ISLNK(file->mode)) {
140 write_int(f,strlen(file->link));
141 write_buf(f,file->link,strlen(file->link));
145 #if SUPPORT_HARD_LINKS
146 if (preserve_hard_links && S_ISREG(file->mode)) {
147 write_int(f,file->dev);
148 write_int(f,file->inode);
152 if (always_checksum) {
153 write_buf(f,file->sum,csum_length);
156 last_mode = file->mode;
157 last_rdev = file->rdev;
158 last_uid = file->uid;
159 last_gid = file->gid;
160 last_time = file->modtime;
162 strcpy(lastname,file->name);
168 void receive_file_entry_v11(struct file_struct *file,
169 unsigned char flags,int f)
171 static time_t last_time=0;
172 static mode_t last_mode=0;
173 static dev_t last_rdev=0;
174 static uid_t last_uid=0;
175 static gid_t last_gid=0;
176 static char lastname[MAXPATHLEN]="";
179 if (flags & SAME_NAME)
182 if (flags & LONG_NAME)
187 bzero((char *)file,sizeof(*file));
189 file->name = (char *)malloc(l1+l2+1);
190 if (!file->name) out_of_memory("receive_file_entry");
192 strncpy(file->name,lastname,l1);
193 read_buf(f,file->name+l1,l2);
194 file->name[l1+l2] = 0;
196 file->length = (off_t)read_int(f);
197 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
198 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
200 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
202 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
203 if (preserve_devices && IS_DEVICE(file->mode))
204 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
207 if (preserve_links && S_ISLNK(file->mode)) {
209 file->link = (char *)malloc(l+1);
210 if (!file->link) out_of_memory("receive_file_entry");
211 read_buf(f,file->link,l);
216 #if SUPPORT_HARD_LINKS
217 if (preserve_hard_links && S_ISREG(file->mode)) {
218 file->dev = read_int(f);
219 file->inode = read_int(f);
224 read_buf(f,file->sum,csum_length);
226 last_mode = file->mode;
227 last_rdev = file->rdev;
228 last_uid = file->uid;
229 last_gid = file->gid;
230 last_time = file->modtime;
232 strcpy(lastname,file->name);
238 static struct file_struct *make_file(char *fname)
240 static struct file_struct file;
242 char sum[SUM_LENGTH];
244 bzero(sum,SUM_LENGTH);
246 if (lstat(fname,&st) != 0) {
247 fprintf(FERROR,"%s: %s\n",
248 fname,strerror(errno));
252 if (S_ISDIR(st.st_mode) && !recurse) {
253 fprintf(FERROR,"skipping directory %s\n",fname);
257 if (one_file_system && st.st_dev != filesystem_dev)
260 if (!match_file_name(fname,&st))
264 fprintf(FERROR,"make_file(%s)\n",fname);
266 bzero((char *)&file,sizeof(file));
268 file.name = strdup(fname);
269 file.modtime = st.st_mtime;
270 file.length = st.st_size;
271 file.mode = st.st_mode;
272 file.uid = st.st_uid;
273 file.gid = st.st_gid;
274 file.dev = st.st_dev;
275 file.inode = st.st_ino;
277 file.rdev = st.st_rdev;
281 if (S_ISLNK(st.st_mode)) {
283 char lnk[MAXPATHLEN];
284 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
285 fprintf(FERROR,"readlink %s : %s\n",fname,strerror(errno));
289 file.link = strdup(lnk);
293 if (always_checksum && S_ISREG(st.st_mode)) {
294 file_checksum(fname,file.sum,st.st_size);
298 file.dir = strdup(flist_dir);
302 if (!S_ISDIR(st.st_mode))
303 total_size += st.st_size;
310 static void send_file_name(int f,struct file_list *flist,char *fname)
312 struct file_struct *file;
314 file = make_file(fname);
318 if (flist->count >= flist->malloced) {
319 flist->malloced += 100;
320 flist->files = (struct file_struct *)realloc(flist->files,
321 sizeof(flist->files[0])*
324 out_of_memory("send_file_name");
327 if (strcmp(file->name,"/")) {
328 flist->files[flist->count++] = *file;
329 send_file_entry(file,f);
332 if (S_ISDIR(file->mode) && recurse) {
333 char **last_exclude_list = local_exclude_list;
334 send_directory(f,flist,file->name);
335 local_exclude_list = last_exclude_list;
342 static void send_directory(int f,struct file_list *flist,char *dir)
346 char fname[MAXPATHLEN];
352 fprintf(FERROR,"%s: %s\n",
353 dir,strerror(errno));
359 if (fname[l-1] != '/')
361 p = fname + strlen(fname);
364 strcpy(p,".cvsignore");
365 local_exclude_list = make_exclude_list(fname,NULL,0);
368 for (di=readdir(d); di; di=readdir(d)) {
369 if (strcmp(di->d_name,".")==0 ||
370 strcmp(di->d_name,"..")==0)
372 strcpy(p,di->d_name);
373 send_file_name(f,flist,fname);
381 struct file_list *send_file_list(int f,int argc,char *argv[])
386 char dbuf[MAXPATHLEN];
387 struct file_list *flist;
389 if (verbose && recurse && !am_server) {
390 fprintf(FINFO,"building file list ... ");
394 flist = (struct file_list *)malloc(sizeof(flist[0]));
395 if (!flist) out_of_memory("send_file_list");
398 flist->malloced = 100;
399 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
401 if (!flist->files) out_of_memory("send_file_list");
403 for (i=0;i<argc;i++) {
404 char fname2[MAXPATHLEN];
405 char *fname = fname2;
407 strcpy(fname,argv[i]);
410 if (l != 1 && fname[l-1] == '/') {
414 if (lstat(fname,&st) != 0) {
415 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
419 if (S_ISDIR(st.st_mode) && !recurse) {
420 fprintf(FERROR,"skipping directory %s\n",fname);
426 if (!relative_paths) {
427 p = strrchr(fname,'/');
442 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
443 fprintf(FERROR,"getwd : %s\n",strerror(errno));
446 if (chdir(dir) != 0) {
447 fprintf(FERROR,"chdir %s : %s\n",dir,strerror(errno));
452 set_filesystem(fname);
453 send_file_name(f,flist,fname);
455 if (chdir(dbuf) != 0) {
456 fprintf(FERROR,"chdir %s : %s\n",dbuf,strerror(errno));
463 set_filesystem(fname);
464 send_file_name(f,flist,fname);
468 send_file_entry(NULL,f);
472 if (verbose && recurse && !am_server)
473 fprintf(FINFO,"done\n");
481 struct file_list *recv_file_list(int f)
483 struct file_list *flist;
486 if (verbose && recurse && !am_server) {
487 fprintf(FINFO,"receiving file list ... ");
491 flist = (struct file_list *)malloc(sizeof(flist[0]));
497 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
503 for (flags=read_byte(f); flags; flags=read_byte(f)) {
504 int i = flist->count;
506 if (i >= flist->malloced) {
507 flist->malloced += 100;
508 flist->files =(struct file_struct *)realloc(flist->files,
509 sizeof(flist->files[0])*
515 receive_file_entry(&flist->files[i],flags,f);
517 if (S_ISREG(flist->files[i].mode))
518 total_size += flist->files[i].length;
523 fprintf(FERROR,"recv_file_name(%s)\n",flist->files[i].name);
528 fprintf(FERROR,"received %d names\n",flist->count);
532 if (verbose && recurse && !am_server) {
533 fprintf(FINFO,"done\n");
539 out_of_memory("recv_file_list");
540 return NULL; /* not reached */
544 int file_compare(struct file_struct *f1,struct file_struct *f2)
546 if (!f1->name && !f2->name) return 0;
547 if (!f1->name) return -1;
548 if (!f2->name) return 1;
549 return strcmp(f1->name,f2->name);
553 int flist_find(struct file_list *flist,struct file_struct *f)
555 int low=0,high=flist->count;
557 while (low != high) {
558 int mid = (low+high)/2;
559 int ret = file_compare(&flist->files[mid],f);
560 if (ret == 0) return mid;
566 if (file_compare(&flist->files[low],f) == 0)
572 static void clean_fname(char *name)
583 if ((p=strstr(name,"/./"))) {
591 if ((p=strstr(name,"//"))) {
599 if (strncmp(p=name,"./",2) == 0) {
608 if (l > 1 && p[l-1] == '/') {
617 * This routine ensures we don't have any duplicate names in our file list.
618 * duplicate names can cause corruption because of the pipelining
620 void clean_flist(struct file_list *flist)
624 if (!flist || flist->count == 0)
627 for (i=0;i<flist->count;i++) {
628 clean_fname(flist->files[i].name);
631 qsort(flist->files,flist->count,
632 sizeof(flist->files[0]),
633 (int (*)())file_compare);
635 for (i=1;i<flist->count;i++) {
636 if (flist->files[i].name &&
637 strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
638 if (verbose > 1 && !am_server)
639 fprintf(FERROR,"removing duplicate name %s from file list %d\n",
640 flist->files[i-1].name,i-1);
641 free(flist->files[i-1].name);
642 bzero((char *)&flist->files[i-1],sizeof(flist->files[i-1]));