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