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