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> | |
b3bf9b9d | 7 | * Copyright (C) 2002-2009 Wayne Davison |
0f78b815 WD |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 3 of the License, or | |
12 | * (at your option) any later version. | |
736a6a29 | 13 | * |
0f78b815 WD |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
172875cf | 18 | * |
e7c67065 | 19 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 20 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 21 | */ |
c627d613 AT |
22 | |
23 | #include "rsync.h" | |
1b42f628 | 24 | #include "ifuncs.h" |
a3e18c76 | 25 | #include "rounding.h" |
5dd14f0c | 26 | #include "inums.h" |
6d4e718f | 27 | #include "io.h" |
c627d613 | 28 | |
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; |
c627d613 | 35 | extern int always_checksum; |
983b1ed3 WD |
36 | extern int module_id; |
37 | extern int ignore_errors; | |
4836c3ee | 38 | extern int numeric_ids; |
a06d19e3 | 39 | extern int recurse; |
a572e126 | 40 | extern int use_qsort; |
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; |
236adddc WD |
46 | extern int preserve_uid; |
47 | extern int preserve_gid; | |
1c3344a1 | 48 | extern int preserve_acls; |
16edf865 | 49 | extern int preserve_xattrs; |
c627d613 | 50 | extern int preserve_links; |
dc5ddbcc | 51 | extern int preserve_hard_links; |
c627d613 | 52 | extern int preserve_devices; |
b5c6a6ae | 53 | extern int preserve_specials; |
42d8ec61 | 54 | extern int missing_args; |
9b25ef35 WD |
55 | extern int uid_ndx; |
56 | extern int gid_ndx; | |
95a44066 | 57 | extern int eol_nulls; |
6574b4f7 | 58 | extern int relative_paths; |
24d0fcde | 59 | extern int implied_dirs; |
99a957d3 | 60 | extern int file_extra_cnt; |
53039410 WD |
61 | extern int ignore_perishable; |
62 | extern int non_perishable_cnt; | |
85aecef6 | 63 | extern int prune_empty_dirs; |
82306bf6 | 64 | extern int copy_links; |
b5313607 | 65 | extern int copy_unsafe_links; |
d04e9c51 | 66 | extern int protocol_version; |
cb13abfe | 67 | extern int sanitize_paths; |
9585b276 | 68 | extern int munge_symlinks; |
f7a76b9c | 69 | extern int need_unsorted_flist; |
91fd15b8 | 70 | extern int sender_symlink_iconv; |
951e826b | 71 | extern int output_needs_newline; |
886df221 | 72 | extern int sender_keeps_checksum; |
f7a76b9c | 73 | extern int unsort_ndx; |
d64e6f42 | 74 | extern struct stats stats; |
95a44066 | 75 | extern char *filesfrom_host; |
2df20057 | 76 | extern char *usermap, *groupmap; |
c627d613 | 77 | |
e1f40891 WD |
78 | extern char curr_dir[MAXPATHLEN]; |
79 | ||
2b7e0f33 WD |
80 | extern struct chmod_mode_struct *chmod_modes; |
81 | ||
7b6c5c77 WD |
82 | extern filter_rule_list filter_list; |
83 | extern filter_rule_list daemon_filter_list; | |
c627d613 | 84 | |
332cf6df | 85 | #ifdef ICONV_OPTION |
2509753f | 86 | extern int filesfrom_convert; |
332cf6df WD |
87 | extern iconv_t ic_send, ic_recv; |
88 | #endif | |
89 | ||
a572e126 WD |
90 | #define PTR_SIZE (sizeof (struct file_struct *)) |
91 | ||
06c28400 | 92 | int io_error; |
7752df41 | 93 | int checksum_len; |
535737bf | 94 | dev_t filesystem_dev; /* used to implement -x */ |
82ad07c4 | 95 | |
c7e6f84f | 96 | struct file_list *cur_flist, *first_flist, *dir_flist; |
4e421735 | 97 | int send_dir_ndx = -1, send_dir_depth = -1; |
c7e6f84f WD |
98 | int flist_cnt = 0; /* how many (non-tmp) file list objects exist */ |
99 | int file_total = 0; /* total of all active items over all file-lists */ | |
ce827c3e | 100 | int file_old_total = 0; /* total of active items that will soon be gone */ |
c7e6f84f WD |
101 | int flist_eof = 0; /* all the file-lists are now known */ |
102 | ||
4e421735 WD |
103 | #define NORMAL_NAME 0 |
104 | #define SLASH_ENDING_NAME 1 | |
84ecaa0e | 105 | #define DOTDIR_NAME 2 |
4e421735 | 106 | |
7e4b6b7b WD |
107 | /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t |
108 | * internally, even if this platform does not allow files to have 64-bit inums. | |
109 | * The only exception is if we're on a platform with no 64-bit type at all. | |
110 | * | |
111 | * Because we use read_longint() to get these off the wire, if you transfer | |
112 | * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a | |
113 | * machine with no 64-bit types then you will get an overflow error. | |
114 | * | |
115 | * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris) | |
116 | * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will | |
117 | * be truncated. But it's a kind of silly thing to do anyhow. */ | |
118 | ||
82ad07c4 WD |
119 | /* The tmp_* vars are used as a cache area by make_file() to store data |
120 | * that the sender doesn't need to remember in its file list. The data | |
121 | * will survive just long enough to be used by send_file_entry(). */ | |
122 | static dev_t tmp_rdev; | |
9d155ecd | 123 | #ifdef SUPPORT_HARD_LINKS |
9d737ecb | 124 | static int64 tmp_dev, tmp_ino; |
9d155ecd | 125 | #endif |
a0456b9c | 126 | static char tmp_sum[MAX_DIGEST_LEN]; |
06c28400 | 127 | |
a0456b9c | 128 | static char empty_sum[MAX_DIGEST_LEN]; |
c7565dad | 129 | static int flist_count_offset; /* for --delete --progress */ |
3d382777 | 130 | |
65b4e4b2 | 131 | static void flist_sort_and_clean(struct file_list *flist, int strip_root); |
32cbfe7b | 132 | static void output_flist(struct file_list *flist); |
0199b05f | 133 | |
61dec11a WD |
134 | void init_flist(void) |
135 | { | |
951e826b | 136 | if (DEBUG_GTE(FLIST, 4)) { |
96293cf9 | 137 | rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n", |
6aef83c9 | 138 | (int)FILE_STRUCT_LEN, (int)EXTRA_LEN); |
96293cf9 | 139 | } |
a0456b9c WD |
140 | checksum_len = protocol_version < 21 ? 2 |
141 | : protocol_version < 30 ? MD4_DIGEST_LEN | |
142 | : MD5_DIGEST_LEN; | |
61dec11a WD |
143 | } |
144 | ||
1bbd10fe | 145 | static int show_filelist_p(void) |
58225000 | 146 | { |
951e826b | 147 | return INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse; |
1bbd10fe | 148 | } |
ebed4c3a | 149 | |
1bbd10fe DD |
150 | static void start_filelist_progress(char *kind) |
151 | { | |
ea124cb3 | 152 | rprintf(FCLIENT, "%s ... ", kind); |
951e826b | 153 | output_needs_newline = 1; |
1bbd10fe | 154 | rflush(FINFO); |
58225000 MP |
155 | } |
156 | ||
53135fe8 | 157 | static void emit_filelist_progress(int count) |
db719fb0 | 158 | { |
ea124cb3 | 159 | rprintf(FCLIENT, " %d files...\r", count); |
db719fb0 MP |
160 | } |
161 | ||
53135fe8 | 162 | static void maybe_emit_filelist_progress(int count) |
58225000 | 163 | { |
951e826b | 164 | if (INFO_GTE(FLIST, 2) && show_filelist_p() && (count % 100) == 0) |
53135fe8 | 165 | emit_filelist_progress(count); |
58225000 MP |
166 | } |
167 | ||
1bbd10fe | 168 | static void finish_filelist_progress(const struct file_list *flist) |
58225000 | 169 | { |
951e826b | 170 | if (INFO_GTE(FLIST, 2)) { |
1bbd10fe | 171 | /* This overwrites the progress line */ |
c7b562be | 172 | rprintf(FINFO, "%d file%sto consider\n", |
9decb4d2 | 173 | flist->used, flist->used == 1 ? " " : "s "); |
951e826b WD |
174 | } else { |
175 | output_needs_newline = 0; | |
1bbd10fe | 176 | rprintf(FINFO, "done\n"); |
951e826b | 177 | } |
58225000 MP |
178 | } |
179 | ||
86943126 MP |
180 | void show_flist_stats(void) |
181 | { | |
182 | /* Nothing yet */ | |
183 | } | |
184 | ||
cad8f6f9 WD |
185 | /* Stat either a symlink or its referent, depending on the settings of |
186 | * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success. | |
4e5db0ad | 187 | * |
cad8f6f9 WD |
188 | * If path is the name of a symlink, then the linkbuf buffer (which must hold |
189 | * MAXPATHLEN chars) will be set to the symlink's target string. | |
4e5db0ad | 190 | * |
cad8f6f9 WD |
191 | * The stat structure pointed to by stp will contain information about the |
192 | * link or the referent as appropriate, if they exist. */ | |
193 | static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf) | |
b5313607 | 194 | { |
4f5b0756 | 195 | #ifdef SUPPORT_LINKS |
cad8f6f9 | 196 | if (link_stat(path, stp, copy_dirlinks) < 0) |
b5313607 | 197 | return -1; |
cad8f6f9 WD |
198 | if (S_ISLNK(stp->st_mode)) { |
199 | int llen = readlink(path, linkbuf, MAXPATHLEN - 1); | |
200 | if (llen < 0) | |
b5313607 | 201 | return -1; |
cad8f6f9 | 202 | linkbuf[llen] = '\0'; |
fc638474 | 203 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
951e826b | 204 | if (INFO_GTE(SYMSAFE, 1)) { |
dbda5fbf | 205 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
0ee6ca98 | 206 | path, linkbuf); |
fc638474 | 207 | } |
9439c0cb | 208 | return x_stat(path, stp, NULL); |
b5313607 | 209 | } |
9585b276 WD |
210 | if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN |
211 | && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) { | |
212 | memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN, | |
213 | llen - SYMLINK_PREFIX_LEN + 1); | |
214 | } | |
b5313607 DD |
215 | } |
216 | return 0; | |
217 | #else | |
9439c0cb | 218 | return x_stat(path, stp, NULL); |
b5313607 DD |
219 | #endif |
220 | } | |
221 | ||
cad8f6f9 | 222 | int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks) |
82306bf6 | 223 | { |
4f5b0756 | 224 | #ifdef SUPPORT_LINKS |
b7736c79 | 225 | if (copy_links) |
9439c0cb WD |
226 | return x_stat(path, stp, NULL); |
227 | if (x_lstat(path, stp, NULL) < 0) | |
314f4591 | 228 | return -1; |
cad8f6f9 | 229 | if (follow_dirlinks && S_ISLNK(stp->st_mode)) { |
314f4591 | 230 | STRUCT_STAT st; |
9439c0cb | 231 | if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode)) |
cad8f6f9 | 232 | *stp = st; |
314f4591 WD |
233 | } |
234 | return 0; | |
82306bf6 | 235 | #else |
9439c0cb | 236 | return x_stat(path, stp, NULL); |
82306bf6 AT |
237 | #endif |
238 | } | |
239 | ||
1aefb7ef WD |
240 | static inline int is_daemon_excluded(const char *fname, int is_dir) |
241 | { | |
819bfe45 | 242 | if (daemon_filter_list.head |
1df02d13 | 243 | && check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) { |
1aefb7ef WD |
244 | errno = ENOENT; |
245 | return 1; | |
246 | } | |
247 | return 0; | |
248 | } | |
249 | ||
29a89172 | 250 | static inline int path_is_daemon_excluded(char *path, int ignore_filename) |
b5daf530 | 251 | { |
f8949e76 | 252 | if (daemon_filter_list.head) { |
29a89172 | 253 | char *slash = path; |
b5daf530 WD |
254 | |
255 | while ((slash = strchr(slash+1, '/')) != NULL) { | |
256 | int ret; | |
257 | *slash = '\0'; | |
1df02d13 | 258 | ret = check_filter(&daemon_filter_list, FLOG, path, 1); |
b5daf530 WD |
259 | *slash = '/'; |
260 | if (ret < 0) { | |
261 | errno = ENOENT; | |
262 | return 1; | |
263 | } | |
264 | } | |
265 | ||
266 | if (!ignore_filename | |
1df02d13 | 267 | && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) { |
b5daf530 WD |
268 | errno = ENOENT; |
269 | return 1; | |
270 | } | |
271 | } | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
7842418b | 276 | /* This function is used to check if a file should be included/excluded |
429f9828 | 277 | * from the list of files based on its name and type etc. The value of |
7842418b | 278 | * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */ |
1aefb7ef | 279 | static int is_excluded(const char *fname, int is_dir, int filter_level) |
c627d613 | 280 | { |
7d687932 | 281 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
7842418b | 282 | if (filter_level == NO_FILTERS) |
429f9828 WD |
283 | return 0; |
284 | #endif | |
1aefb7ef | 285 | if (is_daemon_excluded(fname, is_dir)) |
429f9828 | 286 | return 1; |
7842418b | 287 | if (filter_level != ALL_FILTERS) |
429f9828 | 288 | return 0; |
7842418b | 289 | if (filter_list.head |
1df02d13 | 290 | && check_filter(&filter_list, FINFO, fname, is_dir) < 0) |
76e26e10 | 291 | return 1; |
76e26e10 | 292 | return 0; |
c627d613 AT |
293 | } |
294 | ||
a6e7b978 | 295 | static void send_directory(int f, struct file_list *flist, |
c7e6f84f | 296 | char *fbuf, int len, int flags); |
c627d613 | 297 | |
ef35abb2 WD |
298 | static const char *pathname, *orig_dir; |
299 | static int pathname_len; | |
c627d613 | 300 | |
9decb4d2 | 301 | /* Make sure flist can hold at least flist->used + extra entries. */ |
8ea07c00 | 302 | static void flist_expand(struct file_list *flist, int extra) |
d9d6bc52 | 303 | { |
0501f363 | 304 | struct file_struct **new_ptr; |
2e7d1994 | 305 | |
9decb4d2 | 306 | if (flist->used + extra <= flist->malloced) |
a85906c7 | 307 | return; |
dbda5fbf | 308 | |
a85906c7 S |
309 | if (flist->malloced < FLIST_START) |
310 | flist->malloced = FLIST_START; | |
311 | else if (flist->malloced >= FLIST_LINEAR) | |
312 | flist->malloced += FLIST_LINEAR; | |
313 | else | |
314 | flist->malloced *= 2; | |
315 | ||
a6e7b978 WD |
316 | /* In case count jumped or we are starting the list |
317 | * with a known size just set it. */ | |
9decb4d2 WD |
318 | if (flist->malloced < flist->used + extra) |
319 | flist->malloced = flist->used + extra; | |
a6e7b978 | 320 | |
0501f363 WD |
321 | new_ptr = realloc_array(flist->files, struct file_struct *, |
322 | flist->malloced); | |
2e7d1994 | 323 | |
951e826b | 324 | if (DEBUG_GTE(FLIST, 1) && flist->malloced != FLIST_START) { |
6d56efa6 | 325 | rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n", |
a85906c7 | 326 | who_am_i(), |
adc2476f | 327 | big_num(sizeof flist->files[0] * flist->malloced), |
a85906c7 | 328 | (new_ptr == flist->files) ? " not" : ""); |
d9d6bc52 | 329 | } |
a85906c7 | 330 | |
0501f363 | 331 | flist->files = new_ptr; |
a85906c7 S |
332 | |
333 | if (!flist->files) | |
334 | out_of_memory("flist_expand"); | |
d9d6bc52 MP |
335 | } |
336 | ||
abdcb21a WD |
337 | static void flist_done_allocating(struct file_list *flist) |
338 | { | |
339 | void *ptr = pool_boundary(flist->file_pool, 8*1024); | |
340 | if (flist->pool_boundary == ptr) | |
341 | flist->pool_boundary = NULL; /* list didn't use any pool memory */ | |
342 | else | |
343 | flist->pool_boundary = ptr; | |
344 | } | |
345 | ||
29a89172 WD |
346 | /* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's |
347 | * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir, | |
348 | * with dir == NULL taken to be the starting directory, and dirlen < 0 | |
349 | * indicating that strdup(dir) should be called and then the -dirlen length | |
350 | * value checked to ensure that it is not daemon-excluded. */ | |
351 | int change_pathname(struct file_struct *file, const char *dir, int dirlen) | |
c7e6f84f | 352 | { |
29a89172 WD |
353 | if (dirlen < 0) { |
354 | char *cpy = strdup(dir); | |
355 | if (*cpy != '/') | |
356 | change_dir(orig_dir, CD_SKIP_CHDIR); | |
357 | if (path_is_daemon_excluded(cpy, 0)) | |
358 | goto chdir_error; | |
359 | dir = cpy; | |
360 | dirlen = -dirlen; | |
361 | } else { | |
362 | if (file) { | |
363 | if (pathname == F_PATHNAME(file)) | |
364 | return 1; | |
365 | dir = F_PATHNAME(file); | |
366 | if (dir) | |
367 | dirlen = strlen(dir); | |
368 | } else if (pathname == dir) | |
369 | return 1; | |
370 | if (dir && *dir != '/') | |
371 | change_dir(orig_dir, CD_SKIP_CHDIR); | |
c7e6f84f WD |
372 | } |
373 | ||
5b838296 WD |
374 | pathname = dir; |
375 | pathname_len = dirlen; | |
376 | ||
377 | if (!dir) | |
378 | dir = orig_dir; | |
379 | ||
380 | if (!change_dir(dir, CD_NORMAL)) { | |
29a89172 | 381 | chdir_error: |
c7e6f84f | 382 | io_error |= IOERR_GENERAL; |
342bfb5e | 383 | rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir)); |
5b838296 WD |
384 | if (dir != orig_dir) |
385 | change_dir(orig_dir, CD_NORMAL); | |
29a89172 WD |
386 | pathname = NULL; |
387 | pathname_len = 0; | |
c7e6f84f WD |
388 | return 0; |
389 | } | |
390 | ||
c7e6f84f WD |
391 | return 1; |
392 | } | |
393 | ||
6e5b6822 WD |
394 | static void send_file_entry(int f, const char *fname, struct file_struct *file, |
395 | #ifdef SUPPORT_LINKS | |
396 | const char *symlink_name, int symlink_len, | |
397 | #endif | |
398 | int ndx, int first_ndx) | |
c627d613 | 399 | { |
5911fee5 WD |
400 | static time_t modtime; |
401 | static mode_t mode; | |
c126e66e | 402 | #ifdef SUPPORT_HARD_LINKS |
1490812a | 403 | static int64 dev; |
c126e66e | 404 | #endif |
4124540d | 405 | static dev_t rdev; |
9c5e91f8 | 406 | static uint32 rdev_major; |
5911fee5 WD |
407 | static uid_t uid; |
408 | static gid_t gid; | |
c78cb8f3 | 409 | static const char *user_name, *group_name; |
5911fee5 | 410 | static char lastname[MAXPATHLEN]; |
0c096e29 | 411 | #ifdef SUPPORT_HARD_LINKS |
8cae71eb | 412 | int first_hlink_ndx = -1; |
0c096e29 | 413 | #endif |
ebed4c3a | 414 | int l1, l2; |
61cb031c | 415 | int xflags; |
72914a60 | 416 | |
e366e530 WD |
417 | /* Initialize starting value of xflags and adjust counts. */ |
418 | if (S_ISREG(file->mode)) | |
419 | xflags = 0; | |
420 | else if (S_ISDIR(file->mode)) { | |
421 | stats.num_dirs++; | |
422 | if (protocol_version >= 30) { | |
423 | if (file->flags & FLAG_CONTENT_DIR) | |
424 | xflags = file->flags & FLAG_TOP_DIR; | |
425 | else if (file->flags & FLAG_IMPLIED_DIR) | |
426 | xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR; | |
427 | else | |
428 | xflags = XMIT_NO_CONTENT_DIR; | |
429 | } else | |
430 | xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */ | |
431 | } else { | |
432 | if (S_ISLNK(file->mode)) | |
433 | stats.num_symlinks++; | |
434 | else if (IS_DEVICE(file->mode)) | |
435 | stats.num_devices++; | |
ce66f417 | 436 | else if (IS_SPECIAL(file->mode)) |
e366e530 WD |
437 | stats.num_specials++; |
438 | xflags = 0; | |
439 | } | |
72914a60 | 440 | |
30f337c9 | 441 | if (file->mode == mode) |
61cb031c | 442 | xflags |= XMIT_SAME_MODE; |
1ef00d20 | 443 | else |
30f337c9 | 444 | mode = file->mode; |
75f162e4 | 445 | |
a8e6e148 | 446 | if (preserve_devices && IS_DEVICE(mode)) { |
75bc8600 | 447 | if (protocol_version < 28) { |
82ad07c4 | 448 | if (tmp_rdev == rdev) |
61cb031c | 449 | xflags |= XMIT_SAME_RDEV_pre28; |
b5c6a6ae | 450 | else |
82ad07c4 | 451 | rdev = tmp_rdev; |
b5c6a6ae | 452 | } else { |
82ad07c4 | 453 | rdev = tmp_rdev; |
9c5e91f8 | 454 | if ((uint32)major(rdev) == rdev_major) |
61cb031c | 455 | xflags |= XMIT_SAME_RDEV_MAJOR; |
84a3efa0 | 456 | else |
9c5e91f8 | 457 | rdev_major = major(rdev); |
18438f0b | 458 | if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu) |
61cb031c | 459 | xflags |= XMIT_RDEV_MINOR_8_pre30; |
75bc8600 | 460 | } |
a8e6e148 WD |
461 | } else if (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31) { |
462 | /* Special files don't need an rdev number, so just make | |
463 | * the historical transmission of the value efficient. */ | |
464 | if (protocol_version < 28) | |
465 | xflags |= XMIT_SAME_RDEV_pre28; | |
466 | else { | |
467 | rdev = MAKEDEV(major(rdev), 0); | |
468 | xflags |= XMIT_SAME_RDEV_MAJOR; | |
469 | if (protocol_version < 30) | |
470 | xflags |= XMIT_RDEV_MINOR_8_pre30; | |
471 | } | |
b5c6a6ae | 472 | } else if (protocol_version < 28) |
b4a09b72 | 473 | rdev = MAKEDEV(0, 0); |
fd259802 WD |
474 | if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname)) |
475 | xflags |= XMIT_SAME_UID; | |
476 | else { | |
477 | uid = F_OWNER(file); | |
478 | if (!numeric_ids) { | |
479 | user_name = add_uid(uid); | |
480 | if (inc_recurse && user_name) | |
481 | xflags |= XMIT_USER_NAME_FOLLOWS; | |
c7e6f84f | 482 | } |
82ad07c4 | 483 | } |
fd259802 WD |
484 | if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname)) |
485 | xflags |= XMIT_SAME_GID; | |
486 | else { | |
487 | gid = F_GROUP(file); | |
488 | if (!numeric_ids) { | |
489 | group_name = add_gid(gid); | |
490 | if (inc_recurse && group_name) | |
491 | xflags |= XMIT_GROUP_NAME_FOLLOWS; | |
c7e6f84f | 492 | } |
82ad07c4 | 493 | } |
30f337c9 | 494 | if (file->modtime == modtime) |
61cb031c | 495 | xflags |= XMIT_SAME_TIME; |
1ef00d20 | 496 | else |
30f337c9 | 497 | modtime = file->modtime; |
a289addd | 498 | |
4f5b0756 | 499 | #ifdef SUPPORT_HARD_LINKS |
9d737ecb | 500 | if (tmp_dev != 0) { |
8cae71eb | 501 | if (protocol_version >= 30) { |
9863bdbc | 502 | struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino); |
424d3691 | 503 | first_hlink_ndx = (int32)(long)np->data - 1; |
8cae71eb | 504 | if (first_hlink_ndx < 0) { |
65b4e4b2 | 505 | np->data = (void*)(long)(first_ndx + ndx + 1); |
61cb031c | 506 | xflags |= XMIT_HLINK_FIRST; |
8cae71eb | 507 | } |
0c096e29 | 508 | if (DEBUG_GTE(HLINK, 1)) { |
28a5b78c WD |
509 | if (first_hlink_ndx >= 0) { |
510 | rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n", | |
511 | who_am_i(), first_ndx + ndx, first_hlink_ndx, | |
512 | first_hlink_ndx >= first_ndx ? "" : "un"); | |
513 | } else if (DEBUG_GTE(HLINK, 3)) { | |
514 | rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n", | |
515 | who_am_i(), first_ndx + ndx, | |
adc2476f | 516 | big_num(tmp_dev), big_num(tmp_ino)); |
28a5b78c | 517 | } |
0c096e29 | 518 | } |
8cae71eb | 519 | } else { |
9d737ecb | 520 | if (tmp_dev == dev) { |
8cae71eb | 521 | if (protocol_version >= 28) |
61cb031c | 522 | xflags |= XMIT_SAME_DEV_pre30; |
8cae71eb | 523 | } else |
9d737ecb | 524 | dev = tmp_dev; |
8cae71eb | 525 | } |
fd259802 | 526 | xflags |= XMIT_HLINKED; |
c4b4df4f | 527 | } |
0d162bd1 | 528 | #endif |
ebed4c3a MP |
529 | |
530 | for (l1 = 0; | |
3e491682 S |
531 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); |
532 | l1++) {} | |
eddd5d12 | 533 | l2 = strlen(fname+l1); |
72914a60 | 534 | |
ebed4c3a | 535 | if (l1 > 0) |
61cb031c | 536 | xflags |= XMIT_SAME_NAME; |
ebed4c3a | 537 | if (l2 > 255) |
61cb031c | 538 | xflags |= XMIT_LONG_NAME; |
72914a60 | 539 | |
1aa4caf3 WD |
540 | /* We must make sure we don't send a zero flag byte or the |
541 | * other end will terminate the flist transfer. Note that | |
ee3751c8 | 542 | * the use of XMIT_TOP_DIR on a non-dir has no meaning, so |
1aa4caf3 | 543 | * it's harmless way to add a bit to the first flag byte. */ |
75bc8600 | 544 | if (protocol_version >= 28) { |
61cb031c WD |
545 | if (!xflags && !S_ISDIR(mode)) |
546 | xflags |= XMIT_TOP_DIR; | |
547 | if ((xflags & 0xFF00) || !xflags) { | |
548 | xflags |= XMIT_EXTENDED_FLAGS; | |
549 | write_shortint(f, xflags); | |
75bc8600 | 550 | } else |
61cb031c | 551 | write_byte(f, xflags); |
75bc8600 | 552 | } else { |
61cb031c WD |
553 | if (!(xflags & 0xFF)) |
554 | xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR; | |
555 | write_byte(f, xflags); | |
75bc8600 | 556 | } |
61cb031c | 557 | if (xflags & XMIT_SAME_NAME) |
ebed4c3a | 558 | write_byte(f, l1); |
61cb031c | 559 | if (xflags & XMIT_LONG_NAME) |
f31514ad | 560 | write_varint30(f, l2); |
72914a60 | 561 | else |
ebed4c3a MP |
562 | write_byte(f, l2); |
563 | write_buf(f, fname + l1, l2); | |
72914a60 | 564 | |
0c096e29 | 565 | #ifdef SUPPORT_HARD_LINKS |
8cae71eb | 566 | if (first_hlink_ndx >= 0) { |
9863bdbc | 567 | write_varint(f, first_hlink_ndx); |
28a5b78c | 568 | if (first_hlink_ndx >= first_ndx) |
d4d6646a | 569 | goto the_end; |
8cae71eb | 570 | } |
0c096e29 | 571 | #endif |
8cae71eb | 572 | |
ba59bd68 | 573 | write_varlong30(f, F_LENGTH(file), 3); |
61cb031c | 574 | if (!(xflags & XMIT_SAME_TIME)) { |
ba59bd68 WD |
575 | if (protocol_version >= 30) |
576 | write_varlong(f, modtime, 4); | |
577 | else | |
578 | write_int(f, modtime); | |
579 | } | |
61cb031c | 580 | if (!(xflags & XMIT_SAME_MODE)) |
30f337c9 | 581 | write_int(f, to_wire_mode(mode)); |
236adddc | 582 | if (preserve_uid && !(xflags & XMIT_SAME_UID)) { |
18438f0b WD |
583 | if (protocol_version < 30) |
584 | write_int(f, uid); | |
585 | else { | |
f31514ad | 586 | write_varint(f, uid); |
61cb031c | 587 | if (xflags & XMIT_USER_NAME_FOLLOWS) { |
18438f0b WD |
588 | int len = strlen(user_name); |
589 | write_byte(f, len); | |
590 | write_buf(f, user_name, len); | |
591 | } | |
c7e6f84f | 592 | } |
72914a60 | 593 | } |
236adddc | 594 | if (preserve_gid && !(xflags & XMIT_SAME_GID)) { |
18438f0b WD |
595 | if (protocol_version < 30) |
596 | write_int(f, gid); | |
597 | else { | |
f31514ad | 598 | write_varint(f, gid); |
61cb031c | 599 | if (xflags & XMIT_GROUP_NAME_FOLLOWS) { |
18438f0b WD |
600 | int len = strlen(group_name); |
601 | write_byte(f, len); | |
602 | write_buf(f, group_name, len); | |
603 | } | |
c7e6f84f | 604 | } |
72914a60 | 605 | } |
b5c6a6ae | 606 | if ((preserve_devices && IS_DEVICE(mode)) |
a8e6e148 | 607 | || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) { |
9c5e91f8 | 608 | if (protocol_version < 28) { |
61cb031c | 609 | if (!(xflags & XMIT_SAME_RDEV_pre28)) |
9c5e91f8 WD |
610 | write_int(f, (int)rdev); |
611 | } else { | |
61cb031c | 612 | if (!(xflags & XMIT_SAME_RDEV_MAJOR)) |
f31514ad | 613 | write_varint30(f, major(rdev)); |
18438f0b | 614 | if (protocol_version >= 30) |
f31514ad | 615 | write_varint(f, minor(rdev)); |
61cb031c | 616 | else if (xflags & XMIT_RDEV_MINOR_8_pre30) |
9c5e91f8 WD |
617 | write_byte(f, minor(rdev)); |
618 | else | |
619 | write_int(f, minor(rdev)); | |
620 | } | |
75bc8600 | 621 | } |
c627d613 | 622 | |
4f5b0756 | 623 | #ifdef SUPPORT_LINKS |
6e5b6822 WD |
624 | if (symlink_len) { |
625 | write_varint30(f, symlink_len); | |
626 | write_buf(f, symlink_name, symlink_len); | |
72914a60 | 627 | } |
c627d613 AT |
628 | #endif |
629 | ||
4f5b0756 | 630 | #ifdef SUPPORT_HARD_LINKS |
9d737ecb | 631 | if (tmp_dev != 0 && protocol_version < 30) { |
4124540d | 632 | if (protocol_version < 26) { |
736a6a29 | 633 | /* 32-bit dev_t and ino_t */ |
26404276 | 634 | write_int(f, (int32)dev); |
9d737ecb | 635 | write_int(f, (int32)tmp_ino); |
736a6a29 MP |
636 | } else { |
637 | /* 64-bit dev_t and ino_t */ | |
61cb031c | 638 | if (!(xflags & XMIT_SAME_DEV_pre30)) |
4124540d | 639 | write_longint(f, dev); |
9d737ecb | 640 | write_longint(f, tmp_ino); |
736a6a29 | 641 | } |
72914a60 | 642 | } |
dc5ddbcc AT |
643 | #endif |
644 | ||
9a4a237e | 645 | if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { |
4a19c3b2 | 646 | const char *sum; |
728d0922 | 647 | if (S_ISREG(mode)) |
82ad07c4 | 648 | sum = tmp_sum; |
9a4a237e | 649 | else { |
728d0922 WD |
650 | /* Prior to 28, we sent a useless set of nulls. */ |
651 | sum = empty_sum; | |
728d0922 | 652 | } |
7752df41 | 653 | write_buf(f, sum, checksum_len); |
ebed4c3a | 654 | } |
182dca5c | 655 | |
0c096e29 | 656 | #ifdef SUPPORT_HARD_LINKS |
8cae71eb | 657 | the_end: |
0c096e29 | 658 | #endif |
ebed4c3a | 659 | strlcpy(lastname, fname, MAXPATHLEN); |
ca947dea WD |
660 | |
661 | if (S_ISREG(mode) || S_ISLNK(mode)) | |
662 | stats.total_size += F_LENGTH(file); | |
182dca5c AT |
663 | } |
664 | ||
82ad07c4 | 665 | static struct file_struct *recv_file_entry(struct file_list *flist, |
1564cd5a | 666 | int xflags, int f) |
182dca5c | 667 | { |
f14b3ef4 | 668 | static int64 modtime; |
5911fee5 | 669 | static mode_t mode; |
c126e66e | 670 | #ifdef SUPPORT_HARD_LINKS |
1490812a | 671 | static int64 dev; |
c126e66e | 672 | #endif |
4124540d | 673 | static dev_t rdev; |
9c5e91f8 | 674 | static uint32 rdev_major; |
5911fee5 WD |
675 | static uid_t uid; |
676 | static gid_t gid; | |
cf1b4969 | 677 | static uint16 gid_flags; |
a289addd | 678 | static char lastname[MAXPATHLEN], *lastdir; |
33ffd7c3 | 679 | static int lastdir_depth, lastdir_len = -1; |
42f23f47 | 680 | static unsigned int del_hier_name_len = 0; |
649f8742 | 681 | static int in_del_hier = 0; |
72914a60 | 682 | char thisname[MAXPATHLEN]; |
ebed4c3a | 683 | unsigned int l1 = 0, l2 = 0; |
c7e6f84f | 684 | int alloc_len, basename_len, linkname_len; |
99a957d3 | 685 | int extra_len = file_extra_cnt * EXTRA_LEN; |
8cae71eb | 686 | int first_hlink_ndx = -1; |
4ecf3e06 | 687 | int64 file_length; |
c7e6f84f | 688 | const char *basename; |
72914a60 | 689 | struct file_struct *file; |
ef35abb2 WD |
690 | alloc_pool_t *pool; |
691 | char *bp; | |
72914a60 | 692 | |
1564cd5a | 693 | if (xflags & XMIT_SAME_NAME) |
72914a60 | 694 | l1 = read_byte(f); |
ebed4c3a | 695 | |
1564cd5a | 696 | if (xflags & XMIT_LONG_NAME) |
f31514ad | 697 | l2 = read_varint30(f); |
72914a60 AT |
698 | else |
699 | l2 = read_byte(f); | |
700 | ||
ebed4c3a MP |
701 | if (l2 >= MAXPATHLEN - l1) { |
702 | rprintf(FERROR, | |
1564cd5a WD |
703 | "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n", |
704 | xflags, l1, l2, lastname, who_am_i()); | |
82ad07c4 | 705 | overflow_exit("recv_file_entry"); |
d0fd26aa | 706 | } |
72914a60 | 707 | |
ebed4c3a MP |
708 | strlcpy(thisname, lastname, l1 + 1); |
709 | read_sbuf(f, &thisname[l1], l2); | |
710 | thisname[l1 + l2] = 0; | |
72914a60 | 711 | |
332cf6df WD |
712 | /* Abuse basename_len for a moment... */ |
713 | basename_len = strlcpy(lastname, thisname, MAXPATHLEN); | |
714 | ||
715 | #ifdef ICONV_OPTION | |
716 | if (ic_recv != (iconv_t)-1) { | |
2509753f | 717 | xbuf outbuf, inbuf; |
332cf6df | 718 | |
2509753f | 719 | INIT_CONST_XBUF(outbuf, thisname); |
bb640d32 | 720 | INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1); |
332cf6df | 721 | |
2509753f | 722 | if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) { |
332cf6df | 723 | io_error |= IOERR_GENERAL; |
7c7462cd | 724 | rprintf(FERROR_UTF8, |
332cf6df WD |
725 | "[%s] cannot convert filename: %s (%s)\n", |
726 | who_am_i(), lastname, strerror(errno)); | |
2509753f | 727 | outbuf.len = 0; |
332cf6df | 728 | } |
6e5b6822 | 729 | thisname[outbuf.len] = '\0'; |
332cf6df WD |
730 | } |
731 | #endif | |
72914a60 | 732 | |
7568ff44 WD |
733 | if (*thisname) |
734 | clean_fname(thisname, 0); | |
72914a60 | 735 | |
0d162bd1 | 736 | if (sanitize_paths) |
d48810ba | 737 | sanitize_path(thisname, thisname, "", 0, SP_DEFAULT); |
cb13abfe | 738 | |
a289addd | 739 | if ((basename = strrchr(thisname, '/')) != NULL) { |
c7e6f84f WD |
740 | int len = basename++ - thisname; |
741 | if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) { | |
742 | lastdir = new_array(char, len + 1); | |
743 | memcpy(lastdir, thisname, len); | |
744 | lastdir[len] = '\0'; | |
745 | lastdir_len = len; | |
746 | lastdir_depth = count_dir_elements(lastdir); | |
747 | } | |
748 | } else | |
a289addd | 749 | basename = thisname; |
a289addd | 750 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
72914a60 | 751 | |
8cae71eb WD |
752 | #ifdef SUPPORT_HARD_LINKS |
753 | if (protocol_version >= 30 | |
1564cd5a | 754 | && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) { |
9863bdbc | 755 | first_hlink_ndx = read_varint(f); |
d4d6646a | 756 | if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) { |
8cae71eb WD |
757 | rprintf(FERROR, |
758 | "hard-link reference out of range: %d (%d)\n", | |
d4d6646a | 759 | first_hlink_ndx, flist->ndx_start + flist->used); |
8cae71eb WD |
760 | exit_cleanup(RERR_PROTOCOL); |
761 | } | |
28a5b78c WD |
762 | if (DEBUG_GTE(HLINK, 1)) { |
763 | rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n", | |
764 | who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx, | |
0c096e29 WD |
765 | first_hlink_ndx >= flist->ndx_start ? "" : "un"); |
766 | } | |
d4d6646a WD |
767 | if (first_hlink_ndx >= flist->ndx_start) { |
768 | struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start]; | |
769 | file_length = F_LENGTH(first); | |
770 | modtime = first->modtime; | |
771 | mode = first->mode; | |
236adddc | 772 | if (preserve_uid) |
d4d6646a | 773 | uid = F_OWNER(first); |
236adddc | 774 | if (preserve_gid) |
d4d6646a | 775 | gid = F_GROUP(first); |
a8e6e148 | 776 | if (preserve_devices && IS_DEVICE(mode)) { |
d4d6646a WD |
777 | uint32 *devp = F_RDEV_P(first); |
778 | rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)); | |
779 | extra_len += DEV_EXTRA_CNT * EXTRA_LEN; | |
780 | } | |
781 | if (preserve_links && S_ISLNK(mode)) | |
782 | linkname_len = strlen(F_SYMLINK(first)) + 1; | |
783 | else | |
784 | linkname_len = 0; | |
785 | goto create_object; | |
8cae71eb | 786 | } |
8cae71eb WD |
787 | } |
788 | #endif | |
789 | ||
ba59bd68 | 790 | file_length = read_varlong30(f, 3); |
1564cd5a | 791 | if (!(xflags & XMIT_SAME_TIME)) { |
f14b3ef4 WD |
792 | if (protocol_version >= 30) { |
793 | modtime = read_varlong(f, 4); | |
794 | #if SIZEOF_TIME_T < SIZEOF_INT64 | |
b58f5e17 | 795 | if (!am_generator && (int64)(time_t)modtime != modtime) { |
3f0211b6 | 796 | rprintf(FERROR_XFER, |
f14b3ef4 WD |
797 | "Time value of %s truncated on receiver.\n", |
798 | lastname); | |
799 | } | |
800 | #endif | |
801 | } else | |
802 | modtime = read_int(f); | |
ba59bd68 | 803 | } |
1564cd5a | 804 | if (!(xflags & XMIT_SAME_MODE)) |
30f337c9 | 805 | mode = from_wire_mode(read_int(f)); |
1ef00d20 | 806 | |
4e0fa131 | 807 | if (chmod_modes && !S_ISLNK(mode) && mode) |
8bbe41b5 WD |
808 | mode = tweak_mode(mode, chmod_modes); |
809 | ||
236adddc | 810 | if (preserve_uid && !(xflags & XMIT_SAME_UID)) { |
18438f0b WD |
811 | if (protocol_version < 30) |
812 | uid = (uid_t)read_int(f); | |
813 | else { | |
f31514ad | 814 | uid = (uid_t)read_varint(f); |
1564cd5a | 815 | if (xflags & XMIT_USER_NAME_FOLLOWS) |
18438f0b | 816 | uid = recv_user_name(f, uid); |
2df20057 | 817 | else if (inc_recurse && am_root && (!numeric_ids || usermap)) |
18438f0b WD |
818 | uid = match_uid(uid); |
819 | } | |
c7e6f84f | 820 | } |
236adddc | 821 | if (preserve_gid && !(xflags & XMIT_SAME_GID)) { |
18438f0b WD |
822 | if (protocol_version < 30) |
823 | gid = (gid_t)read_int(f); | |
824 | else { | |
f31514ad | 825 | gid = (gid_t)read_varint(f); |
cf1b4969 | 826 | gid_flags = 0; |
1564cd5a | 827 | if (xflags & XMIT_GROUP_NAME_FOLLOWS) |
cf1b4969 | 828 | gid = recv_group_name(f, gid, &gid_flags); |
2df20057 | 829 | else if (inc_recurse && (!am_root || !numeric_ids || groupmap)) |
cf1b4969 | 830 | gid = match_gid(gid, &gid_flags); |
18438f0b | 831 | } |
c7e6f84f | 832 | } |
a289addd | 833 | |
b5c6a6ae | 834 | if ((preserve_devices && IS_DEVICE(mode)) |
a8e6e148 | 835 | || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) { |
75bc8600 | 836 | if (protocol_version < 28) { |
1564cd5a | 837 | if (!(xflags & XMIT_SAME_RDEV_pre28)) |
b5c6a6ae WD |
838 | rdev = (dev_t)read_int(f); |
839 | } else { | |
9c5e91f8 | 840 | uint32 rdev_minor; |
1564cd5a | 841 | if (!(xflags & XMIT_SAME_RDEV_MAJOR)) |
f31514ad | 842 | rdev_major = read_varint30(f); |
18438f0b | 843 | if (protocol_version >= 30) |
f31514ad | 844 | rdev_minor = read_varint(f); |
1564cd5a | 845 | else if (xflags & XMIT_RDEV_MINOR_8_pre30) |
9c5e91f8 WD |
846 | rdev_minor = read_byte(f); |
847 | else | |
848 | rdev_minor = read_int(f); | |
b4a09b72 | 849 | rdev = MAKEDEV(rdev_major, rdev_minor); |
1ef00d20 | 850 | } |
a8e6e148 WD |
851 | if (IS_DEVICE(mode)) |
852 | extra_len += DEV_EXTRA_CNT * EXTRA_LEN; | |
96293cf9 | 853 | file_length = 0; |
b5c6a6ae | 854 | } else if (protocol_version < 28) |
b4a09b72 | 855 | rdev = MAKEDEV(0, 0); |
72914a60 | 856 | |
4f5b0756 | 857 | #ifdef SUPPORT_LINKS |
30f337c9 | 858 | if (preserve_links && S_ISLNK(mode)) { |
f31514ad | 859 | linkname_len = read_varint30(f) + 1; /* count the '\0' */ |
a289addd WD |
860 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { |
861 | rprintf(FERROR, "overflow: linkname_len=%d\n", | |
862 | linkname_len - 1); | |
82ad07c4 | 863 | overflow_exit("recv_file_entry"); |
9dd891bb | 864 | } |
6e5b6822 WD |
865 | #ifdef ICONV_OPTION |
866 | /* We don't know how much extra room we need to convert | |
87678cef WD |
867 | * the as-yet-unread symlink data, so let's hope that a |
868 | * double-size buffer is plenty. */ | |
91fd15b8 | 869 | if (sender_symlink_iconv) |
87678cef | 870 | linkname_len *= 2; |
6e5b6822 | 871 | #endif |
9585b276 WD |
872 | if (munge_symlinks) |
873 | linkname_len += SYMLINK_PREFIX_LEN; | |
72914a60 | 874 | } |
a289addd | 875 | else |
0d162bd1 | 876 | #endif |
a289addd | 877 | linkname_len = 0; |
0d162bd1 | 878 | |
82ad07c4 | 879 | #ifdef SUPPORT_HARD_LINKS |
8cae71eb | 880 | create_object: |
4785cd43 WD |
881 | if (preserve_hard_links) { |
882 | if (protocol_version < 28 && S_ISREG(mode)) | |
1564cd5a | 883 | xflags |= XMIT_HLINKED; |
cf1b4969 | 884 | if (xflags & XMIT_HLINKED) |
d4d6646a | 885 | extra_len += (inc_recurse+1) * EXTRA_LEN; |
82ad07c4 WD |
886 | } |
887 | #endif | |
888 | ||
1c3344a1 | 889 | #ifdef SUPPORT_ACLS |
d6c9c331 WD |
890 | /* Directories need an extra int32 for the default ACL. */ |
891 | if (preserve_acls && S_ISDIR(mode)) | |
892 | extra_len += EXTRA_LEN; | |
1c3344a1 WD |
893 | #endif |
894 | ||
96293cf9 WD |
895 | if (always_checksum && S_ISREG(mode)) |
896 | extra_len += SUM_EXTRA_CNT * EXTRA_LEN; | |
a289addd | 897 | |
4ecf3e06 | 898 | #if SIZEOF_INT64 >= 8 |
96293cf9 WD |
899 | if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) |
900 | extra_len += EXTRA_LEN; | |
4ecf3e06 WD |
901 | #endif |
902 | if (file_length < 0) { | |
903 | rprintf(FERROR, "Offset underflow: file-length is negative\n"); | |
904 | exit_cleanup(RERR_UNSUPPORTED); | |
905 | } | |
96293cf9 | 906 | |
3ea6e0e7 | 907 | if (inc_recurse && S_ISDIR(mode)) { |
c7e6f84f WD |
908 | if (one_file_system) { |
909 | /* Room to save the dir's device for -x */ | |
83d5e900 | 910 | extra_len += DEV_EXTRA_CNT * EXTRA_LEN; |
c7e6f84f | 911 | } |
ef35abb2 WD |
912 | pool = dir_flist->file_pool; |
913 | } else | |
914 | pool = flist->file_pool; | |
c7e6f84f | 915 | |
b58f5e17 WD |
916 | #if EXTRA_ROUNDING > 0 |
917 | if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN)) | |
918 | extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN; | |
919 | #endif | |
920 | ||
c7e6f84f | 921 | alloc_len = FILE_STRUCT_LEN + extra_len + basename_len |
96293cf9 | 922 | + linkname_len; |
ef35abb2 | 923 | bp = pool_alloc(pool, alloc_len, "recv_file_entry"); |
9935066b | 924 | |
a3e18c76 | 925 | memset(bp, 0, extra_len + FILE_STRUCT_LEN); |
82ad07c4 | 926 | bp += extra_len; |
f5db0993 | 927 | file = (struct file_struct *)bp; |
96293cf9 WD |
928 | bp += FILE_STRUCT_LEN; |
929 | ||
930 | memcpy(bp, basename, basename_len); | |
a289addd | 931 | |
cf1b4969 | 932 | #ifdef SUPPORT_HARD_LINKS |
75f162e4 | 933 | if (xflags & XMIT_HLINKED) |
cf1b4969 WD |
934 | file->flags |= FLAG_HLINKED; |
935 | #endif | |
f14b3ef4 | 936 | file->modtime = (time_t)modtime; |
60af9465 | 937 | file->len32 = (uint32)file_length; |
4ecf3e06 | 938 | #if SIZEOF_INT64 >= 8 |
96293cf9 | 939 | if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) { |
4ecf3e06 WD |
940 | #if SIZEOF_CAPITAL_OFF_T < 8 |
941 | rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n"); | |
942 | exit_cleanup(RERR_UNSUPPORTED); | |
943 | #else | |
96293cf9 WD |
944 | file->flags |= FLAG_LENGTH64; |
945 | OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32); | |
4ecf3e06 | 946 | #endif |
96293cf9 | 947 | } |
4ecf3e06 | 948 | #endif |
a289addd | 949 | file->mode = mode; |
236adddc | 950 | if (preserve_uid) |
f1482c33 | 951 | F_OWNER(file) = uid; |
236adddc | 952 | if (preserve_gid) { |
f1482c33 | 953 | F_GROUP(file) = gid; |
cf1b4969 WD |
954 | file->flags |= gid_flags; |
955 | } | |
f7a76b9c | 956 | if (unsort_ndx) |
9decb4d2 | 957 | F_NDX(file) = flist->used + flist->ndx_start; |
a289addd | 958 | |
c7e6f84f WD |
959 | if (basename != thisname) { |
960 | file->dirname = lastdir; | |
9d737ecb | 961 | F_DEPTH(file) = lastdir_depth + 1; |
f3c3ed44 | 962 | } else |
9d737ecb | 963 | F_DEPTH(file) = 1; |
f3c3ed44 | 964 | |
649f8742 | 965 | if (S_ISDIR(mode)) { |
ee3751c8 | 966 | if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */ |
9d737ecb | 967 | F_DEPTH(file)--; |
ff0284dd | 968 | if (protocol_version >= 30) { |
3f309272 WD |
969 | if (!(xflags & XMIT_NO_CONTENT_DIR)) { |
970 | if (xflags & XMIT_TOP_DIR) | |
971 | file->flags |= FLAG_TOP_DIR; | |
972 | file->flags |= FLAG_CONTENT_DIR; | |
973 | } else if (xflags & XMIT_TOP_DIR) | |
974 | file->flags |= FLAG_IMPLIED_DIR; | |
ff0284dd | 975 | } else if (xflags & XMIT_TOP_DIR) { |
75c51953 | 976 | in_del_hier = recurse; |
9d737ecb | 977 | del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2; |
7a16e122 | 978 | if (relative_paths && del_hier_name_len > 2 |
9cdadbb1 WD |
979 | && lastname[del_hier_name_len-1] == '.' |
980 | && lastname[del_hier_name_len-2] == '/') | |
7a16e122 | 981 | del_hier_name_len -= 2; |
3f309272 | 982 | file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR; |
ee3751c8 | 983 | } else if (in_del_hier) { |
f3c3ed44 WD |
984 | if (!relative_paths || !del_hier_name_len |
985 | || (l1 >= del_hier_name_len | |
9cdadbb1 | 986 | && lastname[del_hier_name_len] == '/')) |
3f309272 | 987 | file->flags |= FLAG_CONTENT_DIR; |
649f8742 WD |
988 | else |
989 | in_del_hier = 0; | |
990 | } | |
991 | } | |
992 | ||
a8e6e148 | 993 | if (preserve_devices && IS_DEVICE(mode)) { |
4785cd43 WD |
994 | uint32 *devp = F_RDEV_P(file); |
995 | DEV_MAJOR(devp) = major(rdev); | |
996 | DEV_MINOR(devp) = minor(rdev); | |
82ad07c4 | 997 | } |
a289addd | 998 | |
4f5b0756 | 999 | #ifdef SUPPORT_LINKS |
a289addd | 1000 | if (linkname_len) { |
cfdb27b0 | 1001 | bp += basename_len; |
d4d6646a | 1002 | if (first_hlink_ndx >= flist->ndx_start) { |
c47e3ee1 | 1003 | struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start]; |
8cae71eb | 1004 | memcpy(bp, F_SYMLINK(first), linkname_len); |
ef3f14e6 | 1005 | } else { |
6e5b6822 WD |
1006 | if (munge_symlinks) { |
1007 | strlcpy(bp, SYMLINK_PREFIX, linkname_len); | |
1008 | bp += SYMLINK_PREFIX_LEN; | |
1009 | linkname_len -= SYMLINK_PREFIX_LEN; | |
1010 | } | |
1011 | #ifdef ICONV_OPTION | |
91fd15b8 | 1012 | if (sender_symlink_iconv) { |
6e5b6822 WD |
1013 | xbuf outbuf, inbuf; |
1014 | ||
1015 | alloc_len = linkname_len; | |
87678cef | 1016 | linkname_len /= 2; |
6e5b6822 | 1017 | |
e34ad4e9 | 1018 | /* Read the symlink data into the end of our double-sized |
6e5b6822 WD |
1019 | * buffer and then convert it into the right spot. */ |
1020 | INIT_XBUF(inbuf, bp + alloc_len - linkname_len, | |
1021 | linkname_len - 1, (size_t)-1); | |
1022 | read_sbuf(f, inbuf.buf, inbuf.len); | |
1023 | INIT_XBUF(outbuf, bp, 0, alloc_len); | |
1024 | ||
1025 | if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) { | |
1026 | io_error |= IOERR_GENERAL; | |
1027 | rprintf(FERROR_XFER, | |
e34ad4e9 | 1028 | "[%s] cannot convert symlink data for: %s (%s)\n", |
6e5b6822 WD |
1029 | who_am_i(), full_fname(thisname), strerror(errno)); |
1030 | bp = (char*)file->basename; | |
1031 | *bp++ = '\0'; | |
1032 | outbuf.len = 0; | |
1033 | } | |
1034 | bp[outbuf.len] = '\0'; | |
1035 | } else | |
1036 | #endif | |
1037 | read_sbuf(f, bp, linkname_len - 1); | |
1038 | if (sanitize_paths && !munge_symlinks && *bp) | |
d48810ba | 1039 | sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT); |
ef3f14e6 | 1040 | } |
a289addd WD |
1041 | } |
1042 | #endif | |
1043 | ||
4f5b0756 | 1044 | #ifdef SUPPORT_HARD_LINKS |
75f162e4 | 1045 | if (preserve_hard_links && xflags & XMIT_HLINKED) { |
8cae71eb | 1046 | if (protocol_version >= 30) { |
a0f29ed8 | 1047 | if (xflags & XMIT_HLINK_FIRST) { |
65b4e4b2 | 1048 | F_HL_GNUM(file) = flist->ndx_start + flist->used; |
a0f29ed8 WD |
1049 | } else |
1050 | F_HL_GNUM(file) = first_hlink_ndx; | |
736a6a29 | 1051 | } else { |
9d737ecb | 1052 | static int32 cnt = 0; |
9863bdbc | 1053 | struct ht_int64_node *np; |
9d737ecb WD |
1054 | int64 ino; |
1055 | int32 ndx; | |
8cae71eb | 1056 | if (protocol_version < 26) { |
9d737ecb WD |
1057 | dev = read_int(f); |
1058 | ino = read_int(f); | |
8cae71eb | 1059 | } else { |
1564cd5a | 1060 | if (!(xflags & XMIT_SAME_DEV_pre30)) |
8cae71eb | 1061 | dev = read_longint(f); |
9d737ecb | 1062 | ino = read_longint(f); |
8cae71eb | 1063 | } |
9863bdbc | 1064 | np = idev_find(dev, ino); |
424d3691 | 1065 | ndx = (int32)(long)np->data - 1; |
9d737ecb WD |
1066 | if (ndx < 0) { |
1067 | ndx = cnt++; | |
424d3691 | 1068 | np->data = (void*)(long)cnt; |
9d737ecb WD |
1069 | } |
1070 | F_HL_GNUM(file) = ndx; | |
736a6a29 | 1071 | } |
72914a60 | 1072 | } |
dc5ddbcc | 1073 | #endif |
ebed4c3a | 1074 | |
96293cf9 WD |
1075 | if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { |
1076 | if (S_ISREG(mode)) | |
83d5e900 | 1077 | bp = F_SUM(file); |
82ad07c4 | 1078 | else { |
fea4db62 | 1079 | /* Prior to 28, we get a useless set of nulls. */ |
82ad07c4 | 1080 | bp = tmp_sum; |
fea4db62 | 1081 | } |
d4d6646a | 1082 | if (first_hlink_ndx >= flist->ndx_start) { |
c47e3ee1 | 1083 | struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start]; |
8cae71eb WD |
1084 | memcpy(bp, F_SUM(first), checksum_len); |
1085 | } else | |
1086 | read_buf(f, bp, checksum_len); | |
72914a60 | 1087 | } |
ebed4c3a | 1088 | |
1c3344a1 WD |
1089 | #ifdef SUPPORT_ACLS |
1090 | if (preserve_acls && !S_ISLNK(mode)) | |
1091 | receive_acl(file, f); | |
1092 | #endif | |
16edf865 WD |
1093 | #ifdef SUPPORT_XATTRS |
1094 | if (preserve_xattrs) | |
1095 | receive_xattr(file, f ); | |
1096 | #endif | |
1c3344a1 | 1097 | |
ca947dea WD |
1098 | if (S_ISREG(mode) || S_ISLNK(mode)) |
1099 | stats.total_size += file_length; | |
1100 | ||
f5db0993 | 1101 | return file; |
c627d613 AT |
1102 | } |
1103 | ||
2f188c8d WD |
1104 | /* Create a file_struct for a named file by reading its stat() information |
1105 | * and performing extensive checks against global options. | |
db719fb0 | 1106 | * |
2f188c8d | 1107 | * Returns a pointer to the new file struct, or NULL if there was an error |
2171b939 MM |
1108 | * or this file should be excluded. |
1109 | * | |
1110 | * Note: Any error (here or in send_file_name) that results in the omission of | |
1111 | * an existent source file from the file list should set | |
1112 | * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the | |
1113 | * destination if --delete is on. */ | |
45d8bfe0 | 1114 | struct file_struct *make_file(const char *fname, struct file_list *flist, |
82ad07c4 | 1115 | STRUCT_STAT *stp, int flags, int filter_level) |
c627d613 | 1116 | { |
a289addd WD |
1117 | static char *lastdir; |
1118 | static int lastdir_len = -1; | |
3ec4dd97 | 1119 | struct file_struct *file; |
1923b1fc | 1120 | char thisname[MAXPATHLEN]; |
e0870f1d | 1121 | char linkname[MAXPATHLEN]; |
c7e6f84f | 1122 | int alloc_len, basename_len, linkname_len; |
99a957d3 | 1123 | int extra_len = file_extra_cnt * EXTRA_LEN; |
c7e6f84f | 1124 | const char *basename; |
8ea07c00 | 1125 | alloc_pool_t *pool; |
2f188c8d | 1126 | STRUCT_STAT st; |
c7e6f84f | 1127 | char *bp; |
9935066b | 1128 | |
b3b32601 | 1129 | if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) { |
2171b939 | 1130 | io_error |= IOERR_GENERAL; |
342bfb5e | 1131 | rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname); |
882e6893 WD |
1132 | return NULL; |
1133 | } | |
58b1999e | 1134 | clean_fname(thisname, 0); |
b7736c79 | 1135 | if (sanitize_paths) |
d48810ba | 1136 | sanitize_path(thisname, thisname, "", 0, SP_DEFAULT); |
3ec4dd97 | 1137 | |
ce66f417 WD |
1138 | if (stp && (S_ISDIR(stp->st_mode) || stp->st_mode == 0)) { |
1139 | /* This is needed to handle a "symlink/." with a --relative | |
1140 | * dir, or a request to delete a specific file. */ | |
1141 | st = *stp; | |
e4fdf1de WD |
1142 | *linkname = '\0'; /* make IBM code checker happy */ |
1143 | } else if (readlink_stat(thisname, &st, linkname) != 0) { | |
76e26e10 | 1144 | int save_errno = errno; |
a4a7e64c | 1145 | /* See if file is excluded before reporting an error. */ |
7842418b | 1146 | if (filter_level != NO_FILTERS |
60cc01a6 | 1147 | && (is_excluded(thisname, 0, filter_level) |
53039410 WD |
1148 | || is_excluded(thisname, 1, filter_level))) { |
1149 | if (ignore_perishable && save_errno != ENOENT) | |
1150 | non_perishable_cnt++; | |
a4a7e64c | 1151 | return NULL; |
53039410 | 1152 | } |
a4a7e64c | 1153 | if (save_errno == ENOENT) { |
4f5b0756 | 1154 | #ifdef SUPPORT_LINKS |
21897ecb WD |
1155 | /* When our options tell us to follow a symlink that |
1156 | * points nowhere, tell the user about the symlink | |
1157 | * instead of giving a "vanished" message. We only | |
1158 | * dereference a symlink if one of the --copy*links | |
1159 | * options was specified, so there's no need for the | |
1160 | * extra lstat() if one of these options isn't on. */ | |
1161 | if ((copy_links || copy_unsafe_links || copy_dirlinks) | |
1162 | && x_lstat(thisname, &st, NULL) == 0 | |
1163 | && S_ISLNK(st.st_mode)) { | |
a4a7e64c | 1164 | io_error |= IOERR_GENERAL; |
3f0211b6 | 1165 | rprintf(FERROR_XFER, "symlink has no referent: %s\n", |
a4a7e64c | 1166 | full_fname(thisname)); |
e5ce3bcf WD |
1167 | } else |
1168 | #endif | |
1169 | { | |
a4a7e64c | 1170 | enum logcode c = am_daemon && protocol_version < 28 |
3f0211b6 | 1171 | ? FERROR : FWARNING; |
a4a7e64c WD |
1172 | io_error |= IOERR_VANISHED; |
1173 | rprintf(c, "file has vanished: %s\n", | |
1174 | full_fname(thisname)); | |
76e26e10 | 1175 | } |
a4a7e64c | 1176 | } else { |
a8726d2a | 1177 | io_error |= IOERR_GENERAL; |
c78e4ea9 | 1178 | rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed", |
d62bcc17 | 1179 | full_fname(thisname)); |
76e26e10 | 1180 | } |
3ec4dd97 | 1181 | return NULL; |
ce66f417 WD |
1182 | } else if (st.st_mode == 0) { |
1183 | io_error |= IOERR_GENERAL; | |
1184 | rprintf(FINFO, "skipping file with bogus (zero) st_mode: %s\n", | |
1185 | full_fname(thisname)); | |
1186 | return NULL; | |
3ec4dd97 | 1187 | } |
c627d613 | 1188 | |
7842418b WD |
1189 | if (filter_level == NO_FILTERS) |
1190 | goto skip_filters; | |
ac1a0994 | 1191 | |
75f162e4 WD |
1192 | if (S_ISDIR(st.st_mode)) { |
1193 | if (!xfer_dirs) { | |
1194 | rprintf(FINFO, "skipping directory %s\n", thisname); | |
1195 | return NULL; | |
1196 | } | |
40e38376 WD |
1197 | /* -x only affects dirs because we need to avoid recursing |
1198 | * into a mount-point directory, not to avoid copying a | |
1199 | * symlinked file if -L (or similar) was specified. */ | |
53ec55a8 WD |
1200 | if (one_file_system && st.st_dev != filesystem_dev |
1201 | && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) { | |
1202 | if (one_file_system > 1) { | |
951e826b | 1203 | if (INFO_GTE(MOUNT, 1)) { |
53ec55a8 WD |
1204 | rprintf(FINFO, |
1205 | "[%s] skipping mount-point dir %s\n", | |
1206 | who_am_i(), thisname); | |
3932c423 | 1207 | } |
53ec55a8 | 1208 | return NULL; |
ebec5eb6 | 1209 | } |
53ec55a8 WD |
1210 | flags |= FLAG_MOUNT_DIR; |
1211 | flags &= ~FLAG_CONTENT_DIR; | |
ebec5eb6 | 1212 | } |
40e38376 | 1213 | } else |
3f309272 | 1214 | flags &= ~FLAG_CONTENT_DIR; |
ebed4c3a | 1215 | |
0d9eba03 | 1216 | if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) { |
53039410 WD |
1217 | if (ignore_perishable) |
1218 | non_perishable_cnt++; | |
76e26e10 | 1219 | return NULL; |
53039410 | 1220 | } |
76e26e10 | 1221 | |
132fcf36 | 1222 | if (lp_ignore_nonreadable(module_id)) { |
4f5b0756 | 1223 | #ifdef SUPPORT_LINKS |
132fcf36 WD |
1224 | if (!S_ISLNK(st.st_mode)) |
1225 | #endif | |
1226 | if (access(thisname, R_OK) != 0) | |
1227 | return NULL; | |
1228 | } | |
ac1a0994 | 1229 | |
97b7bff4 | 1230 | skip_filters: |
ac1a0994 | 1231 | |
c7e6f84f | 1232 | /* Only divert a directory in the main transfer. */ |
8ea07c00 WD |
1233 | if (flist) { |
1234 | if (flist->prev && S_ISDIR(st.st_mode) | |
1235 | && flags & FLAG_DIVERT_DIRS) { | |
1236 | /* Room for parent/sibling/next-child info. */ | |
83d5e900 | 1237 | extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN; |
3932c423 WD |
1238 | if (relative_paths) |
1239 | extra_len += PTR_EXTRA_CNT * EXTRA_LEN; | |
8ea07c00 WD |
1240 | pool = dir_flist->file_pool; |
1241 | } else | |
1242 | pool = flist->file_pool; | |
d6c9c331 WD |
1243 | } else { |
1244 | #ifdef SUPPORT_ACLS | |
1245 | /* Directories need an extra int32 for the default ACL. */ | |
1246 | if (preserve_acls && S_ISDIR(st.st_mode)) | |
1247 | extra_len += EXTRA_LEN; | |
1248 | #endif | |
8ea07c00 | 1249 | pool = NULL; |
d6c9c331 | 1250 | } |
c7e6f84f | 1251 | |
951e826b | 1252 | if (DEBUG_GTE(FLIST, 2)) { |
ea847c62 | 1253 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", |
0ee6ca98 | 1254 | who_am_i(), thisname, filter_level); |
ea847c62 | 1255 | } |
ebed4c3a | 1256 | |
a289addd | 1257 | if ((basename = strrchr(thisname, '/')) != NULL) { |
c7e6f84f WD |
1258 | int len = basename++ - thisname; |
1259 | if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) { | |
1260 | lastdir = new_array(char, len + 1); | |
1261 | memcpy(lastdir, thisname, len); | |
1262 | lastdir[len] = '\0'; | |
1263 | lastdir_len = len; | |
1264 | } | |
1265 | } else | |
a289addd | 1266 | basename = thisname; |
a289addd | 1267 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
c627d613 | 1268 | |
4f5b0756 | 1269 | #ifdef SUPPORT_LINKS |
a289addd WD |
1270 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; |
1271 | #else | |
1272 | linkname_len = 0; | |
1273 | #endif | |
1274 | ||
4ecf3e06 | 1275 | #if SIZEOF_CAPITAL_OFF_T >= 8 |
96293cf9 WD |
1276 | if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) |
1277 | extra_len += EXTRA_LEN; | |
4ecf3e06 | 1278 | #endif |
96293cf9 | 1279 | |
886df221 WD |
1280 | if (always_checksum && am_sender && S_ISREG(st.st_mode)) { |
1281 | file_checksum(thisname, tmp_sum, st.st_size); | |
1282 | if (sender_keeps_checksum) | |
1283 | extra_len += SUM_EXTRA_CNT * EXTRA_LEN; | |
1284 | } | |
1285 | ||
a3e18c76 WD |
1286 | #if EXTRA_ROUNDING > 0 |
1287 | if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN)) | |
1288 | extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN; | |
1289 | #endif | |
1290 | ||
c7e6f84f | 1291 | alloc_len = FILE_STRUCT_LEN + extra_len + basename_len |
96293cf9 | 1292 | + linkname_len; |
8ea07c00 WD |
1293 | if (pool) |
1294 | bp = pool_alloc(pool, alloc_len, "make_file"); | |
99aaa6ca | 1295 | else { |
9935066b | 1296 | if (!(bp = new_array(char, alloc_len))) |
99aaa6ca | 1297 | out_of_memory("make_file"); |
9935066b S |
1298 | } |
1299 | ||
a3e18c76 | 1300 | memset(bp, 0, extra_len + FILE_STRUCT_LEN); |
82ad07c4 | 1301 | bp += extra_len; |
a289addd | 1302 | file = (struct file_struct *)bp; |
96293cf9 WD |
1303 | bp += FILE_STRUCT_LEN; |
1304 | ||
1305 | memcpy(bp, basename, basename_len); | |
82ad07c4 WD |
1306 | |
1307 | #ifdef SUPPORT_HARD_LINKS | |
c7e6f84f | 1308 | if (preserve_hard_links && flist && flist->prev) { |
82ad07c4 WD |
1309 | if (protocol_version >= 28 |
1310 | ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1) | |
1311 | : S_ISREG(st.st_mode)) { | |
c8b823f9 WD |
1312 | tmp_dev = (int64)st.st_dev + 1; |
1313 | tmp_ino = (int64)st.st_ino; | |
82ad07c4 | 1314 | } else |
9d737ecb | 1315 | tmp_dev = 0; |
82ad07c4 WD |
1316 | } |
1317 | #endif | |
a289addd | 1318 | |
96293cf9 | 1319 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
a8e6e148 | 1320 | if (IS_DEVICE(st.st_mode)) { |
96293cf9 WD |
1321 | tmp_rdev = st.st_rdev; |
1322 | st.st_size = 0; | |
a8e6e148 WD |
1323 | } else if (IS_SPECIAL(st.st_mode)) |
1324 | st.st_size = 0; | |
96293cf9 WD |
1325 | #endif |
1326 | ||
a289addd | 1327 | file->flags = flags; |
3ec4dd97 | 1328 | file->modtime = st.st_mtime; |
60af9465 | 1329 | file->len32 = (uint32)st.st_size; |
4ecf3e06 | 1330 | #if SIZEOF_CAPITAL_OFF_T >= 8 |
96293cf9 WD |
1331 | if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) { |
1332 | file->flags |= FLAG_LENGTH64; | |
1333 | OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32); | |
1334 | } | |
4ecf3e06 | 1335 | #endif |
8bbe41b5 | 1336 | file->mode = st.st_mode; |
236adddc | 1337 | if (uid_ndx) /* Check uid_ndx instead of preserve_uid for del support */ |
f1482c33 | 1338 | F_OWNER(file) = st.st_uid; |
236adddc | 1339 | if (gid_ndx) /* Check gid_ndx instead of preserve_gid for del support */ |
f1482c33 | 1340 | F_GROUP(file) = st.st_gid; |
61dec11a | 1341 | |
c7e6f84f WD |
1342 | if (basename != thisname) |
1343 | file->dirname = lastdir; | |
a289addd | 1344 | |
4f5b0756 | 1345 | #ifdef SUPPORT_LINKS |
d6c9c331 | 1346 | if (linkname_len) |
cfdb27b0 | 1347 | memcpy(bp + basename_len, linkname, linkname_len); |
0d162bd1 WD |
1348 | #endif |
1349 | ||
d6c9c331 WD |
1350 | if (am_sender) |
1351 | F_PATHNAME(file) = pathname; | |
1352 | else if (!pool) | |
1353 | F_DEPTH(file) = extra_len / EXTRA_LEN; | |
c627d613 | 1354 | |
a7188cbf WD |
1355 | if (basename_len == 0+1) { |
1356 | if (!pool) | |
1357 | unmake_file(file); | |
82ad07c4 | 1358 | return NULL; |
a7188cbf | 1359 | } |
82ad07c4 | 1360 | |
886df221 WD |
1361 | if (sender_keeps_checksum && S_ISREG(st.st_mode)) |
1362 | memcpy(F_SUM(file), tmp_sum, checksum_len); | |
1363 | ||
f7a76b9c | 1364 | if (unsort_ndx) |
e366e530 | 1365 | F_NDX(file) = stats.num_dirs; |
c7e6f84f | 1366 | |
3ec4dd97 | 1367 | return file; |
c627d613 AT |
1368 | } |
1369 | ||
17026f27 | 1370 | /* Only called for temporary file_struct entries created by make_file(). */ |
82ad07c4 WD |
1371 | void unmake_file(struct file_struct *file) |
1372 | { | |
d6c9c331 | 1373 | free(REQ_EXTRA(file, F_DEPTH(file))); |
82ad07c4 WD |
1374 | } |
1375 | ||
42c6b139 | 1376 | static struct file_struct *send_file_name(int f, struct file_list *flist, |
7568ff44 | 1377 | const char *fname, STRUCT_STAT *stp, |
252af65b | 1378 | int flags, int filter_level) |
c627d613 | 1379 | { |
ebed4c3a MP |
1380 | struct file_struct *file; |
1381 | ||
252af65b | 1382 | file = make_file(fname, flist, stp, flags, filter_level); |
37802f40 | 1383 | if (!file) |
301fb56c | 1384 | return NULL; |
ebed4c3a | 1385 | |
4e0fa131 | 1386 | if (chmod_modes && !S_ISLNK(file->mode) && file->mode) |
8bbe41b5 WD |
1387 | file->mode = tweak_mode(file->mode, chmod_modes); |
1388 | ||
7568ff44 WD |
1389 | if (f >= 0) { |
1390 | char fbuf[MAXPATHLEN]; | |
6e5b6822 WD |
1391 | #ifdef SUPPORT_LINKS |
1392 | const char *symlink_name; | |
1393 | int symlink_len; | |
1394 | #ifdef ICONV_OPTION | |
1395 | char symlink_buf[MAXPATHLEN]; | |
1396 | #endif | |
1397 | #endif | |
7568ff44 WD |
1398 | #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS |
1399 | stat_x sx; | |
09ca0d15 | 1400 | init_stat_x(&sx); |
7568ff44 WD |
1401 | #endif |
1402 | ||
6e5b6822 WD |
1403 | #ifdef SUPPORT_LINKS |
1404 | if (preserve_links && S_ISLNK(file->mode)) { | |
1405 | symlink_name = F_SYMLINK(file); | |
1406 | symlink_len = strlen(symlink_name); | |
6e806137 WD |
1407 | if (symlink_len == 0) { |
1408 | io_error |= IOERR_GENERAL; | |
1409 | f_name(file, fbuf); | |
1410 | rprintf(FERROR_XFER, | |
1411 | "skipping symlink with 0-length value: %s\n", | |
1412 | full_fname(fbuf)); | |
1413 | return NULL; | |
1414 | } | |
6e5b6822 WD |
1415 | } else { |
1416 | symlink_name = NULL; | |
1417 | symlink_len = 0; | |
1418 | } | |
1419 | #endif | |
1420 | ||
7568ff44 WD |
1421 | #ifdef ICONV_OPTION |
1422 | if (ic_send != (iconv_t)-1) { | |
1423 | xbuf outbuf, inbuf; | |
1424 | ||
1425 | INIT_CONST_XBUF(outbuf, fbuf); | |
1426 | ||
1427 | if (file->dirname) { | |
1428 | INIT_XBUF_STRLEN(inbuf, (char*)file->dirname); | |
1429 | outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */ | |
1430 | if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) | |
1431 | goto convert_error; | |
1432 | outbuf.size += 2; | |
6e5b6822 | 1433 | fbuf[outbuf.len++] = '/'; |
7568ff44 WD |
1434 | } |
1435 | ||
1436 | INIT_XBUF_STRLEN(inbuf, (char*)file->basename); | |
1437 | if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) { | |
1438 | convert_error: | |
1439 | io_error |= IOERR_GENERAL; | |
342bfb5e | 1440 | rprintf(FERROR_XFER, |
7568ff44 WD |
1441 | "[%s] cannot convert filename: %s (%s)\n", |
1442 | who_am_i(), f_name(file, fbuf), strerror(errno)); | |
1443 | return NULL; | |
1444 | } | |
6e5b6822 WD |
1445 | fbuf[outbuf.len] = '\0'; |
1446 | ||
1447 | #ifdef SUPPORT_LINKS | |
91fd15b8 | 1448 | if (symlink_len && sender_symlink_iconv) { |
6e5b6822 WD |
1449 | INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1); |
1450 | INIT_CONST_XBUF(outbuf, symlink_buf); | |
1451 | if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) { | |
1452 | io_error |= IOERR_GENERAL; | |
1453 | f_name(file, fbuf); | |
1454 | rprintf(FERROR_XFER, | |
e34ad4e9 | 1455 | "[%s] cannot convert symlink data for: %s (%s)\n", |
6e5b6822 WD |
1456 | who_am_i(), full_fname(fbuf), strerror(errno)); |
1457 | return NULL; | |
1458 | } | |
1459 | symlink_buf[outbuf.len] = '\0'; | |
1460 | ||
1461 | symlink_name = symlink_buf; | |
1462 | symlink_len = outbuf.len; | |
1463 | } | |
1464 | #endif | |
7568ff44 WD |
1465 | } else |
1466 | #endif | |
1467 | f_name(file, fbuf); | |
1468 | ||
1c3344a1 | 1469 | #ifdef SUPPORT_ACLS |
7568ff44 WD |
1470 | if (preserve_acls && !S_ISLNK(file->mode)) { |
1471 | sx.st.st_mode = file->mode; | |
2171b939 MM |
1472 | if (get_acl(fname, &sx) < 0) { |
1473 | io_error |= IOERR_GENERAL; | |
7568ff44 | 1474 | return NULL; |
2171b939 | 1475 | } |
7568ff44 | 1476 | } |
1c3344a1 | 1477 | #endif |
16edf865 | 1478 | #ifdef SUPPORT_XATTRS |
7568ff44 | 1479 | if (preserve_xattrs) { |
0d5ebab1 | 1480 | sx.st.st_mode = file->mode; |
2171b939 MM |
1481 | if (get_xattr(fname, &sx) < 0) { |
1482 | io_error |= IOERR_GENERAL; | |
7568ff44 | 1483 | return NULL; |
2171b939 | 1484 | } |
7568ff44 | 1485 | } |
16edf865 | 1486 | #endif |
1c3344a1 | 1487 | |
6e5b6822 WD |
1488 | send_file_entry(f, fbuf, file, |
1489 | #ifdef SUPPORT_LINKS | |
1490 | symlink_name, symlink_len, | |
1491 | #endif | |
1492 | flist->used, flist->ndx_start); | |
ebed4c3a | 1493 | |
1c3344a1 WD |
1494 | #ifdef SUPPORT_ACLS |
1495 | if (preserve_acls && !S_ISLNK(file->mode)) { | |
1496 | send_acl(&sx, f); | |
1497 | free_acl(&sx); | |
1498 | } | |
16edf865 WD |
1499 | #endif |
1500 | #ifdef SUPPORT_XATTRS | |
1501 | if (preserve_xattrs) { | |
1502 | F_XATTR(file) = send_xattr(&sx, f); | |
1503 | free_xattr(&sx); | |
1504 | } | |
1c3344a1 WD |
1505 | #endif |
1506 | } | |
7568ff44 WD |
1507 | |
1508 | maybe_emit_filelist_progress(flist->used + flist_count_offset); | |
1509 | ||
1510 | flist_expand(flist, 1); | |
1511 | flist->files[flist->used++] = file; | |
1512 | ||
301fb56c WD |
1513 | return file; |
1514 | } | |
ebed4c3a | 1515 | |
301fb56c | 1516 | static void send_if_directory(int f, struct file_list *flist, |
56f0c976 | 1517 | struct file_struct *file, |
c7e6f84f WD |
1518 | char *fbuf, unsigned int ol, |
1519 | int flags) | |
301fb56c | 1520 | { |
56f0c976 | 1521 | char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/'); |
301fb56c WD |
1522 | |
1523 | if (S_ISDIR(file->mode) | |
82ad07c4 | 1524 | && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) { |
f1773e09 WD |
1525 | void *save_filters; |
1526 | unsigned int len = strlen(fbuf); | |
1527 | if (len > 1 && fbuf[len-1] == '/') | |
1528 | fbuf[--len] = '\0'; | |
1529 | if (len >= MAXPATHLEN - 1) { | |
1530 | io_error |= IOERR_GENERAL; | |
3f0211b6 | 1531 | rprintf(FERROR_XFER, "skipping long-named directory: %s\n", |
f1773e09 WD |
1532 | full_fname(fbuf)); |
1533 | return; | |
1534 | } | |
1535 | save_filters = push_local_filters(fbuf, len); | |
a6e7b978 | 1536 | send_directory(f, flist, fbuf, len, flags); |
f1773e09 | 1537 | pop_local_filters(save_filters); |
56f0c976 WD |
1538 | fbuf[ol] = '\0'; |
1539 | if (is_dot_dir) | |
1540 | fbuf[ol-1] = '.'; | |
ebed4c3a | 1541 | } |
c627d613 AT |
1542 | } |
1543 | ||
a572e126 WD |
1544 | static int file_compare(const void *file1, const void *file2) |
1545 | { | |
1546 | return f_name_cmp(*(struct file_struct **)file1, | |
1547 | *(struct file_struct **)file2); | |
1548 | } | |
1549 | ||
56ce72c4 | 1550 | /* The guts of a merge-sort algorithm. This was derived from the glibc |
a572e126 WD |
1551 | * version, but I (Wayne) changed the merge code to do less copying and |
1552 | * to require only half the amount of temporary memory. */ | |
1553 | static void fsort_tmp(struct file_struct **fp, size_t num, | |
1554 | struct file_struct **tmp) | |
1555 | { | |
1556 | struct file_struct **f1, **f2, **t; | |
1557 | size_t n1, n2; | |
1558 | ||
1559 | n1 = num / 2; | |
1560 | n2 = num - n1; | |
1561 | f1 = fp; | |
1562 | f2 = fp + n1; | |
1563 | ||
1564 | if (n1 > 1) | |
1565 | fsort_tmp(f1, n1, tmp); | |
1566 | if (n2 > 1) | |
1567 | fsort_tmp(f2, n2, tmp); | |
1568 | ||
1569 | while (f_name_cmp(*f1, *f2) <= 0) { | |
1570 | if (!--n1) | |
1571 | return; | |
1572 | f1++; | |
1573 | } | |
1574 | ||
1575 | t = tmp; | |
1576 | memcpy(t, f1, n1 * PTR_SIZE); | |
1577 | ||
1578 | *f1++ = *f2++, n2--; | |
1579 | ||
1580 | while (n1 > 0 && n2 > 0) { | |
1581 | if (f_name_cmp(*t, *f2) <= 0) | |
1582 | *f1++ = *t++, n1--; | |
1583 | else | |
1584 | *f1++ = *f2++, n2--; | |
1585 | } | |
1586 | ||
1587 | if (n1 > 0) | |
1588 | memcpy(f1, t, n1 * PTR_SIZE); | |
1589 | } | |
1590 | ||
1591 | /* This file-struct sorting routine makes sure that any identical names in | |
1592 | * the file list stay in the same order as they were in the original list. | |
1593 | * This is particularly vital in inc_recurse mode where we expect a sort | |
1594 | * on the flist to match the exact order of a sort on the dir_flist. */ | |
1595 | static void fsort(struct file_struct **fp, size_t num) | |
c7e6f84f | 1596 | { |
a572e126 WD |
1597 | if (num <= 1) |
1598 | return; | |
1599 | ||
1600 | if (use_qsort) | |
1601 | qsort(fp, num, PTR_SIZE, file_compare); | |
1602 | else { | |
1603 | struct file_struct **tmp = new_array(struct file_struct *, | |
1604 | (num+1) / 2); | |
1605 | fsort_tmp(fp, num, tmp); | |
1606 | free(tmp); | |
1607 | } | |
c7e6f84f WD |
1608 | } |
1609 | ||
8ea07c00 WD |
1610 | /* We take an entire set of sibling dirs from the sorted flist and link them |
1611 | * into the tree, setting the appropriate parent/child/sibling pointers. */ | |
1612 | static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist, | |
1613 | int dir_cnt) | |
c7e6f84f WD |
1614 | { |
1615 | int i; | |
1616 | int32 *dp = NULL; | |
1617 | int32 *parent_dp = parent_ndx < 0 ? NULL | |
83d5e900 | 1618 | : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]); |
c7e6f84f | 1619 | |
8ea07c00 WD |
1620 | flist_expand(dir_flist, dir_cnt); |
1621 | dir_flist->sorted = dir_flist->files; | |
c7e6f84f | 1622 | |
8ea07c00 WD |
1623 | for (i = 0; dir_cnt; i++) { |
1624 | struct file_struct *file = from_flist->sorted[i]; | |
1625 | ||
1626 | if (!S_ISDIR(file->mode)) | |
1627 | continue; | |
1628 | ||
9decb4d2 | 1629 | dir_flist->files[dir_flist->used++] = file; |
8ea07c00 | 1630 | dir_cnt--; |
ae87c434 | 1631 | |
4e421735 WD |
1632 | if (file->basename[0] == '.' && file->basename[1] == '\0') |
1633 | continue; | |
1634 | ||
c7e6f84f | 1635 | if (dp) |
9decb4d2 | 1636 | DIR_NEXT_SIBLING(dp) = dir_flist->used - 1; |
c7e6f84f | 1637 | else if (parent_dp) |
9decb4d2 | 1638 | DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1; |
c7e6f84f | 1639 | else |
9decb4d2 | 1640 | send_dir_ndx = dir_flist->used - 1; |
8ea07c00 | 1641 | |
83d5e900 | 1642 | dp = F_DIR_NODE_P(file); |
c7e6f84f WD |
1643 | DIR_PARENT(dp) = parent_ndx; |
1644 | DIR_FIRST_CHILD(dp) = -1; | |
1645 | } | |
1646 | if (dp) | |
1647 | DIR_NEXT_SIBLING(dp) = -1; | |
1648 | } | |
1649 | ||
d39d60a1 WD |
1650 | static void interpret_stat_error(const char *fname, int is_dir) |
1651 | { | |
1652 | if (errno == ENOENT) { | |
1653 | io_error |= IOERR_VANISHED; | |
1654 | rprintf(FWARNING, "%s has vanished: %s\n", | |
1655 | is_dir ? "directory" : "file", full_fname(fname)); | |
1656 | } else { | |
1657 | io_error |= IOERR_GENERAL; | |
1658 | rsyserr(FERROR_XFER, errno, "link_stat %s failed", | |
1659 | full_fname(fname)); | |
1660 | } | |
1661 | } | |
1662 | ||
301fb56c | 1663 | /* This function is normally called by the sender, but the receiving side also |
7b558d7f | 1664 | * calls it from get_dirlist() with f set to -1 so that we just construct the |
301fb56c WD |
1665 | * file list in memory without sending it over the wire. Also, get_dirlist() |
1666 | * might call this with f set to -2, which also indicates that local filter | |
1667 | * rules should be ignored. */ | |
a6e7b978 WD |
1668 | static void send_directory(int f, struct file_list *flist, char *fbuf, int len, |
1669 | int flags) | |
c627d613 | 1670 | { |
3ec4dd97 | 1671 | struct dirent *di; |
32cbfe7b | 1672 | unsigned remainder; |
3ec4dd97 | 1673 | char *p; |
f1773e09 | 1674 | DIR *d; |
c7e6f84f | 1675 | int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0; |
9decb4d2 | 1676 | int start = flist->used; |
252af65b | 1677 | int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS; |
c7e6f84f WD |
1678 | |
1679 | assert(flist != NULL); | |
3ec4dd97 | 1680 | |
f1773e09 | 1681 | if (!(d = opendir(fbuf))) { |
d39d60a1 WD |
1682 | if (errno == ENOENT) { |
1683 | if (am_sender) /* Can abuse this for vanished error w/ENOENT: */ | |
1684 | interpret_stat_error(fbuf, True); | |
c43c6612 | 1685 | return; |
d39d60a1 | 1686 | } |
06c28400 | 1687 | io_error |= IOERR_GENERAL; |
3f0211b6 | 1688 | rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf)); |
3ec4dd97 AT |
1689 | return; |
1690 | } | |
c627d613 | 1691 | |
19b2a5d9 WD |
1692 | p = fbuf + len; |
1693 | if (len != 1 || *fbuf != '/') | |
eddd5d12 | 1694 | *p++ = '/'; |
f1773e09 | 1695 | *p = '\0'; |
32cbfe7b | 1696 | remainder = MAXPATHLEN - (p - fbuf); |
ebed4c3a | 1697 | |
6a7cc46c | 1698 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 1699 | char *dname = d_name(di); |
6a7cc46c S |
1700 | if (dname[0] == '.' && (dname[1] == '\0' |
1701 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 1702 | continue; |
0ee6ca98 | 1703 | if (strlcpy(p, dname, remainder) >= remainder) { |
eddd5d12 | 1704 | io_error |= IOERR_GENERAL; |
342bfb5e | 1705 | rprintf(FERROR_XFER, |
eddd5d12 | 1706 | "cannot send long-named file %s\n", |
f1773e09 | 1707 | full_fname(fbuf)); |
beaf4954 | 1708 | continue; |
eddd5d12 | 1709 | } |
411c04f0 WD |
1710 | if (dname[0] == '\0') { |
1711 | io_error |= IOERR_GENERAL; | |
342bfb5e | 1712 | rprintf(FERROR_XFER, |
411c04f0 WD |
1713 | "cannot send file with empty name in %s\n", |
1714 | full_fname(fbuf)); | |
1715 | continue; | |
1716 | } | |
0ee6ca98 | 1717 | |
252af65b | 1718 | send_file_name(f, flist, fbuf, NULL, flags, filter_level); |
3ec4dd97 | 1719 | } |
32cbfe7b WD |
1720 | |
1721 | fbuf[len] = '\0'; | |
1722 | ||
6a7cc46c | 1723 | if (errno) { |
06c28400 | 1724 | io_error |= IOERR_GENERAL; |
3f0211b6 | 1725 | rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf)); |
6a7cc46c | 1726 | } |
c627d613 | 1727 | |
3ec4dd97 | 1728 | closedir(d); |
301fb56c | 1729 | |
a6e7b978 | 1730 | if (f >= 0 && recurse && !divert_dirs) { |
9decb4d2 WD |
1731 | int i, end = flist->used - 1; |
1732 | /* send_if_directory() bumps flist->used, so use "end". */ | |
301fb56c | 1733 | for (i = start; i <= end; i++) |
c7e6f84f WD |
1734 | send_if_directory(f, flist, flist->files[i], fbuf, len, flags); |
1735 | } | |
1736 | } | |
1737 | ||
3932c423 | 1738 | static void send_implied_dirs(int f, struct file_list *flist, char *fname, |
4e421735 | 1739 | char *start, char *limit, int flags, char name_type) |
3932c423 | 1740 | { |
af3172c1 WD |
1741 | static char lastpath[MAXPATHLEN] = ""; |
1742 | static int lastpath_len = 0; | |
1743 | static struct file_struct *lastpath_struct = NULL; | |
3932c423 | 1744 | struct file_struct *file; |
54410882 WD |
1745 | item_list *relname_list; |
1746 | relnamecache **rnpp; | |
af3172c1 | 1747 | int len, need_new_dir, depth = 0; |
7b6c5c77 | 1748 | filter_rule_list save_filter_list = filter_list; |
3932c423 | 1749 | |
3f309272 | 1750 | flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR); |
75a01a07 | 1751 | filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */ |
3932c423 WD |
1752 | |
1753 | if (inc_recurse) { | |
1754 | if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname | |
1755 | && lastpath_len == limit - fname | |
1756 | && strncmp(lastpath, fname, lastpath_len) == 0) | |
1757 | need_new_dir = 0; | |
1758 | else | |
1759 | need_new_dir = 1; | |
af3172c1 WD |
1760 | } else { |
1761 | char *tp = fname, *lp = lastpath; | |
1762 | /* Skip any initial directories in our path that we | |
1763 | * have in common with lastpath. */ | |
1764 | assert(start == fname); | |
1765 | for ( ; ; tp++, lp++) { | |
1766 | if (tp == limit) { | |
1767 | if (*lp == '/' || *lp == '\0') | |
1768 | goto done; | |
1769 | break; | |
1770 | } | |
1771 | if (*lp != *tp) | |
1772 | break; | |
1773 | if (*tp == '/') { | |
1774 | start = tp; | |
1775 | depth++; | |
1776 | } | |
1777 | } | |
3932c423 | 1778 | need_new_dir = 1; |
af3172c1 | 1779 | } |
3932c423 WD |
1780 | |
1781 | if (need_new_dir) { | |
1782 | int save_copy_links = copy_links; | |
1783 | int save_xfer_dirs = xfer_dirs; | |
af3172c1 | 1784 | char *slash; |
3932c423 | 1785 | |
74db1f14 | 1786 | copy_links = xfer_dirs = 1; |
3932c423 WD |
1787 | |
1788 | *limit = '\0'; | |
1789 | ||
1790 | for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) { | |
1791 | *slash = '\0'; | |
af3172c1 WD |
1792 | file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS); |
1793 | depth++; | |
1794 | if (!inc_recurse && file && S_ISDIR(file->mode)) | |
1795 | change_local_filter_dir(fname, strlen(fname), depth); | |
3932c423 WD |
1796 | *slash = '/'; |
1797 | } | |
1798 | ||
1799 | file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS); | |
1800 | if (inc_recurse) { | |
1801 | if (file && !S_ISDIR(file->mode)) | |
1802 | file = NULL; | |
3932c423 | 1803 | lastpath_struct = file; |
af3172c1 WD |
1804 | } else if (file && S_ISDIR(file->mode)) |
1805 | change_local_filter_dir(fname, strlen(fname), ++depth); | |
3932c423 WD |
1806 | |
1807 | strlcpy(lastpath, fname, sizeof lastpath); | |
1808 | lastpath_len = limit - fname; | |
1809 | ||
1810 | *limit = '/'; | |
1811 | ||
1812 | copy_links = save_copy_links; | |
1813 | xfer_dirs = save_xfer_dirs; | |
1814 | ||
1815 | if (!inc_recurse) | |
75a01a07 | 1816 | goto done; |
3932c423 WD |
1817 | } |
1818 | ||
1819 | if (!lastpath_struct) | |
75a01a07 | 1820 | goto done; /* dir must have vanished */ |
3932c423 WD |
1821 | |
1822 | len = strlen(limit+1); | |
54410882 WD |
1823 | memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list); |
1824 | if (!relname_list) { | |
1825 | if (!(relname_list = new0(item_list))) | |
3932c423 | 1826 | out_of_memory("send_implied_dirs"); |
54410882 | 1827 | memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list); |
3932c423 | 1828 | } |
54410882 WD |
1829 | rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32); |
1830 | if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len))) | |
3932c423 | 1831 | out_of_memory("send_implied_dirs"); |
4e421735 | 1832 | (*rnpp)->name_type = name_type; |
54410882 | 1833 | strlcpy((*rnpp)->fname, limit+1, len + 1); |
75a01a07 WD |
1834 | |
1835 | done: | |
1836 | filter_list = save_filter_list; | |
3932c423 WD |
1837 | } |
1838 | ||
a6e7b978 | 1839 | static void send1extra(int f, struct file_struct *file, struct file_list *flist) |
c7e6f84f WD |
1840 | { |
1841 | char fbuf[MAXPATHLEN]; | |
54410882 | 1842 | item_list *relname_list; |
3f309272 | 1843 | int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR; |
3932c423 | 1844 | size_t j; |
a6e7b978 WD |
1845 | |
1846 | f_name(file, fbuf); | |
1847 | dlen = strlen(fbuf); | |
1848 | ||
29a89172 WD |
1849 | if (!change_pathname(file, NULL, 0)) |
1850 | exit_cleanup(RERR_FILESELECT); | |
a6e7b978 WD |
1851 | |
1852 | change_local_filter_dir(fbuf, dlen, send_dir_depth); | |
1853 | ||
53ec55a8 WD |
1854 | if (file->flags & FLAG_CONTENT_DIR) { |
1855 | if (one_file_system) { | |
1856 | STRUCT_STAT st; | |
1857 | if (link_stat(fbuf, &st, copy_dirlinks) != 0) { | |
d39d60a1 | 1858 | interpret_stat_error(fbuf, True); |
53ec55a8 WD |
1859 | return; |
1860 | } | |
1861 | filesystem_dev = st.st_dev; | |
1862 | } | |
3932c423 | 1863 | send_directory(f, flist, fbuf, dlen, flags); |
53ec55a8 | 1864 | } |
3932c423 WD |
1865 | |
1866 | if (!relative_paths) | |
1867 | return; | |
1868 | ||
54410882 WD |
1869 | memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list); |
1870 | if (!relname_list) | |
3932c423 WD |
1871 | return; |
1872 | ||
54410882 WD |
1873 | for (j = 0; j < relname_list->count; j++) { |
1874 | char *slash; | |
1875 | relnamecache *rnp = ((relnamecache**)relname_list->items)[j]; | |
4e421735 | 1876 | char name_type = rnp->name_type; |
3932c423 WD |
1877 | |
1878 | fbuf[dlen] = '/'; | |
54410882 WD |
1879 | len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1); |
1880 | free(rnp); | |
3932c423 WD |
1881 | if (len >= (int)sizeof fbuf) |
1882 | continue; /* Impossible... */ | |
1883 | ||
1884 | slash = strchr(fbuf+dlen+1, '/'); | |
1885 | if (slash) { | |
4e421735 | 1886 | send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type); |
3932c423 WD |
1887 | continue; |
1888 | } | |
1889 | ||
4e421735 | 1890 | if (name_type != NORMAL_NAME) { |
3932c423 | 1891 | STRUCT_STAT st; |
a15c4b38 | 1892 | if (link_stat(fbuf, &st, 1) != 0) { |
d39d60a1 | 1893 | interpret_stat_error(fbuf, True); |
3932c423 WD |
1894 | continue; |
1895 | } | |
ff0284dd WD |
1896 | send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS); |
1897 | } else | |
1898 | send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS); | |
3932c423 WD |
1899 | } |
1900 | ||
54410882 | 1901 | free(relname_list); |
a6e7b978 WD |
1902 | } |
1903 | ||
1904 | void send_extra_file_list(int f, int at_least) | |
1905 | { | |
c7e6f84f WD |
1906 | struct file_list *flist; |
1907 | int64 start_write; | |
40e38376 | 1908 | uint16 prev_flags; |
ce827c3e | 1909 | int save_io_error = io_error; |
c7e6f84f | 1910 | |
25c2a6ac | 1911 | if (flist_eof) |
c7e6f84f WD |
1912 | return; |
1913 | ||
ce827c3e WD |
1914 | if (at_least < 0) |
1915 | at_least = file_total - file_old_total + 1; | |
1916 | ||
c7e6f84f WD |
1917 | /* Keep sending data until we have the requested number of |
1918 | * files in the upcoming file-lists. */ | |
ce827c3e | 1919 | while (file_total - file_old_total < at_least) { |
a6e7b978 | 1920 | struct file_struct *file = dir_flist->sorted[send_dir_ndx]; |
e366e530 | 1921 | int dir_ndx, dstart = stats.num_dirs; |
8db8eacb | 1922 | const char *pathname = F_PATHNAME(file); |
c7e6f84f | 1923 | int32 *dp; |
c7e6f84f WD |
1924 | |
1925 | flist = flist_new(0, "send_extra_file_list"); | |
4c9d5fef | 1926 | start_write = stats.total_written; |
c7e6f84f | 1927 | |
f7a76b9c | 1928 | if (unsort_ndx) |
79028af1 WD |
1929 | dir_ndx = F_NDX(file); |
1930 | else | |
79028af1 WD |
1931 | dir_ndx = send_dir_ndx; |
1932 | write_ndx(f, NDX_FLIST_OFFSET - dir_ndx); | |
5459e693 | 1933 | flist->parent_ndx = dir_ndx; |
a6e7b978 | 1934 | |
79028af1 | 1935 | send1extra(f, file, flist); |
40e38376 | 1936 | prev_flags = file->flags; |
83d5e900 | 1937 | dp = F_DIR_NODE_P(file); |
a6e7b978 | 1938 | |
79028af1 WD |
1939 | /* If there are any duplicate directory names that follow, we |
1940 | * send all the dirs together in one file-list. The dir_flist | |
1941 | * tree links all the child subdirs onto the last dup dir. */ | |
1942 | while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0 | |
1943 | && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) { | |
1944 | send_dir_ndx = dir_ndx; | |
1945 | file = dir_flist->sorted[dir_ndx]; | |
40e38376 | 1946 | /* Try to avoid some duplicate scanning of identical dirs. */ |
3f309272 WD |
1947 | if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR) |
1948 | file->flags &= ~FLAG_CONTENT_DIR; | |
40e38376 WD |
1949 | send1extra(f, file, flist); |
1950 | prev_flags = file->flags; | |
83d5e900 | 1951 | dp = F_DIR_NODE_P(file); |
a6e7b978 | 1952 | } |
79028af1 | 1953 | |
8b3e6052 WD |
1954 | if (protocol_version < 31 || io_error == save_io_error || ignore_errors) |
1955 | write_byte(f, 0); | |
1956 | else { | |
1957 | write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST); | |
9784f01c | 1958 | write_varint(f, io_error); |
8b3e6052 | 1959 | } |
a6e7b978 | 1960 | |
a6e7b978 | 1961 | if (need_unsorted_flist) { |
9decb4d2 | 1962 | if (!(flist->sorted = new_array(struct file_struct *, flist->used))) |
8ea07c00 WD |
1963 | out_of_memory("send_extra_file_list"); |
1964 | memcpy(flist->sorted, flist->files, | |
9decb4d2 | 1965 | flist->used * sizeof (struct file_struct*)); |
a6e7b978 | 1966 | } else |
332cf6df | 1967 | flist->sorted = flist->files; |
8ea07c00 | 1968 | |
65b4e4b2 | 1969 | flist_sort_and_clean(flist, 0); |
5459e693 | 1970 | |
e366e530 | 1971 | add_dirs_to_tree(send_dir_ndx, flist, stats.num_dirs - dstart); |
abdcb21a | 1972 | flist_done_allocating(flist); |
a6e7b978 | 1973 | |
9decb4d2 | 1974 | file_total += flist->used; |
c7e6f84f | 1975 | stats.flist_size += stats.total_written - start_write; |
9decb4d2 | 1976 | stats.num_files += flist->used; |
951e826b | 1977 | if (DEBUG_GTE(FLIST, 3)) |
c7e6f84f WD |
1978 | output_flist(flist); |
1979 | ||
c7e6f84f WD |
1980 | if (DIR_FIRST_CHILD(dp) >= 0) { |
1981 | send_dir_ndx = DIR_FIRST_CHILD(dp); | |
1982 | send_dir_depth++; | |
1983 | } else { | |
1984 | while (DIR_NEXT_SIBLING(dp) < 0) { | |
1985 | if ((send_dir_ndx = DIR_PARENT(dp)) < 0) { | |
9ae7a2cd | 1986 | write_ndx(f, NDX_FLIST_EOF); |
c7e6f84f WD |
1987 | flist_eof = 1; |
1988 | change_local_filter_dir(NULL, 0, 0); | |
1989 | goto finish; | |
1990 | } | |
1991 | send_dir_depth--; | |
a6e7b978 | 1992 | file = dir_flist->sorted[send_dir_ndx]; |
83d5e900 | 1993 | dp = F_DIR_NODE_P(file); |
c7e6f84f WD |
1994 | } |
1995 | send_dir_ndx = DIR_NEXT_SIBLING(dp); | |
1996 | } | |
301fb56c | 1997 | } |
c7e6f84f WD |
1998 | |
1999 | finish: | |
8b3e6052 | 2000 | if (io_error != save_io_error && protocol_version == 30 && !ignore_errors) |
c7e6f84f | 2001 | send_msg_int(MSG_IO_ERROR, io_error); |
c627d613 AT |
2002 | } |
2003 | ||
ebed4c3a | 2004 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 2005 | { |
3bb88b43 WD |
2006 | static const char *lastdir; |
2007 | static int lastdir_len = -1; | |
2008 | int len, dirlen; | |
bcacc18b | 2009 | STRUCT_STAT st; |
c7e6f84f | 2010 | char *p, *dir; |
649d65ed | 2011 | struct file_list *flist; |
31b4d25d | 2012 | struct timeval start_tv, end_tv; |
a800434a | 2013 | int64 start_write; |
24d0fcde | 2014 | int use_ff_fd = 0; |
ff0284dd | 2015 | int disable_buffering; |
0d9eba03 | 2016 | int flags = recurse ? FLAG_CONTENT_DIR : 0; |
95a44066 | 2017 | int reading_remotely = filesfrom_host != NULL; |
2509753f | 2018 | int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS) |
22955408 WD |
2019 | #ifdef ICONV_OPTION |
2020 | | (filesfrom_convert ? RL_CONVERT : 0) | |
2021 | #endif | |
2022 | | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0); | |
d48810ba | 2023 | int implied_dot_dir = 0; |
649d65ed | 2024 | |
ea124cb3 | 2025 | rprintf(FLOG, "building file list\n"); |
134f4338 | 2026 | if (show_filelist_p()) |
1bbd10fe | 2027 | start_filelist_progress("building file list"); |
951e826b | 2028 | else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server) |
98b1689d | 2029 | rprintf(FCLIENT, "sending incremental file list\n"); |
c627d613 | 2030 | |
a800434a | 2031 | start_write = stats.total_written; |
31b4d25d | 2032 | gettimeofday(&start_tv, NULL); |
a800434a | 2033 | |
3f309272 WD |
2034 | if (relative_paths && protocol_version >= 30) |
2035 | implied_dirs = 1; /* We send flagged implied dirs */ | |
2036 | ||
8cae71eb | 2037 | #ifdef SUPPORT_HARD_LINKS |
c7e6f84f | 2038 | if (preserve_hard_links && protocol_version >= 30 && !cur_flist) |
8cae71eb WD |
2039 | init_hard_links(); |
2040 | #endif | |
2041 | ||
c7e6f84f | 2042 | flist = cur_flist = flist_new(0, "send_file_list"); |
3ea6e0e7 | 2043 | if (inc_recurse) { |
c7e6f84f | 2044 | dir_flist = flist_new(FLIST_TEMP, "send_file_list"); |
ff0284dd WD |
2045 | flags |= FLAG_DIVERT_DIRS; |
2046 | } else | |
c7e6f84f | 2047 | dir_flist = cur_flist; |
c627d613 | 2048 | |
c7e6f84f | 2049 | disable_buffering = io_start_buffering_out(f); |
134f4338 | 2050 | if (filesfrom_fd >= 0) { |
29a89172 WD |
2051 | if (argv[0] && !change_dir(argv[0], CD_NORMAL)) { |
2052 | rsyserr(FERROR_XFER, errno, "change_dir %s failed", | |
f8949e76 | 2053 | full_fname(argv[0])); |
134f4338 | 2054 | exit_cleanup(RERR_FILESELECT); |
24d0fcde | 2055 | } |
134f4338 | 2056 | use_ff_fd = 1; |
d6dead6b AT |
2057 | } |
2058 | ||
3ce3cabe WD |
2059 | if (!orig_dir) |
2060 | orig_dir = strdup(curr_dir); | |
2061 | ||
24d0fcde | 2062 | while (1) { |
4e421735 | 2063 | char fbuf[MAXPATHLEN], *fn, name_type; |
c627d613 | 2064 | |
24d0fcde | 2065 | if (use_ff_fd) { |
2509753f | 2066 | if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0) |
24d0fcde | 2067 | break; |
d48810ba | 2068 | sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS); |
24d0fcde WD |
2069 | } else { |
2070 | if (argc-- == 0) | |
2071 | break; | |
56f0c976 | 2072 | strlcpy(fbuf, *argv++, MAXPATHLEN); |
24d0fcde | 2073 | if (sanitize_paths) |
d48810ba | 2074 | sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS); |
24d0fcde | 2075 | } |
c627d613 | 2076 | |
56f0c976 | 2077 | len = strlen(fbuf); |
1902a765 WD |
2078 | if (relative_paths) { |
2079 | /* We clean up fbuf below. */ | |
4e421735 | 2080 | name_type = NORMAL_NAME; |
1902a765 | 2081 | } else if (!len || fbuf[len - 1] == '/') { |
56f0c976 | 2082 | if (len == 2 && fbuf[0] == '.') { |
6931c138 | 2083 | /* Turn "./" into just "." rather than "./." */ |
34aa616d | 2084 | fbuf[--len] = '\0'; |
557a35f5 | 2085 | } else { |
56f0c976 | 2086 | if (len + 1 >= MAXPATHLEN) |
a1f99493 | 2087 | overflow_exit("send_file_list"); |
56f0c976 WD |
2088 | fbuf[len++] = '.'; |
2089 | fbuf[len] = '\0'; | |
53f821f1 | 2090 | } |
84ecaa0e | 2091 | name_type = DOTDIR_NAME; |
56f0c976 WD |
2092 | } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.' |
2093 | && (len == 2 || fbuf[len-3] == '/')) { | |
2094 | if (len + 2 >= MAXPATHLEN) | |
a1f99493 | 2095 | overflow_exit("send_file_list"); |
56f0c976 WD |
2096 | fbuf[len++] = '/'; |
2097 | fbuf[len++] = '.'; | |
2098 | fbuf[len] = '\0'; | |
84ecaa0e | 2099 | name_type = DOTDIR_NAME; |
4e421735 | 2100 | } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/')) |
84ecaa0e | 2101 | name_type = DOTDIR_NAME; |
4e421735 WD |
2102 | else |
2103 | name_type = NORMAL_NAME; | |
c627d613 | 2104 | |
649d65ed AT |
2105 | dir = NULL; |
2106 | ||
2107 | if (!relative_paths) { | |
56f0c976 | 2108 | p = strrchr(fbuf, '/'); |
649d65ed | 2109 | if (p) { |
151f59f1 | 2110 | *p = '\0'; |
56f0c976 | 2111 | if (p == fbuf) |
649d65ed AT |
2112 | dir = "/"; |
2113 | else | |
56f0c976 WD |
2114 | dir = fbuf; |
2115 | len -= p - fbuf + 1; | |
2116 | fn = p + 1; | |
2117 | } else | |
2118 | fn = fbuf; | |
1902a765 WD |
2119 | } else { |
2120 | if ((p = strstr(fbuf, "/./")) != NULL) { | |
2121 | *p = '\0'; | |
2122 | if (p == fbuf) | |
2123 | dir = "/"; | |
d48810ba | 2124 | else { |
1902a765 | 2125 | dir = fbuf; |
d48810ba WD |
2126 | clean_fname(dir, 0); |
2127 | } | |
1902a765 | 2128 | fn = p + 3; |
4e421735 WD |
2129 | while (*fn == '/') |
2130 | fn++; | |
2131 | if (!*fn) | |
2132 | *--fn = '\0'; /* ensure room for '.' */ | |
1902a765 WD |
2133 | } else |
2134 | fn = fbuf; | |
d48810ba WD |
2135 | /* A leading ./ can be used in relative mode to affect |
2136 | * the dest dir without its name being in the path. */ | |
2137 | if (*fn == '.' && fn[1] == '/' && !implied_dot_dir) { | |
2138 | send_file_name(f, flist, ".", NULL, | |
2139 | (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR, | |
2140 | ALL_FILTERS); | |
2141 | implied_dot_dir = 1; | |
2142 | } | |
2143 | len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH | |
4e421735 | 2144 | | CFN_DROP_TRAILING_DOT_DIR); |
ff0284dd WD |
2145 | if (len == 1) { |
2146 | if (fn[0] == '/') { | |
2147 | fn = "/."; | |
2148 | len = 2; | |
84ecaa0e | 2149 | name_type = DOTDIR_NAME; |
ff0284dd | 2150 | } else if (fn[0] == '.') |
84ecaa0e | 2151 | name_type = DOTDIR_NAME; |
4e421735 WD |
2152 | } else if (fn[len-1] == '/') { |
2153 | fn[--len] = '\0'; | |
2154 | if (len == 1 && *fn == '.') | |
84ecaa0e | 2155 | name_type = DOTDIR_NAME; |
4e421735 WD |
2156 | else |
2157 | name_type = SLASH_ENDING_NAME; | |
ff0284dd | 2158 | } |
1902a765 | 2159 | /* Reject a ".." dir in the active part of the path. */ |
ede1f0eb WD |
2160 | for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) { |
2161 | if ((p[2] == '/' || p[2] == '\0') | |
2162 | && (p == fn || p[-1] == '/')) { | |
2163 | rprintf(FERROR, | |
2164 | "found \"..\" dir in relative path: %s\n", | |
4e421735 | 2165 | fn); |
ede1f0eb WD |
2166 | exit_cleanup(RERR_SYNTAX); |
2167 | } | |
1902a765 WD |
2168 | } |
2169 | } | |
d2ea5980 | 2170 | |
56f0c976 WD |
2171 | if (!*fn) { |
2172 | len = 1; | |
2173 | fn = "."; | |
84ecaa0e | 2174 | name_type = DOTDIR_NAME; |
56f0c976 | 2175 | } |
d2ea5980 | 2176 | |
3bb88b43 WD |
2177 | dirlen = dir ? strlen(dir) : 0; |
2178 | if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) { | |
29a89172 | 2179 | if (!change_pathname(NULL, dir, -dirlen)) |
3b7bcaaf | 2180 | continue; |
ef35abb2 WD |
2181 | lastdir = pathname; |
2182 | lastdir_len = pathname_len; | |
29a89172 | 2183 | } else if (!change_pathname(NULL, lastdir, lastdir_len)) |
3bb88b43 | 2184 | continue; |
d2ea5980 | 2185 | |
99eba675 | 2186 | if (fn != fbuf) |
56f0c976 WD |
2187 | memmove(fbuf, fn, len + 1); |
2188 | ||
1aefb7ef | 2189 | if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0 |
84ecaa0e | 2190 | || (name_type != DOTDIR_NAME && is_daemon_excluded(fbuf, S_ISDIR(st.st_mode))) |
b5daf530 | 2191 | || (relative_paths && path_is_daemon_excluded(fbuf, 1))) { |
42d8ec61 WD |
2192 | if (errno != ENOENT || missing_args == 0) { |
2193 | /* This is a transfer error, but inhibit deletion | |
2194 | * only if we might be omitting an existing file. */ | |
2195 | if (errno != ENOENT) | |
2196 | io_error |= IOERR_GENERAL; | |
ce66f417 WD |
2197 | rsyserr(FERROR_XFER, errno, "link_stat %s failed", |
2198 | full_fname(fbuf)); | |
2199 | continue; | |
42d8ec61 WD |
2200 | } else if (missing_args == 1) { |
2201 | /* Just ignore the arg. */ | |
2202 | continue; | |
2203 | } else /* (missing_args == 2) */ { | |
2204 | /* Send the arg as a "missing" entry with | |
2205 | * mode 0, which tells the generator to delete it. */ | |
2206 | memset(&st, 0, sizeof st); | |
ce66f417 | 2207 | } |
25c2a6ac WD |
2208 | } |
2209 | ||
0d9eba03 | 2210 | /* A dot-dir should not be excluded! */ |
77812418 | 2211 | if (name_type != DOTDIR_NAME && st.st_mode != 0 |
0d9eba03 WD |
2212 | && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS)) |
2213 | continue; | |
2214 | ||
25c2a6ac WD |
2215 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
2216 | rprintf(FINFO, "skipping directory %s\n", fbuf); | |
2217 | continue; | |
2218 | } | |
2219 | ||
3932c423 | 2220 | if (inc_recurse && relative_paths && *fbuf) { |
0d9eba03 | 2221 | if ((p = strchr(fbuf+1, '/')) != NULL) { |
4e421735 WD |
2222 | if (p - fbuf == 1 && *fbuf == '.') { |
2223 | if ((fn = strchr(p+1, '/')) != NULL) | |
2224 | p = fn; | |
2225 | } else | |
2226 | fn = p; | |
2227 | send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type); | |
2228 | if (fn == p) | |
2229 | continue; | |
3932c423 | 2230 | } |
0d9eba03 | 2231 | } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) { |
151f59f1 WD |
2232 | /* Send the implied directories at the start of the |
2233 | * source spec, so we get their permissions right. */ | |
af3172c1 | 2234 | send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0); |
649d65ed | 2235 | } |
ebed4c3a | 2236 | |
53ec55a8 WD |
2237 | if (one_file_system) |
2238 | filesystem_dev = st.st_dev; | |
2239 | ||
4e421735 | 2240 | if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) { |
42c6b139 | 2241 | struct file_struct *file; |
42c6b139 | 2242 | file = send_file_name(f, flist, fbuf, &st, |
0d9eba03 WD |
2243 | FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags, |
2244 | NO_FILTERS); | |
d48810ba WD |
2245 | if (!file) |
2246 | continue; | |
4e421735 | 2247 | if (inc_recurse) { |
84ecaa0e | 2248 | if (name_type == DOTDIR_NAME) { |
4e421735 WD |
2249 | if (send_dir_depth < 0) { |
2250 | send_dir_depth = 0; | |
2251 | change_local_filter_dir(fbuf, len, send_dir_depth); | |
2252 | } | |
2253 | send_directory(f, flist, fbuf, len, flags); | |
2254 | } | |
d48810ba | 2255 | } else |
42c6b139 WD |
2256 | send_if_directory(f, flist, file, fbuf, len, flags); |
2257 | } else | |
0d9eba03 | 2258 | send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS); |
649d65ed | 2259 | } |
dc5ddbcc | 2260 | |
134f4338 WD |
2261 | gettimeofday(&end_tv, NULL); |
2262 | stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
2263 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
2264 | if (stats.flist_buildtime == 0) | |
2265 | stats.flist_buildtime = 1; | |
2266 | start_tv = end_tv; | |
31b4d25d | 2267 | |
8b3e6052 WD |
2268 | /* Indicate end of file list */ |
2269 | if (protocol_version < 31 || io_error == 0 || ignore_errors) | |
2270 | write_byte(f, 0); | |
2271 | else { | |
2272 | write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST); | |
9784f01c | 2273 | write_varint(f, io_error); |
8b3e6052 | 2274 | } |
8cae71eb WD |
2275 | |
2276 | #ifdef SUPPORT_HARD_LINKS | |
3ea6e0e7 | 2277 | if (preserve_hard_links && protocol_version >= 30 && !inc_recurse) |
8cae71eb WD |
2278 | idev_destroy(); |
2279 | #endif | |
c627d613 | 2280 | |
134f4338 WD |
2281 | if (show_filelist_p()) |
2282 | finish_filelist_progress(flist); | |
31b4d25d | 2283 | |
134f4338 WD |
2284 | gettimeofday(&end_tv, NULL); |
2285 | stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
2286 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
ebed4c3a | 2287 | |
332cf6df WD |
2288 | /* When converting names, both sides keep an unsorted file-list array |
2289 | * because the names will differ on the sending and receiving sides | |
2290 | * (both sides will use the unsorted index number for each item). */ | |
2291 | ||
a6e7b978 WD |
2292 | /* Sort the list without removing any duplicates. This allows the |
2293 | * receiving side to ask for whatever name it kept. For incremental | |
2294 | * recursion mode, the sender marks duplicate dirs so that it can | |
2295 | * send them together in a single file-list. */ | |
332cf6df | 2296 | if (need_unsorted_flist) { |
65b4e4b2 WD |
2297 | if (!(flist->sorted = new_array(struct file_struct *, flist->used))) |
2298 | out_of_memory("send_file_list"); | |
2299 | memcpy(flist->sorted, flist->files, | |
2300 | flist->used * sizeof (struct file_struct*)); | |
2301 | } else | |
332cf6df | 2302 | flist->sorted = flist->files; |
65b4e4b2 | 2303 | flist_sort_and_clean(flist, 0); |
9decb4d2 | 2304 | file_total += flist->used; |
ebed4c3a | 2305 | |
0b52f94d | 2306 | if (numeric_ids <= 0 && !inc_recurse) |
1564cd5a | 2307 | send_id_list(f); |
f6c34742 | 2308 | |
46cd1ef6 WD |
2309 | set_msg_fd_in(-1); |
2310 | ||
134f4338 | 2311 | /* send the io_error flag */ |
c7e6f84f WD |
2312 | if (protocol_version < 30) |
2313 | write_int(f, ignore_errors ? 0 : io_error); | |
8b3e6052 | 2314 | else if (io_error && protocol_version == 30 && !ignore_errors) |
c7e6f84f WD |
2315 | send_msg_int(MSG_IO_ERROR, io_error); |
2316 | ||
2317 | if (disable_buffering) | |
2318 | io_end_buffering_out(); | |
6ba9279f | 2319 | |
134f4338 | 2320 | stats.flist_size = stats.total_written - start_write; |
9decb4d2 | 2321 | stats.num_files = flist->used; |
d6dead6b | 2322 | |
951e826b | 2323 | if (DEBUG_GTE(FLIST, 3)) |
32cbfe7b | 2324 | output_flist(flist); |
cefed3e8 | 2325 | |
951e826b | 2326 | if (DEBUG_GTE(FLIST, 2)) |
ebed4c3a | 2327 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 2328 | |
3ea6e0e7 | 2329 | if (inc_recurse) { |
4e421735 | 2330 | send_dir_depth = 1; |
e366e530 | 2331 | add_dirs_to_tree(-1, flist, stats.num_dirs); |
902ee53e | 2332 | if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0) |
4e421735 | 2333 | flist->parent_ndx = -1; |
abdcb21a | 2334 | flist_done_allocating(flist); |
25c2a6ac WD |
2335 | if (send_dir_ndx < 0) { |
2336 | write_ndx(f, NDX_FLIST_EOF); | |
2337 | flist_eof = 1; | |
2338 | } | |
2339 | else if (file_total == 1) { | |
c7e6f84f WD |
2340 | /* If we're creating incremental file-lists and there |
2341 | * was just 1 item in the first file-list, send 1 more | |
2342 | * file-list to check if this is a 1-file xfer. */ | |
25c2a6ac | 2343 | send_extra_file_list(f, 1); |
c7e6f84f | 2344 | } |
8d10cbfc WD |
2345 | } else |
2346 | flist_eof = 1; | |
c7e6f84f | 2347 | |
649d65ed | 2348 | return flist; |
c627d613 AT |
2349 | } |
2350 | ||
c627d613 AT |
2351 | struct file_list *recv_file_list(int f) |
2352 | { | |
ebed4c3a | 2353 | struct file_list *flist; |
c7e6f84f | 2354 | int dstart, flags; |
ebed4c3a | 2355 | int64 start_read; |
c627d613 | 2356 | |
a02348b5 WD |
2357 | if (!first_flist) { |
2358 | if (show_filelist_p()) | |
2359 | start_filelist_progress("receiving file list"); | |
2360 | else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server) | |
2361 | rprintf(FCLIENT, "receiving incremental file list\n"); | |
c7e6f84f | 2362 | rprintf(FLOG, "receiving file list\n"); |
2df20057 WD |
2363 | if (usermap) |
2364 | parse_name_map(usermap, True); | |
2365 | if (groupmap) | |
2366 | parse_name_map(groupmap, False); | |
a02348b5 | 2367 | } |
c627d613 | 2368 | |
ebed4c3a | 2369 | start_read = stats.total_read; |
a800434a | 2370 | |
8cae71eb | 2371 | #ifdef SUPPORT_HARD_LINKS |
d4d6646a | 2372 | if (preserve_hard_links && !first_flist) |
8cae71eb WD |
2373 | init_hard_links(); |
2374 | #endif | |
c627d613 | 2375 | |
9863bdbc WD |
2376 | flist = flist_new(0, "recv_file_list"); |
2377 | ||
3ea6e0e7 | 2378 | if (inc_recurse) { |
4e421735 | 2379 | if (flist->ndx_start == 1) |
c7e6f84f | 2380 | dir_flist = flist_new(FLIST_TEMP, "recv_file_list"); |
9decb4d2 | 2381 | dstart = dir_flist->used; |
c7e6f84f WD |
2382 | } else { |
2383 | dir_flist = flist; | |
2384 | dstart = 0; | |
2385 | } | |
2386 | ||
1ef00d20 | 2387 | while ((flags = read_byte(f)) != 0) { |
f5db0993 | 2388 | struct file_struct *file; |
dbda5fbf | 2389 | |
d01d15e0 | 2390 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 2391 | flags |= read_byte(f) << 8; |
8b3e6052 WD |
2392 | |
2393 | if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) { | |
2394 | int err; | |
2395 | if (protocol_version < 31) { | |
2396 | rprintf(FERROR, "Invalid flist flag: %x\n", flags); | |
2397 | exit_cleanup(RERR_PROTOCOL); | |
2398 | } | |
9784f01c | 2399 | err = read_varint(f); |
8b3e6052 WD |
2400 | if (!ignore_errors) |
2401 | io_error |= err; | |
2402 | break; | |
2403 | } | |
2404 | ||
2405 | flist_expand(flist, 1); | |
82ad07c4 | 2406 | file = recv_file_entry(flist, flags, f); |
c627d613 | 2407 | |
e366e530 WD |
2408 | if (S_ISREG(file->mode)) { |
2409 | /* Already counted */ | |
2410 | } else if (S_ISDIR(file->mode)) { | |
2411 | if (inc_recurse) { | |
2412 | flist_expand(dir_flist, 1); | |
2413 | dir_flist->files[dir_flist->used++] = file; | |
2414 | } | |
2415 | stats.num_dirs++; | |
2416 | } else if (S_ISLNK(file->mode)) | |
2417 | stats.num_symlinks++; | |
2418 | else if (IS_DEVICE(file->mode)) | |
2419 | stats.num_symlinks++; | |
2420 | else | |
2421 | stats.num_specials++; | |
c7e6f84f | 2422 | |
9decb4d2 | 2423 | flist->files[flist->used++] = file; |
c627d613 | 2424 | |
9decb4d2 | 2425 | maybe_emit_filelist_progress(flist->used); |
1bbd10fe | 2426 | |
951e826b | 2427 | if (DEBUG_GTE(FLIST, 2)) { |
6e5b6822 WD |
2428 | char *name = f_name(file, NULL); |
2429 | rprintf(FINFO, "recv_file_name(%s)\n", NS(name)); | |
5e4ff5f9 | 2430 | } |
ebed4c3a | 2431 | } |
9decb4d2 | 2432 | file_total += flist->used; |
c627d613 | 2433 | |
951e826b | 2434 | if (DEBUG_GTE(FLIST, 2)) |
9decb4d2 | 2435 | rprintf(FINFO, "received %d names\n", flist->used); |
c627d613 | 2436 | |
b7736c79 | 2437 | if (show_filelist_p()) |
1bbd10fe | 2438 | finish_filelist_progress(flist); |
a06d19e3 | 2439 | |
332cf6df WD |
2440 | if (need_unsorted_flist) { |
2441 | /* Create an extra array of index pointers that we can sort for | |
2442 | * the generator's use (for wading through the files in sorted | |
2443 | * order and for calling flist_find()). We keep the "files" | |
2444 | * list unsorted for our exchange of index numbers with the | |
2445 | * other side (since their names may not sort the same). */ | |
9decb4d2 | 2446 | if (!(flist->sorted = new_array(struct file_struct *, flist->used))) |
332cf6df WD |
2447 | out_of_memory("recv_file_list"); |
2448 | memcpy(flist->sorted, flist->files, | |
9decb4d2 WD |
2449 | flist->used * sizeof (struct file_struct*)); |
2450 | if (inc_recurse && dir_flist->used > dstart) { | |
65b4e4b2 WD |
2451 | static int dir_flist_malloced = 0; |
2452 | if (dir_flist_malloced < dir_flist->malloced) { | |
2453 | dir_flist->sorted = realloc_array(dir_flist->sorted, | |
2454 | struct file_struct *, | |
2455 | dir_flist->malloced); | |
2456 | dir_flist_malloced = dir_flist->malloced; | |
2457 | } | |
8ea07c00 | 2458 | memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart, |
9decb4d2 WD |
2459 | (dir_flist->used - dstart) * sizeof (struct file_struct*)); |
2460 | fsort(dir_flist->sorted + dstart, dir_flist->used - dstart); | |
a6e7b978 | 2461 | } |
f7a76b9c | 2462 | } else { |
332cf6df | 2463 | flist->sorted = flist->files; |
9decb4d2 | 2464 | if (inc_recurse && dir_flist->used > dstart) { |
a6e7b978 | 2465 | dir_flist->sorted = dir_flist->files; |
9decb4d2 | 2466 | fsort(dir_flist->sorted + dstart, dir_flist->used - dstart); |
a6e7b978 WD |
2467 | } |
2468 | } | |
332cf6df | 2469 | |
abdcb21a WD |
2470 | if (inc_recurse) |
2471 | flist_done_allocating(flist); | |
8d10cbfc | 2472 | else if (f >= 0) { |
1564cd5a | 2473 | recv_id_list(f, flist); |
8d10cbfc WD |
2474 | flist_eof = 1; |
2475 | } | |
f6c34742 | 2476 | |
65b4e4b2 | 2477 | flist_sort_and_clean(flist, relative_paths); |
a6e7b978 | 2478 | |
c7e6f84f | 2479 | if (protocol_version < 30) { |
b9f592fb | 2480 | /* Recv the io_error flag */ |
8b3e6052 WD |
2481 | int err = read_int(f); |
2482 | if (!ignore_errors) | |
2483 | io_error |= err; | |
4e421735 | 2484 | } else if (inc_recurse && flist->ndx_start == 1) { |
902ee53e | 2485 | if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0) |
4e421735 | 2486 | flist->parent_ndx = -1; |
ebed4c3a | 2487 | } |
6ba9279f | 2488 | |
951e826b | 2489 | if (DEBUG_GTE(FLIST, 3)) |
32cbfe7b | 2490 | output_flist(flist); |
cefed3e8 | 2491 | |
951e826b | 2492 | if (DEBUG_GTE(FLIST, 2)) |
ebed4c3a | 2493 | rprintf(FINFO, "recv_file_list done\n"); |
17faa41c | 2494 | |
c7e6f84f | 2495 | stats.flist_size += stats.total_read - start_read; |
9decb4d2 | 2496 | stats.num_files += flist->used; |
a800434a | 2497 | |
ebed4c3a | 2498 | return flist; |
c627d613 AT |
2499 | } |
2500 | ||
c7e6f84f WD |
2501 | /* This is only used once by the receiver if the very first file-list |
2502 | * has exactly one item in it. */ | |
2503 | void recv_additional_file_list(int f) | |
c627d613 | 2504 | { |
c7e6f84f | 2505 | struct file_list *flist; |
9ae7a2cd | 2506 | int ndx = read_ndx(f); |
25c2a6ac | 2507 | if (ndx == NDX_FLIST_EOF) { |
c7e6f84f WD |
2508 | flist_eof = 1; |
2509 | change_local_filter_dir(NULL, 0, 0); | |
2510 | } else { | |
2511 | ndx = NDX_FLIST_OFFSET - ndx; | |
9decb4d2 | 2512 | if (ndx < 0 || ndx >= dir_flist->used) { |
c7e6f84f WD |
2513 | ndx = NDX_FLIST_OFFSET - ndx; |
2514 | rprintf(FERROR, | |
8ea07c00 WD |
2515 | "[%s] Invalid dir index: %d (%d - %d)\n", |
2516 | who_am_i(), ndx, NDX_FLIST_OFFSET, | |
9decb4d2 | 2517 | NDX_FLIST_OFFSET - dir_flist->used + 1); |
c7e6f84f WD |
2518 | exit_cleanup(RERR_PROTOCOL); |
2519 | } | |
951e826b | 2520 | if (DEBUG_GTE(FLIST, 3)) { |
1faa1a6d WD |
2521 | rprintf(FINFO, "[%s] receiving flist for dir %d\n", |
2522 | who_am_i(), ndx); | |
2523 | } | |
c7e6f84f WD |
2524 | flist = recv_file_list(f); |
2525 | flist->parent_ndx = ndx; | |
2526 | } | |
c627d613 AT |
2527 | } |
2528 | ||
f5db0993 WD |
2529 | /* Search for an identically-named item in the file list. Note that the |
2530 | * items must agree in their directory-ness, or no match is returned. */ | |
2f3cad89 | 2531 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 2532 | { |
f3c3ed44 | 2533 | int low = flist->low, high = flist->high; |
207522ae | 2534 | int diff, mid, mid_up; |
f3c3ed44 WD |
2535 | |
2536 | while (low <= high) { | |
2537 | mid = (low + high) / 2; | |
332cf6df | 2538 | if (F_IS_ACTIVE(flist->sorted[mid])) |
c0b134a4 | 2539 | mid_up = mid; |
7402d583 WD |
2540 | else { |
2541 | /* Scan for the next non-empty entry using the cached | |
2542 | * distance values. If the value isn't fully up-to- | |
2543 | * date, update it. */ | |
332cf6df WD |
2544 | mid_up = mid + F_DEPTH(flist->sorted[mid]); |
2545 | if (!F_IS_ACTIVE(flist->sorted[mid_up])) { | |
85aecef6 | 2546 | do { |
332cf6df WD |
2547 | mid_up += F_DEPTH(flist->sorted[mid_up]); |
2548 | } while (!F_IS_ACTIVE(flist->sorted[mid_up])); | |
2549 | F_DEPTH(flist->sorted[mid]) = mid_up - mid; | |
c0b134a4 | 2550 | } |
c0b134a4 | 2551 | if (mid_up > high) { |
7402d583 WD |
2552 | /* If there's nothing left above us, set high to |
2553 | * a non-empty entry below us and continue. */ | |
332cf6df WD |
2554 | high = mid - (int)flist->sorted[mid]->len32; |
2555 | if (!F_IS_ACTIVE(flist->sorted[high])) { | |
85aecef6 | 2556 | do { |
332cf6df WD |
2557 | high -= (int)flist->sorted[high]->len32; |
2558 | } while (!F_IS_ACTIVE(flist->sorted[high])); | |
2559 | flist->sorted[mid]->len32 = mid - high; | |
7402d583 | 2560 | } |
c0b134a4 WD |
2561 | continue; |
2562 | } | |
c0b134a4 | 2563 | } |
332cf6df | 2564 | diff = f_name_cmp(flist->sorted[mid_up], f); |
207522ae | 2565 | if (diff == 0) { |
f5db0993 | 2566 | if (protocol_version < 29 |
332cf6df | 2567 | && S_ISDIR(flist->sorted[mid_up]->mode) |
f5db0993 WD |
2568 | != S_ISDIR(f->mode)) |
2569 | return -1; | |
f3c3ed44 | 2570 | return mid_up; |
f5db0993 | 2571 | } |
207522ae | 2572 | if (diff < 0) |
f3c3ed44 | 2573 | low = mid_up + 1; |
207522ae WD |
2574 | else |
2575 | high = mid - 1; | |
d966ee25 | 2576 | } |
d966ee25 | 2577 | return -1; |
c627d613 AT |
2578 | } |
2579 | ||
d4d56eed MM |
2580 | /* Search for an identically-named item in the file list. Differs from |
2581 | * flist_find in that an item that agrees with "f" in directory-ness is | |
2582 | * preferred but one that does not is still found. */ | |
2583 | int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f) | |
2584 | { | |
2585 | mode_t save_mode; | |
2586 | int ndx; | |
2587 | ||
2588 | /* First look for an item that agrees in directory-ness. */ | |
2589 | ndx = flist_find(flist, f); | |
2590 | if (ndx >= 0) | |
2591 | return ndx; | |
2592 | ||
2593 | /* Temporarily flip f->mode to look for an item of opposite | |
2594 | * directory-ness. */ | |
2595 | save_mode = f->mode; | |
2596 | f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR; | |
2597 | ndx = flist_find(flist, f); | |
2598 | f->mode = save_mode; | |
2599 | return ndx; | |
2600 | } | |
2601 | ||
3ec4dd97 | 2602 | /* |
9935066b S |
2603 | * Free up any resources a file_struct has allocated |
2604 | * and clear the file. | |
3ec4dd97 | 2605 | */ |
82ad07c4 | 2606 | void clear_file(struct file_struct *file) |
c627d613 | 2607 | { |
a3e18c76 WD |
2608 | /* The +1 zeros out the first char of the basename. */ |
2609 | memset(file, 0, FILE_STRUCT_LEN + 1); | |
9d737ecb | 2610 | /* In an empty entry, F_DEPTH() is an offset to the next non-empty |
96293cf9 | 2611 | * entry. Likewise for len32 in the opposite direction. We assume |
0679ac4c WD |
2612 | * that we're alone for now since flist_find() will adjust the counts |
2613 | * it runs into that aren't up-to-date. */ | |
9d737ecb | 2614 | file->len32 = F_DEPTH(file) = 1; |
3ec4dd97 | 2615 | } |
c627d613 | 2616 | |
4785cd43 | 2617 | /* Allocate a new file list. */ |
c7e6f84f | 2618 | struct file_list *flist_new(int flags, char *msg) |
3d382777 AT |
2619 | { |
2620 | struct file_list *flist; | |
2621 | ||
3932c423 | 2622 | if (!(flist = new0(struct file_list))) |
9935066b | 2623 | out_of_memory(msg); |
3d382777 | 2624 | |
abdcb21a WD |
2625 | if (flags & FLIST_TEMP) { |
2626 | if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0, | |
a02348b5 | 2627 | out_of_memory, |
fb01d1fb | 2628 | POOL_INTERN))) |
abdcb21a WD |
2629 | out_of_memory(msg); |
2630 | } else { | |
c7e6f84f WD |
2631 | /* This is a doubly linked list with prev looping back to |
2632 | * the end of the list, but the last next pointer is NULL. */ | |
abdcb21a WD |
2633 | if (!first_flist) { |
2634 | flist->file_pool = pool_create(NORMAL_EXTENT, 0, | |
a02348b5 | 2635 | out_of_memory, |
fb01d1fb | 2636 | POOL_INTERN); |
abdcb21a WD |
2637 | if (!flist->file_pool) |
2638 | out_of_memory(msg); | |
2639 | ||
65b4e4b2 | 2640 | flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0; |
4e421735 | 2641 | |
c7e6f84f | 2642 | first_flist = cur_flist = flist->prev = flist; |
abdcb21a | 2643 | } else { |
65b4e4b2 WD |
2644 | struct file_list *prev = first_flist->prev; |
2645 | ||
abdcb21a WD |
2646 | flist->file_pool = first_flist->file_pool; |
2647 | ||
65b4e4b2 WD |
2648 | flist->ndx_start = prev->ndx_start + prev->used + 1; |
2649 | flist->flist_num = prev->flist_num + 1; | |
abdcb21a | 2650 | |
65b4e4b2 WD |
2651 | flist->prev = prev; |
2652 | prev->next = first_flist->prev = flist; | |
c7e6f84f | 2653 | } |
abdcb21a | 2654 | flist->pool_boundary = pool_boundary(flist->file_pool, 0); |
c7e6f84f WD |
2655 | flist_cnt++; |
2656 | } | |
2657 | ||
3d382777 AT |
2658 | return flist; |
2659 | } | |
ebed4c3a | 2660 | |
4785cd43 | 2661 | /* Free up all elements in a flist. */ |
3ec4dd97 AT |
2662 | void flist_free(struct file_list *flist) |
2663 | { | |
abdcb21a WD |
2664 | if (!flist->prev) { |
2665 | /* Was FLIST_TEMP dir-list. */ | |
2666 | } else if (flist == flist->prev) { | |
c7e6f84f WD |
2667 | first_flist = cur_flist = NULL; |
2668 | file_total = 0; | |
2669 | flist_cnt = 0; | |
2670 | } else { | |
2671 | if (flist == cur_flist) | |
2672 | cur_flist = flist->next; | |
2673 | if (flist == first_flist) | |
2674 | first_flist = first_flist->next; | |
2675 | else { | |
2676 | flist->prev->next = flist->next; | |
2677 | if (!flist->next) | |
2678 | flist->next = first_flist; | |
2679 | } | |
2680 | flist->next->prev = flist->prev; | |
9decb4d2 | 2681 | file_total -= flist->used; |
c7e6f84f WD |
2682 | flist_cnt--; |
2683 | } | |
2684 | ||
abdcb21a WD |
2685 | if (!flist->prev || !flist_cnt) |
2686 | pool_destroy(flist->file_pool); | |
2687 | else | |
2688 | pool_free_old(flist->file_pool, flist->pool_boundary); | |
2689 | ||
332cf6df WD |
2690 | if (flist->sorted && flist->sorted != flist->files) |
2691 | free(flist->sorted); | |
3ec4dd97 | 2692 | free(flist->files); |
3ec4dd97 | 2693 | free(flist); |
c627d613 AT |
2694 | } |
2695 | ||
a6e7b978 WD |
2696 | /* This routine ensures we don't have any duplicate names in our file list. |
2697 | * duplicate names can cause corruption because of the pipelining. */ | |
65b4e4b2 | 2698 | static void flist_sort_and_clean(struct file_list *flist, int strip_root) |
c627d613 | 2699 | { |
85aecef6 | 2700 | char fbuf[MAXPATHLEN]; |
a6e7b978 | 2701 | int i, prev_i; |
c627d613 | 2702 | |
910ee8c9 WD |
2703 | if (!flist) |
2704 | return; | |
9decb4d2 | 2705 | if (flist->used == 0) { |
910ee8c9 | 2706 | flist->high = -1; |
9decb4d2 | 2707 | flist->low = 0; |
3ec4dd97 | 2708 | return; |
910ee8c9 | 2709 | } |
3ec4dd97 | 2710 | |
9decb4d2 | 2711 | fsort(flist->sorted, flist->used); |
ebed4c3a | 2712 | |
a6e7b978 | 2713 | if (!am_sender || inc_recurse) { |
9decb4d2 | 2714 | for (i = prev_i = 0; i < flist->used; i++) { |
a6e7b978 WD |
2715 | if (F_IS_ACTIVE(flist->sorted[i])) { |
2716 | prev_i = i; | |
2717 | break; | |
2718 | } | |
b91b50c0 | 2719 | } |
a6e7b978 WD |
2720 | flist->low = prev_i; |
2721 | } else { | |
9decb4d2 | 2722 | i = prev_i = flist->used - 1; |
a6e7b978 | 2723 | flist->low = 0; |
b91b50c0 | 2724 | } |
a6e7b978 | 2725 | |
9decb4d2 | 2726 | while (++i < flist->used) { |
fe1c19dc | 2727 | int j; |
332cf6df | 2728 | struct file_struct *file = flist->sorted[i]; |
f5db0993 | 2729 | |
96293cf9 | 2730 | if (!F_IS_ACTIVE(file)) |
b91b50c0 | 2731 | continue; |
332cf6df | 2732 | if (f_name_cmp(file, flist->sorted[prev_i]) == 0) |
fe1c19dc WD |
2733 | j = prev_i; |
2734 | else if (protocol_version >= 29 && S_ISDIR(file->mode)) { | |
f5db0993 WD |
2735 | int save_mode = file->mode; |
2736 | /* Make sure that this directory doesn't duplicate a | |
2737 | * non-directory earlier in the list. */ | |
f5db0993 | 2738 | flist->high = prev_i; |
fe1c19dc WD |
2739 | file->mode = S_IFREG; |
2740 | j = flist_find(flist, file); | |
f5db0993 | 2741 | file->mode = save_mode; |
fe1c19dc WD |
2742 | } else |
2743 | j = -1; | |
2744 | if (j >= 0) { | |
fe1c19dc WD |
2745 | int keep, drop; |
2746 | /* If one is a dir and the other is not, we want to | |
2747 | * keep the dir because it might have contents in the | |
e8e9e12c WD |
2748 | * list. Otherwise keep the first one. */ |
2749 | if (S_ISDIR(file->mode)) { | |
2750 | struct file_struct *fp = flist->sorted[j]; | |
3932c423 | 2751 | if (!S_ISDIR(fp->mode)) |
e8e9e12c WD |
2752 | keep = i, drop = j; |
2753 | else { | |
2754 | if (am_sender) | |
2755 | file->flags |= FLAG_DUPLICATE; | |
3f309272 WD |
2756 | else { /* Make sure we merge our vital flags. */ |
2757 | fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR); | |
2758 | fp->flags &= file->flags | ~FLAG_IMPLIED_DIR; | |
2759 | } | |
e8e9e12c WD |
2760 | keep = j, drop = i; |
2761 | } | |
2762 | } else | |
fe1c19dc | 2763 | keep = j, drop = i; |
fe1c19dc | 2764 | |
e8e9e12c | 2765 | if (!am_sender) { |
951e826b | 2766 | if (DEBUG_GTE(DUP, 1)) { |
a6e7b978 WD |
2767 | rprintf(FINFO, |
2768 | "removing duplicate name %s from file list (%d)\n", | |
ee83e1bd | 2769 | f_name(file, fbuf), drop + flist->ndx_start); |
a6e7b978 | 2770 | } |
a6e7b978 WD |
2771 | clear_file(flist->sorted[drop]); |
2772 | } | |
9935066b | 2773 | |
fe1c19dc WD |
2774 | if (keep == i) { |
2775 | if (flist->low == drop) { | |
2776 | for (j = drop + 1; | |
332cf6df | 2777 | j < i && !F_IS_ACTIVE(flist->sorted[j]); |
fe1c19dc WD |
2778 | j++) {} |
2779 | flist->low = j; | |
2780 | } | |
2781 | prev_i = i; | |
2782 | } | |
728d0922 | 2783 | } else |
6931c138 | 2784 | prev_i = i; |
3ec4dd97 | 2785 | } |
a6e7b978 | 2786 | flist->high = prev_i; |
0199b05f | 2787 | |
c0b134a4 WD |
2788 | if (strip_root) { |
2789 | /* We need to strip off the leading slashes for relative | |
2790 | * paths, but this must be done _after_ the sorting phase. */ | |
2791 | for (i = flist->low; i <= flist->high; i++) { | |
332cf6df | 2792 | struct file_struct *file = flist->sorted[i]; |
c0b134a4 WD |
2793 | |
2794 | if (!file->dirname) | |
2795 | continue; | |
2796 | while (*file->dirname == '/') | |
2797 | file->dirname++; | |
2798 | if (!*file->dirname) | |
2799 | file->dirname = NULL; | |
2800 | } | |
2801 | } | |
2802 | ||
a6e7b978 | 2803 | if (prune_empty_dirs && !am_sender) { |
e6ffb966 | 2804 | int j, prev_depth = 0; |
9c000f5e | 2805 | |
e6ffb966 | 2806 | prev_i = 0; /* It's OK that this isn't really true. */ |
9c000f5e | 2807 | |
f5db0993 | 2808 | for (i = flist->low; i <= flist->high; i++) { |
332cf6df | 2809 | struct file_struct *fp, *file = flist->sorted[i]; |
ebed4c3a | 2810 | |
9d737ecb | 2811 | /* This temporarily abuses the F_DEPTH() value for a |
e6ffb966 WD |
2812 | * directory that is in a chain that might get pruned. |
2813 | * We restore the old value if it gets a reprieve. */ | |
9d737ecb | 2814 | if (S_ISDIR(file->mode) && F_DEPTH(file)) { |
e6ffb966 | 2815 | /* Dump empty dirs when coming back down. */ |
9d737ecb | 2816 | for (j = prev_depth; j >= F_DEPTH(file); j--) { |
332cf6df | 2817 | fp = flist->sorted[prev_i]; |
9d737ecb | 2818 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2819 | break; |
9d737ecb | 2820 | prev_i = -F_DEPTH(fp)-1; |
82ad07c4 | 2821 | clear_file(fp); |
9c000f5e | 2822 | } |
9d737ecb | 2823 | prev_depth = F_DEPTH(file); |
85aecef6 WD |
2824 | if (is_excluded(f_name(file, fbuf), 1, |
2825 | ALL_FILTERS)) { | |
e6ffb966 WD |
2826 | /* Keep dirs through this dir. */ |
2827 | for (j = prev_depth-1; ; j--) { | |
332cf6df | 2828 | fp = flist->sorted[prev_i]; |
9d737ecb | 2829 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2830 | break; |
9d737ecb WD |
2831 | prev_i = -F_DEPTH(fp)-1; |
2832 | F_DEPTH(fp) = j; | |
e6ffb966 | 2833 | } |
85aecef6 | 2834 | } else |
9d737ecb | 2835 | F_DEPTH(file) = -prev_i-1; |
e6ffb966 WD |
2836 | prev_i = i; |
2837 | } else { | |
2838 | /* Keep dirs through this non-dir. */ | |
2839 | for (j = prev_depth; ; j--) { | |
332cf6df | 2840 | fp = flist->sorted[prev_i]; |
9d737ecb | 2841 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2842 | break; |
9d737ecb WD |
2843 | prev_i = -F_DEPTH(fp)-1; |
2844 | F_DEPTH(fp) = j; | |
e6ffb966 | 2845 | } |
0199b05f | 2846 | } |
9c000f5e | 2847 | } |
ca947dea | 2848 | /* Dump all remaining empty dirs. */ |
e6ffb966 | 2849 | while (1) { |
332cf6df | 2850 | struct file_struct *fp = flist->sorted[prev_i]; |
9d737ecb | 2851 | if (F_DEPTH(fp) >= 0) |
e6ffb966 | 2852 | break; |
9d737ecb | 2853 | prev_i = -F_DEPTH(fp)-1; |
82ad07c4 | 2854 | clear_file(fp); |
9c000f5e | 2855 | } |
f5db0993 | 2856 | |
9c000f5e | 2857 | for (i = flist->low; i <= flist->high; i++) { |
332cf6df | 2858 | if (F_IS_ACTIVE(flist->sorted[i])) |
9c000f5e WD |
2859 | break; |
2860 | } | |
2861 | flist->low = i; | |
2862 | for (i = flist->high; i >= flist->low; i--) { | |
332cf6df | 2863 | if (F_IS_ACTIVE(flist->sorted[i])) |
9c000f5e | 2864 | break; |
0199b05f | 2865 | } |
9c000f5e | 2866 | flist->high = i; |
0199b05f | 2867 | } |
cefed3e8 | 2868 | } |
0199b05f | 2869 | |
32cbfe7b | 2870 | static void output_flist(struct file_list *flist) |
cefed3e8 | 2871 | { |
f3c3ed44 | 2872 | char uidbuf[16], gidbuf[16], depthbuf[16]; |
cefed3e8 | 2873 | struct file_struct *file; |
9d737ecb | 2874 | const char *root, *dir, *slash, *name, *trail; |
32cbfe7b | 2875 | const char *who = who_am_i(); |
cefed3e8 | 2876 | int i; |
0199b05f | 2877 | |
65b4e4b2 WD |
2878 | rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n", |
2879 | who, flist->ndx_start, flist->used, flist->low, flist->high); | |
9decb4d2 | 2880 | for (i = 0; i < flist->used; i++) { |
2ad3c717 | 2881 | file = flist->files[i]; |
9b25ef35 | 2882 | if ((am_root || am_sender) && uid_ndx) { |
1564cd5a WD |
2883 | snprintf(uidbuf, sizeof uidbuf, " uid=%u", |
2884 | F_OWNER(file)); | |
82ad07c4 | 2885 | } else |
f05f993e | 2886 | *uidbuf = '\0'; |
9b25ef35 | 2887 | if (gid_ndx) { |
1564cd5a WD |
2888 | static char parens[] = "(\0)\0\0\0"; |
2889 | char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3); | |
2890 | snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s", | |
2891 | pp, F_GROUP(file), pp + 2); | |
82ad07c4 | 2892 | } else |
f05f993e | 2893 | *gidbuf = '\0'; |
f3c3ed44 | 2894 | if (!am_sender) |
9d737ecb | 2895 | snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file)); |
96293cf9 | 2896 | if (F_IS_ACTIVE(file)) { |
ef35abb2 | 2897 | root = am_sender ? NS(F_PATHNAME(file)) : depthbuf; |
96293cf9 WD |
2898 | if ((dir = file->dirname) == NULL) |
2899 | dir = slash = ""; | |
2900 | else | |
2901 | slash = "/"; | |
c58c1dc4 | 2902 | name = file->basename; |
96293cf9 WD |
2903 | trail = S_ISDIR(file->mode) ? "/" : ""; |
2904 | } else | |
9d737ecb | 2905 | root = dir = slash = name = trail = ""; |
ee83e1bd | 2906 | rprintf(FINFO, |
6d56efa6 | 2907 | "[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n", |
ee83e1bd WD |
2908 | who, i + flist->ndx_start, |
2909 | root, dir, slash, name, trail, | |
adc2476f | 2910 | (int)file->mode, comma_num(F_LENGTH(file)), |
ee83e1bd | 2911 | uidbuf, gidbuf, file->flags); |
0199b05f | 2912 | } |
3ec4dd97 AT |
2913 | } |
2914 | ||
8824e2ce | 2915 | enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING }; |
f5db0993 | 2916 | enum fnc_type { t_PATH, t_ITEM }; |
8018edd3 | 2917 | |
569e6f43 WD |
2918 | static int found_prefix; |
2919 | ||
2f3cad89 | 2920 | /* Compare the names of two file_struct entities, similar to how strcmp() |
f5db0993 WD |
2921 | * would do if it were operating on the joined strings. |
2922 | * | |
2923 | * Some differences beginning with protocol_version 29: (1) directory names | |
2924 | * are compared with an assumed trailing slash so that they compare in a | |
2925 | * way that would cause them to sort immediately prior to any content they | |
2926 | * may have; (2) a directory of any name compares after a non-directory of | |
2927 | * any name at the same depth; (3) a directory with name "." compares prior | |
2928 | * to anything else. These changes mean that a directory and a non-dir | |
2929 | * with the same name will not compare as equal (protocol_version >= 29). | |
2930 | * | |
2931 | * The dirname component can be an empty string, but the basename component | |
2932 | * cannot (and never is in the current codebase). The basename component | |
2933 | * may be NULL (for a removed item), in which case it is considered to be | |
2934 | * after any existing item. */ | |
569e6f43 | 2935 | int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2) |
3ec4dd97 | 2936 | { |
8018edd3 WD |
2937 | int dif; |
2938 | const uchar *c1, *c2; | |
1ef00d20 | 2939 | enum fnc_state state1, state2; |
f5db0993 WD |
2940 | enum fnc_type type1, type2; |
2941 | enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM; | |
8018edd3 | 2942 | |
96293cf9 WD |
2943 | if (!f1 || !F_IS_ACTIVE(f1)) { |
2944 | if (!f2 || !F_IS_ACTIVE(f2)) | |
8018edd3 WD |
2945 | return 0; |
2946 | return -1; | |
2947 | } | |
96293cf9 | 2948 | if (!f2 || !F_IS_ACTIVE(f2)) |
8018edd3 WD |
2949 | return 1; |
2950 | ||
14698a3a WD |
2951 | c1 = (uchar*)f1->dirname; |
2952 | c2 = (uchar*)f2->dirname; | |
2953 | if (c1 == c2) | |
2954 | c1 = c2 = NULL; | |
2955 | if (!c1) { | |
f5db0993 | 2956 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2957 | c1 = (const uchar*)f1->basename; |
f5db0993 WD |
2958 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
2959 | type1 = t_ITEM; | |
2960 | state1 = s_TRAILING; | |
2961 | c1 = (uchar*)""; | |
2962 | } else | |
2963 | state1 = s_BASE; | |
f5db0993 WD |
2964 | } else { |
2965 | type1 = t_path; | |
8824e2ce | 2966 | state1 = s_DIR; |
f5db0993 | 2967 | } |
14698a3a | 2968 | if (!c2) { |
f5db0993 | 2969 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2970 | c2 = (const uchar*)f2->basename; |
f5db0993 WD |
2971 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
2972 | type2 = t_ITEM; | |
2973 | state2 = s_TRAILING; | |
2974 | c2 = (uchar*)""; | |
2975 | } else | |
2976 | state2 = s_BASE; | |
f5db0993 WD |
2977 | } else { |
2978 | type2 = t_path; | |
8824e2ce | 2979 | state2 = s_DIR; |
f5db0993 WD |
2980 | } |
2981 | ||
2982 | if (type1 != type2) | |
2983 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 2984 | |
e4fdf1de | 2985 | do { |
f5db0993 | 2986 | if (!*c1) { |
8018edd3 | 2987 | switch (state1) { |
8824e2ce WD |
2988 | case s_DIR: |
2989 | state1 = s_SLASH; | |
e90b8ace | 2990 | c1 = (uchar*)"/"; |
8018edd3 | 2991 | break; |
8824e2ce | 2992 | case s_SLASH: |
f5db0993 | 2993 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
c58c1dc4 | 2994 | c1 = (const uchar*)f1->basename; |
cbb5fa4f WD |
2995 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
2996 | type1 = t_ITEM; | |
2997 | state1 = s_TRAILING; | |
2998 | c1 = (uchar*)""; | |
2999 | } else | |
3000 | state1 = s_BASE; | |
8018edd3 | 3001 | break; |
8824e2ce WD |
3002 | case s_BASE: |
3003 | state1 = s_TRAILING; | |
f5db0993 | 3004 | if (type1 == t_PATH) { |
2f3cad89 | 3005 | c1 = (uchar*)"/"; |
f5db0993 WD |
3006 | break; |
3007 | } | |
3008 | /* FALL THROUGH */ | |
8824e2ce | 3009 | case s_TRAILING: |
f5db0993 | 3010 | type1 = t_ITEM; |
8018edd3 WD |
3011 | break; |
3012 | } | |
f5db0993 WD |
3013 | if (*c2 && type1 != type2) |
3014 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 3015 | } |
f5db0993 | 3016 | if (!*c2) { |
8018edd3 | 3017 | switch (state2) { |
8824e2ce WD |
3018 | case s_DIR: |
3019 | state2 = s_SLASH; | |
e90b8ace | 3020 | c2 = (uchar*)"/"; |
8018edd3 | 3021 | break; |
8824e2ce | 3022 | case s_SLASH: |
f5db0993 | 3023 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
c58c1dc4 | 3024 | c2 = (const uchar*)f2->basename; |
cbb5fa4f WD |
3025 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
3026 | type2 = t_ITEM; | |
3027 | state2 = s_TRAILING; | |
3028 | c2 = (uchar*)""; | |
3029 | } else | |
3030 | state2 = s_BASE; | |
8018edd3 | 3031 | break; |
8824e2ce | 3032 | case s_BASE: |
8824e2ce | 3033 | state2 = s_TRAILING; |
f5db0993 | 3034 | if (type2 == t_PATH) { |
2f3cad89 | 3035 | c2 = (uchar*)"/"; |
f5db0993 WD |
3036 | break; |
3037 | } | |
3038 | /* FALL THROUGH */ | |
8824e2ce | 3039 | case s_TRAILING: |
569e6f43 | 3040 | found_prefix = 1; |
f5db0993 WD |
3041 | if (!*c1) |
3042 | return 0; | |
3043 | type2 = t_ITEM; | |
8018edd3 WD |
3044 | break; |
3045 | } | |
f5db0993 WD |
3046 | if (type1 != type2) |
3047 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 3048 | } |
e4fdf1de | 3049 | } while ((dif = (int)*c1++ - (int)*c2++) == 0); |
8018edd3 WD |
3050 | |
3051 | return dif; | |
3052 | } | |
3053 | ||
569e6f43 WD |
3054 | /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does |
3055 | * not match if f2's basename is not an exact match of a path element in f1. | |
3056 | * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */ | |
3057 | int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2) | |
3058 | { | |
3059 | found_prefix = 0; | |
3060 | f_name_cmp(f1, f2); | |
3061 | return found_prefix; | |
3062 | } | |
3063 | ||
96293cf9 WD |
3064 | char *f_name_buf(void) |
3065 | { | |
3066 | static char names[5][MAXPATHLEN]; | |
3067 | static unsigned int n; | |
3068 | ||
3069 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
3070 | ||
3071 | return names[n]; | |
3072 | } | |
3073 | ||
8018edd3 | 3074 | /* Return a copy of the full filename of a flist entry, using the indicated |
5e4ff5f9 WD |
3075 | * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is |
3076 | * done because we checked the size when creating the file_struct entry. | |
8018edd3 | 3077 | */ |
569e6f43 | 3078 | char *f_name(const struct file_struct *f, char *fbuf) |
8018edd3 | 3079 | { |
96293cf9 | 3080 | if (!f || !F_IS_ACTIVE(f)) |
ebed4c3a | 3081 | return NULL; |
3ec4dd97 | 3082 | |
96293cf9 WD |
3083 | if (!fbuf) |
3084 | fbuf = f_name_buf(); | |
5e4ff5f9 | 3085 | |
3ec4dd97 | 3086 | if (f->dirname) { |
882e6893 WD |
3087 | int len = strlen(f->dirname); |
3088 | memcpy(fbuf, f->dirname, len); | |
3089 | fbuf[len] = '/'; | |
c58c1dc4 | 3090 | strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1)); |
8018edd3 | 3091 | } else |
c58c1dc4 | 3092 | strlcpy(fbuf, f->basename, MAXPATHLEN); |
0ee6ca98 | 3093 | |
b7736c79 | 3094 | return fbuf; |
8018edd3 | 3095 | } |
e03dfae5 | 3096 | |
32cbfe7b WD |
3097 | /* Do a non-recursive scan of the named directory, possibly ignoring all |
3098 | * exclude rules except for the daemon's. If "dlen" is >=0, it is the length | |
3099 | * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN | |
3100 | * buffer (the functions we call will append names onto the end, but the old | |
3101 | * dir value will be restored on exit). */ | |
263d3bfb | 3102 | struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules) |
37802f40 WD |
3103 | { |
3104 | struct file_list *dirlist; | |
3105 | char dirbuf[MAXPATHLEN]; | |
37802f40 | 3106 | int save_recurse = recurse; |
1661fe9b | 3107 | int save_xfer_dirs = xfer_dirs; |
4635fb99 | 3108 | int save_prune_empty_dirs = prune_empty_dirs; |
37802f40 | 3109 | |
32cbfe7b WD |
3110 | if (dlen < 0) { |
3111 | dlen = strlcpy(dirbuf, dirname, MAXPATHLEN); | |
3112 | if (dlen >= MAXPATHLEN) | |
3113 | return NULL; | |
3114 | dirname = dirbuf; | |
3115 | } | |
37802f40 | 3116 | |
c7e6f84f | 3117 | dirlist = flist_new(FLIST_TEMP, "get_dirlist"); |
32cbfe7b | 3118 | |
37802f40 | 3119 | recurse = 0; |
1661fe9b | 3120 | xfer_dirs = 1; |
1a85a969 | 3121 | send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, FLAG_CONTENT_DIR); |
1661fe9b | 3122 | xfer_dirs = save_xfer_dirs; |
37802f40 | 3123 | recurse = save_recurse; |
951e826b | 3124 | if (INFO_GTE(PROGRESS, 1)) |
9decb4d2 | 3125 | flist_count_offset += dirlist->used; |
37802f40 | 3126 | |
4635fb99 | 3127 | prune_empty_dirs = 0; |
332cf6df | 3128 | dirlist->sorted = dirlist->files; |
65b4e4b2 | 3129 | flist_sort_and_clean(dirlist, 0); |
4635fb99 | 3130 | prune_empty_dirs = save_prune_empty_dirs; |
564ef546 | 3131 | |
951e826b | 3132 | if (DEBUG_GTE(FLIST, 3)) |
32cbfe7b | 3133 | output_flist(dirlist); |
45478cc7 | 3134 | |
32cbfe7b | 3135 | return dirlist; |
45478cc7 | 3136 | } |