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