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