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