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