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