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