*** empty log message ***
[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 extern int copy_links;
46
47 static char **local_exclude_list = NULL;
48
49 static void clean_fname(char *name);
50
51
52 int link_stat(const char *Path, struct stat *Buffer) 
53 {
54 #if SUPPORT_LINKS
55     if (copy_links) {
56         return stat(Path, Buffer);
57     } else {
58         return lstat(Path, Buffer);
59     }
60 #else
61     return stat(Path, Buffer);
62 #endif
63 }
64
65 /*
66   This function is used to check if a file should be included/excluded
67   from the list of files based on its name and type etc
68  */
69 static int match_file_name(char *fname,struct stat *st)
70 {
71   if (check_exclude(fname,local_exclude_list)) {
72     if (verbose > 2)
73       fprintf(FERROR,"excluding file %s\n",fname);
74     return 0;
75   }
76   return 1;
77 }
78
79 /* used by the one_file_system code */
80 static dev_t filesystem_dev;
81
82 static void set_filesystem(char *fname)
83 {
84   struct stat st;
85   if (link_stat(fname,&st) != 0) return;
86   filesystem_dev = st.st_dev;
87 }
88
89
90 static void send_directory(int f,struct file_list *flist,char *dir);
91
92 static char *flist_dir = NULL;
93
94 extern void (*send_file_entry)(struct file_struct *file,int f);
95 extern void (*receive_file_entry)(struct file_struct *file,
96                                   unsigned char flags,int f);
97
98
99 void send_file_entry_v11(struct file_struct *file,int f)
100 {
101   unsigned char flags;
102   static time_t last_time=0;
103   static mode_t last_mode=0;
104   static dev_t last_rdev=0;
105   static uid_t last_uid=0;
106   static gid_t last_gid=0;
107   static char lastname[MAXPATHLEN]="";
108   int l1,l2;
109
110   if (f == -1) return;
111
112   if (!file) {
113     write_byte(f,0);
114     return;
115   }
116
117   flags = FILE_VALID;
118
119   if (file->mode == last_mode) flags |= SAME_MODE;
120   if (file->rdev == last_rdev) flags |= SAME_RDEV;
121   if (file->uid == last_uid) flags |= SAME_UID;
122   if (file->gid == last_gid) flags |= SAME_GID;
123   if (file->modtime == last_time) flags |= SAME_TIME;
124
125   for (l1=0;lastname[l1] && file->name[l1] == lastname[l1];l1++) ;
126   l2 = strlen(file->name) - l1;
127
128   if (l1 > 0) flags |= SAME_NAME;
129   if (l2 > 255) flags |= LONG_NAME;
130
131   write_byte(f,flags);  
132   if (flags & SAME_NAME)
133     write_byte(f,l1);
134   if (flags & LONG_NAME)
135     write_int(f,l2);
136   else
137     write_byte(f,l2);
138   write_buf(f,file->name+l1,l2);
139
140   write_int(f,(int)file->length);
141   if (!(flags & SAME_TIME))
142     write_int(f,(int)file->modtime);
143   if (!(flags & SAME_MODE))
144     write_int(f,(int)file->mode);
145   if (preserve_uid && !(flags & SAME_UID))
146     write_int(f,(int)file->uid);
147   if (preserve_gid && !(flags & SAME_GID))
148     write_int(f,(int)file->gid);
149   if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
150     write_int(f,(int)file->rdev);
151
152 #if SUPPORT_LINKS
153   if (preserve_links && S_ISLNK(file->mode)) {
154     write_int(f,strlen(file->link));
155     write_buf(f,file->link,strlen(file->link));
156   }
157 #endif
158
159 #if SUPPORT_HARD_LINKS
160   if (preserve_hard_links && S_ISREG(file->mode)) {
161     write_int(f,file->dev);
162     write_int(f,file->inode);
163   }
164 #endif
165
166   if (always_checksum) {
167     write_buf(f,file->sum,csum_length);
168   }       
169
170   last_mode = file->mode;
171   last_rdev = file->rdev;
172   last_uid = file->uid;
173   last_gid = file->gid;
174   last_time = file->modtime;
175
176   strncpy(lastname,file->name,MAXPATHLEN-1);
177   lastname[MAXPATHLEN-1] = 0;
178 }
179
180
181
182 void receive_file_entry_v11(struct file_struct *file,
183                             unsigned char flags,int f)
184 {
185   static time_t last_time=0;
186   static mode_t last_mode=0;
187   static dev_t last_rdev=0;
188   static uid_t last_uid=0;
189   static gid_t last_gid=0;
190   static char lastname[MAXPATHLEN]="";
191   int l1=0,l2=0;
192
193   if (flags & SAME_NAME)
194     l1 = read_byte(f);
195   
196   if (flags & LONG_NAME)
197     l2 = read_int(f);
198   else
199     l2 = read_byte(f);
200
201   bzero((char *)file,sizeof(*file));
202
203   file->name = (char *)malloc(l1+l2+1);
204   if (!file->name) out_of_memory("receive_file_entry 1");
205
206   strncpy(file->name,lastname,l1);
207   read_buf(f,file->name+l1,l2);
208   file->name[l1+l2] = 0;
209
210   file->length = (off_t)read_int(f);
211   file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
212   file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
213   if (preserve_uid)
214     file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
215   if (preserve_gid)
216     file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
217   if (preserve_devices && IS_DEVICE(file->mode))
218     file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
219
220   if (preserve_links && S_ISLNK(file->mode)) {
221     int l = read_int(f);
222     file->link = (char *)malloc(l+1);
223     if (!file->link) out_of_memory("receive_file_entry 2");
224     read_buf(f,file->link,l);
225     file->link[l] = 0;
226   }
227
228 #if SUPPORT_HARD_LINKS
229   if (preserve_hard_links && S_ISREG(file->mode)) {
230     file->dev = read_int(f);
231     file->inode = read_int(f);
232   }
233 #endif
234   
235   if (always_checksum)
236     read_buf(f,file->sum,csum_length);
237   
238   last_mode = file->mode;
239   last_rdev = file->rdev;
240   last_uid = file->uid;
241   last_gid = file->gid;
242   last_time = file->modtime;
243
244   strncpy(lastname,file->name,MAXPATHLEN-1);
245   lastname[MAXPATHLEN-1] = 0;
246 }
247
248
249
250 static struct file_struct *make_file(char *fname)
251 {
252   static struct file_struct file;
253   struct stat st;
254   char sum[SUM_LENGTH];
255
256   bzero(sum,SUM_LENGTH);
257
258   if (link_stat(fname,&st) != 0) {
259     fprintf(FERROR,"%s: %s\n",
260             fname,strerror(errno));
261     return NULL;
262   }
263
264   if (S_ISDIR(st.st_mode) && !recurse) {
265     fprintf(FERROR,"skipping directory %s\n",fname);
266     return NULL;
267   }
268
269   if (one_file_system && st.st_dev != filesystem_dev)
270     return NULL;
271
272   if (!match_file_name(fname,&st))
273     return NULL;
274
275   if (verbose > 2)
276     fprintf(FERROR,"make_file(%s)\n",fname);
277
278   bzero((char *)&file,sizeof(file));
279
280   file.name = strdup(fname);
281   file.modtime = st.st_mtime;
282   file.length = st.st_size;
283   file.mode = st.st_mode;
284   file.uid = st.st_uid;
285   file.gid = st.st_gid;
286   file.dev = st.st_dev;
287   file.inode = st.st_ino;
288 #ifdef HAVE_ST_RDEV
289   file.rdev = st.st_rdev;
290 #endif
291
292 #if SUPPORT_LINKS
293   if (S_ISLNK(st.st_mode)) {
294     int l;
295     char lnk[MAXPATHLEN];
296     if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
297       fprintf(FERROR,"readlink %s : %s\n",fname,strerror(errno));
298       return NULL;
299     }
300     lnk[l] = 0;
301     file.link = strdup(lnk);
302   }
303 #endif
304
305   if (always_checksum && S_ISREG(st.st_mode)) {
306     file_checksum(fname,file.sum,st.st_size);
307   }       
308
309   if (flist_dir)
310     file.dir = strdup(flist_dir);
311   else
312     file.dir = NULL;
313
314   if (!S_ISDIR(st.st_mode))
315     total_size += st.st_size;
316
317   return &file;
318 }
319
320
321
322 static void send_file_name(int f,struct file_list *flist,char *fname)
323 {
324   struct file_struct *file;
325
326   file = make_file(fname);
327
328   if (!file) return;  
329   
330   if (flist->count >= flist->malloced) {
331           if (flist->malloced < 100)
332                   flist->malloced += 100;
333           else
334                   flist->malloced *= 1.8;
335           flist->files = (struct file_struct *)realloc(flist->files,
336                                                        sizeof(flist->files[0])*
337                                                        flist->malloced);
338           if (!flist->files)
339                   out_of_memory("send_file_name");
340   }
341
342   if (strcmp(file->name,"/")) {
343     flist->files[flist->count++] = *file;    
344     send_file_entry(file,f);
345   }
346
347   if (S_ISDIR(file->mode) && recurse) {
348     char **last_exclude_list = local_exclude_list;
349     send_directory(f,flist,file->name);
350     local_exclude_list = last_exclude_list;
351     return;
352   }
353 }
354
355
356
357 static void send_directory(int f,struct file_list *flist,char *dir)
358 {
359   DIR *d;
360   struct dirent *di;
361   char fname[MAXPATHLEN];
362   int l;
363   char *p;
364
365   d = opendir(dir);
366   if (!d) {
367     fprintf(FERROR,"%s: %s\n",
368             dir,strerror(errno));
369     return;
370   }
371
372   strncpy(fname,dir,MAXPATHLEN-1);
373   fname[MAXPATHLEN-1]=0;
374   l = strlen(fname);
375   if (fname[l-1] != '/') {
376           strcat(fname,"/");
377           l++;
378   }
379   p = fname + strlen(fname);
380
381   if (cvs_exclude) {
382     strcpy(p,".cvsignore");
383     local_exclude_list = make_exclude_list(fname,NULL,0);
384   }  
385
386   for (di=readdir(d); di; di=readdir(d)) {
387     if (strcmp(di->d_name,".")==0 ||
388         strcmp(di->d_name,"..")==0)
389       continue;
390     strncpy(p,di->d_name,MAXPATHLEN-(l+1));
391     send_file_name(f,flist,fname);
392   }
393
394   closedir(d);
395 }
396
397
398
399 struct file_list *send_file_list(int f,int argc,char *argv[])
400 {
401   int i,l;
402   struct stat st;
403   char *p,*dir;
404   char dbuf[MAXPATHLEN];
405   struct file_list *flist;
406
407   if (verbose && recurse && !am_server) {
408     fprintf(FINFO,"building file list ... ");
409     fflush(FINFO);
410   }
411
412   flist = (struct file_list *)malloc(sizeof(flist[0]));
413   if (!flist) out_of_memory("send_file_list");
414
415   flist->count=0;
416   flist->malloced = 100;
417   flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
418                                               flist->malloced);
419   if (!flist->files) out_of_memory("send_file_list");
420
421   for (i=0;i<argc;i++) {
422     char fname2[MAXPATHLEN];
423     char *fname = fname2;
424
425     strncpy(fname,argv[i],MAXPATHLEN-1);
426     fname[MAXPATHLEN-1] = 0;
427
428     l = strlen(fname);
429     if (l != 1 && fname[l-1] == '/') {
430       strcat(fname,".");
431     }
432
433     if (link_stat(fname,&st) != 0) {
434       fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
435       continue;
436     }
437
438     if (S_ISDIR(st.st_mode) && !recurse) {
439       fprintf(FERROR,"skipping directory %s\n",fname);
440       continue;
441     }
442
443     dir = NULL;
444
445     if (!relative_paths) {
446             p = strrchr(fname,'/');
447             if (p) {
448                     *p = 0;
449                     if (p == fname) 
450                             dir = "/";
451                     else
452                             dir = fname;      
453                     fname = p+1;      
454             }
455     }
456
457     if (!*fname)
458       fname = ".";
459
460     if (dir && *dir) {
461       if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
462         fprintf(FERROR,"getwd : %s\n",strerror(errno));
463         exit_cleanup(1);
464       }
465       if (chdir(dir) != 0) {
466         fprintf(FERROR,"chdir %s : %s\n",dir,strerror(errno));
467         continue;
468       }
469       flist_dir = dir;
470       if (one_file_system)
471         set_filesystem(fname);
472       send_file_name(f,flist,fname);
473       flist_dir = NULL;
474       if (chdir(dbuf) != 0) {
475         fprintf(FERROR,"chdir %s : %s\n",dbuf,strerror(errno));
476         exit_cleanup(1);
477       }
478       continue;
479     }
480
481     if (one_file_system)
482       set_filesystem(fname);
483     send_file_name(f,flist,fname);
484   }
485
486   if (f != -1) {
487     send_file_entry(NULL,f);
488     write_flush(f);
489   }
490
491   if (verbose && recurse && !am_server)
492     fprintf(FINFO,"done\n");
493
494   clean_flist(flist);
495
496   return flist;
497 }
498
499
500 struct file_list *recv_file_list(int f)
501 {
502   struct file_list *flist;
503   unsigned char flags;
504
505   if (verbose && recurse && !am_server) {
506     fprintf(FINFO,"receiving file list ... ");
507     fflush(FINFO);
508   }
509
510   flist = (struct file_list *)malloc(sizeof(flist[0]));
511   if (!flist)
512     goto oom;
513
514   flist->count=0;
515   flist->malloced=100;
516   flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
517                                               flist->malloced);
518   if (!flist->files)
519     goto oom;
520
521
522   for (flags=read_byte(f); flags; flags=read_byte(f)) {
523     int i = flist->count;
524
525     if (i >= flist->malloced) {
526           if (flist->malloced < 100)
527                   flist->malloced += 100;
528           else
529                   flist->malloced *= 1.8;
530           flist->files =(struct file_struct *)realloc(flist->files,
531                                                       sizeof(flist->files[0])*
532                                                       flist->malloced);
533           if (!flist->files)
534                   goto oom;
535     }
536
537     receive_file_entry(&flist->files[i],flags,f);
538
539     if (S_ISREG(flist->files[i].mode))
540       total_size += flist->files[i].length;
541
542     flist->count++;
543
544     if (verbose > 2)
545       fprintf(FERROR,"recv_file_name(%s)\n",flist->files[i].name);
546   }
547
548
549   if (verbose > 2)
550     fprintf(FERROR,"received %d names\n",flist->count);
551
552   clean_flist(flist);
553
554   if (verbose && recurse && !am_server) {
555     fprintf(FINFO,"done\n");
556   }
557
558   return flist;
559
560 oom:
561     out_of_memory("recv_file_list");
562     return NULL; /* not reached */
563 }
564
565
566 int file_compare(struct file_struct *f1,struct file_struct *f2)
567 {
568   if (!f1->name && !f2->name) return 0;
569   if (!f1->name) return -1;
570   if (!f2->name) return 1;
571   return strcmp(f1->name,f2->name);
572 }
573
574
575 /* we need this function because of the silly way in which duplicate
576    entries are handled in the file lists - we can't change this
577    without breaking existing versions */
578 static int flist_up(struct file_list *flist, int i)
579 {
580         while (!flist->files[i].name) i++;
581         return i;
582 }
583
584
585 int flist_find(struct file_list *flist,struct file_struct *f)
586 {
587         int low=0,high=flist->count-1;
588
589         if (flist->count <= 0) return -1;
590
591         while (low != high) {
592                 int mid = (low+high)/2;
593                 int ret = file_compare(&flist->files[flist_up(flist, mid)],f);
594                 if (ret == 0) return flist_up(flist, mid);
595                 if (ret > 0) {
596                         high=mid;
597                 } else {
598                         low=mid+1;
599                 }
600         }
601
602         if (file_compare(&flist->files[flist_up(flist,low)],f) == 0)
603                 return flist_up(flist,low);
604         return -1;
605 }
606
607
608 static void clean_fname(char *name)
609 {
610   char *p;
611   int l;
612   int modified = 1;
613
614   if (!name) return;
615
616   while (modified) {
617     modified = 0;
618
619     if ((p=strstr(name,"/./"))) {
620       modified = 1;
621       while (*p) {
622         p[0] = p[2];
623         p++;
624       }
625     }
626
627     if ((p=strstr(name,"//"))) {
628       modified = 1;
629       while (*p) {
630         p[0] = p[1];
631         p++;
632       }
633     }
634
635     if (strncmp(p=name,"./",2) == 0) {      
636       modified = 1;
637       while (*p) {
638         p[0] = p[2];
639         p++;
640       }
641     }
642
643     l = strlen(p=name);
644     if (l > 1 && p[l-1] == '/') {
645       modified = 1;
646       p[l-1] = 0;
647     }
648   }
649 }
650
651
652 /*
653  * This routine ensures we don't have any duplicate names in our file list.
654  * duplicate names can cause corruption because of the pipelining 
655  */
656 void clean_flist(struct file_list *flist)
657 {
658   int i;
659
660   if (!flist || flist->count == 0) 
661     return;
662   
663   for (i=0;i<flist->count;i++) {
664     clean_fname(flist->files[i].name);
665   }
666       
667   qsort(flist->files,flist->count,
668         sizeof(flist->files[0]),
669         (int (*)())file_compare);
670
671   for (i=1;i<flist->count;i++) {
672     if (flist->files[i].name &&
673         strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
674       if (verbose > 1 && !am_server)
675         fprintf(FERROR,"removing duplicate name %s from file list %d\n",
676                 flist->files[i-1].name,i-1);
677       free(flist->files[i-1].name);
678       bzero((char *)&flist->files[i-1],sizeof(flist->files[i-1]));
679     } 
680   }
681 }
682