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