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