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