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