Commit | Line | Data |
---|---|---|
dbda5fbf | 1 | /* |
c627d613 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
736a6a29 | 4 | Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> |
dbda5fbf | 5 | |
c627d613 AT |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
dbda5fbf | 10 | |
c627d613 AT |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
dbda5fbf | 15 | |
c627d613 AT |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
736a6a29 MP |
21 | /** @file flist.c |
22 | * Generate and receive file lists | |
23 | * | |
172875cf MP |
24 | * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html |
25 | * | |
736a6a29 | 26 | **/ |
c627d613 AT |
27 | |
28 | #include "rsync.h" | |
29 | ||
30 | extern int verbose; | |
34e18ecd WD |
31 | extern int dry_run; |
32 | extern int list_only; | |
f05f993e | 33 | extern int am_root; |
c627d613 | 34 | extern int am_server; |
a8726d2a | 35 | extern int am_daemon; |
56194bcd | 36 | extern int am_sender; |
8715db2c | 37 | extern int do_progress; |
c627d613 | 38 | extern int always_checksum; |
983b1ed3 WD |
39 | extern int module_id; |
40 | extern int ignore_errors; | |
4836c3ee | 41 | extern int numeric_ids; |
a06d19e3 | 42 | extern int recurse; |
7e037c42 | 43 | extern int xfer_dirs; |
24d0fcde | 44 | extern int filesfrom_fd; |
c627d613 | 45 | extern int one_file_system; |
314f4591 | 46 | extern int keep_dirlinks; |
c627d613 | 47 | extern int preserve_links; |
dc5ddbcc | 48 | extern int preserve_hard_links; |
c627d613 AT |
49 | extern int preserve_perms; |
50 | extern int preserve_devices; | |
b5c6a6ae | 51 | extern int preserve_specials; |
c627d613 AT |
52 | extern int preserve_uid; |
53 | extern int preserve_gid; | |
6574b4f7 | 54 | extern int relative_paths; |
24d0fcde | 55 | extern int implied_dirs; |
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; |
314f4591 | 61 | extern int orig_umask; |
d64e6f42 WD |
62 | extern struct stats stats; |
63 | extern struct file_list *the_file_list; | |
c627d613 | 64 | |
e1f40891 WD |
65 | extern char curr_dir[MAXPATHLEN]; |
66 | ||
2b7e0f33 WD |
67 | extern struct chmod_mode_struct *chmod_modes; |
68 | ||
7842418b WD |
69 | extern struct filter_list_struct filter_list; |
70 | extern struct filter_list_struct server_filter_list; | |
c627d613 | 71 | |
06c28400 | 72 | int io_error; |
7752df41 | 73 | int checksum_len; |
535737bf | 74 | dev_t filesystem_dev; /* used to implement -x */ |
5e58e3f9 | 75 | unsigned int file_struct_len; |
06c28400 | 76 | |
fea4db62 | 77 | static char empty_sum[MD4_SUM_LENGTH]; |
53135fe8 | 78 | static int flist_count_offset; |
9c000f5e | 79 | static int max_dir_depth = 0; |
3d382777 | 80 | |
827c37f6 | 81 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups); |
32cbfe7b | 82 | static void output_flist(struct file_list *flist); |
0199b05f | 83 | |
61dec11a WD |
84 | void init_flist(void) |
85 | { | |
1f9ae80a | 86 | struct file_struct f; |
61dec11a | 87 | |
1f9ae80a | 88 | /* Figure out how big the file_struct is without trailing padding */ |
7cf8e8d0 | 89 | file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags; |
7752df41 | 90 | checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH; |
61dec11a WD |
91 | } |
92 | ||
1bbd10fe | 93 | static int show_filelist_p(void) |
58225000 | 94 | { |
7e037c42 | 95 | return verbose && xfer_dirs && !am_server; |
1bbd10fe | 96 | } |
ebed4c3a | 97 | |
1bbd10fe DD |
98 | static void start_filelist_progress(char *kind) |
99 | { | |
100 | rprintf(FINFO, "%s ... ", kind); | |
e5ce3bcf | 101 | if (verbose > 1 || do_progress) |
1bbd10fe DD |
102 | rprintf(FINFO, "\n"); |
103 | rflush(FINFO); | |
58225000 MP |
104 | } |
105 | ||
53135fe8 | 106 | static void emit_filelist_progress(int count) |
db719fb0 | 107 | { |
53135fe8 | 108 | rprintf(FINFO, " %d files...\r", count); |
db719fb0 MP |
109 | } |
110 | ||
53135fe8 | 111 | static void maybe_emit_filelist_progress(int count) |
58225000 | 112 | { |
53135fe8 WD |
113 | if (do_progress && show_filelist_p() && (count % 100) == 0) |
114 | emit_filelist_progress(count); | |
58225000 MP |
115 | } |
116 | ||
1bbd10fe | 117 | static void finish_filelist_progress(const struct file_list *flist) |
58225000 | 118 | { |
1bbd10fe DD |
119 | if (do_progress) { |
120 | /* This overwrites the progress line */ | |
c7b562be MP |
121 | rprintf(FINFO, "%d file%sto consider\n", |
122 | flist->count, flist->count == 1 ? " " : "s "); | |
b7736c79 | 123 | } else |
1bbd10fe | 124 | rprintf(FINFO, "done\n"); |
58225000 MP |
125 | } |
126 | ||
86943126 MP |
127 | void show_flist_stats(void) |
128 | { | |
129 | /* Nothing yet */ | |
130 | } | |
131 | ||
f7632fc6 AT |
132 | static void list_file_entry(struct file_struct *f) |
133 | { | |
740819ef | 134 | char perms[11]; |
f7632fc6 | 135 | |
e5ce3bcf | 136 | if (!f->basename) { |
7212be92 DD |
137 | /* this can happen if duplicate names were removed */ |
138 | return; | |
e5ce3bcf | 139 | } |
7212be92 | 140 | |
740819ef MP |
141 | permstring(perms, f->mode); |
142 | ||
4f5b0756 | 143 | #ifdef SUPPORT_LINKS |
f7632fc6 | 144 | if (preserve_links && S_ISLNK(f->mode)) { |
ebed4c3a MP |
145 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
146 | perms, | |
a4a7e64c | 147 | (double)f->length, timestring(f->modtime), |
5e4ff5f9 | 148 | f_name(f, NULL), f->u.link); |
0d162bd1 WD |
149 | } else |
150 | #endif | |
e5ce3bcf | 151 | { |
ebed4c3a MP |
152 | rprintf(FINFO, "%s %11.0f %s %s\n", |
153 | perms, | |
a4a7e64c | 154 | (double)f->length, timestring(f->modtime), |
5e4ff5f9 | 155 | f_name(f, NULL)); |
e5ce3bcf | 156 | } |
f7632fc6 AT |
157 | } |
158 | ||
bd9e9ecc MP |
159 | /** |
160 | * Stat either a symlink or its referent, depending on the settings of | |
161 | * copy_links, copy_unsafe_links, etc. | |
162 | * | |
4e5db0ad MP |
163 | * @retval -1 on error |
164 | * | |
165 | * @retval 0 for success | |
166 | * | |
167 | * @post If @p path is a symlink, then @p linkbuf (of size @c | |
bd9e9ecc | 168 | * MAXPATHLEN) contains the symlink target. |
4e5db0ad MP |
169 | * |
170 | * @post @p buffer contains information about the link or the | |
171 | * referrent as appropriate, if they exist. | |
bd9e9ecc | 172 | **/ |
23f4587f | 173 | static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf) |
b5313607 | 174 | { |
4f5b0756 | 175 | #ifdef SUPPORT_LINKS |
b7736c79 | 176 | if (copy_links) |
6f2623fd | 177 | return do_stat(path, buffer); |
23f4587f | 178 | if (link_stat(path, buffer, 0) < 0) |
b5313607 | 179 | return -1; |
6f2623fd | 180 | if (S_ISLNK(buffer->st_mode)) { |
a4a7e64c | 181 | int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1); |
dbda5fbf | 182 | if (l == -1) |
b5313607 | 183 | return -1; |
6f2623fd | 184 | linkbuf[l] = 0; |
fc638474 DD |
185 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
186 | if (verbose > 1) { | |
dbda5fbf | 187 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
0ee6ca98 | 188 | path, linkbuf); |
fc638474 | 189 | } |
6f2623fd | 190 | return do_stat(path, buffer); |
b5313607 DD |
191 | } |
192 | } | |
193 | return 0; | |
194 | #else | |
6f2623fd | 195 | return do_stat(path, buffer); |
b5313607 DD |
196 | #endif |
197 | } | |
198 | ||
314f4591 | 199 | int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks) |
82306bf6 | 200 | { |
4f5b0756 | 201 | #ifdef SUPPORT_LINKS |
b7736c79 | 202 | if (copy_links) |
6f2623fd | 203 | return do_stat(path, buffer); |
314f4591 WD |
204 | if (do_lstat(path, buffer) < 0) |
205 | return -1; | |
206 | if (follow_dirlinks && S_ISLNK(buffer->st_mode)) { | |
207 | STRUCT_STAT st; | |
208 | if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode)) | |
209 | *buffer = st; | |
210 | } | |
211 | return 0; | |
82306bf6 | 212 | #else |
6f2623fd | 213 | return do_stat(path, buffer); |
82306bf6 AT |
214 | #endif |
215 | } | |
216 | ||
7842418b | 217 | /* This function is used to check if a file should be included/excluded |
429f9828 | 218 | * from the list of files based on its name and type etc. The value of |
7842418b WD |
219 | * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */ |
220 | static int is_excluded(char *fname, int is_dir, int filter_level) | |
c627d613 | 221 | { |
7d687932 | 222 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
7842418b | 223 | if (filter_level == NO_FILTERS) |
429f9828 WD |
224 | return 0; |
225 | #endif | |
6931c138 | 226 | if (fname) { |
429f9828 | 227 | /* never exclude '.', even if somebody does --exclude '*' */ |
6931c138 WD |
228 | if (fname[0] == '.' && !fname[1]) |
229 | return 0; | |
230 | /* Handle the -R version of the '.' dir. */ | |
231 | if (fname[0] == '/') { | |
232 | int len = strlen(fname); | |
233 | if (fname[len-1] == '.' && fname[len-2] == '/') | |
234 | return 0; | |
235 | } | |
76e26e10 | 236 | } |
7842418b WD |
237 | if (server_filter_list.head |
238 | && check_filter(&server_filter_list, fname, is_dir) < 0) | |
429f9828 | 239 | return 1; |
7842418b | 240 | if (filter_level != ALL_FILTERS) |
429f9828 | 241 | return 0; |
7842418b WD |
242 | if (filter_list.head |
243 | && check_filter(&filter_list, fname, is_dir) < 0) | |
76e26e10 | 244 | return 1; |
76e26e10 | 245 | return 0; |
c627d613 AT |
246 | } |
247 | ||
b280a1f4 AT |
248 | static int to_wire_mode(mode_t mode) |
249 | { | |
4f5b0756 | 250 | #ifdef SUPPORT_LINKS |
b7736c79 | 251 | if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) |
b280a1f4 | 252 | return (mode & ~(_S_IFMT)) | 0120000; |
0d162bd1 | 253 | #endif |
a4a7e64c | 254 | return (int)mode; |
b280a1f4 AT |
255 | } |
256 | ||
257 | static mode_t from_wire_mode(int mode) | |
258 | { | |
b7736c79 | 259 | if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) |
efe3037c | 260 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
a4a7e64c | 261 | return (mode_t)mode; |
b280a1f4 AT |
262 | } |
263 | ||
f1773e09 | 264 | static void send_directory(int f, struct file_list *flist, |
32cbfe7b | 265 | char *fbuf, int len); |
c627d613 | 266 | |
3a6a366f | 267 | static char *flist_dir; |
882e6893 | 268 | static int flist_dir_len; |
c627d613 | 269 | |
3ec4dd97 | 270 | |
d9d6bc52 MP |
271 | /** |
272 | * Make sure @p flist is big enough to hold at least @p flist->count | |
273 | * entries. | |
274 | **/ | |
a85906c7 | 275 | void flist_expand(struct file_list *flist) |
d9d6bc52 | 276 | { |
0501f363 | 277 | struct file_struct **new_ptr; |
2e7d1994 | 278 | |
a85906c7 S |
279 | if (flist->count < flist->malloced) |
280 | return; | |
dbda5fbf | 281 | |
a85906c7 S |
282 | if (flist->malloced < FLIST_START) |
283 | flist->malloced = FLIST_START; | |
284 | else if (flist->malloced >= FLIST_LINEAR) | |
285 | flist->malloced += FLIST_LINEAR; | |
286 | else | |
287 | flist->malloced *= 2; | |
288 | ||
289 | /* | |
290 | * In case count jumped or we are starting the list | |
291 | * with a known size just set it. | |
292 | */ | |
293 | if (flist->malloced < flist->count) | |
294 | flist->malloced = flist->count; | |
295 | ||
0501f363 WD |
296 | new_ptr = realloc_array(flist->files, struct file_struct *, |
297 | flist->malloced); | |
2e7d1994 | 298 | |
8c483820 | 299 | if (verbose >= 2 && flist->malloced != FLIST_START) { |
a85906c7 S |
300 | rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n", |
301 | who_am_i(), | |
a4a7e64c | 302 | (double)sizeof flist->files[0] * flist->malloced, |
a85906c7 | 303 | (new_ptr == flist->files) ? " not" : ""); |
d9d6bc52 | 304 | } |
a85906c7 | 305 | |
0501f363 | 306 | flist->files = new_ptr; |
a85906c7 S |
307 | |
308 | if (!flist->files) | |
309 | out_of_memory("flist_expand"); | |
d9d6bc52 MP |
310 | } |
311 | ||
96b87581 | 312 | static void send_file_entry(struct file_struct *file, int f) |
c627d613 | 313 | { |
1ef00d20 | 314 | unsigned short flags; |
5911fee5 WD |
315 | static time_t modtime; |
316 | static mode_t mode; | |
1490812a | 317 | static int64 dev; |
4124540d | 318 | static dev_t rdev; |
9c5e91f8 | 319 | static uint32 rdev_major; |
5911fee5 WD |
320 | static uid_t uid; |
321 | static gid_t gid; | |
322 | static char lastname[MAXPATHLEN]; | |
f376e674 | 323 | char fname[MAXPATHLEN]; |
ebed4c3a | 324 | int l1, l2; |
72914a60 | 325 | |
37802f40 | 326 | if (f < 0) |
ebed4c3a | 327 | return; |
72914a60 AT |
328 | |
329 | if (!file) { | |
ebed4c3a | 330 | write_byte(f, 0); |
5911fee5 | 331 | modtime = 0, mode = 0; |
4124540d | 332 | dev = 0, rdev = makedev(0, 0); |
9c5e91f8 | 333 | rdev_major = 0; |
5911fee5 WD |
334 | uid = 0, gid = 0; |
335 | *lastname = '\0'; | |
72914a60 AT |
336 | return; |
337 | } | |
338 | ||
eca2adb4 MP |
339 | io_write_phase = "send_file_entry"; |
340 | ||
5e4ff5f9 | 341 | f_name(file, fname); |
72914a60 | 342 | |
96b87581 | 343 | flags = file->flags & XMIT_TOP_DIR; |
72914a60 | 344 | |
30f337c9 | 345 | if (file->mode == mode) |
d01d15e0 | 346 | flags |= XMIT_SAME_MODE; |
1ef00d20 | 347 | else |
30f337c9 | 348 | mode = file->mode; |
b5c6a6ae WD |
349 | if ((preserve_devices && IS_DEVICE(mode)) |
350 | || (preserve_specials && IS_SPECIAL(mode))) { | |
75bc8600 | 351 | if (protocol_version < 28) { |
b5c6a6ae WD |
352 | if (file->u.rdev == rdev) |
353 | flags |= XMIT_SAME_RDEV_pre28; | |
354 | else | |
355 | rdev = file->u.rdev; | |
356 | } else { | |
84a3efa0 | 357 | rdev = file->u.rdev; |
9c5e91f8 WD |
358 | if ((uint32)major(rdev) == rdev_major) |
359 | flags |= XMIT_SAME_RDEV_MAJOR; | |
84a3efa0 | 360 | else |
9c5e91f8 WD |
361 | rdev_major = major(rdev); |
362 | if ((uint32)minor(rdev) <= 0xFFu) | |
363 | flags |= XMIT_RDEV_MINOR_IS_SMALL; | |
75bc8600 | 364 | } |
b5c6a6ae WD |
365 | } else if (protocol_version < 28) |
366 | rdev = makedev(0, 0); | |
7b6fa00f | 367 | if (file->uid == uid) |
d01d15e0 | 368 | flags |= XMIT_SAME_UID; |
1ef00d20 | 369 | else |
7b6fa00f WD |
370 | uid = file->uid; |
371 | if (file->gid == gid) | |
d01d15e0 | 372 | flags |= XMIT_SAME_GID; |
1ef00d20 | 373 | else |
7b6fa00f | 374 | gid = file->gid; |
30f337c9 | 375 | if (file->modtime == modtime) |
d01d15e0 | 376 | flags |= XMIT_SAME_TIME; |
1ef00d20 | 377 | else |
30f337c9 | 378 | modtime = file->modtime; |
a289addd | 379 | |
4f5b0756 | 380 | #ifdef SUPPORT_HARD_LINKS |
92cc9dd7 WD |
381 | if (file->link_u.idev) { |
382 | if (file->F_DEV == dev) { | |
c4b4df4f | 383 | if (protocol_version >= 28) |
d01d15e0 | 384 | flags |= XMIT_SAME_DEV; |
728d0922 | 385 | } else |
92cc9dd7 | 386 | dev = file->F_DEV; |
d01d15e0 | 387 | flags |= XMIT_HAS_IDEV_DATA; |
c4b4df4f | 388 | } |
0d162bd1 | 389 | #endif |
ebed4c3a MP |
390 | |
391 | for (l1 = 0; | |
3e491682 S |
392 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); |
393 | l1++) {} | |
eddd5d12 | 394 | l2 = strlen(fname+l1); |
72914a60 | 395 | |
ebed4c3a | 396 | if (l1 > 0) |
d01d15e0 | 397 | flags |= XMIT_SAME_NAME; |
ebed4c3a | 398 | if (l2 > 255) |
d01d15e0 | 399 | flags |= XMIT_LONG_NAME; |
72914a60 | 400 | |
1aa4caf3 WD |
401 | /* We must make sure we don't send a zero flag byte or the |
402 | * other end will terminate the flist transfer. Note that | |
ee3751c8 | 403 | * the use of XMIT_TOP_DIR on a non-dir has no meaning, so |
1aa4caf3 | 404 | * it's harmless way to add a bit to the first flag byte. */ |
75bc8600 | 405 | if (protocol_version >= 28) { |
1aa4caf3 | 406 | if (!flags && !S_ISDIR(mode)) |
ee3751c8 | 407 | flags |= XMIT_TOP_DIR; |
1aa4caf3 | 408 | if ((flags & 0xFF00) || !flags) { |
d01d15e0 | 409 | flags |= XMIT_EXTENDED_FLAGS; |
75bc8600 WD |
410 | write_byte(f, flags); |
411 | write_byte(f, flags >> 8); | |
412 | } else | |
413 | write_byte(f, flags); | |
414 | } else { | |
0a982011 | 415 | if (!(flags & 0xFF)) |
75c51953 | 416 | flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR; |
75bc8600 WD |
417 | write_byte(f, flags); |
418 | } | |
d01d15e0 | 419 | if (flags & XMIT_SAME_NAME) |
ebed4c3a | 420 | write_byte(f, l1); |
d01d15e0 | 421 | if (flags & XMIT_LONG_NAME) |
ebed4c3a | 422 | write_int(f, l2); |
72914a60 | 423 | else |
ebed4c3a MP |
424 | write_byte(f, l2); |
425 | write_buf(f, fname + l1, l2); | |
72914a60 | 426 | |
ebed4c3a | 427 | write_longint(f, file->length); |
d01d15e0 | 428 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 429 | write_int(f, modtime); |
d01d15e0 | 430 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 431 | write_int(f, to_wire_mode(mode)); |
d01d15e0 | 432 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
9f7b8c3b WD |
433 | if (!numeric_ids) |
434 | add_uid(uid); | |
30f337c9 | 435 | write_int(f, uid); |
72914a60 | 436 | } |
d01d15e0 | 437 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { |
9f7b8c3b WD |
438 | if (!numeric_ids) |
439 | add_gid(gid); | |
30f337c9 | 440 | write_int(f, gid); |
72914a60 | 441 | } |
b5c6a6ae WD |
442 | if ((preserve_devices && IS_DEVICE(mode)) |
443 | || (preserve_specials && IS_SPECIAL(mode))) { | |
9c5e91f8 WD |
444 | if (protocol_version < 28) { |
445 | if (!(flags & XMIT_SAME_RDEV_pre28)) | |
446 | write_int(f, (int)rdev); | |
447 | } else { | |
448 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) | |
449 | write_int(f, major(rdev)); | |
450 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) | |
451 | write_byte(f, minor(rdev)); | |
452 | else | |
453 | write_int(f, minor(rdev)); | |
454 | } | |
75bc8600 | 455 | } |
c627d613 | 456 | |
4f5b0756 | 457 | #ifdef SUPPORT_LINKS |
30f337c9 | 458 | if (preserve_links && S_ISLNK(mode)) { |
306ffb8c WD |
459 | int len = strlen(file->u.link); |
460 | write_int(f, len); | |
461 | write_buf(f, file->u.link, len); | |
72914a60 | 462 | } |
c627d613 AT |
463 | #endif |
464 | ||
4f5b0756 | 465 | #ifdef SUPPORT_HARD_LINKS |
d01d15e0 | 466 | if (flags & XMIT_HAS_IDEV_DATA) { |
4124540d | 467 | if (protocol_version < 26) { |
736a6a29 | 468 | /* 32-bit dev_t and ino_t */ |
30f337c9 | 469 | write_int(f, dev); |
92cc9dd7 | 470 | write_int(f, file->F_INODE); |
736a6a29 MP |
471 | } else { |
472 | /* 64-bit dev_t and ino_t */ | |
4124540d WD |
473 | if (!(flags & XMIT_SAME_DEV)) |
474 | write_longint(f, dev); | |
92cc9dd7 | 475 | write_longint(f, file->F_INODE); |
736a6a29 | 476 | } |
72914a60 | 477 | } |
dc5ddbcc AT |
478 | #endif |
479 | ||
9a4a237e | 480 | if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) { |
728d0922 WD |
481 | char *sum; |
482 | if (S_ISREG(mode)) | |
483 | sum = file->u.sum; | |
9a4a237e | 484 | else { |
728d0922 WD |
485 | /* Prior to 28, we sent a useless set of nulls. */ |
486 | sum = empty_sum; | |
728d0922 | 487 | } |
7752df41 | 488 | write_buf(f, sum, checksum_len); |
ebed4c3a | 489 | } |
182dca5c | 490 | |
ebed4c3a | 491 | strlcpy(lastname, fname, MAXPATHLEN); |
eca2adb4 MP |
492 | |
493 | io_write_phase = "unknown"; | |
182dca5c AT |
494 | } |
495 | ||
f5db0993 WD |
496 | static struct file_struct *receive_file_entry(struct file_list *flist, |
497 | unsigned short flags, int f) | |
182dca5c | 498 | { |
5911fee5 WD |
499 | static time_t modtime; |
500 | static mode_t mode; | |
1490812a | 501 | static int64 dev; |
4124540d | 502 | static dev_t rdev; |
9c5e91f8 | 503 | static uint32 rdev_major; |
5911fee5 WD |
504 | static uid_t uid; |
505 | static gid_t gid; | |
a289addd | 506 | static char lastname[MAXPATHLEN], *lastdir; |
33ffd7c3 | 507 | static int lastdir_depth, lastdir_len = -1; |
42f23f47 | 508 | static unsigned int del_hier_name_len = 0; |
649f8742 | 509 | static int in_del_hier = 0; |
72914a60 | 510 | char thisname[MAXPATHLEN]; |
ebed4c3a | 511 | unsigned int l1 = 0, l2 = 0; |
a1d55ad0 | 512 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
a289addd WD |
513 | OFF_T file_length; |
514 | char *basename, *dirname, *bp; | |
72914a60 AT |
515 | struct file_struct *file; |
516 | ||
f3c3ed44 | 517 | if (!flist) { |
5911fee5 | 518 | modtime = 0, mode = 0; |
4124540d | 519 | dev = 0, rdev = makedev(0, 0); |
9c5e91f8 | 520 | rdev_major = 0; |
5911fee5 WD |
521 | uid = 0, gid = 0; |
522 | *lastname = '\0'; | |
3db859e8 | 523 | lastdir_len = -1; |
649f8742 | 524 | in_del_hier = 0; |
f5db0993 | 525 | return NULL; |
5911fee5 WD |
526 | } |
527 | ||
d01d15e0 | 528 | if (flags & XMIT_SAME_NAME) |
72914a60 | 529 | l1 = read_byte(f); |
ebed4c3a | 530 | |
d01d15e0 | 531 | if (flags & XMIT_LONG_NAME) |
72914a60 AT |
532 | l2 = read_int(f); |
533 | else | |
534 | l2 = read_byte(f); | |
535 | ||
ebed4c3a MP |
536 | if (l2 >= MAXPATHLEN - l1) { |
537 | rprintf(FERROR, | |
538 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", | |
0ee6ca98 | 539 | flags, l1, l2, lastname); |
a1f99493 | 540 | overflow_exit("receive_file_entry"); |
d0fd26aa | 541 | } |
72914a60 | 542 | |
ebed4c3a MP |
543 | strlcpy(thisname, lastname, l1 + 1); |
544 | read_sbuf(f, &thisname[l1], l2); | |
545 | thisname[l1 + l2] = 0; | |
72914a60 | 546 | |
ebed4c3a | 547 | strlcpy(lastname, thisname, MAXPATHLEN); |
72914a60 | 548 | |
58b1999e | 549 | clean_fname(thisname, 0); |
72914a60 | 550 | |
0d162bd1 | 551 | if (sanitize_paths) |
33ffd7c3 | 552 | sanitize_path(thisname, thisname, "", 0); |
cb13abfe | 553 | |
a289addd WD |
554 | if ((basename = strrchr(thisname, '/')) != NULL) { |
555 | dirname_len = ++basename - thisname; /* counts future '\0' */ | |
556 | if (lastdir_len == dirname_len - 1 | |
557 | && strncmp(thisname, lastdir, lastdir_len) == 0) { | |
558 | dirname = lastdir; | |
559 | dirname_len = 0; /* indicates no copy is needed */ | |
560 | } else | |
561 | dirname = thisname; | |
72914a60 | 562 | } else { |
a289addd WD |
563 | basename = thisname; |
564 | dirname = NULL; | |
565 | dirname_len = 0; | |
72914a60 | 566 | } |
a289addd | 567 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
72914a60 | 568 | |
a289addd | 569 | file_length = read_longint(f); |
d01d15e0 | 570 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 571 | modtime = (time_t)read_int(f); |
d01d15e0 | 572 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 573 | mode = from_wire_mode(read_int(f)); |
1ef00d20 | 574 | |
ebec5eb6 | 575 | if (chmod_modes && !S_ISLNK(mode)) |
8bbe41b5 WD |
576 | mode = tweak_mode(mode, chmod_modes); |
577 | ||
a289addd WD |
578 | if (preserve_uid && !(flags & XMIT_SAME_UID)) |
579 | uid = (uid_t)read_int(f); | |
580 | if (preserve_gid && !(flags & XMIT_SAME_GID)) | |
581 | gid = (gid_t)read_int(f); | |
582 | ||
b5c6a6ae WD |
583 | if ((preserve_devices && IS_DEVICE(mode)) |
584 | || (preserve_specials && IS_SPECIAL(mode))) { | |
75bc8600 | 585 | if (protocol_version < 28) { |
b5c6a6ae WD |
586 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
587 | rdev = (dev_t)read_int(f); | |
588 | } else { | |
9c5e91f8 WD |
589 | uint32 rdev_minor; |
590 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) | |
591 | rdev_major = read_int(f); | |
592 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) | |
593 | rdev_minor = read_byte(f); | |
594 | else | |
595 | rdev_minor = read_int(f); | |
596 | rdev = makedev(rdev_major, rdev_minor); | |
1ef00d20 | 597 | } |
b5c6a6ae WD |
598 | } else if (protocol_version < 28) |
599 | rdev = makedev(0, 0); | |
72914a60 | 600 | |
4f5b0756 | 601 | #ifdef SUPPORT_LINKS |
30f337c9 | 602 | if (preserve_links && S_ISLNK(mode)) { |
a289addd WD |
603 | linkname_len = read_int(f) + 1; /* count the '\0' */ |
604 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { | |
605 | rprintf(FERROR, "overflow: linkname_len=%d\n", | |
606 | linkname_len - 1); | |
a1f99493 | 607 | overflow_exit("receive_file_entry"); |
9dd891bb | 608 | } |
72914a60 | 609 | } |
a289addd | 610 | else |
0d162bd1 | 611 | #endif |
a289addd | 612 | linkname_len = 0; |
0d162bd1 | 613 | |
a289addd WD |
614 | sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0; |
615 | ||
61dec11a | 616 | alloc_len = file_struct_len + dirname_len + basename_len |
9935066b S |
617 | + linkname_len + sum_len; |
618 | bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry"); | |
619 | ||
f5db0993 | 620 | file = (struct file_struct *)bp; |
9935066b | 621 | memset(bp, 0, file_struct_len); |
61dec11a | 622 | bp += file_struct_len; |
a289addd | 623 | |
a289addd WD |
624 | file->modtime = modtime; |
625 | file->length = file_length; | |
626 | file->mode = mode; | |
7b6fa00f WD |
627 | file->uid = uid; |
628 | file->gid = gid; | |
a289addd | 629 | |
f3c3ed44 WD |
630 | if (dirname_len) { |
631 | file->dirname = lastdir = bp; | |
632 | lastdir_len = dirname_len - 1; | |
633 | memcpy(bp, dirname, dirname_len - 1); | |
634 | bp += dirname_len; | |
635 | bp[-1] = '\0'; | |
636 | lastdir_depth = count_dir_elements(lastdir); | |
637 | file->dir.depth = lastdir_depth + 1; | |
9c000f5e WD |
638 | if (lastdir_depth >= max_dir_depth) |
639 | max_dir_depth = lastdir_depth + 1; | |
f3c3ed44 WD |
640 | } else if (dirname) { |
641 | file->dirname = dirname; /* we're reusing lastname */ | |
642 | file->dir.depth = lastdir_depth + 1; | |
643 | } else | |
644 | file->dir.depth = 1; | |
645 | ||
649f8742 | 646 | if (S_ISDIR(mode)) { |
ee3751c8 | 647 | if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */ |
f3c3ed44 | 648 | file->dir.depth--; |
ee3751c8 | 649 | if (flags & XMIT_TOP_DIR) { |
75c51953 | 650 | in_del_hier = recurse; |
f3c3ed44 | 651 | del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2; |
7a16e122 | 652 | if (relative_paths && del_hier_name_len > 2 |
9cdadbb1 WD |
653 | && lastname[del_hier_name_len-1] == '.' |
654 | && lastname[del_hier_name_len-2] == '/') | |
7a16e122 | 655 | del_hier_name_len -= 2; |
ee3751c8 WD |
656 | file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE; |
657 | } else if (in_del_hier) { | |
f3c3ed44 WD |
658 | if (!relative_paths || !del_hier_name_len |
659 | || (l1 >= del_hier_name_len | |
9cdadbb1 | 660 | && lastname[del_hier_name_len] == '/')) |
ee3751c8 | 661 | file->flags |= FLAG_DEL_HERE; |
649f8742 WD |
662 | else |
663 | in_del_hier = 0; | |
664 | } | |
665 | } | |
666 | ||
a289addd WD |
667 | file->basename = bp; |
668 | memcpy(bp, basename, basename_len); | |
669 | bp += basename_len; | |
670 | ||
b5c6a6ae WD |
671 | if ((preserve_devices && IS_DEVICE(mode)) |
672 | || (preserve_specials && IS_SPECIAL(mode))) | |
a289addd WD |
673 | file->u.rdev = rdev; |
674 | ||
4f5b0756 | 675 | #ifdef SUPPORT_LINKS |
a289addd WD |
676 | if (linkname_len) { |
677 | file->u.link = bp; | |
678 | read_sbuf(f, bp, linkname_len - 1); | |
679 | if (sanitize_paths) | |
33ffd7c3 | 680 | sanitize_path(bp, bp, "", lastdir_depth); |
a289addd WD |
681 | bp += linkname_len; |
682 | } | |
683 | #endif | |
684 | ||
4f5b0756 | 685 | #ifdef SUPPORT_HARD_LINKS |
9935066b S |
686 | if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) |
687 | flags |= XMIT_HAS_IDEV_DATA; | |
97a67bdf | 688 | if (flags & XMIT_HAS_IDEV_DATA) { |
1490812a | 689 | int64 inode; |
4124540d WD |
690 | if (protocol_version < 26) { |
691 | dev = read_int(f); | |
9935066b | 692 | inode = read_int(f); |
736a6a29 | 693 | } else { |
4124540d WD |
694 | if (!(flags & XMIT_SAME_DEV)) |
695 | dev = read_longint(f); | |
9935066b S |
696 | inode = read_longint(f); |
697 | } | |
698 | if (flist->hlink_pool) { | |
5bf63a11 S |
699 | file->link_u.idev = pool_talloc(flist->hlink_pool, |
700 | struct idev, 1, "inode_table"); | |
9935066b S |
701 | file->F_INODE = inode; |
702 | file->F_DEV = dev; | |
736a6a29 | 703 | } |
72914a60 | 704 | } |
dc5ddbcc | 705 | #endif |
ebed4c3a | 706 | |
9a4a237e | 707 | if (always_checksum && (sum_len || protocol_version < 28)) { |
7c4f063b | 708 | char *sum; |
a289addd WD |
709 | if (sum_len) { |
710 | file->u.sum = sum = bp; | |
711 | /*bp += sum_len;*/ | |
9a4a237e | 712 | } else { |
fea4db62 | 713 | /* Prior to 28, we get a useless set of nulls. */ |
7c4f063b | 714 | sum = empty_sum; |
fea4db62 | 715 | } |
7752df41 | 716 | read_buf(f, sum, checksum_len); |
72914a60 | 717 | } |
ebed4c3a | 718 | |
72914a60 | 719 | if (!preserve_perms) { |
72914a60 | 720 | /* set an appropriate set of permissions based on original |
fea4db62 | 721 | * permissions and umask. This emulates what GNU cp does */ |
72914a60 AT |
722 | file->mode &= ~orig_umask; |
723 | } | |
f5db0993 WD |
724 | |
725 | return file; | |
c627d613 AT |
726 | } |
727 | ||
db719fb0 MP |
728 | /** |
729 | * Create a file_struct for a named file by reading its stat() | |
730 | * information and performing extensive checks against global | |
731 | * options. | |
732 | * | |
733 | * @return the new file, or NULL if there was an error or this file | |
734 | * should be excluded. | |
735 | * | |
736 | * @todo There is a small optimization opportunity here to avoid | |
737 | * stat()ing the file in some circumstances, which has a certain cost. | |
738 | * We are called immediately after doing readdir(), and so we may | |
739 | * already know the d_type of the file. We could for example avoid | |
740 | * statting directories if we're not recursing, but this is not a very | |
741 | * important case. Some systems may not have d_type. | |
742 | **/ | |
314f4591 | 743 | struct file_struct *make_file(char *fname, struct file_list *flist, |
96b87581 WD |
744 | STRUCT_STAT *stp, unsigned short flags, |
745 | int filter_level) | |
c627d613 | 746 | { |
a289addd WD |
747 | static char *lastdir; |
748 | static int lastdir_len = -1; | |
3ec4dd97 | 749 | struct file_struct *file; |
bcacc18b | 750 | STRUCT_STAT st; |
3ec4dd97 | 751 | char sum[SUM_LENGTH]; |
1923b1fc | 752 | char thisname[MAXPATHLEN]; |
e0870f1d | 753 | char linkname[MAXPATHLEN]; |
a1d55ad0 | 754 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
a289addd | 755 | char *basename, *dirname, *bp; |
3ec4dd97 | 756 | |
d508258a | 757 | if (!flist || !flist->count) /* Ignore lastdir when invalid. */ |
7de2483f | 758 | lastdir_len = -1; |
9935066b | 759 | |
1923b1fc WD |
760 | if (strlcpy(thisname, fname, sizeof thisname) |
761 | >= sizeof thisname - flist_dir_len) { | |
0ee6ca98 | 762 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
882e6893 WD |
763 | return NULL; |
764 | } | |
58b1999e | 765 | clean_fname(thisname, 0); |
b7736c79 | 766 | if (sanitize_paths) |
33ffd7c3 | 767 | sanitize_path(thisname, thisname, "", 0); |
3ec4dd97 | 768 | |
ebed4c3a | 769 | memset(sum, 0, SUM_LENGTH); |
3ec4dd97 | 770 | |
ede1f0eb WD |
771 | if (stp && S_ISDIR(stp->st_mode)) |
772 | st = *stp; /* Needed for "symlink/." with --relative. */ | |
773 | else if (readlink_stat(thisname, &st, linkname) != 0) { | |
76e26e10 | 774 | int save_errno = errno; |
a4a7e64c | 775 | /* See if file is excluded before reporting an error. */ |
7842418b WD |
776 | if (filter_level != NO_FILTERS |
777 | && is_excluded(thisname, 0, filter_level)) | |
a4a7e64c WD |
778 | return NULL; |
779 | if (save_errno == ENOENT) { | |
4f5b0756 | 780 | #ifdef SUPPORT_LINKS |
a4a7e64c WD |
781 | /* Avoid "vanished" error if symlink points nowhere. */ |
782 | if (copy_links && do_lstat(thisname, &st) == 0 | |
783 | && S_ISLNK(st.st_mode)) { | |
784 | io_error |= IOERR_GENERAL; | |
785 | rprintf(FERROR, "symlink has no referent: %s\n", | |
786 | full_fname(thisname)); | |
e5ce3bcf WD |
787 | } else |
788 | #endif | |
789 | { | |
a4a7e64c WD |
790 | enum logcode c = am_daemon && protocol_version < 28 |
791 | ? FERROR : FINFO; | |
792 | io_error |= IOERR_VANISHED; | |
793 | rprintf(c, "file has vanished: %s\n", | |
794 | full_fname(thisname)); | |
76e26e10 | 795 | } |
a4a7e64c | 796 | } else { |
a8726d2a | 797 | io_error |= IOERR_GENERAL; |
d62bcc17 WD |
798 | rsyserr(FERROR, save_errno, "readlink %s failed", |
799 | full_fname(thisname)); | |
76e26e10 | 800 | } |
3ec4dd97 AT |
801 | return NULL; |
802 | } | |
c627d613 | 803 | |
7842418b WD |
804 | /* backup.c calls us with filter_level set to NO_FILTERS. */ |
805 | if (filter_level == NO_FILTERS) | |
806 | goto skip_filters; | |
ac1a0994 | 807 | |
7e037c42 | 808 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
0ee6ca98 | 809 | rprintf(FINFO, "skipping directory %s\n", thisname); |
3ec4dd97 AT |
810 | return NULL; |
811 | } | |
ebed4c3a | 812 | |
e90cdb8a WD |
813 | /* We only care about directories because we need to avoid recursing |
814 | * into a mount-point directory, not to avoid copying a symlinked | |
815 | * file if -L (or similar) was specified. */ | |
535737bf WD |
816 | if (one_file_system && st.st_dev != filesystem_dev |
817 | && S_ISDIR(st.st_mode)) { | |
818 | if (one_file_system > 1) { | |
819 | if (verbose > 2) { | |
820 | rprintf(FINFO, "skipping mount-point dir %s\n", | |
821 | thisname); | |
ebec5eb6 | 822 | } |
535737bf | 823 | return NULL; |
ebec5eb6 | 824 | } |
535737bf | 825 | flags |= FLAG_MOUNT_POINT; |
3b173846 | 826 | } |
ebed4c3a | 827 | |
7842418b | 828 | if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) |
76e26e10 DD |
829 | return NULL; |
830 | ||
132fcf36 | 831 | if (lp_ignore_nonreadable(module_id)) { |
4f5b0756 | 832 | #ifdef SUPPORT_LINKS |
132fcf36 WD |
833 | if (!S_ISLNK(st.st_mode)) |
834 | #endif | |
835 | if (access(thisname, R_OK) != 0) | |
836 | return NULL; | |
837 | } | |
ac1a0994 | 838 | |
97b7bff4 | 839 | skip_filters: |
ac1a0994 | 840 | |
ea847c62 WD |
841 | if (verbose > 2) { |
842 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", | |
0ee6ca98 | 843 | who_am_i(), thisname, filter_level); |
ea847c62 | 844 | } |
ebed4c3a | 845 | |
a289addd WD |
846 | if ((basename = strrchr(thisname, '/')) != NULL) { |
847 | dirname_len = ++basename - thisname; /* counts future '\0' */ | |
848 | if (lastdir_len == dirname_len - 1 | |
849 | && strncmp(thisname, lastdir, lastdir_len) == 0) { | |
850 | dirname = lastdir; | |
851 | dirname_len = 0; /* indicates no copy is needed */ | |
852 | } else | |
853 | dirname = thisname; | |
3ec4dd97 | 854 | } else { |
a289addd WD |
855 | basename = thisname; |
856 | dirname = NULL; | |
857 | dirname_len = 0; | |
3ec4dd97 | 858 | } |
a289addd | 859 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
c627d613 | 860 | |
4f5b0756 | 861 | #ifdef SUPPORT_LINKS |
a289addd WD |
862 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; |
863 | #else | |
864 | linkname_len = 0; | |
865 | #endif | |
866 | ||
902f03d1 WD |
867 | sum_len = always_checksum && am_sender && S_ISREG(st.st_mode) |
868 | ? MD4_SUM_LENGTH : 0; | |
a289addd | 869 | |
61dec11a | 870 | alloc_len = file_struct_len + dirname_len + basename_len |
9935066b | 871 | + linkname_len + sum_len; |
99aaa6ca WD |
872 | if (flist) |
873 | bp = pool_alloc(flist->file_pool, alloc_len, "make_file"); | |
874 | else { | |
9935066b | 875 | if (!(bp = new_array(char, alloc_len))) |
99aaa6ca | 876 | out_of_memory("make_file"); |
9935066b S |
877 | } |
878 | ||
a289addd | 879 | file = (struct file_struct *)bp; |
9935066b | 880 | memset(bp, 0, file_struct_len); |
61dec11a | 881 | bp += file_struct_len; |
a289addd WD |
882 | |
883 | file->flags = flags; | |
3ec4dd97 AT |
884 | file->modtime = st.st_mtime; |
885 | file->length = st.st_size; | |
8bbe41b5 | 886 | file->mode = st.st_mode; |
7b6fa00f WD |
887 | file->uid = st.st_uid; |
888 | file->gid = st.st_gid; | |
0d162bd1 | 889 | |
4f5b0756 | 890 | #ifdef SUPPORT_HARD_LINKS |
9935066b S |
891 | if (flist && flist->hlink_pool) { |
892 | if (protocol_version < 28) { | |
893 | if (S_ISREG(st.st_mode)) | |
894 | file->link_u.idev = pool_talloc( | |
895 | flist->hlink_pool, struct idev, 1, | |
896 | "inode_table"); | |
897 | } else { | |
898 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) | |
899 | file->link_u.idev = pool_talloc( | |
900 | flist->hlink_pool, struct idev, 1, | |
901 | "inode_table"); | |
902 | } | |
903 | } | |
904 | if (file->link_u.idev) { | |
61dec11a WD |
905 | file->F_DEV = st.st_dev; |
906 | file->F_INODE = st.st_ino; | |
907 | } | |
908 | #endif | |
909 | ||
a289addd WD |
910 | if (dirname_len) { |
911 | file->dirname = lastdir = bp; | |
912 | lastdir_len = dirname_len - 1; | |
913 | memcpy(bp, dirname, dirname_len - 1); | |
914 | bp += dirname_len; | |
915 | bp[-1] = '\0'; | |
916 | } else if (dirname) | |
917 | file->dirname = dirname; | |
918 | ||
919 | file->basename = bp; | |
920 | memcpy(bp, basename, basename_len); | |
921 | bp += basename_len; | |
922 | ||
4f5b0756 | 923 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
b5c6a6ae WD |
924 | if ((preserve_devices && IS_DEVICE(st.st_mode)) |
925 | || (preserve_specials && IS_SPECIAL(st.st_mode))) | |
0d162bd1 WD |
926 | file->u.rdev = st.st_rdev; |
927 | #endif | |
928 | ||
4f5b0756 | 929 | #ifdef SUPPORT_LINKS |
a289addd WD |
930 | if (linkname_len) { |
931 | file->u.link = bp; | |
932 | memcpy(bp, linkname, linkname_len); | |
933 | bp += linkname_len; | |
934 | } | |
0d162bd1 WD |
935 | #endif |
936 | ||
a289addd WD |
937 | if (sum_len) { |
938 | file->u.sum = bp; | |
939 | file_checksum(thisname, bp, st.st_size); | |
940 | /*bp += sum_len;*/ | |
ebed4c3a | 941 | } |
c627d613 | 942 | |
f3c3ed44 | 943 | file->dir.root = flist_dir; |
c627d613 | 944 | |
23f4587f WD |
945 | /* This code is only used by the receiver when it is building |
946 | * a list of files for a delete pass. */ | |
947 | if (keep_dirlinks && linkname_len && flist) { | |
948 | STRUCT_STAT st2; | |
f5db0993 | 949 | int save_mode = file->mode; |
8bbe41b5 | 950 | file->mode = S_IFDIR; /* Find a directory with our name. */ |
d64e6f42 | 951 | if (flist_find(the_file_list, file) >= 0 |
23f4587f WD |
952 | && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) { |
953 | file->modtime = st2.st_mtime; | |
954 | file->length = st2.st_size; | |
955 | file->mode = st2.st_mode; | |
7b6fa00f WD |
956 | file->uid = st2.st_uid; |
957 | file->gid = st2.st_gid; | |
23f4587f | 958 | file->u.link = NULL; |
f5db0993 WD |
959 | } else |
960 | file->mode = save_mode; | |
23f4587f WD |
961 | } |
962 | ||
d17190df | 963 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
a800434a | 964 | stats.total_size += st.st_size; |
c627d613 | 965 | |
3ec4dd97 | 966 | return file; |
c627d613 AT |
967 | } |
968 | ||
301fb56c | 969 | static struct file_struct *send_file_name(int f, struct file_list *flist, |
ede1f0eb | 970 | char *fname, STRUCT_STAT *stp, |
96b87581 | 971 | unsigned short flags) |
c627d613 | 972 | { |
ebed4c3a MP |
973 | struct file_struct *file; |
974 | ||
96b87581 | 975 | file = make_file(fname, flist, stp, flags, |
ede1f0eb | 976 | f == -2 ? SERVER_FILTERS : ALL_FILTERS); |
37802f40 | 977 | if (!file) |
301fb56c | 978 | return NULL; |
ebed4c3a | 979 | |
ebec5eb6 | 980 | if (chmod_modes && !S_ISLNK(file->mode)) |
8bbe41b5 WD |
981 | file->mode = tweak_mode(file->mode, chmod_modes); |
982 | ||
53135fe8 | 983 | maybe_emit_filelist_progress(flist->count + flist_count_offset); |
ebed4c3a | 984 | |
d9d6bc52 | 985 | flist_expand(flist); |
ebed4c3a | 986 | |
c120ff37 | 987 | if (file->basename[0]) { |
ebed4c3a | 988 | flist->files[flist->count++] = file; |
96b87581 | 989 | send_file_entry(file, f); |
ebed4c3a | 990 | } |
301fb56c WD |
991 | return file; |
992 | } | |
ebed4c3a | 993 | |
301fb56c | 994 | static void send_if_directory(int f, struct file_list *flist, |
56f0c976 WD |
995 | struct file_struct *file, |
996 | char *fbuf, unsigned int ol) | |
301fb56c | 997 | { |
56f0c976 | 998 | char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/'); |
301fb56c WD |
999 | |
1000 | if (S_ISDIR(file->mode) | |
5e4ff5f9 | 1001 | && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) { |
f1773e09 WD |
1002 | void *save_filters; |
1003 | unsigned int len = strlen(fbuf); | |
1004 | if (len > 1 && fbuf[len-1] == '/') | |
1005 | fbuf[--len] = '\0'; | |
1006 | if (len >= MAXPATHLEN - 1) { | |
1007 | io_error |= IOERR_GENERAL; | |
1008 | rprintf(FERROR, "skipping long-named directory: %s\n", | |
1009 | full_fname(fbuf)); | |
1010 | return; | |
1011 | } | |
1012 | save_filters = push_local_filters(fbuf, len); | |
1013 | send_directory(f, flist, fbuf, len); | |
1014 | pop_local_filters(save_filters); | |
56f0c976 WD |
1015 | fbuf[ol] = '\0'; |
1016 | if (is_dot_dir) | |
1017 | fbuf[ol-1] = '.'; | |
ebed4c3a | 1018 | } |
c627d613 AT |
1019 | } |
1020 | ||
301fb56c | 1021 | /* This function is normally called by the sender, but the receiving side also |
7b558d7f | 1022 | * calls it from get_dirlist() with f set to -1 so that we just construct the |
301fb56c WD |
1023 | * file list in memory without sending it over the wire. Also, get_dirlist() |
1024 | * might call this with f set to -2, which also indicates that local filter | |
1025 | * rules should be ignored. */ | |
f1773e09 | 1026 | static void send_directory(int f, struct file_list *flist, |
32cbfe7b | 1027 | char *fbuf, int len) |
c627d613 | 1028 | { |
3ec4dd97 | 1029 | struct dirent *di; |
32cbfe7b | 1030 | unsigned remainder; |
3ec4dd97 | 1031 | char *p; |
f1773e09 | 1032 | DIR *d; |
301fb56c | 1033 | int start = flist->count; |
3ec4dd97 | 1034 | |
f1773e09 | 1035 | if (!(d = opendir(fbuf))) { |
06c28400 | 1036 | io_error |= IOERR_GENERAL; |
f1773e09 | 1037 | rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf)); |
3ec4dd97 AT |
1038 | return; |
1039 | } | |
c627d613 | 1040 | |
19b2a5d9 WD |
1041 | p = fbuf + len; |
1042 | if (len != 1 || *fbuf != '/') | |
eddd5d12 | 1043 | *p++ = '/'; |
f1773e09 | 1044 | *p = '\0'; |
32cbfe7b | 1045 | remainder = MAXPATHLEN - (p - fbuf); |
ebed4c3a | 1046 | |
6a7cc46c | 1047 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 1048 | char *dname = d_name(di); |
6a7cc46c S |
1049 | if (dname[0] == '.' && (dname[1] == '\0' |
1050 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 1051 | continue; |
0ee6ca98 | 1052 | if (strlcpy(p, dname, remainder) >= remainder) { |
eddd5d12 WD |
1053 | io_error |= IOERR_GENERAL; |
1054 | rprintf(FINFO, | |
1055 | "cannot send long-named file %s\n", | |
f1773e09 | 1056 | full_fname(fbuf)); |
beaf4954 | 1057 | continue; |
eddd5d12 | 1058 | } |
0ee6ca98 | 1059 | |
ede1f0eb | 1060 | send_file_name(f, flist, fbuf, NULL, 0); |
3ec4dd97 | 1061 | } |
32cbfe7b WD |
1062 | |
1063 | fbuf[len] = '\0'; | |
1064 | ||
6a7cc46c | 1065 | if (errno) { |
06c28400 | 1066 | io_error |= IOERR_GENERAL; |
71903f60 | 1067 | rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf)); |
6a7cc46c | 1068 | } |
c627d613 | 1069 | |
3ec4dd97 | 1070 | closedir(d); |
301fb56c WD |
1071 | |
1072 | if (recurse) { | |
1073 | int i, end = flist->count - 1; | |
1074 | for (i = start; i <= end; i++) | |
56f0c976 | 1075 | send_if_directory(f, flist, flist->files[i], fbuf, len); |
301fb56c | 1076 | } |
c627d613 AT |
1077 | } |
1078 | ||
ebed4c3a | 1079 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 1080 | { |
56f0c976 | 1081 | int len; |
bcacc18b | 1082 | STRUCT_STAT st; |
808c57c3 | 1083 | char *p, *dir, olddir[sizeof curr_dir]; |
ebed4c3a | 1084 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 1085 | struct file_list *flist; |
31b4d25d | 1086 | struct timeval start_tv, end_tv; |
a800434a | 1087 | int64 start_write; |
24d0fcde | 1088 | int use_ff_fd = 0; |
649d65ed | 1089 | |
134f4338 | 1090 | if (show_filelist_p()) |
1bbd10fe | 1091 | start_filelist_progress("building file list"); |
c627d613 | 1092 | |
a800434a | 1093 | start_write = stats.total_written; |
31b4d25d | 1094 | gettimeofday(&start_tv, NULL); |
a800434a | 1095 | |
134f4338 | 1096 | flist = flist_new(WITH_HLINK, "send_file_list"); |
c627d613 | 1097 | |
134f4338 WD |
1098 | io_start_buffering_out(); |
1099 | if (filesfrom_fd >= 0) { | |
1100 | if (argv[0] && !push_dir(argv[0])) { | |
1101 | rsyserr(FERROR, errno, "push_dir %s failed", | |
1102 | full_fname(argv[0])); | |
1103 | exit_cleanup(RERR_FILESELECT); | |
24d0fcde | 1104 | } |
134f4338 | 1105 | use_ff_fd = 1; |
d6dead6b AT |
1106 | } |
1107 | ||
24d0fcde | 1108 | while (1) { |
56f0c976 WD |
1109 | char fbuf[MAXPATHLEN]; |
1110 | char *fn; | |
301fb56c | 1111 | int is_dot_dir; |
c627d613 | 1112 | |
24d0fcde | 1113 | if (use_ff_fd) { |
56f0c976 | 1114 | if (read_filesfrom_line(filesfrom_fd, fbuf) == 0) |
24d0fcde | 1115 | break; |
56f0c976 | 1116 | sanitize_path(fbuf, fbuf, "", 0); |
24d0fcde WD |
1117 | } else { |
1118 | if (argc-- == 0) | |
1119 | break; | |
56f0c976 | 1120 | strlcpy(fbuf, *argv++, MAXPATHLEN); |
24d0fcde | 1121 | if (sanitize_paths) |
56f0c976 | 1122 | sanitize_path(fbuf, fbuf, "", 0); |
24d0fcde | 1123 | } |
c627d613 | 1124 | |
56f0c976 | 1125 | len = strlen(fbuf); |
1902a765 WD |
1126 | if (relative_paths) { |
1127 | /* We clean up fbuf below. */ | |
1128 | is_dot_dir = 0; | |
1129 | } else if (!len || fbuf[len - 1] == '/') { | |
56f0c976 | 1130 | if (len == 2 && fbuf[0] == '.') { |
6931c138 | 1131 | /* Turn "./" into just "." rather than "./." */ |
56f0c976 | 1132 | fbuf[1] = '\0'; |
557a35f5 | 1133 | } else { |
56f0c976 | 1134 | if (len + 1 >= MAXPATHLEN) |
a1f99493 | 1135 | overflow_exit("send_file_list"); |
56f0c976 WD |
1136 | fbuf[len++] = '.'; |
1137 | fbuf[len] = '\0'; | |
53f821f1 | 1138 | } |
301fb56c | 1139 | is_dot_dir = 1; |
56f0c976 WD |
1140 | } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.' |
1141 | && (len == 2 || fbuf[len-3] == '/')) { | |
1142 | if (len + 2 >= MAXPATHLEN) | |
a1f99493 | 1143 | overflow_exit("send_file_list"); |
56f0c976 WD |
1144 | fbuf[len++] = '/'; |
1145 | fbuf[len++] = '.'; | |
1146 | fbuf[len] = '\0'; | |
a289f89f | 1147 | is_dot_dir = 1; |
301fb56c | 1148 | } else { |
56f0c976 WD |
1149 | is_dot_dir = fbuf[len-1] == '.' |
1150 | && (len == 1 || fbuf[len-2] == '/'); | |
649d65ed | 1151 | } |
c627d613 | 1152 | |
56f0c976 | 1153 | if (link_stat(fbuf, &st, keep_dirlinks) != 0) { |
134f4338 WD |
1154 | io_error |= IOERR_GENERAL; |
1155 | rsyserr(FERROR, errno, "link_stat %s failed", | |
56f0c976 | 1156 | full_fname(fbuf)); |
649d65ed AT |
1157 | continue; |
1158 | } | |
c627d613 | 1159 | |
7e037c42 | 1160 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
0ee6ca98 | 1161 | rprintf(FINFO, "skipping directory %s\n", fbuf); |
649d65ed AT |
1162 | continue; |
1163 | } | |
c627d613 | 1164 | |
649d65ed | 1165 | dir = NULL; |
808c57c3 | 1166 | olddir[0] = '\0'; |
649d65ed AT |
1167 | |
1168 | if (!relative_paths) { | |
56f0c976 | 1169 | p = strrchr(fbuf, '/'); |
649d65ed | 1170 | if (p) { |
151f59f1 | 1171 | *p = '\0'; |
56f0c976 | 1172 | if (p == fbuf) |
649d65ed AT |
1173 | dir = "/"; |
1174 | else | |
56f0c976 WD |
1175 | dir = fbuf; |
1176 | len -= p - fbuf + 1; | |
1177 | fn = p + 1; | |
1178 | } else | |
1179 | fn = fbuf; | |
1902a765 WD |
1180 | } else { |
1181 | if ((p = strstr(fbuf, "/./")) != NULL) { | |
1182 | *p = '\0'; | |
1183 | if (p == fbuf) | |
1184 | dir = "/"; | |
1185 | else | |
1186 | dir = fbuf; | |
1187 | len -= p - fbuf + 3; | |
1188 | fn = p + 3; | |
1189 | } else | |
1190 | fn = fbuf; | |
1191 | /* Get rid of trailing "/" and "/.". */ | |
1192 | while (len) { | |
ede1f0eb WD |
1193 | if (fn[len - 1] == '/') { |
1194 | is_dot_dir = 1; | |
1195 | if (!--len && !dir) { | |
1196 | len++; | |
1197 | break; | |
1198 | } | |
1199 | } | |
1902a765 WD |
1200 | else if (len >= 2 && fn[len - 1] == '.' |
1201 | && fn[len - 2] == '/') { | |
ede1f0eb | 1202 | is_dot_dir = 1; |
1902a765 WD |
1203 | if (!(len -= 2) && !dir) { |
1204 | len++; | |
1205 | break; | |
1206 | } | |
1207 | } else | |
1208 | break; | |
1209 | } | |
1210 | fn[len] = '\0'; | |
1211 | /* Reject a ".." dir in the active part of the path. */ | |
ede1f0eb WD |
1212 | for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) { |
1213 | if ((p[2] == '/' || p[2] == '\0') | |
1214 | && (p == fn || p[-1] == '/')) { | |
1215 | rprintf(FERROR, | |
1216 | "found \"..\" dir in relative path: %s\n", | |
1217 | fbuf); | |
1218 | exit_cleanup(RERR_SYNTAX); | |
1219 | } | |
1902a765 WD |
1220 | } |
1221 | } | |
d2ea5980 | 1222 | |
56f0c976 WD |
1223 | if (!*fn) { |
1224 | len = 1; | |
1225 | fn = "."; | |
1226 | } | |
d2ea5980 WD |
1227 | |
1228 | if (dir && *dir) { | |
1229 | static char *lastdir; | |
1230 | static int lastdir_len; | |
1231 | ||
8ad5cea3 | 1232 | strlcpy(olddir, curr_dir, sizeof olddir); |
d2ea5980 WD |
1233 | |
1234 | if (!push_dir(dir)) { | |
1235 | io_error |= IOERR_GENERAL; | |
1236 | rsyserr(FERROR, errno, "push_dir %s failed", | |
1237 | full_fname(dir)); | |
1238 | continue; | |
1239 | } | |
1240 | ||
1241 | if (lastdir && strcmp(lastdir, dir) == 0) { | |
1242 | flist_dir = lastdir; | |
1243 | flist_dir_len = lastdir_len; | |
1244 | } else { | |
1245 | flist_dir = lastdir = strdup(dir); | |
1246 | flist_dir_len = lastdir_len = strlen(dir); | |
1247 | } | |
1248 | } | |
1249 | ||
56f0c976 WD |
1250 | if (fn != fbuf) |
1251 | memmove(fbuf, fn, len + 1); | |
1252 | ||
1253 | if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) { | |
151f59f1 WD |
1254 | /* Send the implied directories at the start of the |
1255 | * source spec, so we get their permissions right. */ | |
56f0c976 | 1256 | char *lp = lastpath, *slash = fbuf; |
151f59f1 | 1257 | *p = '\0'; |
2154309a WD |
1258 | /* Skip any initial directories in our path that we |
1259 | * have in common with lastpath. */ | |
56f0c976 | 1260 | for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) { |
2154309a WD |
1261 | if (*fn == '/') |
1262 | slash = fn; | |
2154309a WD |
1263 | } |
1264 | *p = '/'; | |
1265 | if (fn != p || (*lp && *lp != '/')) { | |
0f57446d | 1266 | int save_copy_links = copy_links; |
7e037c42 | 1267 | int save_xfer_dirs = xfer_dirs; |
2154309a | 1268 | copy_links = copy_unsafe_links; |
7e037c42 | 1269 | xfer_dirs = 1; |
2154309a | 1270 | while ((slash = strchr(slash+1, '/')) != 0) { |
151f59f1 | 1271 | *slash = '\0'; |
ede1f0eb | 1272 | send_file_name(f, flist, fbuf, NULL, 0); |
2154309a | 1273 | *slash = '/'; |
649d65ed | 1274 | } |
0f57446d | 1275 | copy_links = save_copy_links; |
7e037c42 | 1276 | xfer_dirs = save_xfer_dirs; |
151f59f1 | 1277 | *p = '\0'; |
56f0c976 | 1278 | strlcpy(lastpath, fbuf, sizeof lastpath); |
649d65ed AT |
1279 | *p = '/'; |
1280 | } | |
1281 | } | |
ebed4c3a | 1282 | |
535737bf WD |
1283 | if (one_file_system) |
1284 | filesystem_dev = st.st_dev; | |
1285 | ||
75c51953 WD |
1286 | if (recurse || (xfer_dirs && is_dot_dir)) { |
1287 | struct file_struct *file; | |
96b87581 | 1288 | file = send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR); |
ede1f0eb | 1289 | if (file) |
56f0c976 | 1290 | send_if_directory(f, flist, file, fbuf, len); |
75c51953 | 1291 | } else |
ede1f0eb | 1292 | send_file_name(f, flist, fbuf, &st, 0); |
2bca43f6 | 1293 | |
808c57c3 | 1294 | if (olddir[0]) { |
649d65ed | 1295 | flist_dir = NULL; |
882e6893 | 1296 | flist_dir_len = 0; |
808c57c3 | 1297 | if (!pop_dir(olddir)) { |
d62bcc17 | 1298 | rsyserr(FERROR, errno, "pop_dir %s failed", |
92cdc393 | 1299 | full_fname(olddir)); |
65417579 | 1300 | exit_cleanup(RERR_FILESELECT); |
649d65ed | 1301 | } |
649d65ed | 1302 | } |
649d65ed | 1303 | } |
dc5ddbcc | 1304 | |
134f4338 WD |
1305 | gettimeofday(&end_tv, NULL); |
1306 | stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1307 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
1308 | if (stats.flist_buildtime == 0) | |
1309 | stats.flist_buildtime = 1; | |
1310 | start_tv = end_tv; | |
31b4d25d | 1311 | |
96b87581 | 1312 | send_file_entry(NULL, f); |
c627d613 | 1313 | |
134f4338 WD |
1314 | if (show_filelist_p()) |
1315 | finish_filelist_progress(flist); | |
31b4d25d | 1316 | |
134f4338 WD |
1317 | gettimeofday(&end_tv, NULL); |
1318 | stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1319 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
ebed4c3a | 1320 | |
7cf8e8d0 | 1321 | if (flist->hlink_pool) { |
9935066b S |
1322 | pool_destroy(flist->hlink_pool); |
1323 | flist->hlink_pool = NULL; | |
1324 | } | |
1325 | ||
f5db0993 WD |
1326 | /* Sort the list without removing any duplicates. This allows the |
1327 | * receiving side to ask for any name they like, which gives us the | |
1328 | * flexibility to change the way we unduplicate names in the future | |
1329 | * without causing a compatibility problem with older versions. */ | |
827c37f6 | 1330 | clean_flist(flist, 0, 0); |
ebed4c3a | 1331 | |
134f4338 | 1332 | send_uid_list(f); |
f6c34742 | 1333 | |
134f4338 WD |
1334 | /* send the io_error flag */ |
1335 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); | |
6ba9279f | 1336 | |
134f4338 WD |
1337 | io_end_buffering(); |
1338 | stats.flist_size = stats.total_written - start_write; | |
1339 | stats.num_files = flist->count; | |
d6dead6b | 1340 | |
cefed3e8 | 1341 | if (verbose > 3) |
32cbfe7b | 1342 | output_flist(flist); |
cefed3e8 | 1343 | |
17faa41c | 1344 | if (verbose > 2) |
ebed4c3a | 1345 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1346 | |
649d65ed | 1347 | return flist; |
c627d613 AT |
1348 | } |
1349 | ||
c627d613 AT |
1350 | struct file_list *recv_file_list(int f) |
1351 | { | |
ebed4c3a | 1352 | struct file_list *flist; |
1ef00d20 | 1353 | unsigned short flags; |
ebed4c3a | 1354 | int64 start_read; |
c627d613 | 1355 | |
1bbd10fe DD |
1356 | if (show_filelist_p()) |
1357 | start_filelist_progress("receiving file list"); | |
c627d613 | 1358 | |
ebed4c3a | 1359 | start_read = stats.total_read; |
a800434a | 1360 | |
9935066b | 1361 | flist = flist_new(WITH_HLINK, "recv_file_list"); |
c627d613 | 1362 | |
ebed4c3a MP |
1363 | flist->count = 0; |
1364 | flist->malloced = 1000; | |
58cadc86 | 1365 | flist->files = new_array(struct file_struct *, flist->malloced); |
ebed4c3a MP |
1366 | if (!flist->files) |
1367 | goto oom; | |
c627d613 | 1368 | |
1ef00d20 | 1369 | while ((flags = read_byte(f)) != 0) { |
f5db0993 | 1370 | struct file_struct *file; |
dbda5fbf | 1371 | |
d9d6bc52 | 1372 | flist_expand(flist); |
c627d613 | 1373 | |
d01d15e0 | 1374 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1375 | flags |= read_byte(f) << 8; |
f5db0993 | 1376 | file = receive_file_entry(flist, flags, f); |
c627d613 | 1377 | |
f5db0993 WD |
1378 | if (S_ISREG(file->mode)) |
1379 | stats.total_size += file->length; | |
c627d613 | 1380 | |
f5db0993 | 1381 | flist->files[flist->count++] = file; |
c627d613 | 1382 | |
53135fe8 | 1383 | maybe_emit_filelist_progress(flist->count); |
1bbd10fe | 1384 | |
5e4ff5f9 WD |
1385 | if (verbose > 2) { |
1386 | rprintf(FINFO, "recv_file_name(%s)\n", | |
1387 | f_name(file, NULL)); | |
1388 | } | |
ebed4c3a | 1389 | } |
f5db0993 | 1390 | receive_file_entry(NULL, 0, 0); /* Signal that we're done. */ |
c627d613 | 1391 | |
ebed4c3a MP |
1392 | if (verbose > 2) |
1393 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1394 | |
b7736c79 | 1395 | if (show_filelist_p()) |
1bbd10fe | 1396 | finish_filelist_progress(flist); |
a06d19e3 | 1397 | |
983b1ed3 WD |
1398 | clean_flist(flist, relative_paths, 1); |
1399 | ||
37802f40 | 1400 | if (f >= 0) { |
7b6fa00f | 1401 | recv_uid_list(f, flist); |
f6c34742 | 1402 | |
b9f592fb WD |
1403 | /* Recv the io_error flag */ |
1404 | if (lp_ignore_errors(module_id) || ignore_errors) | |
1405 | read_int(f); | |
1406 | else | |
1407 | io_error |= read_int(f); | |
ebed4c3a | 1408 | } |
6ba9279f | 1409 | |
cefed3e8 | 1410 | if (verbose > 3) |
32cbfe7b | 1411 | output_flist(flist); |
cefed3e8 | 1412 | |
ebed4c3a MP |
1413 | if (list_only) { |
1414 | int i; | |
b7736c79 | 1415 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1416 | list_file_entry(flist->files[i]); |
ebed4c3a | 1417 | } |
f7632fc6 | 1418 | |
ebed4c3a MP |
1419 | if (verbose > 2) |
1420 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 1421 | |
ebed4c3a MP |
1422 | stats.flist_size = stats.total_read - start_read; |
1423 | stats.num_files = flist->count; | |
a800434a | 1424 | |
ebed4c3a | 1425 | return flist; |
c627d613 | 1426 | |
97b7bff4 | 1427 | oom: |
ebed4c3a MP |
1428 | out_of_memory("recv_file_list"); |
1429 | return NULL; /* not reached */ | |
c627d613 AT |
1430 | } |
1431 | ||
14698a3a | 1432 | static int file_compare(struct file_struct **file1, struct file_struct **file2) |
c627d613 | 1433 | { |
14698a3a | 1434 | return f_name_cmp(*file1, *file2); |
c627d613 AT |
1435 | } |
1436 | ||
f5db0993 WD |
1437 | /* Search for an identically-named item in the file list. Note that the |
1438 | * items must agree in their directory-ness, or no match is returned. */ | |
2f3cad89 | 1439 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 1440 | { |
f3c3ed44 WD |
1441 | int low = flist->low, high = flist->high; |
1442 | int ret, mid, mid_up; | |
1443 | ||
1444 | while (low <= high) { | |
1445 | mid = (low + high) / 2; | |
c0b134a4 WD |
1446 | if (flist->files[mid]->basename) |
1447 | mid_up = mid; | |
7402d583 WD |
1448 | else { |
1449 | /* Scan for the next non-empty entry using the cached | |
1450 | * distance values. If the value isn't fully up-to- | |
1451 | * date, update it. */ | |
85aecef6 WD |
1452 | mid_up = mid + flist->files[mid]->dir.depth; |
1453 | if (!flist->files[mid_up]->basename) { | |
1454 | do { | |
1455 | mid_up += flist->files[mid_up]->dir.depth; | |
1456 | } while (!flist->files[mid_up]->basename); | |
1457 | flist->files[mid]->dir.depth = mid_up - mid; | |
c0b134a4 | 1458 | } |
c0b134a4 | 1459 | if (mid_up > high) { |
7402d583 WD |
1460 | /* If there's nothing left above us, set high to |
1461 | * a non-empty entry below us and continue. */ | |
85aecef6 WD |
1462 | high = mid - flist->files[mid]->length; |
1463 | if (!flist->files[high]->basename) { | |
1464 | do { | |
1465 | high -= flist->files[high]->length; | |
1466 | } while (!flist->files[high]->basename); | |
1467 | flist->files[mid]->length = mid - high; | |
7402d583 | 1468 | } |
c0b134a4 WD |
1469 | continue; |
1470 | } | |
c0b134a4 WD |
1471 | } |
1472 | ret = f_name_cmp(flist->files[mid_up], f); | |
f5db0993 WD |
1473 | if (ret == 0) { |
1474 | if (protocol_version < 29 | |
1475 | && S_ISDIR(flist->files[mid_up]->mode) | |
1476 | != S_ISDIR(f->mode)) | |
1477 | return -1; | |
f3c3ed44 | 1478 | return mid_up; |
f5db0993 | 1479 | } |
b7736c79 | 1480 | if (ret > 0) |
f3c3ed44 | 1481 | high = mid - 1; |
b7736c79 | 1482 | else |
f3c3ed44 | 1483 | low = mid_up + 1; |
d966ee25 | 1484 | } |
d966ee25 | 1485 | return -1; |
c627d613 AT |
1486 | } |
1487 | ||
3ec4dd97 | 1488 | /* |
9935066b S |
1489 | * Free up any resources a file_struct has allocated |
1490 | * and clear the file. | |
3ec4dd97 | 1491 | */ |
9935066b | 1492 | void clear_file(int i, struct file_list *flist) |
c627d613 | 1493 | { |
7402d583 WD |
1494 | struct file_struct *file = flist->files[i]; |
1495 | if (flist->hlink_pool && file->link_u.idev) | |
1496 | pool_free(flist->hlink_pool, 0, file->link_u.idev); | |
1497 | memset(file, 0, file_struct_len); | |
1498 | /* In an empty entry, dir.depth is an offset to the next non-empty | |
1499 | * entry. Likewise for length in the opposite direction. We assume | |
1500 | * that we're alone for now since flist_find() will collate adjacent | |
1501 | * items for any entries that are encountered during the find. */ | |
1502 | file->length = file->dir.depth = 1; | |
3ec4dd97 | 1503 | } |
c627d613 | 1504 | |
3d382777 AT |
1505 | /* |
1506 | * allocate a new file list | |
1507 | */ | |
9935066b | 1508 | struct file_list *flist_new(int with_hlink, char *msg) |
3d382777 AT |
1509 | { |
1510 | struct file_list *flist; | |
1511 | ||
58cadc86 | 1512 | flist = new(struct file_list); |
ebed4c3a | 1513 | if (!flist) |
9935066b | 1514 | out_of_memory(msg); |
3d382777 | 1515 | |
9935066b S |
1516 | memset(flist, 0, sizeof (struct file_list)); |
1517 | ||
1518 | if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, | |
1519 | out_of_memory, POOL_INTERN))) | |
1520 | out_of_memory(msg); | |
1521 | ||
4f5b0756 | 1522 | #ifdef SUPPORT_HARD_LINKS |
9935066b | 1523 | if (with_hlink && preserve_hard_links) { |
3e491682 | 1524 | if (!(flist->hlink_pool = pool_create(HLINK_EXTENT, |
9935066b S |
1525 | sizeof (struct idev), out_of_memory, POOL_INTERN))) |
1526 | out_of_memory(msg); | |
1527 | } | |
1528 | #endif | |
d9d6bc52 | 1529 | |
3d382777 AT |
1530 | return flist; |
1531 | } | |
ebed4c3a | 1532 | |
3ec4dd97 AT |
1533 | /* |
1534 | * free up all elements in a flist | |
1535 | */ | |
1536 | void flist_free(struct file_list *flist) | |
1537 | { | |
9935066b S |
1538 | pool_destroy(flist->file_pool); |
1539 | pool_destroy(flist->hlink_pool); | |
3ec4dd97 | 1540 | free(flist->files); |
3ec4dd97 | 1541 | free(flist); |
c627d613 AT |
1542 | } |
1543 | ||
c627d613 AT |
1544 | /* |
1545 | * This routine ensures we don't have any duplicate names in our file list. | |
dbda5fbf | 1546 | * duplicate names can cause corruption because of the pipelining |
c627d613 | 1547 | */ |
827c37f6 | 1548 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
c627d613 | 1549 | { |
85aecef6 | 1550 | char fbuf[MAXPATHLEN]; |
6931c138 | 1551 | int i, prev_i = 0; |
c627d613 | 1552 | |
910ee8c9 WD |
1553 | if (!flist) |
1554 | return; | |
1555 | if (flist->count == 0) { | |
1556 | flist->high = -1; | |
3ec4dd97 | 1557 | return; |
910ee8c9 | 1558 | } |
3ec4dd97 | 1559 | |
ebed4c3a | 1560 | qsort(flist->files, flist->count, |
a4a7e64c | 1561 | sizeof flist->files[0], (int (*)())file_compare); |
ebed4c3a | 1562 | |
827c37f6 | 1563 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
b91b50c0 | 1564 | if (flist->files[i]->basename) { |
6931c138 | 1565 | prev_i = i; |
b91b50c0 WD |
1566 | break; |
1567 | } | |
1568 | } | |
f3c3ed44 | 1569 | flist->low = prev_i; |
b91b50c0 | 1570 | while (++i < flist->count) { |
fe1c19dc | 1571 | int j; |
f5db0993 WD |
1572 | struct file_struct *file = flist->files[i]; |
1573 | ||
1574 | if (!file->basename) | |
b91b50c0 | 1575 | continue; |
fe1c19dc WD |
1576 | if (f_name_cmp(file, flist->files[prev_i]) == 0) |
1577 | j = prev_i; | |
1578 | else if (protocol_version >= 29 && S_ISDIR(file->mode)) { | |
f5db0993 WD |
1579 | int save_mode = file->mode; |
1580 | /* Make sure that this directory doesn't duplicate a | |
1581 | * non-directory earlier in the list. */ | |
f5db0993 | 1582 | flist->high = prev_i; |
fe1c19dc WD |
1583 | file->mode = S_IFREG; |
1584 | j = flist_find(flist, file); | |
f5db0993 | 1585 | file->mode = save_mode; |
fe1c19dc WD |
1586 | } else |
1587 | j = -1; | |
1588 | if (j >= 0) { | |
1589 | struct file_struct *fp = flist->files[j]; | |
1590 | int keep, drop; | |
1591 | /* If one is a dir and the other is not, we want to | |
1592 | * keep the dir because it might have contents in the | |
1593 | * list. */ | |
1594 | if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) { | |
1595 | if (S_ISDIR(file->mode)) | |
1596 | keep = i, drop = j; | |
1597 | else | |
1598 | keep = j, drop = i; | |
1599 | } else | |
1600 | keep = j, drop = i; | |
b91b50c0 | 1601 | if (verbose > 1 && !am_server) { |
ebed4c3a | 1602 | rprintf(FINFO, |
fe1c19dc | 1603 | "removing duplicate name %s from file list (%d)\n", |
85aecef6 | 1604 | f_name(file, fbuf), drop); |
b91b50c0 | 1605 | } |
9c000f5e WD |
1606 | /* Make sure we don't lose track of a user-specified |
1607 | * top directory. */ | |
0f0b2e66 WD |
1608 | flist->files[keep]->flags |= flist->files[drop]->flags |
1609 | & (FLAG_TOP_DIR|FLAG_DEL_HERE); | |
fe1c19dc WD |
1610 | |
1611 | clear_file(drop, flist); | |
9935066b | 1612 | |
fe1c19dc WD |
1613 | if (keep == i) { |
1614 | if (flist->low == drop) { | |
1615 | for (j = drop + 1; | |
1616 | j < i && !flist->files[j]->basename; | |
1617 | j++) {} | |
1618 | flist->low = j; | |
1619 | } | |
1620 | prev_i = i; | |
1621 | } | |
728d0922 | 1622 | } else |
6931c138 | 1623 | prev_i = i; |
3ec4dd97 | 1624 | } |
f5db0993 | 1625 | flist->high = no_dups ? prev_i : flist->count - 1; |
0199b05f | 1626 | |
c0b134a4 WD |
1627 | if (strip_root) { |
1628 | /* We need to strip off the leading slashes for relative | |
1629 | * paths, but this must be done _after_ the sorting phase. */ | |
1630 | for (i = flist->low; i <= flist->high; i++) { | |
1631 | struct file_struct *file = flist->files[i]; | |
1632 | ||
1633 | if (!file->dirname) | |
1634 | continue; | |
1635 | while (*file->dirname == '/') | |
1636 | file->dirname++; | |
1637 | if (!*file->dirname) | |
1638 | file->dirname = NULL; | |
1639 | } | |
1640 | } | |
1641 | ||
85aecef6 | 1642 | if (prune_empty_dirs && no_dups && max_dir_depth) { |
9c000f5e WD |
1643 | int j, cur_depth = 0; |
1644 | int *maybe_dirs = new_array(int, max_dir_depth); | |
1645 | ||
1646 | maybe_dirs[0] = -1; | |
1647 | ||
f5db0993 WD |
1648 | for (i = flist->low; i <= flist->high; i++) { |
1649 | struct file_struct *file = flist->files[i]; | |
ebed4c3a | 1650 | |
9c000f5e WD |
1651 | if (S_ISDIR(file->mode) && file->dir.depth) { |
1652 | j = cur_depth; | |
1653 | cur_depth = file->dir.depth - 1; | |
1654 | for ( ; j >= cur_depth; j--) { | |
1655 | if (maybe_dirs[j] < 0) | |
1656 | continue; | |
1657 | clear_file(maybe_dirs[j], flist); | |
1658 | } | |
85aecef6 WD |
1659 | if (is_excluded(f_name(file, fbuf), 1, |
1660 | ALL_FILTERS)) { | |
1661 | for (j = 0; j <= cur_depth; j++) | |
1662 | maybe_dirs[j] = -1; | |
1663 | } else | |
1664 | maybe_dirs[cur_depth] = i; | |
9c000f5e WD |
1665 | } else if (maybe_dirs[cur_depth] >= 0) { |
1666 | for (j = 0; j <= cur_depth; j++) | |
1667 | maybe_dirs[j] = -1; | |
0199b05f | 1668 | } |
9c000f5e WD |
1669 | } |
1670 | for (j = cur_depth; j >= 0; j--) { | |
1671 | if (maybe_dirs[j] < 0) | |
1672 | continue; | |
1673 | clear_file(maybe_dirs[j], flist); | |
1674 | } | |
f5db0993 | 1675 | |
9c000f5e WD |
1676 | for (i = flist->low; i <= flist->high; i++) { |
1677 | if (flist->files[i]->basename) | |
1678 | break; | |
1679 | } | |
1680 | flist->low = i; | |
1681 | for (i = flist->high; i >= flist->low; i--) { | |
1682 | if (flist->files[i]->basename) | |
1683 | break; | |
0199b05f | 1684 | } |
9c000f5e | 1685 | flist->high = i; |
0199b05f | 1686 | } |
cefed3e8 | 1687 | } |
0199b05f | 1688 | |
32cbfe7b | 1689 | static void output_flist(struct file_list *flist) |
cefed3e8 | 1690 | { |
f3c3ed44 | 1691 | char uidbuf[16], gidbuf[16], depthbuf[16]; |
cefed3e8 | 1692 | struct file_struct *file; |
32cbfe7b | 1693 | const char *who = who_am_i(); |
cefed3e8 | 1694 | int i; |
0199b05f | 1695 | |
ebed4c3a | 1696 | for (i = 0; i < flist->count; i++) { |
cefed3e8 | 1697 | file = flist->files[i]; |
56194bcd | 1698 | if ((am_root || am_sender) && preserve_uid) |
7b6fa00f | 1699 | sprintf(uidbuf, " uid=%ld", (long)file->uid); |
f05f993e WD |
1700 | else |
1701 | *uidbuf = '\0'; | |
7b6fa00f WD |
1702 | if (preserve_gid && file->gid != GID_NONE) |
1703 | sprintf(gidbuf, " gid=%ld", (long)file->gid); | |
f05f993e WD |
1704 | else |
1705 | *gidbuf = '\0'; | |
f3c3ed44 WD |
1706 | if (!am_sender) |
1707 | sprintf(depthbuf, "%d", file->dir.depth); | |
14698a3a | 1708 | rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n", |
32cbfe7b | 1709 | who, i, am_sender ? NS(file->dir.root) : depthbuf, |
0ee6ca98 | 1710 | file->dirname ? file->dirname : "", |
14698a3a WD |
1711 | file->dirname ? "/" : "", NS(file->basename), |
1712 | S_ISDIR(file->mode) ? "/" : "", (int)file->mode, | |
f3c3ed44 | 1713 | (double)file->length, uidbuf, gidbuf, file->flags); |
0199b05f | 1714 | } |
3ec4dd97 AT |
1715 | } |
1716 | ||
8824e2ce | 1717 | enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING }; |
f5db0993 | 1718 | enum fnc_type { t_PATH, t_ITEM }; |
8018edd3 | 1719 | |
2f3cad89 | 1720 | /* Compare the names of two file_struct entities, similar to how strcmp() |
f5db0993 WD |
1721 | * would do if it were operating on the joined strings. |
1722 | * | |
1723 | * Some differences beginning with protocol_version 29: (1) directory names | |
1724 | * are compared with an assumed trailing slash so that they compare in a | |
1725 | * way that would cause them to sort immediately prior to any content they | |
1726 | * may have; (2) a directory of any name compares after a non-directory of | |
1727 | * any name at the same depth; (3) a directory with name "." compares prior | |
1728 | * to anything else. These changes mean that a directory and a non-dir | |
1729 | * with the same name will not compare as equal (protocol_version >= 29). | |
1730 | * | |
1731 | * The dirname component can be an empty string, but the basename component | |
1732 | * cannot (and never is in the current codebase). The basename component | |
1733 | * may be NULL (for a removed item), in which case it is considered to be | |
1734 | * after any existing item. */ | |
8018edd3 | 1735 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 1736 | { |
8018edd3 WD |
1737 | int dif; |
1738 | const uchar *c1, *c2; | |
1ef00d20 | 1739 | enum fnc_state state1, state2; |
f5db0993 WD |
1740 | enum fnc_type type1, type2; |
1741 | enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM; | |
8018edd3 WD |
1742 | |
1743 | if (!f1 || !f1->basename) { | |
1744 | if (!f2 || !f2->basename) | |
1745 | return 0; | |
1746 | return -1; | |
1747 | } | |
1748 | if (!f2 || !f2->basename) | |
1749 | return 1; | |
1750 | ||
14698a3a WD |
1751 | c1 = (uchar*)f1->dirname; |
1752 | c2 = (uchar*)f2->dirname; | |
1753 | if (c1 == c2) | |
1754 | c1 = c2 = NULL; | |
1755 | if (!c1) { | |
f5db0993 | 1756 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
e90b8ace | 1757 | c1 = (uchar*)f1->basename; |
f5db0993 WD |
1758 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
1759 | type1 = t_ITEM; | |
1760 | state1 = s_TRAILING; | |
1761 | c1 = (uchar*)""; | |
1762 | } else | |
1763 | state1 = s_BASE; | |
080ddf58 | 1764 | } else if (!*c1) { |
f5db0993 | 1765 | type1 = t_path; |
8824e2ce | 1766 | state1 = s_SLASH; |
080ddf58 | 1767 | c1 = (uchar*)"/"; |
f5db0993 WD |
1768 | } else { |
1769 | type1 = t_path; | |
8824e2ce | 1770 | state1 = s_DIR; |
f5db0993 | 1771 | } |
14698a3a | 1772 | if (!c2) { |
f5db0993 | 1773 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
e90b8ace | 1774 | c2 = (uchar*)f2->basename; |
f5db0993 WD |
1775 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
1776 | type2 = t_ITEM; | |
1777 | state2 = s_TRAILING; | |
1778 | c2 = (uchar*)""; | |
1779 | } else | |
1780 | state2 = s_BASE; | |
080ddf58 | 1781 | } else if (!*c2) { |
f5db0993 | 1782 | type2 = t_path; |
8824e2ce | 1783 | state2 = s_SLASH; |
080ddf58 | 1784 | c2 = (uchar*)"/"; |
f5db0993 WD |
1785 | } else { |
1786 | type2 = t_path; | |
8824e2ce | 1787 | state2 = s_DIR; |
f5db0993 WD |
1788 | } |
1789 | ||
1790 | if (type1 != type2) | |
1791 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 WD |
1792 | |
1793 | while (1) { | |
f5db0993 | 1794 | if ((dif = (int)*c1++ - (int)*c2++) != 0) |
8018edd3 | 1795 | break; |
f5db0993 | 1796 | if (!*c1) { |
8018edd3 | 1797 | switch (state1) { |
8824e2ce WD |
1798 | case s_DIR: |
1799 | state1 = s_SLASH; | |
e90b8ace | 1800 | c1 = (uchar*)"/"; |
8018edd3 | 1801 | break; |
8824e2ce | 1802 | case s_SLASH: |
f5db0993 | 1803 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
e90b8ace | 1804 | c1 = (uchar*)f1->basename; |
cbb5fa4f WD |
1805 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
1806 | type1 = t_ITEM; | |
1807 | state1 = s_TRAILING; | |
1808 | c1 = (uchar*)""; | |
1809 | } else | |
1810 | state1 = s_BASE; | |
8018edd3 | 1811 | break; |
8824e2ce WD |
1812 | case s_BASE: |
1813 | state1 = s_TRAILING; | |
f5db0993 | 1814 | if (type1 == t_PATH) { |
2f3cad89 | 1815 | c1 = (uchar*)"/"; |
f5db0993 WD |
1816 | break; |
1817 | } | |
1818 | /* FALL THROUGH */ | |
8824e2ce | 1819 | case s_TRAILING: |
f5db0993 | 1820 | type1 = t_ITEM; |
8018edd3 WD |
1821 | break; |
1822 | } | |
f5db0993 WD |
1823 | if (*c2 && type1 != type2) |
1824 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 1825 | } |
f5db0993 | 1826 | if (!*c2) { |
8018edd3 | 1827 | switch (state2) { |
8824e2ce WD |
1828 | case s_DIR: |
1829 | state2 = s_SLASH; | |
e90b8ace | 1830 | c2 = (uchar*)"/"; |
8018edd3 | 1831 | break; |
8824e2ce | 1832 | case s_SLASH: |
f5db0993 | 1833 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
e90b8ace | 1834 | c2 = (uchar*)f2->basename; |
cbb5fa4f WD |
1835 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
1836 | type2 = t_ITEM; | |
1837 | state2 = s_TRAILING; | |
1838 | c2 = (uchar*)""; | |
1839 | } else | |
1840 | state2 = s_BASE; | |
8018edd3 | 1841 | break; |
8824e2ce | 1842 | case s_BASE: |
8824e2ce | 1843 | state2 = s_TRAILING; |
f5db0993 | 1844 | if (type2 == t_PATH) { |
2f3cad89 | 1845 | c2 = (uchar*)"/"; |
f5db0993 WD |
1846 | break; |
1847 | } | |
1848 | /* FALL THROUGH */ | |
8824e2ce | 1849 | case s_TRAILING: |
f5db0993 WD |
1850 | if (!*c1) |
1851 | return 0; | |
1852 | type2 = t_ITEM; | |
8018edd3 WD |
1853 | break; |
1854 | } | |
f5db0993 WD |
1855 | if (type1 != type2) |
1856 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 WD |
1857 | } |
1858 | } | |
1859 | ||
1860 | return dif; | |
1861 | } | |
1862 | ||
8018edd3 | 1863 | /* Return a copy of the full filename of a flist entry, using the indicated |
5e4ff5f9 WD |
1864 | * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is |
1865 | * done because we checked the size when creating the file_struct entry. | |
8018edd3 | 1866 | */ |
5e4ff5f9 | 1867 | char *f_name(struct file_struct *f, char *fbuf) |
8018edd3 | 1868 | { |
ebed4c3a MP |
1869 | if (!f || !f->basename) |
1870 | return NULL; | |
3ec4dd97 | 1871 | |
5e4ff5f9 WD |
1872 | if (!fbuf) { |
1873 | static char names[5][MAXPATHLEN]; | |
1874 | static unsigned int n; | |
1875 | ||
1876 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
1877 | ||
1878 | fbuf = names[n]; | |
1879 | } | |
1880 | ||
3ec4dd97 | 1881 | if (f->dirname) { |
882e6893 WD |
1882 | int len = strlen(f->dirname); |
1883 | memcpy(fbuf, f->dirname, len); | |
1884 | fbuf[len] = '/'; | |
1885 | strcpy(fbuf + len + 1, f->basename); | |
8018edd3 | 1886 | } else |
882e6893 | 1887 | strcpy(fbuf, f->basename); |
0ee6ca98 | 1888 | |
b7736c79 | 1889 | return fbuf; |
8018edd3 | 1890 | } |
e03dfae5 | 1891 | |
32cbfe7b WD |
1892 | /* Do a non-recursive scan of the named directory, possibly ignoring all |
1893 | * exclude rules except for the daemon's. If "dlen" is >=0, it is the length | |
1894 | * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN | |
1895 | * buffer (the functions we call will append names onto the end, but the old | |
1896 | * dir value will be restored on exit). */ | |
1897 | struct file_list *get_dirlist(char *dirname, int dlen, | |
1898 | int ignore_filter_rules) | |
37802f40 WD |
1899 | { |
1900 | struct file_list *dirlist; | |
1901 | char dirbuf[MAXPATHLEN]; | |
37802f40 WD |
1902 | int save_recurse = recurse; |
1903 | ||
32cbfe7b WD |
1904 | if (dlen < 0) { |
1905 | dlen = strlcpy(dirbuf, dirname, MAXPATHLEN); | |
1906 | if (dlen >= MAXPATHLEN) | |
1907 | return NULL; | |
1908 | dirname = dirbuf; | |
1909 | } | |
37802f40 WD |
1910 | |
1911 | dirlist = flist_new(WITHOUT_HLINK, "get_dirlist"); | |
32cbfe7b | 1912 | |
37802f40 | 1913 | recurse = 0; |
32cbfe7b | 1914 | send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen); |
37802f40 | 1915 | recurse = save_recurse; |
53135fe8 WD |
1916 | if (do_progress) |
1917 | flist_count_offset += dirlist->count; | |
37802f40 | 1918 | |
564ef546 WD |
1919 | clean_flist(dirlist, 0, 0); |
1920 | ||
45478cc7 | 1921 | if (verbose > 3) |
32cbfe7b | 1922 | output_flist(dirlist); |
45478cc7 | 1923 | |
32cbfe7b | 1924 | return dirlist; |
45478cc7 | 1925 | } |