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