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