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