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