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