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