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