patch from Jim Meyering <meyering@eng.ascend.com>
[rsync/rsync.git] / flist.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
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.
9    
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.
14    
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.
18 */
19
20 /* generate and receive file lists */
21
22 #include "rsync.h"
23
24 extern int csum_length;
25
26 extern int verbose;
27 extern int am_server;
28 extern int always_checksum;
29 extern off_t total_size;
30
31 extern int cvs_exclude;
32
33 extern int recurse;
34
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
46 static char **local_exclude_list = NULL;
47
48 static void clean_fname(char *name);
49
50
51 /*
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
54  */
55 static int match_file_name(char *fname,struct stat *st)
56 {
57   if (check_exclude(fname,local_exclude_list)) {
58     if (verbose > 2)
59       fprintf(FERROR,"excluding file %s\n",fname);
60     return 0;
61   }
62   return 1;
63 }
64
65 /* used by the one_file_system code */
66 static dev_t filesystem_dev;
67
68 static void set_filesystem(char *fname)
69 {
70   struct stat st;
71   if (lstat(fname,&st) != 0) return;
72   filesystem_dev = st.st_dev;
73 }
74
75
76 static void send_directory(int f,struct file_list *flist,char *dir);
77
78 static char *flist_dir = NULL;
79
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);
83
84
85 void send_file_entry_v11(struct file_struct *file,int f)
86 {
87   unsigned char flags;
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]="";
94   int l1,l2;
95
96   if (f == -1) return;
97
98   if (!file) {
99     write_byte(f,0);
100     return;
101   }
102
103   flags = FILE_VALID;
104
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;
110
111   for (l1=0;lastname[l1] && file->name[l1] == lastname[l1];l1++) ;
112   l2 = strlen(file->name) - l1;
113
114   if (l1 > 0) flags |= SAME_NAME;
115   if (l2 > 255) flags |= LONG_NAME;
116
117   write_byte(f,flags);  
118   if (flags & SAME_NAME)
119     write_byte(f,l1);
120   if (flags & LONG_NAME)
121     write_int(f,l2);
122   else
123     write_byte(f,l2);
124   write_buf(f,file->name+l1,l2);
125
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);
137
138 #if SUPPORT_LINKS
139   if (preserve_links && S_ISLNK(file->mode)) {
140     write_int(f,strlen(file->link));
141     write_buf(f,file->link,strlen(file->link));
142   }
143 #endif
144
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);
149   }
150 #endif
151
152   if (always_checksum) {
153     write_buf(f,file->sum,csum_length);
154   }       
155
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;
161
162   strcpy(lastname,file->name);
163   lastname[255] = 0;
164 }
165
166
167
168 void receive_file_entry_v11(struct file_struct *file,
169                             unsigned char flags,int f)
170 {
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]="";
177   int l1=0,l2=0;
178
179   if (flags & SAME_NAME)
180     l1 = read_byte(f);
181   
182   if (flags & LONG_NAME)
183     l2 = read_int(f);
184   else
185     l2 = read_byte(f);
186
187   bzero((char *)file,sizeof(*file));
188
189   file->name = (char *)malloc(l1+l2+1);
190   if (!file->name) out_of_memory("receive_file_entry");
191
192   strncpy(file->name,lastname,l1);
193   read_buf(f,file->name+l1,l2);
194   file->name[l1+l2] = 0;
195
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);
199   if (preserve_uid)
200     file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
201   if (preserve_gid)
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);
205
206 #if SUPPORT_LINKS
207   if (preserve_links && S_ISLNK(file->mode)) {
208     int l = read_int(f);
209     file->link = (char *)malloc(l+1);
210     if (!file->link) out_of_memory("receive_file_entry");
211     read_buf(f,file->link,l);
212     file->link[l] = 0;
213   }
214 #endif
215
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);
220   }
221 #endif
222   
223   if (always_checksum)
224     read_buf(f,file->sum,csum_length);
225   
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;
231
232   strcpy(lastname,file->name);
233   lastname[255] = 0;
234 }
235
236
237
238 static struct file_struct *make_file(char *fname)
239 {
240   static struct file_struct file;
241   struct stat st;
242   char sum[SUM_LENGTH];
243
244   bzero(sum,SUM_LENGTH);
245
246   if (lstat(fname,&st) != 0) {
247     fprintf(FERROR,"%s: %s\n",
248             fname,strerror(errno));
249     return NULL;
250   }
251
252   if (S_ISDIR(st.st_mode) && !recurse) {
253     fprintf(FERROR,"skipping directory %s\n",fname);
254     return NULL;
255   }
256
257   if (one_file_system && st.st_dev != filesystem_dev)
258     return NULL;
259
260   if (!match_file_name(fname,&st))
261     return NULL;
262
263   if (verbose > 2)
264     fprintf(FERROR,"make_file(%s)\n",fname);
265
266   bzero((char *)&file,sizeof(file));
267
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;
276 #ifdef HAVE_ST_RDEV
277   file.rdev = st.st_rdev;
278 #endif
279
280 #if SUPPORT_LINKS
281   if (S_ISLNK(st.st_mode)) {
282     int l;
283     char lnk[MAXPATHLEN];
284     if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
285       fprintf(FERROR,"readlink %s : %s\n",fname,strerror(errno));
286       return NULL;
287     }
288     lnk[l] = 0;
289     file.link = strdup(lnk);
290   }
291 #endif
292
293   if (always_checksum && S_ISREG(st.st_mode)) {
294     file_checksum(fname,file.sum,st.st_size);
295   }       
296
297   if (flist_dir)
298     file.dir = strdup(flist_dir);
299   else
300     file.dir = NULL;
301
302   if (!S_ISDIR(st.st_mode))
303     total_size += st.st_size;
304
305   return &file;
306 }
307
308
309
310 static void send_file_name(int f,struct file_list *flist,char *fname)
311 {
312   struct file_struct *file;
313
314   file = make_file(fname);
315
316   if (!file) return;  
317   
318   if (flist->count >= flist->malloced) {
319           if (flist->malloced < 100)
320                   flist->malloced += 100;
321           else
322                   flist->malloced *= 1.8;
323           flist->files = (struct file_struct *)realloc(flist->files,
324                                                        sizeof(flist->files[0])*
325                                                        flist->malloced);
326           if (!flist->files)
327                   out_of_memory("send_file_name");
328   }
329
330   if (strcmp(file->name,"/")) {
331     flist->files[flist->count++] = *file;    
332     send_file_entry(file,f);
333   }
334
335   if (S_ISDIR(file->mode) && recurse) {
336     char **last_exclude_list = local_exclude_list;
337     send_directory(f,flist,file->name);
338     local_exclude_list = last_exclude_list;
339     return;
340   }
341 }
342
343
344
345 static void send_directory(int f,struct file_list *flist,char *dir)
346 {
347   DIR *d;
348   struct dirent *di;
349   char fname[MAXPATHLEN];
350   int l;
351   char *p;
352
353   d = opendir(dir);
354   if (!d) {
355     fprintf(FERROR,"%s: %s\n",
356             dir,strerror(errno));
357     return;
358   }
359
360   strcpy(fname,dir);
361   l = strlen(fname);
362   if (fname[l-1] != '/')
363     strcat(fname,"/");
364   p = fname + strlen(fname);
365
366   if (cvs_exclude) {
367     strcpy(p,".cvsignore");
368     local_exclude_list = make_exclude_list(fname,NULL,0);
369   }  
370
371   for (di=readdir(d); di; di=readdir(d)) {
372     if (strcmp(di->d_name,".")==0 ||
373         strcmp(di->d_name,"..")==0)
374       continue;
375     strcpy(p,di->d_name);
376     send_file_name(f,flist,fname);
377   }
378
379   closedir(d);
380 }
381
382
383
384 struct file_list *send_file_list(int f,int argc,char *argv[])
385 {
386   int i,l;
387   struct stat st;
388   char *p,*dir;
389   char dbuf[MAXPATHLEN];
390   struct file_list *flist;
391
392   if (verbose && recurse && !am_server) {
393     fprintf(FINFO,"building file list ... ");
394     fflush(FINFO);
395   }
396
397   flist = (struct file_list *)malloc(sizeof(flist[0]));
398   if (!flist) out_of_memory("send_file_list");
399
400   flist->count=0;
401   flist->malloced = 100;
402   flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
403                                               flist->malloced);
404   if (!flist->files) out_of_memory("send_file_list");
405
406   for (i=0;i<argc;i++) {
407     char fname2[MAXPATHLEN];
408     char *fname = fname2;
409
410     strcpy(fname,argv[i]);
411
412     l = strlen(fname);
413     if (l != 1 && fname[l-1] == '/') {
414       strcat(fname,".");
415     }
416
417     if (lstat(fname,&st) != 0) {
418       fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
419       continue;
420     }
421
422     if (S_ISDIR(st.st_mode) && !recurse) {
423       fprintf(FERROR,"skipping directory %s\n",fname);
424       continue;
425     }
426
427     dir = NULL;
428
429     if (!relative_paths) {
430             p = strrchr(fname,'/');
431             if (p) {
432                     *p = 0;
433                     if (p == fname) 
434                             dir = "/";
435                     else
436                             dir = fname;      
437                     fname = p+1;      
438             }
439     }
440
441     if (!*fname)
442       fname = ".";
443
444     if (dir && *dir) {
445       if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
446         fprintf(FERROR,"getwd : %s\n",strerror(errno));
447         exit_cleanup(1);
448       }
449       if (chdir(dir) != 0) {
450         fprintf(FERROR,"chdir %s : %s\n",dir,strerror(errno));
451         continue;
452       }
453       flist_dir = dir;
454       if (one_file_system)
455         set_filesystem(fname);
456       send_file_name(f,flist,fname);
457       flist_dir = NULL;
458       if (chdir(dbuf) != 0) {
459         fprintf(FERROR,"chdir %s : %s\n",dbuf,strerror(errno));
460         exit_cleanup(1);
461       }
462       continue;
463     }
464
465     if (one_file_system)
466       set_filesystem(fname);
467     send_file_name(f,flist,fname);
468   }
469
470   if (f != -1) {
471     send_file_entry(NULL,f);
472     write_flush(f);
473   }
474
475   if (verbose && recurse && !am_server)
476     fprintf(FINFO,"done\n");
477
478   clean_flist(flist);
479
480   return flist;
481 }
482
483
484 struct file_list *recv_file_list(int f)
485 {
486   struct file_list *flist;
487   unsigned char flags;
488
489   if (verbose && recurse && !am_server) {
490     fprintf(FINFO,"receiving file list ... ");
491     fflush(FINFO);
492   }
493
494   flist = (struct file_list *)malloc(sizeof(flist[0]));
495   if (!flist)
496     goto oom;
497
498   flist->count=0;
499   flist->malloced=100;
500   flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
501                                               flist->malloced);
502   if (!flist->files)
503     goto oom;
504
505
506   for (flags=read_byte(f); flags; flags=read_byte(f)) {
507     int i = flist->count;
508
509     if (i >= flist->malloced) {
510           if (flist->malloced < 100)
511                   flist->malloced += 100;
512           else
513                   flist->malloced *= 1.8;
514           flist->files =(struct file_struct *)realloc(flist->files,
515                                                       sizeof(flist->files[0])*
516                                                       flist->malloced);
517           if (!flist->files)
518                   goto oom;
519     }
520
521     receive_file_entry(&flist->files[i],flags,f);
522
523     if (S_ISREG(flist->files[i].mode))
524       total_size += flist->files[i].length;
525
526     flist->count++;
527
528     if (verbose > 2)
529       fprintf(FERROR,"recv_file_name(%s)\n",flist->files[i].name);
530   }
531
532
533   if (verbose > 2)
534     fprintf(FERROR,"received %d names\n",flist->count);
535
536   clean_flist(flist);
537
538   if (verbose && recurse && !am_server) {
539     fprintf(FINFO,"done\n");
540   }
541
542   return flist;
543
544 oom:
545     out_of_memory("recv_file_list");
546     return NULL; /* not reached */
547 }
548
549
550 int file_compare(struct file_struct *f1,struct file_struct *f2)
551 {
552   if (!f1->name && !f2->name) return 0;
553   if (!f1->name) return -1;
554   if (!f2->name) return 1;
555   return strcmp(f1->name,f2->name);
556 }
557
558
559 int flist_find(struct file_list *flist,struct file_struct *f)
560 {
561   int low=0,high=flist->count;
562
563   while (low != high) {
564     int mid = (low+high)/2;
565     int ret = file_compare(&flist->files[mid],f);
566     if (ret == 0) return mid;
567     if (ret > 0) 
568       high=mid;
569     else
570       low=mid+1;
571   }
572   if (file_compare(&flist->files[low],f) == 0)
573     return low;
574   return -1;
575 }
576
577
578 static void clean_fname(char *name)
579 {
580   char *p;
581   int l;
582   int modified = 1;
583
584   if (!name) return;
585
586   while (modified) {
587     modified = 0;
588
589     if ((p=strstr(name,"/./"))) {
590       modified = 1;
591       while (*p) {
592         p[0] = p[2];
593         p++;
594       }
595     }
596
597     if ((p=strstr(name,"//"))) {
598       modified = 1;
599       while (*p) {
600         p[0] = p[1];
601         p++;
602       }
603     }
604
605     if (strncmp(p=name,"./",2) == 0) {      
606       modified = 1;
607       while (*p) {
608         p[0] = p[2];
609         p++;
610       }
611     }
612
613     l = strlen(p=name);
614     if (l > 1 && p[l-1] == '/') {
615       modified = 1;
616       p[l-1] = 0;
617     }
618   }
619 }
620
621
622 /*
623  * This routine ensures we don't have any duplicate names in our file list.
624  * duplicate names can cause corruption because of the pipelining 
625  */
626 void clean_flist(struct file_list *flist)
627 {
628   int i;
629
630   if (!flist || flist->count == 0) 
631     return;
632   
633   for (i=0;i<flist->count;i++) {
634     clean_fname(flist->files[i].name);
635   }
636       
637   qsort(flist->files,flist->count,
638         sizeof(flist->files[0]),
639         (int (*)())file_compare);
640
641   for (i=1;i<flist->count;i++) {
642     if (flist->files[i].name &&
643         strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
644       if (verbose > 1 && !am_server)
645         fprintf(FERROR,"removing duplicate name %s from file list %d\n",
646                 flist->files[i-1].name,i-1);
647       free(flist->files[i-1].name);
648       bzero((char *)&flist->files[i-1],sizeof(flist->files[i-1]));
649     } 
650   }
651 }
652