Commit | Line | Data |
---|---|---|
dbda5fbf | 1 | /* |
0f78b815 WD |
2 | * Generate and receive file lists. |
3 | * | |
4 | * Copyright (C) 1996 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
6 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> | |
ba2133d6 | 7 | * Copyright (C) 2002-2007 Wayne Davison |
0f78b815 WD |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
ba2133d6 WD |
10 | * it under the terms of the GNU General Public License version 2 as |
11 | * published by the Free Software Foundation. | |
736a6a29 | 12 | * |
0f78b815 WD |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
172875cf | 17 | * |
e7c67065 WD |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 21 | */ |
c627d613 AT |
22 | |
23 | #include "rsync.h" | |
a3e18c76 | 24 | #include "rounding.h" |
6d4e718f | 25 | #include "io.h" |
c627d613 AT |
26 | |
27 | extern int verbose; | |
34e18ecd | 28 | extern int list_only; |
f05f993e | 29 | extern int am_root; |
c627d613 | 30 | extern int am_server; |
a8726d2a | 31 | extern int am_daemon; |
56194bcd | 32 | extern int am_sender; |
3ea6e0e7 | 33 | extern int inc_recurse; |
8715db2c | 34 | extern int do_progress; |
c627d613 | 35 | extern int always_checksum; |
983b1ed3 WD |
36 | extern int module_id; |
37 | extern int ignore_errors; | |
4836c3ee | 38 | extern int numeric_ids; |
a06d19e3 | 39 | extern int recurse; |
7e037c42 | 40 | extern int xfer_dirs; |
24d0fcde | 41 | extern int filesfrom_fd; |
c627d613 | 42 | extern int one_file_system; |
88c2190a | 43 | extern int copy_dirlinks; |
314f4591 | 44 | extern int keep_dirlinks; |
1c3344a1 | 45 | extern int preserve_acls; |
c627d613 | 46 | extern int preserve_links; |
dc5ddbcc | 47 | extern int preserve_hard_links; |
c627d613 | 48 | extern int preserve_devices; |
b5c6a6ae | 49 | extern int preserve_specials; |
c627d613 AT |
50 | extern int preserve_uid; |
51 | extern int preserve_gid; | |
6574b4f7 | 52 | extern int relative_paths; |
24d0fcde | 53 | extern int implied_dirs; |
99a957d3 | 54 | extern int file_extra_cnt; |
53039410 WD |
55 | extern int ignore_perishable; |
56 | extern int non_perishable_cnt; | |
85aecef6 | 57 | extern int prune_empty_dirs; |
82306bf6 | 58 | extern int copy_links; |
b5313607 | 59 | extern int copy_unsafe_links; |
d04e9c51 | 60 | extern int protocol_version; |
cb13abfe | 61 | extern int sanitize_paths; |
d64e6f42 | 62 | extern struct stats stats; |
c627d613 | 63 | |
e1f40891 WD |
64 | extern char curr_dir[MAXPATHLEN]; |
65 | ||
2b7e0f33 WD |
66 | extern struct chmod_mode_struct *chmod_modes; |
67 | ||
7842418b WD |
68 | extern struct filter_list_struct filter_list; |
69 | extern struct filter_list_struct server_filter_list; | |
c627d613 | 70 | |
06c28400 | 71 | int io_error; |
7752df41 | 72 | int checksum_len; |
535737bf | 73 | dev_t filesystem_dev; /* used to implement -x */ |
82ad07c4 | 74 | |
c7e6f84f WD |
75 | struct file_list *cur_flist, *first_flist, *dir_flist; |
76 | int send_dir_ndx = -1, send_dir_depth = 0; | |
77 | int flist_cnt = 0; /* how many (non-tmp) file list objects exist */ | |
78 | int file_total = 0; /* total of all active items over all file-lists */ | |
79 | int flist_eof = 0; /* all the file-lists are now known */ | |
80 | ||
82ad07c4 WD |
81 | /* The tmp_* vars are used as a cache area by make_file() to store data |
82 | * that the sender doesn't need to remember in its file list. The data | |
83 | * will survive just long enough to be used by send_file_entry(). */ | |
84 | static dev_t tmp_rdev; | |
9d155ecd | 85 | #ifdef SUPPORT_HARD_LINKS |
9d737ecb | 86 | static int64 tmp_dev, tmp_ino; |
9d155ecd | 87 | #endif |
a0456b9c | 88 | static char tmp_sum[MAX_DIGEST_LEN]; |
06c28400 | 89 | |
a0456b9c | 90 | static char empty_sum[MAX_DIGEST_LEN]; |
c7565dad | 91 | static int flist_count_offset; /* for --delete --progress */ |
3d382777 | 92 | |
827c37f6 | 93 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups); |
32cbfe7b | 94 | static void output_flist(struct file_list *flist); |
0199b05f | 95 | |
61dec11a WD |
96 | void init_flist(void) |
97 | { | |
96293cf9 WD |
98 | if (verbose > 4) { |
99 | rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n", | |
6aef83c9 | 100 | (int)FILE_STRUCT_LEN, (int)EXTRA_LEN); |
96293cf9 | 101 | } |
a0456b9c WD |
102 | checksum_len = protocol_version < 21 ? 2 |
103 | : protocol_version < 30 ? MD4_DIGEST_LEN | |
104 | : MD5_DIGEST_LEN; | |
61dec11a WD |
105 | } |
106 | ||
1bbd10fe | 107 | static int show_filelist_p(void) |
58225000 | 108 | { |
3ea6e0e7 | 109 | return verbose && xfer_dirs && !am_server && !inc_recurse; |
1bbd10fe | 110 | } |
ebed4c3a | 111 | |
1bbd10fe DD |
112 | static void start_filelist_progress(char *kind) |
113 | { | |
ea124cb3 | 114 | rprintf(FCLIENT, "%s ... ", kind); |
e5ce3bcf | 115 | if (verbose > 1 || do_progress) |
ea124cb3 | 116 | rprintf(FCLIENT, "\n"); |
1bbd10fe | 117 | rflush(FINFO); |
58225000 MP |
118 | } |
119 | ||
53135fe8 | 120 | static void emit_filelist_progress(int count) |
db719fb0 | 121 | { |
ea124cb3 | 122 | rprintf(FCLIENT, " %d files...\r", count); |
db719fb0 MP |
123 | } |
124 | ||
53135fe8 | 125 | static void maybe_emit_filelist_progress(int count) |
58225000 | 126 | { |
53135fe8 WD |
127 | if (do_progress && show_filelist_p() && (count % 100) == 0) |
128 | emit_filelist_progress(count); | |
58225000 MP |
129 | } |
130 | ||
1bbd10fe | 131 | static void finish_filelist_progress(const struct file_list *flist) |
58225000 | 132 | { |
1bbd10fe DD |
133 | if (do_progress) { |
134 | /* This overwrites the progress line */ | |
c7b562be MP |
135 | rprintf(FINFO, "%d file%sto consider\n", |
136 | flist->count, flist->count == 1 ? " " : "s "); | |
b7736c79 | 137 | } else |
1bbd10fe | 138 | rprintf(FINFO, "done\n"); |
58225000 MP |
139 | } |
140 | ||
86943126 MP |
141 | void show_flist_stats(void) |
142 | { | |
143 | /* Nothing yet */ | |
144 | } | |
145 | ||
f7632fc6 AT |
146 | static void list_file_entry(struct file_struct *f) |
147 | { | |
78d146e8 | 148 | char permbuf[PERMSTRING_SIZE]; |
96293cf9 | 149 | double len; |
f7632fc6 | 150 | |
96293cf9 | 151 | if (!F_IS_ACTIVE(f)) { |
7212be92 DD |
152 | /* this can happen if duplicate names were removed */ |
153 | return; | |
e5ce3bcf | 154 | } |
7212be92 | 155 | |
78d146e8 | 156 | permstring(permbuf, f->mode); |
96293cf9 | 157 | len = F_LENGTH(f); |
740819ef | 158 | |
1c3344a1 WD |
159 | /* TODO: indicate '+' if the entry has an ACL. */ |
160 | ||
4f5b0756 | 161 | #ifdef SUPPORT_LINKS |
f7632fc6 | 162 | if (preserve_links && S_ISLNK(f->mode)) { |
ebed4c3a | 163 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
96293cf9 | 164 | permbuf, len, timestring(f->modtime), |
82ad07c4 | 165 | f_name(f, NULL), F_SYMLINK(f)); |
0d162bd1 WD |
166 | } else |
167 | #endif | |
e5ce3bcf | 168 | { |
ebed4c3a | 169 | rprintf(FINFO, "%s %11.0f %s %s\n", |
96293cf9 | 170 | permbuf, len, timestring(f->modtime), |
5e4ff5f9 | 171 | f_name(f, NULL)); |
e5ce3bcf | 172 | } |
f7632fc6 AT |
173 | } |
174 | ||
cad8f6f9 WD |
175 | /* Stat either a symlink or its referent, depending on the settings of |
176 | * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success. | |
4e5db0ad | 177 | * |
cad8f6f9 WD |
178 | * If path is the name of a symlink, then the linkbuf buffer (which must hold |
179 | * MAXPATHLEN chars) will be set to the symlink's target string. | |
4e5db0ad | 180 | * |
cad8f6f9 WD |
181 | * The stat structure pointed to by stp will contain information about the |
182 | * link or the referent as appropriate, if they exist. */ | |
183 | static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf) | |
b5313607 | 184 | { |
4f5b0756 | 185 | #ifdef SUPPORT_LINKS |
cad8f6f9 | 186 | if (link_stat(path, stp, copy_dirlinks) < 0) |
b5313607 | 187 | return -1; |
cad8f6f9 WD |
188 | if (S_ISLNK(stp->st_mode)) { |
189 | int llen = readlink(path, linkbuf, MAXPATHLEN - 1); | |
190 | if (llen < 0) | |
b5313607 | 191 | return -1; |
cad8f6f9 | 192 | linkbuf[llen] = '\0'; |
fc638474 DD |
193 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
194 | if (verbose > 1) { | |
dbda5fbf | 195 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
0ee6ca98 | 196 | path, linkbuf); |
fc638474 | 197 | } |
1a7f3d99 | 198 | return do_stat(path, stp); |
b5313607 DD |
199 | } |
200 | } | |
201 | return 0; | |
202 | #else | |
cad8f6f9 | 203 | return do_stat(path, stp); |
b5313607 DD |
204 | #endif |
205 | } | |
206 | ||
cad8f6f9 | 207 | int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks) |
82306bf6 | 208 | { |
4f5b0756 | 209 | #ifdef SUPPORT_LINKS |
b7736c79 | 210 | if (copy_links) |
1a7f3d99 | 211 | return do_stat(path, stp); |
cad8f6f9 | 212 | if (do_lstat(path, stp) < 0) |
314f4591 | 213 | return -1; |
cad8f6f9 | 214 | if (follow_dirlinks && S_ISLNK(stp->st_mode)) { |
314f4591 | 215 | STRUCT_STAT st; |
1a7f3d99 | 216 | if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode)) |
cad8f6f9 | 217 | *stp = st; |
314f4591 WD |
218 | } |
219 | return 0; | |
82306bf6 | 220 | #else |
cad8f6f9 | 221 | return do_stat(path, stp); |
82306bf6 AT |
222 | #endif |
223 | } | |
224 | ||
7842418b | 225 | /* This function is used to check if a file should be included/excluded |
429f9828 | 226 | * from the list of files based on its name and type etc. The value of |
7842418b WD |
227 | * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */ |
228 | static int is_excluded(char *fname, int is_dir, int filter_level) | |
c627d613 | 229 | { |
7d687932 | 230 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
7842418b | 231 | if (filter_level == NO_FILTERS) |
429f9828 WD |
232 | return 0; |
233 | #endif | |
6931c138 | 234 | if (fname) { |
429f9828 | 235 | /* never exclude '.', even if somebody does --exclude '*' */ |
6931c138 WD |
236 | if (fname[0] == '.' && !fname[1]) |
237 | return 0; | |
238 | /* Handle the -R version of the '.' dir. */ | |
239 | if (fname[0] == '/') { | |
240 | int len = strlen(fname); | |
241 | if (fname[len-1] == '.' && fname[len-2] == '/') | |
242 | return 0; | |
243 | } | |
76e26e10 | 244 | } |
7842418b WD |
245 | if (server_filter_list.head |
246 | && check_filter(&server_filter_list, fname, is_dir) < 0) | |
429f9828 | 247 | return 1; |
7842418b | 248 | if (filter_level != ALL_FILTERS) |
429f9828 | 249 | return 0; |
7842418b WD |
250 | if (filter_list.head |
251 | && check_filter(&filter_list, fname, is_dir) < 0) | |
76e26e10 | 252 | return 1; |
76e26e10 | 253 | return 0; |
c627d613 AT |
254 | } |
255 | ||
b280a1f4 AT |
256 | static int to_wire_mode(mode_t mode) |
257 | { | |
4f5b0756 | 258 | #ifdef SUPPORT_LINKS |
e4fdf1de WD |
259 | #if _S_IFLNK != 0120000 |
260 | if (S_ISLNK(mode)) | |
b280a1f4 | 261 | return (mode & ~(_S_IFMT)) | 0120000; |
0d162bd1 | 262 | #endif |
e4fdf1de WD |
263 | #endif |
264 | return mode; | |
b280a1f4 AT |
265 | } |
266 | ||
267 | static mode_t from_wire_mode(int mode) | |
268 | { | |
e4fdf1de WD |
269 | #if _S_IFLNK != 0120000 |
270 | if ((mode & (_S_IFMT)) == 0120000) | |
efe3037c | 271 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
e4fdf1de WD |
272 | #endif |
273 | return mode; | |
b280a1f4 AT |
274 | } |
275 | ||
c7e6f84f WD |
276 | static void send_directory(int f, struct file_list *flist, int ndx, |
277 | char *fbuf, int len, int flags); | |
c627d613 | 278 | |
c7e6f84f | 279 | static const char *flist_dir, *orig_dir; |
882e6893 | 280 | static int flist_dir_len; |
c627d613 | 281 | |
3ec4dd97 | 282 | |
d9d6bc52 MP |
283 | /** |
284 | * Make sure @p flist is big enough to hold at least @p flist->count | |
285 | * entries. | |
286 | **/ | |
a85906c7 | 287 | void flist_expand(struct file_list *flist) |
d9d6bc52 | 288 | { |
0501f363 | 289 | struct file_struct **new_ptr; |
2e7d1994 | 290 | |
a85906c7 S |
291 | if (flist->count < flist->malloced) |
292 | return; | |
dbda5fbf | 293 | |
a85906c7 S |
294 | if (flist->malloced < FLIST_START) |
295 | flist->malloced = FLIST_START; | |
296 | else if (flist->malloced >= FLIST_LINEAR) | |
297 | flist->malloced += FLIST_LINEAR; | |
298 | else | |
299 | flist->malloced *= 2; | |
300 | ||
301 | /* | |
302 | * In case count jumped or we are starting the list | |
303 | * with a known size just set it. | |
304 | */ | |
305 | if (flist->malloced < flist->count) | |
306 | flist->malloced = flist->count; | |
307 | ||
0501f363 WD |
308 | new_ptr = realloc_array(flist->files, struct file_struct *, |
309 | flist->malloced); | |
2e7d1994 | 310 | |
8c483820 | 311 | if (verbose >= 2 && flist->malloced != FLIST_START) { |
ea124cb3 | 312 | rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n", |
a85906c7 | 313 | who_am_i(), |
a4a7e64c | 314 | (double)sizeof flist->files[0] * flist->malloced, |
a85906c7 | 315 | (new_ptr == flist->files) ? " not" : ""); |
d9d6bc52 | 316 | } |
a85906c7 | 317 | |
0501f363 | 318 | flist->files = new_ptr; |
a85906c7 S |
319 | |
320 | if (!flist->files) | |
321 | out_of_memory("flist_expand"); | |
d9d6bc52 MP |
322 | } |
323 | ||
c7e6f84f WD |
324 | int push_flist_dir(const char *dir, int len) |
325 | { | |
326 | if (dir == flist_dir) | |
327 | return 1; | |
328 | ||
329 | if (!orig_dir) | |
330 | orig_dir = strdup(curr_dir); | |
331 | ||
332 | if (flist_dir && !pop_dir(orig_dir)) { | |
333 | rsyserr(FERROR, errno, "pop_dir %s failed", | |
334 | full_fname(orig_dir)); | |
335 | exit_cleanup(RERR_FILESELECT); | |
336 | } | |
337 | ||
338 | if (dir && !push_dir(dir, 0)) { | |
339 | io_error |= IOERR_GENERAL; | |
340 | rsyserr(FERROR, errno, "push_dir %s failed", | |
341 | full_fname(dir)); | |
342 | return 0; | |
343 | } | |
344 | ||
345 | flist_dir = dir; | |
346 | flist_dir_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0; | |
347 | ||
348 | return 1; | |
349 | } | |
350 | ||
42c6b139 | 351 | static void send_file_entry(int f, struct file_struct *file, int ndx) |
c627d613 | 352 | { |
5911fee5 WD |
353 | static time_t modtime; |
354 | static mode_t mode; | |
1490812a | 355 | static int64 dev; |
4124540d | 356 | static dev_t rdev; |
9c5e91f8 | 357 | static uint32 rdev_major; |
5911fee5 WD |
358 | static uid_t uid; |
359 | static gid_t gid; | |
c7e6f84f | 360 | static char *user_name, *group_name; |
5911fee5 | 361 | static char lastname[MAXPATHLEN]; |
f376e674 | 362 | char fname[MAXPATHLEN]; |
8cae71eb | 363 | int first_hlink_ndx = -1; |
ebed4c3a | 364 | int l1, l2; |
99a957d3 | 365 | int flags; |
72914a60 | 366 | |
5e4ff5f9 | 367 | f_name(file, fname); |
72914a60 | 368 | |
82ad07c4 | 369 | flags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */ |
72914a60 | 370 | |
30f337c9 | 371 | if (file->mode == mode) |
d01d15e0 | 372 | flags |= XMIT_SAME_MODE; |
1ef00d20 | 373 | else |
30f337c9 | 374 | mode = file->mode; |
b5c6a6ae WD |
375 | if ((preserve_devices && IS_DEVICE(mode)) |
376 | || (preserve_specials && IS_SPECIAL(mode))) { | |
75bc8600 | 377 | if (protocol_version < 28) { |
82ad07c4 | 378 | if (tmp_rdev == rdev) |
b5c6a6ae WD |
379 | flags |= XMIT_SAME_RDEV_pre28; |
380 | else | |
82ad07c4 | 381 | rdev = tmp_rdev; |
b5c6a6ae | 382 | } else { |
82ad07c4 | 383 | rdev = tmp_rdev; |
9c5e91f8 WD |
384 | if ((uint32)major(rdev) == rdev_major) |
385 | flags |= XMIT_SAME_RDEV_MAJOR; | |
84a3efa0 | 386 | else |
9c5e91f8 | 387 | rdev_major = major(rdev); |
18438f0b WD |
388 | if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu) |
389 | flags |= XMIT_RDEV_MINOR_8_pre30; | |
75bc8600 | 390 | } |
b5c6a6ae | 391 | } else if (protocol_version < 28) |
b4a09b72 | 392 | rdev = MAKEDEV(0, 0); |
82ad07c4 | 393 | if (preserve_uid) { |
c7e6f84f | 394 | if (F_UID(file) == uid && *lastname) |
82ad07c4 | 395 | flags |= XMIT_SAME_UID; |
c7e6f84f | 396 | else { |
82ad07c4 | 397 | uid = F_UID(file); |
c7e6f84f WD |
398 | if (preserve_uid && !numeric_ids) { |
399 | user_name = add_uid(uid); | |
3ea6e0e7 | 400 | if (inc_recurse && user_name) |
c7e6f84f WD |
401 | flags |= XMIT_USER_NAME_FOLLOWS; |
402 | } | |
403 | } | |
82ad07c4 WD |
404 | } |
405 | if (preserve_gid) { | |
c7e6f84f | 406 | if (F_GID(file) == gid && *lastname) |
82ad07c4 | 407 | flags |= XMIT_SAME_GID; |
c7e6f84f | 408 | else { |
82ad07c4 | 409 | gid = F_GID(file); |
c7e6f84f WD |
410 | if (preserve_gid && !numeric_ids) { |
411 | group_name = add_gid(gid); | |
3ea6e0e7 | 412 | if (inc_recurse && group_name) |
c7e6f84f WD |
413 | flags |= XMIT_GROUP_NAME_FOLLOWS; |
414 | } | |
415 | } | |
82ad07c4 | 416 | } |
30f337c9 | 417 | if (file->modtime == modtime) |
d01d15e0 | 418 | flags |= XMIT_SAME_TIME; |
1ef00d20 | 419 | else |
30f337c9 | 420 | modtime = file->modtime; |
a289addd | 421 | |
4f5b0756 | 422 | #ifdef SUPPORT_HARD_LINKS |
9d737ecb | 423 | if (tmp_dev != 0) { |
8cae71eb | 424 | if (protocol_version >= 30) { |
9d737ecb | 425 | struct idev_node *np = idev_node(tmp_dev, tmp_ino); |
424d3691 | 426 | first_hlink_ndx = (int32)(long)np->data - 1; |
8cae71eb | 427 | if (first_hlink_ndx < 0) { |
424d3691 | 428 | np->data = (void*)(long)(ndx + 1); |
8cae71eb WD |
429 | flags |= XMIT_HLINK_FIRST; |
430 | } | |
431 | flags |= XMIT_HLINKED; | |
432 | } else { | |
9d737ecb | 433 | if (tmp_dev == dev) { |
8cae71eb WD |
434 | if (protocol_version >= 28) |
435 | flags |= XMIT_SAME_DEV_pre30; | |
436 | } else | |
9d737ecb | 437 | dev = tmp_dev; |
8cae71eb WD |
438 | flags |= XMIT_HLINKED; |
439 | } | |
c4b4df4f | 440 | } |
0d162bd1 | 441 | #endif |
ebed4c3a MP |
442 | |
443 | for (l1 = 0; | |
3e491682 S |
444 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); |
445 | l1++) {} | |
eddd5d12 | 446 | l2 = strlen(fname+l1); |
72914a60 | 447 | |
ebed4c3a | 448 | if (l1 > 0) |
d01d15e0 | 449 | flags |= XMIT_SAME_NAME; |
ebed4c3a | 450 | if (l2 > 255) |
d01d15e0 | 451 | flags |= XMIT_LONG_NAME; |
72914a60 | 452 | |
1aa4caf3 WD |
453 | /* We must make sure we don't send a zero flag byte or the |
454 | * other end will terminate the flist transfer. Note that | |
ee3751c8 | 455 | * the use of XMIT_TOP_DIR on a non-dir has no meaning, so |
1aa4caf3 | 456 | * it's harmless way to add a bit to the first flag byte. */ |
75bc8600 | 457 | if (protocol_version >= 28) { |
1aa4caf3 | 458 | if (!flags && !S_ISDIR(mode)) |
ee3751c8 | 459 | flags |= XMIT_TOP_DIR; |
1aa4caf3 | 460 | if ((flags & 0xFF00) || !flags) { |
d01d15e0 | 461 | flags |= XMIT_EXTENDED_FLAGS; |
d521e1c2 | 462 | write_shortint(f, flags); |
75bc8600 WD |
463 | } else |
464 | write_byte(f, flags); | |
465 | } else { | |
0a982011 | 466 | if (!(flags & 0xFF)) |
75c51953 | 467 | flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR; |
75bc8600 WD |
468 | write_byte(f, flags); |
469 | } | |
d01d15e0 | 470 | if (flags & XMIT_SAME_NAME) |
ebed4c3a | 471 | write_byte(f, l1); |
d01d15e0 | 472 | if (flags & XMIT_LONG_NAME) |
1c3344a1 | 473 | write_abbrevint30(f, l2); |
72914a60 | 474 | else |
ebed4c3a MP |
475 | write_byte(f, l2); |
476 | write_buf(f, fname + l1, l2); | |
72914a60 | 477 | |
8cae71eb | 478 | if (first_hlink_ndx >= 0) { |
1c3344a1 | 479 | write_abbrevint30(f, first_hlink_ndx); |
8cae71eb WD |
480 | goto the_end; |
481 | } | |
482 | ||
96293cf9 | 483 | write_longint(f, F_LENGTH(file)); |
d01d15e0 | 484 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 485 | write_int(f, modtime); |
d01d15e0 | 486 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 487 | write_int(f, to_wire_mode(mode)); |
d01d15e0 | 488 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
18438f0b WD |
489 | if (protocol_version < 30) |
490 | write_int(f, uid); | |
491 | else { | |
492 | write_abbrevint(f, uid); | |
493 | if (flags & XMIT_USER_NAME_FOLLOWS) { | |
494 | int len = strlen(user_name); | |
495 | write_byte(f, len); | |
496 | write_buf(f, user_name, len); | |
497 | } | |
c7e6f84f | 498 | } |
72914a60 | 499 | } |
d01d15e0 | 500 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { |
18438f0b WD |
501 | if (protocol_version < 30) |
502 | write_int(f, gid); | |
503 | else { | |
504 | write_abbrevint(f, gid); | |
505 | if (flags & XMIT_GROUP_NAME_FOLLOWS) { | |
506 | int len = strlen(group_name); | |
507 | write_byte(f, len); | |
508 | write_buf(f, group_name, len); | |
509 | } | |
c7e6f84f | 510 | } |
72914a60 | 511 | } |
b5c6a6ae WD |
512 | if ((preserve_devices && IS_DEVICE(mode)) |
513 | || (preserve_specials && IS_SPECIAL(mode))) { | |
9c5e91f8 WD |
514 | if (protocol_version < 28) { |
515 | if (!(flags & XMIT_SAME_RDEV_pre28)) | |
516 | write_int(f, (int)rdev); | |
517 | } else { | |
518 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) | |
18438f0b WD |
519 | write_abbrevint30(f, major(rdev)); |
520 | if (protocol_version >= 30) | |
521 | write_abbrevint(f, minor(rdev)); | |
522 | else if (flags & XMIT_RDEV_MINOR_8_pre30) | |
9c5e91f8 WD |
523 | write_byte(f, minor(rdev)); |
524 | else | |
525 | write_int(f, minor(rdev)); | |
526 | } | |
75bc8600 | 527 | } |
c627d613 | 528 | |
4f5b0756 | 529 | #ifdef SUPPORT_LINKS |
30f337c9 | 530 | if (preserve_links && S_ISLNK(mode)) { |
82ad07c4 WD |
531 | const char *sl = F_SYMLINK(file); |
532 | int len = strlen(sl); | |
1c3344a1 | 533 | write_abbrevint30(f, len); |
82ad07c4 | 534 | write_buf(f, sl, len); |
72914a60 | 535 | } |
c627d613 AT |
536 | #endif |
537 | ||
4f5b0756 | 538 | #ifdef SUPPORT_HARD_LINKS |
9d737ecb | 539 | if (tmp_dev != 0 && protocol_version < 30) { |
4124540d | 540 | if (protocol_version < 26) { |
736a6a29 | 541 | /* 32-bit dev_t and ino_t */ |
26404276 | 542 | write_int(f, (int32)dev); |
9d737ecb | 543 | write_int(f, (int32)tmp_ino); |
736a6a29 MP |
544 | } else { |
545 | /* 64-bit dev_t and ino_t */ | |
8cae71eb | 546 | if (!(flags & XMIT_SAME_DEV_pre30)) |
4124540d | 547 | write_longint(f, dev); |
9d737ecb | 548 | write_longint(f, tmp_ino); |
736a6a29 | 549 | } |
72914a60 | 550 | } |
dc5ddbcc AT |
551 | #endif |
552 | ||
9a4a237e | 553 | if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { |
4a19c3b2 | 554 | const char *sum; |
728d0922 | 555 | if (S_ISREG(mode)) |
82ad07c4 | 556 | sum = tmp_sum; |
9a4a237e | 557 | else { |
728d0922 WD |
558 | /* Prior to 28, we sent a useless set of nulls. */ |
559 | sum = empty_sum; | |
728d0922 | 560 | } |
7752df41 | 561 | write_buf(f, sum, checksum_len); |
ebed4c3a | 562 | } |
182dca5c | 563 | |
8cae71eb | 564 | the_end: |
ebed4c3a | 565 | strlcpy(lastname, fname, MAXPATHLEN); |
ca947dea WD |
566 | |
567 | if (S_ISREG(mode) || S_ISLNK(mode)) | |
568 | stats.total_size += F_LENGTH(file); | |
182dca5c AT |
569 | } |
570 | ||
82ad07c4 | 571 | static struct file_struct *recv_file_entry(struct file_list *flist, |
99a957d3 | 572 | int flags, int f) |
182dca5c | 573 | { |
5911fee5 WD |
574 | static time_t modtime; |
575 | static mode_t mode; | |
1490812a | 576 | static int64 dev; |
4124540d | 577 | static dev_t rdev; |
9c5e91f8 | 578 | static uint32 rdev_major; |
5911fee5 WD |
579 | static uid_t uid; |
580 | static gid_t gid; | |
a289addd | 581 | static char lastname[MAXPATHLEN], *lastdir; |
33ffd7c3 | 582 | static int lastdir_depth, lastdir_len = -1; |
42f23f47 | 583 | static unsigned int del_hier_name_len = 0; |
649f8742 | 584 | static int in_del_hier = 0; |
72914a60 | 585 | char thisname[MAXPATHLEN]; |
ebed4c3a | 586 | unsigned int l1 = 0, l2 = 0; |
c7e6f84f | 587 | int alloc_len, basename_len, linkname_len; |
99a957d3 | 588 | int extra_len = file_extra_cnt * EXTRA_LEN; |
8cae71eb | 589 | int first_hlink_ndx = -1; |
a289addd | 590 | OFF_T file_length; |
c7e6f84f WD |
591 | const char *basename; |
592 | char *bp; | |
72914a60 AT |
593 | struct file_struct *file; |
594 | ||
d01d15e0 | 595 | if (flags & XMIT_SAME_NAME) |
72914a60 | 596 | l1 = read_byte(f); |
ebed4c3a | 597 | |
d01d15e0 | 598 | if (flags & XMIT_LONG_NAME) |
1c3344a1 | 599 | l2 = read_abbrevint30(f); |
72914a60 AT |
600 | else |
601 | l2 = read_byte(f); | |
602 | ||
ebed4c3a MP |
603 | if (l2 >= MAXPATHLEN - l1) { |
604 | rprintf(FERROR, | |
82ad07c4 WD |
605 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s [%s]\n", |
606 | flags, l1, l2, lastname, who_am_i()); | |
607 | overflow_exit("recv_file_entry"); | |
d0fd26aa | 608 | } |
72914a60 | 609 | |
ebed4c3a MP |
610 | strlcpy(thisname, lastname, l1 + 1); |
611 | read_sbuf(f, &thisname[l1], l2); | |
612 | thisname[l1 + l2] = 0; | |
72914a60 | 613 | |
ebed4c3a | 614 | strlcpy(lastname, thisname, MAXPATHLEN); |
72914a60 | 615 | |
58b1999e | 616 | clean_fname(thisname, 0); |
72914a60 | 617 | |
0d162bd1 | 618 | if (sanitize_paths) |
a2248aea | 619 | sanitize_path(thisname, thisname, "", 0, NULL); |
cb13abfe | 620 | |
a289addd | 621 | if ((basename = strrchr(thisname, '/')) != NULL) { |
c7e6f84f WD |
622 | int len = basename++ - thisname; |
623 | if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) { | |
624 | lastdir = new_array(char, len + 1); | |
625 | memcpy(lastdir, thisname, len); | |
626 | lastdir[len] = '\0'; | |
627 | lastdir_len = len; | |
628 | lastdir_depth = count_dir_elements(lastdir); | |
629 | } | |
630 | } else | |
a289addd | 631 | basename = thisname; |
a289addd | 632 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
72914a60 | 633 | |
8cae71eb WD |
634 | #ifdef SUPPORT_HARD_LINKS |
635 | if (protocol_version >= 30 | |
636 | && BITS_SETnUNSET(flags, XMIT_HLINKED, XMIT_HLINK_FIRST)) { | |
637 | struct file_struct *first; | |
1c3344a1 | 638 | first_hlink_ndx = read_abbrevint30(f); |
8cae71eb WD |
639 | if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) { |
640 | rprintf(FERROR, | |
641 | "hard-link reference out of range: %d (%d)\n", | |
642 | first_hlink_ndx, flist->count); | |
643 | exit_cleanup(RERR_PROTOCOL); | |
644 | } | |
645 | first = flist->files[first_hlink_ndx]; | |
646 | file_length = F_LENGTH(first); | |
647 | modtime = first->modtime; | |
648 | mode = first->mode; | |
649 | if (preserve_uid) | |
650 | uid = F_UID(first); | |
651 | if (preserve_gid) | |
652 | gid = F_GID(first); | |
653 | if ((preserve_devices && IS_DEVICE(mode)) | |
654 | || (preserve_specials && IS_SPECIAL(mode))) { | |
655 | uint32 *devp = F_RDEV_P(first); | |
656 | rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)); | |
057f649f | 657 | extra_len += 2 * EXTRA_LEN; |
8cae71eb WD |
658 | } |
659 | if (preserve_links && S_ISLNK(mode)) | |
660 | linkname_len = strlen(F_SYMLINK(first)) + 1; | |
661 | else | |
662 | linkname_len = 0; | |
663 | goto create_object; | |
664 | } | |
665 | #endif | |
666 | ||
a289addd | 667 | file_length = read_longint(f); |
d01d15e0 | 668 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 669 | modtime = (time_t)read_int(f); |
d01d15e0 | 670 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 671 | mode = from_wire_mode(read_int(f)); |
1ef00d20 | 672 | |
ebec5eb6 | 673 | if (chmod_modes && !S_ISLNK(mode)) |
8bbe41b5 WD |
674 | mode = tweak_mode(mode, chmod_modes); |
675 | ||
c7e6f84f | 676 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
18438f0b WD |
677 | if (protocol_version < 30) |
678 | uid = (uid_t)read_int(f); | |
679 | else { | |
680 | uid = (uid_t)read_abbrevint(f); | |
681 | if (flags & XMIT_USER_NAME_FOLLOWS) | |
682 | uid = recv_user_name(f, uid); | |
683 | else if (inc_recurse && am_root && !numeric_ids) | |
684 | uid = match_uid(uid); | |
685 | } | |
c7e6f84f WD |
686 | } |
687 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { | |
18438f0b WD |
688 | if (protocol_version < 30) |
689 | gid = (gid_t)read_int(f); | |
690 | else { | |
691 | gid = (gid_t)read_abbrevint(f); | |
692 | if (flags & XMIT_GROUP_NAME_FOLLOWS) | |
693 | gid = recv_group_name(f, gid); | |
694 | else if (inc_recurse && (!am_root || !numeric_ids)) | |
695 | gid = match_gid(gid); | |
696 | } | |
c7e6f84f | 697 | } |
a289addd | 698 | |
b5c6a6ae WD |
699 | if ((preserve_devices && IS_DEVICE(mode)) |
700 | || (preserve_specials && IS_SPECIAL(mode))) { | |
75bc8600 | 701 | if (protocol_version < 28) { |
b5c6a6ae WD |
702 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
703 | rdev = (dev_t)read_int(f); | |
704 | } else { | |
9c5e91f8 WD |
705 | uint32 rdev_minor; |
706 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) | |
18438f0b WD |
707 | rdev_major = read_abbrevint30(f); |
708 | if (protocol_version >= 30) | |
709 | rdev_minor = read_abbrevint(f); | |
710 | else if (flags & XMIT_RDEV_MINOR_8_pre30) | |
9c5e91f8 WD |
711 | rdev_minor = read_byte(f); |
712 | else | |
713 | rdev_minor = read_int(f); | |
b4a09b72 | 714 | rdev = MAKEDEV(rdev_major, rdev_minor); |
1ef00d20 | 715 | } |
96293cf9 WD |
716 | extra_len += 2 * EXTRA_LEN; |
717 | file_length = 0; | |
b5c6a6ae | 718 | } else if (protocol_version < 28) |
b4a09b72 | 719 | rdev = MAKEDEV(0, 0); |
72914a60 | 720 | |
4f5b0756 | 721 | #ifdef SUPPORT_LINKS |
30f337c9 | 722 | if (preserve_links && S_ISLNK(mode)) { |
1c3344a1 | 723 | linkname_len = read_abbrevint30(f) + 1; /* count the '\0' */ |
a289addd WD |
724 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { |
725 | rprintf(FERROR, "overflow: linkname_len=%d\n", | |
726 | linkname_len - 1); | |
82ad07c4 | 727 | overflow_exit("recv_file_entry"); |
9dd891bb | 728 | } |
72914a60 | 729 | } |
a289addd | 730 | else |
0d162bd1 | 731 | #endif |
a289addd | 732 | linkname_len = 0; |
0d162bd1 | 733 | |
82ad07c4 | 734 | #ifdef SUPPORT_HARD_LINKS |
8cae71eb | 735 | create_object: |
4785cd43 WD |
736 | if (preserve_hard_links) { |
737 | if (protocol_version < 28 && S_ISREG(mode)) | |
c7565dad WD |
738 | flags |= XMIT_HLINKED; |
739 | if (flags & XMIT_HLINKED) | |
4785cd43 | 740 | extra_len += EXTRA_LEN; |
82ad07c4 WD |
741 | } |
742 | #endif | |
743 | ||
1c3344a1 WD |
744 | #ifdef SUPPORT_ACLS |
745 | /* We need one or two index int32s when we're preserving ACLs. */ | |
746 | if (preserve_acls) | |
747 | extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN; | |
748 | #endif | |
749 | ||
96293cf9 WD |
750 | if (always_checksum && S_ISREG(mode)) |
751 | extra_len += SUM_EXTRA_CNT * EXTRA_LEN; | |
a289addd | 752 | |
96293cf9 WD |
753 | if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) |
754 | extra_len += EXTRA_LEN; | |
755 | ||
a3e18c76 WD |
756 | #if EXTRA_ROUNDING > 0 |
757 | if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN)) | |
758 | extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN; | |
759 | #endif | |
760 | ||
3ea6e0e7 | 761 | if (inc_recurse && S_ISDIR(mode)) { |
c7e6f84f WD |
762 | if (one_file_system) { |
763 | /* Room to save the dir's device for -x */ | |
764 | extra_len += 2 * EXTRA_LEN; | |
765 | } | |
766 | flist = dir_flist; | |
767 | } | |
768 | ||
769 | alloc_len = FILE_STRUCT_LEN + extra_len + basename_len | |
96293cf9 | 770 | + linkname_len; |
82ad07c4 | 771 | bp = pool_alloc(flist->file_pool, alloc_len, "recv_file_entry"); |
9935066b | 772 | |
a3e18c76 | 773 | memset(bp, 0, extra_len + FILE_STRUCT_LEN); |
82ad07c4 | 774 | bp += extra_len; |
f5db0993 | 775 | file = (struct file_struct *)bp; |
96293cf9 WD |
776 | bp += FILE_STRUCT_LEN; |
777 | ||
778 | memcpy(bp, basename, basename_len); | |
779 | bp += basename_len + linkname_len; /* skip space for symlink too */ | |
a289addd | 780 | |
82ad07c4 | 781 | #ifdef SUPPORT_HARD_LINKS |
c7565dad | 782 | if (flags & XMIT_HLINKED) |
96293cf9 | 783 | file->flags |= FLAG_HLINKED; |
82ad07c4 | 784 | #endif |
a289addd | 785 | file->modtime = modtime; |
60af9465 | 786 | file->len32 = (uint32)file_length; |
96293cf9 WD |
787 | if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) { |
788 | file->flags |= FLAG_LENGTH64; | |
789 | OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32); | |
790 | } | |
a289addd | 791 | file->mode = mode; |
82ad07c4 | 792 | if (preserve_uid) |
f1482c33 | 793 | F_OWNER(file) = uid; |
82ad07c4 | 794 | if (preserve_gid) |
f1482c33 | 795 | F_GROUP(file) = gid; |
a289addd | 796 | |
c7e6f84f WD |
797 | if (basename != thisname) { |
798 | file->dirname = lastdir; | |
9d737ecb | 799 | F_DEPTH(file) = lastdir_depth + 1; |
f3c3ed44 | 800 | } else |
9d737ecb | 801 | F_DEPTH(file) = 1; |
f3c3ed44 | 802 | |
649f8742 | 803 | if (S_ISDIR(mode)) { |
ee3751c8 | 804 | if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */ |
9d737ecb | 805 | F_DEPTH(file)--; |
ee3751c8 | 806 | if (flags & XMIT_TOP_DIR) { |
75c51953 | 807 | in_del_hier = recurse; |
9d737ecb | 808 | del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2; |
7a16e122 | 809 | if (relative_paths && del_hier_name_len > 2 |
9cdadbb1 WD |
810 | && lastname[del_hier_name_len-1] == '.' |
811 | && lastname[del_hier_name_len-2] == '/') | |
7a16e122 | 812 | del_hier_name_len -= 2; |
82ad07c4 | 813 | file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR; |
ee3751c8 | 814 | } else if (in_del_hier) { |
f3c3ed44 WD |
815 | if (!relative_paths || !del_hier_name_len |
816 | || (l1 >= del_hier_name_len | |
9cdadbb1 | 817 | && lastname[del_hier_name_len] == '/')) |
82ad07c4 | 818 | file->flags |= FLAG_XFER_DIR; |
649f8742 WD |
819 | else |
820 | in_del_hier = 0; | |
821 | } | |
822 | } | |
823 | ||
b5c6a6ae | 824 | if ((preserve_devices && IS_DEVICE(mode)) |
82ad07c4 | 825 | || (preserve_specials && IS_SPECIAL(mode))) { |
4785cd43 WD |
826 | uint32 *devp = F_RDEV_P(file); |
827 | DEV_MAJOR(devp) = major(rdev); | |
828 | DEV_MINOR(devp) = minor(rdev); | |
82ad07c4 | 829 | } |
a289addd | 830 | |
4f5b0756 | 831 | #ifdef SUPPORT_LINKS |
a289addd | 832 | if (linkname_len) { |
c58c1dc4 | 833 | bp = (char*)file->basename + basename_len; |
8cae71eb WD |
834 | if (first_hlink_ndx >= 0) { |
835 | struct file_struct *first = flist->files[first_hlink_ndx]; | |
836 | memcpy(bp, F_SYMLINK(first), linkname_len); | |
837 | } else | |
838 | read_sbuf(f, bp, linkname_len - 1); | |
1a7f3d99 | 839 | if (sanitize_paths) |
dcebf78f | 840 | sanitize_path(bp, bp, "", lastdir_depth, NULL); |
a289addd WD |
841 | } |
842 | #endif | |
843 | ||
4f5b0756 | 844 | #ifdef SUPPORT_HARD_LINKS |
c7565dad | 845 | if (preserve_hard_links && flags & XMIT_HLINKED) { |
8cae71eb WD |
846 | if (protocol_version >= 30) { |
847 | F_HL_GNUM(file) = flags & XMIT_HLINK_FIRST | |
848 | ? flist->count : first_hlink_ndx; | |
736a6a29 | 849 | } else { |
9d737ecb WD |
850 | static int32 cnt = 0; |
851 | struct idev_node *np; | |
852 | int64 ino; | |
853 | int32 ndx; | |
8cae71eb | 854 | if (protocol_version < 26) { |
9d737ecb WD |
855 | dev = read_int(f); |
856 | ino = read_int(f); | |
8cae71eb WD |
857 | } else { |
858 | if (!(flags & XMIT_SAME_DEV_pre30)) | |
859 | dev = read_longint(f); | |
9d737ecb | 860 | ino = read_longint(f); |
8cae71eb | 861 | } |
9d737ecb | 862 | np = idev_node(dev, ino); |
424d3691 | 863 | ndx = (int32)(long)np->data - 1; |
9d737ecb WD |
864 | if (ndx < 0) { |
865 | ndx = cnt++; | |
424d3691 | 866 | np->data = (void*)(long)cnt; |
9d737ecb WD |
867 | } |
868 | F_HL_GNUM(file) = ndx; | |
736a6a29 | 869 | } |
72914a60 | 870 | } |
dc5ddbcc | 871 | #endif |
ebed4c3a | 872 | |
96293cf9 WD |
873 | if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { |
874 | if (S_ISREG(mode)) | |
875 | bp = (char*)F_SUM(file); | |
82ad07c4 | 876 | else { |
fea4db62 | 877 | /* Prior to 28, we get a useless set of nulls. */ |
82ad07c4 | 878 | bp = tmp_sum; |
fea4db62 | 879 | } |
8cae71eb WD |
880 | if (first_hlink_ndx >= 0) { |
881 | struct file_struct *first = flist->files[first_hlink_ndx]; | |
882 | memcpy(bp, F_SUM(first), checksum_len); | |
883 | } else | |
884 | read_buf(f, bp, checksum_len); | |
72914a60 | 885 | } |
ebed4c3a | 886 | |
1c3344a1 WD |
887 | #ifdef SUPPORT_ACLS |
888 | if (preserve_acls && !S_ISLNK(mode)) | |
889 | receive_acl(file, f); | |
890 | #endif | |
891 | ||
ca947dea WD |
892 | if (S_ISREG(mode) || S_ISLNK(mode)) |
893 | stats.total_size += file_length; | |
894 | ||
f5db0993 | 895 | return file; |
c627d613 AT |
896 | } |
897 | ||
db719fb0 MP |
898 | /** |
899 | * Create a file_struct for a named file by reading its stat() | |
900 | * information and performing extensive checks against global | |
901 | * options. | |
902 | * | |
903 | * @return the new file, or NULL if there was an error or this file | |
904 | * should be excluded. | |
905 | * | |
906 | * @todo There is a small optimization opportunity here to avoid | |
907 | * stat()ing the file in some circumstances, which has a certain cost. | |
908 | * We are called immediately after doing readdir(), and so we may | |
909 | * already know the d_type of the file. We could for example avoid | |
910 | * statting directories if we're not recursing, but this is not a very | |
911 | * important case. Some systems may not have d_type. | |
912 | **/ | |
45d8bfe0 | 913 | struct file_struct *make_file(const char *fname, struct file_list *flist, |
82ad07c4 | 914 | STRUCT_STAT *stp, int flags, int filter_level) |
c627d613 | 915 | { |
a289addd WD |
916 | static char *lastdir; |
917 | static int lastdir_len = -1; | |
3ec4dd97 | 918 | struct file_struct *file; |
bcacc18b | 919 | STRUCT_STAT st; |
1923b1fc | 920 | char thisname[MAXPATHLEN]; |
e0870f1d | 921 | char linkname[MAXPATHLEN]; |
c7e6f84f | 922 | int alloc_len, basename_len, linkname_len; |
99a957d3 | 923 | int extra_len = file_extra_cnt * EXTRA_LEN; |
c7e6f84f WD |
924 | const char *basename; |
925 | char *bp; | |
9935066b | 926 | |
1923b1fc WD |
927 | if (strlcpy(thisname, fname, sizeof thisname) |
928 | >= sizeof thisname - flist_dir_len) { | |
0ee6ca98 | 929 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
882e6893 WD |
930 | return NULL; |
931 | } | |
58b1999e | 932 | clean_fname(thisname, 0); |
b7736c79 | 933 | if (sanitize_paths) |
a2248aea | 934 | sanitize_path(thisname, thisname, "", 0, NULL); |
3ec4dd97 | 935 | |
e4fdf1de | 936 | if (stp && S_ISDIR(stp->st_mode)) { |
ede1f0eb | 937 | st = *stp; /* Needed for "symlink/." with --relative. */ |
e4fdf1de WD |
938 | *linkname = '\0'; /* make IBM code checker happy */ |
939 | } else if (readlink_stat(thisname, &st, linkname) != 0) { | |
76e26e10 | 940 | int save_errno = errno; |
a4a7e64c | 941 | /* See if file is excluded before reporting an error. */ |
7842418b | 942 | if (filter_level != NO_FILTERS |
60cc01a6 | 943 | && (is_excluded(thisname, 0, filter_level) |
53039410 WD |
944 | || is_excluded(thisname, 1, filter_level))) { |
945 | if (ignore_perishable && save_errno != ENOENT) | |
946 | non_perishable_cnt++; | |
a4a7e64c | 947 | return NULL; |
53039410 | 948 | } |
a4a7e64c | 949 | if (save_errno == ENOENT) { |
4f5b0756 | 950 | #ifdef SUPPORT_LINKS |
a4a7e64c WD |
951 | /* Avoid "vanished" error if symlink points nowhere. */ |
952 | if (copy_links && do_lstat(thisname, &st) == 0 | |
953 | && S_ISLNK(st.st_mode)) { | |
954 | io_error |= IOERR_GENERAL; | |
955 | rprintf(FERROR, "symlink has no referent: %s\n", | |
956 | full_fname(thisname)); | |
e5ce3bcf WD |
957 | } else |
958 | #endif | |
959 | { | |
a4a7e64c WD |
960 | enum logcode c = am_daemon && protocol_version < 28 |
961 | ? FERROR : FINFO; | |
962 | io_error |= IOERR_VANISHED; | |
963 | rprintf(c, "file has vanished: %s\n", | |
964 | full_fname(thisname)); | |
76e26e10 | 965 | } |
a4a7e64c | 966 | } else { |
a8726d2a | 967 | io_error |= IOERR_GENERAL; |
d62bcc17 WD |
968 | rsyserr(FERROR, save_errno, "readlink %s failed", |
969 | full_fname(thisname)); | |
76e26e10 | 970 | } |
3ec4dd97 AT |
971 | return NULL; |
972 | } | |
c627d613 | 973 | |
7842418b WD |
974 | /* backup.c calls us with filter_level set to NO_FILTERS. */ |
975 | if (filter_level == NO_FILTERS) | |
976 | goto skip_filters; | |
ac1a0994 | 977 | |
7e037c42 | 978 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
0ee6ca98 | 979 | rprintf(FINFO, "skipping directory %s\n", thisname); |
3ec4dd97 AT |
980 | return NULL; |
981 | } | |
ebed4c3a | 982 | |
d521e1c2 | 983 | /* -x only affects directories because we need to avoid recursing |
e90cdb8a WD |
984 | * into a mount-point directory, not to avoid copying a symlinked |
985 | * file if -L (or similar) was specified. */ | |
535737bf WD |
986 | if (one_file_system && st.st_dev != filesystem_dev |
987 | && S_ISDIR(st.st_mode)) { | |
988 | if (one_file_system > 1) { | |
989 | if (verbose > 2) { | |
990 | rprintf(FINFO, "skipping mount-point dir %s\n", | |
991 | thisname); | |
ebec5eb6 | 992 | } |
535737bf | 993 | return NULL; |
ebec5eb6 | 994 | } |
82ad07c4 | 995 | flags |= FLAG_MOUNT_DIR; |
3b173846 | 996 | } |
ebed4c3a | 997 | |
53039410 WD |
998 | if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) { |
999 | if (ignore_perishable) | |
1000 | non_perishable_cnt++; | |
76e26e10 | 1001 | return NULL; |
53039410 | 1002 | } |
76e26e10 | 1003 | |
132fcf36 | 1004 | if (lp_ignore_nonreadable(module_id)) { |
4f5b0756 | 1005 | #ifdef SUPPORT_LINKS |
132fcf36 WD |
1006 | if (!S_ISLNK(st.st_mode)) |
1007 | #endif | |
1008 | if (access(thisname, R_OK) != 0) | |
1009 | return NULL; | |
1010 | } | |
ac1a0994 | 1011 | |
97b7bff4 | 1012 | skip_filters: |
ac1a0994 | 1013 | |
c7e6f84f WD |
1014 | /* Only divert a directory in the main transfer. */ |
1015 | if (flist && flist->prev && S_ISDIR(st.st_mode) | |
1016 | && flags & FLAG_DIVERT_DIRS) { | |
1017 | flist = dir_flist; | |
1018 | /* Room for parent/sibling/next-child info. */ | |
1019 | extra_len += 3 * EXTRA_LEN; | |
1020 | } | |
1021 | ||
ea847c62 WD |
1022 | if (verbose > 2) { |
1023 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", | |
0ee6ca98 | 1024 | who_am_i(), thisname, filter_level); |
ea847c62 | 1025 | } |
ebed4c3a | 1026 | |
a289addd | 1027 | if ((basename = strrchr(thisname, '/')) != NULL) { |
c7e6f84f WD |
1028 | int len = basename++ - thisname; |
1029 | if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) { | |
1030 | lastdir = new_array(char, len + 1); | |
1031 | memcpy(lastdir, thisname, len); | |
1032 | lastdir[len] = '\0'; | |
1033 | lastdir_len = len; | |
1034 | } | |
1035 | } else | |
a289addd | 1036 | basename = thisname; |
a289addd | 1037 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
c627d613 | 1038 | |
4f5b0756 | 1039 | #ifdef SUPPORT_LINKS |
a289addd WD |
1040 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; |
1041 | #else | |
1042 | linkname_len = 0; | |
1043 | #endif | |
1044 | ||
96293cf9 WD |
1045 | if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) |
1046 | extra_len += EXTRA_LEN; | |
1047 | ||
a3e18c76 WD |
1048 | #if EXTRA_ROUNDING > 0 |
1049 | if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN)) | |
1050 | extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN; | |
1051 | #endif | |
1052 | ||
c7e6f84f | 1053 | alloc_len = FILE_STRUCT_LEN + extra_len + basename_len |
96293cf9 | 1054 | + linkname_len; |
99aaa6ca WD |
1055 | if (flist) |
1056 | bp = pool_alloc(flist->file_pool, alloc_len, "make_file"); | |
1057 | else { | |
9935066b | 1058 | if (!(bp = new_array(char, alloc_len))) |
99aaa6ca | 1059 | out_of_memory("make_file"); |
9935066b S |
1060 | } |
1061 | ||
a3e18c76 | 1062 | memset(bp, 0, extra_len + FILE_STRUCT_LEN); |
82ad07c4 | 1063 | bp += extra_len; |
a289addd | 1064 | file = (struct file_struct *)bp; |
96293cf9 WD |
1065 | bp += FILE_STRUCT_LEN; |
1066 | ||
1067 | memcpy(bp, basename, basename_len); | |
1068 | bp += basename_len + linkname_len; /* skip space for symlink too */ | |
82ad07c4 WD |
1069 | |
1070 | #ifdef SUPPORT_HARD_LINKS | |
c7e6f84f | 1071 | if (preserve_hard_links && flist && flist->prev) { |
82ad07c4 WD |
1072 | if (protocol_version >= 28 |
1073 | ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1) | |
1074 | : S_ISREG(st.st_mode)) { | |
9d737ecb WD |
1075 | tmp_dev = st.st_dev; |
1076 | tmp_ino = st.st_ino; | |
82ad07c4 | 1077 | } else |
9d737ecb | 1078 | tmp_dev = 0; |
82ad07c4 WD |
1079 | } |
1080 | #endif | |
a289addd | 1081 | |
96293cf9 WD |
1082 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
1083 | if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) { | |
1084 | tmp_rdev = st.st_rdev; | |
1085 | st.st_size = 0; | |
1086 | } | |
1087 | #endif | |
1088 | ||
a289addd | 1089 | file->flags = flags; |
3ec4dd97 | 1090 | file->modtime = st.st_mtime; |
60af9465 | 1091 | file->len32 = (uint32)st.st_size; |
96293cf9 WD |
1092 | if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) { |
1093 | file->flags |= FLAG_LENGTH64; | |
1094 | OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32); | |
1095 | } | |
8bbe41b5 | 1096 | file->mode = st.st_mode; |
82ad07c4 | 1097 | if (preserve_uid) |
f1482c33 | 1098 | F_OWNER(file) = st.st_uid; |
82ad07c4 | 1099 | if (preserve_gid) |
f1482c33 | 1100 | F_GROUP(file) = st.st_gid; |
61dec11a | 1101 | |
c7e6f84f WD |
1102 | if (basename != thisname) |
1103 | file->dirname = lastdir; | |
a289addd | 1104 | |
4f5b0756 | 1105 | #ifdef SUPPORT_LINKS |
96293cf9 | 1106 | if (linkname_len) { |
c58c1dc4 | 1107 | bp = (char*)file->basename + basename_len; |
96293cf9 WD |
1108 | memcpy(bp, linkname, linkname_len); |
1109 | } | |
0d162bd1 WD |
1110 | #endif |
1111 | ||
82ad07c4 WD |
1112 | if (always_checksum && am_sender && S_ISREG(st.st_mode)) |
1113 | file_checksum(thisname, tmp_sum, st.st_size); | |
c627d613 | 1114 | |
9d737ecb | 1115 | F_ROOTDIR(file) = flist_dir; |
c627d613 | 1116 | |
23f4587f WD |
1117 | /* This code is only used by the receiver when it is building |
1118 | * a list of files for a delete pass. */ | |
1119 | if (keep_dirlinks && linkname_len && flist) { | |
1120 | STRUCT_STAT st2; | |
f5db0993 | 1121 | int save_mode = file->mode; |
8bbe41b5 | 1122 | file->mode = S_IFDIR; /* Find a directory with our name. */ |
c7e6f84f | 1123 | if (flist_find(dir_flist, file) >= 0 |
1a7f3d99 | 1124 | && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) { |
23f4587f | 1125 | file->modtime = st2.st_mtime; |
96293cf9 | 1126 | file->len32 = 0; |
23f4587f | 1127 | file->mode = st2.st_mode; |
82ad07c4 | 1128 | if (preserve_uid) |
f1482c33 | 1129 | F_OWNER(file) = st2.st_uid; |
82ad07c4 | 1130 | if (preserve_gid) |
f1482c33 | 1131 | F_GROUP(file) = st2.st_gid; |
f5db0993 WD |
1132 | } else |
1133 | file->mode = save_mode; | |
23f4587f WD |
1134 | } |
1135 | ||
82ad07c4 WD |
1136 | if (basename_len == 0+1) |
1137 | return NULL; | |
1138 | ||
3ea6e0e7 | 1139 | if (inc_recurse && flist == dir_flist) { |
c7e6f84f WD |
1140 | flist_expand(flist); |
1141 | flist->files[flist->count++] = file; | |
1142 | } | |
1143 | ||
3ec4dd97 | 1144 | return file; |
c627d613 AT |
1145 | } |
1146 | ||
17026f27 | 1147 | /* Only called for temporary file_struct entries created by make_file(). */ |
82ad07c4 WD |
1148 | void unmake_file(struct file_struct *file) |
1149 | { | |
99a957d3 | 1150 | int extra_cnt = file_extra_cnt + LEN64_BUMP(file); |
a3e18c76 WD |
1151 | #if EXTRA_ROUNDING > 0 |
1152 | if (extra_cnt & EXTRA_ROUNDING) | |
1153 | extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1; | |
1154 | #endif | |
1155 | free(REQ_EXTRA(file, extra_cnt)); | |
82ad07c4 WD |
1156 | } |
1157 | ||
42c6b139 | 1158 | static struct file_struct *send_file_name(int f, struct file_list *flist, |
ede1f0eb | 1159 | char *fname, STRUCT_STAT *stp, |
c7e6f84f | 1160 | int flags, int filter_flags) |
c627d613 | 1161 | { |
ebed4c3a | 1162 | struct file_struct *file; |
1c3344a1 WD |
1163 | #ifdef SUPPORT_ACLS |
1164 | statx sx; | |
1165 | #endif | |
ebed4c3a | 1166 | |
c7e6f84f | 1167 | file = make_file(fname, flist, stp, flags, filter_flags); |
37802f40 | 1168 | if (!file) |
301fb56c | 1169 | return NULL; |
ebed4c3a | 1170 | |
ebec5eb6 | 1171 | if (chmod_modes && !S_ISLNK(file->mode)) |
8bbe41b5 WD |
1172 | file->mode = tweak_mode(file->mode, chmod_modes); |
1173 | ||
1c3344a1 WD |
1174 | #ifdef SUPPORT_ACLS |
1175 | if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) { | |
1176 | sx.st.st_mode = file->mode; | |
1177 | sx.acc_acl = sx.def_acl = NULL; | |
1178 | if (get_acl(fname, &sx) < 0) | |
1179 | return NULL; | |
1180 | } | |
1181 | #endif | |
1182 | ||
53135fe8 | 1183 | maybe_emit_filelist_progress(flist->count + flist_count_offset); |
ebed4c3a | 1184 | |
d9d6bc52 | 1185 | flist_expand(flist); |
82ad07c4 | 1186 | flist->files[flist->count++] = file; |
1c3344a1 | 1187 | if (f >= 0) { |
42c6b139 | 1188 | send_file_entry(f, file, flist->count - 1); |
1c3344a1 WD |
1189 | #ifdef SUPPORT_ACLS |
1190 | if (preserve_acls && !S_ISLNK(file->mode)) { | |
1191 | send_acl(&sx, f); | |
1192 | free_acl(&sx); | |
1193 | } | |
1194 | #endif | |
1195 | } | |
301fb56c WD |
1196 | return file; |
1197 | } | |
ebed4c3a | 1198 | |
301fb56c | 1199 | static void send_if_directory(int f, struct file_list *flist, |
56f0c976 | 1200 | struct file_struct *file, |
c7e6f84f WD |
1201 | char *fbuf, unsigned int ol, |
1202 | int flags) | |
301fb56c | 1203 | { |
56f0c976 | 1204 | char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/'); |
301fb56c WD |
1205 | |
1206 | if (S_ISDIR(file->mode) | |
82ad07c4 | 1207 | && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) { |
f1773e09 WD |
1208 | void *save_filters; |
1209 | unsigned int len = strlen(fbuf); | |
1210 | if (len > 1 && fbuf[len-1] == '/') | |
1211 | fbuf[--len] = '\0'; | |
1212 | if (len >= MAXPATHLEN - 1) { | |
1213 | io_error |= IOERR_GENERAL; | |
1214 | rprintf(FERROR, "skipping long-named directory: %s\n", | |
1215 | full_fname(fbuf)); | |
1216 | return; | |
1217 | } | |
1218 | save_filters = push_local_filters(fbuf, len); | |
c7e6f84f | 1219 | send_directory(f, flist, -1, fbuf, len, flags); |
f1773e09 | 1220 | pop_local_filters(save_filters); |
56f0c976 WD |
1221 | fbuf[ol] = '\0'; |
1222 | if (is_dot_dir) | |
1223 | fbuf[ol-1] = '.'; | |
ebed4c3a | 1224 | } |
c627d613 AT |
1225 | } |
1226 | ||
c7e6f84f WD |
1227 | static int file_compare(struct file_struct **file1, struct file_struct **file2) |
1228 | { | |
1229 | return f_name_cmp(*file1, *file2); | |
1230 | } | |
1231 | ||
1232 | /* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end), | |
1233 | * sort them by name, and link them into the tree, setting the appropriate | |
1234 | * parent/child/sibling pointers. */ | |
1235 | static void add_dirs_to_tree(int parent_ndx, int start, int end) | |
1236 | { | |
1237 | int i; | |
1238 | int32 *dp = NULL; | |
1239 | int32 *parent_dp = parent_ndx < 0 ? NULL | |
1240 | : F_DIRNODE_P(dir_flist->files[parent_ndx]); | |
1241 | ||
1242 | qsort(dir_flist->files + start, end - start + 1, | |
1243 | sizeof dir_flist->files[0], (int (*)())file_compare); | |
1244 | ||
1245 | for (i = start; i <= end; i++) { | |
1246 | struct file_struct *file = dir_flist->files[i]; | |
1247 | if (!(file->flags & FLAG_XFER_DIR) | |
1248 | || file->flags & FLAG_MOUNT_DIR) | |
1249 | continue; | |
1250 | if (dp) | |
1251 | DIR_NEXT_SIBLING(dp) = i; | |
1252 | else if (parent_dp) | |
1253 | DIR_FIRST_CHILD(parent_dp) = i; | |
1254 | else | |
1255 | send_dir_ndx = i; | |
1256 | dp = F_DIRNODE_P(file); | |
1257 | DIR_PARENT(dp) = parent_ndx; | |
1258 | DIR_FIRST_CHILD(dp) = -1; | |
1259 | } | |
1260 | if (dp) | |
1261 | DIR_NEXT_SIBLING(dp) = -1; | |
1262 | } | |
1263 | ||
301fb56c | 1264 | /* This function is normally called by the sender, but the receiving side also |
7b558d7f | 1265 | * calls it from get_dirlist() with f set to -1 so that we just construct the |
301fb56c WD |
1266 | * file list in memory without sending it over the wire. Also, get_dirlist() |
1267 | * might call this with f set to -2, which also indicates that local filter | |
1268 | * rules should be ignored. */ | |
c7e6f84f WD |
1269 | static void send_directory(int f, struct file_list *flist, int parent_ndx, |
1270 | char *fbuf, int len, int flags) | |
c627d613 | 1271 | { |
3ec4dd97 | 1272 | struct dirent *di; |
32cbfe7b | 1273 | unsigned remainder; |
3ec4dd97 | 1274 | char *p; |
f1773e09 | 1275 | DIR *d; |
c7e6f84f WD |
1276 | int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0; |
1277 | int start = divert_dirs ? dir_flist->count : flist->count; | |
1278 | int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS; | |
c7e6f84f WD |
1279 | |
1280 | assert(flist != NULL); | |
3ec4dd97 | 1281 | |
f1773e09 | 1282 | if (!(d = opendir(fbuf))) { |
06c28400 | 1283 | io_error |= IOERR_GENERAL; |
f1773e09 | 1284 | rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf)); |
3ec4dd97 AT |
1285 | return; |
1286 | } | |
c627d613 | 1287 | |
19b2a5d9 WD |
1288 | p = fbuf + len; |
1289 | if (len != 1 || *fbuf != '/') | |
eddd5d12 | 1290 | *p++ = '/'; |
f1773e09 | 1291 | *p = '\0'; |
32cbfe7b | 1292 | remainder = MAXPATHLEN - (p - fbuf); |
ebed4c3a | 1293 | |
6a7cc46c | 1294 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 1295 | char *dname = d_name(di); |
6a7cc46c S |
1296 | if (dname[0] == '.' && (dname[1] == '\0' |
1297 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 1298 | continue; |
0ee6ca98 | 1299 | if (strlcpy(p, dname, remainder) >= remainder) { |
eddd5d12 WD |
1300 | io_error |= IOERR_GENERAL; |
1301 | rprintf(FINFO, | |
1302 | "cannot send long-named file %s\n", | |
f1773e09 | 1303 | full_fname(fbuf)); |
beaf4954 | 1304 | continue; |
eddd5d12 | 1305 | } |
0ee6ca98 | 1306 | |
42c6b139 | 1307 | send_file_name(f, flist, fbuf, NULL, flags, filter_flags); |
3ec4dd97 | 1308 | } |
32cbfe7b WD |
1309 | |
1310 | fbuf[len] = '\0'; | |
1311 | ||
6a7cc46c | 1312 | if (errno) { |
06c28400 | 1313 | io_error |= IOERR_GENERAL; |
71903f60 | 1314 | rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf)); |
6a7cc46c | 1315 | } |
c627d613 | 1316 | |
3ec4dd97 | 1317 | closedir(d); |
301fb56c | 1318 | |
c7e6f84f WD |
1319 | if (f < 0) |
1320 | return; | |
1321 | ||
1322 | if (divert_dirs) | |
1323 | add_dirs_to_tree(parent_ndx, start, dir_flist->count - 1); | |
1324 | else if (recurse) { | |
301fb56c | 1325 | int i, end = flist->count - 1; |
8cae71eb | 1326 | /* send_if_directory() bumps flist->count, so use "end". */ |
301fb56c | 1327 | for (i = start; i <= end; i++) |
c7e6f84f WD |
1328 | send_if_directory(f, flist, flist->files[i], fbuf, len, flags); |
1329 | } | |
1330 | } | |
1331 | ||
1332 | void send_extra_file_list(int f, int at_least) | |
1333 | { | |
1334 | char fbuf[MAXPATHLEN]; | |
1335 | struct file_list *flist; | |
1336 | int64 start_write; | |
7a2fa0c2 | 1337 | int future_cnt, save_io_error = io_error; |
c7e6f84f WD |
1338 | |
1339 | if (send_dir_ndx < 0) | |
1340 | return; | |
1341 | ||
1342 | /* Keep sending data until we have the requested number of | |
1343 | * files in the upcoming file-lists. */ | |
7a2fa0c2 WD |
1344 | if (cur_flist->next) { |
1345 | flist = first_flist->prev; /* the newest flist */ | |
1346 | future_cnt = flist->count | |
1347 | + flist->ndx_start - cur_flist->next->ndx_start; | |
1348 | } else | |
1349 | future_cnt = 0; | |
1350 | while (future_cnt < at_least) { | |
c7e6f84f WD |
1351 | struct file_struct *file = dir_flist->files[send_dir_ndx]; |
1352 | int32 *dp; | |
1353 | int dlen; | |
1354 | ||
1355 | f_name(file, fbuf); | |
1356 | dlen = strlen(fbuf); | |
1357 | ||
1358 | if (F_ROOTDIR(file) != flist_dir) { | |
1359 | if (!push_flist_dir(F_ROOTDIR(file), -1)) | |
1360 | exit_cleanup(RERR_FILESELECT); | |
1361 | } | |
1362 | ||
1363 | flist = flist_new(0, "send_extra_file_list"); | |
4c9d5fef | 1364 | start_write = stats.total_written; |
c7e6f84f | 1365 | |
9ae7a2cd | 1366 | write_ndx(f, NDX_FLIST_OFFSET - send_dir_ndx); |
c7e6f84f WD |
1367 | change_local_filter_dir(fbuf, dlen, send_dir_depth); |
1368 | send_directory(f, flist, send_dir_ndx, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR); | |
1369 | write_byte(f, 0); | |
1370 | ||
1371 | clean_flist(flist, 0, 0); | |
1372 | file_total += flist->count; | |
7a2fa0c2 | 1373 | future_cnt += flist->count; |
c7e6f84f WD |
1374 | stats.flist_size += stats.total_written - start_write; |
1375 | stats.num_files += flist->count; | |
1376 | if (verbose > 3) | |
1377 | output_flist(flist); | |
1378 | ||
1379 | dp = F_DIRNODE_P(file); | |
1380 | if (DIR_FIRST_CHILD(dp) >= 0) { | |
1381 | send_dir_ndx = DIR_FIRST_CHILD(dp); | |
1382 | send_dir_depth++; | |
1383 | } else { | |
1384 | while (DIR_NEXT_SIBLING(dp) < 0) { | |
1385 | if ((send_dir_ndx = DIR_PARENT(dp)) < 0) { | |
9ae7a2cd | 1386 | write_ndx(f, NDX_FLIST_EOF); |
c7e6f84f WD |
1387 | flist_eof = 1; |
1388 | change_local_filter_dir(NULL, 0, 0); | |
1389 | goto finish; | |
1390 | } | |
1391 | send_dir_depth--; | |
1392 | file = dir_flist->files[send_dir_ndx]; | |
1393 | dp = F_DIRNODE_P(file); | |
1394 | } | |
1395 | send_dir_ndx = DIR_NEXT_SIBLING(dp); | |
1396 | } | |
301fb56c | 1397 | } |
c7e6f84f WD |
1398 | |
1399 | finish: | |
1400 | if (io_error != save_io_error && !ignore_errors) | |
1401 | send_msg_int(MSG_IO_ERROR, io_error); | |
c627d613 AT |
1402 | } |
1403 | ||
ebed4c3a | 1404 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 1405 | { |
56f0c976 | 1406 | int len; |
bcacc18b | 1407 | STRUCT_STAT st; |
c7e6f84f | 1408 | char *p, *dir; |
ebed4c3a | 1409 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 1410 | struct file_list *flist; |
31b4d25d | 1411 | struct timeval start_tv, end_tv; |
a800434a | 1412 | int64 start_write; |
24d0fcde | 1413 | int use_ff_fd = 0; |
c7e6f84f | 1414 | int flags, disable_buffering; |
649d65ed | 1415 | |
ea124cb3 | 1416 | rprintf(FLOG, "building file list\n"); |
134f4338 | 1417 | if (show_filelist_p()) |
1bbd10fe | 1418 | start_filelist_progress("building file list"); |
3ea6e0e7 | 1419 | else if (inc_recurse && verbose && !am_server) |
98b1689d | 1420 | rprintf(FCLIENT, "sending incremental file list\n"); |
c627d613 | 1421 | |
a800434a | 1422 | start_write = stats.total_written; |
31b4d25d | 1423 | gettimeofday(&start_tv, NULL); |
a800434a | 1424 | |
8cae71eb | 1425 | #ifdef SUPPORT_HARD_LINKS |
c7e6f84f | 1426 | if (preserve_hard_links && protocol_version >= 30 && !cur_flist) |
8cae71eb WD |
1427 | init_hard_links(); |
1428 | #endif | |
1429 | ||
c7e6f84f | 1430 | flist = cur_flist = flist_new(0, "send_file_list"); |
3ea6e0e7 | 1431 | if (inc_recurse) { |
c7e6f84f WD |
1432 | dir_flist = flist_new(FLIST_TEMP, "send_file_list"); |
1433 | flags = FLAG_DIVERT_DIRS; | |
1434 | } else { | |
1435 | dir_flist = cur_flist; | |
1436 | flags = 0; | |
1437 | } | |
c627d613 | 1438 | |
c7e6f84f | 1439 | disable_buffering = io_start_buffering_out(f); |
134f4338 | 1440 | if (filesfrom_fd >= 0) { |
6f3684ff | 1441 | if (argv[0] && !push_dir(argv[0], 0)) { |
134f4338 WD |
1442 | rsyserr(FERROR, errno, "push_dir %s failed", |
1443 | full_fname(argv[0])); | |
1444 | exit_cleanup(RERR_FILESELECT); | |
24d0fcde | 1445 | } |
134f4338 | 1446 | use_ff_fd = 1; |
d6dead6b AT |
1447 | } |
1448 | ||
24d0fcde | 1449 | while (1) { |
56f0c976 WD |
1450 | char fbuf[MAXPATHLEN]; |
1451 | char *fn; | |
301fb56c | 1452 | int is_dot_dir; |
c627d613 | 1453 | |
24d0fcde | 1454 | if (use_ff_fd) { |
56f0c976 | 1455 | if (read_filesfrom_line(filesfrom_fd, fbuf) == 0) |
24d0fcde | 1456 | break; |
a2248aea | 1457 | sanitize_path(fbuf, fbuf, "", 0, NULL); |
24d0fcde WD |
1458 | } else { |
1459 | if (argc-- == 0) | |
1460 | break; | |
56f0c976 | 1461 | strlcpy(fbuf, *argv++, MAXPATHLEN); |
24d0fcde | 1462 | if (sanitize_paths) |
a2248aea | 1463 | sanitize_path(fbuf, fbuf, "", 0, NULL); |
24d0fcde | 1464 | } |
c627d613 | 1465 | |
56f0c976 | 1466 | len = strlen(fbuf); |
1902a765 WD |
1467 | if (relative_paths) { |
1468 | /* We clean up fbuf below. */ | |
1469 | is_dot_dir = 0; | |
1470 | } else if (!len || fbuf[len - 1] == '/') { | |
56f0c976 | 1471 | if (len == 2 && fbuf[0] == '.') { |
6931c138 | 1472 | /* Turn "./" into just "." rather than "./." */ |
56f0c976 | 1473 | fbuf[1] = '\0'; |
557a35f5 | 1474 | } else { |
56f0c976 | 1475 | if (len + 1 >= MAXPATHLEN) |
a1f99493 | 1476 | overflow_exit("send_file_list"); |
56f0c976 WD |
1477 | fbuf[len++] = '.'; |
1478 | fbuf[len] = '\0'; | |
53f821f1 | 1479 | } |
301fb56c | 1480 | is_dot_dir = 1; |
56f0c976 WD |
1481 | } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.' |
1482 | && (len == 2 || fbuf[len-3] == '/')) { | |
1483 | if (len + 2 >= MAXPATHLEN) | |
a1f99493 | 1484 | overflow_exit("send_file_list"); |
56f0c976 WD |
1485 | fbuf[len++] = '/'; |
1486 | fbuf[len++] = '.'; | |
1487 | fbuf[len] = '\0'; | |
a289f89f | 1488 | is_dot_dir = 1; |
301fb56c | 1489 | } else { |
56f0c976 WD |
1490 | is_dot_dir = fbuf[len-1] == '.' |
1491 | && (len == 1 || fbuf[len-2] == '/'); | |
649d65ed | 1492 | } |
c627d613 | 1493 | |
88c2190a | 1494 | if (link_stat(fbuf, &st, copy_dirlinks) != 0) { |
134f4338 WD |
1495 | io_error |= IOERR_GENERAL; |
1496 | rsyserr(FERROR, errno, "link_stat %s failed", | |
56f0c976 | 1497 | full_fname(fbuf)); |
649d65ed AT |
1498 | continue; |
1499 | } | |
c627d613 | 1500 | |
7e037c42 | 1501 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
0ee6ca98 | 1502 | rprintf(FINFO, "skipping directory %s\n", fbuf); |
649d65ed AT |
1503 | continue; |
1504 | } | |
c627d613 | 1505 | |
649d65ed AT |
1506 | dir = NULL; |
1507 | ||
1508 | if (!relative_paths) { | |
56f0c976 | 1509 | p = strrchr(fbuf, '/'); |
649d65ed | 1510 | if (p) { |
151f59f1 | 1511 | *p = '\0'; |
56f0c976 | 1512 | if (p == fbuf) |
649d65ed AT |
1513 | dir = "/"; |
1514 | else | |
56f0c976 WD |
1515 | dir = fbuf; |
1516 | len -= p - fbuf + 1; | |
1517 | fn = p + 1; | |
1518 | } else | |
1519 | fn = fbuf; | |
1902a765 WD |
1520 | } else { |
1521 | if ((p = strstr(fbuf, "/./")) != NULL) { | |
1522 | *p = '\0'; | |
1523 | if (p == fbuf) | |
1524 | dir = "/"; | |
1525 | else | |
1526 | dir = fbuf; | |
1527 | len -= p - fbuf + 3; | |
1528 | fn = p + 3; | |
1529 | } else | |
1530 | fn = fbuf; | |
1531 | /* Get rid of trailing "/" and "/.". */ | |
1532 | while (len) { | |
ede1f0eb WD |
1533 | if (fn[len - 1] == '/') { |
1534 | is_dot_dir = 1; | |
1535 | if (!--len && !dir) { | |
1536 | len++; | |
1537 | break; | |
1538 | } | |
1539 | } | |
1902a765 WD |
1540 | else if (len >= 2 && fn[len - 1] == '.' |
1541 | && fn[len - 2] == '/') { | |
ede1f0eb | 1542 | is_dot_dir = 1; |
1902a765 WD |
1543 | if (!(len -= 2) && !dir) { |
1544 | len++; | |
1545 | break; | |
1546 | } | |
1547 | } else | |
1548 | break; | |
1549 | } | |
8c449e62 WD |
1550 | if (len == 1 && fn[0] == '/') |
1551 | fn[len++] = '.'; | |
1902a765 WD |
1552 | fn[len] = '\0'; |
1553 | /* Reject a ".." dir in the active part of the path. */ | |
ede1f0eb WD |
1554 | for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) { |
1555 | if ((p[2] == '/' || p[2] == '\0') | |
1556 | && (p == fn || p[-1] == '/')) { | |
1557 | rprintf(FERROR, | |
1558 | "found \"..\" dir in relative path: %s\n", | |
1559 | fbuf); | |
1560 | exit_cleanup(RERR_SYNTAX); | |
1561 | } | |
1902a765 WD |
1562 | } |
1563 | } | |
d2ea5980 | 1564 | |
56f0c976 WD |
1565 | if (!*fn) { |
1566 | len = 1; | |
1567 | fn = "."; | |
1568 | } | |
d2ea5980 WD |
1569 | |
1570 | if (dir && *dir) { | |
cd87e2f5 | 1571 | static const char *lastdir; |
c7e6f84f WD |
1572 | static int lastdir_len = -1; |
1573 | int len = strlen(dir); | |
1574 | ||
1575 | if (len != lastdir_len || memcmp(lastdir, dir, len) != 0) { | |
1576 | if (!push_flist_dir(strdup(dir), len)) | |
1577 | goto push_error; | |
1578 | lastdir = flist_dir; | |
1579 | lastdir_len = flist_dir_len; | |
1580 | } else if (!push_flist_dir(lastdir, lastdir_len)) { | |
1581 | push_error: | |
d2ea5980 WD |
1582 | io_error |= IOERR_GENERAL; |
1583 | rsyserr(FERROR, errno, "push_dir %s failed", | |
1584 | full_fname(dir)); | |
1585 | continue; | |
1586 | } | |
d2ea5980 WD |
1587 | } |
1588 | ||
99eba675 | 1589 | if (fn != fbuf) |
56f0c976 WD |
1590 | memmove(fbuf, fn, len + 1); |
1591 | ||
1592 | if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) { | |
151f59f1 WD |
1593 | /* Send the implied directories at the start of the |
1594 | * source spec, so we get their permissions right. */ | |
56f0c976 | 1595 | char *lp = lastpath, *slash = fbuf; |
151f59f1 | 1596 | *p = '\0'; |
2154309a WD |
1597 | /* Skip any initial directories in our path that we |
1598 | * have in common with lastpath. */ | |
56f0c976 | 1599 | for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) { |
2154309a WD |
1600 | if (*fn == '/') |
1601 | slash = fn; | |
2154309a WD |
1602 | } |
1603 | *p = '/'; | |
1604 | if (fn != p || (*lp && *lp != '/')) { | |
0f57446d | 1605 | int save_copy_links = copy_links; |
7e037c42 | 1606 | int save_xfer_dirs = xfer_dirs; |
3ea6e0e7 | 1607 | int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0; |
88c2190a | 1608 | copy_links |= copy_unsafe_links; |
7e037c42 | 1609 | xfer_dirs = 1; |
2154309a | 1610 | while ((slash = strchr(slash+1, '/')) != 0) { |
151f59f1 | 1611 | *slash = '\0'; |
42c6b139 WD |
1612 | send_file_name(f, flist, fbuf, NULL, |
1613 | dir_flags, ALL_FILTERS); | |
2154309a | 1614 | *slash = '/'; |
649d65ed | 1615 | } |
0f57446d | 1616 | copy_links = save_copy_links; |
7e037c42 | 1617 | xfer_dirs = save_xfer_dirs; |
151f59f1 | 1618 | *p = '\0'; |
56f0c976 | 1619 | strlcpy(lastpath, fbuf, sizeof lastpath); |
649d65ed AT |
1620 | *p = '/'; |
1621 | } | |
1622 | } | |
ebed4c3a | 1623 | |
535737bf WD |
1624 | if (one_file_system) |
1625 | filesystem_dev = st.st_dev; | |
1626 | ||
75c51953 | 1627 | if (recurse || (xfer_dirs && is_dot_dir)) { |
42c6b139 | 1628 | struct file_struct *file; |
c7e6f84f WD |
1629 | int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR |
1630 | | (is_dot_dir ? 0 : flags) | |
3ea6e0e7 | 1631 | | (inc_recurse ? FLAG_DIVERT_DIRS : 0); |
42c6b139 WD |
1632 | file = send_file_name(f, flist, fbuf, &st, |
1633 | top_flags, ALL_FILTERS); | |
3ea6e0e7 | 1634 | if (file && !inc_recurse) |
42c6b139 WD |
1635 | send_if_directory(f, flist, file, fbuf, len, flags); |
1636 | } else | |
1637 | send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS); | |
649d65ed | 1638 | } |
dc5ddbcc | 1639 | |
134f4338 WD |
1640 | gettimeofday(&end_tv, NULL); |
1641 | stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1642 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
1643 | if (stats.flist_buildtime == 0) | |
1644 | stats.flist_buildtime = 1; | |
1645 | start_tv = end_tv; | |
31b4d25d | 1646 | |
8cae71eb WD |
1647 | write_byte(f, 0); /* Indicate end of file list */ |
1648 | ||
1649 | #ifdef SUPPORT_HARD_LINKS | |
3ea6e0e7 | 1650 | if (preserve_hard_links && protocol_version >= 30 && !inc_recurse) |
8cae71eb WD |
1651 | idev_destroy(); |
1652 | #endif | |
c627d613 | 1653 | |
134f4338 WD |
1654 | if (show_filelist_p()) |
1655 | finish_filelist_progress(flist); | |
31b4d25d | 1656 | |
134f4338 WD |
1657 | gettimeofday(&end_tv, NULL); |
1658 | stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1659 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
ebed4c3a | 1660 | |
c7e6f84f WD |
1661 | /* Sort the list without removing any duplicates in non-incremental |
1662 | * mode. This allows the receiving side to ask for whatever name it | |
1663 | * kept. For incremental mode, the sender also removes duplicates | |
1664 | * in this initial file-list so that it avoids re-sending duplicated | |
1665 | * directories. */ | |
3ea6e0e7 | 1666 | clean_flist(flist, 0, inc_recurse); |
c7e6f84f | 1667 | file_total += flist->count; |
ebed4c3a | 1668 | |
3ea6e0e7 | 1669 | if (!numeric_ids && !inc_recurse) |
82ad07c4 | 1670 | send_uid_list(f); |
f6c34742 | 1671 | |
134f4338 | 1672 | /* send the io_error flag */ |
c7e6f84f WD |
1673 | if (protocol_version < 30) |
1674 | write_int(f, ignore_errors ? 0 : io_error); | |
1675 | else if (io_error && !ignore_errors) | |
1676 | send_msg_int(MSG_IO_ERROR, io_error); | |
1677 | ||
1678 | if (disable_buffering) | |
1679 | io_end_buffering_out(); | |
6ba9279f | 1680 | |
134f4338 WD |
1681 | stats.flist_size = stats.total_written - start_write; |
1682 | stats.num_files = flist->count; | |
d6dead6b | 1683 | |
cefed3e8 | 1684 | if (verbose > 3) |
32cbfe7b | 1685 | output_flist(flist); |
cefed3e8 | 1686 | |
17faa41c | 1687 | if (verbose > 2) |
ebed4c3a | 1688 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1689 | |
3ea6e0e7 | 1690 | if (inc_recurse) { |
c7e6f84f WD |
1691 | add_dirs_to_tree(-1, 0, dir_flist->count - 1); |
1692 | if (file_total == 1) { | |
1693 | /* If we're creating incremental file-lists and there | |
1694 | * was just 1 item in the first file-list, send 1 more | |
1695 | * file-list to check if this is a 1-file xfer. */ | |
1696 | if (send_dir_ndx < 0) | |
9ae7a2cd | 1697 | write_ndx(f, NDX_DONE); |
c7e6f84f WD |
1698 | else |
1699 | send_extra_file_list(f, 1); | |
1700 | } | |
1701 | } | |
1702 | ||
649d65ed | 1703 | return flist; |
c627d613 AT |
1704 | } |
1705 | ||
c627d613 AT |
1706 | struct file_list *recv_file_list(int f) |
1707 | { | |
ebed4c3a | 1708 | struct file_list *flist; |
c7e6f84f | 1709 | int dstart, flags; |
ebed4c3a | 1710 | int64 start_read; |
c627d613 | 1711 | |
98b1689d | 1712 | if (!first_flist) |
c7e6f84f | 1713 | rprintf(FLOG, "receiving file list\n"); |
1bbd10fe DD |
1714 | if (show_filelist_p()) |
1715 | start_filelist_progress("receiving file list"); | |
3ea6e0e7 | 1716 | else if (inc_recurse && verbose && !am_server && !first_flist) |
98b1689d | 1717 | rprintf(FCLIENT, "receiving incremental file list\n"); |
c627d613 | 1718 | |
ebed4c3a | 1719 | start_read = stats.total_read; |
a800434a | 1720 | |
c7e6f84f | 1721 | flist = flist_new(0, "recv_file_list"); |
c627d613 | 1722 | |
8cae71eb WD |
1723 | #ifdef SUPPORT_HARD_LINKS |
1724 | if (preserve_hard_links && protocol_version < 30) | |
1725 | init_hard_links(); | |
1726 | #endif | |
c627d613 | 1727 | |
3ea6e0e7 | 1728 | if (inc_recurse) { |
c7e6f84f WD |
1729 | if (flist->ndx_start == 0) |
1730 | dir_flist = flist_new(FLIST_TEMP, "recv_file_list"); | |
1731 | dstart = dir_flist->count; | |
1732 | } else { | |
1733 | dir_flist = flist; | |
1734 | dstart = 0; | |
1735 | } | |
1736 | ||
1ef00d20 | 1737 | while ((flags = read_byte(f)) != 0) { |
f5db0993 | 1738 | struct file_struct *file; |
dbda5fbf | 1739 | |
d9d6bc52 | 1740 | flist_expand(flist); |
c627d613 | 1741 | |
d01d15e0 | 1742 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1743 | flags |= read_byte(f) << 8; |
82ad07c4 | 1744 | file = recv_file_entry(flist, flags, f); |
c627d613 | 1745 | |
3ea6e0e7 | 1746 | if (inc_recurse && S_ISDIR(file->mode)) { |
c7e6f84f WD |
1747 | flist_expand(dir_flist); |
1748 | dir_flist->files[dir_flist->count++] = file; | |
1749 | } | |
1750 | ||
f5db0993 | 1751 | flist->files[flist->count++] = file; |
c627d613 | 1752 | |
53135fe8 | 1753 | maybe_emit_filelist_progress(flist->count); |
1bbd10fe | 1754 | |
5e4ff5f9 WD |
1755 | if (verbose > 2) { |
1756 | rprintf(FINFO, "recv_file_name(%s)\n", | |
1757 | f_name(file, NULL)); | |
1758 | } | |
ebed4c3a | 1759 | } |
c7e6f84f | 1760 | file_total += flist->count; |
c627d613 | 1761 | |
ebed4c3a MP |
1762 | if (verbose > 2) |
1763 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1764 | |
b7736c79 | 1765 | if (show_filelist_p()) |
1bbd10fe | 1766 | finish_filelist_progress(flist); |
a06d19e3 | 1767 | |
983b1ed3 WD |
1768 | clean_flist(flist, relative_paths, 1); |
1769 | ||
3ea6e0e7 | 1770 | if (inc_recurse) { |
c7e6f84f WD |
1771 | qsort(dir_flist->files + dstart, dir_flist->count - dstart, |
1772 | sizeof dir_flist->files[0], (int (*)())file_compare); | |
1773 | } else if (f >= 0) | |
7b6fa00f | 1774 | recv_uid_list(f, flist); |
f6c34742 | 1775 | |
c7e6f84f | 1776 | if (protocol_version < 30) { |
b9f592fb | 1777 | /* Recv the io_error flag */ |
1f56188f | 1778 | if (ignore_errors) |
b9f592fb WD |
1779 | read_int(f); |
1780 | else | |
1781 | io_error |= read_int(f); | |
ebed4c3a | 1782 | } |
6ba9279f | 1783 | |
cefed3e8 | 1784 | if (verbose > 3) |
32cbfe7b | 1785 | output_flist(flist); |
cefed3e8 | 1786 | |
ebed4c3a MP |
1787 | if (list_only) { |
1788 | int i; | |
b7736c79 | 1789 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1790 | list_file_entry(flist->files[i]); |
ebed4c3a | 1791 | } |
f7632fc6 | 1792 | |
ebed4c3a MP |
1793 | if (verbose > 2) |
1794 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 1795 | |
c7e6f84f WD |
1796 | stats.flist_size += stats.total_read - start_read; |
1797 | stats.num_files += flist->count; | |
a800434a | 1798 | |
ebed4c3a | 1799 | return flist; |
c627d613 AT |
1800 | } |
1801 | ||
c7e6f84f WD |
1802 | /* This is only used once by the receiver if the very first file-list |
1803 | * has exactly one item in it. */ | |
1804 | void recv_additional_file_list(int f) | |
c627d613 | 1805 | { |
c7e6f84f | 1806 | struct file_list *flist; |
9ae7a2cd | 1807 | int ndx = read_ndx(f); |
c7e6f84f WD |
1808 | if (ndx == NDX_DONE) { |
1809 | flist_eof = 1; | |
1810 | change_local_filter_dir(NULL, 0, 0); | |
1811 | } else { | |
1812 | ndx = NDX_FLIST_OFFSET - ndx; | |
1813 | if (ndx < 0 || ndx >= dir_flist->count) { | |
1814 | ndx = NDX_FLIST_OFFSET - ndx; | |
1815 | rprintf(FERROR, | |
1816 | "Invalid dir index: %d (%d - %d)\n", | |
1817 | ndx, NDX_FLIST_OFFSET, | |
1818 | NDX_FLIST_OFFSET - dir_flist->count); | |
1819 | exit_cleanup(RERR_PROTOCOL); | |
1820 | } | |
1821 | flist = recv_file_list(f); | |
1822 | flist->parent_ndx = ndx; | |
1823 | } | |
c627d613 AT |
1824 | } |
1825 | ||
f5db0993 WD |
1826 | /* Search for an identically-named item in the file list. Note that the |
1827 | * items must agree in their directory-ness, or no match is returned. */ | |
2f3cad89 | 1828 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 1829 | { |
f3c3ed44 | 1830 | int low = flist->low, high = flist->high; |
207522ae | 1831 | int diff, mid, mid_up; |
f3c3ed44 WD |
1832 | |
1833 | while (low <= high) { | |
1834 | mid = (low + high) / 2; | |
96293cf9 | 1835 | if (F_IS_ACTIVE(flist->files[mid])) |
c0b134a4 | 1836 | mid_up = mid; |
7402d583 WD |
1837 | else { |
1838 | /* Scan for the next non-empty entry using the cached | |
1839 | * distance values. If the value isn't fully up-to- | |
1840 | * date, update it. */ | |
9d737ecb | 1841 | mid_up = mid + F_DEPTH(flist->files[mid]); |
96293cf9 | 1842 | if (!F_IS_ACTIVE(flist->files[mid_up])) { |
85aecef6 | 1843 | do { |
9d737ecb | 1844 | mid_up += F_DEPTH(flist->files[mid_up]); |
96293cf9 | 1845 | } while (!F_IS_ACTIVE(flist->files[mid_up])); |
9d737ecb | 1846 | F_DEPTH(flist->files[mid]) = mid_up - mid; |
c0b134a4 | 1847 | } |
c0b134a4 | 1848 | if (mid_up > high) { |
7402d583 WD |
1849 | /* If there's nothing left above us, set high to |
1850 | * a non-empty entry below us and continue. */ | |
96293cf9 WD |
1851 | high = mid - (int)flist->files[mid]->len32; |
1852 | if (!F_IS_ACTIVE(flist->files[high])) { | |
85aecef6 | 1853 | do { |
96293cf9 WD |
1854 | high -= (int)flist->files[high]->len32; |
1855 | } while (!F_IS_ACTIVE(flist->files[high])); | |
1856 | flist->files[mid]->len32 = mid - high; | |
7402d583 | 1857 | } |
c0b134a4 WD |
1858 | continue; |
1859 | } | |
c0b134a4 | 1860 | } |
207522ae WD |
1861 | diff = f_name_cmp(flist->files[mid_up], f); |
1862 | if (diff == 0) { | |
f5db0993 WD |
1863 | if (protocol_version < 29 |
1864 | && S_ISDIR(flist->files[mid_up]->mode) | |
1865 | != S_ISDIR(f->mode)) | |
1866 | return -1; | |
f3c3ed44 | 1867 | return mid_up; |
f5db0993 | 1868 | } |
207522ae | 1869 | if (diff < 0) |
f3c3ed44 | 1870 | low = mid_up + 1; |
207522ae WD |
1871 | else |
1872 | high = mid - 1; | |
d966ee25 | 1873 | } |
d966ee25 | 1874 | return -1; |
c627d613 AT |
1875 | } |
1876 | ||
3ec4dd97 | 1877 | /* |
9935066b S |
1878 | * Free up any resources a file_struct has allocated |
1879 | * and clear the file. | |
3ec4dd97 | 1880 | */ |
82ad07c4 | 1881 | void clear_file(struct file_struct *file) |
c627d613 | 1882 | { |
a3e18c76 WD |
1883 | /* The +1 zeros out the first char of the basename. */ |
1884 | memset(file, 0, FILE_STRUCT_LEN + 1); | |
9d737ecb | 1885 | /* In an empty entry, F_DEPTH() is an offset to the next non-empty |
96293cf9 | 1886 | * entry. Likewise for len32 in the opposite direction. We assume |
0679ac4c WD |
1887 | * that we're alone for now since flist_find() will adjust the counts |
1888 | * it runs into that aren't up-to-date. */ | |
9d737ecb | 1889 | file->len32 = F_DEPTH(file) = 1; |
3ec4dd97 | 1890 | } |
c627d613 | 1891 | |
4785cd43 | 1892 | /* Allocate a new file list. */ |
c7e6f84f | 1893 | struct file_list *flist_new(int flags, char *msg) |
3d382777 AT |
1894 | { |
1895 | struct file_list *flist; | |
1896 | ||
58cadc86 | 1897 | flist = new(struct file_list); |
ebed4c3a | 1898 | if (!flist) |
9935066b | 1899 | out_of_memory(msg); |
3d382777 | 1900 | |
82ad07c4 | 1901 | memset(flist, 0, sizeof flist[0]); |
9935066b | 1902 | |
c7e6f84f WD |
1903 | if (!(flags & FLIST_TEMP)) { |
1904 | if (first_flist) { | |
1905 | flist->ndx_start = first_flist->prev->ndx_start | |
1906 | + first_flist->prev->count; | |
1907 | } | |
1908 | /* This is a doubly linked list with prev looping back to | |
1909 | * the end of the list, but the last next pointer is NULL. */ | |
1910 | if (!first_flist) | |
1911 | first_flist = cur_flist = flist->prev = flist; | |
1912 | else { | |
1913 | flist->prev = first_flist->prev; | |
1914 | flist->prev->next = first_flist->prev = flist; | |
1915 | } | |
1916 | flist_cnt++; | |
1917 | } | |
1918 | ||
4a19c3b2 | 1919 | if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN))) |
9935066b S |
1920 | out_of_memory(msg); |
1921 | ||
3d382777 AT |
1922 | return flist; |
1923 | } | |
ebed4c3a | 1924 | |
4785cd43 | 1925 | /* Free up all elements in a flist. */ |
3ec4dd97 AT |
1926 | void flist_free(struct file_list *flist) |
1927 | { | |
c7e6f84f WD |
1928 | if (!flist->prev) |
1929 | ; /* Was FLIST_TEMP dir-list. */ | |
1930 | else if (flist == flist->prev) { | |
1931 | first_flist = cur_flist = NULL; | |
1932 | file_total = 0; | |
1933 | flist_cnt = 0; | |
1934 | } else { | |
1935 | if (flist == cur_flist) | |
1936 | cur_flist = flist->next; | |
1937 | if (flist == first_flist) | |
1938 | first_flist = first_flist->next; | |
1939 | else { | |
1940 | flist->prev->next = flist->next; | |
1941 | if (!flist->next) | |
1942 | flist->next = first_flist; | |
1943 | } | |
1944 | flist->next->prev = flist->prev; | |
1945 | file_total -= flist->count; | |
1946 | flist_cnt--; | |
1947 | } | |
1948 | ||
9935066b | 1949 | pool_destroy(flist->file_pool); |
3ec4dd97 | 1950 | free(flist->files); |
3ec4dd97 | 1951 | free(flist); |
c627d613 AT |
1952 | } |
1953 | ||
c627d613 AT |
1954 | /* |
1955 | * This routine ensures we don't have any duplicate names in our file list. | |
dbda5fbf | 1956 | * duplicate names can cause corruption because of the pipelining |
c627d613 | 1957 | */ |
827c37f6 | 1958 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
c627d613 | 1959 | { |
85aecef6 | 1960 | char fbuf[MAXPATHLEN]; |
6931c138 | 1961 | int i, prev_i = 0; |
c627d613 | 1962 | |
910ee8c9 WD |
1963 | if (!flist) |
1964 | return; | |
1965 | if (flist->count == 0) { | |
1966 | flist->high = -1; | |
3ec4dd97 | 1967 | return; |
910ee8c9 | 1968 | } |
3ec4dd97 | 1969 | |
ebed4c3a | 1970 | qsort(flist->files, flist->count, |
a4a7e64c | 1971 | sizeof flist->files[0], (int (*)())file_compare); |
ebed4c3a | 1972 | |
827c37f6 | 1973 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
96293cf9 | 1974 | if (F_IS_ACTIVE(flist->files[i])) { |
6931c138 | 1975 | prev_i = i; |
b91b50c0 WD |
1976 | break; |
1977 | } | |
1978 | } | |
f3c3ed44 | 1979 | flist->low = prev_i; |
b91b50c0 | 1980 | while (++i < flist->count) { |
fe1c19dc | 1981 | int j; |
f5db0993 WD |
1982 | struct file_struct *file = flist->files[i]; |
1983 | ||
96293cf9 | 1984 | if (!F_IS_ACTIVE(file)) |
b91b50c0 | 1985 | continue; |
fe1c19dc WD |
1986 | if (f_name_cmp(file, flist->files[prev_i]) == 0) |
1987 | j = prev_i; | |
1988 | else if (protocol_version >= 29 && S_ISDIR(file->mode)) { | |
f5db0993 WD |
1989 | int save_mode = file->mode; |
1990 | /* Make sure that this directory doesn't duplicate a | |
1991 | * non-directory earlier in the list. */ | |
f5db0993 | 1992 | flist->high = prev_i; |
fe1c19dc WD |
1993 | file->mode = S_IFREG; |
1994 | j = flist_find(flist, file); | |
f5db0993 | 1995 | file->mode = save_mode; |
fe1c19dc WD |
1996 | } else |
1997 | j = -1; | |
1998 | if (j >= 0) { | |
1999 | struct file_struct *fp = flist->files[j]; | |
2000 | int keep, drop; | |
2001 | /* If one is a dir and the other is not, we want to | |
2002 | * keep the dir because it might have contents in the | |
2003 | * list. */ | |
2004 | if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) { | |
2005 | if (S_ISDIR(file->mode)) | |
2006 | keep = i, drop = j; | |
2007 | else | |
2008 | keep = j, drop = i; | |
c7e6f84f | 2009 | } else if (protocol_version < 27) |
fe1c19dc | 2010 | keep = j, drop = i; |
c7e6f84f WD |
2011 | else |
2012 | keep = i, drop = j; | |
b91b50c0 | 2013 | if (verbose > 1 && !am_server) { |
ebed4c3a | 2014 | rprintf(FINFO, |
fe1c19dc | 2015 | "removing duplicate name %s from file list (%d)\n", |
85aecef6 | 2016 | f_name(file, fbuf), drop); |
b91b50c0 | 2017 | } |
9c000f5e WD |
2018 | /* Make sure we don't lose track of a user-specified |
2019 | * top directory. */ | |
0f0b2e66 | 2020 | flist->files[keep]->flags |= flist->files[drop]->flags |
82ad07c4 | 2021 | & (FLAG_TOP_DIR|FLAG_XFER_DIR); |
fe1c19dc | 2022 | |
82ad07c4 | 2023 | clear_file(flist->files[drop]); |
9935066b | 2024 | |
fe1c19dc WD |
2025 | if (keep == i) { |
2026 | if (flist->low == drop) { | |
2027 | for (j = drop + 1; | |
96293cf9 | 2028 | j < i && !F_IS_ACTIVE(flist->files[j]); |
fe1c19dc WD |
2029 | j++) {} |
2030 | flist->low = j; | |
2031 | } | |
2032 | prev_i = i; | |
2033 | } | |
728d0922 | 2034 | } else |
6931c138 | 2035 | prev_i = i; |
3ec4dd97 | 2036 | } |
f5db0993 | 2037 | flist->high = no_dups ? prev_i : flist->count - 1; |
0199b05f | 2038 | |
c0b134a4 WD |
2039 | if (strip_root) { |
2040 | /* We need to strip off the leading slashes for relative | |
2041 | * paths, but this must be done _after_ the sorting phase. */ | |
2042 | for (i = flist->low; i <= flist->high; i++) { | |
2043 | struct file_struct *file = flist->files[i]; | |
2044 | ||
2045 | if (!file->dirname) | |
2046 | continue; | |
2047 | while (*file->dirname == '/') | |
2048 | file->dirname++; | |
2049 | if (!*file->dirname) | |
2050 | file->dirname = NULL; | |
2051 | } | |
2052 | } | |
2053 | ||
e6ffb966 WD |
2054 | if (prune_empty_dirs && no_dups) { |
2055 | int j, prev_depth = 0; | |
9c000f5e | 2056 | |
e6ffb966 | 2057 | prev_i = 0; /* It's OK that this isn't really true. */ |
9c000f5e | 2058 | |
f5db0993 | 2059 | for (i = flist->low; i <= flist->high; i++) { |
e6ffb966 | 2060 | struct file_struct *fp, *file = flist->files[i]; |
ebed4c3a | 2061 | |
9d737ecb | 2062 | /* This temporarily abuses the F_DEPTH() value for a |
e6ffb966 WD |
2063 | * directory that is in a chain that might get pruned. |
2064 | * We restore the old value if it gets a reprieve. */ | |
9d737ecb | 2065 | if (S_ISDIR(file->mode) && F_DEPTH(file)) { |
e6ffb966 | 2066 | /* Dump empty dirs when coming back down. */ |
9d737ecb | 2067 | for (j = prev_depth; j >= F_DEPTH(file); j--) { |
e6ffb966 | 2068 | fp = flist->files[prev_i]; |
9d737ecb | 2069 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2070 | break; |
9d737ecb | 2071 | prev_i = -F_DEPTH(fp)-1; |
82ad07c4 | 2072 | clear_file(fp); |
9c000f5e | 2073 | } |
9d737ecb | 2074 | prev_depth = F_DEPTH(file); |
85aecef6 WD |
2075 | if (is_excluded(f_name(file, fbuf), 1, |
2076 | ALL_FILTERS)) { | |
e6ffb966 WD |
2077 | /* Keep dirs through this dir. */ |
2078 | for (j = prev_depth-1; ; j--) { | |
2079 | fp = flist->files[prev_i]; | |
9d737ecb | 2080 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2081 | break; |
9d737ecb WD |
2082 | prev_i = -F_DEPTH(fp)-1; |
2083 | F_DEPTH(fp) = j; | |
e6ffb966 | 2084 | } |
85aecef6 | 2085 | } else |
9d737ecb | 2086 | F_DEPTH(file) = -prev_i-1; |
e6ffb966 WD |
2087 | prev_i = i; |
2088 | } else { | |
2089 | /* Keep dirs through this non-dir. */ | |
2090 | for (j = prev_depth; ; j--) { | |
2091 | fp = flist->files[prev_i]; | |
9d737ecb | 2092 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2093 | break; |
9d737ecb WD |
2094 | prev_i = -F_DEPTH(fp)-1; |
2095 | F_DEPTH(fp) = j; | |
e6ffb966 | 2096 | } |
0199b05f | 2097 | } |
9c000f5e | 2098 | } |
ca947dea | 2099 | /* Dump all remaining empty dirs. */ |
e6ffb966 WD |
2100 | while (1) { |
2101 | struct file_struct *fp = flist->files[prev_i]; | |
9d737ecb | 2102 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2103 | break; |
9d737ecb | 2104 | prev_i = -F_DEPTH(fp)-1; |
82ad07c4 | 2105 | clear_file(fp); |
9c000f5e | 2106 | } |
f5db0993 | 2107 | |
9c000f5e | 2108 | for (i = flist->low; i <= flist->high; i++) { |
96293cf9 | 2109 | if (F_IS_ACTIVE(flist->files[i])) |
9c000f5e WD |
2110 | break; |
2111 | } | |
2112 | flist->low = i; | |
2113 | for (i = flist->high; i >= flist->low; i--) { | |
96293cf9 | 2114 | if (F_IS_ACTIVE(flist->files[i])) |
9c000f5e | 2115 | break; |
0199b05f | 2116 | } |
9c000f5e | 2117 | flist->high = i; |
0199b05f | 2118 | } |
cefed3e8 | 2119 | } |
0199b05f | 2120 | |
32cbfe7b | 2121 | static void output_flist(struct file_list *flist) |
cefed3e8 | 2122 | { |
f3c3ed44 | 2123 | char uidbuf[16], gidbuf[16], depthbuf[16]; |
cefed3e8 | 2124 | struct file_struct *file; |
9d737ecb | 2125 | const char *root, *dir, *slash, *name, *trail; |
32cbfe7b | 2126 | const char *who = who_am_i(); |
cefed3e8 | 2127 | int i; |
0199b05f | 2128 | |
c7e6f84f WD |
2129 | rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n", |
2130 | who, flist->ndx_start, flist->count, flist->low, flist->high); | |
ebed4c3a | 2131 | for (i = 0; i < flist->count; i++) { |
cefed3e8 | 2132 | file = flist->files[i]; |
82ad07c4 WD |
2133 | if ((am_root || am_sender) && preserve_uid) { |
2134 | snprintf(uidbuf, sizeof uidbuf, " uid=%ld", | |
2135 | (long)F_UID(file)); | |
2136 | } else | |
f05f993e | 2137 | *uidbuf = '\0'; |
82ad07c4 WD |
2138 | if (preserve_gid && F_GID(file) != GID_NONE) { |
2139 | snprintf(gidbuf, sizeof gidbuf, " gid=%ld", | |
2140 | (long)F_GID(file)); | |
2141 | } else | |
f05f993e | 2142 | *gidbuf = '\0'; |
f3c3ed44 | 2143 | if (!am_sender) |
9d737ecb | 2144 | snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file)); |
96293cf9 | 2145 | if (F_IS_ACTIVE(file)) { |
9d737ecb | 2146 | root = am_sender ? NS(F_ROOTDIR(file)) : depthbuf; |
96293cf9 WD |
2147 | if ((dir = file->dirname) == NULL) |
2148 | dir = slash = ""; | |
2149 | else | |
2150 | slash = "/"; | |
c58c1dc4 | 2151 | name = file->basename; |
96293cf9 WD |
2152 | trail = S_ISDIR(file->mode) ? "/" : ""; |
2153 | } else | |
9d737ecb | 2154 | root = dir = slash = name = trail = ""; |
14698a3a | 2155 | rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n", |
9d737ecb | 2156 | who, i, root, dir, slash, name, trail, (int)file->mode, |
96293cf9 | 2157 | (double)F_LENGTH(file), uidbuf, gidbuf, file->flags); |
0199b05f | 2158 | } |
3ec4dd97 AT |
2159 | } |
2160 | ||
8824e2ce | 2161 | enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING }; |
f5db0993 | 2162 | enum fnc_type { t_PATH, t_ITEM }; |
8018edd3 | 2163 | |
2f3cad89 | 2164 | /* Compare the names of two file_struct entities, similar to how strcmp() |
f5db0993 WD |
2165 | * would do if it were operating on the joined strings. |
2166 | * | |
2167 | * Some differences beginning with protocol_version 29: (1) directory names | |
2168 | * are compared with an assumed trailing slash so that they compare in a | |
2169 | * way that would cause them to sort immediately prior to any content they | |
2170 | * may have; (2) a directory of any name compares after a non-directory of | |
2171 | * any name at the same depth; (3) a directory with name "." compares prior | |
2172 | * to anything else. These changes mean that a directory and a non-dir | |
2173 | * with the same name will not compare as equal (protocol_version >= 29). | |
2174 | * | |
2175 | * The dirname component can be an empty string, but the basename component | |
2176 | * cannot (and never is in the current codebase). The basename component | |
2177 | * may be NULL (for a removed item), in which case it is considered to be | |
2178 | * after any existing item. */ | |
8018edd3 | 2179 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 2180 | { |
8018edd3 WD |
2181 | int dif; |
2182 | const uchar *c1, *c2; | |
1ef00d20 | 2183 | enum fnc_state state1, state2; |
f5db0993 WD |
2184 | enum fnc_type type1, type2; |
2185 | enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM; | |
8018edd3 | 2186 | |
96293cf9 WD |
2187 | if (!f1 || !F_IS_ACTIVE(f1)) { |
2188 | if (!f2 || !F_IS_ACTIVE(f2)) | |
8018edd3 WD |
2189 | return 0; |
2190 | return -1; | |
2191 | } | |
96293cf9 | 2192 | if (!f2 || !F_IS_ACTIVE(f2)) |
8018edd3 WD |
2193 | return 1; |
2194 | ||
14698a3a WD |
2195 | c1 = (uchar*)f1->dirname; |
2196 | c2 = (uchar*)f2->dirname; | |
2197 | if (c1 == c2) | |
2198 | c1 = c2 = NULL; | |
2199 | if (!c1) { | |
f5db0993 | 2200 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2201 | c1 = (const uchar*)f1->basename; |
f5db0993 WD |
2202 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
2203 | type1 = t_ITEM; | |
2204 | state1 = s_TRAILING; | |
2205 | c1 = (uchar*)""; | |
2206 | } else | |
2207 | state1 = s_BASE; | |
f5db0993 WD |
2208 | } else { |
2209 | type1 = t_path; | |
8824e2ce | 2210 | state1 = s_DIR; |
f5db0993 | 2211 | } |
14698a3a | 2212 | if (!c2) { |
f5db0993 | 2213 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2214 | c2 = (const uchar*)f2->basename; |
f5db0993 WD |
2215 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
2216 | type2 = t_ITEM; | |
2217 | state2 = s_TRAILING; | |
2218 | c2 = (uchar*)""; | |
2219 | } else | |
2220 | state2 = s_BASE; | |
f5db0993 WD |
2221 | } else { |
2222 | type2 = t_path; | |
8824e2ce | 2223 | state2 = s_DIR; |
f5db0993 WD |
2224 | } |
2225 | ||
2226 | if (type1 != type2) | |
2227 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 2228 | |
e4fdf1de | 2229 | do { |
f5db0993 | 2230 | if (!*c1) { |
8018edd3 | 2231 | switch (state1) { |
8824e2ce WD |
2232 | case s_DIR: |
2233 | state1 = s_SLASH; | |
e90b8ace | 2234 | c1 = (uchar*)"/"; |
8018edd3 | 2235 | break; |
8824e2ce | 2236 | case s_SLASH: |
f5db0993 | 2237 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2238 | c1 = (const uchar*)f1->basename; |
cbb5fa4f WD |
2239 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
2240 | type1 = t_ITEM; | |
2241 | state1 = s_TRAILING; | |
2242 | c1 = (uchar*)""; | |
2243 | } else | |
2244 | state1 = s_BASE; | |
8018edd3 | 2245 | break; |
8824e2ce WD |
2246 | case s_BASE: |
2247 | state1 = s_TRAILING; | |
f5db0993 | 2248 | if (type1 == t_PATH) { |
2f3cad89 | 2249 | c1 = (uchar*)"/"; |
f5db0993 WD |
2250 | break; |
2251 | } | |
2252 | /* FALL THROUGH */ | |
8824e2ce | 2253 | case s_TRAILING: |
f5db0993 | 2254 | type1 = t_ITEM; |
8018edd3 WD |
2255 | break; |
2256 | } | |
f5db0993 WD |
2257 | if (*c2 && type1 != type2) |
2258 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 2259 | } |
f5db0993 | 2260 | if (!*c2) { |
8018edd3 | 2261 | switch (state2) { |
8824e2ce WD |
2262 | case s_DIR: |
2263 | state2 = s_SLASH; | |
e90b8ace | 2264 | c2 = (uchar*)"/"; |
8018edd3 | 2265 | break; |
8824e2ce | 2266 | case s_SLASH: |
f5db0993 | 2267 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2268 | c2 = (const uchar*)f2->basename; |
cbb5fa4f WD |
2269 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
2270 | type2 = t_ITEM; | |
2271 | state2 = s_TRAILING; | |
2272 | c2 = (uchar*)""; | |
2273 | } else | |
2274 | state2 = s_BASE; | |
8018edd3 | 2275 | break; |
8824e2ce | 2276 | case s_BASE: |
8824e2ce | 2277 | state2 = s_TRAILING; |
f5db0993 | 2278 | if (type2 == t_PATH) { |
2f3cad89 | 2279 | c2 = (uchar*)"/"; |
f5db0993 WD |
2280 | break; |
2281 | } | |
2282 | /* FALL THROUGH */ | |
8824e2ce | 2283 | case s_TRAILING: |
f5db0993 WD |
2284 | if (!*c1) |
2285 | return 0; | |
2286 | type2 = t_ITEM; | |
8018edd3 WD |
2287 | break; |
2288 | } | |
f5db0993 WD |
2289 | if (type1 != type2) |
2290 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 2291 | } |
e4fdf1de | 2292 | } while ((dif = (int)*c1++ - (int)*c2++) == 0); |
8018edd3 WD |
2293 | |
2294 | return dif; | |
2295 | } | |
2296 | ||
96293cf9 WD |
2297 | char *f_name_buf(void) |
2298 | { | |
2299 | static char names[5][MAXPATHLEN]; | |
2300 | static unsigned int n; | |
2301 | ||
2302 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
2303 | ||
2304 | return names[n]; | |
2305 | } | |
2306 | ||
8018edd3 | 2307 | /* Return a copy of the full filename of a flist entry, using the indicated |
5e4ff5f9 WD |
2308 | * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is |
2309 | * done because we checked the size when creating the file_struct entry. | |
8018edd3 | 2310 | */ |
5e4ff5f9 | 2311 | char *f_name(struct file_struct *f, char *fbuf) |
8018edd3 | 2312 | { |
96293cf9 | 2313 | if (!f || !F_IS_ACTIVE(f)) |
ebed4c3a | 2314 | return NULL; |
3ec4dd97 | 2315 | |
96293cf9 WD |
2316 | if (!fbuf) |
2317 | fbuf = f_name_buf(); | |
5e4ff5f9 | 2318 | |
3ec4dd97 | 2319 | if (f->dirname) { |
882e6893 WD |
2320 | int len = strlen(f->dirname); |
2321 | memcpy(fbuf, f->dirname, len); | |
2322 | fbuf[len] = '/'; | |
c58c1dc4 | 2323 | strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1)); |
8018edd3 | 2324 | } else |
c58c1dc4 | 2325 | strlcpy(fbuf, f->basename, MAXPATHLEN); |
0ee6ca98 | 2326 | |
b7736c79 | 2327 | return fbuf; |
8018edd3 | 2328 | } |
e03dfae5 | 2329 | |
32cbfe7b WD |
2330 | /* Do a non-recursive scan of the named directory, possibly ignoring all |
2331 | * exclude rules except for the daemon's. If "dlen" is >=0, it is the length | |
2332 | * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN | |
2333 | * buffer (the functions we call will append names onto the end, but the old | |
2334 | * dir value will be restored on exit). */ | |
263d3bfb | 2335 | struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules) |
37802f40 WD |
2336 | { |
2337 | struct file_list *dirlist; | |
2338 | char dirbuf[MAXPATHLEN]; | |
37802f40 | 2339 | int save_recurse = recurse; |
1661fe9b | 2340 | int save_xfer_dirs = xfer_dirs; |
37802f40 | 2341 | |
32cbfe7b WD |
2342 | if (dlen < 0) { |
2343 | dlen = strlcpy(dirbuf, dirname, MAXPATHLEN); | |
2344 | if (dlen >= MAXPATHLEN) | |
2345 | return NULL; | |
2346 | dirname = dirbuf; | |
2347 | } | |
37802f40 | 2348 | |
c7e6f84f | 2349 | dirlist = flist_new(FLIST_TEMP, "get_dirlist"); |
32cbfe7b | 2350 | |
37802f40 | 2351 | recurse = 0; |
1661fe9b | 2352 | xfer_dirs = 1; |
c7e6f84f | 2353 | send_directory(ignore_filter_rules ? -2 : -1, dirlist, -1, dirname, dlen, 0); |
1661fe9b | 2354 | xfer_dirs = save_xfer_dirs; |
37802f40 | 2355 | recurse = save_recurse; |
53135fe8 WD |
2356 | if (do_progress) |
2357 | flist_count_offset += dirlist->count; | |
37802f40 | 2358 | |
564ef546 WD |
2359 | clean_flist(dirlist, 0, 0); |
2360 | ||
45478cc7 | 2361 | if (verbose > 3) |
32cbfe7b | 2362 | output_flist(dirlist); |
45478cc7 | 2363 | |
32cbfe7b | 2364 | return dirlist; |
45478cc7 | 2365 | } |