*** empty log message ***
[rsync/rsync.git] / flist.c
CommitLineData
c627d613
AT
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
43a481dc
AT
24extern int csum_length;
25
c627d613
AT
26extern int verbose;
27extern int am_server;
28extern int always_checksum;
29extern off_t total_size;
30
31extern int cvs_exclude;
32
a06d19e3
AT
33extern int recurse;
34
c627d613
AT
35extern int one_file_system;
36extern int make_backups;
37extern int preserve_links;
dc5ddbcc 38extern int preserve_hard_links;
c627d613
AT
39extern int preserve_perms;
40extern int preserve_devices;
41extern int preserve_uid;
42extern int preserve_gid;
43extern int preserve_times;
6574b4f7 44extern int relative_paths;
82306bf6 45extern int copy_links;
c627d613
AT
46
47static char **local_exclude_list = NULL;
48
49static void clean_fname(char *name);
50
51
82306bf6
AT
52int 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
c627d613
AT
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 */
69static int match_file_name(char *fname,struct stat *st)
70{
71 if (check_exclude(fname,local_exclude_list)) {
72 if (verbose > 2)
dc5ddbcc 73 fprintf(FERROR,"excluding file %s\n",fname);
c627d613
AT
74 return 0;
75 }
76 return 1;
77}
78
79/* used by the one_file_system code */
80static dev_t filesystem_dev;
81
82static void set_filesystem(char *fname)
83{
84 struct stat st;
82306bf6 85 if (link_stat(fname,&st) != 0) return;
c627d613
AT
86 filesystem_dev = st.st_dev;
87}
88
89
90static void send_directory(int f,struct file_list *flist,char *dir);
91
92static char *flist_dir = NULL;
93
4fe159a8
AT
94extern void (*send_file_entry)(struct file_struct *file,int f);
95extern void (*receive_file_entry)(struct file_struct *file,
96 unsigned char flags,int f);
97
98
99void send_file_entry_v11(struct file_struct *file,int f)
c627d613 100{
182dca5c 101 unsigned char flags;
4fe159a8 102 static time_t last_time=0;
182dca5c 103 static mode_t last_mode=0;
dc5ddbcc 104 static dev_t last_rdev=0;
182dca5c
AT
105 static uid_t last_uid=0;
106 static gid_t last_gid=0;
4fe159a8
AT
107 static char lastname[MAXPATHLEN]="";
108 int l1,l2;
182dca5c 109
c627d613
AT
110 if (f == -1) return;
111
182dca5c
AT
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;
dc5ddbcc 120 if (file->rdev == last_rdev) flags |= SAME_RDEV;
182dca5c
AT
121 if (file->uid == last_uid) flags |= SAME_UID;
122 if (file->gid == last_gid) flags |= SAME_GID;
4fe159a8 123 if (file->modtime == last_time) flags |= SAME_TIME;
182dca5c 124
4fe159a8
AT
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;
d89322c4 130
4fe159a8
AT
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);
9f3541e6 136 else
4fe159a8
AT
137 write_byte(f,l2);
138 write_buf(f,file->name+l1,l2);
139
c627d613 140 write_int(f,(int)file->length);
4fe159a8
AT
141 if (!(flags & SAME_TIME))
142 write_int(f,(int)file->modtime);
182dca5c
AT
143 if (!(flags & SAME_MODE))
144 write_int(f,(int)file->mode);
145 if (preserve_uid && !(flags & SAME_UID))
c627d613 146 write_int(f,(int)file->uid);
182dca5c 147 if (preserve_gid && !(flags & SAME_GID))
c627d613 148 write_int(f,(int)file->gid);
dc5ddbcc
AT
149 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
150 write_int(f,(int)file->rdev);
c627d613
AT
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
dc5ddbcc
AT
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
c627d613 166 if (always_checksum) {
43a481dc 167 write_buf(f,file->sum,csum_length);
c627d613 168 }
182dca5c
AT
169
170 last_mode = file->mode;
dc5ddbcc 171 last_rdev = file->rdev;
182dca5c
AT
172 last_uid = file->uid;
173 last_gid = file->gid;
4fe159a8
AT
174 last_time = file->modtime;
175
13a1f792
AT
176 strncpy(lastname,file->name,MAXPATHLEN-1);
177 lastname[MAXPATHLEN-1] = 0;
182dca5c
AT
178}
179
180
181
4fe159a8
AT
182void receive_file_entry_v11(struct file_struct *file,
183 unsigned char flags,int f)
182dca5c 184{
d89322c4 185 static time_t last_time=0;
182dca5c 186 static mode_t last_mode=0;
dc5ddbcc 187 static dev_t last_rdev=0;
182dca5c
AT
188 static uid_t last_uid=0;
189 static gid_t last_gid=0;
4fe159a8
AT
190 static char lastname[MAXPATHLEN]="";
191 int l1=0,l2=0;
182dca5c 192
4fe159a8 193 if (flags & SAME_NAME)
9f3541e6 194 l1 = read_byte(f);
4fe159a8
AT
195
196 if (flags & LONG_NAME)
197 l2 = read_int(f);
198 else
199 l2 = read_byte(f);
182dca5c 200
dc5ddbcc
AT
201 bzero((char *)file,sizeof(*file));
202
182dca5c 203 file->name = (char *)malloc(l1+l2+1);
6dd1782c 204 if (!file->name) out_of_memory("receive_file_entry 1");
182dca5c 205
4fe159a8
AT
206 strncpy(file->name,lastname,l1);
207 read_buf(f,file->name+l1,l2);
182dca5c
AT
208 file->name[l1+l2] = 0;
209
182dca5c 210 file->length = (off_t)read_int(f);
4fe159a8 211 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
182dca5c
AT
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))
dc5ddbcc 218 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
182dca5c 219
182dca5c
AT
220 if (preserve_links && S_ISLNK(file->mode)) {
221 int l = read_int(f);
222 file->link = (char *)malloc(l+1);
6dd1782c 223 if (!file->link) out_of_memory("receive_file_entry 2");
182dca5c
AT
224 read_buf(f,file->link,l);
225 file->link[l] = 0;
226 }
dc5ddbcc
AT
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
182dca5c
AT
234
235 if (always_checksum)
43a481dc 236 read_buf(f,file->sum,csum_length);
182dca5c
AT
237
238 last_mode = file->mode;
dc5ddbcc 239 last_rdev = file->rdev;
182dca5c
AT
240 last_uid = file->uid;
241 last_gid = file->gid;
4fe159a8
AT
242 last_time = file->modtime;
243
13a1f792
AT
244 strncpy(lastname,file->name,MAXPATHLEN-1);
245 lastname[MAXPATHLEN-1] = 0;
c627d613
AT
246}
247
248
4fe159a8 249
a06d19e3 250static struct file_struct *make_file(char *fname)
c627d613
AT
251{
252 static struct file_struct file;
253 struct stat st;
254 char sum[SUM_LENGTH];
255
256 bzero(sum,SUM_LENGTH);
257
82306bf6 258 if (link_stat(fname,&st) != 0) {
dc5ddbcc 259 fprintf(FERROR,"%s: %s\n",
c627d613
AT
260 fname,strerror(errno));
261 return NULL;
262 }
263
264 if (S_ISDIR(st.st_mode) && !recurse) {
dc5ddbcc 265 fprintf(FERROR,"skipping directory %s\n",fname);
c627d613
AT
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)
dc5ddbcc
AT
276 fprintf(FERROR,"make_file(%s)\n",fname);
277
278 bzero((char *)&file,sizeof(file));
c627d613
AT
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;
dc5ddbcc
AT
286 file.dev = st.st_dev;
287 file.inode = st.st_ino;
c627d613 288#ifdef HAVE_ST_RDEV
dc5ddbcc 289 file.rdev = st.st_rdev;
c627d613
AT
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) {
dc5ddbcc 297 fprintf(FERROR,"readlink %s : %s\n",fname,strerror(errno));
c627d613
AT
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
a06d19e3 322static void send_file_name(int f,struct file_list *flist,char *fname)
c627d613
AT
323{
324 struct file_struct *file;
325
a06d19e3 326 file = make_file(fname);
c627d613
AT
327
328 if (!file) return;
329
330 if (flist->count >= flist->malloced) {
f9c51620
AT
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");
c627d613
AT
340 }
341
e9d4e304 342 if (strcmp(file->name,"/")) {
c627d613
AT
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
357static 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) {
dc5ddbcc 367 fprintf(FERROR,"%s: %s\n",
c627d613
AT
368 dir,strerror(errno));
369 return;
370 }
371
13a1f792
AT
372 strncpy(fname,dir,MAXPATHLEN-1);
373 fname[MAXPATHLEN-1]=0;
c627d613 374 l = strlen(fname);
9a52223b 375 if (fname[l-1] != '/') {
684b4e31
AT
376 if (l == MAXPATHLEN-1) {
377 fprintf(FERROR,"skipping long-named directory %s\n",fname);
378 closedir(d);
379 return;
380 }
9a52223b
AT
381 strcat(fname,"/");
382 l++;
383 }
c627d613
AT
384 p = fname + strlen(fname);
385
386 if (cvs_exclude) {
684b4e31
AT
387 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
388 strcpy(p,".cvsignore");
389 local_exclude_list = make_exclude_list(fname,NULL,0);
390 } else {
391 fprintf(FERROR,"cannot cvs-exclude in long-named directory %s\n",fname);
392 }
c627d613
AT
393 }
394
395 for (di=readdir(d); di; di=readdir(d)) {
396 if (strcmp(di->d_name,".")==0 ||
397 strcmp(di->d_name,"..")==0)
398 continue;
9a52223b 399 strncpy(p,di->d_name,MAXPATHLEN-(l+1));
a06d19e3 400 send_file_name(f,flist,fname);
c627d613
AT
401 }
402
403 closedir(d);
404}
405
406
407
a06d19e3 408struct file_list *send_file_list(int f,int argc,char *argv[])
c627d613
AT
409{
410 int i,l;
411 struct stat st;
412 char *p,*dir;
413 char dbuf[MAXPATHLEN];
414 struct file_list *flist;
415
95a38e86 416 if (verbose && recurse && !am_server && f != -1) {
dc5ddbcc
AT
417 fprintf(FINFO,"building file list ... ");
418 fflush(FINFO);
c627d613
AT
419 }
420
421 flist = (struct file_list *)malloc(sizeof(flist[0]));
422 if (!flist) out_of_memory("send_file_list");
423
424 flist->count=0;
425 flist->malloced = 100;
426 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
427 flist->malloced);
428 if (!flist->files) out_of_memory("send_file_list");
429
430 for (i=0;i<argc;i++) {
431 char fname2[MAXPATHLEN];
432 char *fname = fname2;
433
13a1f792
AT
434 strncpy(fname,argv[i],MAXPATHLEN-1);
435 fname[MAXPATHLEN-1] = 0;
c627d613
AT
436
437 l = strlen(fname);
438 if (l != 1 && fname[l-1] == '/') {
439 strcat(fname,".");
440 }
441
82306bf6 442 if (link_stat(fname,&st) != 0) {
dc5ddbcc 443 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
c627d613
AT
444 continue;
445 }
446
447 if (S_ISDIR(st.st_mode) && !recurse) {
dc5ddbcc 448 fprintf(FERROR,"skipping directory %s\n",fname);
c627d613
AT
449 continue;
450 }
451
452 dir = NULL;
6574b4f7
AT
453
454 if (!relative_paths) {
455 p = strrchr(fname,'/');
456 if (p) {
457 *p = 0;
458 if (p == fname)
459 dir = "/";
460 else
461 dir = fname;
462 fname = p+1;
463 }
c627d613 464 }
6574b4f7 465
c627d613
AT
466 if (!*fname)
467 fname = ".";
468
469 if (dir && *dir) {
470 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
dc5ddbcc 471 fprintf(FERROR,"getwd : %s\n",strerror(errno));
34ccb63e 472 exit_cleanup(1);
c627d613
AT
473 }
474 if (chdir(dir) != 0) {
dc5ddbcc 475 fprintf(FERROR,"chdir %s : %s\n",dir,strerror(errno));
c627d613
AT
476 continue;
477 }
478 flist_dir = dir;
479 if (one_file_system)
480 set_filesystem(fname);
a06d19e3 481 send_file_name(f,flist,fname);
c627d613
AT
482 flist_dir = NULL;
483 if (chdir(dbuf) != 0) {
dc5ddbcc 484 fprintf(FERROR,"chdir %s : %s\n",dbuf,strerror(errno));
34ccb63e 485 exit_cleanup(1);
c627d613
AT
486 }
487 continue;
488 }
489
490 if (one_file_system)
491 set_filesystem(fname);
a06d19e3 492 send_file_name(f,flist,fname);
c627d613
AT
493 }
494
495 if (f != -1) {
182dca5c 496 send_file_entry(NULL,f);
c627d613
AT
497 write_flush(f);
498 }
499
95a38e86 500 if (verbose && recurse && !am_server && f != -1)
dc5ddbcc
AT
501 fprintf(FINFO,"done\n");
502
503 clean_flist(flist);
c627d613
AT
504
505 return flist;
506}
507
508
509struct file_list *recv_file_list(int f)
510{
c627d613 511 struct file_list *flist;
182dca5c 512 unsigned char flags;
c627d613 513
a06d19e3
AT
514 if (verbose && recurse && !am_server) {
515 fprintf(FINFO,"receiving file list ... ");
516 fflush(FINFO);
517 }
c627d613
AT
518
519 flist = (struct file_list *)malloc(sizeof(flist[0]));
520 if (!flist)
521 goto oom;
522
523 flist->count=0;
524 flist->malloced=100;
525 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
526 flist->malloced);
527 if (!flist->files)
528 goto oom;
529
530
182dca5c 531 for (flags=read_byte(f); flags; flags=read_byte(f)) {
c627d613
AT
532 int i = flist->count;
533
534 if (i >= flist->malloced) {
f9c51620
AT
535 if (flist->malloced < 100)
536 flist->malloced += 100;
537 else
538 flist->malloced *= 1.8;
539 flist->files =(struct file_struct *)realloc(flist->files,
540 sizeof(flist->files[0])*
541 flist->malloced);
542 if (!flist->files)
543 goto oom;
c627d613
AT
544 }
545
182dca5c 546 receive_file_entry(&flist->files[i],flags,f);
c627d613
AT
547
548 if (S_ISREG(flist->files[i].mode))
549 total_size += flist->files[i].length;
550
551 flist->count++;
552
553 if (verbose > 2)
dc5ddbcc 554 fprintf(FERROR,"recv_file_name(%s)\n",flist->files[i].name);
c627d613
AT
555 }
556
557
558 if (verbose > 2)
dc5ddbcc 559 fprintf(FERROR,"received %d names\n",flist->count);
c627d613
AT
560
561 clean_flist(flist);
562
a06d19e3
AT
563 if (verbose && recurse && !am_server) {
564 fprintf(FINFO,"done\n");
565 }
566
c627d613
AT
567 return flist;
568
569oom:
570 out_of_memory("recv_file_list");
571 return NULL; /* not reached */
572}
573
574
dc5ddbcc 575int file_compare(struct file_struct *f1,struct file_struct *f2)
c627d613
AT
576{
577 if (!f1->name && !f2->name) return 0;
578 if (!f1->name) return -1;
579 if (!f2->name) return 1;
580 return strcmp(f1->name,f2->name);
581}
582
583
584int flist_find(struct file_list *flist,struct file_struct *f)
585{
d966ee25
AT
586 int low=0,high=flist->count-1;
587
588 if (flist->count <= 0) return -1;
589
590 while (low != high) {
591 int mid = (low+high)/2;
592 int ret = file_compare(&flist->files[flist_up(flist, mid)],f);
593 if (ret == 0) return flist_up(flist, mid);
594 if (ret > 0) {
595 high=mid;
596 } else {
597 low=mid+1;
598 }
599 }
600
601 if (file_compare(&flist->files[flist_up(flist,low)],f) == 0)
602 return flist_up(flist,low);
603 return -1;
c627d613
AT
604}
605
606
607static void clean_fname(char *name)
608{
609 char *p;
610 int l;
611 int modified = 1;
612
613 if (!name) return;
614
615 while (modified) {
616 modified = 0;
617
618 if ((p=strstr(name,"/./"))) {
619 modified = 1;
620 while (*p) {
621 p[0] = p[2];
622 p++;
623 }
624 }
625
626 if ((p=strstr(name,"//"))) {
627 modified = 1;
628 while (*p) {
629 p[0] = p[1];
630 p++;
631 }
632 }
633
634 if (strncmp(p=name,"./",2) == 0) {
635 modified = 1;
e3cd198f 636 do {
c627d613 637 p[0] = p[2];
e3cd198f 638 } while (*p++);
c627d613
AT
639 }
640
641 l = strlen(p=name);
642 if (l > 1 && p[l-1] == '/') {
643 modified = 1;
644 p[l-1] = 0;
645 }
646 }
647}
648
649
650/*
651 * This routine ensures we don't have any duplicate names in our file list.
652 * duplicate names can cause corruption because of the pipelining
653 */
654void clean_flist(struct file_list *flist)
655{
656 int i;
657
658 if (!flist || flist->count == 0)
659 return;
660
661 for (i=0;i<flist->count;i++) {
662 clean_fname(flist->files[i].name);
663 }
664
665 qsort(flist->files,flist->count,
666 sizeof(flist->files[0]),
dc5ddbcc 667 (int (*)())file_compare);
c627d613
AT
668
669 for (i=1;i<flist->count;i++) {
670 if (flist->files[i].name &&
671 strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
672 if (verbose > 1 && !am_server)
dc5ddbcc
AT
673 fprintf(FERROR,"removing duplicate name %s from file list %d\n",
674 flist->files[i-1].name,i-1);
c627d613 675 free(flist->files[i-1].name);
dc5ddbcc
AT
676 bzero((char *)&flist->files[i-1],sizeof(flist->files[i-1]));
677 }
c627d613
AT
678 }
679}
680