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