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