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
429f9828
WD
67extern struct exclude_struct **exclude_list;
68extern struct exclude_struct **server_exclude_list;
69extern struct exclude_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 }
6931c138 229 if (server_exclude_list
5387514e
WD
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;
5387514e
WD
235 if (exclude_list && check_exclude(exclude_list, fname, is_dir,
236 "pattern"))
429f9828 237 return 1;
6931c138 238 if (local_exclude_list
5387514e
WD
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)) {
d01d15e0 959 struct exclude_struct **last_exclude_list = local_exclude_list;
882e6893 960 send_directory(f, flist, f_name_to(file, fbuf));
ebed4c3a
MP
961 local_exclude_list = last_exclude_list;
962 return;
963 }
c627d613
AT
964}
965
966
ebed4c3a 967static void send_directory(int f, struct file_list *flist, char *dir)
c627d613 968{
3ec4dd97
AT
969 DIR *d;
970 struct dirent *di;
971 char fname[MAXPATHLEN];
eddd5d12 972 unsigned int offset;
3ec4dd97
AT
973 char *p;
974
975 d = opendir(dir);
976 if (!d) {
06c28400 977 io_error |= IOERR_GENERAL;
ea42541f
WD
978 rprintf(FERROR, "opendir %s failed: %s\n",
979 full_fname(dir), strerror(errno));
3ec4dd97
AT
980 return;
981 }
c627d613 982
eddd5d12
WD
983 offset = strlcpy(fname, dir, MAXPATHLEN);
984 p = fname + offset;
985 if (offset >= MAXPATHLEN || p[-1] != '/') {
986 if (offset >= MAXPATHLEN - 1) {
06c28400 987 io_error |= IOERR_GENERAL;
ea42541f
WD
988 rprintf(FERROR, "skipping long-named directory: %s\n",
989 full_fname(fname));
3ec4dd97
AT
990 closedir(d);
991 return;
992 }
eddd5d12
WD
993 *p++ = '/';
994 offset++;
3ec4dd97 995 }
c627d613 996
3d913675
AT
997 local_exclude_list = NULL;
998
3ec4dd97 999 if (cvs_exclude) {
eddd5d12 1000 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
9c5e91f8
WD
1001 < MAXPATHLEN - offset) {
1002 add_exclude_file(&local_exclude_list, fname,
357406ec 1003 XFLG_WORD_SPLIT | XFLG_NO_PREFIXES);
9c5e91f8 1004 } else {
06c28400 1005 io_error |= IOERR_GENERAL;
ebed4c3a
MP
1006 rprintf(FINFO,
1007 "cannot cvs-exclude in long-named directory %s\n",
ea42541f 1008 full_fname(fname));
3ec4dd97 1009 }
ebed4c3a
MP
1010 }
1011
6a7cc46c 1012 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1013 char *dname = d_name(di);
6a7cc46c
S
1014 if (dname[0] == '.' && (dname[1] == '\0'
1015 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1016 continue;
eddd5d12
WD
1017 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
1018 send_file_name(f, flist, fname, recurse, 0);
1019 else {
1020 io_error |= IOERR_GENERAL;
1021 rprintf(FINFO,
1022 "cannot send long-named file %s\n",
1023 full_fname(fname));
1024 }
3ec4dd97 1025 }
6a7cc46c 1026 if (errno) {
06c28400 1027 io_error |= IOERR_GENERAL;
6a7cc46c 1028 rprintf(FERROR, "readdir(%s): (%d) %s\n",
eddd5d12 1029 dir, errno, strerror(errno));
6a7cc46c 1030 }
c627d613 1031
429f9828
WD
1032 if (local_exclude_list)
1033 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
3d913675 1034
3ec4dd97 1035 closedir(d);
c627d613
AT
1036}
1037
1038
c4fea82f 1039/**
429f9828
WD
1040 * The delete_files() function in receiver.c sets f to -1 so that we just
1041 * construct the file list in memory without sending it over the wire. It
1042 * also has the side-effect of ignoring user-excludes if delete_excluded
1043 * is set (so that the delete list includes user-excluded files).
c4fea82f 1044 **/
ebed4c3a 1045struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1046{
24d0fcde 1047 int l;
bcacc18b 1048 STRUCT_STAT st;
808c57c3 1049 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 1050 char lastpath[MAXPATHLEN] = "";
649d65ed 1051 struct file_list *flist;
a800434a 1052 int64 start_write;
24d0fcde 1053 int use_ff_fd = 0;
649d65ed 1054
1bbd10fe
DD
1055 if (show_filelist_p() && f != -1)
1056 start_filelist_progress("building file list");
c627d613 1057
a800434a
AT
1058 start_write = stats.total_written;
1059
9935066b
S
1060 flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK,
1061 "send_file_list");
c627d613 1062
d6dead6b 1063 if (f != -1) {
76c21947 1064 io_start_buffering_out(f);
24d0fcde 1065 if (filesfrom_fd >= 0) {
808c57c3 1066 if (argv[0] && !push_dir(argv[0])) {
ea42541f
WD
1067 rprintf(FERROR, "push_dir %s failed: %s\n",
1068 full_fname(argv[0]), strerror(errno));
24d0fcde
WD
1069 exit_cleanup(RERR_FILESELECT);
1070 }
1071 use_ff_fd = 1;
1072 }
d6dead6b
AT
1073 }
1074
24d0fcde 1075 while (1) {
fc638474
DD
1076 char fname2[MAXPATHLEN];
1077 char *fname = fname2;
c627d613 1078
24d0fcde
WD
1079 if (use_ff_fd) {
1080 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1081 break;
1082 sanitize_path(fname, NULL);
1083 } else {
1084 if (argc-- == 0)
1085 break;
1086 strlcpy(fname, *argv++, MAXPATHLEN);
1087 if (sanitize_paths)
1088 sanitize_path(fname, NULL);
1089 }
c627d613 1090
649d65ed 1091 l = strlen(fname);
6931c138
WD
1092 if (fname[l - 1] == '/') {
1093 if (l == 2 && fname[0] == '.') {
1094 /* Turn "./" into just "." rather than "./." */
1095 fname[1] = '\0';
eddd5d12
WD
1096 } else if (l < MAXPATHLEN) {
1097 fname[l++] = '.';
1098 fname[l] = '\0';
53f821f1 1099 }
649d65ed 1100 }
c627d613 1101
ebed4c3a 1102 if (link_stat(fname, &st) != 0) {
f76933b1 1103 if (f != -1) {
06c28400 1104 io_error |= IOERR_GENERAL;
ea42541f
WD
1105 rprintf(FERROR, "link_stat %s failed: %s\n",
1106 full_fname(fname), strerror(errno));
f76933b1 1107 }
649d65ed
AT
1108 continue;
1109 }
c627d613 1110
24d0fcde 1111 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
ebed4c3a 1112 rprintf(FINFO, "skipping directory %s\n", fname);
649d65ed
AT
1113 continue;
1114 }
c627d613 1115
649d65ed 1116 dir = NULL;
808c57c3 1117 olddir[0] = '\0';
649d65ed
AT
1118
1119 if (!relative_paths) {
ebed4c3a 1120 p = strrchr(fname, '/');
649d65ed
AT
1121 if (p) {
1122 *p = 0;
ebed4c3a 1123 if (p == fname)
649d65ed
AT
1124 dir = "/";
1125 else
ebed4c3a
MP
1126 dir = fname;
1127 fname = p + 1;
649d65ed 1128 }
24d0fcde 1129 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
649d65ed
AT
1130 /* this ensures we send the intermediate directories,
1131 thus getting their permissions right */
2154309a 1132 char *lp = lastpath, *fn = fname, *slash = fname;
649d65ed 1133 *p = 0;
2154309a
WD
1134 /* Skip any initial directories in our path that we
1135 * have in common with lastpath. */
1136 while (*fn && *lp == *fn) {
1137 if (*fn == '/')
1138 slash = fn;
1139 lp++, fn++;
1140 }
1141 *p = '/';
1142 if (fn != p || (*lp && *lp != '/')) {
1143 int copy_links_saved = copy_links;
1144 int recurse_saved = recurse;
1145 copy_links = copy_unsafe_links;
1146 /* set recurse to 1 to prevent make_file
1147 * from ignoring directory, but still
1148 * turn off the recursive parameter to
1149 * send_file_name */
1150 recurse = 1;
1151 while ((slash = strchr(slash+1, '/')) != 0) {
1152 *slash = 0;
1153 send_file_name(f, flist, fname, 0, 0);
1154 *slash = '/';
649d65ed 1155 }
2154309a
WD
1156 copy_links = copy_links_saved;
1157 recurse = recurse_saved;
1158 *p = 0;
1159 strlcpy(lastpath, fname, sizeof lastpath);
649d65ed
AT
1160 *p = '/';
1161 }
1162 }
ebed4c3a 1163
649d65ed
AT
1164 if (!*fname)
1165 fname = ".";
ebed4c3a 1166
649d65ed 1167 if (dir && *dir) {
882e6893
WD
1168 static char *lastdir;
1169 static int lastdir_len;
1170
808c57c3 1171 strcpy(olddir, curr_dir); /* can't overflow */
5243c216 1172
808c57c3 1173 if (!push_dir(dir)) {
06c28400 1174 io_error |= IOERR_GENERAL;
ea42541f
WD
1175 rprintf(FERROR, "push_dir %s failed: %s\n",
1176 full_fname(dir), strerror(errno));
649d65ed
AT
1177 continue;
1178 }
5243c216 1179
882e6893
WD
1180 if (lastdir && strcmp(lastdir, dir) == 0) {
1181 flist_dir = lastdir;
1182 flist_dir_len = lastdir_len;
1183 } else {
882e6893
WD
1184 flist_dir = lastdir = strdup(dir);
1185 flist_dir_len = lastdir_len = strlen(dir);
1186 }
2bca43f6 1187 }
ebed4c3a 1188
2bca43f6
DD
1189 if (one_file_system)
1190 set_filesystem(fname);
1191
d01d15e0 1192 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
2bca43f6 1193
808c57c3 1194 if (olddir[0]) {
649d65ed 1195 flist_dir = NULL;
882e6893 1196 flist_dir_len = 0;
808c57c3 1197 if (!pop_dir(olddir)) {
ea42541f
WD
1198 rprintf(FERROR, "pop_dir %s failed: %s\n",
1199 full_fname(dir), strerror(errno));
65417579 1200 exit_cleanup(RERR_FILESELECT);
649d65ed 1201 }
649d65ed 1202 }
649d65ed 1203 }
dc5ddbcc 1204
983b1ed3 1205 if (f != -1) {
ebed4c3a 1206 send_file_entry(NULL, f, 0);
c627d613 1207
983b1ed3
WD
1208 if (show_filelist_p())
1209 finish_filelist_progress(flist);
1210 }
ebed4c3a 1211
7cf8e8d0 1212 if (flist->hlink_pool) {
9935066b
S
1213 pool_destroy(flist->hlink_pool);
1214 flist->hlink_pool = NULL;
1215 }
1216
827c37f6 1217 clean_flist(flist, 0, 0);
ebed4c3a 1218
785db4ce
WD
1219 if (f != -1) {
1220 /* Now send the uid/gid list. This was introduced in
1221 * protocol version 15 */
649d65ed 1222 send_uid_list(f);
f6c34742 1223
983b1ed3 1224 /* send the io_error flag */
ebed4c3a 1225 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1226
a261989c 1227 io_end_buffering();
a800434a
AT
1228 stats.flist_size = stats.total_written - start_write;
1229 stats.num_files = flist->count;
64c3523a 1230 if (write_batch)
ebed4c3a 1231 write_batch_flist_info(flist->count, flist->files);
d6dead6b
AT
1232 }
1233
cefed3e8
WD
1234 if (verbose > 3)
1235 output_flist(flist);
1236
17faa41c 1237 if (verbose > 2)
ebed4c3a 1238 rprintf(FINFO, "send_file_list done\n");
17faa41c 1239
649d65ed 1240 return flist;
c627d613
AT
1241}
1242
1243
1244struct file_list *recv_file_list(int f)
1245{
ebed4c3a 1246 struct file_list *flist;
1ef00d20 1247 unsigned short flags;
ebed4c3a
MP
1248 int64 start_read;
1249 extern int list_only;
c627d613 1250
1bbd10fe
DD
1251 if (show_filelist_p())
1252 start_filelist_progress("receiving file list");
c627d613 1253
ebed4c3a 1254 start_read = stats.total_read;
a800434a 1255
9935066b 1256 flist = flist_new(WITH_HLINK, "recv_file_list");
c627d613 1257
ebed4c3a
MP
1258 flist->count = 0;
1259 flist->malloced = 1000;
58cadc86 1260 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1261 if (!flist->files)
1262 goto oom;
c627d613
AT
1263
1264
1ef00d20 1265 while ((flags = read_byte(f)) != 0) {
5d2c5c4c 1266 int i = flist->count;
dbda5fbf 1267
d9d6bc52 1268 flist_expand(flist);
c627d613 1269
d01d15e0 1270 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1271 flags |= read_byte(f) << 8;
9935066b 1272 receive_file_entry(&flist->files[i], flags, flist, f);
c627d613 1273
ebed4c3a
MP
1274 if (S_ISREG(flist->files[i]->mode))
1275 stats.total_size += flist->files[i]->length;
c627d613 1276
ebed4c3a 1277 flist->count++;
c627d613 1278
db719fb0 1279 maybe_emit_filelist_progress(flist);
1bbd10fe 1280
8018edd3 1281 if (verbose > 2) {
ebed4c3a
MP
1282 rprintf(FINFO, "recv_file_name(%s)\n",
1283 f_name(flist->files[i]));
8018edd3 1284 }
ebed4c3a 1285 }
9935066b 1286 receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */
c627d613 1287
ebed4c3a
MP
1288 if (verbose > 2)
1289 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1290
b7736c79 1291 if (show_filelist_p())
1bbd10fe 1292 finish_filelist_progress(flist);
a06d19e3 1293
983b1ed3
WD
1294 clean_flist(flist, relative_paths, 1);
1295
785db4ce
WD
1296 if (f != -1) {
1297 /* Now send the uid/gid list. This was introduced in
1298 * protocol version 15 */
ebed4c3a 1299 recv_uid_list(f, flist);
f6c34742 1300
785db4ce
WD
1301 if (!read_batch) {
1302 /* Recv the io_error flag */
1303 if (lp_ignore_errors(module_id) || ignore_errors)
1304 read_int(f);
1305 else
1306 io_error |= read_int(f);
1307 }
ebed4c3a 1308 }
6ba9279f 1309
cefed3e8
WD
1310 if (verbose > 3)
1311 output_flist(flist);
1312
ebed4c3a
MP
1313 if (list_only) {
1314 int i;
b7736c79 1315 for (i = 0; i < flist->count; i++)
ebed4c3a 1316 list_file_entry(flist->files[i]);
ebed4c3a 1317 }
f7632fc6 1318
ebed4c3a
MP
1319 if (verbose > 2)
1320 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1321
ebed4c3a
MP
1322 stats.flist_size = stats.total_read - start_read;
1323 stats.num_files = flist->count;
a800434a 1324
ebed4c3a 1325 return flist;
c627d613 1326
3e491682 1327oom:
ebed4c3a
MP
1328 out_of_memory("recv_file_list");
1329 return NULL; /* not reached */
c627d613
AT
1330}
1331
1332
fa45cda1 1333int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1334{
fa45cda1
S
1335 struct file_struct *f1 = *file1;
1336 struct file_struct *f2 = *file2;
1337
1338 if (!f1->basename && !f2->basename)
ebed4c3a 1339 return 0;
fa45cda1 1340 if (!f1->basename)
ebed4c3a 1341 return -1;
fa45cda1 1342 if (!f2->basename)
ebed4c3a 1343 return 1;
fa45cda1
S
1344 if (f1->dirname == f2->dirname)
1345 return u_strcmp(f1->basename, f2->basename);
1346 return f_name_cmp(f1, f2);
c627d613
AT
1347}
1348
1349
ebed4c3a 1350int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1351{
ebed4c3a 1352 int low = 0, high = flist->count - 1;
d966ee25 1353
ca23c51a
WD
1354 while (high >= 0 && !flist->files[high]->basename) high--;
1355
1356 if (high < 0)
ebed4c3a 1357 return -1;
d966ee25
AT
1358
1359 while (low != high) {
ebed4c3a 1360 int mid = (low + high) / 2;
b7736c79 1361 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
ebed4c3a
MP
1362 if (ret == 0)
1363 return flist_up(flist, mid);
b7736c79 1364 if (ret > 0)
ebed4c3a 1365 high = mid;
b7736c79 1366 else
ebed4c3a 1367 low = mid + 1;
d966ee25
AT
1368 }
1369
ebed4c3a
MP
1370 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1371 return flist_up(flist, low);
d966ee25 1372 return -1;
c627d613
AT
1373}
1374
3ec4dd97 1375/*
9935066b
S
1376 * Free up any resources a file_struct has allocated
1377 * and clear the file.
3ec4dd97 1378 */
9935066b 1379void clear_file(int i, struct file_list *flist)
c627d613 1380{
9935066b
S
1381 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1382 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1383 memset(flist->files[i], 0, file_struct_len);
3ec4dd97 1384}
c627d613 1385
c627d613 1386
3d382777
AT
1387/*
1388 * allocate a new file list
1389 */
9935066b 1390struct file_list *flist_new(int with_hlink, char *msg)
3d382777
AT
1391{
1392 struct file_list *flist;
1393
58cadc86 1394 flist = new(struct file_list);
ebed4c3a 1395 if (!flist)
9935066b 1396 out_of_memory(msg);
3d382777 1397
9935066b
S
1398 memset(flist, 0, sizeof (struct file_list));
1399
1400 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1401 out_of_memory, POOL_INTERN)))
1402 out_of_memory(msg);
1403
1404#if SUPPORT_HARD_LINKS
1405 if (with_hlink && preserve_hard_links) {
3e491682 1406 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
9935066b
S
1407 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1408 out_of_memory(msg);
1409 }
1410#endif
d9d6bc52 1411
3d382777
AT
1412 return flist;
1413}
ebed4c3a 1414
3ec4dd97
AT
1415/*
1416 * free up all elements in a flist
1417 */
1418void flist_free(struct file_list *flist)
1419{
9935066b
S
1420 pool_destroy(flist->file_pool);
1421 pool_destroy(flist->hlink_pool);
3ec4dd97 1422 free(flist->files);
3ec4dd97 1423 free(flist);
c627d613
AT
1424}
1425
1426
1427/*
1428 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1429 * duplicate names can cause corruption because of the pipelining
c627d613 1430 */
827c37f6 1431static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1432{
6931c138 1433 int i, prev_i = 0;
c627d613 1434
ebed4c3a 1435 if (!flist || flist->count == 0)
3ec4dd97 1436 return;
3ec4dd97 1437
ebed4c3a 1438 qsort(flist->files, flist->count,
3e491682 1439 sizeof flist->files[0], (int (*)()) file_compare);
ebed4c3a 1440
827c37f6 1441 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1442 if (flist->files[i]->basename) {
6931c138 1443 prev_i = i;
b91b50c0
WD
1444 break;
1445 }
1446 }
1447 while (++i < flist->count) {
1448 if (!flist->files[i]->basename)
1449 continue;
8018edd3 1450 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
b91b50c0 1451 if (verbose > 1 && !am_server) {
ebed4c3a
MP
1452 rprintf(FINFO,
1453 "removing duplicate name %s from file list %d\n",
8018edd3 1454 f_name(flist->files[i]), i);
b91b50c0 1455 }
6931c138
WD
1456 /* Make sure that if we unduplicate '.', that we don't
1457 * lose track of a user-specified starting point (or
1458 * else deletions will mysteriously fail with -R). */
d01d15e0
WD
1459 if (flist->files[i]->flags & FLAG_TOP_DIR)
1460 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
9935066b
S
1461
1462 clear_file(i, flist);
728d0922 1463 } else
6931c138 1464 prev_i = i;
3ec4dd97 1465 }
0199b05f
AT
1466
1467 if (strip_root) {
1468 /* we need to strip off the root directory in the case
1469 of relative paths, but this must be done _after_
1470 the sorting phase */
ebed4c3a 1471 for (i = 0; i < flist->count; i++) {
0199b05f
AT
1472 if (flist->files[i]->dirname &&
1473 flist->files[i]->dirname[0] == '/') {
1474 memmove(&flist->files[i]->dirname[0],
1475 &flist->files[i]->dirname[1],
1476 strlen(flist->files[i]->dirname));
1477 }
ebed4c3a
MP
1478
1479 if (flist->files[i]->dirname &&
0199b05f
AT
1480 !flist->files[i]->dirname[0]) {
1481 flist->files[i]->dirname = NULL;
1482 }
1483 }
1484 }
cefed3e8 1485}
0199b05f 1486
cefed3e8
WD
1487static void output_flist(struct file_list *flist)
1488{
1489 char uidbuf[16], gidbuf[16];
1490 struct file_struct *file;
1491 int i;
0199b05f 1492
ebed4c3a 1493 for (i = 0; i < flist->count; i++) {
cefed3e8 1494 file = flist->files[i];
f05f993e
WD
1495 if (am_root && preserve_uid)
1496 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1497 else
1498 *uidbuf = '\0';
cefed3e8 1499 if (preserve_gid && file->gid != GID_NONE)
f05f993e
WD
1500 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1501 else
1502 *gidbuf = '\0';
1503 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n",
1504 who_am_i(), i, NS(file->basedir), NS(file->dirname),
1505 NS(file->basename), (int) file->mode,
1506 (double) file->length, uidbuf, gidbuf);
0199b05f 1507 }
3ec4dd97
AT
1508}
1509
1510
8018edd3
WD
1511enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1512
1513/* Compare the names of two file_struct entities, just like strcmp()
1514 * would do if it were operating on the joined strings. We assume
1515 * that there are no 0-length strings.
3ec4dd97 1516 */
8018edd3 1517int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1518{
8018edd3
WD
1519 int dif;
1520 const uchar *c1, *c2;
1ef00d20 1521 enum fnc_state state1, state2;
8018edd3
WD
1522
1523 if (!f1 || !f1->basename) {
1524 if (!f2 || !f2->basename)
1525 return 0;
1526 return -1;
1527 }
1528 if (!f2 || !f2->basename)
1529 return 1;
1530
e90b8ace 1531 if (!(c1 = (uchar*)f1->dirname)) {
8018edd3 1532 state1 = fnc_BASE;
e90b8ace 1533 c1 = (uchar*)f1->basename;
728d0922 1534 } else
1ef00d20 1535 state1 = fnc_DIR;
e90b8ace 1536 if (!(c2 = (uchar*)f2->dirname)) {
8018edd3 1537 state2 = fnc_BASE;
e90b8ace 1538 c2 = (uchar*)f2->basename;
728d0922 1539 } else
1ef00d20 1540 state2 = fnc_DIR;
8018edd3
WD
1541
1542 while (1) {
1543 if ((dif = (int)*c1 - (int)*c2) != 0)
1544 break;
1545 if (!*++c1) {
1546 switch (state1) {
1547 case fnc_DIR:
1548 state1 = fnc_SLASH;
e90b8ace 1549 c1 = (uchar*)"/";
8018edd3
WD
1550 break;
1551 case fnc_SLASH:
1552 state1 = fnc_BASE;
e90b8ace 1553 c1 = (uchar*)f1->basename;
8018edd3
WD
1554 break;
1555 case fnc_BASE:
1556 break;
1557 }
1558 }
1559 if (!*++c2) {
1560 switch (state2) {
1561 case fnc_DIR:
1562 state2 = fnc_SLASH;
e90b8ace 1563 c2 = (uchar*)"/";
8018edd3
WD
1564 break;
1565 case fnc_SLASH:
1566 state2 = fnc_BASE;
e90b8ace 1567 c2 = (uchar*)f2->basename;
8018edd3
WD
1568 break;
1569 case fnc_BASE:
1570 if (!*c1)
1571 return 0;
1572 break;
1573 }
1574 }
1575 }
1576
1577 return dif;
1578}
1579
3ec4dd97 1580
8018edd3 1581/* Return a copy of the full filename of a flist entry, using the indicated
882e6893
WD
1582 * buffer. No size-checking is done because we checked the size when creating
1583 * the file_struct entry.
8018edd3 1584 */
882e6893 1585char *f_name_to(struct file_struct *f, char *fbuf)
8018edd3 1586{
ebed4c3a
MP
1587 if (!f || !f->basename)
1588 return NULL;
3ec4dd97 1589
3ec4dd97 1590 if (f->dirname) {
882e6893
WD
1591 int len = strlen(f->dirname);
1592 memcpy(fbuf, f->dirname, len);
1593 fbuf[len] = '/';
1594 strcpy(fbuf + len + 1, f->basename);
8018edd3 1595 } else
882e6893 1596 strcpy(fbuf, f->basename);
b7736c79 1597 return fbuf;
8018edd3 1598}
e03dfae5 1599
3ec4dd97 1600
8018edd3
WD
1601/* Like f_name_to(), but we rotate through 5 static buffers of our own.
1602 */
1603char *f_name(struct file_struct *f)
1604{
1605 static char names[5][MAXPATHLEN];
1606 static unsigned int n;
1607
1608 n = (n + 1) % (sizeof names / sizeof names[0]);
1609
882e6893 1610 return f_name_to(f, names[n]);
c627d613 1611}