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