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