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