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 */
26 extern int always_checksum;
27 extern off_t total_size;
29 extern int cvs_exclude;
31 extern int one_file_system;
32 extern int make_backups;
33 extern int preserve_links;
34 extern int preserve_perms;
35 extern int preserve_devices;
36 extern int preserve_uid;
37 extern int preserve_gid;
38 extern int preserve_times;
40 static char **local_exclude_list = NULL;
42 static void clean_fname(char *name);
46 This function is used to check if a file should be included/excluded
47 from the list of files based on its name and type etc
49 static int match_file_name(char *fname,struct stat *st)
51 if (check_exclude(fname,local_exclude_list)) {
53 fprintf(stderr,"excluding file %s\n",fname);
59 /* used by the one_file_system code */
60 static dev_t filesystem_dev;
62 static void set_filesystem(char *fname)
65 if (lstat(fname,&st) != 0) return;
66 filesystem_dev = st.st_dev;
70 static void send_directory(int f,struct file_list *flist,char *dir);
72 static char *flist_dir = NULL;
74 extern void (*send_file_entry)(struct file_struct *file,int f);
75 extern void (*receive_file_entry)(struct file_struct *file,
76 unsigned char flags,int f);
79 void send_file_entry_v11(struct file_struct *file,int f)
82 static time_t last_time=0;
83 static mode_t last_mode=0;
84 static dev_t last_dev=0;
85 static uid_t last_uid=0;
86 static gid_t last_gid=0;
87 static char lastname[MAXPATHLEN]="";
99 if (file->mode == last_mode) flags |= SAME_MODE;
100 if (file->dev == last_dev) flags |= SAME_DEV;
101 if (file->uid == last_uid) flags |= SAME_UID;
102 if (file->gid == last_gid) flags |= SAME_GID;
103 if (file->modtime == last_time) flags |= SAME_TIME;
105 for (l1=0;lastname[l1] && file->name[l1] == lastname[l1];l1++) ;
106 l2 = strlen(file->name) - l1;
108 if (l1 > 0) flags |= SAME_NAME;
109 if (l2 > 255) flags |= LONG_NAME;
112 if (flags & SAME_NAME)
114 if (flags & LONG_NAME)
118 write_buf(f,file->name+l1,l2);
120 write_int(f,(int)file->length);
121 if (!(flags & SAME_TIME))
122 write_int(f,(int)file->modtime);
123 if (!(flags & SAME_MODE))
124 write_int(f,(int)file->mode);
125 if (preserve_uid && !(flags & SAME_UID))
126 write_int(f,(int)file->uid);
127 if (preserve_gid && !(flags & SAME_GID))
128 write_int(f,(int)file->gid);
129 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_DEV))
130 write_int(f,(int)file->dev);
133 if (preserve_links && S_ISLNK(file->mode)) {
134 write_int(f,strlen(file->link));
135 write_buf(f,file->link,strlen(file->link));
139 if (always_checksum) {
140 write_buf(f,file->sum,SUM_LENGTH);
143 last_mode = file->mode;
144 last_dev = file->dev;
145 last_uid = file->uid;
146 last_gid = file->gid;
147 last_time = file->modtime;
149 strcpy(lastname,file->name);
155 void receive_file_entry_v11(struct file_struct *file,
156 unsigned char flags,int f)
158 static mode_t last_time=0;
159 static mode_t last_mode=0;
160 static dev_t last_dev=0;
161 static uid_t last_uid=0;
162 static gid_t last_gid=0;
163 static char lastname[MAXPATHLEN]="";
166 if (flags & SAME_NAME)
169 if (flags & LONG_NAME)
174 file->name = (char *)malloc(l1+l2+1);
175 if (!file->name) out_of_memory("receive_file_entry");
177 strncpy(file->name,lastname,l1);
178 read_buf(f,file->name+l1,l2);
179 file->name[l1+l2] = 0;
181 file->length = (off_t)read_int(f);
182 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
183 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
185 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
187 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
188 if (preserve_devices && IS_DEVICE(file->mode))
189 file->dev = (flags & SAME_DEV) ? last_dev : (dev_t)read_int(f);
192 if (preserve_links && S_ISLNK(file->mode)) {
194 file->link = (char *)malloc(l+1);
195 if (!file->link) out_of_memory("receive_file_entry");
196 read_buf(f,file->link,l);
202 read_buf(f,file->sum,SUM_LENGTH);
204 last_mode = file->mode;
205 last_dev = file->dev;
206 last_uid = file->uid;
207 last_gid = file->gid;
208 last_time = file->modtime;
210 strcpy(lastname,file->name);
216 static struct file_struct *make_file(int recurse,char *fname)
218 static struct file_struct file;
220 char sum[SUM_LENGTH];
222 bzero(sum,SUM_LENGTH);
224 if (lstat(fname,&st) != 0) {
225 fprintf(stderr,"%s: %s\n",
226 fname,strerror(errno));
230 if (S_ISDIR(st.st_mode) && !recurse) {
231 fprintf(stderr,"skipping directory %s\n",fname);
235 if (one_file_system && st.st_dev != filesystem_dev)
238 if (!match_file_name(fname,&st))
242 fprintf(stderr,"make_file(%s)\n",fname);
244 file.name = strdup(fname);
245 file.modtime = st.st_mtime;
246 file.length = st.st_size;
247 file.mode = st.st_mode;
248 file.uid = st.st_uid;
249 file.gid = st.st_gid;
251 file.dev = st.st_rdev;
255 if (S_ISLNK(st.st_mode)) {
257 char lnk[MAXPATHLEN];
258 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
259 fprintf(stderr,"readlink %s : %s\n",fname,strerror(errno));
263 file.link = strdup(lnk);
267 if (always_checksum && S_ISREG(st.st_mode)) {
268 file_checksum(fname,file.sum,st.st_size);
272 file.dir = strdup(flist_dir);
276 if (!S_ISDIR(st.st_mode))
277 total_size += st.st_size;
284 static void send_file_name(int f,struct file_list *flist,
285 int recurse,char *fname)
287 struct file_struct *file;
289 file = make_file(recurse,fname);
293 if (flist->count >= flist->malloced) {
294 flist->malloced += 100;
295 flist->files = (struct file_struct *)realloc(flist->files,
296 sizeof(flist->files[0])*
299 out_of_memory("send_file_name");
302 if (strcmp(file->name,".") && strcmp(file->name,"/")) {
303 flist->files[flist->count++] = *file;
304 send_file_entry(file,f);
307 if (S_ISDIR(file->mode) && recurse) {
308 char **last_exclude_list = local_exclude_list;
309 send_directory(f,flist,file->name);
310 local_exclude_list = last_exclude_list;
317 static void send_directory(int f,struct file_list *flist,char *dir)
321 char fname[MAXPATHLEN];
327 fprintf(stderr,"%s: %s\n",
328 dir,strerror(errno));
334 if (fname[l-1] != '/')
336 p = fname + strlen(fname);
339 strcpy(p,".cvsignore");
340 local_exclude_list = make_exclude_list(fname,NULL,0);
343 for (di=readdir(d); di; di=readdir(d)) {
344 if (strcmp(di->d_name,".")==0 ||
345 strcmp(di->d_name,"..")==0)
347 strcpy(p,di->d_name);
348 send_file_name(f,flist,1,fname);
356 struct file_list *send_file_list(int f,int recurse,int argc,char *argv[])
361 char dbuf[MAXPATHLEN];
362 struct file_list *flist;
364 if (verbose && recurse) {
365 fprintf(am_server?stderr:stdout,"building file list ... ");
366 fflush(am_server?stderr:stdout);
369 flist = (struct file_list *)malloc(sizeof(flist[0]));
370 if (!flist) out_of_memory("send_file_list");
373 flist->malloced = 100;
374 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
376 if (!flist->files) out_of_memory("send_file_list");
378 for (i=0;i<argc;i++) {
379 char fname2[MAXPATHLEN];
380 char *fname = fname2;
382 strcpy(fname,argv[i]);
385 if (l != 1 && fname[l-1] == '/') {
389 if (lstat(fname,&st) != 0) {
390 fprintf(stderr,"%s : %s\n",fname,strerror(errno));
394 if (S_ISDIR(st.st_mode) && !recurse) {
395 fprintf(stderr,"skipping directory %s\n",fname);
400 p = strrchr(fname,'/');
410 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
411 fprintf(stderr,"getwd : %s\n",strerror(errno));
414 if (chdir(dir) != 0) {
415 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
420 set_filesystem(fname);
421 send_file_name(f,flist,recurse,fname);
423 if (chdir(dbuf) != 0) {
424 fprintf(stderr,"chdir %s : %s\n",dbuf,strerror(errno));
431 set_filesystem(fname);
432 send_file_name(f,flist,recurse,fname);
436 send_file_entry(NULL,f);
442 if (verbose && recurse)
443 fprintf(am_server?stderr:stdout,"done\n");
449 struct file_list *recv_file_list(int f)
451 struct file_list *flist;
455 fprintf(stderr,"recv_file_list starting\n");
457 flist = (struct file_list *)malloc(sizeof(flist[0]));
463 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
469 for (flags=read_byte(f); flags; flags=read_byte(f)) {
470 int i = flist->count;
472 if (i >= flist->malloced) {
473 flist->malloced += 100;
474 flist->files =(struct file_struct *)realloc(flist->files,
475 sizeof(flist->files[0])*
481 receive_file_entry(&flist->files[i],flags,f);
483 if (S_ISREG(flist->files[i].mode))
484 total_size += flist->files[i].length;
489 fprintf(stderr,"recv_file_name(%s)\n",flist->files[i].name);
494 fprintf(stderr,"received %d names\n",flist->count);
501 out_of_memory("recv_file_list");
502 return NULL; /* not reached */
506 static int flist_compare(struct file_struct *f1,struct file_struct *f2)
508 if (!f1->name && !f2->name) return 0;
509 if (!f1->name) return -1;
510 if (!f2->name) return 1;
511 return strcmp(f1->name,f2->name);
515 int flist_find(struct file_list *flist,struct file_struct *f)
517 int low=0,high=flist->count;
519 while (low != high) {
520 int mid = (low+high)/2;
521 int ret = flist_compare(&flist->files[mid],f);
522 if (ret == 0) return mid;
528 if (flist_compare(&flist->files[low],f) == 0)
534 static void clean_fname(char *name)
545 if ((p=strstr(name,"/./"))) {
553 if ((p=strstr(name,"//"))) {
561 if (strncmp(p=name,"./",2) == 0) {
570 if (l > 1 && p[l-1] == '/') {
579 * This routine ensures we don't have any duplicate names in our file list.
580 * duplicate names can cause corruption because of the pipelining
582 void clean_flist(struct file_list *flist)
586 if (!flist || flist->count == 0)
589 for (i=0;i<flist->count;i++) {
590 clean_fname(flist->files[i].name);
593 qsort(flist->files,flist->count,
594 sizeof(flist->files[0]),
595 (int (*)())flist_compare);
597 for (i=1;i<flist->count;i++) {
598 if (flist->files[i].name &&
599 strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
600 if (verbose > 1 && !am_server)
601 fprintf(stderr,"removing duplicate name %s from file list\n",
602 flist->files[i].name);
603 free(flist->files[i-1].name);
604 flist->files[i-1].name = NULL;