Be consistent on use of '=' on options that take a parameter.
[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 struct stats stats;
25
26extern int csum_length;
27
28extern int verbose;
29extern int am_server;
30extern int always_checksum;
31
32extern int cvs_exclude;
33
34extern int recurse;
35
36extern int one_file_system;
37extern int make_backups;
38extern int preserve_links;
39extern int preserve_hard_links;
40extern int preserve_perms;
41extern int preserve_devices;
42extern int preserve_uid;
43extern int preserve_gid;
44extern int preserve_times;
45extern int relative_paths;
46extern int copy_links;
47extern int remote_version;
48extern int io_error;
49
50static struct exclude_struct **local_exclude_list;
51
52static void clean_flist(struct file_list *flist, int strip_root);
53
54
55static void list_file_entry(struct file_struct *f)
56{
57 char perms[11] = "----------";
58 char *perm_map = "rwxrwxrwx";
59 int i;
60
61 if (!f->basename)
62 /* this can happen if duplicate names were removed */
63 return;
64
65 for (i=0;i<9;i++) {
66 if (f->mode & (1<<i)) perms[9-i] = perm_map[8-i];
67 }
68 if (S_ISLNK(f->mode)) perms[0] = 'l';
69 if (S_ISDIR(f->mode)) perms[0] = 'd';
70 if (S_ISBLK(f->mode)) perms[0] = 'b';
71 if (S_ISCHR(f->mode)) perms[0] = 'c';
72 if (S_ISSOCK(f->mode)) perms[0] = 's';
73 if (S_ISFIFO(f->mode)) perms[0] = 'p';
74
75 if (preserve_links && S_ISLNK(f->mode)) {
76 rprintf(FINFO,"%s %11.0f %s %s -> %s\n",
77 perms,
78 (double)f->length, timestring(f->modtime),
79 f_name(f), f->link);
80 } else {
81 rprintf(FINFO,"%s %11.0f %s %s\n",
82 perms,
83 (double)f->length, timestring(f->modtime), f_name(f));
84 }
85}
86
87
88int link_stat(const char *Path, STRUCT_STAT *Buffer)
89{
90#if SUPPORT_LINKS
91 if (copy_links) {
92 return do_stat(Path, Buffer);
93 } else {
94 return do_lstat(Path, Buffer);
95 }
96#else
97 return do_stat(Path, Buffer);
98#endif
99}
100
101/*
102 This function is used to check if a file should be included/excluded
103 from the list of files based on its name and type etc
104 */
105static int match_file_name(char *fname,STRUCT_STAT *st)
106{
107 if (check_exclude(fname,local_exclude_list,st)) {
108 if (verbose > 2)
109 rprintf(FINFO,"excluding file %s\n",fname);
110 return 0;
111 }
112 return 1;
113}
114
115/* used by the one_file_system code */
116static dev_t filesystem_dev;
117
118static void set_filesystem(char *fname)
119{
120 STRUCT_STAT st;
121 if (link_stat(fname,&st) != 0) return;
122 filesystem_dev = st.st_dev;
123}
124
125
126static int to_wire_mode(mode_t mode)
127{
128 if (S_ISLNK(mode) && (S_IFLNK != 0120000)) {
129 return (mode & ~(_S_IFMT)) | 0120000;
130 }
131 return (int)mode;
132}
133
134static mode_t from_wire_mode(int mode)
135{
136 if ((mode & (_S_IFMT)) == 0120000 && (S_IFLNK != 0120000)) {
137 return (mode & ~(_S_IFMT)) | S_IFLNK;
138 }
139 return (mode_t)mode;
140}
141
142
143static void send_directory(int f,struct file_list *flist,char *dir);
144
145static char *flist_dir;
146
147
148static void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
149{
150 unsigned char flags;
151 static time_t last_time;
152 static mode_t last_mode;
153 static dev_t last_rdev;
154 static uid_t last_uid;
155 static gid_t last_gid;
156 static char lastname[MAXPATHLEN];
157 char *fname;
158 int l1,l2;
159
160 if (f == -1) return;
161
162 if (!file) {
163 write_byte(f,0);
164 return;
165 }
166
167 fname = f_name(file);
168
169 flags = base_flags;
170
171 if (file->mode == last_mode) flags |= SAME_MODE;
172 if (file->rdev == last_rdev) flags |= SAME_RDEV;
173 if (file->uid == last_uid) flags |= SAME_UID;
174 if (file->gid == last_gid) flags |= SAME_GID;
175 if (file->modtime == last_time) flags |= SAME_TIME;
176
177 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
178 l2 = strlen(fname) - l1;
179
180 if (l1 > 0) flags |= SAME_NAME;
181 if (l2 > 255) flags |= LONG_NAME;
182
183 /* we must make sure we don't send a zero flags byte or the other
184 end will terminate the flist transfer */
185 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
186 if (flags == 0) flags |= LONG_NAME;
187
188 write_byte(f,flags);
189 if (flags & SAME_NAME)
190 write_byte(f,l1);
191 if (flags & LONG_NAME)
192 write_int(f,l2);
193 else
194 write_byte(f,l2);
195 write_buf(f,fname+l1,l2);
196
197 write_longint(f,file->length);
198 if (!(flags & SAME_TIME))
199 write_int(f,(int)file->modtime);
200 if (!(flags & SAME_MODE))
201 write_int(f,to_wire_mode(file->mode));
202 if (preserve_uid && !(flags & SAME_UID)) {
203 add_uid(file->uid);
204 write_int(f,(int)file->uid);
205 }
206 if (preserve_gid && !(flags & SAME_GID)) {
207 add_gid(file->gid);
208 write_int(f,(int)file->gid);
209 }
210 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
211 write_int(f,(int)file->rdev);
212
213#if SUPPORT_LINKS
214 if (preserve_links && S_ISLNK(file->mode)) {
215 write_int(f,strlen(file->link));
216 write_buf(f,file->link,strlen(file->link));
217 }
218#endif
219
220#if SUPPORT_HARD_LINKS
221 if (preserve_hard_links && S_ISREG(file->mode)) {
222 write_int(f,(int)file->dev);
223 write_int(f,(int)file->inode);
224 }
225#endif
226
227 if (always_checksum) {
228 write_buf(f,file->sum,csum_length);
229 }
230
231 last_mode = file->mode;
232 last_rdev = file->rdev;
233 last_uid = file->uid;
234 last_gid = file->gid;
235 last_time = file->modtime;
236
237 strlcpy(lastname,fname,MAXPATHLEN);
238 lastname[MAXPATHLEN-1] = 0;
239}
240
241
242
243static void receive_file_entry(struct file_struct **fptr,
244 unsigned flags,int f)
245{
246 static time_t last_time;
247 static mode_t last_mode;
248 static dev_t last_rdev;
249 static uid_t last_uid;
250 static gid_t last_gid;
251 static char lastname[MAXPATHLEN];
252 char thisname[MAXPATHLEN];
253 int l1=0,l2=0;
254 char *p;
255 struct file_struct *file;
256
257 if (flags & SAME_NAME)
258 l1 = read_byte(f);
259
260 if (flags & LONG_NAME)
261 l2 = read_int(f);
262 else
263 l2 = read_byte(f);
264
265 file = (struct file_struct *)malloc(sizeof(*file));
266 if (!file) out_of_memory("receive_file_entry");
267 memset((char *)file, 0, sizeof(*file));
268 (*fptr) = file;
269
270 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
271
272 strlcpy(thisname,lastname,l1+1);
273 read_sbuf(f,&thisname[l1],l2);
274 thisname[l1+l2] = 0;
275
276 strlcpy(lastname,thisname,MAXPATHLEN);
277 lastname[MAXPATHLEN-1] = 0;
278
279 clean_fname(thisname);
280
281 if ((p = strrchr(thisname,'/'))) {
282 static char *lastdir;
283 *p = 0;
284 if (lastdir && strcmp(thisname, lastdir)==0) {
285 file->dirname = lastdir;
286 } else {
287 file->dirname = strdup(thisname);
288 lastdir = file->dirname;
289 }
290 file->basename = strdup(p+1);
291 } else {
292 file->dirname = NULL;
293 file->basename = strdup(thisname);
294 }
295
296 if (!file->basename) out_of_memory("receive_file_entry 1");
297
298
299 file->flags = flags;
300 file->length = read_longint(f);
301 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
302 file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
303 if (preserve_uid)
304 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
305 if (preserve_gid)
306 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
307 if (preserve_devices && IS_DEVICE(file->mode))
308 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
309
310 if (preserve_links && S_ISLNK(file->mode)) {
311 int l = read_int(f);
312 file->link = (char *)malloc(l+1);
313 if (!file->link) out_of_memory("receive_file_entry 2");
314 read_sbuf(f,file->link,l);
315 }
316
317#if SUPPORT_HARD_LINKS
318 if (preserve_hard_links && S_ISREG(file->mode)) {
319 file->dev = read_int(f);
320 file->inode = read_int(f);
321 }
322#endif
323
324 if (always_checksum) {
325 file->sum = (char *)malloc(MD4_SUM_LENGTH);
326 if (!file->sum) out_of_memory("md4 sum");
327 read_buf(f,file->sum,csum_length);
328 }
329
330 last_mode = file->mode;
331 last_rdev = file->rdev;
332 last_uid = file->uid;
333 last_gid = file->gid;
334 last_time = file->modtime;
335
336 if (!preserve_perms) {
337 extern int orig_umask;
338 /* set an appropriate set of permissions based on original
339 permissions and umask. This emulates what GNU cp does */
340 file->mode &= ~orig_umask;
341 }
342}
343
344
345/* determine if a file in a different filesstem should be skipped
346 when one_file_system is set. We bascally only want to include
347 the mount points - but they can be hard to find! */
348static int skip_filesystem(char *fname, STRUCT_STAT *st)
349{
350 STRUCT_STAT st2;
351 char *p = strrchr(fname, '/');
352
353 /* skip all but directories */
354 if (!S_ISDIR(st->st_mode)) return 1;
355
356 /* if its not a subdirectory then allow */
357 if (!p) return 0;
358
359 *p = 0;
360 if (link_stat(fname, &st2)) {
361 *p = '/';
362 return 0;
363 }
364 *p = '/';
365
366 return (st2.st_dev != filesystem_dev);
367}
368
369static struct file_struct *make_file(char *fname)
370{
371 struct file_struct *file;
372 STRUCT_STAT st;
373 char sum[SUM_LENGTH];
374 char *p;
375 char cleaned_name[MAXPATHLEN];
376
377 strlcpy(cleaned_name, fname, MAXPATHLEN);
378 cleaned_name[MAXPATHLEN-1] = 0;
379 clean_fname(cleaned_name);
380 fname = cleaned_name;
381
382 memset(sum,0,SUM_LENGTH);
383
384 if (link_stat(fname,&st) != 0) {
385 io_error = 1;
386 rprintf(FERROR,"%s: %s\n",
387 fname,strerror(errno));
388 return NULL;
389 }
390
391 if (S_ISDIR(st.st_mode) && !recurse) {
392 rprintf(FINFO,"skipping directory %s\n",fname);
393 return NULL;
394 }
395
396 if (one_file_system && st.st_dev != filesystem_dev) {
397 if (skip_filesystem(fname, &st))
398 return NULL;
399 }
400
401 if (!match_file_name(fname,&st))
402 return NULL;
403
404 if (verbose > 2)
405 rprintf(FINFO,"make_file(%s)\n",fname);
406
407 file = (struct file_struct *)malloc(sizeof(*file));
408 if (!file) out_of_memory("make_file");
409 memset((char *)file,0,sizeof(*file));
410
411 if ((p = strrchr(fname,'/'))) {
412 static char *lastdir;
413 *p = 0;
414 if (lastdir && strcmp(fname, lastdir)==0) {
415 file->dirname = lastdir;
416 } else {
417 file->dirname = strdup(fname);
418 lastdir = file->dirname;
419 }
420 file->basename = strdup(p+1);
421 *p = '/';
422 } else {
423 file->dirname = NULL;
424 file->basename = strdup(fname);
425 }
426
427 file->modtime = st.st_mtime;
428 file->length = st.st_size;
429 file->mode = st.st_mode;
430 file->uid = st.st_uid;
431 file->gid = st.st_gid;
432 file->dev = st.st_dev;
433 file->inode = st.st_ino;
434#ifdef HAVE_ST_RDEV
435 file->rdev = st.st_rdev;
436#endif
437
438#if SUPPORT_LINKS
439 if (S_ISLNK(st.st_mode)) {
440 int l;
441 char lnk[MAXPATHLEN];
442 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
443 io_error=1;
444 rprintf(FERROR,"readlink %s : %s\n",
445 fname,strerror(errno));
446 return NULL;
447 }
448 lnk[l] = 0;
449 file->link = strdup(lnk);
450 }
451#endif
452
453 if (always_checksum) {
454 file->sum = (char *)malloc(MD4_SUM_LENGTH);
455 if (!file->sum) out_of_memory("md4 sum");
456 /* drat. we have to provide a null checksum for non-regular
457 files in order to be compatible with earlier versions
458 of rsync */
459 if (S_ISREG(st.st_mode)) {
460 file_checksum(fname,file->sum,st.st_size);
461 } else {
462 memset(file->sum, 0, MD4_SUM_LENGTH);
463 }
464 }
465
466 if (flist_dir) {
467 static char *lastdir;
468 if (lastdir && strcmp(lastdir, flist_dir)==0) {
469 file->basedir = lastdir;
470 } else {
471 file->basedir = strdup(flist_dir);
472 lastdir = file->basedir;
473 }
474 } else {
475 file->basedir = NULL;
476 }
477
478 if (!S_ISDIR(st.st_mode))
479 stats.total_size += st.st_size;
480
481 return file;
482}
483
484
485
486void send_file_name(int f,struct file_list *flist,char *fname,
487 int recursive, unsigned base_flags)
488{
489 struct file_struct *file;
490
491 file = make_file(fname);
492
493 if (!file) return;
494
495 if (flist->count >= flist->malloced) {
496 if (flist->malloced < 1000)
497 flist->malloced += 1000;
498 else
499 flist->malloced *= 2;
500 flist->files = (struct file_struct **)realloc(flist->files,
501 sizeof(flist->files[0])*
502 flist->malloced);
503 if (!flist->files)
504 out_of_memory("send_file_name");
505 }
506
507 if (strcmp(file->basename,"")) {
508 flist->files[flist->count++] = file;
509 send_file_entry(file,f,base_flags);
510 }
511
512 if (S_ISDIR(file->mode) && recursive) {
513 struct exclude_struct **last_exclude_list = local_exclude_list;
514 send_directory(f,flist,f_name(file));
515 local_exclude_list = last_exclude_list;
516 return;
517 }
518}
519
520
521
522static void send_directory(int f,struct file_list *flist,char *dir)
523{
524 DIR *d;
525 struct dirent *di;
526 char fname[MAXPATHLEN];
527 int l;
528 char *p;
529
530 d = opendir(dir);
531 if (!d) {
532 io_error = 1;
533 rprintf(FERROR,"opendir(%s): %s\n",
534 dir,strerror(errno));
535 return;
536 }
537
538 strlcpy(fname,dir,MAXPATHLEN);
539 l = strlen(fname);
540 if (fname[l-1] != '/') {
541 if (l == MAXPATHLEN-1) {
542 io_error = 1;
543 rprintf(FERROR,"skipping long-named directory %s\n",fname);
544 closedir(d);
545 return;
546 }
547 strlcat(fname,"/", MAXPATHLEN);
548 l++;
549 }
550 p = fname + strlen(fname);
551
552 local_exclude_list = NULL;
553
554 if (cvs_exclude) {
555 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
556 strcpy(p,".cvsignore");
557 local_exclude_list = make_exclude_list(fname,NULL,0,0);
558 } else {
559 io_error = 1;
560 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
561 }
562 }
563
564 for (di=readdir(d); di; di=readdir(d)) {
565 char *dname = d_name(di);
566 if (strcmp(dname,".")==0 ||
567 strcmp(dname,"..")==0)
568 continue;
569 strlcpy(p,dname,MAXPATHLEN-l);
570 send_file_name(f,flist,fname,recurse,0);
571 }
572
573 if (local_exclude_list) {
574 add_exclude_list("!", &local_exclude_list, 0);
575 }
576
577 closedir(d);
578}
579
580
581
582struct file_list *send_file_list(int f,int argc,char *argv[])
583{
584 int i,l;
585 STRUCT_STAT st;
586 char *p,*dir,*olddir;
587 char lastpath[MAXPATHLEN]="";
588 struct file_list *flist;
589 int64 start_write;
590
591 if (verbose && recurse && !am_server && f != -1) {
592 rprintf(FINFO,"building file list ... ");
593 rflush(FINFO);
594 }
595
596 start_write = stats.total_written;
597
598 flist = (struct file_list *)malloc(sizeof(flist[0]));
599 if (!flist) out_of_memory("send_file_list");
600
601 flist->count=0;
602 flist->malloced = 1000;
603 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
604 flist->malloced);
605 if (!flist->files) out_of_memory("send_file_list");
606
607 if (f != -1) {
608 io_start_buffering(f);
609 }
610
611 for (i=0;i<argc;i++) {
612 char fname2[MAXPATHLEN];
613 char *fname = fname2;
614
615 strlcpy(fname,argv[i],MAXPATHLEN);
616
617 l = strlen(fname);
618 if (l != 1 && fname[l-1] == '/') {
619 strlcat(fname,".",MAXPATHLEN);
620 }
621
622 if (link_stat(fname,&st) != 0) {
623 io_error=1;
624 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
625 continue;
626 }
627
628 if (S_ISDIR(st.st_mode) && !recurse) {
629 rprintf(FINFO,"skipping directory %s\n",fname);
630 continue;
631 }
632
633 dir = NULL;
634 olddir = NULL;
635
636 if (!relative_paths) {
637 p = strrchr(fname,'/');
638 if (p) {
639 *p = 0;
640 if (p == fname)
641 dir = "/";
642 else
643 dir = fname;
644 fname = p+1;
645 }
646 } else if (f != -1 && (p=strrchr(fname,'/'))) {
647 /* this ensures we send the intermediate directories,
648 thus getting their permissions right */
649 *p = 0;
650 if (strcmp(lastpath,fname)) {
651 strlcpy(lastpath, fname, sizeof(lastpath));
652 *p = '/';
653 for (p=fname+1; (p=strchr(p,'/')); p++) {
654 int copy_links_saved = copy_links;
655 *p = 0;
656 copy_links = 0;
657 send_file_name(f, flist, fname, 0, 0);
658 copy_links = copy_links_saved;
659 *p = '/';
660 }
661 } else {
662 *p = '/';
663 }
664 }
665
666 if (!*fname)
667 fname = ".";
668
669 if (dir && *dir) {
670 olddir = push_dir(dir, 1);
671
672 if (!olddir) {
673 io_error=1;
674 rprintf(FERROR,"push_dir %s : %s\n",
675 dir,strerror(errno));
676 continue;
677 }
678
679 flist_dir = dir;
680 }
681
682 if (one_file_system)
683 set_filesystem(fname);
684
685 if (!recurse || !send_included_file_names(f,flist))
686 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
687
688 if (olddir != NULL) {
689 flist_dir = NULL;
690 if (pop_dir(olddir) != 0) {
691 rprintf(FERROR,"pop_dir %s : %s\n",
692 dir,strerror(errno));
693 exit_cleanup(RERR_FILESELECT);
694 }
695 }
696 }
697
698 if (f != -1) {
699 send_file_entry(NULL,f,0);
700 }
701
702 if (verbose && recurse && !am_server && f != -1)
703 rprintf(FINFO,"done\n");
704
705 clean_flist(flist, 0);
706
707 /* now send the uid/gid list. This was introduced in protocol
708 version 15 */
709 if (f != -1 && remote_version >= 15) {
710 send_uid_list(f);
711 }
712
713 /* if protocol version is >= 17 then send the io_error flag */
714 if (f != -1 && remote_version >= 17) {
715 write_int(f, io_error);
716 }
717
718 if (f != -1) {
719 io_end_buffering(f);
720 stats.flist_size = stats.total_written - start_write;
721 stats.num_files = flist->count;
722 }
723
724 if (verbose > 2)
725 rprintf(FINFO,"send_file_list done\n");
726
727 return flist;
728}
729
730
731struct file_list *recv_file_list(int f)
732{
733 struct file_list *flist;
734 unsigned char flags;
735 int64 start_read;
736 extern int list_only;
737
738 if (verbose && recurse && !am_server) {
739 rprintf(FINFO,"receiving file list ... ");
740 rflush(FINFO);
741 }
742
743 start_read = stats.total_read;
744
745 flist = (struct file_list *)malloc(sizeof(flist[0]));
746 if (!flist)
747 goto oom;
748
749 flist->count=0;
750 flist->malloced=1000;
751 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
752 flist->malloced);
753 if (!flist->files)
754 goto oom;
755
756
757 for (flags=read_byte(f); flags; flags=read_byte(f)) {
758 int i = flist->count;
759
760 if (i >= flist->malloced) {
761 if (flist->malloced < 1000)
762 flist->malloced += 1000;
763 else
764 flist->malloced *= 2;
765 flist->files =(struct file_struct **)realloc(flist->files,
766 sizeof(flist->files[0])*
767 flist->malloced);
768 if (!flist->files)
769 goto oom;
770 }
771
772 receive_file_entry(&flist->files[i],flags,f);
773
774 if (S_ISREG(flist->files[i]->mode))
775 stats.total_size += flist->files[i]->length;
776
777 flist->count++;
778
779 if (verbose > 2)
780 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
781 }
782
783
784 if (verbose > 2)
785 rprintf(FINFO,"received %d names\n",flist->count);
786
787 clean_flist(flist, relative_paths);
788
789 if (verbose && recurse && !am_server) {
790 rprintf(FINFO,"done\n");
791 }
792
793 /* now recv the uid/gid list. This was introduced in protocol version 15 */
794 if (f != -1 && remote_version >= 15) {
795 recv_uid_list(f, flist);
796 }
797
798 /* if protocol version is >= 17 then recv the io_error flag */
799 if (f != -1 && remote_version >= 17) {
800 io_error |= read_int(f);
801 }
802
803 if (list_only) {
804 int i;
805 for (i=0;i<flist->count;i++) {
806 list_file_entry(flist->files[i]);
807 }
808 }
809
810
811 if (verbose > 2)
812 rprintf(FINFO,"recv_file_list done\n");
813
814 stats.flist_size = stats.total_read - start_read;
815 stats.num_files = flist->count;
816
817 return flist;
818
819oom:
820 out_of_memory("recv_file_list");
821 return NULL; /* not reached */
822}
823
824
825int file_compare(struct file_struct **f1,struct file_struct **f2)
826{
827 if (!(*f1)->basename && !(*f2)->basename) return 0;
828 if (!(*f1)->basename) return -1;
829 if (!(*f2)->basename) return 1;
830 if ((*f1)->dirname == (*f2)->dirname)
831 return u_strcmp((*f1)->basename, (*f2)->basename);
832 return u_strcmp(f_name(*f1),f_name(*f2));
833}
834
835
836int flist_find(struct file_list *flist,struct file_struct *f)
837{
838 int low=0,high=flist->count-1;
839
840 if (flist->count <= 0) return -1;
841
842 while (low != high) {
843 int mid = (low+high)/2;
844 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
845 if (ret == 0) return flist_up(flist, mid);
846 if (ret > 0) {
847 high=mid;
848 } else {
849 low=mid+1;
850 }
851 }
852
853 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
854 return flist_up(flist,low);
855 return -1;
856}
857
858
859/*
860 * free up one file
861 */
862static void free_file(struct file_struct *file)
863{
864 if (!file) return;
865 if (file->basename) free(file->basename);
866 if (file->link) free(file->link);
867 if (file->sum) free(file->sum);
868 memset((char *)file, 0, sizeof(*file));
869}
870
871
872/*
873 * free up all elements in a flist
874 */
875void flist_free(struct file_list *flist)
876{
877 int i;
878 for (i=1;i<flist->count;i++) {
879 free_file(flist->files[i]);
880 free(flist->files[i]);
881 }
882 memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
883 free(flist->files);
884 memset((char *)flist, 0, sizeof(*flist));
885 free(flist);
886}
887
888
889/*
890 * This routine ensures we don't have any duplicate names in our file list.
891 * duplicate names can cause corruption because of the pipelining
892 */
893static void clean_flist(struct file_list *flist, int strip_root)
894{
895 int i;
896
897 if (!flist || flist->count == 0)
898 return;
899
900 qsort(flist->files,flist->count,
901 sizeof(flist->files[0]),
902 (int (*)())file_compare);
903
904 for (i=1;i<flist->count;i++) {
905 if (flist->files[i]->basename &&
906 flist->files[i-1]->basename &&
907 strcmp(f_name(flist->files[i]),
908 f_name(flist->files[i-1])) == 0) {
909 if (verbose > 1 && !am_server)
910 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
911 f_name(flist->files[i-1]),i-1);
912 free_file(flist->files[i]);
913 }
914 }
915
916 if (strip_root) {
917 /* we need to strip off the root directory in the case
918 of relative paths, but this must be done _after_
919 the sorting phase */
920 for (i=0;i<flist->count;i++) {
921 if (flist->files[i]->dirname &&
922 flist->files[i]->dirname[0] == '/') {
923 memmove(&flist->files[i]->dirname[0],
924 &flist->files[i]->dirname[1],
925 strlen(flist->files[i]->dirname));
926 }
927
928 if (flist->files[i]->dirname &&
929 !flist->files[i]->dirname[0]) {
930 flist->files[i]->dirname = NULL;
931 }
932 }
933 }
934
935
936 if (verbose <= 3) return;
937
938 for (i=0;i<flist->count;i++) {
939 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%d\n",
940 getpid(), i,
941 NS(flist->files[i]->dirname),
942 NS(flist->files[i]->basename),
943 flist->files[i]->mode,
944 (int)flist->files[i]->length);
945 }
946}
947
948
949/*
950 * return the full filename of a flist entry
951 */
952char *f_name(struct file_struct *f)
953{
954 static char names[10][MAXPATHLEN];
955 static int n;
956 char *p = names[n];
957
958 if (!f || !f->basename) return NULL;
959
960 n = (n+1)%10;
961
962 if (f->dirname) {
963 strlcpy(p, f->dirname, MAXPATHLEN);
964 strlcat(p, "/", MAXPATHLEN);
965 strlcat(p, f->basename, MAXPATHLEN);
966 } else {
967 strlcpy(p, f->basename, MAXPATHLEN);
968 }
969
970 return p;
971}
972