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