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 = NULL;
50 static void clean_fname(char *name);
53 int link_stat(const char *Path, struct stat *Buffer)
57 return stat(Path, Buffer);
59 return lstat(Path, Buffer);
62 return stat(Path, Buffer);
67 This function is used to check if a file should be included/excluded
68 from the list of files based on its name and type etc
70 static int match_file_name(char *fname,struct stat *st)
72 if (check_exclude(fname,local_exclude_list)) {
74 fprintf(FERROR,"excluding file %s\n",fname);
80 /* used by the one_file_system code */
81 static dev_t filesystem_dev;
83 static void set_filesystem(char *fname)
86 if (link_stat(fname,&st) != 0) return;
87 filesystem_dev = st.st_dev;
91 static void send_directory(int f,struct file_list *flist,char *dir);
93 static char *flist_dir = NULL;
95 extern void (*send_file_entry)(struct file_struct *file,int f);
96 extern void (*receive_file_entry)(struct file_struct *file,
97 unsigned char flags,int f);
100 void send_file_entry_v11(struct file_struct *file,int f)
103 static time_t last_time=0;
104 static mode_t last_mode=0;
105 static dev_t last_rdev=0;
106 static uid_t last_uid=0;
107 static gid_t last_gid=0;
108 static char lastname[MAXPATHLEN]="";
120 if (file->mode == last_mode) flags |= SAME_MODE;
121 if (file->rdev == last_rdev) flags |= SAME_RDEV;
122 if (file->uid == last_uid) flags |= SAME_UID;
123 if (file->gid == last_gid) flags |= SAME_GID;
124 if (file->modtime == last_time) flags |= SAME_TIME;
126 for (l1=0;lastname[l1] && file->name[l1] == lastname[l1];l1++) ;
127 l2 = strlen(file->name) - l1;
129 if (l1 > 0) flags |= SAME_NAME;
130 if (l2 > 255) flags |= LONG_NAME;
133 if (flags & SAME_NAME)
135 if (flags & LONG_NAME)
139 write_buf(f,file->name+l1,l2);
141 write_int(f,(int)file->length);
142 if (!(flags & SAME_TIME))
143 write_int(f,(int)file->modtime);
144 if (!(flags & SAME_MODE))
145 write_int(f,(int)file->mode);
146 if (preserve_uid && !(flags & SAME_UID)) {
148 write_int(f,(int)file->uid);
150 if (preserve_gid && !(flags & SAME_GID)) {
152 write_int(f,(int)file->gid);
154 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
155 write_int(f,(int)file->rdev);
158 if (preserve_links && S_ISLNK(file->mode)) {
159 write_int(f,strlen(file->link));
160 write_buf(f,file->link,strlen(file->link));
164 #if SUPPORT_HARD_LINKS
165 if (preserve_hard_links && S_ISREG(file->mode)) {
166 write_int(f,file->dev);
167 write_int(f,file->inode);
171 if (always_checksum) {
172 write_buf(f,file->sum,csum_length);
175 last_mode = file->mode;
176 last_rdev = file->rdev;
177 last_uid = file->uid;
178 last_gid = file->gid;
179 last_time = file->modtime;
181 strncpy(lastname,file->name,MAXPATHLEN-1);
182 lastname[MAXPATHLEN-1] = 0;
187 void receive_file_entry_v11(struct file_struct *file,
188 unsigned char flags,int f)
190 static time_t last_time=0;
191 static mode_t last_mode=0;
192 static dev_t last_rdev=0;
193 static uid_t last_uid=0;
194 static gid_t last_gid=0;
195 static char lastname[MAXPATHLEN]="";
198 if (flags & SAME_NAME)
201 if (flags & LONG_NAME)
206 bzero((char *)file,sizeof(*file));
208 file->name = (char *)malloc(l1+l2+1);
209 if (!file->name) out_of_memory("receive_file_entry 1");
211 strncpy(file->name,lastname,l1);
212 read_buf(f,file->name+l1,l2);
213 file->name[l1+l2] = 0;
215 file->length = (off_t)read_int(f);
216 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
217 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
219 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
221 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
222 if (preserve_devices && IS_DEVICE(file->mode))
223 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
225 if (preserve_links && S_ISLNK(file->mode)) {
227 file->link = (char *)malloc(l+1);
228 if (!file->link) out_of_memory("receive_file_entry 2");
229 read_buf(f,file->link,l);
233 #if SUPPORT_HARD_LINKS
234 if (preserve_hard_links && S_ISREG(file->mode)) {
235 file->dev = read_int(f);
236 file->inode = read_int(f);
241 read_buf(f,file->sum,csum_length);
243 last_mode = file->mode;
244 last_rdev = file->rdev;
245 last_uid = file->uid;
246 last_gid = file->gid;
247 last_time = file->modtime;
249 strncpy(lastname,file->name,MAXPATHLEN-1);
250 lastname[MAXPATHLEN-1] = 0;
255 static struct file_struct *make_file(char *fname)
257 static struct file_struct file;
259 char sum[SUM_LENGTH];
261 bzero(sum,SUM_LENGTH);
263 if (link_stat(fname,&st) != 0) {
264 fprintf(FERROR,"%s: %s\n",
265 fname,strerror(errno));
269 if (S_ISDIR(st.st_mode) && !recurse) {
270 fprintf(FERROR,"skipping directory %s\n",fname);
274 if (one_file_system && st.st_dev != filesystem_dev)
277 if (!match_file_name(fname,&st))
281 fprintf(FERROR,"make_file(%s)\n",fname);
283 bzero((char *)&file,sizeof(file));
285 file.name = strdup(fname);
286 file.modtime = st.st_mtime;
287 file.length = st.st_size;
288 file.mode = st.st_mode;
289 file.uid = st.st_uid;
290 file.gid = st.st_gid;
291 file.dev = st.st_dev;
292 file.inode = st.st_ino;
294 file.rdev = st.st_rdev;
298 if (S_ISLNK(st.st_mode)) {
300 char lnk[MAXPATHLEN];
301 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
302 fprintf(FERROR,"readlink %s : %s\n",fname,strerror(errno));
306 file.link = strdup(lnk);
310 if (always_checksum && S_ISREG(st.st_mode)) {
311 file_checksum(fname,file.sum,st.st_size);
315 file.dir = strdup(flist_dir);
319 if (!S_ISDIR(st.st_mode))
320 total_size += st.st_size;
327 static void send_file_name(int f,struct file_list *flist,char *fname)
329 struct file_struct *file;
331 file = make_file(fname);
335 if (flist->count >= flist->malloced) {
336 if (flist->malloced < 100)
337 flist->malloced += 100;
339 flist->malloced *= 1.8;
340 flist->files = (struct file_struct *)realloc(flist->files,
341 sizeof(flist->files[0])*
344 out_of_memory("send_file_name");
347 if (strcmp(file->name,"/")) {
348 flist->files[flist->count++] = *file;
349 send_file_entry(file,f);
352 if (S_ISDIR(file->mode) && recurse) {
353 char **last_exclude_list = local_exclude_list;
354 send_directory(f,flist,file->name);
355 local_exclude_list = last_exclude_list;
362 static void send_directory(int f,struct file_list *flist,char *dir)
366 char fname[MAXPATHLEN];
372 fprintf(FERROR,"%s: %s\n",
373 dir,strerror(errno));
377 strncpy(fname,dir,MAXPATHLEN-1);
378 fname[MAXPATHLEN-1]=0;
380 if (fname[l-1] != '/') {
381 if (l == MAXPATHLEN-1) {
382 fprintf(FERROR,"skipping long-named directory %s\n",fname);
389 p = fname + strlen(fname);
392 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
393 strcpy(p,".cvsignore");
394 local_exclude_list = make_exclude_list(fname,NULL,0);
396 fprintf(FERROR,"cannot cvs-exclude in long-named directory %s\n",fname);
400 for (di=readdir(d); di; di=readdir(d)) {
401 if (strcmp(di->d_name,".")==0 ||
402 strcmp(di->d_name,"..")==0)
404 strncpy(p,di->d_name,MAXPATHLEN-(l+1));
405 send_file_name(f,flist,fname);
413 struct file_list *send_file_list(int f,int argc,char *argv[])
418 char dbuf[MAXPATHLEN];
419 struct file_list *flist;
421 if (verbose && recurse && !am_server && f != -1) {
422 fprintf(FINFO,"building file list ... ");
426 flist = (struct file_list *)malloc(sizeof(flist[0]));
427 if (!flist) out_of_memory("send_file_list");
430 flist->malloced = 100;
431 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
433 if (!flist->files) out_of_memory("send_file_list");
435 for (i=0;i<argc;i++) {
436 char fname2[MAXPATHLEN];
437 char *fname = fname2;
439 strncpy(fname,argv[i],MAXPATHLEN-1);
440 fname[MAXPATHLEN-1] = 0;
443 if (l != 1 && fname[l-1] == '/') {
447 if (link_stat(fname,&st) != 0) {
448 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
452 if (S_ISDIR(st.st_mode) && !recurse) {
453 fprintf(FERROR,"skipping directory %s\n",fname);
459 if (!relative_paths) {
460 p = strrchr(fname,'/');
475 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
476 fprintf(FERROR,"getwd : %s\n",strerror(errno));
479 if (chdir(dir) != 0) {
480 fprintf(FERROR,"chdir %s : %s\n",dir,strerror(errno));
485 set_filesystem(fname);
486 send_file_name(f,flist,fname);
488 if (chdir(dbuf) != 0) {
489 fprintf(FERROR,"chdir %s : %s\n",dbuf,strerror(errno));
496 set_filesystem(fname);
497 send_file_name(f,flist,fname);
501 send_file_entry(NULL,f);
505 if (verbose && recurse && !am_server && f != -1)
506 fprintf(FINFO,"done\n");
510 /* now send the uid/gid list. This was introduced in protocol version 15 */
511 if (f != -1 && remote_version >= 15) {
519 struct file_list *recv_file_list(int f)
521 struct file_list *flist;
524 if (verbose && recurse && !am_server) {
525 fprintf(FINFO,"receiving file list ... ");
529 flist = (struct file_list *)malloc(sizeof(flist[0]));
535 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
541 for (flags=read_byte(f); flags; flags=read_byte(f)) {
542 int i = flist->count;
544 if (i >= flist->malloced) {
545 if (flist->malloced < 100)
546 flist->malloced += 100;
548 flist->malloced *= 1.8;
549 flist->files =(struct file_struct *)realloc(flist->files,
550 sizeof(flist->files[0])*
556 receive_file_entry(&flist->files[i],flags,f);
558 if (S_ISREG(flist->files[i].mode))
559 total_size += flist->files[i].length;
564 fprintf(FERROR,"recv_file_name(%s)\n",flist->files[i].name);
569 fprintf(FERROR,"received %d names\n",flist->count);
573 if (verbose && recurse && !am_server) {
574 fprintf(FINFO,"done\n");
577 /* now recv the uid/gid list. This was introduced in protocol version 15 */
578 if (f != -1 && remote_version >= 15) {
579 recv_uid_list(f, flist);
585 out_of_memory("recv_file_list");
586 return NULL; /* not reached */
590 int file_compare(struct file_struct *f1,struct file_struct *f2)
592 if (!f1->name && !f2->name) return 0;
593 if (!f1->name) return -1;
594 if (!f2->name) return 1;
595 return strcmp(f1->name,f2->name);
599 int flist_find(struct file_list *flist,struct file_struct *f)
601 int low=0,high=flist->count-1;
603 if (flist->count <= 0) return -1;
605 while (low != high) {
606 int mid = (low+high)/2;
607 int ret = file_compare(&flist->files[flist_up(flist, mid)],f);
608 if (ret == 0) return flist_up(flist, mid);
616 if (file_compare(&flist->files[flist_up(flist,low)],f) == 0)
617 return flist_up(flist,low);
622 static void clean_fname(char *name)
633 if ((p=strstr(name,"/./"))) {
641 if ((p=strstr(name,"//"))) {
649 if (strncmp(p=name,"./",2) == 0) {
657 if (l > 1 && p[l-1] == '/') {
666 * This routine ensures we don't have any duplicate names in our file list.
667 * duplicate names can cause corruption because of the pipelining
669 void clean_flist(struct file_list *flist)
673 if (!flist || flist->count == 0)
676 for (i=0;i<flist->count;i++) {
677 clean_fname(flist->files[i].name);
680 qsort(flist->files,flist->count,
681 sizeof(flist->files[0]),
682 (int (*)())file_compare);
684 for (i=1;i<flist->count;i++) {
685 if (flist->files[i].name &&
686 strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
687 if (verbose > 1 && !am_server)
688 fprintf(FERROR,"removing duplicate name %s from file list %d\n",
689 flist->files[i-1].name,i-1);
690 free(flist->files[i-1].name);
691 bzero((char *)&flist->files[i-1],sizeof(flist->files[i-1]));