Got rid of a superfluous empty line.
[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;
51extern int preserve_uid;
52extern int preserve_gid;
6574b4f7 53extern int relative_paths;
24d0fcde 54extern int implied_dirs;
82306bf6 55extern int copy_links;
b5313607 56extern int copy_unsafe_links;
d04e9c51 57extern int protocol_version;
cb13abfe 58extern int sanitize_paths;
314f4591 59extern int orig_umask;
d64e6f42
WD
60extern struct stats stats;
61extern struct file_list *the_file_list;
c627d613 62
e1f40891
WD
63extern char curr_dir[MAXPATHLEN];
64
2b7e0f33
WD
65extern struct chmod_mode_struct *chmod_modes;
66
7842418b
WD
67extern struct filter_list_struct filter_list;
68extern struct filter_list_struct server_filter_list;
c627d613 69
06c28400 70int io_error;
7752df41 71int checksum_len;
32cbfe7b 72dev_t filesystem_dev; /* used to implement -x */
06c28400 73
fea4db62 74static char empty_sum[MD4_SUM_LENGTH];
53135fe8 75static int flist_count_offset;
9935066b 76static unsigned int file_struct_len;
d64e6f42 77static struct file_list *sorting_flist;
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),
71903f60 146 safe_fname(f_name(f)), safe_fname(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),
54b40598 153 safe_fname(f_name(f)));
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",
71903f60 186 safe_fname(path), safe_fname(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
7b1a0c19 310void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
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
f376e674 339 f_name_to(file, fname);
72914a60
AT
340
341 flags = base_flags;
342
30f337c9 343 if (file->mode == mode)
d01d15e0 344 flags |= XMIT_SAME_MODE;
1ef00d20 345 else
30f337c9 346 mode = file->mode;
75bc8600
WD
347 if (preserve_devices) {
348 if (protocol_version < 28) {
728d0922 349 if (IS_DEVICE(mode)) {
9c5e91f8
WD
350 if (file->u.rdev == rdev)
351 flags |= XMIT_SAME_RDEV_pre28;
352 else
728d0922
WD
353 rdev = file->u.rdev;
354 } else
9c5e91f8 355 rdev = makedev(0, 0);
728d0922 356 } else if (IS_DEVICE(mode)) {
84a3efa0 357 rdev = file->u.rdev;
9c5e91f8
WD
358 if ((uint32)major(rdev) == rdev_major)
359 flags |= XMIT_SAME_RDEV_MAJOR;
84a3efa0 360 else
9c5e91f8
WD
361 rdev_major = major(rdev);
362 if ((uint32)minor(rdev) <= 0xFFu)
363 flags |= XMIT_RDEV_MINOR_IS_SMALL;
75bc8600
WD
364 }
365 }
30f337c9 366 if (file->uid == uid)
d01d15e0 367 flags |= XMIT_SAME_UID;
1ef00d20 368 else
30f337c9
WD
369 uid = file->uid;
370 if (file->gid == gid)
d01d15e0 371 flags |= XMIT_SAME_GID;
1ef00d20 372 else
30f337c9
WD
373 gid = file->gid;
374 if (file->modtime == modtime)
d01d15e0 375 flags |= XMIT_SAME_TIME;
1ef00d20 376 else
30f337c9 377 modtime = file->modtime;
a289addd 378
4f5b0756 379#ifdef SUPPORT_HARD_LINKS
92cc9dd7
WD
380 if (file->link_u.idev) {
381 if (file->F_DEV == dev) {
c4b4df4f 382 if (protocol_version >= 28)
d01d15e0 383 flags |= XMIT_SAME_DEV;
728d0922 384 } else
92cc9dd7 385 dev = file->F_DEV;
d01d15e0 386 flags |= XMIT_HAS_IDEV_DATA;
c4b4df4f 387 }
0d162bd1 388#endif
ebed4c3a
MP
389
390 for (l1 = 0;
3e491682
S
391 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
392 l1++) {}
eddd5d12 393 l2 = strlen(fname+l1);
72914a60 394
ebed4c3a 395 if (l1 > 0)
d01d15e0 396 flags |= XMIT_SAME_NAME;
ebed4c3a 397 if (l2 > 255)
d01d15e0 398 flags |= XMIT_LONG_NAME;
72914a60 399
1aa4caf3
WD
400 /* We must make sure we don't send a zero flag byte or the
401 * other end will terminate the flist transfer. Note that
ee3751c8 402 * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
1aa4caf3 403 * it's harmless way to add a bit to the first flag byte. */
75bc8600 404 if (protocol_version >= 28) {
1aa4caf3 405 if (!flags && !S_ISDIR(mode))
ee3751c8 406 flags |= XMIT_TOP_DIR;
1aa4caf3 407 if ((flags & 0xFF00) || !flags) {
d01d15e0 408 flags |= XMIT_EXTENDED_FLAGS;
75bc8600
WD
409 write_byte(f, flags);
410 write_byte(f, flags >> 8);
411 } else
412 write_byte(f, flags);
413 } else {
0a982011 414 if (!(flags & 0xFF))
75c51953 415 flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
75bc8600
WD
416 write_byte(f, flags);
417 }
d01d15e0 418 if (flags & XMIT_SAME_NAME)
ebed4c3a 419 write_byte(f, l1);
d01d15e0 420 if (flags & XMIT_LONG_NAME)
ebed4c3a 421 write_int(f, l2);
72914a60 422 else
ebed4c3a
MP
423 write_byte(f, l2);
424 write_buf(f, fname + l1, l2);
72914a60 425
ebed4c3a 426 write_longint(f, file->length);
d01d15e0 427 if (!(flags & XMIT_SAME_TIME))
30f337c9 428 write_int(f, modtime);
d01d15e0 429 if (!(flags & XMIT_SAME_MODE))
30f337c9 430 write_int(f, to_wire_mode(mode));
d01d15e0 431 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
9f7b8c3b
WD
432 if (!numeric_ids)
433 add_uid(uid);
30f337c9 434 write_int(f, uid);
72914a60 435 }
d01d15e0 436 if (preserve_gid && !(flags & XMIT_SAME_GID)) {
9f7b8c3b
WD
437 if (!numeric_ids)
438 add_gid(gid);
30f337c9 439 write_int(f, gid);
72914a60 440 }
30f337c9 441 if (preserve_devices && IS_DEVICE(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",
71903f60 537 flags, l1, l2, safe_fname(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
a289addd
WD
573 if (preserve_uid && !(flags & XMIT_SAME_UID))
574 uid = (uid_t)read_int(f);
575 if (preserve_gid && !(flags & XMIT_SAME_GID))
576 gid = (gid_t)read_int(f);
577
1ef00d20 578 if (preserve_devices) {
75bc8600 579 if (protocol_version < 28) {
30f337c9 580 if (IS_DEVICE(mode)) {
d01d15e0 581 if (!(flags & XMIT_SAME_RDEV_pre28))
9c5e91f8 582 rdev = (dev_t)read_int(f);
75bc8600 583 } else
9c5e91f8 584 rdev = makedev(0, 0);
30f337c9 585 } else if (IS_DEVICE(mode)) {
9c5e91f8
WD
586 uint32 rdev_minor;
587 if (!(flags & XMIT_SAME_RDEV_MAJOR))
588 rdev_major = read_int(f);
589 if (flags & XMIT_RDEV_MINOR_IS_SMALL)
590 rdev_minor = read_byte(f);
591 else
592 rdev_minor = read_int(f);
593 rdev = makedev(rdev_major, rdev_minor);
1ef00d20 594 }
b7736c79 595 }
72914a60 596
4f5b0756 597#ifdef SUPPORT_LINKS
30f337c9 598 if (preserve_links && S_ISLNK(mode)) {
a289addd
WD
599 linkname_len = read_int(f) + 1; /* count the '\0' */
600 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
601 rprintf(FERROR, "overflow: linkname_len=%d\n",
602 linkname_len - 1);
a1f99493 603 overflow_exit("receive_file_entry");
9dd891bb 604 }
72914a60 605 }
a289addd 606 else
0d162bd1 607#endif
a289addd 608 linkname_len = 0;
0d162bd1 609
a289addd
WD
610 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
611
61dec11a 612 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
613 + linkname_len + sum_len;
614 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
615
f5db0993 616 file = (struct file_struct *)bp;
9935066b 617 memset(bp, 0, file_struct_len);
61dec11a 618 bp += file_struct_len;
a289addd 619
649f8742 620 file->flags = 0;
a289addd
WD
621 file->modtime = modtime;
622 file->length = file_length;
623 file->mode = mode;
624 file->uid = uid;
625 file->gid = gid;
626
f3c3ed44
WD
627 if (dirname_len) {
628 file->dirname = lastdir = bp;
629 lastdir_len = dirname_len - 1;
630 memcpy(bp, dirname, dirname_len - 1);
631 bp += dirname_len;
632 bp[-1] = '\0';
633 lastdir_depth = count_dir_elements(lastdir);
634 file->dir.depth = lastdir_depth + 1;
635 } else if (dirname) {
636 file->dirname = dirname; /* we're reusing lastname */
637 file->dir.depth = lastdir_depth + 1;
638 } else
639 file->dir.depth = 1;
640
649f8742 641 if (S_ISDIR(mode)) {
ee3751c8 642 if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
f3c3ed44 643 file->dir.depth--;
ee3751c8 644 if (flags & XMIT_TOP_DIR) {
75c51953 645 in_del_hier = recurse;
f3c3ed44 646 del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
7a16e122
WD
647 if (relative_paths && del_hier_name_len > 2
648 && basename_len == 1+1 && *basename == '.')
649 del_hier_name_len -= 2;
ee3751c8
WD
650 file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE;
651 } else if (in_del_hier) {
f3c3ed44
WD
652 if (!relative_paths || !del_hier_name_len
653 || (l1 >= del_hier_name_len
654 && thisname[del_hier_name_len] == '/'))
ee3751c8 655 file->flags |= FLAG_DEL_HERE;
649f8742
WD
656 else
657 in_del_hier = 0;
658 }
659 }
660
a289addd
WD
661 file->basename = bp;
662 memcpy(bp, basename, basename_len);
663 bp += basename_len;
664
665 if (preserve_devices && IS_DEVICE(mode))
666 file->u.rdev = rdev;
667
4f5b0756 668#ifdef SUPPORT_LINKS
a289addd
WD
669 if (linkname_len) {
670 file->u.link = bp;
671 read_sbuf(f, bp, linkname_len - 1);
672 if (sanitize_paths)
33ffd7c3 673 sanitize_path(bp, bp, "", lastdir_depth);
a289addd
WD
674 bp += linkname_len;
675 }
676#endif
677
4f5b0756 678#ifdef SUPPORT_HARD_LINKS
9935066b
S
679 if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
680 flags |= XMIT_HAS_IDEV_DATA;
97a67bdf 681 if (flags & XMIT_HAS_IDEV_DATA) {
1490812a 682 int64 inode;
4124540d
WD
683 if (protocol_version < 26) {
684 dev = read_int(f);
9935066b 685 inode = read_int(f);
736a6a29 686 } else {
4124540d
WD
687 if (!(flags & XMIT_SAME_DEV))
688 dev = read_longint(f);
9935066b
S
689 inode = read_longint(f);
690 }
691 if (flist->hlink_pool) {
5bf63a11
S
692 file->link_u.idev = pool_talloc(flist->hlink_pool,
693 struct idev, 1, "inode_table");
9935066b
S
694 file->F_INODE = inode;
695 file->F_DEV = dev;
736a6a29 696 }
72914a60 697 }
dc5ddbcc 698#endif
ebed4c3a 699
9a4a237e 700 if (always_checksum && (sum_len || protocol_version < 28)) {
7c4f063b 701 char *sum;
a289addd
WD
702 if (sum_len) {
703 file->u.sum = sum = bp;
704 /*bp += sum_len;*/
9a4a237e 705 } else {
fea4db62 706 /* Prior to 28, we get a useless set of nulls. */
7c4f063b 707 sum = empty_sum;
fea4db62 708 }
7752df41 709 read_buf(f, sum, checksum_len);
72914a60 710 }
ebed4c3a 711
72914a60 712 if (!preserve_perms) {
72914a60 713 /* set an appropriate set of permissions based on original
fea4db62 714 * permissions and umask. This emulates what GNU cp does */
72914a60
AT
715 file->mode &= ~orig_umask;
716 }
f5db0993
WD
717
718 return file;
c627d613
AT
719}
720
db719fb0
MP
721/**
722 * Create a file_struct for a named file by reading its stat()
723 * information and performing extensive checks against global
724 * options.
725 *
726 * @return the new file, or NULL if there was an error or this file
727 * should be excluded.
728 *
729 * @todo There is a small optimization opportunity here to avoid
730 * stat()ing the file in some circumstances, which has a certain cost.
731 * We are called immediately after doing readdir(), and so we may
732 * already know the d_type of the file. We could for example avoid
733 * statting directories if we're not recursing, but this is not a very
734 * important case. Some systems may not have d_type.
735 **/
314f4591 736struct file_struct *make_file(char *fname, struct file_list *flist,
7842418b 737 int filter_level)
c627d613 738{
a289addd
WD
739 static char *lastdir;
740 static int lastdir_len = -1;
3ec4dd97 741 struct file_struct *file;
bcacc18b 742 STRUCT_STAT st;
3ec4dd97 743 char sum[SUM_LENGTH];
1923b1fc 744 char thisname[MAXPATHLEN];
e0870f1d 745 char linkname[MAXPATHLEN];
a1d55ad0 746 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
a289addd 747 char *basename, *dirname, *bp;
4844449a 748 unsigned short flags = 0;
3ec4dd97 749
d508258a 750 if (!flist || !flist->count) /* Ignore lastdir when invalid. */
7de2483f 751 lastdir_len = -1;
9935066b 752
1923b1fc
WD
753 if (strlcpy(thisname, fname, sizeof thisname)
754 >= sizeof thisname - flist_dir_len) {
71903f60
WD
755 rprintf(FINFO, "skipping overly long name: %s\n",
756 safe_fname(fname));
882e6893
WD
757 return NULL;
758 }
58b1999e 759 clean_fname(thisname, 0);
b7736c79 760 if (sanitize_paths)
33ffd7c3 761 sanitize_path(thisname, thisname, "", 0);
3ec4dd97 762
ebed4c3a 763 memset(sum, 0, SUM_LENGTH);
3ec4dd97 764
e0870f1d 765 if (readlink_stat(thisname, &st, linkname) != 0) {
76e26e10 766 int save_errno = errno;
a4a7e64c 767 /* See if file is excluded before reporting an error. */
7842418b
WD
768 if (filter_level != NO_FILTERS
769 && is_excluded(thisname, 0, filter_level))
a4a7e64c
WD
770 return NULL;
771 if (save_errno == ENOENT) {
4f5b0756 772#ifdef SUPPORT_LINKS
a4a7e64c
WD
773 /* Avoid "vanished" error if symlink points nowhere. */
774 if (copy_links && do_lstat(thisname, &st) == 0
775 && S_ISLNK(st.st_mode)) {
776 io_error |= IOERR_GENERAL;
777 rprintf(FERROR, "symlink has no referent: %s\n",
778 full_fname(thisname));
e5ce3bcf
WD
779 } else
780#endif
781 {
a4a7e64c
WD
782 enum logcode c = am_daemon && protocol_version < 28
783 ? FERROR : FINFO;
784 io_error |= IOERR_VANISHED;
785 rprintf(c, "file has vanished: %s\n",
786 full_fname(thisname));
76e26e10 787 }
a4a7e64c 788 } else {
a8726d2a 789 io_error |= IOERR_GENERAL;
d62bcc17
WD
790 rsyserr(FERROR, save_errno, "readlink %s failed",
791 full_fname(thisname));
76e26e10 792 }
3ec4dd97
AT
793 return NULL;
794 }
c627d613 795
7842418b
WD
796 /* backup.c calls us with filter_level set to NO_FILTERS. */
797 if (filter_level == NO_FILTERS)
798 goto skip_filters;
ac1a0994 799
7e037c42 800 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
71903f60 801 rprintf(FINFO, "skipping directory %s\n", safe_fname(thisname));
3ec4dd97
AT
802 return NULL;
803 }
ebed4c3a 804
e90cdb8a
WD
805 /* We only care about directories because we need to avoid recursing
806 * into a mount-point directory, not to avoid copying a symlinked
807 * file if -L (or similar) was specified. */
808 if (one_file_system && st.st_dev != filesystem_dev
809 && S_ISDIR(st.st_mode))
4844449a 810 flags |= FLAG_MOUNT_POINT;
ebed4c3a 811
7842418b 812 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level))
76e26e10
DD
813 return NULL;
814
132fcf36 815 if (lp_ignore_nonreadable(module_id)) {
4f5b0756 816#ifdef SUPPORT_LINKS
132fcf36
WD
817 if (!S_ISLNK(st.st_mode))
818#endif
819 if (access(thisname, R_OK) != 0)
820 return NULL;
821 }
ac1a0994 822
902f03d1 823 skip_filters:
ac1a0994 824
ea847c62
WD
825 if (verbose > 2) {
826 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
71903f60 827 who_am_i(), safe_fname(thisname), filter_level);
ea847c62 828 }
ebed4c3a 829
a289addd
WD
830 if ((basename = strrchr(thisname, '/')) != NULL) {
831 dirname_len = ++basename - thisname; /* counts future '\0' */
832 if (lastdir_len == dirname_len - 1
833 && strncmp(thisname, lastdir, lastdir_len) == 0) {
834 dirname = lastdir;
835 dirname_len = 0; /* indicates no copy is needed */
836 } else
837 dirname = thisname;
3ec4dd97 838 } else {
a289addd
WD
839 basename = thisname;
840 dirname = NULL;
841 dirname_len = 0;
3ec4dd97 842 }
a289addd 843 basename_len = strlen(basename) + 1; /* count the '\0' */
c627d613 844
4f5b0756 845#ifdef SUPPORT_LINKS
a289addd
WD
846 linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
847#else
848 linkname_len = 0;
849#endif
850
902f03d1
WD
851 sum_len = always_checksum && am_sender && S_ISREG(st.st_mode)
852 ? MD4_SUM_LENGTH : 0;
a289addd 853
61dec11a 854 alloc_len = file_struct_len + dirname_len + basename_len
9935066b
S
855 + linkname_len + sum_len;
856 if (flist) {
857 bp = pool_alloc(flist->file_pool, alloc_len,
858 "receive_file_entry");
859 } else {
860 if (!(bp = new_array(char, alloc_len)))
861 out_of_memory("receive_file_entry");
862 }
863
a289addd 864 file = (struct file_struct *)bp;
9935066b 865 memset(bp, 0, file_struct_len);
61dec11a 866 bp += file_struct_len;
a289addd
WD
867
868 file->flags = flags;
3ec4dd97
AT
869 file->modtime = st.st_mtime;
870 file->length = st.st_size;
2b7e0f33
WD
871 if (chmod_modes && am_sender && (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
872 file->mode = tweak_mode(st.st_mode, chmod_modes);
873 else
874 file->mode = st.st_mode;
3ec4dd97
AT
875 file->uid = st.st_uid;
876 file->gid = st.st_gid;
0d162bd1 877
4f5b0756 878#ifdef SUPPORT_HARD_LINKS
9935066b
S
879 if (flist && flist->hlink_pool) {
880 if (protocol_version < 28) {
881 if (S_ISREG(st.st_mode))
882 file->link_u.idev = pool_talloc(
883 flist->hlink_pool, struct idev, 1,
884 "inode_table");
885 } else {
886 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
887 file->link_u.idev = pool_talloc(
888 flist->hlink_pool, struct idev, 1,
889 "inode_table");
890 }
891 }
892 if (file->link_u.idev) {
61dec11a
WD
893 file->F_DEV = st.st_dev;
894 file->F_INODE = st.st_ino;
895 }
896#endif
897
a289addd
WD
898 if (dirname_len) {
899 file->dirname = lastdir = bp;
900 lastdir_len = dirname_len - 1;
901 memcpy(bp, dirname, dirname_len - 1);
902 bp += dirname_len;
903 bp[-1] = '\0';
904 } else if (dirname)
905 file->dirname = dirname;
906
907 file->basename = bp;
908 memcpy(bp, basename, basename_len);
909 bp += basename_len;
910
4f5b0756 911#ifdef HAVE_STRUCT_STAT_ST_RDEV
0d162bd1
WD
912 if (preserve_devices && IS_DEVICE(st.st_mode))
913 file->u.rdev = st.st_rdev;
914#endif
915
4f5b0756 916#ifdef SUPPORT_LINKS
a289addd
WD
917 if (linkname_len) {
918 file->u.link = bp;
919 memcpy(bp, linkname, linkname_len);
920 bp += linkname_len;
921 }
0d162bd1
WD
922#endif
923
a289addd
WD
924 if (sum_len) {
925 file->u.sum = bp;
926 file_checksum(thisname, bp, st.st_size);
927 /*bp += sum_len;*/
ebed4c3a 928 }
c627d613 929
f3c3ed44 930 file->dir.root = flist_dir;
c627d613 931
23f4587f
WD
932 /* This code is only used by the receiver when it is building
933 * a list of files for a delete pass. */
934 if (keep_dirlinks && linkname_len && flist) {
935 STRUCT_STAT st2;
f5db0993
WD
936 int save_mode = file->mode;
937 file->mode = S_IFDIR; /* find a directory w/our name */
d64e6f42 938 if (flist_find(the_file_list, file) >= 0
23f4587f
WD
939 && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
940 file->modtime = st2.st_mtime;
941 file->length = st2.st_size;
942 file->mode = st2.st_mode;
943 file->uid = st2.st_uid;
944 file->gid = st2.st_gid;
945 file->u.link = NULL;
f5db0993
WD
946 } else
947 file->mode = save_mode;
23f4587f
WD
948 }
949
d17190df 950 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
a800434a 951 stats.total_size += st.st_size;
c627d613 952
3ec4dd97 953 return file;
c627d613
AT
954}
955
301fb56c
WD
956static struct file_struct *send_file_name(int f, struct file_list *flist,
957 char *fname, unsigned short base_flags)
c627d613 958{
ebed4c3a
MP
959 struct file_struct *file;
960
37802f40
WD
961 file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
962 if (!file)
301fb56c 963 return NULL;
ebed4c3a 964
53135fe8 965 maybe_emit_filelist_progress(flist->count + flist_count_offset);
ebed4c3a 966
d9d6bc52 967 flist_expand(flist);
ebed4c3a 968
c120ff37 969 if (file->basename[0]) {
ebed4c3a
MP
970 flist->files[flist->count++] = file;
971 send_file_entry(file, f, base_flags);
972 }
301fb56c
WD
973 return file;
974}
ebed4c3a 975
301fb56c 976static void send_if_directory(int f, struct file_list *flist,
56f0c976
WD
977 struct file_struct *file,
978 char *fbuf, unsigned int ol)
301fb56c 979{
56f0c976 980 char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
301fb56c
WD
981
982 if (S_ISDIR(file->mode)
f1773e09
WD
983 && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
984 void *save_filters;
985 unsigned int len = strlen(fbuf);
986 if (len > 1 && fbuf[len-1] == '/')
987 fbuf[--len] = '\0';
988 if (len >= MAXPATHLEN - 1) {
989 io_error |= IOERR_GENERAL;
990 rprintf(FERROR, "skipping long-named directory: %s\n",
991 full_fname(fbuf));
992 return;
993 }
994 save_filters = push_local_filters(fbuf, len);
995 send_directory(f, flist, fbuf, len);
996 pop_local_filters(save_filters);
56f0c976
WD
997 fbuf[ol] = '\0';
998 if (is_dot_dir)
999 fbuf[ol-1] = '.';
ebed4c3a 1000 }
c627d613
AT
1001}
1002
301fb56c 1003/* This function is normally called by the sender, but the receiving side also
7b558d7f 1004 * calls it from get_dirlist() with f set to -1 so that we just construct the
301fb56c
WD
1005 * file list in memory without sending it over the wire. Also, get_dirlist()
1006 * might call this with f set to -2, which also indicates that local filter
1007 * rules should be ignored. */
f1773e09 1008static void send_directory(int f, struct file_list *flist,
32cbfe7b 1009 char *fbuf, int len)
c627d613 1010{
3ec4dd97 1011 struct dirent *di;
32cbfe7b 1012 unsigned remainder;
3ec4dd97 1013 char *p;
f1773e09 1014 DIR *d;
301fb56c 1015 int start = flist->count;
3ec4dd97 1016
f1773e09 1017 if (!(d = opendir(fbuf))) {
06c28400 1018 io_error |= IOERR_GENERAL;
f1773e09 1019 rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf));
3ec4dd97
AT
1020 return;
1021 }
c627d613 1022
19b2a5d9
WD
1023 p = fbuf + len;
1024 if (len != 1 || *fbuf != '/')
eddd5d12 1025 *p++ = '/';
f1773e09 1026 *p = '\0';
32cbfe7b 1027 remainder = MAXPATHLEN - (p - fbuf);
ebed4c3a 1028
6a7cc46c 1029 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
d6e6ecbd 1030 char *dname = d_name(di);
6a7cc46c
S
1031 if (dname[0] == '.' && (dname[1] == '\0'
1032 || (dname[1] == '.' && dname[2] == '\0')))
3ec4dd97 1033 continue;
301fb56c
WD
1034 if (strlcpy(p, dname, remainder) < remainder)
1035 send_file_name(f, flist, fbuf, 0);
1036 else {
eddd5d12
WD
1037 io_error |= IOERR_GENERAL;
1038 rprintf(FINFO,
1039 "cannot send long-named file %s\n",
f1773e09 1040 full_fname(fbuf));
eddd5d12 1041 }
3ec4dd97 1042 }
32cbfe7b
WD
1043
1044 fbuf[len] = '\0';
1045
6a7cc46c 1046 if (errno) {
06c28400 1047 io_error |= IOERR_GENERAL;
71903f60 1048 rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf));
6a7cc46c 1049 }
c627d613 1050
3ec4dd97 1051 closedir(d);
301fb56c
WD
1052
1053 if (recurse) {
1054 int i, end = flist->count - 1;
1055 for (i = start; i <= end; i++)
56f0c976 1056 send_if_directory(f, flist, flist->files[i], fbuf, len);
301fb56c 1057 }
c627d613
AT
1058}
1059
ebed4c3a 1060struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 1061{
56f0c976 1062 int len;
bcacc18b 1063 STRUCT_STAT st;
808c57c3 1064 char *p, *dir, olddir[sizeof curr_dir];
ebed4c3a 1065 char lastpath[MAXPATHLEN] = "";
649d65ed 1066 struct file_list *flist;
31b4d25d 1067 struct timeval start_tv, end_tv;
a800434a 1068 int64 start_write;
24d0fcde 1069 int use_ff_fd = 0;
649d65ed 1070
134f4338 1071 if (show_filelist_p())
1bbd10fe 1072 start_filelist_progress("building file list");
c627d613 1073
a800434a 1074 start_write = stats.total_written;
31b4d25d 1075 gettimeofday(&start_tv, NULL);
a800434a 1076
134f4338 1077 flist = flist_new(WITH_HLINK, "send_file_list");
c627d613 1078
134f4338
WD
1079 io_start_buffering_out();
1080 if (filesfrom_fd >= 0) {
1081 if (argv[0] && !push_dir(argv[0])) {
1082 rsyserr(FERROR, errno, "push_dir %s failed",
1083 full_fname(argv[0]));
1084 exit_cleanup(RERR_FILESELECT);
24d0fcde 1085 }
134f4338 1086 use_ff_fd = 1;
d6dead6b
AT
1087 }
1088
24d0fcde 1089 while (1) {
56f0c976
WD
1090 char fbuf[MAXPATHLEN];
1091 char *fn;
301fb56c 1092 int is_dot_dir;
c627d613 1093
24d0fcde 1094 if (use_ff_fd) {
56f0c976 1095 if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
24d0fcde 1096 break;
56f0c976 1097 sanitize_path(fbuf, fbuf, "", 0);
24d0fcde
WD
1098 } else {
1099 if (argc-- == 0)
1100 break;
56f0c976 1101 strlcpy(fbuf, *argv++, MAXPATHLEN);
24d0fcde 1102 if (sanitize_paths)
56f0c976 1103 sanitize_path(fbuf, fbuf, "", 0);
24d0fcde 1104 }
c627d613 1105
56f0c976
WD
1106 len = strlen(fbuf);
1107 if (!len || fbuf[len - 1] == '/') {
1108 if (len == 2 && fbuf[0] == '.') {
6931c138 1109 /* Turn "./" into just "." rather than "./." */
56f0c976 1110 fbuf[1] = '\0';
557a35f5 1111 } else {
56f0c976 1112 if (len + 1 >= MAXPATHLEN)
a1f99493 1113 overflow_exit("send_file_list");
56f0c976
WD
1114 fbuf[len++] = '.';
1115 fbuf[len] = '\0';
53f821f1 1116 }
301fb56c 1117 is_dot_dir = 1;
56f0c976
WD
1118 } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
1119 && (len == 2 || fbuf[len-3] == '/')) {
1120 if (len + 2 >= MAXPATHLEN)
a1f99493 1121 overflow_exit("send_file_list");
56f0c976
WD
1122 fbuf[len++] = '/';
1123 fbuf[len++] = '.';
1124 fbuf[len] = '\0';
a289f89f 1125 is_dot_dir = 1;
301fb56c 1126 } else {
56f0c976
WD
1127 is_dot_dir = fbuf[len-1] == '.'
1128 && (len == 1 || fbuf[len-2] == '/');
649d65ed 1129 }
c627d613 1130
56f0c976 1131 if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
134f4338
WD
1132 io_error |= IOERR_GENERAL;
1133 rsyserr(FERROR, errno, "link_stat %s failed",
56f0c976 1134 full_fname(fbuf));
649d65ed
AT
1135 continue;
1136 }
c627d613 1137
7e037c42 1138 if (S_ISDIR(st.st_mode) && !xfer_dirs) {
71903f60 1139 rprintf(FINFO, "skipping directory %s\n",
56f0c976 1140 safe_fname(fbuf));
649d65ed
AT
1141 continue;
1142 }
c627d613 1143
649d65ed 1144 dir = NULL;
808c57c3 1145 olddir[0] = '\0';
649d65ed
AT
1146
1147 if (!relative_paths) {
56f0c976 1148 p = strrchr(fbuf, '/');
649d65ed 1149 if (p) {
151f59f1 1150 *p = '\0';
56f0c976 1151 if (p == fbuf)
649d65ed
AT
1152 dir = "/";
1153 else
56f0c976
WD
1154 dir = fbuf;
1155 len -= p - fbuf + 1;
1156 fn = p + 1;
1157 } else
1158 fn = fbuf;
1159 } else if ((p = strstr(fbuf, "/./")) != NULL) {
d2ea5980 1160 *p = '\0';
56f0c976 1161 if (p == fbuf)
d2ea5980
WD
1162 dir = "/";
1163 else
56f0c976
WD
1164 dir = fbuf;
1165 len -= p - fbuf + 3;
1166 fn = p + 3;
1167 } else
1168 fn = fbuf;
d2ea5980 1169
56f0c976
WD
1170 if (!*fn) {
1171 len = 1;
1172 fn = ".";
1173 }
d2ea5980
WD
1174
1175 if (dir && *dir) {
1176 static char *lastdir;
1177 static int lastdir_len;
1178
8ad5cea3 1179 strlcpy(olddir, curr_dir, sizeof olddir);
d2ea5980
WD
1180
1181 if (!push_dir(dir)) {
1182 io_error |= IOERR_GENERAL;
1183 rsyserr(FERROR, errno, "push_dir %s failed",
1184 full_fname(dir));
1185 continue;
1186 }
1187
1188 if (lastdir && strcmp(lastdir, dir) == 0) {
1189 flist_dir = lastdir;
1190 flist_dir_len = lastdir_len;
1191 } else {
1192 flist_dir = lastdir = strdup(dir);
1193 flist_dir_len = lastdir_len = strlen(dir);
1194 }
1195 }
1196
56f0c976
WD
1197 if (fn != fbuf)
1198 memmove(fbuf, fn, len + 1);
1199
1200 if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
151f59f1
WD
1201 /* Send the implied directories at the start of the
1202 * source spec, so we get their permissions right. */
56f0c976 1203 char *lp = lastpath, *slash = fbuf;
151f59f1 1204 *p = '\0';
2154309a
WD
1205 /* Skip any initial directories in our path that we
1206 * have in common with lastpath. */
56f0c976 1207 for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
2154309a
WD
1208 if (*fn == '/')
1209 slash = fn;
2154309a
WD
1210 }
1211 *p = '/';
1212 if (fn != p || (*lp && *lp != '/')) {
0f57446d 1213 int save_copy_links = copy_links;
7e037c42 1214 int save_xfer_dirs = xfer_dirs;
2154309a 1215 copy_links = copy_unsafe_links;
7e037c42 1216 xfer_dirs = 1;
2154309a 1217 while ((slash = strchr(slash+1, '/')) != 0) {
151f59f1 1218 *slash = '\0';
56f0c976 1219 send_file_name(f, flist, fbuf, 0);
2154309a 1220 *slash = '/';
649d65ed 1221 }
0f57446d 1222 copy_links = save_copy_links;
7e037c42 1223 xfer_dirs = save_xfer_dirs;
151f59f1 1224 *p = '\0';
56f0c976 1225 strlcpy(lastpath, fbuf, sizeof lastpath);
649d65ed
AT
1226 *p = '/';
1227 }
1228 }
ebed4c3a 1229
2bca43f6 1230 if (one_file_system)
45478cc7 1231 filesystem_dev = st.st_dev;
2bca43f6 1232
75c51953
WD
1233 if (recurse || (xfer_dirs && is_dot_dir)) {
1234 struct file_struct *file;
56f0c976
WD
1235 if ((file = send_file_name(f, flist, fbuf, XMIT_TOP_DIR)))
1236 send_if_directory(f, flist, file, fbuf, len);
75c51953 1237 } else
56f0c976 1238 send_file_name(f, flist, fbuf, 0);
2bca43f6 1239
808c57c3 1240 if (olddir[0]) {
649d65ed 1241 flist_dir = NULL;
882e6893 1242 flist_dir_len = 0;
808c57c3 1243 if (!pop_dir(olddir)) {
d62bcc17 1244 rsyserr(FERROR, errno, "pop_dir %s failed",
92cdc393 1245 full_fname(olddir));
65417579 1246 exit_cleanup(RERR_FILESELECT);
649d65ed 1247 }
649d65ed 1248 }
649d65ed 1249 }
dc5ddbcc 1250
134f4338
WD
1251 gettimeofday(&end_tv, NULL);
1252 stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1253 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
1254 if (stats.flist_buildtime == 0)
1255 stats.flist_buildtime = 1;
1256 start_tv = end_tv;
31b4d25d 1257
134f4338 1258 send_file_entry(NULL, f, 0);
c627d613 1259
134f4338
WD
1260 if (show_filelist_p())
1261 finish_filelist_progress(flist);
31b4d25d 1262
134f4338
WD
1263 gettimeofday(&end_tv, NULL);
1264 stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
1265 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
ebed4c3a 1266
7cf8e8d0 1267 if (flist->hlink_pool) {
9935066b
S
1268 pool_destroy(flist->hlink_pool);
1269 flist->hlink_pool = NULL;
1270 }
1271
f5db0993
WD
1272 /* Sort the list without removing any duplicates. This allows the
1273 * receiving side to ask for any name they like, which gives us the
1274 * flexibility to change the way we unduplicate names in the future
1275 * without causing a compatibility problem with older versions. */
827c37f6 1276 clean_flist(flist, 0, 0);
ebed4c3a 1277
134f4338
WD
1278 /* Now send the uid/gid list. This was introduced in
1279 * protocol version 15 */
1280 send_uid_list(f);
f6c34742 1281
134f4338
WD
1282 /* send the io_error flag */
1283 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f 1284
134f4338
WD
1285 io_end_buffering();
1286 stats.flist_size = stats.total_written - start_write;
1287 stats.num_files = flist->count;
d6dead6b 1288
cefed3e8 1289 if (verbose > 3)
32cbfe7b 1290 output_flist(flist);
cefed3e8 1291
17faa41c 1292 if (verbose > 2)
ebed4c3a 1293 rprintf(FINFO, "send_file_list done\n");
17faa41c 1294
649d65ed 1295 return flist;
c627d613
AT
1296}
1297
c627d613
AT
1298struct file_list *recv_file_list(int f)
1299{
ebed4c3a 1300 struct file_list *flist;
1ef00d20 1301 unsigned short flags;
ebed4c3a 1302 int64 start_read;
c627d613 1303
1bbd10fe
DD
1304 if (show_filelist_p())
1305 start_filelist_progress("receiving file list");
c627d613 1306
ebed4c3a 1307 start_read = stats.total_read;
a800434a 1308
9935066b 1309 flist = flist_new(WITH_HLINK, "recv_file_list");
c627d613 1310
ebed4c3a
MP
1311 flist->count = 0;
1312 flist->malloced = 1000;
58cadc86 1313 flist->files = new_array(struct file_struct *, flist->malloced);
ebed4c3a
MP
1314 if (!flist->files)
1315 goto oom;
c627d613
AT
1316
1317
1ef00d20 1318 while ((flags = read_byte(f)) != 0) {
f5db0993 1319 struct file_struct *file;
dbda5fbf 1320
d9d6bc52 1321 flist_expand(flist);
c627d613 1322
d01d15e0 1323 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
75bc8600 1324 flags |= read_byte(f) << 8;
f5db0993 1325 file = receive_file_entry(flist, flags, f);
c627d613 1326
f5db0993
WD
1327 if (S_ISREG(file->mode))
1328 stats.total_size += file->length;
c627d613 1329
f5db0993 1330 flist->files[flist->count++] = file;
c627d613 1331
53135fe8 1332 maybe_emit_filelist_progress(flist->count);
1bbd10fe 1333
8018edd3 1334 if (verbose > 2) {
ebed4c3a 1335 rprintf(FINFO, "recv_file_name(%s)\n",
f5db0993 1336 safe_fname(f_name(file)));
8018edd3 1337 }
ebed4c3a 1338 }
f5db0993 1339 receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
c627d613 1340
ebed4c3a
MP
1341 if (verbose > 2)
1342 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1343
b7736c79 1344 if (show_filelist_p())
1bbd10fe 1345 finish_filelist_progress(flist);
a06d19e3 1346
983b1ed3
WD
1347 clean_flist(flist, relative_paths, 1);
1348
37802f40 1349 if (f >= 0) {
785db4ce
WD
1350 /* Now send the uid/gid list. This was introduced in
1351 * protocol version 15 */
ebed4c3a 1352 recv_uid_list(f, flist);
f6c34742 1353
b9f592fb
WD
1354 /* Recv the io_error flag */
1355 if (lp_ignore_errors(module_id) || ignore_errors)
1356 read_int(f);
1357 else
1358 io_error |= read_int(f);
ebed4c3a 1359 }
6ba9279f 1360
cefed3e8 1361 if (verbose > 3)
32cbfe7b 1362 output_flist(flist);
cefed3e8 1363
ebed4c3a
MP
1364 if (list_only) {
1365 int i;
b7736c79 1366 for (i = 0; i < flist->count; i++)
ebed4c3a 1367 list_file_entry(flist->files[i]);
ebed4c3a 1368 }
f7632fc6 1369
ebed4c3a
MP
1370 if (verbose > 2)
1371 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1372
ebed4c3a
MP
1373 stats.flist_size = stats.total_read - start_read;
1374 stats.num_files = flist->count;
a800434a 1375
ebed4c3a 1376 return flist;
c627d613 1377
3e491682 1378oom:
ebed4c3a
MP
1379 out_of_memory("recv_file_list");
1380 return NULL; /* not reached */
c627d613
AT
1381}
1382
14698a3a 1383static int file_compare(struct file_struct **file1, struct file_struct **file2)
c627d613 1384{
14698a3a 1385 return f_name_cmp(*file1, *file2);
c627d613
AT
1386}
1387
f5db0993
WD
1388/* Search for an identically-named item in the file list. Note that the
1389 * items must agree in their directory-ness, or no match is returned. */
2f3cad89 1390int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1391{
f3c3ed44
WD
1392 int low = flist->low, high = flist->high;
1393 int ret, mid, mid_up;
1394
1395 while (low <= high) {
1396 mid = (low + high) / 2;
1397 for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {}
1398 if (mid_up <= high)
14698a3a 1399 ret = f_name_cmp(flist->files[mid_up], f);
f3c3ed44
WD
1400 else
1401 ret = 1;
f5db0993
WD
1402 if (ret == 0) {
1403 if (protocol_version < 29
1404 && S_ISDIR(flist->files[mid_up]->mode)
1405 != S_ISDIR(f->mode))
1406 return -1;
f3c3ed44 1407 return mid_up;
f5db0993 1408 }
b7736c79 1409 if (ret > 0)
f3c3ed44 1410 high = mid - 1;
b7736c79 1411 else
f3c3ed44 1412 low = mid_up + 1;
d966ee25 1413 }
d966ee25 1414 return -1;
c627d613
AT
1415}
1416
3ec4dd97 1417/*
9935066b
S
1418 * Free up any resources a file_struct has allocated
1419 * and clear the file.
3ec4dd97 1420 */
9935066b 1421void clear_file(int i, struct file_list *flist)
c627d613 1422{
9935066b
S
1423 if (flist->hlink_pool && flist->files[i]->link_u.idev)
1424 pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev);
1425 memset(flist->files[i], 0, file_struct_len);
3ec4dd97 1426}
c627d613 1427
3d382777
AT
1428/*
1429 * allocate a new file list
1430 */
9935066b 1431struct file_list *flist_new(int with_hlink, char *msg)
3d382777
AT
1432{
1433 struct file_list *flist;
1434
58cadc86 1435 flist = new(struct file_list);
ebed4c3a 1436 if (!flist)
9935066b 1437 out_of_memory(msg);
3d382777 1438
9935066b
S
1439 memset(flist, 0, sizeof (struct file_list));
1440
1441 if (!(flist->file_pool = pool_create(FILE_EXTENT, 0,
1442 out_of_memory, POOL_INTERN)))
1443 out_of_memory(msg);
1444
4f5b0756 1445#ifdef SUPPORT_HARD_LINKS
9935066b 1446 if (with_hlink && preserve_hard_links) {
3e491682 1447 if (!(flist->hlink_pool = pool_create(HLINK_EXTENT,
9935066b
S
1448 sizeof (struct idev), out_of_memory, POOL_INTERN)))
1449 out_of_memory(msg);
1450 }
1451#endif
d9d6bc52 1452
3d382777
AT
1453 return flist;
1454}
ebed4c3a 1455
3ec4dd97
AT
1456/*
1457 * free up all elements in a flist
1458 */
1459void flist_free(struct file_list *flist)
1460{
9935066b
S
1461 pool_destroy(flist->file_pool);
1462 pool_destroy(flist->hlink_pool);
3ec4dd97 1463 free(flist->files);
3ec4dd97 1464 free(flist);
c627d613
AT
1465}
1466
c627d613
AT
1467/*
1468 * This routine ensures we don't have any duplicate names in our file list.
dbda5fbf 1469 * duplicate names can cause corruption because of the pipelining
c627d613 1470 */
827c37f6 1471static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
c627d613 1472{
6931c138 1473 int i, prev_i = 0;
c627d613 1474
910ee8c9
WD
1475 if (!flist)
1476 return;
1477 if (flist->count == 0) {
1478 flist->high = -1;
3ec4dd97 1479 return;
910ee8c9 1480 }
3ec4dd97 1481
bf0c5bec 1482 sorting_flist = flist;
ebed4c3a 1483 qsort(flist->files, flist->count,
a4a7e64c 1484 sizeof flist->files[0], (int (*)())file_compare);
bf0c5bec 1485 sorting_flist = NULL;
ebed4c3a 1486
827c37f6 1487 for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
b91b50c0 1488 if (flist->files[i]->basename) {
6931c138 1489 prev_i = i;
b91b50c0
WD
1490 break;
1491 }
1492 }
f3c3ed44 1493 flist->low = prev_i;
b91b50c0 1494 while (++i < flist->count) {
fe1c19dc 1495 int j;
f5db0993
WD
1496 struct file_struct *file = flist->files[i];
1497
1498 if (!file->basename)
b91b50c0 1499 continue;
fe1c19dc
WD
1500 if (f_name_cmp(file, flist->files[prev_i]) == 0)
1501 j = prev_i;
1502 else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
f5db0993
WD
1503 int save_mode = file->mode;
1504 /* Make sure that this directory doesn't duplicate a
1505 * non-directory earlier in the list. */
f5db0993 1506 flist->high = prev_i;
fe1c19dc
WD
1507 file->mode = S_IFREG;
1508 j = flist_find(flist, file);
f5db0993 1509 file->mode = save_mode;
fe1c19dc
WD
1510 } else
1511 j = -1;
1512 if (j >= 0) {
1513 struct file_struct *fp = flist->files[j];
1514 int keep, drop;
1515 /* If one is a dir and the other is not, we want to
1516 * keep the dir because it might have contents in the
1517 * list. */
1518 if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) {
1519 if (S_ISDIR(file->mode))
1520 keep = i, drop = j;
1521 else
1522 keep = j, drop = i;
1523 } else
1524 keep = j, drop = i;
b91b50c0 1525 if (verbose > 1 && !am_server) {
ebed4c3a 1526 rprintf(FINFO,
fe1c19dc
WD
1527 "removing duplicate name %s from file list (%d)\n",
1528 safe_fname(f_name(file)), drop);
b91b50c0 1529 }
6931c138 1530 /* Make sure that if we unduplicate '.', that we don't
ee3751c8 1531 * lose track of a user-specified top directory. */
0f0b2e66
WD
1532 flist->files[keep]->flags |= flist->files[drop]->flags
1533 & (FLAG_TOP_DIR|FLAG_DEL_HERE);
fe1c19dc
WD
1534
1535 clear_file(drop, flist);
9935066b 1536
fe1c19dc
WD
1537 if (keep == i) {
1538 if (flist->low == drop) {
1539 for (j = drop + 1;
1540 j < i && !flist->files[j]->basename;
1541 j++) {}
1542 flist->low = j;
1543 }
1544 prev_i = i;
1545 }
728d0922 1546 } else
6931c138 1547 prev_i = i;
3ec4dd97 1548 }
f5db0993 1549 flist->high = no_dups ? prev_i : flist->count - 1;
0199b05f
AT
1550
1551 if (strip_root) {
f5db0993
WD
1552 /* We need to strip off the leading slashes for relative
1553 * paths, but this must be done _after_ the sorting phase. */
1554 for (i = flist->low; i <= flist->high; i++) {
1555 struct file_struct *file = flist->files[i];
ebed4c3a 1556
f5db0993
WD
1557 if (!file->dirname)
1558 continue;
1559 if (*file->dirname == '/') {
1560 char *s = file->dirname + 1;
1561 while (*s == '/') s++;
1562 memmove(file->dirname, s, strlen(s) + 1);
0199b05f 1563 }
f5db0993
WD
1564
1565 if (!*file->dirname)
1566 file->dirname = NULL;
0199b05f
AT
1567 }
1568 }
cefed3e8 1569}
0199b05f 1570
32cbfe7b 1571static void output_flist(struct file_list *flist)
cefed3e8 1572{
f3c3ed44 1573 char uidbuf[16], gidbuf[16], depthbuf[16];
cefed3e8 1574 struct file_struct *file;
32cbfe7b 1575 const char *who = who_am_i();
cefed3e8 1576 int i;
0199b05f 1577
ebed4c3a 1578 for (i = 0; i < flist->count; i++) {
cefed3e8 1579 file = flist->files[i];
56194bcd 1580 if ((am_root || am_sender) && preserve_uid)
f05f993e
WD
1581 sprintf(uidbuf, " uid=%ld", (long)file->uid);
1582 else
1583 *uidbuf = '\0';
cefed3e8 1584 if (preserve_gid && file->gid != GID_NONE)
f05f993e
WD
1585 sprintf(gidbuf, " gid=%ld", (long)file->gid);
1586 else
1587 *gidbuf = '\0';
f3c3ed44
WD
1588 if (!am_sender)
1589 sprintf(depthbuf, "%d", file->dir.depth);
14698a3a 1590 rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
32cbfe7b 1591 who, i, am_sender ? NS(file->dir.root) : depthbuf,
71903f60 1592 file->dirname ? safe_fname(file->dirname) : "",
14698a3a
WD
1593 file->dirname ? "/" : "", NS(file->basename),
1594 S_ISDIR(file->mode) ? "/" : "", (int)file->mode,
f3c3ed44 1595 (double)file->length, uidbuf, gidbuf, file->flags);
0199b05f 1596 }
3ec4dd97
AT
1597}
1598
8824e2ce 1599enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
f5db0993 1600enum fnc_type { t_PATH, t_ITEM };
8018edd3 1601
2f3cad89 1602/* Compare the names of two file_struct entities, similar to how strcmp()
f5db0993
WD
1603 * would do if it were operating on the joined strings.
1604 *
1605 * Some differences beginning with protocol_version 29: (1) directory names
1606 * are compared with an assumed trailing slash so that they compare in a
1607 * way that would cause them to sort immediately prior to any content they
1608 * may have; (2) a directory of any name compares after a non-directory of
1609 * any name at the same depth; (3) a directory with name "." compares prior
1610 * to anything else. These changes mean that a directory and a non-dir
1611 * with the same name will not compare as equal (protocol_version >= 29).
1612 *
1613 * The dirname component can be an empty string, but the basename component
1614 * cannot (and never is in the current codebase). The basename component
1615 * may be NULL (for a removed item), in which case it is considered to be
1616 * after any existing item. */
8018edd3 1617int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
3ec4dd97 1618{
8018edd3
WD
1619 int dif;
1620 const uchar *c1, *c2;
1ef00d20 1621 enum fnc_state state1, state2;
f5db0993
WD
1622 enum fnc_type type1, type2;
1623 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
8018edd3
WD
1624
1625 if (!f1 || !f1->basename) {
1626 if (!f2 || !f2->basename)
1627 return 0;
1628 return -1;
1629 }
1630 if (!f2 || !f2->basename)
1631 return 1;
1632
14698a3a
WD
1633 c1 = (uchar*)f1->dirname;
1634 c2 = (uchar*)f2->dirname;
1635 if (c1 == c2)
1636 c1 = c2 = NULL;
1637 if (!c1) {
f5db0993 1638 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1639 c1 = (uchar*)f1->basename;
f5db0993
WD
1640 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1641 type1 = t_ITEM;
1642 state1 = s_TRAILING;
1643 c1 = (uchar*)"";
1644 } else
1645 state1 = s_BASE;
080ddf58 1646 } else if (!*c1) {
f5db0993 1647 type1 = t_path;
8824e2ce 1648 state1 = s_SLASH;
080ddf58 1649 c1 = (uchar*)"/";
f5db0993
WD
1650 } else {
1651 type1 = t_path;
8824e2ce 1652 state1 = s_DIR;
f5db0993 1653 }
14698a3a 1654 if (!c2) {
f5db0993 1655 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1656 c2 = (uchar*)f2->basename;
f5db0993
WD
1657 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1658 type2 = t_ITEM;
1659 state2 = s_TRAILING;
1660 c2 = (uchar*)"";
1661 } else
1662 state2 = s_BASE;
080ddf58 1663 } else if (!*c2) {
f5db0993 1664 type2 = t_path;
8824e2ce 1665 state2 = s_SLASH;
080ddf58 1666 c2 = (uchar*)"/";
f5db0993
WD
1667 } else {
1668 type2 = t_path;
8824e2ce 1669 state2 = s_DIR;
f5db0993
WD
1670 }
1671
1672 if (type1 != type2)
1673 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1674
1675 while (1) {
f5db0993 1676 if ((dif = (int)*c1++ - (int)*c2++) != 0)
8018edd3 1677 break;
f5db0993 1678 if (!*c1) {
8018edd3 1679 switch (state1) {
8824e2ce
WD
1680 case s_DIR:
1681 state1 = s_SLASH;
e90b8ace 1682 c1 = (uchar*)"/";
8018edd3 1683 break;
8824e2ce 1684 case s_SLASH:
f5db0993 1685 type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
e90b8ace 1686 c1 = (uchar*)f1->basename;
cbb5fa4f
WD
1687 if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
1688 type1 = t_ITEM;
1689 state1 = s_TRAILING;
1690 c1 = (uchar*)"";
1691 } else
1692 state1 = s_BASE;
8018edd3 1693 break;
8824e2ce
WD
1694 case s_BASE:
1695 state1 = s_TRAILING;
f5db0993 1696 if (type1 == t_PATH) {
2f3cad89 1697 c1 = (uchar*)"/";
f5db0993
WD
1698 break;
1699 }
1700 /* FALL THROUGH */
8824e2ce 1701 case s_TRAILING:
f5db0993 1702 type1 = t_ITEM;
8018edd3
WD
1703 break;
1704 }
f5db0993
WD
1705 if (*c2 && type1 != type2)
1706 return type1 == t_PATH ? 1 : -1;
8018edd3 1707 }
f5db0993 1708 if (!*c2) {
8018edd3 1709 switch (state2) {
8824e2ce
WD
1710 case s_DIR:
1711 state2 = s_SLASH;
e90b8ace 1712 c2 = (uchar*)"/";
8018edd3 1713 break;
8824e2ce 1714 case s_SLASH:
f5db0993 1715 type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
e90b8ace 1716 c2 = (uchar*)f2->basename;
cbb5fa4f
WD
1717 if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
1718 type2 = t_ITEM;
1719 state2 = s_TRAILING;
1720 c2 = (uchar*)"";
1721 } else
1722 state2 = s_BASE;
8018edd3 1723 break;
8824e2ce 1724 case s_BASE:
8824e2ce 1725 state2 = s_TRAILING;
f5db0993 1726 if (type2 == t_PATH) {
2f3cad89 1727 c2 = (uchar*)"/";
f5db0993
WD
1728 break;
1729 }
1730 /* FALL THROUGH */
8824e2ce 1731 case s_TRAILING:
f5db0993
WD
1732 if (!*c1)
1733 return 0;
1734 type2 = t_ITEM;
8018edd3
WD
1735 break;
1736 }
f5db0993
WD
1737 if (type1 != type2)
1738 return type1 == t_PATH ? 1 : -1;
8018edd3
WD
1739 }
1740 }
1741
1742 return dif;
1743}
1744
8018edd3 1745/* Return a copy of the full filename of a flist entry, using the indicated
882e6893
WD
1746 * buffer. No size-checking is done because we checked the size when creating
1747 * the file_struct entry.
8018edd3 1748 */
882e6893 1749char *f_name_to(struct file_struct *f, char *fbuf)
8018edd3 1750{
ebed4c3a
MP
1751 if (!f || !f->basename)
1752 return NULL;
3ec4dd97 1753
3ec4dd97 1754 if (f->dirname) {
882e6893
WD
1755 int len = strlen(f->dirname);
1756 memcpy(fbuf, f->dirname, len);
1757 fbuf[len] = '/';
1758 strcpy(fbuf + len + 1, f->basename);
8018edd3 1759 } else
882e6893 1760 strcpy(fbuf, f->basename);
b7736c79 1761 return fbuf;
8018edd3 1762}
e03dfae5 1763
45478cc7 1764/* Like f_name_to(), but we rotate through 5 static buffers of our own. */
8018edd3
WD
1765char *f_name(struct file_struct *f)
1766{
1767 static char names[5][MAXPATHLEN];
1768 static unsigned int n;
1769
1770 n = (n + 1) % (sizeof names / sizeof names[0]);
1771
882e6893 1772 return f_name_to(f, names[n]);
c627d613 1773}
649f8742 1774
32cbfe7b
WD
1775/* Do a non-recursive scan of the named directory, possibly ignoring all
1776 * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
1777 * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
1778 * buffer (the functions we call will append names onto the end, but the old
1779 * dir value will be restored on exit). */
1780struct file_list *get_dirlist(char *dirname, int dlen,
1781 int ignore_filter_rules)
37802f40
WD
1782{
1783 struct file_list *dirlist;
1784 char dirbuf[MAXPATHLEN];
37802f40
WD
1785 int save_recurse = recurse;
1786
32cbfe7b
WD
1787 if (dlen < 0) {
1788 dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
1789 if (dlen >= MAXPATHLEN)
1790 return NULL;
1791 dirname = dirbuf;
1792 }
37802f40
WD
1793
1794 dirlist = flist_new(WITHOUT_HLINK, "get_dirlist");
32cbfe7b 1795
37802f40 1796 recurse = 0;
32cbfe7b 1797 send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen);
37802f40 1798 recurse = save_recurse;
53135fe8
WD
1799 if (do_progress)
1800 flist_count_offset += dirlist->count;
37802f40 1801
564ef546
WD
1802 clean_flist(dirlist, 0, 0);
1803
45478cc7 1804 if (verbose > 3)
32cbfe7b 1805 output_flist(dirlist);
45478cc7 1806
32cbfe7b 1807 return dirlist;
45478cc7 1808}