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