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;
33 extern int one_file_system;
34 extern int make_backups;
35 extern int preserve_links;
36 extern int preserve_perms;
37 extern int preserve_devices;
38 extern int preserve_uid;
39 extern int preserve_gid;
40 extern int preserve_times;
42 static char **local_exclude_list = NULL;
44 static void clean_fname(char *name);
48 This function is used to check if a file should be included/excluded
49 from the list of files based on its name and type etc
51 static int match_file_name(char *fname,struct stat *st)
53 if (check_exclude(fname,local_exclude_list)) {
55 fprintf(stderr,"excluding file %s\n",fname);
61 /* used by the one_file_system code */
62 static dev_t filesystem_dev;
64 static void set_filesystem(char *fname)
67 if (lstat(fname,&st) != 0) return;
68 filesystem_dev = st.st_dev;
72 static void send_directory(int f,struct file_list *flist,char *dir);
74 static char *flist_dir = NULL;
76 extern void (*send_file_entry)(struct file_struct *file,int f);
77 extern void (*receive_file_entry)(struct file_struct *file,
78 unsigned char flags,int f);
81 void send_file_entry_v11(struct file_struct *file,int f)
84 static time_t last_time=0;
85 static mode_t last_mode=0;
86 static dev_t last_dev=0;
87 static uid_t last_uid=0;
88 static gid_t last_gid=0;
89 static char lastname[MAXPATHLEN]="";
101 if (file->mode == last_mode) flags |= SAME_MODE;
102 if (file->dev == last_dev) flags |= SAME_DEV;
103 if (file->uid == last_uid) flags |= SAME_UID;
104 if (file->gid == last_gid) flags |= SAME_GID;
105 if (file->modtime == last_time) flags |= SAME_TIME;
107 for (l1=0;lastname[l1] && file->name[l1] == lastname[l1];l1++) ;
108 l2 = strlen(file->name) - l1;
110 if (l1 > 0) flags |= SAME_NAME;
111 if (l2 > 255) flags |= LONG_NAME;
114 if (flags & SAME_NAME)
116 if (flags & LONG_NAME)
120 write_buf(f,file->name+l1,l2);
122 write_int(f,(int)file->length);
123 if (!(flags & SAME_TIME))
124 write_int(f,(int)file->modtime);
125 if (!(flags & SAME_MODE))
126 write_int(f,(int)file->mode);
127 if (preserve_uid && !(flags & SAME_UID))
128 write_int(f,(int)file->uid);
129 if (preserve_gid && !(flags & SAME_GID))
130 write_int(f,(int)file->gid);
131 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_DEV))
132 write_int(f,(int)file->dev);
135 if (preserve_links && S_ISLNK(file->mode)) {
136 write_int(f,strlen(file->link));
137 write_buf(f,file->link,strlen(file->link));
141 if (always_checksum) {
142 write_buf(f,file->sum,csum_length);
145 last_mode = file->mode;
146 last_dev = file->dev;
147 last_uid = file->uid;
148 last_gid = file->gid;
149 last_time = file->modtime;
151 strcpy(lastname,file->name);
157 void receive_file_entry_v11(struct file_struct *file,
158 unsigned char flags,int f)
160 static mode_t last_time=0;
161 static mode_t last_mode=0;
162 static dev_t last_dev=0;
163 static uid_t last_uid=0;
164 static gid_t last_gid=0;
165 static char lastname[MAXPATHLEN]="";
168 if (flags & SAME_NAME)
171 if (flags & LONG_NAME)
176 file->name = (char *)malloc(l1+l2+1);
177 if (!file->name) out_of_memory("receive_file_entry");
179 strncpy(file->name,lastname,l1);
180 read_buf(f,file->name+l1,l2);
181 file->name[l1+l2] = 0;
183 file->length = (off_t)read_int(f);
184 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
185 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
187 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
189 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
190 if (preserve_devices && IS_DEVICE(file->mode))
191 file->dev = (flags & SAME_DEV) ? last_dev : (dev_t)read_int(f);
194 if (preserve_links && S_ISLNK(file->mode)) {
196 file->link = (char *)malloc(l+1);
197 if (!file->link) out_of_memory("receive_file_entry");
198 read_buf(f,file->link,l);
204 read_buf(f,file->sum,csum_length);
206 last_mode = file->mode;
207 last_dev = file->dev;
208 last_uid = file->uid;
209 last_gid = file->gid;
210 last_time = file->modtime;
212 strcpy(lastname,file->name);
218 static struct file_struct *make_file(int recurse,char *fname)
220 static struct file_struct file;
222 char sum[SUM_LENGTH];
224 bzero(sum,SUM_LENGTH);
226 if (lstat(fname,&st) != 0) {
227 fprintf(stderr,"%s: %s\n",
228 fname,strerror(errno));
232 if (S_ISDIR(st.st_mode) && !recurse) {
233 fprintf(stderr,"skipping directory %s\n",fname);
237 if (one_file_system && st.st_dev != filesystem_dev)
240 if (!match_file_name(fname,&st))
244 fprintf(stderr,"make_file(%s)\n",fname);
246 file.name = strdup(fname);
247 file.modtime = st.st_mtime;
248 file.length = st.st_size;
249 file.mode = st.st_mode;
250 file.uid = st.st_uid;
251 file.gid = st.st_gid;
253 file.dev = st.st_rdev;
257 if (S_ISLNK(st.st_mode)) {
259 char lnk[MAXPATHLEN];
260 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
261 fprintf(stderr,"readlink %s : %s\n",fname,strerror(errno));
265 file.link = strdup(lnk);
269 if (always_checksum && S_ISREG(st.st_mode)) {
270 file_checksum(fname,file.sum,st.st_size);
274 file.dir = strdup(flist_dir);
278 if (!S_ISDIR(st.st_mode))
279 total_size += st.st_size;
286 static void send_file_name(int f,struct file_list *flist,
287 int recurse,char *fname)
289 struct file_struct *file;
291 file = make_file(recurse,fname);
295 if (flist->count >= flist->malloced) {
296 flist->malloced += 100;
297 flist->files = (struct file_struct *)realloc(flist->files,
298 sizeof(flist->files[0])*
301 out_of_memory("send_file_name");
304 if (strcmp(file->name,".") && strcmp(file->name,"/")) {
305 flist->files[flist->count++] = *file;
306 send_file_entry(file,f);
309 if (S_ISDIR(file->mode) && recurse) {
310 char **last_exclude_list = local_exclude_list;
311 send_directory(f,flist,file->name);
312 local_exclude_list = last_exclude_list;
319 static void send_directory(int f,struct file_list *flist,char *dir)
323 char fname[MAXPATHLEN];
329 fprintf(stderr,"%s: %s\n",
330 dir,strerror(errno));
336 if (fname[l-1] != '/')
338 p = fname + strlen(fname);
341 strcpy(p,".cvsignore");
342 local_exclude_list = make_exclude_list(fname,NULL,0);
345 for (di=readdir(d); di; di=readdir(d)) {
346 if (strcmp(di->d_name,".")==0 ||
347 strcmp(di->d_name,"..")==0)
349 strcpy(p,di->d_name);
350 send_file_name(f,flist,1,fname);
358 struct file_list *send_file_list(int f,int recurse,int argc,char *argv[])
363 char dbuf[MAXPATHLEN];
364 struct file_list *flist;
366 if (verbose && recurse) {
367 fprintf(am_server?stderr:stdout,"building file list ... ");
368 fflush(am_server?stderr:stdout);
371 flist = (struct file_list *)malloc(sizeof(flist[0]));
372 if (!flist) out_of_memory("send_file_list");
375 flist->malloced = 100;
376 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
378 if (!flist->files) out_of_memory("send_file_list");
380 for (i=0;i<argc;i++) {
381 char fname2[MAXPATHLEN];
382 char *fname = fname2;
384 strcpy(fname,argv[i]);
387 if (l != 1 && fname[l-1] == '/') {
391 if (lstat(fname,&st) != 0) {
392 fprintf(stderr,"%s : %s\n",fname,strerror(errno));
396 if (S_ISDIR(st.st_mode) && !recurse) {
397 fprintf(stderr,"skipping directory %s\n",fname);
402 p = strrchr(fname,'/');
412 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
413 fprintf(stderr,"getwd : %s\n",strerror(errno));
416 if (chdir(dir) != 0) {
417 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
422 set_filesystem(fname);
423 send_file_name(f,flist,recurse,fname);
425 if (chdir(dbuf) != 0) {
426 fprintf(stderr,"chdir %s : %s\n",dbuf,strerror(errno));
433 set_filesystem(fname);
434 send_file_name(f,flist,recurse,fname);
438 send_file_entry(NULL,f);
444 if (verbose && recurse)
445 fprintf(am_server?stderr:stdout,"done\n");
451 struct file_list *recv_file_list(int f)
453 struct file_list *flist;
457 fprintf(stderr,"recv_file_list starting\n");
459 flist = (struct file_list *)malloc(sizeof(flist[0]));
465 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
471 for (flags=read_byte(f); flags; flags=read_byte(f)) {
472 int i = flist->count;
474 if (i >= flist->malloced) {
475 flist->malloced += 100;
476 flist->files =(struct file_struct *)realloc(flist->files,
477 sizeof(flist->files[0])*
483 receive_file_entry(&flist->files[i],flags,f);
485 if (S_ISREG(flist->files[i].mode))
486 total_size += flist->files[i].length;
491 fprintf(stderr,"recv_file_name(%s)\n",flist->files[i].name);
496 fprintf(stderr,"received %d names\n",flist->count);
503 out_of_memory("recv_file_list");
504 return NULL; /* not reached */
508 static int flist_compare(struct file_struct *f1,struct file_struct *f2)
510 if (!f1->name && !f2->name) return 0;
511 if (!f1->name) return -1;
512 if (!f2->name) return 1;
513 return strcmp(f1->name,f2->name);
517 int flist_find(struct file_list *flist,struct file_struct *f)
519 int low=0,high=flist->count;
521 while (low != high) {
522 int mid = (low+high)/2;
523 int ret = flist_compare(&flist->files[mid],f);
524 if (ret == 0) return mid;
530 if (flist_compare(&flist->files[low],f) == 0)
536 static void clean_fname(char *name)
547 if ((p=strstr(name,"/./"))) {
555 if ((p=strstr(name,"//"))) {
563 if (strncmp(p=name,"./",2) == 0) {
572 if (l > 1 && p[l-1] == '/') {
581 * This routine ensures we don't have any duplicate names in our file list.
582 * duplicate names can cause corruption because of the pipelining
584 void clean_flist(struct file_list *flist)
588 if (!flist || flist->count == 0)
591 for (i=0;i<flist->count;i++) {
592 clean_fname(flist->files[i].name);
595 qsort(flist->files,flist->count,
596 sizeof(flist->files[0]),
597 (int (*)())flist_compare);
599 for (i=1;i<flist->count;i++) {
600 if (flist->files[i].name &&
601 strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
602 if (verbose > 1 && !am_server)
603 fprintf(stderr,"removing duplicate name %s from file list\n",
604 flist->files[i].name);
605 free(flist->files[i-1].name);
606 flist->files[i-1].name = NULL;