add the dummy file to .cvsignore
[rsync/rsync.git] / flist.c
... / ...
CommitLineData
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
24extern int csum_length;
25
26extern int verbose;
27extern int am_server;
28extern int always_checksum;
29extern int64 total_size;
30
31extern int cvs_exclude;
32
33extern int recurse;
34
35extern int one_file_system;
36extern int make_backups;
37extern int preserve_links;
38extern int preserve_hard_links;
39extern int preserve_perms;
40extern int preserve_devices;
41extern int preserve_uid;
42extern int preserve_gid;
43extern int preserve_times;
44extern int relative_paths;
45extern int copy_links;
46extern int remote_version;
47
48static char **local_exclude_list;
49
50int 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 */
67static 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 */
78static dev_t filesystem_dev;
79
80static 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
88static void send_directory(int f,struct file_list *flist,char *dir);
89
90static char *flist_dir;
91
92static 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
136void send_file_entry(struct file_struct *file,int f)
137{
138 unsigned char flags;
139 static time_t last_time;
140 static mode_t last_mode;
141 static dev_t last_rdev;
142 static uid_t last_uid;
143 static gid_t last_gid;
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_longint(f,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,(int)file->dev);
206 write_int(f,(int)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
226void receive_file_entry(struct file_struct **fptr,
227 unsigned char flags,int f)
228{
229 static time_t last_time;
230 static mode_t last_mode;
231 static dev_t last_rdev;
232 static uid_t last_uid;
233 static gid_t last_gid;
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 = read_longint(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
320static 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
426static void send_file_name(int f,struct file_list *flist,char *fname,
427 int recursive)
428{
429 struct file_struct *file;
430
431 file = make_file(fname);
432
433 if (!file) return;
434
435 if (flist->count >= flist->malloced) {
436 if (flist->malloced < 1000)
437 flist->malloced += 1000;
438 else
439 flist->malloced *= 2;
440 flist->files = (struct file_struct **)realloc(flist->files,
441 sizeof(flist->files[0])*
442 flist->malloced);
443 if (!flist->files)
444 out_of_memory("send_file_name");
445 }
446
447 if (strcmp(file->basename,"")) {
448 flist->files[flist->count++] = file;
449 send_file_entry(file,f);
450 }
451
452 if (S_ISDIR(file->mode) && recursive) {
453 char **last_exclude_list = local_exclude_list;
454 send_directory(f,flist,f_name(file));
455 local_exclude_list = last_exclude_list;
456 return;
457 }
458}
459
460
461
462static void send_directory(int f,struct file_list *flist,char *dir)
463{
464 DIR *d;
465 struct dirent *di;
466 char fname[MAXPATHLEN];
467 int l;
468 char *p;
469
470 d = opendir(dir);
471 if (!d) {
472 fprintf(FERROR,"%s: %s\n",
473 dir,strerror(errno));
474 return;
475 }
476
477 strncpy(fname,dir,MAXPATHLEN-1);
478 fname[MAXPATHLEN-1]=0;
479 l = strlen(fname);
480 if (fname[l-1] != '/') {
481 if (l == MAXPATHLEN-1) {
482 fprintf(FERROR,"skipping long-named directory %s\n",fname);
483 closedir(d);
484 return;
485 }
486 strcat(fname,"/");
487 l++;
488 }
489 p = fname + strlen(fname);
490
491 if (cvs_exclude) {
492 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
493 strcpy(p,".cvsignore");
494 local_exclude_list = make_exclude_list(fname,NULL,0);
495 } else {
496 fprintf(FERROR,"cannot cvs-exclude in long-named directory %s\n",fname);
497 }
498 }
499
500 for (di=readdir(d); di; di=readdir(d)) {
501 if (strcmp(di->d_name,".")==0 ||
502 strcmp(di->d_name,"..")==0)
503 continue;
504 strncpy(p,di->d_name,MAXPATHLEN-(l+1));
505 send_file_name(f,flist,fname,recurse);
506 }
507
508 closedir(d);
509}
510
511
512
513struct file_list *send_file_list(int f,int argc,char *argv[])
514{
515 int i,l;
516 struct stat st;
517 char *p,*dir;
518 char dbuf[MAXPATHLEN];
519 char lastpath[MAXPATHLEN]="";
520 struct file_list *flist;
521
522 if (verbose && recurse && !am_server && f != -1) {
523 fprintf(FINFO,"building file list ... ");
524 fflush(FINFO);
525 }
526
527 flist = (struct file_list *)malloc(sizeof(flist[0]));
528 if (!flist) out_of_memory("send_file_list");
529
530 flist->count=0;
531 flist->malloced = 1000;
532 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
533 flist->malloced);
534 if (!flist->files) out_of_memory("send_file_list");
535
536 for (i=0;i<argc;i++) {
537 char fname2[MAXPATHLEN];
538 char *fname = fname2;
539
540 strncpy(fname,argv[i],MAXPATHLEN-1);
541 fname[MAXPATHLEN-1] = 0;
542
543 l = strlen(fname);
544 if (l != 1 && fname[l-1] == '/') {
545 strcat(fname,".");
546 }
547
548 if (link_stat(fname,&st) != 0) {
549 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
550 continue;
551 }
552
553 if (S_ISDIR(st.st_mode) && !recurse) {
554 fprintf(FERROR,"skipping directory %s\n",fname);
555 continue;
556 }
557
558 dir = NULL;
559
560 if (!relative_paths) {
561 p = strrchr(fname,'/');
562 if (p) {
563 *p = 0;
564 if (p == fname)
565 dir = "/";
566 else
567 dir = fname;
568 fname = p+1;
569 }
570 } else if (f != -1 && (p=strrchr(fname,'/'))) {
571 /* this ensures we send the intermediate directories,
572 thus getting their permissions right */
573 *p = 0;
574 if (strcmp(lastpath,fname)) {
575 strcpy(lastpath, fname);
576 *p = '/';
577 for (p=fname+1; (p=strchr(p,'/')); p++) {
578 *p = 0;
579 send_file_name(f, flist, fname, 0);
580 *p = '/';
581 }
582 } else {
583 *p = '/';
584 }
585 }
586
587 if (!*fname)
588 fname = ".";
589
590 if (dir && *dir) {
591 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
592 fprintf(FERROR,"getwd : %s\n",strerror(errno));
593 exit_cleanup(1);
594 }
595 if (chdir(dir) != 0) {
596 fprintf(FERROR,"chdir %s : %s\n",
597 dir,strerror(errno));
598 continue;
599 }
600 flist_dir = dir;
601 if (one_file_system)
602 set_filesystem(fname);
603 send_file_name(f,flist,fname,recurse);
604 flist_dir = NULL;
605 if (chdir(dbuf) != 0) {
606 fprintf(FERROR,"chdir %s : %s\n",
607 dbuf,strerror(errno));
608 exit_cleanup(1);
609 }
610 continue;
611 }
612
613 if (one_file_system)
614 set_filesystem(fname);
615 send_file_name(f,flist,fname,recurse);
616 }
617
618 if (f != -1) {
619 send_file_entry(NULL,f);
620 write_flush(f);
621 }
622
623 if (verbose && recurse && !am_server && f != -1)
624 fprintf(FINFO,"done\n");
625
626 clean_flist(flist);
627
628 /* now send the uid/gid list. This was introduced in protocol
629 version 15 */
630 if (f != -1 && remote_version >= 15) {
631 send_uid_list(f);
632 }
633
634 return flist;
635}
636
637
638struct file_list *recv_file_list(int f)
639{
640 struct file_list *flist;
641 unsigned char flags;
642
643 if (verbose && recurse && !am_server) {
644 fprintf(FINFO,"receiving file list ... ");
645 fflush(FINFO);
646 }
647
648 flist = (struct file_list *)malloc(sizeof(flist[0]));
649 if (!flist)
650 goto oom;
651
652 flist->count=0;
653 flist->malloced=1000;
654 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
655 flist->malloced);
656 if (!flist->files)
657 goto oom;
658
659
660 for (flags=read_byte(f); flags; flags=read_byte(f)) {
661 int i = flist->count;
662
663 if (i >= flist->malloced) {
664 if (flist->malloced < 1000)
665 flist->malloced += 1000;
666 else
667 flist->malloced *= 2;
668 flist->files =(struct file_struct **)realloc(flist->files,
669 sizeof(flist->files[0])*
670 flist->malloced);
671 if (!flist->files)
672 goto oom;
673 }
674
675 receive_file_entry(&flist->files[i],flags,f);
676
677 if (S_ISREG(flist->files[i]->mode))
678 total_size += flist->files[i]->length;
679
680 flist->count++;
681
682 if (verbose > 2)
683 fprintf(FERROR,"recv_file_name(%s)\n",f_name(flist->files[i]));
684 }
685
686
687 if (verbose > 2)
688 fprintf(FERROR,"received %d names\n",flist->count);
689
690 clean_flist(flist);
691
692 if (verbose && recurse && !am_server) {
693 fprintf(FINFO,"done\n");
694 }
695
696 /* now recv the uid/gid list. This was introduced in protocol version 15 */
697 if (f != -1 && remote_version >= 15) {
698 recv_uid_list(f, flist);
699 }
700
701 return flist;
702
703oom:
704 out_of_memory("recv_file_list");
705 return NULL; /* not reached */
706}
707
708
709int file_compare(struct file_struct **f1,struct file_struct **f2)
710{
711 if (!(*f1)->basename && !(*f2)->basename) return 0;
712 if (!(*f1)->basename) return -1;
713 if (!(*f2)->basename) return 1;
714 if ((*f1)->dirname == (*f2)->dirname)
715 return strcmp((*f1)->basename, (*f2)->basename);
716 return strcmp(f_name(*f1),f_name(*f2));
717}
718
719
720int flist_find(struct file_list *flist,struct file_struct *f)
721{
722 int low=0,high=flist->count-1;
723
724 if (flist->count <= 0) return -1;
725
726 while (low != high) {
727 int mid = (low+high)/2;
728 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
729 if (ret == 0) return flist_up(flist, mid);
730 if (ret > 0) {
731 high=mid;
732 } else {
733 low=mid+1;
734 }
735 }
736
737 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
738 return flist_up(flist,low);
739 return -1;
740}
741
742
743/*
744 * free up one file
745 */
746static void free_file(struct file_struct *file)
747{
748 if (!file) return;
749 if (file->basename) free(file->basename);
750 if (file->link) free(file->link);
751 if (file->sum) free(file->sum);
752 bzero((char *)file, sizeof(*file));
753}
754
755
756/*
757 * free up all elements in a flist
758 */
759void flist_free(struct file_list *flist)
760{
761 int i;
762 for (i=1;i<flist->count;i++) {
763 free_file(flist->files[i]);
764 free(flist->files[i]);
765 }
766 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
767 free(flist->files);
768 bzero((char *)flist, sizeof(*flist));
769 free(flist);
770}
771
772
773/*
774 * This routine ensures we don't have any duplicate names in our file list.
775 * duplicate names can cause corruption because of the pipelining
776 */
777void clean_flist(struct file_list *flist)
778{
779 int i;
780
781 if (!flist || flist->count == 0)
782 return;
783
784 qsort(flist->files,flist->count,
785 sizeof(flist->files[0]),
786 (int (*)())file_compare);
787
788 for (i=1;i<flist->count;i++) {
789 if (flist->files[i]->basename &&
790 flist->files[i-1]->basename &&
791 strcmp(f_name(flist->files[i]),
792 f_name(flist->files[i-1])) == 0) {
793 if (verbose > 1 && !am_server)
794 fprintf(FERROR,"removing duplicate name %s from file list %d\n",
795 f_name(flist->files[i-1]),i-1);
796 free_file(flist->files[i]);
797 }
798 }
799}
800
801
802/*
803 * return the full filename of a flist entry
804 */
805char *f_name(struct file_struct *f)
806{
807 static char names[10][MAXPATHLEN];
808 static int n;
809 char *p = names[n];
810
811 if (!f || !f->basename) return NULL;
812
813 n = (n+1)%10;
814
815 if (f->dirname) {
816 sprintf(p, "%s/%s", f->dirname, f->basename);
817 } else {
818 strcpy(p, f->basename);
819 }
820
821 return p;
822}
823