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