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