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