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) { |
858d45f1 | 415 | if ((uid_t)F_OWNER(file) == uid && *lastname) |
82ad07c4 | 416 | flags |= XMIT_SAME_UID; |
c7e6f84f | 417 | else { |
1564cd5a | 418 | uid = F_OWNER(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) { | |
858d45f1 | 427 | if ((gid_t)F_GROUP(file) == gid && *lastname) |
82ad07c4 | 428 | flags |= XMIT_SAME_GID; |
c7e6f84f | 429 | else { |
1564cd5a | 430 | gid = F_GROUP(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, |
1564cd5a | 597 | int xflags, 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; | |
cf1b4969 | 606 | static uint16 gid_flags; |
a289addd | 607 | static char lastname[MAXPATHLEN], *lastdir; |
33ffd7c3 | 608 | static int lastdir_depth, lastdir_len = -1; |
42f23f47 | 609 | static unsigned int del_hier_name_len = 0; |
649f8742 | 610 | static int in_del_hier = 0; |
72914a60 | 611 | char thisname[MAXPATHLEN]; |
ebed4c3a | 612 | unsigned int l1 = 0, l2 = 0; |
c7e6f84f | 613 | int alloc_len, basename_len, linkname_len; |
99a957d3 | 614 | int extra_len = file_extra_cnt * EXTRA_LEN; |
8cae71eb | 615 | int first_hlink_ndx = -1; |
a289addd | 616 | OFF_T file_length; |
c7e6f84f | 617 | const char *basename; |
72914a60 | 618 | struct file_struct *file; |
ef35abb2 WD |
619 | alloc_pool_t *pool; |
620 | char *bp; | |
72914a60 | 621 | |
1564cd5a | 622 | if (xflags & XMIT_SAME_NAME) |
72914a60 | 623 | l1 = read_byte(f); |
ebed4c3a | 624 | |
1564cd5a | 625 | if (xflags & XMIT_LONG_NAME) |
f31514ad | 626 | l2 = read_varint30(f); |
72914a60 AT |
627 | else |
628 | l2 = read_byte(f); | |
629 | ||
ebed4c3a MP |
630 | if (l2 >= MAXPATHLEN - l1) { |
631 | rprintf(FERROR, | |
1564cd5a WD |
632 | "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n", |
633 | xflags, l1, l2, lastname, who_am_i()); | |
82ad07c4 | 634 | overflow_exit("recv_file_entry"); |
d0fd26aa | 635 | } |
72914a60 | 636 | |
ebed4c3a MP |
637 | strlcpy(thisname, lastname, l1 + 1); |
638 | read_sbuf(f, &thisname[l1], l2); | |
639 | thisname[l1 + l2] = 0; | |
72914a60 | 640 | |
332cf6df WD |
641 | /* Abuse basename_len for a moment... */ |
642 | basename_len = strlcpy(lastname, thisname, MAXPATHLEN); | |
643 | ||
644 | #ifdef ICONV_OPTION | |
645 | if (ic_recv != (iconv_t)-1) { | |
58a37ed3 WD |
646 | char *obuf = thisname; |
647 | ICONV_CONST char *ibuf = (ICONV_CONST char *)lastname; | |
332cf6df WD |
648 | size_t ocnt = MAXPATHLEN, icnt = basename_len; |
649 | ||
650 | if (icnt >= MAXPATHLEN) { | |
651 | errno = E2BIG; | |
652 | goto convert_error; | |
653 | } | |
654 | ||
655 | iconv(ic_recv, NULL,0, NULL,0); | |
656 | if (iconv(ic_recv, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) { | |
657 | convert_error: | |
658 | io_error |= IOERR_GENERAL; | |
659 | rprintf(FINFO, | |
660 | "[%s] cannot convert filename: %s (%s)\n", | |
661 | who_am_i(), lastname, strerror(errno)); | |
662 | obuf = thisname; | |
663 | } | |
664 | *obuf = '\0'; | |
665 | } | |
666 | #endif | |
72914a60 | 667 | |
58b1999e | 668 | clean_fname(thisname, 0); |
72914a60 | 669 | |
0d162bd1 | 670 | if (sanitize_paths) |
a2248aea | 671 | sanitize_path(thisname, thisname, "", 0, NULL); |
cb13abfe | 672 | |
a289addd | 673 | if ((basename = strrchr(thisname, '/')) != NULL) { |
c7e6f84f WD |
674 | int len = basename++ - thisname; |
675 | if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) { | |
676 | lastdir = new_array(char, len + 1); | |
677 | memcpy(lastdir, thisname, len); | |
678 | lastdir[len] = '\0'; | |
679 | lastdir_len = len; | |
680 | lastdir_depth = count_dir_elements(lastdir); | |
681 | } | |
682 | } else | |
a289addd | 683 | basename = thisname; |
a289addd | 684 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
72914a60 | 685 | |
8cae71eb WD |
686 | #ifdef SUPPORT_HARD_LINKS |
687 | if (protocol_version >= 30 | |
1564cd5a | 688 | && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) { |
8cae71eb | 689 | struct file_struct *first; |
f31514ad | 690 | first_hlink_ndx = read_varint30(f); |
8cae71eb WD |
691 | if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) { |
692 | rprintf(FERROR, | |
693 | "hard-link reference out of range: %d (%d)\n", | |
694 | first_hlink_ndx, flist->count); | |
695 | exit_cleanup(RERR_PROTOCOL); | |
696 | } | |
697 | first = flist->files[first_hlink_ndx]; | |
698 | file_length = F_LENGTH(first); | |
699 | modtime = first->modtime; | |
700 | mode = first->mode; | |
701 | if (preserve_uid) | |
1564cd5a | 702 | uid = F_OWNER(first); |
8cae71eb | 703 | if (preserve_gid) |
1564cd5a | 704 | gid = F_GROUP(first); |
8cae71eb WD |
705 | if ((preserve_devices && IS_DEVICE(mode)) |
706 | || (preserve_specials && IS_SPECIAL(mode))) { | |
707 | uint32 *devp = F_RDEV_P(first); | |
708 | rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)); | |
057f649f | 709 | extra_len += 2 * EXTRA_LEN; |
8cae71eb WD |
710 | } |
711 | if (preserve_links && S_ISLNK(mode)) | |
712 | linkname_len = strlen(F_SYMLINK(first)) + 1; | |
713 | else | |
714 | linkname_len = 0; | |
715 | goto create_object; | |
716 | } | |
717 | #endif | |
718 | ||
ba59bd68 | 719 | file_length = read_varlong30(f, 3); |
1564cd5a | 720 | if (!(xflags & XMIT_SAME_TIME)) { |
f14b3ef4 WD |
721 | if (protocol_version >= 30) { |
722 | modtime = read_varlong(f, 4); | |
723 | #if SIZEOF_TIME_T < SIZEOF_INT64 | |
724 | if ((modtime > INT_MAX || modtime < INT_MIN) && !am_generator) { | |
725 | rprintf(FERROR, | |
726 | "Time value of %s truncated on receiver.\n", | |
727 | lastname); | |
728 | } | |
729 | #endif | |
730 | } else | |
731 | modtime = read_int(f); | |
ba59bd68 | 732 | } |
1564cd5a | 733 | if (!(xflags & XMIT_SAME_MODE)) |
30f337c9 | 734 | mode = from_wire_mode(read_int(f)); |
1ef00d20 | 735 | |
ebec5eb6 | 736 | if (chmod_modes && !S_ISLNK(mode)) |
8bbe41b5 WD |
737 | mode = tweak_mode(mode, chmod_modes); |
738 | ||
1564cd5a | 739 | if (preserve_uid && !(xflags & XMIT_SAME_UID)) { |
18438f0b WD |
740 | if (protocol_version < 30) |
741 | uid = (uid_t)read_int(f); | |
742 | else { | |
f31514ad | 743 | uid = (uid_t)read_varint(f); |
1564cd5a | 744 | if (xflags & XMIT_USER_NAME_FOLLOWS) |
18438f0b WD |
745 | uid = recv_user_name(f, uid); |
746 | else if (inc_recurse && am_root && !numeric_ids) | |
747 | uid = match_uid(uid); | |
748 | } | |
c7e6f84f | 749 | } |
1564cd5a | 750 | if (preserve_gid && !(xflags & XMIT_SAME_GID)) { |
18438f0b WD |
751 | if (protocol_version < 30) |
752 | gid = (gid_t)read_int(f); | |
753 | else { | |
f31514ad | 754 | gid = (gid_t)read_varint(f); |
cf1b4969 | 755 | gid_flags = 0; |
1564cd5a | 756 | if (xflags & XMIT_GROUP_NAME_FOLLOWS) |
cf1b4969 | 757 | gid = recv_group_name(f, gid, &gid_flags); |
18438f0b | 758 | else if (inc_recurse && (!am_root || !numeric_ids)) |
cf1b4969 | 759 | gid = match_gid(gid, &gid_flags); |
18438f0b | 760 | } |
c7e6f84f | 761 | } |
a289addd | 762 | |
b5c6a6ae WD |
763 | if ((preserve_devices && IS_DEVICE(mode)) |
764 | || (preserve_specials && IS_SPECIAL(mode))) { | |
75bc8600 | 765 | if (protocol_version < 28) { |
1564cd5a | 766 | if (!(xflags & XMIT_SAME_RDEV_pre28)) |
b5c6a6ae WD |
767 | rdev = (dev_t)read_int(f); |
768 | } else { | |
9c5e91f8 | 769 | uint32 rdev_minor; |
1564cd5a | 770 | if (!(xflags & XMIT_SAME_RDEV_MAJOR)) |
f31514ad | 771 | rdev_major = read_varint30(f); |
18438f0b | 772 | if (protocol_version >= 30) |
f31514ad | 773 | rdev_minor = read_varint(f); |
1564cd5a | 774 | else if (xflags & XMIT_RDEV_MINOR_8_pre30) |
9c5e91f8 WD |
775 | rdev_minor = read_byte(f); |
776 | else | |
777 | rdev_minor = read_int(f); | |
b4a09b72 | 778 | rdev = MAKEDEV(rdev_major, rdev_minor); |
1ef00d20 | 779 | } |
96293cf9 WD |
780 | extra_len += 2 * EXTRA_LEN; |
781 | file_length = 0; | |
b5c6a6ae | 782 | } else if (protocol_version < 28) |
b4a09b72 | 783 | rdev = MAKEDEV(0, 0); |
72914a60 | 784 | |
4f5b0756 | 785 | #ifdef SUPPORT_LINKS |
30f337c9 | 786 | if (preserve_links && S_ISLNK(mode)) { |
f31514ad | 787 | linkname_len = read_varint30(f) + 1; /* count the '\0' */ |
a289addd WD |
788 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { |
789 | rprintf(FERROR, "overflow: linkname_len=%d\n", | |
790 | linkname_len - 1); | |
82ad07c4 | 791 | overflow_exit("recv_file_entry"); |
9dd891bb | 792 | } |
72914a60 | 793 | } |
a289addd | 794 | else |
0d162bd1 | 795 | #endif |
a289addd | 796 | linkname_len = 0; |
0d162bd1 | 797 | |
82ad07c4 | 798 | #ifdef SUPPORT_HARD_LINKS |
8cae71eb | 799 | create_object: |
4785cd43 WD |
800 | if (preserve_hard_links) { |
801 | if (protocol_version < 28 && S_ISREG(mode)) | |
1564cd5a | 802 | xflags |= XMIT_HLINKED; |
cf1b4969 | 803 | if (xflags & XMIT_HLINKED) |
4785cd43 | 804 | extra_len += EXTRA_LEN; |
82ad07c4 WD |
805 | } |
806 | #endif | |
807 | ||
1c3344a1 WD |
808 | #ifdef SUPPORT_ACLS |
809 | /* We need one or two index int32s when we're preserving ACLs. */ | |
810 | if (preserve_acls) | |
811 | extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN; | |
812 | #endif | |
813 | ||
96293cf9 WD |
814 | if (always_checksum && S_ISREG(mode)) |
815 | extra_len += SUM_EXTRA_CNT * EXTRA_LEN; | |
a289addd | 816 | |
96293cf9 WD |
817 | if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) |
818 | extra_len += EXTRA_LEN; | |
819 | ||
a3e18c76 WD |
820 | #if EXTRA_ROUNDING > 0 |
821 | if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN)) | |
822 | extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN; | |
823 | #endif | |
824 | ||
3ea6e0e7 | 825 | if (inc_recurse && S_ISDIR(mode)) { |
c7e6f84f WD |
826 | if (one_file_system) { |
827 | /* Room to save the dir's device for -x */ | |
828 | extra_len += 2 * EXTRA_LEN; | |
829 | } | |
ef35abb2 WD |
830 | pool = dir_flist->file_pool; |
831 | } else | |
832 | pool = flist->file_pool; | |
c7e6f84f WD |
833 | |
834 | alloc_len = FILE_STRUCT_LEN + extra_len + basename_len | |
96293cf9 | 835 | + linkname_len; |
ef35abb2 | 836 | bp = pool_alloc(pool, alloc_len, "recv_file_entry"); |
9935066b | 837 | |
a3e18c76 | 838 | memset(bp, 0, extra_len + FILE_STRUCT_LEN); |
82ad07c4 | 839 | bp += extra_len; |
f5db0993 | 840 | file = (struct file_struct *)bp; |
96293cf9 WD |
841 | bp += FILE_STRUCT_LEN; |
842 | ||
843 | memcpy(bp, basename, basename_len); | |
844 | bp += basename_len + linkname_len; /* skip space for symlink too */ | |
a289addd | 845 | |
cf1b4969 WD |
846 | #ifdef SUPPORT_HARD_LINKS |
847 | if (xflags & XMIT_HLINKED) | |
848 | file->flags |= FLAG_HLINKED; | |
849 | #endif | |
f14b3ef4 | 850 | file->modtime = (time_t)modtime; |
60af9465 | 851 | file->len32 = (uint32)file_length; |
96293cf9 WD |
852 | if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) { |
853 | file->flags |= FLAG_LENGTH64; | |
854 | OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32); | |
855 | } | |
a289addd | 856 | file->mode = mode; |
82ad07c4 | 857 | if (preserve_uid) |
f1482c33 | 858 | F_OWNER(file) = uid; |
cf1b4969 | 859 | if (preserve_gid) { |
f1482c33 | 860 | F_GROUP(file) = gid; |
cf1b4969 WD |
861 | file->flags |= gid_flags; |
862 | } | |
332cf6df WD |
863 | #ifdef ICONV_OPTION |
864 | if (ic_ndx) | |
865 | F_NDX(file) = flist->count + flist->ndx_start; | |
866 | #endif | |
a289addd | 867 | |
c7e6f84f WD |
868 | if (basename != thisname) { |
869 | file->dirname = lastdir; | |
9d737ecb | 870 | F_DEPTH(file) = lastdir_depth + 1; |
f3c3ed44 | 871 | } else |
9d737ecb | 872 | F_DEPTH(file) = 1; |
f3c3ed44 | 873 | |
649f8742 | 874 | if (S_ISDIR(mode)) { |
ee3751c8 | 875 | if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */ |
9d737ecb | 876 | F_DEPTH(file)--; |
1564cd5a | 877 | if (xflags & XMIT_TOP_DIR) { |
75c51953 | 878 | in_del_hier = recurse; |
9d737ecb | 879 | del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2; |
7a16e122 | 880 | if (relative_paths && del_hier_name_len > 2 |
9cdadbb1 WD |
881 | && lastname[del_hier_name_len-1] == '.' |
882 | && lastname[del_hier_name_len-2] == '/') | |
7a16e122 | 883 | del_hier_name_len -= 2; |
82ad07c4 | 884 | file->flags |= FLAG_TOP_DIR | FLAG_XFER_DIR; |
ee3751c8 | 885 | } else if (in_del_hier) { |
f3c3ed44 WD |
886 | if (!relative_paths || !del_hier_name_len |
887 | || (l1 >= del_hier_name_len | |
9cdadbb1 | 888 | && lastname[del_hier_name_len] == '/')) |
82ad07c4 | 889 | file->flags |= FLAG_XFER_DIR; |
649f8742 WD |
890 | else |
891 | in_del_hier = 0; | |
892 | } | |
893 | } | |
894 | ||
b5c6a6ae | 895 | if ((preserve_devices && IS_DEVICE(mode)) |
82ad07c4 | 896 | || (preserve_specials && IS_SPECIAL(mode))) { |
4785cd43 WD |
897 | uint32 *devp = F_RDEV_P(file); |
898 | DEV_MAJOR(devp) = major(rdev); | |
899 | DEV_MINOR(devp) = minor(rdev); | |
82ad07c4 | 900 | } |
a289addd | 901 | |
4f5b0756 | 902 | #ifdef SUPPORT_LINKS |
a289addd | 903 | if (linkname_len) { |
c58c1dc4 | 904 | bp = (char*)file->basename + basename_len; |
8cae71eb WD |
905 | if (first_hlink_ndx >= 0) { |
906 | struct file_struct *first = flist->files[first_hlink_ndx]; | |
907 | memcpy(bp, F_SYMLINK(first), linkname_len); | |
908 | } else | |
909 | read_sbuf(f, bp, linkname_len - 1); | |
1a7f3d99 | 910 | if (sanitize_paths) |
dcebf78f | 911 | sanitize_path(bp, bp, "", lastdir_depth, NULL); |
a289addd WD |
912 | } |
913 | #endif | |
914 | ||
4f5b0756 | 915 | #ifdef SUPPORT_HARD_LINKS |
1564cd5a | 916 | if (preserve_hard_links && xflags & XMIT_HLINKED) { |
8cae71eb | 917 | if (protocol_version >= 30) { |
1564cd5a | 918 | F_HL_GNUM(file) = xflags & XMIT_HLINK_FIRST |
8cae71eb | 919 | ? flist->count : first_hlink_ndx; |
736a6a29 | 920 | } else { |
9d737ecb WD |
921 | static int32 cnt = 0; |
922 | struct idev_node *np; | |
923 | int64 ino; | |
924 | int32 ndx; | |
8cae71eb | 925 | if (protocol_version < 26) { |
9d737ecb WD |
926 | dev = read_int(f); |
927 | ino = read_int(f); | |
8cae71eb | 928 | } else { |
1564cd5a | 929 | if (!(xflags & XMIT_SAME_DEV_pre30)) |
8cae71eb | 930 | dev = read_longint(f); |
9d737ecb | 931 | ino = read_longint(f); |
8cae71eb | 932 | } |
9d737ecb | 933 | np = idev_node(dev, ino); |
424d3691 | 934 | ndx = (int32)(long)np->data - 1; |
9d737ecb WD |
935 | if (ndx < 0) { |
936 | ndx = cnt++; | |
424d3691 | 937 | np->data = (void*)(long)cnt; |
9d737ecb WD |
938 | } |
939 | F_HL_GNUM(file) = ndx; | |
736a6a29 | 940 | } |
72914a60 | 941 | } |
dc5ddbcc | 942 | #endif |
ebed4c3a | 943 | |
96293cf9 WD |
944 | if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { |
945 | if (S_ISREG(mode)) | |
946 | bp = (char*)F_SUM(file); | |
82ad07c4 | 947 | else { |
fea4db62 | 948 | /* Prior to 28, we get a useless set of nulls. */ |
82ad07c4 | 949 | bp = tmp_sum; |
fea4db62 | 950 | } |
8cae71eb WD |
951 | if (first_hlink_ndx >= 0) { |
952 | struct file_struct *first = flist->files[first_hlink_ndx]; | |
953 | memcpy(bp, F_SUM(first), checksum_len); | |
954 | } else | |
955 | read_buf(f, bp, checksum_len); | |
72914a60 | 956 | } |
ebed4c3a | 957 | |
1c3344a1 WD |
958 | #ifdef SUPPORT_ACLS |
959 | if (preserve_acls && !S_ISLNK(mode)) | |
960 | receive_acl(file, f); | |
961 | #endif | |
16edf865 WD |
962 | #ifdef SUPPORT_XATTRS |
963 | if (preserve_xattrs) | |
964 | receive_xattr(file, f ); | |
965 | #endif | |
1c3344a1 | 966 | |
ca947dea WD |
967 | if (S_ISREG(mode) || S_ISLNK(mode)) |
968 | stats.total_size += file_length; | |
969 | ||
f5db0993 | 970 | return file; |
c627d613 AT |
971 | } |
972 | ||
db719fb0 MP |
973 | /** |
974 | * Create a file_struct for a named file by reading its stat() | |
975 | * information and performing extensive checks against global | |
976 | * options. | |
977 | * | |
978 | * @return the new file, or NULL if there was an error or this file | |
979 | * should be excluded. | |
980 | * | |
981 | * @todo There is a small optimization opportunity here to avoid | |
982 | * stat()ing the file in some circumstances, which has a certain cost. | |
983 | * We are called immediately after doing readdir(), and so we may | |
984 | * already know the d_type of the file. We could for example avoid | |
985 | * statting directories if we're not recursing, but this is not a very | |
986 | * important case. Some systems may not have d_type. | |
987 | **/ | |
45d8bfe0 | 988 | struct file_struct *make_file(const char *fname, struct file_list *flist, |
82ad07c4 | 989 | STRUCT_STAT *stp, int flags, int filter_level) |
c627d613 | 990 | { |
a289addd WD |
991 | static char *lastdir; |
992 | static int lastdir_len = -1; | |
3ec4dd97 | 993 | struct file_struct *file; |
bcacc18b | 994 | STRUCT_STAT st; |
1923b1fc | 995 | char thisname[MAXPATHLEN]; |
e0870f1d | 996 | char linkname[MAXPATHLEN]; |
c7e6f84f | 997 | int alloc_len, basename_len, linkname_len; |
99a957d3 | 998 | int extra_len = file_extra_cnt * EXTRA_LEN; |
c7e6f84f WD |
999 | const char *basename; |
1000 | char *bp; | |
9935066b | 1001 | |
1923b1fc | 1002 | if (strlcpy(thisname, fname, sizeof thisname) |
ef35abb2 | 1003 | >= sizeof thisname - pathname_len) { |
0ee6ca98 | 1004 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
882e6893 WD |
1005 | return NULL; |
1006 | } | |
58b1999e | 1007 | clean_fname(thisname, 0); |
b7736c79 | 1008 | if (sanitize_paths) |
a2248aea | 1009 | sanitize_path(thisname, thisname, "", 0, NULL); |
3ec4dd97 | 1010 | |
e4fdf1de | 1011 | if (stp && S_ISDIR(stp->st_mode)) { |
ede1f0eb | 1012 | st = *stp; /* Needed for "symlink/." with --relative. */ |
e4fdf1de WD |
1013 | *linkname = '\0'; /* make IBM code checker happy */ |
1014 | } else if (readlink_stat(thisname, &st, linkname) != 0) { | |
76e26e10 | 1015 | int save_errno = errno; |
a4a7e64c | 1016 | /* See if file is excluded before reporting an error. */ |
7842418b | 1017 | if (filter_level != NO_FILTERS |
60cc01a6 | 1018 | && (is_excluded(thisname, 0, filter_level) |
53039410 WD |
1019 | || is_excluded(thisname, 1, filter_level))) { |
1020 | if (ignore_perishable && save_errno != ENOENT) | |
1021 | non_perishable_cnt++; | |
a4a7e64c | 1022 | return NULL; |
53039410 | 1023 | } |
a4a7e64c | 1024 | if (save_errno == ENOENT) { |
4f5b0756 | 1025 | #ifdef SUPPORT_LINKS |
a4a7e64c | 1026 | /* Avoid "vanished" error if symlink points nowhere. */ |
9439c0cb | 1027 | if (copy_links && x_lstat(thisname, &st, NULL) == 0 |
a4a7e64c WD |
1028 | && S_ISLNK(st.st_mode)) { |
1029 | io_error |= IOERR_GENERAL; | |
1030 | rprintf(FERROR, "symlink has no referent: %s\n", | |
1031 | full_fname(thisname)); | |
e5ce3bcf WD |
1032 | } else |
1033 | #endif | |
1034 | { | |
a4a7e64c WD |
1035 | enum logcode c = am_daemon && protocol_version < 28 |
1036 | ? FERROR : FINFO; | |
1037 | io_error |= IOERR_VANISHED; | |
1038 | rprintf(c, "file has vanished: %s\n", | |
1039 | full_fname(thisname)); | |
76e26e10 | 1040 | } |
a4a7e64c | 1041 | } else { |
a8726d2a | 1042 | io_error |= IOERR_GENERAL; |
d62bcc17 WD |
1043 | rsyserr(FERROR, save_errno, "readlink %s failed", |
1044 | full_fname(thisname)); | |
76e26e10 | 1045 | } |
3ec4dd97 AT |
1046 | return NULL; |
1047 | } | |
c627d613 | 1048 | |
7842418b WD |
1049 | /* backup.c calls us with filter_level set to NO_FILTERS. */ |
1050 | if (filter_level == NO_FILTERS) | |
1051 | goto skip_filters; | |
ac1a0994 | 1052 | |
7e037c42 | 1053 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
0ee6ca98 | 1054 | rprintf(FINFO, "skipping directory %s\n", thisname); |
3ec4dd97 AT |
1055 | return NULL; |
1056 | } | |
ebed4c3a | 1057 | |
d521e1c2 | 1058 | /* -x only affects directories because we need to avoid recursing |
e90cdb8a WD |
1059 | * into a mount-point directory, not to avoid copying a symlinked |
1060 | * file if -L (or similar) was specified. */ | |
535737bf WD |
1061 | if (one_file_system && st.st_dev != filesystem_dev |
1062 | && S_ISDIR(st.st_mode)) { | |
1063 | if (one_file_system > 1) { | |
1064 | if (verbose > 2) { | |
1065 | rprintf(FINFO, "skipping mount-point dir %s\n", | |
1066 | thisname); | |
ebec5eb6 | 1067 | } |
535737bf | 1068 | return NULL; |
ebec5eb6 | 1069 | } |
82ad07c4 | 1070 | flags |= FLAG_MOUNT_DIR; |
3b173846 | 1071 | } |
ebed4c3a | 1072 | |
53039410 WD |
1073 | if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) { |
1074 | if (ignore_perishable) | |
1075 | non_perishable_cnt++; | |
76e26e10 | 1076 | return NULL; |
53039410 | 1077 | } |
76e26e10 | 1078 | |
132fcf36 | 1079 | if (lp_ignore_nonreadable(module_id)) { |
4f5b0756 | 1080 | #ifdef SUPPORT_LINKS |
132fcf36 WD |
1081 | if (!S_ISLNK(st.st_mode)) |
1082 | #endif | |
1083 | if (access(thisname, R_OK) != 0) | |
1084 | return NULL; | |
1085 | } | |
ac1a0994 | 1086 | |
97b7bff4 | 1087 | skip_filters: |
ac1a0994 | 1088 | |
c7e6f84f WD |
1089 | /* Only divert a directory in the main transfer. */ |
1090 | if (flist && flist->prev && S_ISDIR(st.st_mode) | |
1091 | && flags & FLAG_DIVERT_DIRS) { | |
1092 | flist = dir_flist; | |
1093 | /* Room for parent/sibling/next-child info. */ | |
1094 | extra_len += 3 * EXTRA_LEN; | |
1095 | } | |
1096 | ||
ea847c62 WD |
1097 | if (verbose > 2) { |
1098 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", | |
0ee6ca98 | 1099 | who_am_i(), thisname, filter_level); |
ea847c62 | 1100 | } |
ebed4c3a | 1101 | |
a289addd | 1102 | if ((basename = strrchr(thisname, '/')) != NULL) { |
c7e6f84f WD |
1103 | int len = basename++ - thisname; |
1104 | if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) { | |
1105 | lastdir = new_array(char, len + 1); | |
1106 | memcpy(lastdir, thisname, len); | |
1107 | lastdir[len] = '\0'; | |
1108 | lastdir_len = len; | |
1109 | } | |
1110 | } else | |
a289addd | 1111 | basename = thisname; |
a289addd | 1112 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
c627d613 | 1113 | |
4f5b0756 | 1114 | #ifdef SUPPORT_LINKS |
a289addd WD |
1115 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; |
1116 | #else | |
1117 | linkname_len = 0; | |
1118 | #endif | |
1119 | ||
96293cf9 WD |
1120 | if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) |
1121 | extra_len += EXTRA_LEN; | |
1122 | ||
a3e18c76 WD |
1123 | #if EXTRA_ROUNDING > 0 |
1124 | if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN)) | |
1125 | extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN; | |
1126 | #endif | |
1127 | ||
c7e6f84f | 1128 | alloc_len = FILE_STRUCT_LEN + extra_len + basename_len |
96293cf9 | 1129 | + linkname_len; |
99aaa6ca WD |
1130 | if (flist) |
1131 | bp = pool_alloc(flist->file_pool, alloc_len, "make_file"); | |
1132 | else { | |
9935066b | 1133 | if (!(bp = new_array(char, alloc_len))) |
99aaa6ca | 1134 | out_of_memory("make_file"); |
9935066b S |
1135 | } |
1136 | ||
a3e18c76 | 1137 | memset(bp, 0, extra_len + FILE_STRUCT_LEN); |
82ad07c4 | 1138 | bp += extra_len; |
a289addd | 1139 | file = (struct file_struct *)bp; |
96293cf9 WD |
1140 | bp += FILE_STRUCT_LEN; |
1141 | ||
1142 | memcpy(bp, basename, basename_len); | |
1143 | bp += basename_len + linkname_len; /* skip space for symlink too */ | |
82ad07c4 WD |
1144 | |
1145 | #ifdef SUPPORT_HARD_LINKS | |
c7e6f84f | 1146 | if (preserve_hard_links && flist && flist->prev) { |
82ad07c4 WD |
1147 | if (protocol_version >= 28 |
1148 | ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1) | |
1149 | : S_ISREG(st.st_mode)) { | |
9d737ecb WD |
1150 | tmp_dev = st.st_dev; |
1151 | tmp_ino = st.st_ino; | |
82ad07c4 | 1152 | } else |
9d737ecb | 1153 | tmp_dev = 0; |
82ad07c4 WD |
1154 | } |
1155 | #endif | |
a289addd | 1156 | |
96293cf9 WD |
1157 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
1158 | if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) { | |
1159 | tmp_rdev = st.st_rdev; | |
1160 | st.st_size = 0; | |
1161 | } | |
1162 | #endif | |
1163 | ||
a289addd | 1164 | file->flags = flags; |
3ec4dd97 | 1165 | file->modtime = st.st_mtime; |
60af9465 | 1166 | file->len32 = (uint32)st.st_size; |
96293cf9 WD |
1167 | if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) { |
1168 | file->flags |= FLAG_LENGTH64; | |
1169 | OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32); | |
1170 | } | |
8bbe41b5 | 1171 | file->mode = st.st_mode; |
82ad07c4 | 1172 | if (preserve_uid) |
f1482c33 | 1173 | F_OWNER(file) = st.st_uid; |
82ad07c4 | 1174 | if (preserve_gid) |
f1482c33 | 1175 | F_GROUP(file) = st.st_gid; |
61dec11a | 1176 | |
c7e6f84f WD |
1177 | if (basename != thisname) |
1178 | file->dirname = lastdir; | |
a289addd | 1179 | |
4f5b0756 | 1180 | #ifdef SUPPORT_LINKS |
96293cf9 | 1181 | if (linkname_len) { |
c58c1dc4 | 1182 | bp = (char*)file->basename + basename_len; |
96293cf9 WD |
1183 | memcpy(bp, linkname, linkname_len); |
1184 | } | |
0d162bd1 WD |
1185 | #endif |
1186 | ||
82ad07c4 WD |
1187 | if (always_checksum && am_sender && S_ISREG(st.st_mode)) |
1188 | file_checksum(thisname, tmp_sum, st.st_size); | |
c627d613 | 1189 | |
ef35abb2 | 1190 | F_PATHNAME(file) = pathname; |
c627d613 | 1191 | |
23f4587f WD |
1192 | /* This code is only used by the receiver when it is building |
1193 | * a list of files for a delete pass. */ | |
1194 | if (keep_dirlinks && linkname_len && flist) { | |
1195 | STRUCT_STAT st2; | |
f5db0993 | 1196 | int save_mode = file->mode; |
8bbe41b5 | 1197 | file->mode = S_IFDIR; /* Find a directory with our name. */ |
c7e6f84f | 1198 | if (flist_find(dir_flist, file) >= 0 |
9439c0cb | 1199 | && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) { |
23f4587f | 1200 | file->modtime = st2.st_mtime; |
96293cf9 | 1201 | file->len32 = 0; |
23f4587f | 1202 | file->mode = st2.st_mode; |
82ad07c4 | 1203 | if (preserve_uid) |
f1482c33 | 1204 | F_OWNER(file) = st2.st_uid; |
82ad07c4 | 1205 | if (preserve_gid) |
f1482c33 | 1206 | F_GROUP(file) = st2.st_gid; |
f5db0993 WD |
1207 | } else |
1208 | file->mode = save_mode; | |
23f4587f WD |
1209 | } |
1210 | ||
82ad07c4 WD |
1211 | if (basename_len == 0+1) |
1212 | return NULL; | |
1213 | ||
3ea6e0e7 | 1214 | if (inc_recurse && flist == dir_flist) { |
76e754a0 | 1215 | flist_expand(dir_flist); |
a6e7b978 WD |
1216 | #ifdef ICONV_OPTION |
1217 | if (ic_ndx) | |
1218 | F_NDX(file) = dir_flist->count; | |
a6e7b978 WD |
1219 | if (need_unsorted_flist) |
1220 | dir_flist->sorted[dir_flist->count] = file; | |
76e754a0 | 1221 | #endif |
a6e7b978 | 1222 | dir_flist->files[dir_flist->count++] = file; |
c7e6f84f WD |
1223 | } |
1224 | ||
3ec4dd97 | 1225 | return file; |
c627d613 AT |
1226 | } |
1227 | ||
17026f27 | 1228 | /* Only called for temporary file_struct entries created by make_file(). */ |
82ad07c4 WD |
1229 | void unmake_file(struct file_struct *file) |
1230 | { | |
99a957d3 | 1231 | int extra_cnt = file_extra_cnt + LEN64_BUMP(file); |
a3e18c76 WD |
1232 | #if EXTRA_ROUNDING > 0 |
1233 | if (extra_cnt & EXTRA_ROUNDING) | |
1234 | extra_cnt = (extra_cnt | EXTRA_ROUNDING) + 1; | |
1235 | #endif | |
1236 | free(REQ_EXTRA(file, extra_cnt)); | |
82ad07c4 WD |
1237 | } |
1238 | ||
42c6b139 | 1239 | static struct file_struct *send_file_name(int f, struct file_list *flist, |
ede1f0eb | 1240 | char *fname, STRUCT_STAT *stp, |
c7e6f84f | 1241 | int flags, int filter_flags) |
c627d613 | 1242 | { |
ebed4c3a | 1243 | struct file_struct *file; |
16edf865 | 1244 | #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS |
1c3344a1 WD |
1245 | statx sx; |
1246 | #endif | |
ebed4c3a | 1247 | |
c7e6f84f | 1248 | file = make_file(fname, flist, stp, flags, filter_flags); |
37802f40 | 1249 | if (!file) |
301fb56c | 1250 | return NULL; |
ebed4c3a | 1251 | |
ebec5eb6 | 1252 | if (chmod_modes && !S_ISLNK(file->mode)) |
8bbe41b5 WD |
1253 | file->mode = tweak_mode(file->mode, chmod_modes); |
1254 | ||
1c3344a1 WD |
1255 | #ifdef SUPPORT_ACLS |
1256 | if (preserve_acls && !S_ISLNK(file->mode) && f >= 0) { | |
1257 | sx.st.st_mode = file->mode; | |
1258 | sx.acc_acl = sx.def_acl = NULL; | |
1259 | if (get_acl(fname, &sx) < 0) | |
1260 | return NULL; | |
1261 | } | |
1262 | #endif | |
16edf865 WD |
1263 | #ifdef SUPPORT_XATTRS |
1264 | if (preserve_xattrs && f >= 0) { | |
1265 | sx.xattr = NULL; | |
1266 | if (get_xattr(fname, &sx) < 0) | |
1267 | return NULL; | |
1268 | } | |
1269 | #endif | |
1c3344a1 | 1270 | |
53135fe8 | 1271 | maybe_emit_filelist_progress(flist->count + flist_count_offset); |
ebed4c3a | 1272 | |
d9d6bc52 | 1273 | flist_expand(flist); |
82ad07c4 | 1274 | flist->files[flist->count++] = file; |
1c3344a1 | 1275 | if (f >= 0) { |
42c6b139 | 1276 | send_file_entry(f, file, flist->count - 1); |
1c3344a1 WD |
1277 | #ifdef SUPPORT_ACLS |
1278 | if (preserve_acls && !S_ISLNK(file->mode)) { | |
1279 | send_acl(&sx, f); | |
1280 | free_acl(&sx); | |
1281 | } | |
16edf865 WD |
1282 | #endif |
1283 | #ifdef SUPPORT_XATTRS | |
1284 | if (preserve_xattrs) { | |
1285 | F_XATTR(file) = send_xattr(&sx, f); | |
1286 | free_xattr(&sx); | |
1287 | } | |
1c3344a1 WD |
1288 | #endif |
1289 | } | |
301fb56c WD |
1290 | return file; |
1291 | } | |
ebed4c3a | 1292 | |
301fb56c | 1293 | static void send_if_directory(int f, struct file_list *flist, |
56f0c976 | 1294 | struct file_struct *file, |
c7e6f84f WD |
1295 | char *fbuf, unsigned int ol, |
1296 | int flags) | |
301fb56c | 1297 | { |
56f0c976 | 1298 | char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/'); |
301fb56c WD |
1299 | |
1300 | if (S_ISDIR(file->mode) | |
82ad07c4 | 1301 | && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) { |
f1773e09 WD |
1302 | void *save_filters; |
1303 | unsigned int len = strlen(fbuf); | |
1304 | if (len > 1 && fbuf[len-1] == '/') | |
1305 | fbuf[--len] = '\0'; | |
1306 | if (len >= MAXPATHLEN - 1) { | |
1307 | io_error |= IOERR_GENERAL; | |
1308 | rprintf(FERROR, "skipping long-named directory: %s\n", | |
1309 | full_fname(fbuf)); | |
1310 | return; | |
1311 | } | |
1312 | save_filters = push_local_filters(fbuf, len); | |
a6e7b978 | 1313 | send_directory(f, flist, fbuf, len, flags); |
f1773e09 | 1314 | pop_local_filters(save_filters); |
56f0c976 WD |
1315 | fbuf[ol] = '\0'; |
1316 | if (is_dot_dir) | |
1317 | fbuf[ol-1] = '.'; | |
ebed4c3a | 1318 | } |
c627d613 AT |
1319 | } |
1320 | ||
c7e6f84f WD |
1321 | static int file_compare(struct file_struct **file1, struct file_struct **file2) |
1322 | { | |
1323 | return f_name_cmp(*file1, *file2); | |
1324 | } | |
1325 | ||
1326 | /* We take an entire set of sibling dirs from dir_flist (start <= ndx <= end), | |
1327 | * sort them by name, and link them into the tree, setting the appropriate | |
1328 | * parent/child/sibling pointers. */ | |
1329 | static void add_dirs_to_tree(int parent_ndx, int start, int end) | |
1330 | { | |
1331 | int i; | |
1332 | int32 *dp = NULL; | |
1333 | int32 *parent_dp = parent_ndx < 0 ? NULL | |
a6e7b978 | 1334 | : F_DIRNODE_P(dir_flist->sorted[parent_ndx]); |
c7e6f84f | 1335 | |
a6e7b978 WD |
1336 | qsort(dir_flist->sorted + start, end - start + 1, |
1337 | sizeof dir_flist->sorted[0], (int (*)())file_compare); | |
c7e6f84f WD |
1338 | |
1339 | for (i = start; i <= end; i++) { | |
a6e7b978 | 1340 | struct file_struct *file = dir_flist->sorted[i]; |
c7e6f84f WD |
1341 | if (!(file->flags & FLAG_XFER_DIR) |
1342 | || file->flags & FLAG_MOUNT_DIR) | |
1343 | continue; | |
1344 | if (dp) | |
1345 | DIR_NEXT_SIBLING(dp) = i; | |
1346 | else if (parent_dp) | |
1347 | DIR_FIRST_CHILD(parent_dp) = i; | |
1348 | else | |
1349 | send_dir_ndx = i; | |
1350 | dp = F_DIRNODE_P(file); | |
1351 | DIR_PARENT(dp) = parent_ndx; | |
1352 | DIR_FIRST_CHILD(dp) = -1; | |
1353 | } | |
1354 | if (dp) | |
1355 | DIR_NEXT_SIBLING(dp) = -1; | |
1356 | } | |
1357 | ||
301fb56c | 1358 | /* This function is normally called by the sender, but the receiving side also |
7b558d7f | 1359 | * calls it from get_dirlist() with f set to -1 so that we just construct the |
301fb56c WD |
1360 | * file list in memory without sending it over the wire. Also, get_dirlist() |
1361 | * might call this with f set to -2, which also indicates that local filter | |
1362 | * rules should be ignored. */ | |
a6e7b978 WD |
1363 | static void send_directory(int f, struct file_list *flist, char *fbuf, int len, |
1364 | int flags) | |
c627d613 | 1365 | { |
3ec4dd97 | 1366 | struct dirent *di; |
32cbfe7b | 1367 | unsigned remainder; |
3ec4dd97 | 1368 | char *p; |
f1773e09 | 1369 | DIR *d; |
c7e6f84f | 1370 | int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0; |
a6e7b978 | 1371 | int start = flist->count; |
c7e6f84f | 1372 | int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS; |
c7e6f84f WD |
1373 | |
1374 | assert(flist != NULL); | |
3ec4dd97 | 1375 | |
f1773e09 | 1376 | if (!(d = opendir(fbuf))) { |
06c28400 | 1377 | io_error |= IOERR_GENERAL; |
f1773e09 | 1378 | rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf)); |
3ec4dd97 AT |
1379 | return; |
1380 | } | |
c627d613 | 1381 | |
19b2a5d9 WD |
1382 | p = fbuf + len; |
1383 | if (len != 1 || *fbuf != '/') | |
eddd5d12 | 1384 | *p++ = '/'; |
f1773e09 | 1385 | *p = '\0'; |
32cbfe7b | 1386 | remainder = MAXPATHLEN - (p - fbuf); |
ebed4c3a | 1387 | |
6a7cc46c | 1388 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 1389 | char *dname = d_name(di); |
6a7cc46c S |
1390 | if (dname[0] == '.' && (dname[1] == '\0' |
1391 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 1392 | continue; |
0ee6ca98 | 1393 | if (strlcpy(p, dname, remainder) >= remainder) { |
eddd5d12 WD |
1394 | io_error |= IOERR_GENERAL; |
1395 | rprintf(FINFO, | |
1396 | "cannot send long-named file %s\n", | |
f1773e09 | 1397 | full_fname(fbuf)); |
beaf4954 | 1398 | continue; |
eddd5d12 | 1399 | } |
0ee6ca98 | 1400 | |
42c6b139 | 1401 | send_file_name(f, flist, fbuf, NULL, flags, filter_flags); |
3ec4dd97 | 1402 | } |
32cbfe7b WD |
1403 | |
1404 | fbuf[len] = '\0'; | |
1405 | ||
6a7cc46c | 1406 | if (errno) { |
06c28400 | 1407 | io_error |= IOERR_GENERAL; |
71903f60 | 1408 | rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf)); |
6a7cc46c | 1409 | } |
c627d613 | 1410 | |
3ec4dd97 | 1411 | closedir(d); |
301fb56c | 1412 | |
a6e7b978 | 1413 | if (f >= 0 && recurse && !divert_dirs) { |
301fb56c | 1414 | int i, end = flist->count - 1; |
8cae71eb | 1415 | /* send_if_directory() bumps flist->count, so use "end". */ |
301fb56c | 1416 | for (i = start; i <= end; i++) |
c7e6f84f WD |
1417 | send_if_directory(f, flist, flist->files[i], fbuf, len, flags); |
1418 | } | |
1419 | } | |
1420 | ||
a6e7b978 | 1421 | static void send1extra(int f, struct file_struct *file, struct file_list *flist) |
c7e6f84f WD |
1422 | { |
1423 | char fbuf[MAXPATHLEN]; | |
a6e7b978 WD |
1424 | int dlen; |
1425 | ||
1426 | f_name(file, fbuf); | |
1427 | dlen = strlen(fbuf); | |
1428 | ||
1429 | if (F_PATHNAME(file) != pathname) { | |
1430 | if (!push_pathname(F_PATHNAME(file), -1)) | |
1431 | exit_cleanup(RERR_FILESELECT); | |
1432 | } | |
1433 | ||
1434 | change_local_filter_dir(fbuf, dlen, send_dir_depth); | |
1435 | ||
1436 | send_directory(f, flist, fbuf, dlen, FLAG_DIVERT_DIRS | FLAG_XFER_DIR); | |
1437 | } | |
1438 | ||
1439 | void send_extra_file_list(int f, int at_least) | |
1440 | { | |
c7e6f84f WD |
1441 | struct file_list *flist; |
1442 | int64 start_write; | |
7a2fa0c2 | 1443 | int future_cnt, save_io_error = io_error; |
c7e6f84f | 1444 | |
25c2a6ac | 1445 | if (flist_eof) |
c7e6f84f WD |
1446 | return; |
1447 | ||
1448 | /* Keep sending data until we have the requested number of | |
1449 | * files in the upcoming file-lists. */ | |
7a2fa0c2 WD |
1450 | if (cur_flist->next) { |
1451 | flist = first_flist->prev; /* the newest flist */ | |
ef35abb2 WD |
1452 | future_cnt = flist->count + flist->ndx_start |
1453 | - cur_flist->next->ndx_start; | |
7a2fa0c2 WD |
1454 | } else |
1455 | future_cnt = 0; | |
1456 | while (future_cnt < at_least) { | |
a6e7b978 WD |
1457 | struct file_struct *file = dir_flist->sorted[send_dir_ndx]; |
1458 | int start = dir_flist->count; | |
c7e6f84f | 1459 | int32 *dp; |
c7e6f84f WD |
1460 | |
1461 | flist = flist_new(0, "send_extra_file_list"); | |
4c9d5fef | 1462 | start_write = stats.total_written; |
c7e6f84f | 1463 | |
a6e7b978 WD |
1464 | /* If this is the first of a set of duplicate dirs, we must |
1465 | * send all the dirs together in a single file-list. We must | |
1466 | * also send the index of the last dir in the header. */ | |
1467 | if (file->flags & FLAG_DUPLICATE) { | |
1468 | int dir_ndx, end_ndx = send_dir_ndx; | |
1469 | struct file_struct *fp = file; | |
1470 | ||
1471 | while (1) { | |
1472 | dp = F_DIRNODE_P(fp); | |
1473 | end_ndx = DIR_NEXT_SIBLING(dp); | |
1474 | fp = dir_flist->sorted[end_ndx]; | |
1475 | if (!(fp->flags & FLAG_DUPLICATE)) | |
1476 | break; | |
1477 | } | |
c7e6f84f | 1478 | |
332cf6df | 1479 | #ifdef ICONV_OPTION |
a6e7b978 WD |
1480 | if (ic_ndx) |
1481 | dir_ndx = F_NDX(fp); | |
1482 | else | |
1483 | #endif | |
1484 | dir_ndx = end_ndx; | |
1485 | write_ndx(f, NDX_FLIST_OFFSET - dir_ndx); | |
1486 | ||
1487 | while (1) { | |
1488 | send1extra(f, file, flist); | |
1489 | if (send_dir_ndx == end_ndx) | |
1490 | break; | |
1491 | dp = F_DIRNODE_P(file); | |
1492 | send_dir_ndx = DIR_NEXT_SIBLING(dp); | |
1493 | file = dir_flist->sorted[send_dir_ndx]; | |
1494 | } | |
1495 | } else { | |
1496 | int dir_ndx; | |
1497 | #ifdef ICONV_OPTION | |
1498 | if (ic_ndx) | |
1499 | dir_ndx = F_NDX(file); | |
1500 | else | |
1501 | #endif | |
1502 | dir_ndx = send_dir_ndx; | |
1503 | write_ndx(f, NDX_FLIST_OFFSET - dir_ndx); | |
1504 | ||
1505 | send1extra(f, file, flist); | |
1506 | } | |
1507 | write_byte(f, 0); | |
1508 | ||
76e754a0 | 1509 | #ifdef ICONV_OPTION |
332cf6df | 1510 | if (!need_unsorted_flist) |
76e754a0 | 1511 | #endif |
a6e7b978 WD |
1512 | dir_flist->sorted = dir_flist->files; |
1513 | add_dirs_to_tree(send_dir_ndx, start, dir_flist->count - 1); | |
1514 | ||
1515 | #ifdef ICONV_OPTION | |
1516 | if (need_unsorted_flist) { | |
1517 | if (inc_recurse) { | |
1518 | if (!(flist->sorted = new_array(struct file_struct *, flist->count))) | |
1519 | out_of_memory("send_extra_file_list"); | |
1520 | memcpy(flist->sorted, flist->files, | |
1521 | flist->count * sizeof (struct file_struct*)); | |
1522 | clean_flist(flist, 0); | |
1523 | } else | |
1524 | flist->sorted = flist->files; | |
1525 | } else | |
332cf6df WD |
1526 | #endif |
1527 | { | |
1528 | flist->sorted = flist->files; | |
a6e7b978 | 1529 | clean_flist(flist, 0); |
332cf6df | 1530 | } |
a6e7b978 | 1531 | |
c7e6f84f | 1532 | file_total += flist->count; |
7a2fa0c2 | 1533 | future_cnt += flist->count; |
c7e6f84f WD |
1534 | stats.flist_size += stats.total_written - start_write; |
1535 | stats.num_files += flist->count; | |
1536 | if (verbose > 3) | |
1537 | output_flist(flist); | |
1538 | ||
1539 | dp = F_DIRNODE_P(file); | |
1540 | if (DIR_FIRST_CHILD(dp) >= 0) { | |
1541 | send_dir_ndx = DIR_FIRST_CHILD(dp); | |
1542 | send_dir_depth++; | |
1543 | } else { | |
1544 | while (DIR_NEXT_SIBLING(dp) < 0) { | |
1545 | if ((send_dir_ndx = DIR_PARENT(dp)) < 0) { | |
9ae7a2cd | 1546 | write_ndx(f, NDX_FLIST_EOF); |
c7e6f84f WD |
1547 | flist_eof = 1; |
1548 | change_local_filter_dir(NULL, 0, 0); | |
1549 | goto finish; | |
1550 | } | |
1551 | send_dir_depth--; | |
a6e7b978 | 1552 | file = dir_flist->sorted[send_dir_ndx]; |
c7e6f84f WD |
1553 | dp = F_DIRNODE_P(file); |
1554 | } | |
1555 | send_dir_ndx = DIR_NEXT_SIBLING(dp); | |
1556 | } | |
301fb56c | 1557 | } |
c7e6f84f WD |
1558 | |
1559 | finish: | |
1560 | if (io_error != save_io_error && !ignore_errors) | |
1561 | send_msg_int(MSG_IO_ERROR, io_error); | |
c627d613 AT |
1562 | } |
1563 | ||
ebed4c3a | 1564 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 1565 | { |
3bb88b43 WD |
1566 | static const char *lastdir; |
1567 | static int lastdir_len = -1; | |
1568 | int len, dirlen; | |
bcacc18b | 1569 | STRUCT_STAT st; |
c7e6f84f | 1570 | char *p, *dir; |
ebed4c3a | 1571 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 1572 | struct file_list *flist; |
31b4d25d | 1573 | struct timeval start_tv, end_tv; |
a800434a | 1574 | int64 start_write; |
24d0fcde | 1575 | int use_ff_fd = 0; |
c7e6f84f | 1576 | int flags, disable_buffering; |
649d65ed | 1577 | |
ea124cb3 | 1578 | rprintf(FLOG, "building file list\n"); |
134f4338 | 1579 | if (show_filelist_p()) |
1bbd10fe | 1580 | start_filelist_progress("building file list"); |
3ea6e0e7 | 1581 | else if (inc_recurse && verbose && !am_server) |
98b1689d | 1582 | rprintf(FCLIENT, "sending incremental file list\n"); |
c627d613 | 1583 | |
a800434a | 1584 | start_write = stats.total_written; |
31b4d25d | 1585 | gettimeofday(&start_tv, NULL); |
a800434a | 1586 | |
8cae71eb | 1587 | #ifdef SUPPORT_HARD_LINKS |
c7e6f84f | 1588 | if (preserve_hard_links && protocol_version >= 30 && !cur_flist) |
8cae71eb WD |
1589 | init_hard_links(); |
1590 | #endif | |
1591 | ||
c7e6f84f | 1592 | flist = cur_flist = flist_new(0, "send_file_list"); |
3ea6e0e7 | 1593 | if (inc_recurse) { |
c7e6f84f WD |
1594 | dir_flist = flist_new(FLIST_TEMP, "send_file_list"); |
1595 | flags = FLAG_DIVERT_DIRS; | |
1596 | } else { | |
1597 | dir_flist = cur_flist; | |
1598 | flags = 0; | |
1599 | } | |
c627d613 | 1600 | |
c7e6f84f | 1601 | disable_buffering = io_start_buffering_out(f); |
134f4338 | 1602 | if (filesfrom_fd >= 0) { |
6f3684ff | 1603 | if (argv[0] && !push_dir(argv[0], 0)) { |
63d83e94 WD |
1604 | rsyserr(FERROR, errno, "push_dir %s failed in %s", |
1605 | full_fname(argv[0]), curr_dir); | |
134f4338 | 1606 | exit_cleanup(RERR_FILESELECT); |
24d0fcde | 1607 | } |
134f4338 | 1608 | use_ff_fd = 1; |
d6dead6b AT |
1609 | } |
1610 | ||
24d0fcde | 1611 | while (1) { |
56f0c976 WD |
1612 | char fbuf[MAXPATHLEN]; |
1613 | char *fn; | |
301fb56c | 1614 | int is_dot_dir; |
c627d613 | 1615 | |
24d0fcde | 1616 | if (use_ff_fd) { |
56f0c976 | 1617 | if (read_filesfrom_line(filesfrom_fd, fbuf) == 0) |
24d0fcde | 1618 | break; |
a2248aea | 1619 | sanitize_path(fbuf, fbuf, "", 0, NULL); |
24d0fcde WD |
1620 | } else { |
1621 | if (argc-- == 0) | |
1622 | break; | |
56f0c976 | 1623 | strlcpy(fbuf, *argv++, MAXPATHLEN); |
24d0fcde | 1624 | if (sanitize_paths) |
a2248aea | 1625 | sanitize_path(fbuf, fbuf, "", 0, NULL); |
24d0fcde | 1626 | } |
c627d613 | 1627 | |
56f0c976 | 1628 | len = strlen(fbuf); |
1902a765 WD |
1629 | if (relative_paths) { |
1630 | /* We clean up fbuf below. */ | |
1631 | is_dot_dir = 0; | |
1632 | } else if (!len || fbuf[len - 1] == '/') { | |
56f0c976 | 1633 | if (len == 2 && fbuf[0] == '.') { |
6931c138 | 1634 | /* Turn "./" into just "." rather than "./." */ |
56f0c976 | 1635 | fbuf[1] = '\0'; |
557a35f5 | 1636 | } else { |
56f0c976 | 1637 | if (len + 1 >= MAXPATHLEN) |
a1f99493 | 1638 | overflow_exit("send_file_list"); |
56f0c976 WD |
1639 | fbuf[len++] = '.'; |
1640 | fbuf[len] = '\0'; | |
53f821f1 | 1641 | } |
301fb56c | 1642 | is_dot_dir = 1; |
56f0c976 WD |
1643 | } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.' |
1644 | && (len == 2 || fbuf[len-3] == '/')) { | |
1645 | if (len + 2 >= MAXPATHLEN) | |
a1f99493 | 1646 | overflow_exit("send_file_list"); |
56f0c976 WD |
1647 | fbuf[len++] = '/'; |
1648 | fbuf[len++] = '.'; | |
1649 | fbuf[len] = '\0'; | |
a289f89f | 1650 | is_dot_dir = 1; |
301fb56c | 1651 | } else { |
56f0c976 WD |
1652 | is_dot_dir = fbuf[len-1] == '.' |
1653 | && (len == 1 || fbuf[len-2] == '/'); | |
649d65ed | 1654 | } |
c627d613 | 1655 | |
649d65ed AT |
1656 | dir = NULL; |
1657 | ||
1658 | if (!relative_paths) { | |
56f0c976 | 1659 | p = strrchr(fbuf, '/'); |
649d65ed | 1660 | if (p) { |
151f59f1 | 1661 | *p = '\0'; |
56f0c976 | 1662 | if (p == fbuf) |
649d65ed AT |
1663 | dir = "/"; |
1664 | else | |
56f0c976 WD |
1665 | dir = fbuf; |
1666 | len -= p - fbuf + 1; | |
1667 | fn = p + 1; | |
1668 | } else | |
1669 | fn = fbuf; | |
1902a765 WD |
1670 | } else { |
1671 | if ((p = strstr(fbuf, "/./")) != NULL) { | |
1672 | *p = '\0'; | |
1673 | if (p == fbuf) | |
1674 | dir = "/"; | |
1675 | else | |
1676 | dir = fbuf; | |
1677 | len -= p - fbuf + 3; | |
1678 | fn = p + 3; | |
1679 | } else | |
1680 | fn = fbuf; | |
1681 | /* Get rid of trailing "/" and "/.". */ | |
1682 | while (len) { | |
ede1f0eb WD |
1683 | if (fn[len - 1] == '/') { |
1684 | is_dot_dir = 1; | |
1685 | if (!--len && !dir) { | |
1686 | len++; | |
1687 | break; | |
1688 | } | |
1689 | } | |
1902a765 WD |
1690 | else if (len >= 2 && fn[len - 1] == '.' |
1691 | && fn[len - 2] == '/') { | |
ede1f0eb | 1692 | is_dot_dir = 1; |
1902a765 WD |
1693 | if (!(len -= 2) && !dir) { |
1694 | len++; | |
1695 | break; | |
1696 | } | |
1697 | } else | |
1698 | break; | |
1699 | } | |
8c449e62 WD |
1700 | if (len == 1 && fn[0] == '/') |
1701 | fn[len++] = '.'; | |
1902a765 WD |
1702 | fn[len] = '\0'; |
1703 | /* Reject a ".." dir in the active part of the path. */ | |
ede1f0eb WD |
1704 | for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) { |
1705 | if ((p[2] == '/' || p[2] == '\0') | |
1706 | && (p == fn || p[-1] == '/')) { | |
1707 | rprintf(FERROR, | |
1708 | "found \"..\" dir in relative path: %s\n", | |
1709 | fbuf); | |
1710 | exit_cleanup(RERR_SYNTAX); | |
1711 | } | |
1902a765 WD |
1712 | } |
1713 | } | |
d2ea5980 | 1714 | |
56f0c976 WD |
1715 | if (!*fn) { |
1716 | len = 1; | |
1717 | fn = "."; | |
1718 | } | |
d2ea5980 | 1719 | |
3bb88b43 WD |
1720 | dirlen = dir ? strlen(dir) : 0; |
1721 | if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) { | |
ef35abb2 | 1722 | if (!push_pathname(dir ? strdup(dir) : NULL, dirlen)) |
3bb88b43 | 1723 | goto push_error; |
ef35abb2 WD |
1724 | lastdir = pathname; |
1725 | lastdir_len = pathname_len; | |
1726 | } else if (!push_pathname(lastdir, lastdir_len)) { | |
3bb88b43 WD |
1727 | push_error: |
1728 | io_error |= IOERR_GENERAL; | |
1729 | rsyserr(FERROR, errno, "push_dir %s failed in %s", | |
1730 | full_fname(dir), curr_dir); | |
1731 | continue; | |
d2ea5980 WD |
1732 | } |
1733 | ||
99eba675 | 1734 | if (fn != fbuf) |
56f0c976 WD |
1735 | memmove(fbuf, fn, len + 1); |
1736 | ||
25c2a6ac WD |
1737 | if (link_stat(fbuf, &st, copy_dirlinks) != 0) { |
1738 | io_error |= IOERR_GENERAL; | |
1739 | rsyserr(FERROR, errno, "link_stat %s failed", | |
1740 | full_fname(fbuf)); | |
1741 | continue; | |
1742 | } | |
1743 | ||
1744 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { | |
1745 | rprintf(FINFO, "skipping directory %s\n", fbuf); | |
1746 | continue; | |
1747 | } | |
1748 | ||
56f0c976 | 1749 | if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) { |
151f59f1 WD |
1750 | /* Send the implied directories at the start of the |
1751 | * source spec, so we get their permissions right. */ | |
56f0c976 | 1752 | char *lp = lastpath, *slash = fbuf; |
151f59f1 | 1753 | *p = '\0'; |
2154309a WD |
1754 | /* Skip any initial directories in our path that we |
1755 | * have in common with lastpath. */ | |
56f0c976 | 1756 | for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) { |
2154309a WD |
1757 | if (*fn == '/') |
1758 | slash = fn; | |
2154309a WD |
1759 | } |
1760 | *p = '/'; | |
1761 | if (fn != p || (*lp && *lp != '/')) { | |
0f57446d | 1762 | int save_copy_links = copy_links; |
7e037c42 | 1763 | int save_xfer_dirs = xfer_dirs; |
3ea6e0e7 | 1764 | int dir_flags = inc_recurse ? FLAG_DIVERT_DIRS : 0; |
88c2190a | 1765 | copy_links |= copy_unsafe_links; |
7e037c42 | 1766 | xfer_dirs = 1; |
2154309a | 1767 | while ((slash = strchr(slash+1, '/')) != 0) { |
151f59f1 | 1768 | *slash = '\0'; |
42c6b139 WD |
1769 | send_file_name(f, flist, fbuf, NULL, |
1770 | dir_flags, ALL_FILTERS); | |
2154309a | 1771 | *slash = '/'; |
649d65ed | 1772 | } |
0f57446d | 1773 | copy_links = save_copy_links; |
7e037c42 | 1774 | xfer_dirs = save_xfer_dirs; |
151f59f1 | 1775 | *p = '\0'; |
56f0c976 | 1776 | strlcpy(lastpath, fbuf, sizeof lastpath); |
649d65ed AT |
1777 | *p = '/'; |
1778 | } | |
1779 | } | |
ebed4c3a | 1780 | |
535737bf WD |
1781 | if (one_file_system) |
1782 | filesystem_dev = st.st_dev; | |
1783 | ||
75c51953 | 1784 | if (recurse || (xfer_dirs && is_dot_dir)) { |
42c6b139 | 1785 | struct file_struct *file; |
ef35abb2 | 1786 | int top_flags = FLAG_TOP_DIR | FLAG_XFER_DIR | flags; |
42c6b139 WD |
1787 | file = send_file_name(f, flist, fbuf, &st, |
1788 | top_flags, ALL_FILTERS); | |
3ea6e0e7 | 1789 | if (file && !inc_recurse) |
42c6b139 WD |
1790 | send_if_directory(f, flist, file, fbuf, len, flags); |
1791 | } else | |
1792 | send_file_name(f, flist, fbuf, &st, flags, ALL_FILTERS); | |
649d65ed | 1793 | } |
dc5ddbcc | 1794 | |
134f4338 WD |
1795 | gettimeofday(&end_tv, NULL); |
1796 | stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1797 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
1798 | if (stats.flist_buildtime == 0) | |
1799 | stats.flist_buildtime = 1; | |
1800 | start_tv = end_tv; | |
31b4d25d | 1801 | |
8cae71eb WD |
1802 | write_byte(f, 0); /* Indicate end of file list */ |
1803 | ||
1804 | #ifdef SUPPORT_HARD_LINKS | |
3ea6e0e7 | 1805 | if (preserve_hard_links && protocol_version >= 30 && !inc_recurse) |
8cae71eb WD |
1806 | idev_destroy(); |
1807 | #endif | |
c627d613 | 1808 | |
134f4338 WD |
1809 | if (show_filelist_p()) |
1810 | finish_filelist_progress(flist); | |
31b4d25d | 1811 | |
134f4338 WD |
1812 | gettimeofday(&end_tv, NULL); |
1813 | stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1814 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
ebed4c3a | 1815 | |
332cf6df WD |
1816 | /* When converting names, both sides keep an unsorted file-list array |
1817 | * because the names will differ on the sending and receiving sides | |
1818 | * (both sides will use the unsorted index number for each item). */ | |
1819 | ||
a6e7b978 WD |
1820 | /* Sort the list without removing any duplicates. This allows the |
1821 | * receiving side to ask for whatever name it kept. For incremental | |
1822 | * recursion mode, the sender marks duplicate dirs so that it can | |
1823 | * send them together in a single file-list. */ | |
332cf6df WD |
1824 | #ifdef ICONV_OPTION |
1825 | if (need_unsorted_flist) { | |
1826 | if (inc_recurse) { | |
1827 | if (!(flist->sorted = new_array(struct file_struct *, flist->count))) | |
a6e7b978 | 1828 | out_of_memory("send_file_list"); |
332cf6df WD |
1829 | memcpy(flist->sorted, flist->files, |
1830 | flist->count * sizeof (struct file_struct*)); | |
a6e7b978 WD |
1831 | clean_flist(flist, 0); |
1832 | } else | |
1833 | flist->sorted = flist->files; | |
332cf6df WD |
1834 | } else |
1835 | #endif | |
1836 | { | |
1837 | flist->sorted = flist->files; | |
a6e7b978 | 1838 | clean_flist(flist, 0); |
332cf6df | 1839 | } |
c7e6f84f | 1840 | file_total += flist->count; |
ebed4c3a | 1841 | |
3ea6e0e7 | 1842 | if (!numeric_ids && !inc_recurse) |
1564cd5a | 1843 | send_id_list(f); |
f6c34742 | 1844 | |
134f4338 | 1845 | /* send the io_error flag */ |
c7e6f84f WD |
1846 | if (protocol_version < 30) |
1847 | write_int(f, ignore_errors ? 0 : io_error); | |
1848 | else if (io_error && !ignore_errors) | |
1849 | send_msg_int(MSG_IO_ERROR, io_error); | |
1850 | ||
1851 | if (disable_buffering) | |
1852 | io_end_buffering_out(); | |
6ba9279f | 1853 | |
134f4338 WD |
1854 | stats.flist_size = stats.total_written - start_write; |
1855 | stats.num_files = flist->count; | |
d6dead6b | 1856 | |
cefed3e8 | 1857 | if (verbose > 3) |
32cbfe7b | 1858 | output_flist(flist); |
cefed3e8 | 1859 | |
17faa41c | 1860 | if (verbose > 2) |
ebed4c3a | 1861 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1862 | |
3ea6e0e7 | 1863 | if (inc_recurse) { |
76e754a0 | 1864 | #ifdef ICONV_OPTION |
a6e7b978 | 1865 | if (!need_unsorted_flist) |
76e754a0 | 1866 | #endif |
a6e7b978 | 1867 | dir_flist->sorted = dir_flist->files; |
c7e6f84f | 1868 | add_dirs_to_tree(-1, 0, dir_flist->count - 1); |
25c2a6ac WD |
1869 | if (send_dir_ndx < 0) { |
1870 | write_ndx(f, NDX_FLIST_EOF); | |
1871 | flist_eof = 1; | |
1872 | } | |
1873 | else if (file_total == 1) { | |
c7e6f84f WD |
1874 | /* If we're creating incremental file-lists and there |
1875 | * was just 1 item in the first file-list, send 1 more | |
1876 | * file-list to check if this is a 1-file xfer. */ | |
25c2a6ac | 1877 | send_extra_file_list(f, 1); |
c7e6f84f WD |
1878 | } |
1879 | } | |
1880 | ||
649d65ed | 1881 | return flist; |
c627d613 AT |
1882 | } |
1883 | ||
c627d613 AT |
1884 | struct file_list *recv_file_list(int f) |
1885 | { | |
ebed4c3a | 1886 | struct file_list *flist; |
c7e6f84f | 1887 | int dstart, flags; |
ebed4c3a | 1888 | int64 start_read; |
c627d613 | 1889 | |
98b1689d | 1890 | if (!first_flist) |
c7e6f84f | 1891 | rprintf(FLOG, "receiving file list\n"); |
1bbd10fe DD |
1892 | if (show_filelist_p()) |
1893 | start_filelist_progress("receiving file list"); | |
3ea6e0e7 | 1894 | else if (inc_recurse && verbose && !am_server && !first_flist) |
98b1689d | 1895 | rprintf(FCLIENT, "receiving incremental file list\n"); |
c627d613 | 1896 | |
ebed4c3a | 1897 | start_read = stats.total_read; |
a800434a | 1898 | |
c7e6f84f | 1899 | flist = flist_new(0, "recv_file_list"); |
c627d613 | 1900 | |
8cae71eb WD |
1901 | #ifdef SUPPORT_HARD_LINKS |
1902 | if (preserve_hard_links && protocol_version < 30) | |
1903 | init_hard_links(); | |
1904 | #endif | |
c627d613 | 1905 | |
3ea6e0e7 | 1906 | if (inc_recurse) { |
c7e6f84f WD |
1907 | if (flist->ndx_start == 0) |
1908 | dir_flist = flist_new(FLIST_TEMP, "recv_file_list"); | |
1909 | dstart = dir_flist->count; | |
1910 | } else { | |
1911 | dir_flist = flist; | |
1912 | dstart = 0; | |
1913 | } | |
1914 | ||
1ef00d20 | 1915 | while ((flags = read_byte(f)) != 0) { |
f5db0993 | 1916 | struct file_struct *file; |
dbda5fbf | 1917 | |
d9d6bc52 | 1918 | flist_expand(flist); |
c627d613 | 1919 | |
d01d15e0 | 1920 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1921 | flags |= read_byte(f) << 8; |
82ad07c4 | 1922 | file = recv_file_entry(flist, flags, f); |
c627d613 | 1923 | |
3ea6e0e7 | 1924 | if (inc_recurse && S_ISDIR(file->mode)) { |
c7e6f84f | 1925 | flist_expand(dir_flist); |
76e754a0 | 1926 | #ifdef ICONV_OPTION |
a6e7b978 WD |
1927 | if (need_unsorted_flist) |
1928 | dir_flist->sorted[dir_flist->count] = file; | |
76e754a0 | 1929 | #endif |
c7e6f84f WD |
1930 | dir_flist->files[dir_flist->count++] = file; |
1931 | } | |
1932 | ||
f5db0993 | 1933 | flist->files[flist->count++] = file; |
c627d613 | 1934 | |
53135fe8 | 1935 | maybe_emit_filelist_progress(flist->count); |
1bbd10fe | 1936 | |
5e4ff5f9 WD |
1937 | if (verbose > 2) { |
1938 | rprintf(FINFO, "recv_file_name(%s)\n", | |
1939 | f_name(file, NULL)); | |
1940 | } | |
ebed4c3a | 1941 | } |
c7e6f84f | 1942 | file_total += flist->count; |
c627d613 | 1943 | |
ebed4c3a MP |
1944 | if (verbose > 2) |
1945 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1946 | |
b7736c79 | 1947 | if (show_filelist_p()) |
1bbd10fe | 1948 | finish_filelist_progress(flist); |
a06d19e3 | 1949 | |
332cf6df WD |
1950 | #ifdef ICONV_OPTION |
1951 | if (need_unsorted_flist) { | |
1952 | /* Create an extra array of index pointers that we can sort for | |
1953 | * the generator's use (for wading through the files in sorted | |
1954 | * order and for calling flist_find()). We keep the "files" | |
1955 | * list unsorted for our exchange of index numbers with the | |
1956 | * other side (since their names may not sort the same). */ | |
1957 | if (!(flist->sorted = new_array(struct file_struct *, flist->count))) | |
1958 | out_of_memory("recv_file_list"); | |
1959 | memcpy(flist->sorted, flist->files, | |
1960 | flist->count * sizeof (struct file_struct*)); | |
a6e7b978 WD |
1961 | if (inc_recurse) { |
1962 | qsort(dir_flist->sorted + dstart, dir_flist->count - dstart, | |
1963 | sizeof (struct file_struct*), (int (*)())file_compare); | |
1964 | } | |
332cf6df WD |
1965 | } else |
1966 | #endif | |
a6e7b978 | 1967 | { |
332cf6df | 1968 | flist->sorted = flist->files; |
a6e7b978 WD |
1969 | if (inc_recurse) { |
1970 | dir_flist->sorted = dir_flist->files; | |
1971 | qsort(dir_flist->sorted + dstart, dir_flist->count - dstart, | |
1972 | sizeof (struct file_struct*), (int (*)())file_compare); | |
1973 | } | |
1974 | } | |
332cf6df | 1975 | |
a6e7b978 | 1976 | if (!inc_recurse && f >= 0) |
1564cd5a | 1977 | recv_id_list(f, flist); |
f6c34742 | 1978 | |
a6e7b978 WD |
1979 | clean_flist(flist, relative_paths); |
1980 | ||
c7e6f84f | 1981 | if (protocol_version < 30) { |
b9f592fb | 1982 | /* Recv the io_error flag */ |
1f56188f | 1983 | if (ignore_errors) |
b9f592fb WD |
1984 | read_int(f); |
1985 | else | |
1986 | io_error |= read_int(f); | |
ebed4c3a | 1987 | } |
6ba9279f | 1988 | |
cefed3e8 | 1989 | if (verbose > 3) |
32cbfe7b | 1990 | output_flist(flist); |
cefed3e8 | 1991 | |
ebed4c3a MP |
1992 | if (list_only) { |
1993 | int i; | |
b7736c79 | 1994 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1995 | list_file_entry(flist->files[i]); |
ebed4c3a | 1996 | } |
f7632fc6 | 1997 | |
ebed4c3a MP |
1998 | if (verbose > 2) |
1999 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 2000 | |
c7e6f84f WD |
2001 | stats.flist_size += stats.total_read - start_read; |
2002 | stats.num_files += flist->count; | |
a800434a | 2003 | |
ebed4c3a | 2004 | return flist; |
c627d613 AT |
2005 | } |
2006 | ||
c7e6f84f WD |
2007 | /* This is only used once by the receiver if the very first file-list |
2008 | * has exactly one item in it. */ | |
2009 | void recv_additional_file_list(int f) | |
c627d613 | 2010 | { |
c7e6f84f | 2011 | struct file_list *flist; |
9ae7a2cd | 2012 | int ndx = read_ndx(f); |
25c2a6ac | 2013 | if (ndx == NDX_FLIST_EOF) { |
c7e6f84f WD |
2014 | flist_eof = 1; |
2015 | change_local_filter_dir(NULL, 0, 0); | |
2016 | } else { | |
2017 | ndx = NDX_FLIST_OFFSET - ndx; | |
2018 | if (ndx < 0 || ndx >= dir_flist->count) { | |
2019 | ndx = NDX_FLIST_OFFSET - ndx; | |
2020 | rprintf(FERROR, | |
2021 | "Invalid dir index: %d (%d - %d)\n", | |
2022 | ndx, NDX_FLIST_OFFSET, | |
2023 | NDX_FLIST_OFFSET - dir_flist->count); | |
2024 | exit_cleanup(RERR_PROTOCOL); | |
2025 | } | |
2026 | flist = recv_file_list(f); | |
2027 | flist->parent_ndx = ndx; | |
2028 | } | |
c627d613 AT |
2029 | } |
2030 | ||
f5db0993 WD |
2031 | /* Search for an identically-named item in the file list. Note that the |
2032 | * items must agree in their directory-ness, or no match is returned. */ | |
2f3cad89 | 2033 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 2034 | { |
f3c3ed44 | 2035 | int low = flist->low, high = flist->high; |
207522ae | 2036 | int diff, mid, mid_up; |
f3c3ed44 WD |
2037 | |
2038 | while (low <= high) { | |
2039 | mid = (low + high) / 2; | |
332cf6df | 2040 | if (F_IS_ACTIVE(flist->sorted[mid])) |
c0b134a4 | 2041 | mid_up = mid; |
7402d583 WD |
2042 | else { |
2043 | /* Scan for the next non-empty entry using the cached | |
2044 | * distance values. If the value isn't fully up-to- | |
2045 | * date, update it. */ | |
332cf6df WD |
2046 | mid_up = mid + F_DEPTH(flist->sorted[mid]); |
2047 | if (!F_IS_ACTIVE(flist->sorted[mid_up])) { | |
85aecef6 | 2048 | do { |
332cf6df WD |
2049 | mid_up += F_DEPTH(flist->sorted[mid_up]); |
2050 | } while (!F_IS_ACTIVE(flist->sorted[mid_up])); | |
2051 | F_DEPTH(flist->sorted[mid]) = mid_up - mid; | |
c0b134a4 | 2052 | } |
c0b134a4 | 2053 | if (mid_up > high) { |
7402d583 WD |
2054 | /* If there's nothing left above us, set high to |
2055 | * a non-empty entry below us and continue. */ | |
332cf6df WD |
2056 | high = mid - (int)flist->sorted[mid]->len32; |
2057 | if (!F_IS_ACTIVE(flist->sorted[high])) { | |
85aecef6 | 2058 | do { |
332cf6df WD |
2059 | high -= (int)flist->sorted[high]->len32; |
2060 | } while (!F_IS_ACTIVE(flist->sorted[high])); | |
2061 | flist->sorted[mid]->len32 = mid - high; | |
7402d583 | 2062 | } |
c0b134a4 WD |
2063 | continue; |
2064 | } | |
c0b134a4 | 2065 | } |
332cf6df | 2066 | diff = f_name_cmp(flist->sorted[mid_up], f); |
207522ae | 2067 | if (diff == 0) { |
f5db0993 | 2068 | if (protocol_version < 29 |
332cf6df | 2069 | && S_ISDIR(flist->sorted[mid_up]->mode) |
f5db0993 WD |
2070 | != S_ISDIR(f->mode)) |
2071 | return -1; | |
f3c3ed44 | 2072 | return mid_up; |
f5db0993 | 2073 | } |
207522ae | 2074 | if (diff < 0) |
f3c3ed44 | 2075 | low = mid_up + 1; |
207522ae WD |
2076 | else |
2077 | high = mid - 1; | |
d966ee25 | 2078 | } |
d966ee25 | 2079 | return -1; |
c627d613 AT |
2080 | } |
2081 | ||
3ec4dd97 | 2082 | /* |
9935066b S |
2083 | * Free up any resources a file_struct has allocated |
2084 | * and clear the file. | |
3ec4dd97 | 2085 | */ |
82ad07c4 | 2086 | void clear_file(struct file_struct *file) |
c627d613 | 2087 | { |
a3e18c76 WD |
2088 | /* The +1 zeros out the first char of the basename. */ |
2089 | memset(file, 0, FILE_STRUCT_LEN + 1); | |
9d737ecb | 2090 | /* In an empty entry, F_DEPTH() is an offset to the next non-empty |
96293cf9 | 2091 | * entry. Likewise for len32 in the opposite direction. We assume |
0679ac4c WD |
2092 | * that we're alone for now since flist_find() will adjust the counts |
2093 | * it runs into that aren't up-to-date. */ | |
9d737ecb | 2094 | file->len32 = F_DEPTH(file) = 1; |
3ec4dd97 | 2095 | } |
c627d613 | 2096 | |
4785cd43 | 2097 | /* Allocate a new file list. */ |
c7e6f84f | 2098 | struct file_list *flist_new(int flags, char *msg) |
3d382777 AT |
2099 | { |
2100 | struct file_list *flist; | |
2101 | ||
58cadc86 | 2102 | flist = new(struct file_list); |
ebed4c3a | 2103 | if (!flist) |
9935066b | 2104 | out_of_memory(msg); |
3d382777 | 2105 | |
82ad07c4 | 2106 | memset(flist, 0, sizeof flist[0]); |
9935066b | 2107 | |
c7e6f84f WD |
2108 | if (!(flags & FLIST_TEMP)) { |
2109 | if (first_flist) { | |
2110 | flist->ndx_start = first_flist->prev->ndx_start | |
2111 | + first_flist->prev->count; | |
2112 | } | |
2113 | /* This is a doubly linked list with prev looping back to | |
2114 | * the end of the list, but the last next pointer is NULL. */ | |
2115 | if (!first_flist) | |
2116 | first_flist = cur_flist = flist->prev = flist; | |
2117 | else { | |
2118 | flist->prev = first_flist->prev; | |
2119 | flist->prev->next = first_flist->prev = flist; | |
2120 | } | |
2121 | flist_cnt++; | |
2122 | } | |
2123 | ||
4a19c3b2 | 2124 | if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, out_of_memory, POOL_INTERN))) |
9935066b S |
2125 | out_of_memory(msg); |
2126 | ||
3d382777 AT |
2127 | return flist; |
2128 | } | |
ebed4c3a | 2129 | |
4785cd43 | 2130 | /* Free up all elements in a flist. */ |
3ec4dd97 AT |
2131 | void flist_free(struct file_list *flist) |
2132 | { | |
c7e6f84f WD |
2133 | if (!flist->prev) |
2134 | ; /* Was FLIST_TEMP dir-list. */ | |
2135 | else if (flist == flist->prev) { | |
2136 | first_flist = cur_flist = NULL; | |
2137 | file_total = 0; | |
2138 | flist_cnt = 0; | |
2139 | } else { | |
2140 | if (flist == cur_flist) | |
2141 | cur_flist = flist->next; | |
2142 | if (flist == first_flist) | |
2143 | first_flist = first_flist->next; | |
2144 | else { | |
2145 | flist->prev->next = flist->next; | |
2146 | if (!flist->next) | |
2147 | flist->next = first_flist; | |
2148 | } | |
2149 | flist->next->prev = flist->prev; | |
2150 | file_total -= flist->count; | |
2151 | flist_cnt--; | |
2152 | } | |
2153 | ||
9935066b | 2154 | pool_destroy(flist->file_pool); |
332cf6df WD |
2155 | if (flist->sorted && flist->sorted != flist->files) |
2156 | free(flist->sorted); | |
3ec4dd97 | 2157 | free(flist->files); |
3ec4dd97 | 2158 | free(flist); |
c627d613 AT |
2159 | } |
2160 | ||
a6e7b978 WD |
2161 | /* This routine ensures we don't have any duplicate names in our file list. |
2162 | * duplicate names can cause corruption because of the pipelining. */ | |
2163 | static void clean_flist(struct file_list *flist, int strip_root) | |
c627d613 | 2164 | { |
85aecef6 | 2165 | char fbuf[MAXPATHLEN]; |
a6e7b978 | 2166 | int i, prev_i; |
c627d613 | 2167 | |
910ee8c9 WD |
2168 | if (!flist) |
2169 | return; | |
2170 | if (flist->count == 0) { | |
2171 | flist->high = -1; | |
3ec4dd97 | 2172 | return; |
910ee8c9 | 2173 | } |
3ec4dd97 | 2174 | |
332cf6df WD |
2175 | qsort(flist->sorted, flist->count, |
2176 | sizeof flist->sorted[0], (int (*)())file_compare); | |
ebed4c3a | 2177 | |
a6e7b978 WD |
2178 | if (!am_sender || inc_recurse) { |
2179 | for (i = prev_i = 0; i < flist->count; i++) { | |
2180 | if (F_IS_ACTIVE(flist->sorted[i])) { | |
2181 | prev_i = i; | |
2182 | break; | |
2183 | } | |
b91b50c0 | 2184 | } |
a6e7b978 WD |
2185 | flist->low = prev_i; |
2186 | } else { | |
2187 | i = prev_i = flist->count - 1; | |
2188 | flist->low = 0; | |
b91b50c0 | 2189 | } |
a6e7b978 | 2190 | |
b91b50c0 | 2191 | while (++i < flist->count) { |
fe1c19dc | 2192 | int j; |
332cf6df | 2193 | struct file_struct *file = flist->sorted[i]; |
f5db0993 | 2194 | |
96293cf9 | 2195 | if (!F_IS_ACTIVE(file)) |
b91b50c0 | 2196 | continue; |
332cf6df | 2197 | if (f_name_cmp(file, flist->sorted[prev_i]) == 0) |
fe1c19dc WD |
2198 | j = prev_i; |
2199 | else if (protocol_version >= 29 && S_ISDIR(file->mode)) { | |
f5db0993 WD |
2200 | int save_mode = file->mode; |
2201 | /* Make sure that this directory doesn't duplicate a | |
2202 | * non-directory earlier in the list. */ | |
f5db0993 | 2203 | flist->high = prev_i; |
fe1c19dc WD |
2204 | file->mode = S_IFREG; |
2205 | j = flist_find(flist, file); | |
f5db0993 | 2206 | file->mode = save_mode; |
fe1c19dc WD |
2207 | } else |
2208 | j = -1; | |
2209 | if (j >= 0) { | |
332cf6df | 2210 | struct file_struct *fp = flist->sorted[j]; |
fe1c19dc WD |
2211 | int keep, drop; |
2212 | /* If one is a dir and the other is not, we want to | |
2213 | * keep the dir because it might have contents in the | |
2214 | * list. */ | |
2215 | if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) { | |
2216 | if (S_ISDIR(file->mode)) | |
2217 | keep = i, drop = j; | |
2218 | else | |
2219 | keep = j, drop = i; | |
c7e6f84f | 2220 | } else if (protocol_version < 27) |
fe1c19dc | 2221 | keep = j, drop = i; |
c7e6f84f WD |
2222 | else |
2223 | keep = i, drop = j; | |
fe1c19dc | 2224 | |
a6e7b978 WD |
2225 | if (am_sender) |
2226 | flist->sorted[drop]->flags |= FLAG_DUPLICATE; | |
2227 | else { | |
2228 | if (verbose > 1) { | |
2229 | rprintf(FINFO, | |
2230 | "removing duplicate name %s from file list (%d)\n", | |
2231 | f_name(file, fbuf), drop); | |
2232 | } | |
2233 | /* Make sure we don't lose track of a user-specified | |
2234 | * top directory. */ | |
2235 | flist->sorted[keep]->flags |= flist->sorted[drop]->flags | |
2236 | & (FLAG_TOP_DIR|FLAG_XFER_DIR); | |
2237 | ||
2238 | clear_file(flist->sorted[drop]); | |
2239 | } | |
9935066b | 2240 | |
fe1c19dc WD |
2241 | if (keep == i) { |
2242 | if (flist->low == drop) { | |
2243 | for (j = drop + 1; | |
332cf6df | 2244 | j < i && !F_IS_ACTIVE(flist->sorted[j]); |
fe1c19dc WD |
2245 | j++) {} |
2246 | flist->low = j; | |
2247 | } | |
2248 | prev_i = i; | |
2249 | } | |
728d0922 | 2250 | } else |
6931c138 | 2251 | prev_i = i; |
3ec4dd97 | 2252 | } |
a6e7b978 | 2253 | flist->high = prev_i; |
0199b05f | 2254 | |
c0b134a4 WD |
2255 | if (strip_root) { |
2256 | /* We need to strip off the leading slashes for relative | |
2257 | * paths, but this must be done _after_ the sorting phase. */ | |
2258 | for (i = flist->low; i <= flist->high; i++) { | |
332cf6df | 2259 | struct file_struct *file = flist->sorted[i]; |
c0b134a4 WD |
2260 | |
2261 | if (!file->dirname) | |
2262 | continue; | |
2263 | while (*file->dirname == '/') | |
2264 | file->dirname++; | |
2265 | if (!*file->dirname) | |
2266 | file->dirname = NULL; | |
2267 | } | |
2268 | } | |
2269 | ||
a6e7b978 | 2270 | if (prune_empty_dirs && !am_sender) { |
e6ffb966 | 2271 | int j, prev_depth = 0; |
9c000f5e | 2272 | |
e6ffb966 | 2273 | prev_i = 0; /* It's OK that this isn't really true. */ |
9c000f5e | 2274 | |
f5db0993 | 2275 | for (i = flist->low; i <= flist->high; i++) { |
332cf6df | 2276 | struct file_struct *fp, *file = flist->sorted[i]; |
ebed4c3a | 2277 | |
9d737ecb | 2278 | /* This temporarily abuses the F_DEPTH() value for a |
e6ffb966 WD |
2279 | * directory that is in a chain that might get pruned. |
2280 | * We restore the old value if it gets a reprieve. */ | |
9d737ecb | 2281 | if (S_ISDIR(file->mode) && F_DEPTH(file)) { |
e6ffb966 | 2282 | /* Dump empty dirs when coming back down. */ |
9d737ecb | 2283 | for (j = prev_depth; j >= F_DEPTH(file); j--) { |
332cf6df | 2284 | fp = flist->sorted[prev_i]; |
9d737ecb | 2285 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2286 | break; |
9d737ecb | 2287 | prev_i = -F_DEPTH(fp)-1; |
82ad07c4 | 2288 | clear_file(fp); |
9c000f5e | 2289 | } |
9d737ecb | 2290 | prev_depth = F_DEPTH(file); |
85aecef6 WD |
2291 | if (is_excluded(f_name(file, fbuf), 1, |
2292 | ALL_FILTERS)) { | |
e6ffb966 WD |
2293 | /* Keep dirs through this dir. */ |
2294 | for (j = prev_depth-1; ; j--) { | |
332cf6df | 2295 | fp = flist->sorted[prev_i]; |
9d737ecb | 2296 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2297 | break; |
9d737ecb WD |
2298 | prev_i = -F_DEPTH(fp)-1; |
2299 | F_DEPTH(fp) = j; | |
e6ffb966 | 2300 | } |
85aecef6 | 2301 | } else |
9d737ecb | 2302 | F_DEPTH(file) = -prev_i-1; |
e6ffb966 WD |
2303 | prev_i = i; |
2304 | } else { | |
2305 | /* Keep dirs through this non-dir. */ | |
2306 | for (j = prev_depth; ; j--) { | |
332cf6df | 2307 | fp = flist->sorted[prev_i]; |
9d737ecb | 2308 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2309 | break; |
9d737ecb WD |
2310 | prev_i = -F_DEPTH(fp)-1; |
2311 | F_DEPTH(fp) = j; | |
e6ffb966 | 2312 | } |
0199b05f | 2313 | } |
9c000f5e | 2314 | } |
ca947dea | 2315 | /* Dump all remaining empty dirs. */ |
e6ffb966 | 2316 | while (1) { |
332cf6df | 2317 | struct file_struct *fp = flist->sorted[prev_i]; |
9d737ecb | 2318 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2319 | break; |
9d737ecb | 2320 | prev_i = -F_DEPTH(fp)-1; |
82ad07c4 | 2321 | clear_file(fp); |
9c000f5e | 2322 | } |
f5db0993 | 2323 | |
9c000f5e | 2324 | for (i = flist->low; i <= flist->high; i++) { |
332cf6df | 2325 | if (F_IS_ACTIVE(flist->sorted[i])) |
9c000f5e WD |
2326 | break; |
2327 | } | |
2328 | flist->low = i; | |
2329 | for (i = flist->high; i >= flist->low; i--) { | |
332cf6df | 2330 | if (F_IS_ACTIVE(flist->sorted[i])) |
9c000f5e | 2331 | break; |
0199b05f | 2332 | } |
9c000f5e | 2333 | flist->high = i; |
0199b05f | 2334 | } |
cefed3e8 | 2335 | } |
0199b05f | 2336 | |
32cbfe7b | 2337 | static void output_flist(struct file_list *flist) |
cefed3e8 | 2338 | { |
f3c3ed44 | 2339 | char uidbuf[16], gidbuf[16], depthbuf[16]; |
cefed3e8 | 2340 | struct file_struct *file; |
9d737ecb | 2341 | const char *root, *dir, *slash, *name, *trail; |
32cbfe7b | 2342 | const char *who = who_am_i(); |
cefed3e8 | 2343 | int i; |
0199b05f | 2344 | |
c7e6f84f WD |
2345 | rprintf(FINFO, "[%s] flist start=%d, count=%d, low=%d, high=%d\n", |
2346 | who, flist->ndx_start, flist->count, flist->low, flist->high); | |
ebed4c3a | 2347 | for (i = 0; i < flist->count; i++) { |
a6e7b978 | 2348 | file = flist->sorted[i]; |
82ad07c4 | 2349 | if ((am_root || am_sender) && preserve_uid) { |
1564cd5a WD |
2350 | snprintf(uidbuf, sizeof uidbuf, " uid=%u", |
2351 | F_OWNER(file)); | |
82ad07c4 | 2352 | } else |
f05f993e | 2353 | *uidbuf = '\0'; |
1564cd5a WD |
2354 | if (preserve_gid) { |
2355 | static char parens[] = "(\0)\0\0\0"; | |
2356 | char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3); | |
2357 | snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s", | |
2358 | pp, F_GROUP(file), pp + 2); | |
82ad07c4 | 2359 | } else |
f05f993e | 2360 | *gidbuf = '\0'; |
f3c3ed44 | 2361 | if (!am_sender) |
9d737ecb | 2362 | snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file)); |
96293cf9 | 2363 | if (F_IS_ACTIVE(file)) { |
ef35abb2 | 2364 | root = am_sender ? NS(F_PATHNAME(file)) : depthbuf; |
96293cf9 WD |
2365 | if ((dir = file->dirname) == NULL) |
2366 | dir = slash = ""; | |
2367 | else | |
2368 | slash = "/"; | |
c58c1dc4 | 2369 | name = file->basename; |
96293cf9 WD |
2370 | trail = S_ISDIR(file->mode) ? "/" : ""; |
2371 | } else | |
9d737ecb | 2372 | root = dir = slash = name = trail = ""; |
14698a3a | 2373 | rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n", |
9d737ecb | 2374 | who, i, root, dir, slash, name, trail, (int)file->mode, |
96293cf9 | 2375 | (double)F_LENGTH(file), uidbuf, gidbuf, file->flags); |
0199b05f | 2376 | } |
3ec4dd97 AT |
2377 | } |
2378 | ||
8824e2ce | 2379 | enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING }; |
f5db0993 | 2380 | enum fnc_type { t_PATH, t_ITEM }; |
8018edd3 | 2381 | |
2f3cad89 | 2382 | /* Compare the names of two file_struct entities, similar to how strcmp() |
f5db0993 WD |
2383 | * would do if it were operating on the joined strings. |
2384 | * | |
2385 | * Some differences beginning with protocol_version 29: (1) directory names | |
2386 | * are compared with an assumed trailing slash so that they compare in a | |
2387 | * way that would cause them to sort immediately prior to any content they | |
2388 | * may have; (2) a directory of any name compares after a non-directory of | |
2389 | * any name at the same depth; (3) a directory with name "." compares prior | |
2390 | * to anything else. These changes mean that a directory and a non-dir | |
2391 | * with the same name will not compare as equal (protocol_version >= 29). | |
2392 | * | |
2393 | * The dirname component can be an empty string, but the basename component | |
2394 | * cannot (and never is in the current codebase). The basename component | |
2395 | * may be NULL (for a removed item), in which case it is considered to be | |
2396 | * after any existing item. */ | |
8018edd3 | 2397 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 2398 | { |
8018edd3 WD |
2399 | int dif; |
2400 | const uchar *c1, *c2; | |
1ef00d20 | 2401 | enum fnc_state state1, state2; |
f5db0993 WD |
2402 | enum fnc_type type1, type2; |
2403 | enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM; | |
8018edd3 | 2404 | |
96293cf9 WD |
2405 | if (!f1 || !F_IS_ACTIVE(f1)) { |
2406 | if (!f2 || !F_IS_ACTIVE(f2)) | |
8018edd3 WD |
2407 | return 0; |
2408 | return -1; | |
2409 | } | |
96293cf9 | 2410 | if (!f2 || !F_IS_ACTIVE(f2)) |
8018edd3 WD |
2411 | return 1; |
2412 | ||
14698a3a WD |
2413 | c1 = (uchar*)f1->dirname; |
2414 | c2 = (uchar*)f2->dirname; | |
2415 | if (c1 == c2) | |
2416 | c1 = c2 = NULL; | |
2417 | if (!c1) { | |
f5db0993 | 2418 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2419 | c1 = (const uchar*)f1->basename; |
f5db0993 WD |
2420 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
2421 | type1 = t_ITEM; | |
2422 | state1 = s_TRAILING; | |
2423 | c1 = (uchar*)""; | |
2424 | } else | |
2425 | state1 = s_BASE; | |
f5db0993 WD |
2426 | } else { |
2427 | type1 = t_path; | |
8824e2ce | 2428 | state1 = s_DIR; |
f5db0993 | 2429 | } |
14698a3a | 2430 | if (!c2) { |
f5db0993 | 2431 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2432 | c2 = (const uchar*)f2->basename; |
f5db0993 WD |
2433 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
2434 | type2 = t_ITEM; | |
2435 | state2 = s_TRAILING; | |
2436 | c2 = (uchar*)""; | |
2437 | } else | |
2438 | state2 = s_BASE; | |
f5db0993 WD |
2439 | } else { |
2440 | type2 = t_path; | |
8824e2ce | 2441 | state2 = s_DIR; |
f5db0993 WD |
2442 | } |
2443 | ||
2444 | if (type1 != type2) | |
2445 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 2446 | |
e4fdf1de | 2447 | do { |
f5db0993 | 2448 | if (!*c1) { |
8018edd3 | 2449 | switch (state1) { |
8824e2ce WD |
2450 | case s_DIR: |
2451 | state1 = s_SLASH; | |
e90b8ace | 2452 | c1 = (uchar*)"/"; |
8018edd3 | 2453 | break; |
8824e2ce | 2454 | case s_SLASH: |
f5db0993 | 2455 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2456 | c1 = (const uchar*)f1->basename; |
cbb5fa4f WD |
2457 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
2458 | type1 = t_ITEM; | |
2459 | state1 = s_TRAILING; | |
2460 | c1 = (uchar*)""; | |
2461 | } else | |
2462 | state1 = s_BASE; | |
8018edd3 | 2463 | break; |
8824e2ce WD |
2464 | case s_BASE: |
2465 | state1 = s_TRAILING; | |
f5db0993 | 2466 | if (type1 == t_PATH) { |
2f3cad89 | 2467 | c1 = (uchar*)"/"; |
f5db0993 WD |
2468 | break; |
2469 | } | |
2470 | /* FALL THROUGH */ | |
8824e2ce | 2471 | case s_TRAILING: |
f5db0993 | 2472 | type1 = t_ITEM; |
8018edd3 WD |
2473 | break; |
2474 | } | |
f5db0993 WD |
2475 | if (*c2 && type1 != type2) |
2476 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 2477 | } |
f5db0993 | 2478 | if (!*c2) { |
8018edd3 | 2479 | switch (state2) { |
8824e2ce WD |
2480 | case s_DIR: |
2481 | state2 = s_SLASH; | |
e90b8ace | 2482 | c2 = (uchar*)"/"; |
8018edd3 | 2483 | break; |
8824e2ce | 2484 | case s_SLASH: |
f5db0993 | 2485 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2486 | c2 = (const uchar*)f2->basename; |
cbb5fa4f WD |
2487 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
2488 | type2 = t_ITEM; | |
2489 | state2 = s_TRAILING; | |
2490 | c2 = (uchar*)""; | |
2491 | } else | |
2492 | state2 = s_BASE; | |
8018edd3 | 2493 | break; |
8824e2ce | 2494 | case s_BASE: |
8824e2ce | 2495 | state2 = s_TRAILING; |
f5db0993 | 2496 | if (type2 == t_PATH) { |
2f3cad89 | 2497 | c2 = (uchar*)"/"; |
f5db0993 WD |
2498 | break; |
2499 | } | |
2500 | /* FALL THROUGH */ | |
8824e2ce | 2501 | case s_TRAILING: |
f5db0993 WD |
2502 | if (!*c1) |
2503 | return 0; | |
2504 | type2 = t_ITEM; | |
8018edd3 WD |
2505 | break; |
2506 | } | |
f5db0993 WD |
2507 | if (type1 != type2) |
2508 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 2509 | } |
e4fdf1de | 2510 | } while ((dif = (int)*c1++ - (int)*c2++) == 0); |
8018edd3 WD |
2511 | |
2512 | return dif; | |
2513 | } | |
2514 | ||
96293cf9 WD |
2515 | char *f_name_buf(void) |
2516 | { | |
2517 | static char names[5][MAXPATHLEN]; | |
2518 | static unsigned int n; | |
2519 | ||
2520 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
2521 | ||
2522 | return names[n]; | |
2523 | } | |
2524 | ||
8018edd3 | 2525 | /* Return a copy of the full filename of a flist entry, using the indicated |
5e4ff5f9 WD |
2526 | * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is |
2527 | * done because we checked the size when creating the file_struct entry. | |
8018edd3 | 2528 | */ |
5e4ff5f9 | 2529 | char *f_name(struct file_struct *f, char *fbuf) |
8018edd3 | 2530 | { |
96293cf9 | 2531 | if (!f || !F_IS_ACTIVE(f)) |
ebed4c3a | 2532 | return NULL; |
3ec4dd97 | 2533 | |
96293cf9 WD |
2534 | if (!fbuf) |
2535 | fbuf = f_name_buf(); | |
5e4ff5f9 | 2536 | |
3ec4dd97 | 2537 | if (f->dirname) { |
882e6893 WD |
2538 | int len = strlen(f->dirname); |
2539 | memcpy(fbuf, f->dirname, len); | |
2540 | fbuf[len] = '/'; | |
c58c1dc4 | 2541 | strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1)); |
8018edd3 | 2542 | } else |
c58c1dc4 | 2543 | strlcpy(fbuf, f->basename, MAXPATHLEN); |
0ee6ca98 | 2544 | |
b7736c79 | 2545 | return fbuf; |
8018edd3 | 2546 | } |
e03dfae5 | 2547 | |
32cbfe7b WD |
2548 | /* Do a non-recursive scan of the named directory, possibly ignoring all |
2549 | * exclude rules except for the daemon's. If "dlen" is >=0, it is the length | |
2550 | * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN | |
2551 | * buffer (the functions we call will append names onto the end, but the old | |
2552 | * dir value will be restored on exit). */ | |
263d3bfb | 2553 | struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules) |
37802f40 WD |
2554 | { |
2555 | struct file_list *dirlist; | |
2556 | char dirbuf[MAXPATHLEN]; | |
37802f40 | 2557 | int save_recurse = recurse; |
1661fe9b | 2558 | int save_xfer_dirs = xfer_dirs; |
37802f40 | 2559 | |
32cbfe7b WD |
2560 | if (dlen < 0) { |
2561 | dlen = strlcpy(dirbuf, dirname, MAXPATHLEN); | |
2562 | if (dlen >= MAXPATHLEN) | |
2563 | return NULL; | |
2564 | dirname = dirbuf; | |
2565 | } | |
37802f40 | 2566 | |
c7e6f84f | 2567 | dirlist = flist_new(FLIST_TEMP, "get_dirlist"); |
32cbfe7b | 2568 | |
37802f40 | 2569 | recurse = 0; |
1661fe9b | 2570 | xfer_dirs = 1; |
a6e7b978 | 2571 | send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0); |
1661fe9b | 2572 | xfer_dirs = save_xfer_dirs; |
37802f40 | 2573 | recurse = save_recurse; |
53135fe8 WD |
2574 | if (do_progress) |
2575 | flist_count_offset += dirlist->count; | |
37802f40 | 2576 | |
332cf6df | 2577 | dirlist->sorted = dirlist->files; |
a6e7b978 | 2578 | clean_flist(dirlist, 0); |
564ef546 | 2579 | |
45478cc7 | 2580 | if (verbose > 3) |
32cbfe7b | 2581 | output_flist(dirlist); |
45478cc7 | 2582 | |
32cbfe7b | 2583 | return dirlist; |
45478cc7 | 2584 | } |