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