Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Routines that are exclusive to the generator process. | |
3 | * | |
4 | * Copyright (C) 1996-2000 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
6 | * Copyright (C) 2002 Martin Pool <mbp@samba.org> | |
7 | * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
e7c67065 WD |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 22 | */ |
2f03f956 AT |
23 | |
24 | #include "rsync.h" | |
25 | ||
26 | extern int verbose; | |
27 | extern int dry_run; | |
beb51aa0 | 28 | extern int do_xfers; |
17bda2d1 | 29 | extern int stdout_format_has_i; |
ecc7623e | 30 | extern int logfile_format_has_i; |
6c3862fa WD |
31 | extern int am_root; |
32 | extern int am_server; | |
33 | extern int am_daemon; | |
083acd49 | 34 | extern int do_progress; |
2f03f956 | 35 | extern int relative_paths; |
ccb16b46 | 36 | extern int implied_dirs; |
716e73d4 | 37 | extern int keep_dirlinks; |
2f03f956 | 38 | extern int preserve_links; |
2f03f956 | 39 | extern int preserve_devices; |
b5c6a6ae | 40 | extern int preserve_specials; |
2f03f956 | 41 | extern int preserve_hard_links; |
6744b62d WD |
42 | extern int preserve_perms; |
43 | extern int preserve_uid; | |
44 | extern int preserve_gid; | |
3ea9bbd6 WD |
45 | extern int preserve_times; |
46 | extern int omit_dir_times; | |
e35d9f2d | 47 | extern int delete_mode; |
59faec8b | 48 | extern int delete_before; |
fa13f396 | 49 | extern int delete_during; |
59faec8b WD |
50 | extern int delete_after; |
51 | extern int module_id; | |
52 | extern int ignore_errors; | |
47c11975 | 53 | extern int remove_source_files; |
70352269 | 54 | extern int delay_updates; |
2f03f956 | 55 | extern int update_only; |
e90aab49 WD |
56 | extern int ignore_existing; |
57 | extern int ignore_non_existing; | |
cd6aa5b5 | 58 | extern int inplace; |
6cc11982 | 59 | extern int append_mode; |
cd6aa5b5 | 60 | extern int make_backups; |
2f03f956 AT |
61 | extern int csum_length; |
62 | extern int ignore_times; | |
f83f0548 | 63 | extern int size_only; |
7d1bfaf7 | 64 | extern OFF_T max_size; |
02b5cb23 | 65 | extern OFF_T min_size; |
59faec8b | 66 | extern int io_error; |
ac40b747 | 67 | extern int allowed_lull; |
ee1d11c4 | 68 | extern int sock_f_out; |
9ac2395b | 69 | extern int ignore_timeout; |
d04e9c51 | 70 | extern int protocol_version; |
8e85be0a | 71 | extern int fuzzy_basis; |
2f03f956 | 72 | extern int always_checksum; |
cfe39780 | 73 | extern int checksum_len; |
a7260c40 | 74 | extern char *partial_dir; |
b7e8628c | 75 | extern char *basis_dir[]; |
2be2fb3e | 76 | extern int compare_dest; |
967866d4 | 77 | extern int copy_dest; |
59c95e42 | 78 | extern int link_dest; |
5774786f | 79 | extern int whole_file; |
5774786f | 80 | extern int list_only; |
1ef5bf3c | 81 | extern int new_root_dir; |
b9f592fb | 82 | extern int read_batch; |
5774786f | 83 | extern int safe_symlinks; |
a255c592 | 84 | extern long block_size; /* "long" because popt can't set an int32. */ |
59faec8b WD |
85 | extern int max_delete; |
86 | extern int force_delete; | |
0e82af2d | 87 | extern int one_file_system; |
6c3862fa | 88 | extern struct stats stats; |
0e82af2d | 89 | extern dev_t filesystem_dev; |
59faec8b WD |
90 | extern char *backup_dir; |
91 | extern char *backup_suffix; | |
92 | extern int backup_suffix_len; | |
ee1d11c4 | 93 | extern struct file_list *the_file_list; |
7842418b | 94 | extern struct filter_list_struct server_filter_list; |
97f9dcae | 95 | |
146c2c36 WD |
96 | int ignore_perishable = 0; |
97 | int non_perishable_cnt = 0; | |
98 | ||
59faec8b WD |
99 | static int deletion_count = 0; /* used to implement --max-delete */ |
100 | ||
c6fadc0e WD |
101 | /* For calling delete_item() */ |
102 | #define DEL_RECURSE (1<<1) /* recurse */ | |
146c2c36 | 103 | #define DEL_DIR_IS_EMPTY (1<<2) /* used by delete_dir_contents() */ |
d71dad3b | 104 | |
116a4769 WD |
105 | enum nonregtype { |
106 | TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK | |
107 | }; | |
59faec8b | 108 | |
c6fadc0e | 109 | enum delret { |
146c2c36 | 110 | DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY |
c6fadc0e WD |
111 | }; |
112 | ||
113 | /* Forward declaration for delete_item(). */ | |
114 | static enum delret delete_dir_contents(char *fname, int flags); | |
115 | ||
116 | ||
59faec8b WD |
117 | static int is_backup_file(char *fn) |
118 | { | |
119 | int k = strlen(fn) - backup_suffix_len; | |
120 | return k > 0 && strcmp(fn+k, backup_suffix) == 0; | |
121 | } | |
122 | ||
c6fadc0e WD |
123 | /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will |
124 | * delete recursively. | |
0e5665d3 WD |
125 | * |
126 | * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's | |
127 | * a directory! (The buffer is used for recursion, but returned unchanged.) | |
128 | */ | |
c6fadc0e | 129 | static enum delret delete_item(char *fname, int mode, char *replace, int flags) |
59faec8b | 130 | { |
c6fadc0e WD |
131 | enum delret ret; |
132 | char *what; | |
133 | int ok; | |
59faec8b | 134 | |
c6fadc0e WD |
135 | if (verbose > 2) { |
136 | rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n", | |
137 | fname, mode, flags); | |
59faec8b WD |
138 | } |
139 | ||
146c2c36 WD |
140 | if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) { |
141 | ignore_perishable = 1; | |
142 | /* If DEL_RECURSE is not set, this just reports emptiness. */ | |
c6fadc0e | 143 | ret = delete_dir_contents(fname, flags); |
146c2c36 WD |
144 | ignore_perishable = 0; |
145 | if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT) | |
c6fadc0e WD |
146 | goto check_ret; |
147 | /* OK: try to delete the directory. */ | |
148 | } | |
149 | ||
150 | if (!replace && max_delete >= 0 && ++deletion_count > max_delete) | |
151 | return DR_AT_LIMIT; | |
152 | ||
153 | if (S_ISDIR(mode)) { | |
154 | what = "rmdir"; | |
59faec8b | 155 | ok = do_rmdir(fname) == 0; |
c6fadc0e WD |
156 | } else if (make_backups && (backup_dir || !is_backup_file(fname))) { |
157 | what = "make_backup"; | |
158 | ok = make_backup(fname); | |
159 | } else { | |
160 | what = "unlink"; | |
161 | ok = robust_unlink(fname) == 0; | |
162 | } | |
163 | ||
59faec8b | 164 | if (ok) { |
c6fadc0e | 165 | if (!replace) |
59faec8b | 166 | log_delete(fname, mode); |
c6fadc0e WD |
167 | ret = DR_SUCCESS; |
168 | } else { | |
169 | if (S_ISDIR(mode) && errno == ENOTEMPTY) { | |
146c2c36 | 170 | rprintf(FINFO, "cannot delete non-empty directory: %s\n", |
c6fadc0e WD |
171 | fname); |
172 | ret = DR_NOT_EMPTY; | |
173 | } else if (errno != ENOENT) { | |
174 | rsyserr(FERROR, errno, "delete_file: %s(%s) failed", | |
175 | what, full_fname(fname)); | |
176 | ret = DR_FAILURE; | |
177 | } else { | |
178 | deletion_count--; | |
179 | ret = DR_SUCCESS; | |
180 | } | |
f75a53e7 | 181 | } |
c6fadc0e WD |
182 | |
183 | check_ret: | |
184 | if (replace && ret != DR_SUCCESS) { | |
185 | rprintf(FERROR, "could not make way for new %s: %s\n", | |
186 | replace, fname); | |
59faec8b | 187 | } |
c6fadc0e WD |
188 | return ret; |
189 | } | |
190 | ||
146c2c36 WD |
191 | /* The directory is about to be deleted: if DEL_RECURSE is given, delete all |
192 | * its contents, otherwise just checks for content. Returns DR_SUCCESS or | |
193 | * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The | |
194 | * buffer is used for recursion, but returned unchanged.) | |
c6fadc0e WD |
195 | */ |
196 | static enum delret delete_dir_contents(char *fname, int flags) | |
197 | { | |
198 | struct file_list *dirlist; | |
146c2c36 | 199 | enum delret ret; |
c6fadc0e WD |
200 | unsigned remainder; |
201 | void *save_filters; | |
202 | int j, dlen; | |
203 | char *p; | |
59faec8b | 204 | |
f5761a34 WD |
205 | if (verbose > 3) { |
206 | rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n", | |
207 | fname, flags); | |
208 | } | |
209 | ||
0e5665d3 WD |
210 | dlen = strlen(fname); |
211 | save_filters = push_local_filters(fname, dlen); | |
212 | ||
146c2c36 | 213 | non_perishable_cnt = 0; |
0e5665d3 | 214 | dirlist = get_dirlist(fname, dlen, 0); |
146c2c36 WD |
215 | ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS; |
216 | ||
217 | if (!dirlist->count) | |
218 | goto done; | |
219 | ||
220 | if (!(flags & DEL_RECURSE)) { | |
221 | ret = DR_NOT_EMPTY; | |
222 | goto done; | |
223 | } | |
0e5665d3 WD |
224 | |
225 | p = fname + dlen; | |
226 | if (dlen != 1 || *fname != '/') | |
227 | *p++ = '/'; | |
228 | remainder = MAXPATHLEN - (p - fname); | |
59faec8b | 229 | |
f5761a34 | 230 | /* We do our own recursion, so make delete_item() non-recursive. */ |
146c2c36 | 231 | flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY; |
c6fadc0e | 232 | |
59faec8b WD |
233 | for (j = dirlist->count; j--; ) { |
234 | struct file_struct *fp = dirlist->files[j]; | |
235 | ||
c6fadc0e WD |
236 | if (fp->flags & FLAG_MOUNT_POINT) { |
237 | if (verbose > 1) { | |
238 | rprintf(FINFO, | |
239 | "mount point, %s, pins parent directory\n", | |
240 | f_name(fp, NULL)); | |
241 | } | |
146c2c36 | 242 | ret = DR_NOT_EMPTY; |
59faec8b | 243 | continue; |
c6fadc0e | 244 | } |
59faec8b | 245 | |
0e5665d3 | 246 | strlcpy(p, fp->basename, remainder); |
146c2c36 WD |
247 | /* Save stack by recursing to ourself directly. */ |
248 | if (S_ISDIR(fp->mode) | |
249 | && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS) | |
250 | ret = DR_NOT_EMPTY; | |
251 | if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS) | |
f5761a34 | 252 | ret = DR_NOT_EMPTY; |
59faec8b | 253 | } |
59faec8b | 254 | |
0e5665d3 WD |
255 | fname[dlen] = '\0'; |
256 | ||
146c2c36 | 257 | done: |
c6fadc0e | 258 | flist_free(dirlist); |
146c2c36 | 259 | pop_local_filters(save_filters); |
59faec8b | 260 | |
146c2c36 WD |
261 | if (ret == DR_NOT_EMPTY) { |
262 | rprintf(FINFO, "cannot delete non-empty directory: %s\n", | |
263 | fname); | |
264 | } | |
c6fadc0e | 265 | return ret; |
59faec8b WD |
266 | } |
267 | ||
268 | ||
269 | /* This function is used to implement per-directory deletion, and is used by | |
270 | * all the --delete-WHEN options. Note that the fbuf pointer must point to a | |
271 | * MAXPATHLEN buffer with the name of the directory in it (the functions we | |
272 | * call will append names onto the end, but the old dir value will be restored | |
273 | * on exit). */ | |
274 | static void delete_in_dir(struct file_list *flist, char *fbuf, | |
0e82af2d | 275 | struct file_struct *file, STRUCT_STAT *stp) |
59faec8b WD |
276 | { |
277 | static int min_depth = MAXPATHLEN, cur_depth = -1; | |
278 | static void *filt_array[MAXPATHLEN/2+1]; | |
717b0430 | 279 | static int already_warned = 0; |
59faec8b WD |
280 | struct file_list *dirlist; |
281 | char delbuf[MAXPATHLEN]; | |
468d7668 | 282 | int dlen, i; |
59faec8b WD |
283 | |
284 | if (!flist) { | |
285 | while (cur_depth >= min_depth) | |
286 | pop_local_filters(filt_array[cur_depth--]); | |
287 | min_depth = MAXPATHLEN; | |
288 | cur_depth = -1; | |
289 | return; | |
290 | } | |
291 | ||
292 | if (verbose > 2) | |
45c49b52 | 293 | rprintf(FINFO, "delete_in_dir(%s)\n", fbuf); |
59faec8b WD |
294 | |
295 | if (allowed_lull) | |
ee1d11c4 | 296 | maybe_send_keepalive(); |
59faec8b WD |
297 | |
298 | if (file->dir.depth >= MAXPATHLEN/2+1) | |
299 | return; /* Impossible... */ | |
300 | ||
59faec8b | 301 | if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) { |
717b0430 | 302 | if (already_warned) |
f75a53e7 | 303 | return; |
59faec8b WD |
304 | rprintf(FINFO, |
305 | "IO error encountered -- skipping file deletion\n"); | |
717b0430 | 306 | already_warned = 1; |
59faec8b WD |
307 | return; |
308 | } | |
309 | ||
310 | while (cur_depth >= file->dir.depth && cur_depth >= min_depth) | |
311 | pop_local_filters(filt_array[cur_depth--]); | |
312 | cur_depth = file->dir.depth; | |
313 | if (min_depth > cur_depth) | |
314 | min_depth = cur_depth; | |
315 | dlen = strlen(fbuf); | |
316 | filt_array[cur_depth] = push_local_filters(fbuf, dlen); | |
317 | ||
0e82af2d WD |
318 | if (one_file_system) { |
319 | if (file->flags & FLAG_TOP_DIR) | |
320 | filesystem_dev = stp->st_dev; | |
321 | else if (filesystem_dev != stp->st_dev) | |
322 | return; | |
323 | } | |
59faec8b | 324 | |
59faec8b WD |
325 | dirlist = get_dirlist(fbuf, dlen, 0); |
326 | ||
327 | /* If an item in dirlist is not found in flist, delete it | |
328 | * from the filesystem. */ | |
329 | for (i = dirlist->count; i--; ) { | |
f3ab64d3 | 330 | struct file_struct *fp = dirlist->files[i]; |
c6fadc0e WD |
331 | if (!fp->basename) |
332 | continue; | |
333 | if (fp->flags & FLAG_MOUNT_POINT) { | |
334 | if (verbose > 1) | |
146c2c36 | 335 | rprintf(FINFO, "cannot delete mount point: %s\n", |
c6fadc0e | 336 | f_name(fp, NULL)); |
bb0d8edf | 337 | continue; |
c6fadc0e | 338 | } |
468d7668 | 339 | if (flist_find(flist, fp) < 0) { |
6cbde57d | 340 | f_name(fp, delbuf); |
c6fadc0e | 341 | delete_item(delbuf, fp->mode, NULL, DEL_RECURSE); |
468d7668 | 342 | } |
59faec8b WD |
343 | } |
344 | ||
345 | flist_free(dirlist); | |
346 | } | |
347 | ||
348 | /* This deletes any files on the receiving side that are not present on the | |
349 | * sending side. This is used by --delete-before and --delete-after. */ | |
ee1d11c4 | 350 | static void do_delete_pass(struct file_list *flist) |
59faec8b WD |
351 | { |
352 | char fbuf[MAXPATHLEN]; | |
bb0d8edf | 353 | STRUCT_STAT st; |
59faec8b WD |
354 | int j; |
355 | ||
caff3322 WD |
356 | /* dry_run is incremented when the destination doesn't exist yet. */ |
357 | if (dry_run > 1 || list_only) | |
f75a53e7 WD |
358 | return; |
359 | ||
59faec8b WD |
360 | for (j = 0; j < flist->count; j++) { |
361 | struct file_struct *file = flist->files[j]; | |
362 | ||
363 | if (!(file->flags & FLAG_DEL_HERE)) | |
364 | continue; | |
365 | ||
6cbde57d | 366 | f_name(file, fbuf); |
59faec8b | 367 | if (verbose > 1 && file->flags & FLAG_TOP_DIR) |
45c49b52 | 368 | rprintf(FINFO, "deleting in %s\n", fbuf); |
59faec8b | 369 | |
bb0d8edf WD |
370 | if (link_stat(fbuf, &st, keep_dirlinks) < 0 |
371 | || !S_ISDIR(st.st_mode)) | |
372 | continue; | |
373 | ||
0e82af2d | 374 | delete_in_dir(flist, fbuf, file, &st); |
59faec8b | 375 | } |
0e82af2d | 376 | delete_in_dir(NULL, NULL, NULL, NULL); |
0e5665d3 | 377 | |
a06e2b7c WD |
378 | if (do_progress && !am_server) |
379 | rprintf(FINFO, " \r"); | |
59faec8b WD |
380 | } |
381 | ||
48224e4c | 382 | int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st) |
2f03f956 | 383 | { |
b7e8628c WD |
384 | if (preserve_perms |
385 | && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) | |
84acca07 | 386 | return 0; |
bb24028f | 387 | |
7b6fa00f | 388 | if (am_root && preserve_uid && st->st_uid != file->uid) |
b7e8628c | 389 | return 0; |
bb24028f | 390 | |
7b6fa00f | 391 | if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid) |
b7e8628c WD |
392 | return 0; |
393 | ||
394 | return 1; | |
395 | } | |
396 | ||
ee1d11c4 | 397 | void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st, |
ef20efcb | 398 | int32 iflags, uchar fnamecmp_type, char *xname) |
06a1dbad | 399 | { |
48224e4c WD |
400 | if (statret >= 0) { /* A from-dest-dir statret can == 1! */ |
401 | int keep_time = !preserve_times ? 0 | |
402 | : S_ISDIR(file->mode) ? !omit_dir_times | |
403 | : !S_ISLNK(file->mode); | |
404 | ||
c557eb8c WD |
405 | if (S_ISREG(file->mode) && file->length != st->st_size) |
406 | iflags |= ITEM_REPORT_SIZE; | |
48224e4c | 407 | if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time |
116a4769 WD |
408 | && !(iflags & ITEM_MATCHED) |
409 | && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname)) | |
410 | || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0)) | |
48224e4c | 411 | iflags |= ITEM_REPORT_TIME; |
b9887818 | 412 | if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS)) |
48224e4c | 413 | iflags |= ITEM_REPORT_PERMS; |
7b6fa00f | 414 | if (preserve_uid && am_root && file->uid != st->st_uid) |
48224e4c | 415 | iflags |= ITEM_REPORT_OWNER; |
7b6fa00f WD |
416 | if (preserve_gid && file->gid != GID_NONE |
417 | && st->st_gid != file->gid) | |
48224e4c | 418 | iflags |= ITEM_REPORT_GROUP; |
ef20efcb | 419 | } else |
ee1d11c4 | 420 | iflags |= ITEM_IS_NEW; |
c557eb8c | 421 | |
a1d23b53 | 422 | iflags &= 0xffff; |
ee1d11c4 | 423 | if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1 |
17bda2d1 | 424 | || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) { |
6c3862fa WD |
425 | if (protocol_version >= 29) { |
426 | if (ndx >= 0) | |
ee1d11c4 WD |
427 | write_int(sock_f_out, ndx); |
428 | write_shortint(sock_f_out, iflags); | |
ef20efcb WD |
429 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) |
430 | write_byte(sock_f_out, fnamecmp_type); | |
431 | if (iflags & ITEM_XNAME_FOLLOWS) | |
432 | write_vstring(sock_f_out, xname, strlen(xname)); | |
1925c344 WD |
433 | } else if (ndx >= 0) { |
434 | enum logcode code = logfile_format_has_i ? FINFO : FCLIENT; | |
435 | log_item(code, file, &stats, iflags, xname); | |
436 | } | |
06a1dbad WD |
437 | } |
438 | } | |
439 | ||
440 | ||
b7e8628c | 441 | /* Perform our quick-check heuristic for determining if a file is unchanged. */ |
48224e4c | 442 | int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st) |
b7e8628c WD |
443 | { |
444 | if (st->st_size != file->length) | |
445 | return 0; | |
59c95e42 | 446 | |
2cda2560 | 447 | /* if always checksum is set then we use the checksum instead |
2f03f956 AT |
448 | of the file time to determine whether to sync */ |
449 | if (always_checksum && S_ISREG(st->st_mode)) { | |
450 | char sum[MD4_SUM_LENGTH]; | |
b7e8628c | 451 | file_checksum(fn, sum, st->st_size); |
cfe39780 | 452 | return memcmp(sum, file->u.sum, checksum_len) == 0; |
2f03f956 AT |
453 | } |
454 | ||
cc1e997d | 455 | if (size_only) |
84acca07 | 456 | return 1; |
2f03f956 | 457 | |
cc1e997d | 458 | if (ignore_times) |
84acca07 | 459 | return 0; |
cc1e997d | 460 | |
0f86c74e | 461 | return cmp_time(st->st_mtime, file->modtime) == 0; |
2f03f956 AT |
462 | } |
463 | ||
464 | ||
ec8290c8 | 465 | /* |
195bd906 | 466 | * set (initialize) the size entries in the per-file sum_struct |
ec8290c8 | 467 | * calculating dynamic block and checksum sizes. |
195bd906 | 468 | * |
ec8290c8 | 469 | * This is only called from generate_and_send_sums() but is a separate |
195bd906 S |
470 | * function to encapsulate the logic. |
471 | * | |
472 | * The block size is a rounded square root of file length. | |
473 | * | |
474 | * The checksum size is determined according to: | |
ed7e7955 | 475 | * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len) |
195bd906 S |
476 | * provided by Donovan Baarda which gives a probability of rsync |
477 | * algorithm corrupting data and falling back using the whole md4 | |
478 | * checksums. | |
479 | * | |
480 | * This might be made one of several selectable heuristics. | |
481 | */ | |
1490812a | 482 | static void sum_sizes_sqroot(struct sum_struct *sum, int64 len) |
195bd906 | 483 | { |
a255c592 | 484 | int32 blength; |
da9d12f5 | 485 | int s2length; |
195bd906 | 486 | |
a255c592 | 487 | if (block_size) |
195bd906 | 488 | blength = block_size; |
a255c592 | 489 | else if (len <= BLOCK_SIZE * BLOCK_SIZE) |
195bd906 | 490 | blength = BLOCK_SIZE; |
a255c592 WD |
491 | else { |
492 | int32 c; | |
1490812a | 493 | int64 l; |
eae7165c WD |
494 | int cnt; |
495 | for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {} | |
496 | if (cnt >= 31 || c >= MAX_BLOCK_SIZE) | |
497 | blength = MAX_BLOCK_SIZE; | |
498 | else { | |
499 | blength = 0; | |
500 | do { | |
501 | blength |= c; | |
1490812a | 502 | if (len < (int64)blength * blength) |
eae7165c WD |
503 | blength &= ~c; |
504 | c >>= 1; | |
505 | } while (c >= 8); /* round to multiple of 8 */ | |
506 | blength = MAX(blength, BLOCK_SIZE); | |
195bd906 | 507 | } |
195bd906 S |
508 | } |
509 | ||
d04e9c51 | 510 | if (protocol_version < 27) { |
195bd906 S |
511 | s2length = csum_length; |
512 | } else if (csum_length == SUM_LENGTH) { | |
513 | s2length = SUM_LENGTH; | |
514 | } else { | |
a255c592 | 515 | int32 c; |
1490812a | 516 | int64 l; |
da9d12f5 | 517 | int b = BLOCKSUM_BIAS; |
a255c592 | 518 | for (l = len; l >>= 1; b += 2) {} |
f4164b73 | 519 | for (c = blength; (c >>= 1) && b; b--) {} |
a255c592 WD |
520 | /* add a bit, subtract rollsum, round up. */ |
521 | s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */ | |
195bd906 S |
522 | s2length = MAX(s2length, csum_length); |
523 | s2length = MIN(s2length, SUM_LENGTH); | |
524 | } | |
525 | ||
526 | sum->flength = len; | |
527 | sum->blength = blength; | |
528 | sum->s2length = s2length; | |
a5b786d8 WD |
529 | sum->remainder = len % blength; |
530 | sum->count = len / blength + (sum->remainder != 0); | |
195bd906 S |
531 | |
532 | if (sum->count && verbose > 2) { | |
a255c592 WD |
533 | rprintf(FINFO, |
534 | "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n", | |
535 | (double)sum->count, (long)sum->remainder, (long)sum->blength, | |
da9d12f5 | 536 | sum->s2length, (double)sum->flength); |
195bd906 S |
537 | } |
538 | } | |
80605142 | 539 | |
bceec82f | 540 | |
80605142 WD |
541 | /* |
542 | * Generate and send a stream of signatures/checksums that describe a buffer | |
e66dfd18 | 543 | * |
80605142 WD |
544 | * Generate approximately one checksum every block_len bytes. |
545 | */ | |
cd6aa5b5 | 546 | static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy) |
2f03f956 | 547 | { |
a1cbe76e | 548 | int32 i; |
6e45e1dd | 549 | struct map_struct *mapbuf; |
80605142 | 550 | struct sum_struct sum; |
2f03f956 AT |
551 | OFF_T offset = 0; |
552 | ||
423dba8e | 553 | sum_sizes_sqroot(&sum, len); |
6cc11982 WD |
554 | write_sum_head(f_out, &sum); |
555 | ||
556 | if (append_mode > 0 && f_copy < 0) | |
557 | return; | |
e66dfd18 | 558 | |
6e45e1dd | 559 | if (len > 0) |
96d910c7 | 560 | mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength); |
6e45e1dd WD |
561 | else |
562 | mapbuf = NULL; | |
563 | ||
80605142 | 564 | for (i = 0; i < sum.count; i++) { |
a255c592 | 565 | int32 n1 = (int32)MIN(len, (OFF_T)sum.blength); |
6e45e1dd | 566 | char *map = map_ptr(mapbuf, offset, n1); |
80605142 | 567 | char sum2[SUM_LENGTH]; |
6cc11982 WD |
568 | uint32 sum1; |
569 | ||
570 | len -= n1; | |
571 | offset += n1; | |
2f03f956 | 572 | |
6cc11982 | 573 | if (f_copy >= 0) { |
cd6aa5b5 | 574 | full_write(f_copy, map, n1); |
6cc11982 WD |
575 | if (append_mode > 0) |
576 | continue; | |
577 | } | |
cd6aa5b5 | 578 | |
6cc11982 | 579 | sum1 = get_checksum1(map, n1); |
80605142 | 580 | get_checksum2(map, n1, sum2); |
2f03f956 | 581 | |
80605142 | 582 | if (verbose > 3) { |
e66dfd18 | 583 | rprintf(FINFO, |
a255c592 | 584 | "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n", |
6cc11982 | 585 | (double)i, (double)offset - n1, (long)n1, |
0e36d9da | 586 | (unsigned long)sum1); |
80605142 WD |
587 | } |
588 | write_int(f_out, sum1); | |
fc0257c9 | 589 | write_buf(f_out, sum2, sum.s2length); |
2f03f956 | 590 | } |
6e45e1dd WD |
591 | |
592 | if (mapbuf) | |
593 | unmap_file(mapbuf); | |
2f03f956 AT |
594 | } |
595 | ||
06a1dbad | 596 | |
8e85be0a WD |
597 | /* Try to find a filename in the same dir as "fname" with a similar name. */ |
598 | static int find_fuzzy(struct file_struct *file, struct file_list *dirlist) | |
599 | { | |
600 | int fname_len, fname_suf_len; | |
601 | const char *fname_suf, *fname = file->basename; | |
f328e0f3 | 602 | uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */ |
8e85be0a WD |
603 | int j, lowest_j = -1; |
604 | ||
605 | fname_len = strlen(fname); | |
606 | fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len); | |
607 | ||
608 | for (j = 0; j < dirlist->count; j++) { | |
609 | struct file_struct *fp = dirlist->files[j]; | |
610 | const char *suf, *name; | |
611 | int len, suf_len; | |
612 | uint32 dist; | |
613 | ||
614 | if (!S_ISREG(fp->mode) || !fp->length | |
615 | || fp->flags & FLAG_NO_FUZZY) | |
616 | continue; | |
617 | ||
618 | name = fp->basename; | |
619 | ||
620 | if (fp->length == file->length | |
0f86c74e | 621 | && cmp_time(fp->modtime, file->modtime) == 0) { |
8e85be0a WD |
622 | if (verbose > 4) { |
623 | rprintf(FINFO, | |
624 | "fuzzy size/modtime match for %s\n", | |
625 | name); | |
626 | } | |
627 | return j; | |
628 | } | |
629 | ||
630 | len = strlen(name); | |
631 | suf = find_filename_suffix(name, len, &suf_len); | |
632 | ||
633 | dist = fuzzy_distance(name, len, fname, fname_len); | |
634 | /* Add some extra weight to how well the suffixes match. */ | |
635 | dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len) | |
636 | * 10; | |
637 | if (verbose > 4) { | |
638 | rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n", | |
639 | name, (int)(dist>>16), (int)(dist&0xFFFF)); | |
640 | } | |
641 | if (dist <= lowest_dist) { | |
642 | lowest_dist = dist; | |
643 | lowest_j = j; | |
644 | } | |
645 | } | |
646 | ||
647 | return lowest_j; | |
648 | } | |
649 | ||
ee1d11c4 WD |
650 | void check_for_finished_hlinks(int itemizing, enum logcode code) |
651 | { | |
652 | struct file_struct *file; | |
653 | int ndx; | |
654 | ||
655 | while ((ndx = get_hlink_num()) != -1) { | |
656 | if (ndx < 0 || ndx >= the_file_list->count) | |
657 | continue; | |
658 | ||
659 | file = the_file_list->files[ndx]; | |
660 | if (!file->link_u.links) | |
661 | continue; | |
662 | ||
663 | hard_link_cluster(file, ndx, itemizing, code); | |
664 | } | |
665 | } | |
2f03f956 | 666 | |
48224e4c WD |
667 | /* This is only called for regular files. We return -2 if we've finished |
668 | * handling the file, -1 if no dest-linking occurred, or a non-negative | |
669 | * value if we found an alternate basis file. */ | |
670 | static int try_dests_reg(struct file_struct *file, char *fname, int ndx, | |
671 | char *cmpbuf, STRUCT_STAT *stp, int itemizing, | |
e912bd4d | 672 | int maybe_ATTRS_REPORT, enum logcode code) |
48224e4c WD |
673 | { |
674 | int best_match = -1; | |
675 | int match_level = 0; | |
676 | int j = 0; | |
677 | ||
678 | do { | |
679 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
680 | if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode)) | |
681 | continue; | |
682 | switch (match_level) { | |
683 | case 0: | |
684 | best_match = j; | |
685 | match_level = 1; | |
686 | /* FALL THROUGH */ | |
687 | case 1: | |
688 | if (!unchanged_file(cmpbuf, file, stp)) | |
689 | continue; | |
690 | best_match = j; | |
691 | match_level = 2; | |
692 | /* FALL THROUGH */ | |
693 | case 2: | |
694 | if (!unchanged_attrs(file, stp)) | |
695 | continue; | |
6bfc7b4d | 696 | if (always_checksum && preserve_times |
0f86c74e | 697 | && cmp_time(stp->st_mtime, file->modtime)) |
48224e4c WD |
698 | continue; |
699 | best_match = j; | |
700 | match_level = 3; | |
701 | break; | |
702 | } | |
703 | break; | |
704 | } while (basis_dir[++j] != NULL); | |
705 | ||
706 | if (!match_level) | |
707 | return -1; | |
708 | ||
709 | if (j != best_match) { | |
710 | j = best_match; | |
711 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
712 | if (link_stat(cmpbuf, stp, 0) < 0) | |
f964ac5e | 713 | return -1; |
48224e4c WD |
714 | } |
715 | ||
48224e4c | 716 | if (match_level == 3 && !copy_dest) { |
40410a38 | 717 | #ifdef SUPPORT_HARD_LINKS |
48224e4c | 718 | if (link_dest) { |
116a4769 | 719 | int i = itemizing && (verbose > 1 || stdout_format_has_i > 1); |
48224e4c | 720 | if (hard_link_one(file, ndx, fname, 0, stp, |
116a4769 | 721 | cmpbuf, 1, i, code) < 0) |
48224e4c | 722 | goto try_a_copy; |
d9163a4c WD |
723 | if (preserve_hard_links && file->link_u.links) { |
724 | if (dry_run) | |
725 | file->link_u.links->link_dest_used = j + 1; | |
3447d610 | 726 | hard_link_cluster(file, ndx, itemizing, code); |
d9163a4c | 727 | } |
40410a38 WD |
728 | } else |
729 | #endif | |
730 | if (itemizing) | |
48224e4c | 731 | itemize(file, ndx, 0, stp, 0, 0, NULL); |
e912bd4d | 732 | if (verbose > 1 && maybe_ATTRS_REPORT) { |
1925c344 | 733 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
48224e4c WD |
734 | } |
735 | return -2; | |
736 | } | |
48224e4c WD |
737 | |
738 | if (match_level >= 2) { | |
739 | try_a_copy: /* Copy the file locally. */ | |
740 | if (copy_file(cmpbuf, fname, file->mode) < 0) { | |
741 | if (verbose) { | |
3447d610 | 742 | rsyserr(FINFO, errno, "copy_file %s => %s", |
45c49b52 | 743 | full_fname(cmpbuf), fname); |
48224e4c WD |
744 | } |
745 | return -1; | |
746 | } | |
3447d610 WD |
747 | if (itemizing) |
748 | itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL); | |
e912bd4d WD |
749 | set_file_attrs(fname, file, NULL, 0); |
750 | if (maybe_ATTRS_REPORT | |
48224e4c WD |
751 | && ((!itemizing && verbose && match_level == 2) |
752 | || (verbose > 1 && match_level == 3))) { | |
1925c344 | 753 | code = match_level == 3 ? FCLIENT : FINFO; |
45c49b52 | 754 | rprintf(code, "%s%s\n", fname, |
48224e4c WD |
755 | match_level == 3 ? " is uptodate" : ""); |
756 | } | |
3447d610 WD |
757 | if (preserve_hard_links && file->link_u.links) |
758 | hard_link_cluster(file, ndx, itemizing, code); | |
48224e4c WD |
759 | return -2; |
760 | } | |
761 | ||
762 | return FNAMECMP_BASIS_DIR_LOW + j; | |
763 | } | |
764 | ||
765 | /* This is only called for non-regular files. We return -2 if we've finished | |
116a4769 WD |
766 | * handling the file, or -1 if no dest-linking occurred, or a non-negative |
767 | * value if we found an alternate basis file. */ | |
48224e4c | 768 | static int try_dests_non(struct file_struct *file, char *fname, int ndx, |
116a4769 WD |
769 | char *cmpbuf, STRUCT_STAT *stp, int itemizing, |
770 | int maybe_ATTRS_REPORT, enum logcode code) | |
48224e4c | 771 | { |
116a4769 WD |
772 | char lnk[MAXPATHLEN]; |
773 | int best_match = -1; | |
774 | int match_level = 0; | |
775 | enum nonregtype type; | |
776 | int len, j = 0; | |
777 | ||
778 | #ifndef SUPPORT_LINKS | |
779 | if (S_ISLNK(file->mode)) | |
780 | return -1; | |
781 | #endif | |
782 | if (S_ISDIR(file->mode)) { | |
783 | type = TYPE_DIR; | |
784 | } else if (IS_SPECIAL(file->mode)) | |
785 | type = TYPE_SPECIAL; | |
786 | else if (IS_DEVICE(file->mode)) | |
787 | type = TYPE_DEVICE; | |
788 | #ifdef SUPPORT_LINKS | |
789 | else if (S_ISLNK(file->mode)) | |
790 | type = TYPE_SYMLINK; | |
791 | #endif | |
792 | else { | |
793 | rprintf(FERROR, | |
794 | "internal: try_dests_non() called with invalid mode (%o)\n", | |
795 | (int)file->mode); | |
796 | exit_cleanup(RERR_UNSUPPORTED); | |
797 | } | |
48224e4c WD |
798 | |
799 | do { | |
116a4769 WD |
800 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); |
801 | if (link_stat(cmpbuf, stp, 0) < 0) | |
48224e4c | 802 | continue; |
116a4769 WD |
803 | switch (type) { |
804 | case TYPE_DIR: | |
805 | if (!S_ISDIR(stp->st_mode)) | |
806 | continue; | |
807 | break; | |
808 | case TYPE_SPECIAL: | |
809 | if (!IS_SPECIAL(stp->st_mode)) | |
810 | continue; | |
811 | break; | |
812 | case TYPE_DEVICE: | |
813 | if (!IS_DEVICE(stp->st_mode)) | |
814 | continue; | |
815 | break; | |
bb0d8edf | 816 | #ifdef SUPPORT_LINKS |
116a4769 WD |
817 | case TYPE_SYMLINK: |
818 | if (!S_ISLNK(stp->st_mode)) | |
48224e4c | 819 | continue; |
116a4769 | 820 | break; |
bb0d8edf | 821 | #endif |
116a4769 WD |
822 | } |
823 | if (match_level < 1) { | |
824 | match_level = 1; | |
825 | best_match = j; | |
826 | } | |
827 | switch (type) { | |
828 | case TYPE_DIR: | |
829 | break; | |
830 | case TYPE_SPECIAL: | |
831 | case TYPE_DEVICE: | |
832 | if (stp->st_rdev != file->u.rdev) | |
48224e4c | 833 | continue; |
116a4769 WD |
834 | break; |
835 | #ifdef SUPPORT_LINKS | |
836 | case TYPE_SYMLINK: | |
837 | if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0) | |
ba212fe0 | 838 | continue; |
116a4769 WD |
839 | lnk[len] = '\0'; |
840 | if (strcmp(lnk, file->u.link) != 0) | |
ba212fe0 | 841 | continue; |
116a4769 WD |
842 | break; |
843 | #endif | |
844 | } | |
845 | if (match_level < 2) { | |
846 | match_level = 2; | |
847 | best_match = j; | |
848 | } | |
849 | if (unchanged_attrs(file, stp)) { | |
850 | match_level = 3; | |
851 | best_match = j; | |
852 | break; | |
5f93b4d3 | 853 | } |
116a4769 WD |
854 | } while (basis_dir[++j] != NULL); |
855 | ||
856 | if (!match_level) | |
857 | return -1; | |
858 | ||
859 | if (j != best_match) { | |
860 | j = best_match; | |
861 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
862 | if (link_stat(cmpbuf, stp, 0) < 0) | |
863 | return -1; | |
864 | } | |
865 | ||
866 | if (match_level == 3) { | |
40410a38 | 867 | #ifdef SUPPORT_HARD_LINKS |
ba212fe0 WD |
868 | if (link_dest |
869 | #ifndef CAN_HARDLINK_SYMLINK | |
870 | && !S_ISLNK(file->mode) | |
871 | #endif | |
872 | #ifndef CAN_HARDLINK_SPECIAL | |
43476426 | 873 | && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode) |
ba212fe0 | 874 | #endif |
116a4769 WD |
875 | && !S_ISDIR(file->mode)) { |
876 | if (do_link(cmpbuf, fname) < 0) { | |
1c6e9dfa WD |
877 | rsyserr(FERROR, errno, |
878 | "failed to hard-link %s with %s", | |
116a4769 WD |
879 | cmpbuf, fname); |
880 | return j; | |
48224e4c | 881 | } |
3447d610 WD |
882 | if (preserve_hard_links && file->link_u.links) |
883 | hard_link_cluster(file, ndx, itemizing, code); | |
116a4769 | 884 | } else |
40410a38 | 885 | #endif |
116a4769 WD |
886 | match_level = 2; |
887 | if (itemizing && stdout_format_has_i | |
888 | && (verbose > 1 || stdout_format_has_i > 1)) { | |
889 | int chg = compare_dest && type != TYPE_DIR ? 0 | |
890 | : ITEM_LOCAL_CHANGE | |
891 | + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0); | |
892 | char *lp = match_level == 3 ? "" : NULL; | |
893 | itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp); | |
48224e4c | 894 | } |
e912bd4d | 895 | if (verbose > 1 && maybe_ATTRS_REPORT) { |
116a4769 WD |
896 | rprintf(FCLIENT, "%s%s is uptodate\n", |
897 | fname, type == TYPE_DIR ? "/" : ""); | |
48224e4c WD |
898 | } |
899 | return -2; | |
116a4769 | 900 | } |
48224e4c | 901 | |
116a4769 | 902 | return j; |
48224e4c WD |
903 | } |
904 | ||
ed7e7955 WD |
905 | static int phase = 0; |
906 | ||
ee1d11c4 | 907 | /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir, |
0492fdfb WD |
908 | * make sure it exists, and has the right permissions/timestamp info. For |
909 | * all other non-regular files (symlinks, etc.) we create them here. For | |
910 | * regular files that have changed, we try to find a basis file and then | |
911 | * start sending checksums. | |
ef1aa910 | 912 | * |
0e5665d3 WD |
913 | * When fname is non-null, it must point to a MAXPATHLEN buffer! |
914 | * | |
0492fdfb WD |
915 | * Note that f_out is set to -1 when doing final directory-permission and |
916 | * modification-time repair. */ | |
ee1d11c4 | 917 | static void recv_generator(char *fname, struct file_struct *file, int ndx, |
e912bd4d | 918 | int itemizing, int maybe_ATTRS_REPORT, |
ef20efcb | 919 | enum logcode code, int f_out) |
2cda2560 | 920 | { |
8174bc35 | 921 | static int missing_below = -1, excluded_below = -1; |
a9cc1283 | 922 | static char *parent_dirname = ""; |
8e85be0a | 923 | static struct file_list *fuzzy_dirlist = NULL; |
8470a515 | 924 | static int need_fuzzy_dirlist = 0; |
8e85be0a | 925 | struct file_struct *fuzzy_file = NULL; |
41cfde6b | 926 | int fd = -1, f_copy = -1; |
1f1d368a | 927 | STRUCT_STAT st, real_st, partial_st; |
41cfde6b | 928 | struct file_struct *back_file = NULL; |
1f1d368a | 929 | int statret, real_ret, stat_errno; |
41cfde6b | 930 | char *fnamecmp, *partialptr, *backupptr = NULL; |
375a4556 | 931 | char fnamecmpbuf[MAXPATHLEN]; |
41cfde6b | 932 | uchar fnamecmp_type; |
c6fadc0e | 933 | int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0; |
f7632fc6 | 934 | |
dfd5ba6a WD |
935 | if (list_only) |
936 | return; | |
2f03f956 | 937 | |
8e85be0a WD |
938 | if (!fname) { |
939 | if (fuzzy_dirlist) { | |
8470a515 | 940 | flist_free(fuzzy_dirlist); |
8e85be0a | 941 | fuzzy_dirlist = NULL; |
8e85be0a WD |
942 | } |
943 | if (missing_below >= 0) { | |
caff3322 WD |
944 | if (dry_run) |
945 | dry_run--; | |
8e85be0a WD |
946 | missing_below = -1; |
947 | } | |
8c9e06d7 | 948 | parent_dirname = ""; |
8e85be0a WD |
949 | return; |
950 | } | |
951 | ||
45c49b52 WD |
952 | if (verbose > 2) |
953 | rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx); | |
2f03f956 | 954 | |
8174bc35 WD |
955 | if (server_filter_list.head) { |
956 | if (excluded_below >= 0) { | |
957 | if (file->dir.depth > excluded_below) | |
958 | goto skipping; | |
959 | excluded_below = -1; | |
960 | } | |
961 | if (check_filter(&server_filter_list, fname, | |
962 | S_ISDIR(file->mode)) < 0) { | |
963 | if (S_ISDIR(file->mode)) | |
964 | excluded_below = file->dir.depth; | |
b2e7c913 | 965 | skipping: |
8174bc35 WD |
966 | if (verbose) { |
967 | rprintf(FINFO, | |
968 | "skipping server-excluded file \"%s\"\n", | |
45c49b52 | 969 | fname); |
8174bc35 WD |
970 | } |
971 | return; | |
3e35c34b | 972 | } |
3e35c34b | 973 | } |
97f9dcae | 974 | |
caff3322 WD |
975 | if (missing_below >= 0) { |
976 | if (file->dir.depth <= missing_below) { | |
977 | if (dry_run) | |
978 | dry_run--; | |
979 | missing_below = -1; | |
980 | } else if (!dry_run) | |
981 | return; | |
df337831 | 982 | } |
73f7af0e WD |
983 | if (dry_run > 1) { |
984 | statret = -1; | |
985 | stat_errno = ENOENT; | |
986 | } else { | |
8c9e06d7 WD |
987 | char *dn = file->dirname ? file->dirname : "."; |
988 | if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) { | |
aa37022e | 989 | if (relative_paths && !implied_dirs |
1a7f3d99 | 990 | && do_stat(dn, &st) < 0 |
904e5af1 | 991 | && create_directory_path(fname) < 0) { |
8c9e06d7 WD |
992 | rsyserr(FERROR, errno, |
993 | "recv_generator: mkdir %s failed", | |
994 | full_fname(dn)); | |
8e85be0a | 995 | } |
8c9e06d7 | 996 | if (fuzzy_dirlist) { |
8470a515 | 997 | flist_free(fuzzy_dirlist); |
8c9e06d7 WD |
998 | fuzzy_dirlist = NULL; |
999 | } | |
1000 | if (fuzzy_basis) | |
8470a515 | 1001 | need_fuzzy_dirlist = 1; |
8e85be0a | 1002 | } |
8c9e06d7 | 1003 | parent_dirname = dn; |
8e85be0a | 1004 | |
6f2a222c | 1005 | if (need_fuzzy_dirlist && S_ISREG(file->mode)) { |
865c3b5f | 1006 | fuzzy_dirlist = get_dirlist(dn, -1, 1); |
6f2a222c WD |
1007 | need_fuzzy_dirlist = 0; |
1008 | } | |
865c3b5f | 1009 | |
73f7af0e WD |
1010 | statret = link_stat(fname, &st, |
1011 | keep_dirlinks && S_ISDIR(file->mode)); | |
1012 | stat_errno = errno; | |
1013 | } | |
63787382 | 1014 | |
e90aab49 | 1015 | if (ignore_non_existing && statret == -1 && stat_errno == ENOENT) { |
ecc81fce | 1016 | if (verbose > 1) { |
15164c0a WD |
1017 | rprintf(FINFO, "not creating new %s \"%s\"\n", |
1018 | S_ISDIR(file->mode) ? "directory" : "file", | |
45c49b52 | 1019 | fname); |
ecc81fce | 1020 | } |
1347d512 AT |
1021 | return; |
1022 | } | |
1023 | ||
a9d6e6fc WD |
1024 | /* If we're not preserving permissions, change the file-list's |
1025 | * mode based on the local permissions and some heuristics. */ | |
1026 | if (!preserve_perms) { | |
1027 | int exists = statret == 0 | |
1028 | && S_ISDIR(st.st_mode) == S_ISDIR(file->mode); | |
1029 | file->mode = dest_mode(file->mode, st.st_mode, exists); | |
4df9f368 AT |
1030 | } |
1031 | ||
2f03f956 | 1032 | if (S_ISDIR(file->mode)) { |
2cda2560 WD |
1033 | /* The file to be received is a directory, so we need |
1034 | * to prepare appropriately. If there is already a | |
1035 | * file of that name and it is *not* a directory, then | |
1036 | * we need to delete it. If it doesn't exist, then | |
027428eb | 1037 | * (perhaps recursively) create it. */ |
2f03f956 | 1038 | if (statret == 0 && !S_ISDIR(st.st_mode)) { |
c6fadc0e | 1039 | if (delete_item(fname, st.st_mode, "directory", del_opts) != 0) |
1f1d368a | 1040 | return; |
2f03f956 AT |
1041 | statret = -1; |
1042 | } | |
df337831 WD |
1043 | if (dry_run && statret != 0 && missing_below < 0) { |
1044 | missing_below = file->dir.depth; | |
1045 | dry_run++; | |
1046 | } | |
b467495c WD |
1047 | real_ret = statret; |
1048 | real_st = st; | |
116a4769 WD |
1049 | if (new_root_dir) { |
1050 | if (*fname == '.' && fname[1] == '\0') | |
b467495c | 1051 | statret = -1; |
116a4769 WD |
1052 | new_root_dir = 0; |
1053 | } | |
b467495c | 1054 | if (statret != 0 && basis_dir[0] != NULL) { |
116a4769 WD |
1055 | int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st, |
1056 | itemizing, maybe_ATTRS_REPORT, code); | |
1057 | if (j == -2) { | |
1058 | itemizing = 0; | |
1059 | code = FNONE; | |
1060 | } else if (j >= 0) | |
b467495c | 1061 | statret = 1; |
116a4769 | 1062 | } |
ee1d11c4 | 1063 | if (itemizing && f_out != -1) { |
b467495c WD |
1064 | itemize(file, ndx, statret, &st, |
1065 | statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL); | |
ee1d11c4 | 1066 | } |
b467495c | 1067 | if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) { |
027428eb | 1068 | if (!relative_paths || errno != ENOENT |
904e5af1 | 1069 | || create_directory_path(fname) < 0 |
09290693 | 1070 | || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) { |
d62bcc17 WD |
1071 | rsyserr(FERROR, errno, |
1072 | "recv_generator: mkdir %s failed", | |
1073 | full_fname(fname)); | |
caff3322 WD |
1074 | file->flags |= FLAG_MISSING; |
1075 | if (ndx+1 < the_file_list->count | |
1076 | && the_file_list->files[ndx+1]->dir.depth > file->dir.depth) { | |
1077 | rprintf(FERROR, | |
1078 | "*** Skipping everything below this failed directory ***\n"); | |
1079 | missing_below = file->dir.depth; | |
1080 | } | |
1081 | return; | |
2f03f956 AT |
1082 | } |
1083 | } | |
b467495c | 1084 | if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0) |
f4164b73 | 1085 | && verbose && code != FNONE && f_out != -1) |
45c49b52 | 1086 | rprintf(code, "%s/\n", fname); |
b467495c | 1087 | if (real_ret != 0 && one_file_system) |
b2e4811d | 1088 | real_st.st_dev = filesystem_dev; |
f75a53e7 | 1089 | if (delete_during && f_out != -1 && !phase && dry_run < 2 |
31937d36 | 1090 | && (file->flags & FLAG_DEL_HERE)) |
b2e4811d | 1091 | delete_in_dir(the_file_list, fname, file, &real_st); |
2f03f956 | 1092 | return; |
06a1dbad | 1093 | } |
6c3862fa | 1094 | |
273a7ed5 WD |
1095 | if (preserve_hard_links && file->link_u.links |
1096 | && hard_link_check(file, ndx, fname, statret, &st, | |
1097 | itemizing, code, HL_CHECK_MASTER)) | |
1098 | return; | |
1099 | ||
2f03f956 | 1100 | if (preserve_links && S_ISLNK(file->mode)) { |
4f5b0756 | 1101 | #ifdef SUPPORT_LINKS |
728d0922 | 1102 | if (safe_symlinks && unsafe_symlink(file->u.link, fname)) { |
2f03f956 | 1103 | if (verbose) { |
2a5d5a8c | 1104 | if (the_file_list->count == 1) |
6cbde57d | 1105 | fname = f_name(file, NULL); |
4875d6b6 WD |
1106 | rprintf(FINFO, |
1107 | "ignoring unsafe symlink %s -> \"%s\"\n", | |
45c49b52 | 1108 | full_fname(fname), file->u.link); |
2f03f956 AT |
1109 | } |
1110 | return; | |
1111 | } | |
1112 | if (statret == 0) { | |
7e38410e WD |
1113 | char lnk[MAXPATHLEN]; |
1114 | int len; | |
1115 | ||
116a4769 WD |
1116 | if (!S_ISLNK(st.st_mode)) |
1117 | statret = -1; | |
1118 | else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0 | |
1119 | && strncmp(lnk, file->u.link, len) == 0 | |
1120 | && file->u.link[len] == '\0') { | |
1121 | /* The link is pointing to the right place. */ | |
1122 | if (itemizing) | |
1123 | itemize(file, ndx, 0, &st, 0, 0, NULL); | |
1124 | set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); | |
1125 | if (preserve_hard_links && file->link_u.links) | |
1126 | hard_link_cluster(file, ndx, itemizing, code); | |
1127 | if (remove_source_files == 1) | |
1128 | goto return_with_success; | |
1129 | return; | |
2cda2560 | 1130 | } |
7e38410e WD |
1131 | /* Not the right symlink (or not a symlink), so |
1132 | * delete it. */ | |
c6fadc0e | 1133 | if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0) |
1f1d368a | 1134 | return; |
1c6e9dfa | 1135 | } else if (basis_dir[0] != NULL) { |
116a4769 WD |
1136 | int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st, |
1137 | itemizing, maybe_ATTRS_REPORT, code); | |
1138 | if (j == -2) { | |
ba212fe0 WD |
1139 | #ifndef CAN_HARDLINK_SYMLINK |
1140 | if (link_dest) { | |
1141 | /* Resort to --copy-dest behavior. */ | |
1142 | } else | |
1143 | #endif | |
48224e4c WD |
1144 | if (!copy_dest) |
1145 | return; | |
f4164b73 WD |
1146 | itemizing = 0; |
1147 | code = FNONE; | |
116a4769 WD |
1148 | } else if (j >= 0) |
1149 | statret = 1; | |
2f03f956 | 1150 | } |
273a7ed5 WD |
1151 | if (preserve_hard_links && file->link_u.links |
1152 | && hard_link_check(file, ndx, fname, -1, &st, | |
1153 | itemizing, code, HL_SKIP)) | |
1154 | return; | |
116a4769 | 1155 | if (do_symlink(file->u.link, fname) != 0) { |
d62bcc17 | 1156 | rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed", |
45c49b52 | 1157 | full_fname(fname), file->u.link); |
2f03f956 | 1158 | } else { |
e912bd4d | 1159 | set_file_attrs(fname, file, NULL, 0); |
6c3862fa | 1160 | if (itemizing) { |
ee1d11c4 | 1161 | itemize(file, ndx, statret, &st, |
ef20efcb | 1162 | ITEM_LOCAL_CHANGE, 0, NULL); |
6c3862fa | 1163 | } |
116a4769 WD |
1164 | if (code != FNONE && verbose) |
1165 | rprintf(code, "%s -> %s\n", fname, file->u.link); | |
273a7ed5 WD |
1166 | if (preserve_hard_links && file->link_u.links) |
1167 | hard_link_cluster(file, ndx, itemizing, code); | |
f964ac5e WD |
1168 | /* This does not check remove_source_files == 1 |
1169 | * because this is one of the items that the old | |
1170 | * --remove-sent-files option would remove. */ | |
47c11975 | 1171 | if (remove_source_files) |
04cd8789 | 1172 | goto return_with_success; |
2f03f956 AT |
1173 | } |
1174 | #endif | |
1175 | return; | |
1176 | } | |
1177 | ||
b5c6a6ae WD |
1178 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
1179 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
116a4769 | 1180 | if (statret == 0) { |
c6fadc0e WD |
1181 | char *t; |
1182 | if (IS_DEVICE(file->mode)) { | |
1183 | if (!IS_DEVICE(st.st_mode)) | |
1184 | statret = -1; | |
1185 | t = "device file"; | |
1186 | } else { | |
1187 | if (!IS_SPECIAL(st.st_mode)) | |
1188 | statret = -1; | |
1189 | t = "special file"; | |
1190 | } | |
1191 | if (statret == 0 | |
1192 | && (st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS) | |
1193 | && st.st_rdev == file->u.rdev) { | |
116a4769 WD |
1194 | /* The device or special file is identical. */ |
1195 | if (itemizing) | |
1196 | itemize(file, ndx, 0, &st, 0, 0, NULL); | |
1197 | set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); | |
1198 | if (preserve_hard_links && file->link_u.links) | |
1199 | hard_link_cluster(file, ndx, itemizing, code); | |
1200 | if (remove_source_files == 1) | |
1201 | goto return_with_success; | |
1202 | return; | |
1203 | } | |
c6fadc0e | 1204 | if (delete_item(fname, st.st_mode, t, del_opts) != 0) |
116a4769 WD |
1205 | return; |
1206 | } else if (basis_dir[0] != NULL) { | |
1207 | int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st, | |
1208 | itemizing, maybe_ATTRS_REPORT, code); | |
1209 | if (j == -2) { | |
ba212fe0 WD |
1210 | #ifndef CAN_HARDLINK_SPECIAL |
1211 | if (link_dest) { | |
1212 | /* Resort to --copy-dest behavior. */ | |
1213 | } else | |
1214 | #endif | |
48224e4c WD |
1215 | if (!copy_dest) |
1216 | return; | |
f4164b73 WD |
1217 | itemizing = 0; |
1218 | code = FNONE; | |
116a4769 WD |
1219 | } else if (j >= 0) |
1220 | statret = 1; | |
48224e4c | 1221 | } |
116a4769 WD |
1222 | if (preserve_hard_links && file->link_u.links |
1223 | && hard_link_check(file, ndx, fname, -1, &st, | |
1224 | itemizing, code, HL_SKIP)) | |
1225 | return; | |
1226 | if (verbose > 2) { | |
1227 | rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", | |
1228 | fname, (int)file->mode, (int)file->u.rdev); | |
1229 | } | |
1230 | if (do_mknod(fname, file->mode, file->u.rdev) < 0) { | |
1231 | rsyserr(FERROR, errno, "mknod %s failed", | |
1232 | full_fname(fname)); | |
2f03f956 | 1233 | } else { |
116a4769 WD |
1234 | set_file_attrs(fname, file, NULL, 0); |
1235 | if (itemizing) { | |
1236 | itemize(file, ndx, statret, &st, | |
1237 | ITEM_LOCAL_CHANGE, 0, NULL); | |
1238 | } | |
1239 | if (code != FNONE && verbose) | |
1240 | rprintf(code, "%s\n", fname); | |
273a7ed5 WD |
1241 | if (preserve_hard_links && file->link_u.links) |
1242 | hard_link_cluster(file, ndx, itemizing, code); | |
47c11975 | 1243 | if (remove_source_files == 1) |
04cd8789 | 1244 | goto return_with_success; |
2f03f956 AT |
1245 | } |
1246 | return; | |
1247 | } | |
2f03f956 | 1248 | |
2f03f956 | 1249 | if (!S_ISREG(file->mode)) { |
2a5d5a8c | 1250 | if (the_file_list->count == 1) |
6cbde57d | 1251 | fname = f_name(file, NULL); |
45c49b52 | 1252 | rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname); |
2f03f956 AT |
1253 | return; |
1254 | } | |
1255 | ||
289a3216 WD |
1256 | if (max_size && file->length > max_size) { |
1257 | if (verbose > 1) { | |
1258 | if (the_file_list->count == 1) | |
6cbde57d | 1259 | fname = f_name(file, NULL); |
45c49b52 | 1260 | rprintf(FINFO, "%s is over max-size\n", fname); |
289a3216 WD |
1261 | } |
1262 | return; | |
1263 | } | |
02b5cb23 WD |
1264 | if (min_size && file->length < min_size) { |
1265 | if (verbose > 1) { | |
1266 | if (the_file_list->count == 1) | |
6cbde57d | 1267 | fname = f_name(file, NULL); |
45c49b52 | 1268 | rprintf(FINFO, "%s is under min-size\n", fname); |
02b5cb23 WD |
1269 | } |
1270 | return; | |
1271 | } | |
289a3216 | 1272 | |
e90aab49 | 1273 | if (ignore_existing && statret == 0) { |
c3cbcfb8 | 1274 | if (verbose > 1) |
45c49b52 | 1275 | rprintf(FINFO, "%s exists\n", fname); |
c3cbcfb8 WD |
1276 | return; |
1277 | } | |
1278 | ||
1279 | if (update_only && statret == 0 | |
0f86c74e | 1280 | && cmp_time(st.st_mtime, file->modtime) > 0) { |
c3cbcfb8 | 1281 | if (verbose > 1) |
45c49b52 | 1282 | rprintf(FINFO, "%s is newer\n", fname); |
c3cbcfb8 WD |
1283 | return; |
1284 | } | |
1285 | ||
375a4556 | 1286 | fnamecmp = fname; |
41cfde6b | 1287 | fnamecmp_type = FNAMECMP_FNAME; |
375a4556 | 1288 | |
1f1d368a | 1289 | if (statret == 0 && !S_ISREG(st.st_mode)) { |
c6fadc0e | 1290 | if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0) |
1f1d368a WD |
1291 | return; |
1292 | statret = -1; | |
1293 | stat_errno = ENOENT; | |
1294 | } | |
1295 | ||
06a1dbad | 1296 | if (statret != 0 && basis_dir[0] != NULL) { |
48224e4c | 1297 | int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st, |
e912bd4d | 1298 | itemizing, maybe_ATTRS_REPORT, code); |
04cd8789 | 1299 | if (j == -2) { |
47c11975 | 1300 | if (remove_source_files == 1) |
04cd8789 | 1301 | goto return_with_success; |
48224e4c | 1302 | return; |
04cd8789 | 1303 | } |
b06050f9 | 1304 | if (j >= 0) { |
48224e4c WD |
1305 | fnamecmp = fnamecmpbuf; |
1306 | fnamecmp_type = j; | |
9ba46343 | 1307 | statret = 0; |
e7d13fe5 WD |
1308 | } |
1309 | } | |
1310 | ||
1f1d368a WD |
1311 | real_ret = statret; |
1312 | real_st = st; | |
375a4556 | 1313 | |
9d954dca | 1314 | if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL |
72c19bb3 WD |
1315 | && link_stat(partialptr, &partial_st, 0) == 0 |
1316 | && S_ISREG(partial_st.st_mode)) { | |
06a1dbad | 1317 | if (statret != 0) |
72c19bb3 WD |
1318 | goto prepare_to_open; |
1319 | } else | |
1320 | partialptr = NULL; | |
89f7eff3 | 1321 | |
ccb16b46 | 1322 | if (statret != 0 && fuzzy_dirlist && dry_run <= 1) { |
8e85be0a WD |
1323 | int j = find_fuzzy(file, fuzzy_dirlist); |
1324 | if (j >= 0) { | |
1325 | fuzzy_file = fuzzy_dirlist->files[j]; | |
6cbde57d | 1326 | f_name(fuzzy_file, fnamecmpbuf); |
8e85be0a WD |
1327 | if (verbose > 2) { |
1328 | rprintf(FINFO, "fuzzy basis selected for %s: %s\n", | |
45c49b52 | 1329 | fname, fnamecmpbuf); |
8e85be0a | 1330 | } |
8e85be0a | 1331 | st.st_size = fuzzy_file->length; |
8e85be0a WD |
1332 | statret = 0; |
1333 | fnamecmp = fnamecmpbuf; | |
1334 | fnamecmp_type = FNAMECMP_FUZZY; | |
1335 | } | |
1336 | } | |
1337 | ||
06a1dbad | 1338 | if (statret != 0) { |
273a7ed5 | 1339 | if (preserve_hard_links && file->link_u.links |
d8169e6f WD |
1340 | && hard_link_check(file, ndx, fname, statret, &st, |
1341 | itemizing, code, HL_SKIP)) | |
6dff5992 | 1342 | return; |
41cfde6b WD |
1343 | if (stat_errno == ENOENT) |
1344 | goto notify_others; | |
991daf00 WD |
1345 | rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s", |
1346 | full_fname(fname)); | |
2f03f956 AT |
1347 | return; |
1348 | } | |
1349 | ||
6cc11982 WD |
1350 | if (append_mode && st.st_size > file->length) |
1351 | return; | |
1352 | ||
48224e4c | 1353 | if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH) |
b7e8628c | 1354 | ; |
8e85be0a WD |
1355 | else if (fnamecmp_type == FNAMECMP_FUZZY) |
1356 | ; | |
b7e8628c | 1357 | else if (unchanged_file(fnamecmp, file, &st)) { |
d8b108c2 WD |
1358 | if (partialptr) { |
1359 | do_unlink(partialptr); | |
1360 | handle_partial_dir(partialptr, PDIR_DELETE); | |
1361 | } | |
48224e4c WD |
1362 | if (itemizing) { |
1363 | itemize(file, ndx, real_ret, &real_st, | |
1364 | 0, 0, NULL); | |
a1d23b53 | 1365 | } |
e912bd4d | 1366 | set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT); |
48224e4c WD |
1367 | if (preserve_hard_links && file->link_u.links) |
1368 | hard_link_cluster(file, ndx, itemizing, code); | |
47c11975 | 1369 | if (remove_source_files != 1) |
04cd8789 WD |
1370 | return; |
1371 | return_with_success: | |
1372 | if (!dry_run) { | |
1373 | char numbuf[4]; | |
1374 | SIVAL(numbuf, 0, ndx); | |
1375 | send_msg(MSG_SUCCESS, numbuf, 4); | |
1376 | } | |
967866d4 | 1377 | return; |
2f03f956 AT |
1378 | } |
1379 | ||
b2e7c913 | 1380 | prepare_to_open: |
9d954dca WD |
1381 | if (partialptr) { |
1382 | st = partial_st; | |
1383 | fnamecmp = partialptr; | |
1384 | fnamecmp_type = FNAMECMP_PARTIAL_DIR; | |
1385 | statret = 0; | |
1386 | } | |
1387 | ||
beb51aa0 | 1388 | if (!do_xfers || read_batch || whole_file) |
3841a04e | 1389 | goto notify_others; |
2f03f956 | 1390 | |
ccb16b46 | 1391 | if (fuzzy_dirlist) { |
8e85be0a WD |
1392 | int j = flist_find(fuzzy_dirlist, file); |
1393 | if (j >= 0) /* don't use changing file as future fuzzy basis */ | |
1394 | fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY; | |
1395 | } | |
1396 | ||
2cda2560 | 1397 | /* open the file */ |
8c9fd200 | 1398 | fd = do_open(fnamecmp, O_RDONLY, 0); |
2f03f956 AT |
1399 | |
1400 | if (fd == -1) { | |
d62bcc17 WD |
1401 | rsyserr(FERROR, errno, "failed to open %s, continuing", |
1402 | full_fname(fnamecmp)); | |
b2e7c913 | 1403 | pretend_missing: |
60be6acf | 1404 | /* pretend the file didn't exist */ |
273a7ed5 | 1405 | if (preserve_hard_links && file->link_u.links |
d8169e6f WD |
1406 | && hard_link_check(file, ndx, fname, statret, &st, |
1407 | itemizing, code, HL_SKIP)) | |
6dff5992 | 1408 | return; |
1f1d368a | 1409 | statret = real_ret = -1; |
41cfde6b | 1410 | goto notify_others; |
2f03f956 AT |
1411 | } |
1412 | ||
ef20efcb | 1413 | if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) { |
cd6aa5b5 WD |
1414 | if (!(backupptr = get_backup_name(fname))) { |
1415 | close(fd); | |
1416 | return; | |
1417 | } | |
3de73827 | 1418 | if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) { |
cd6aa5b5 WD |
1419 | close(fd); |
1420 | goto pretend_missing; | |
1421 | } | |
1422 | if (robust_unlink(backupptr) && errno != ENOENT) { | |
1423 | rsyserr(FERROR, errno, "unlink %s", | |
1424 | full_fname(backupptr)); | |
1425 | free(back_file); | |
1426 | close(fd); | |
1427 | return; | |
1428 | } | |
1429 | if ((f_copy = do_open(backupptr, | |
1430 | O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) { | |
1431 | rsyserr(FERROR, errno, "open %s", | |
1432 | full_fname(backupptr)); | |
1433 | free(back_file); | |
1434 | close(fd); | |
1435 | return; | |
1436 | } | |
41cfde6b | 1437 | fnamecmp_type = FNAMECMP_BACKUP; |
cd6aa5b5 WD |
1438 | } |
1439 | ||
dfd5ba6a | 1440 | if (verbose > 3) { |
ecc81fce | 1441 | rprintf(FINFO, "gen mapped %s of size %.0f\n", |
45c49b52 | 1442 | fnamecmp, (double)st.st_size); |
dfd5ba6a | 1443 | } |
2f03f956 | 1444 | |
2f03f956 | 1445 | if (verbose > 2) |
0492fdfb | 1446 | rprintf(FINFO, "generating and sending sums for %d\n", ndx); |
2f03f956 | 1447 | |
b2e7c913 | 1448 | notify_others: |
47c11975 | 1449 | if (remove_source_files && !delay_updates && !phase) |
04cd8789 | 1450 | increment_active_files(ndx, itemizing, code); |
0492fdfb | 1451 | write_int(f_out, ndx); |
6c3862fa | 1452 | if (itemizing) { |
ee1d11c4 | 1453 | int iflags = ITEM_TRANSFER; |
8237f930 | 1454 | if (always_checksum) |
a1d23b53 | 1455 | iflags |= ITEM_REPORT_CHECKSUM; |
352963dd | 1456 | if (fnamecmp_type != FNAMECMP_FNAME) |
ef20efcb WD |
1457 | iflags |= ITEM_BASIS_TYPE_FOLLOWS; |
1458 | if (fnamecmp_type == FNAMECMP_FUZZY) | |
1459 | iflags |= ITEM_XNAME_FOLLOWS; | |
1f1d368a | 1460 | itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type, |
ef20efcb | 1461 | fuzzy_file ? fuzzy_file->basename : NULL); |
8e85be0a | 1462 | } |
cd6aa5b5 | 1463 | |
beb51aa0 | 1464 | if (!do_xfers) { |
ee1d11c4 WD |
1465 | if (preserve_hard_links && file->link_u.links) |
1466 | hard_link_cluster(file, ndx, itemizing, code); | |
1467 | return; | |
1468 | } | |
1469 | if (read_batch) | |
41cfde6b WD |
1470 | return; |
1471 | ||
33ab4ad8 | 1472 | if (statret != 0 || whole_file) { |
e86ae6bc WD |
1473 | write_sum_head(f_out, NULL); |
1474 | return; | |
1475 | } | |
1476 | ||
1477 | generate_and_send_sums(fd, st.st_size, f_out, f_copy); | |
1478 | ||
1479 | if (f_copy >= 0) { | |
1480 | close(f_copy); | |
e912bd4d | 1481 | set_file_attrs(backupptr, back_file, NULL, 0); |
e86ae6bc WD |
1482 | if (verbose > 1) { |
1483 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 1484 | fname, backupptr); |
41cfde6b | 1485 | } |
e86ae6bc WD |
1486 | free(back_file); |
1487 | } | |
41cfde6b | 1488 | |
e86ae6bc | 1489 | close(fd); |
2f03f956 AT |
1490 | } |
1491 | ||
ef20efcb | 1492 | void generate_files(int f_out, struct file_list *flist, char *local_name) |
2f03f956 | 1493 | { |
ac40b747 | 1494 | int i; |
968c8030 | 1495 | char fbuf[MAXPATHLEN]; |
e912bd4d | 1496 | int itemizing, maybe_ATTRS_REPORT; |
7433d73a | 1497 | enum logcode code; |
ac40b747 | 1498 | int lull_mod = allowed_lull * 5; |
3ea9bbd6 WD |
1499 | int need_retouch_dir_times = preserve_times && !omit_dir_times; |
1500 | int need_retouch_dir_perms = 0; | |
e90aab49 WD |
1501 | int save_ignore_existing = ignore_existing; |
1502 | int save_ignore_non_existing = ignore_non_existing; | |
083acd49 | 1503 | int save_do_progress = do_progress; |
cd908ef4 | 1504 | int save_make_backups = make_backups; |
88a16c8b | 1505 | int dir_tweaking = !(list_only || local_name || dry_run); |
ee1d11c4 | 1506 | |
7433d73a WD |
1507 | if (protocol_version >= 29) { |
1508 | itemizing = 1; | |
17bda2d1 | 1509 | maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT; |
f4164b73 | 1510 | code = logfile_format_has_i ? FNONE : FLOG; |
7433d73a | 1511 | } else if (am_daemon) { |
ecc7623e | 1512 | itemizing = logfile_format_has_i && do_xfers; |
e912bd4d | 1513 | maybe_ATTRS_REPORT = ATTRS_REPORT; |
beb51aa0 | 1514 | code = itemizing || !do_xfers ? FCLIENT : FINFO; |
7433d73a | 1515 | } else if (!am_server) { |
17bda2d1 WD |
1516 | itemizing = stdout_format_has_i; |
1517 | maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT; | |
f4164b73 | 1518 | code = itemizing ? FNONE : FINFO; |
7433d73a WD |
1519 | } else { |
1520 | itemizing = 0; | |
e912bd4d | 1521 | maybe_ATTRS_REPORT = ATTRS_REPORT; |
7433d73a WD |
1522 | code = FINFO; |
1523 | } | |
2f03f956 | 1524 | |
45e08edb WD |
1525 | if (verbose > 2) { |
1526 | rprintf(FINFO, "generator starting pid=%ld count=%d\n", | |
1527 | (long)getpid(), flist->count); | |
1528 | } | |
2f03f956 | 1529 | |
59faec8b | 1530 | if (delete_before && !local_name && flist->count > 0) |
ee1d11c4 | 1531 | do_delete_pass(flist); |
083acd49 | 1532 | do_progress = 0; |
59faec8b | 1533 | |
6cc11982 | 1534 | if (append_mode || whole_file < 0) |
33ab4ad8 | 1535 | whole_file = 0; |
3e7053ac | 1536 | if (verbose >= 2) { |
1b1fef20 | 1537 | rprintf(FINFO, "delta-transmission %s\n", |
33ab4ad8 | 1538 | whole_file |
1b1fef20 WD |
1539 | ? "disabled for local transfer or --whole-file" |
1540 | : "enabled"); | |
3e7053ac | 1541 | } |
2cda2560 | 1542 | |
513fd04d WD |
1543 | /* Since we often fill up the outgoing socket and then just sit around |
1544 | * waiting for the other 2 processes to do their thing, we don't want | |
1545 | * to exit on a timeout. If the data stops flowing, the receiver will | |
1546 | * notice that and let us know via the redo pipe (or its closing). */ | |
1547 | ignore_timeout = 1; | |
a57873b7 | 1548 | |
2f03f956 AT |
1549 | for (i = 0; i < flist->count; i++) { |
1550 | struct file_struct *file = flist->files[i]; | |
2f03f956 | 1551 | |
dfd5ba6a WD |
1552 | if (!file->basename) |
1553 | continue; | |
0492fdfb | 1554 | |
0e5665d3 WD |
1555 | if (local_name) |
1556 | strlcpy(fbuf, local_name, sizeof fbuf); | |
1557 | else | |
6cbde57d | 1558 | f_name(file, fbuf); |
e912bd4d | 1559 | recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT, |
0e5665d3 | 1560 | code, f_out); |
1f7e29b9 | 1561 | |
0492fdfb WD |
1562 | /* We need to ensure that any dirs we create have writeable |
1563 | * permissions during the time we are putting files within | |
1564 | * them. This is then fixed after the transfer is done. */ | |
00b96184 | 1565 | #ifdef HAVE_CHMOD |
41b84ce0 | 1566 | if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR) |
88a16c8b | 1567 | && dir_tweaking) { |
e912bd4d | 1568 | mode_t mode = file->mode | S_IWUSR; /* user write */ |
1f7e29b9 | 1569 | char *fname = local_name ? local_name : fbuf; |
00b96184 | 1570 | if (do_chmod(fname, mode) < 0) { |
1f7e29b9 WD |
1571 | rsyserr(FERROR, errno, |
1572 | "failed to modify permissions on %s", | |
1573 | full_fname(fname)); | |
1574 | } | |
3ea9bbd6 | 1575 | need_retouch_dir_perms = 1; |
2f03f956 | 1576 | } |
00b96184 | 1577 | #endif |
2f03f956 | 1578 | |
ee1d11c4 WD |
1579 | if (preserve_hard_links) |
1580 | check_for_finished_hlinks(itemizing, code); | |
9ac2395b | 1581 | |
18a11cfd | 1582 | if (allowed_lull && !(i % lull_mod)) |
ee1d11c4 | 1583 | maybe_send_keepalive(); |
c2523a05 | 1584 | else if (!(i % 200)) |
417099fa | 1585 | maybe_flush_socket(); |
2f03f956 | 1586 | } |
ef20efcb | 1587 | recv_generator(NULL, NULL, 0, 0, 0, code, -1); |
c93fad5e | 1588 | if (delete_during) |
0e82af2d | 1589 | delete_in_dir(NULL, NULL, NULL, NULL); |
2f03f956 AT |
1590 | |
1591 | phase++; | |
1592 | csum_length = SUM_LENGTH; | |
02b5cb23 | 1593 | max_size = min_size = ignore_existing = ignore_non_existing = 0; |
0492fdfb | 1594 | update_only = always_checksum = size_only = 0; |
e1f67417 | 1595 | ignore_times = 1; |
6cc11982 WD |
1596 | if (append_mode) /* resend w/o append mode */ |
1597 | append_mode = -1; /* ... but only longer files */ | |
e6bc6f42 | 1598 | make_backups = 0; /* avoid a duplicate backup for inplace processing */ |
2f03f956 AT |
1599 | |
1600 | if (verbose > 2) | |
1601 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
1602 | ||
7daccb8e | 1603 | write_int(f_out, -1); |
2f03f956 | 1604 | |
bc63ae3f S |
1605 | /* files can cycle through the system more than once |
1606 | * to catch initial checksum errors */ | |
ee1d11c4 | 1607 | while ((i = get_redo_num(itemizing, code)) != -1) { |
bc63ae3f | 1608 | struct file_struct *file = flist->files[i]; |
0e5665d3 WD |
1609 | if (local_name) |
1610 | strlcpy(fbuf, local_name, sizeof fbuf); | |
1611 | else | |
6cbde57d | 1612 | f_name(file, fbuf); |
e912bd4d | 1613 | recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT, |
0e5665d3 | 1614 | code, f_out); |
bc63ae3f | 1615 | } |
2f03f956 | 1616 | |
bc63ae3f | 1617 | phase++; |
e90aab49 WD |
1618 | ignore_non_existing = save_ignore_non_existing; |
1619 | ignore_existing = save_ignore_existing; | |
cd908ef4 | 1620 | make_backups = save_make_backups; |
0492fdfb | 1621 | |
bc63ae3f S |
1622 | if (verbose > 2) |
1623 | rprintf(FINFO,"generate_files phase=%d\n",phase); | |
2f03f956 | 1624 | |
7daccb8e | 1625 | write_int(f_out, -1); |
70352269 WD |
1626 | /* Reduce round-trip lag-time for a useless delay-updates phase. */ |
1627 | if (protocol_version >= 29 && !delay_updates) | |
1628 | write_int(f_out, -1); | |
6dff5992 | 1629 | |
0438f100 | 1630 | /* Read MSG_DONE for the redo phase (and any prior messages). */ |
ee1d11c4 | 1631 | get_redo_num(itemizing, code); |
6dff5992 | 1632 | |
0438f100 WD |
1633 | if (protocol_version >= 29) { |
1634 | phase++; | |
1635 | if (verbose > 2) | |
1636 | rprintf(FINFO, "generate_files phase=%d\n", phase); | |
70352269 WD |
1637 | if (delay_updates) |
1638 | write_int(f_out, -1); | |
1639 | /* Read MSG_DONE for delay-updates phase & prior messages. */ | |
0438f100 WD |
1640 | get_redo_num(itemizing, code); |
1641 | } | |
1642 | ||
083acd49 | 1643 | do_progress = save_do_progress; |
59faec8b | 1644 | if (delete_after && !local_name && flist->count > 0) |
ee1d11c4 | 1645 | do_delete_pass(flist); |
59faec8b | 1646 | |
88a16c8b | 1647 | if ((need_retouch_dir_perms || need_retouch_dir_times) && dir_tweaking) { |
18a11cfd | 1648 | int j = 0; |
3ea9bbd6 WD |
1649 | /* Now we need to fix any directory permissions that were |
1650 | * modified during the transfer and/or re-set any tweaked | |
1651 | * modified-time values. */ | |
1652 | for (i = 0; i < flist->count; i++) { | |
1653 | struct file_struct *file = flist->files[i]; | |
b435d717 | 1654 | |
3ea9bbd6 WD |
1655 | if (!file->basename || !S_ISDIR(file->mode)) |
1656 | continue; | |
1657 | if (!need_retouch_dir_times && file->mode & S_IWUSR) | |
1658 | continue; | |
caff3322 WD |
1659 | if (file->flags & FLAG_MISSING) { |
1660 | int missing = file->dir.depth; | |
1661 | while (++i < flist->count) { | |
1662 | file = flist->files[i]; | |
1663 | if (file->dir.depth <= missing) | |
1664 | break; | |
1665 | } | |
1666 | i--; | |
1667 | continue; | |
1668 | } | |
6cbde57d | 1669 | recv_generator(f_name(file, NULL), file, i, itemizing, |
e912bd4d | 1670 | maybe_ATTRS_REPORT, code, -1); |
417099fa | 1671 | if (allowed_lull && !(++j % lull_mod)) |
ee1d11c4 | 1672 | maybe_send_keepalive(); |
c2523a05 | 1673 | else if (!(j % 200)) |
417099fa | 1674 | maybe_flush_socket(); |
3ea9bbd6 | 1675 | } |
6dff5992 | 1676 | } |
ef20efcb | 1677 | recv_generator(NULL, NULL, 0, 0, 0, code, -1); |
6dff5992 | 1678 | |
e5e85283 | 1679 | if (max_delete >= 0 && deletion_count > max_delete) { |
f75a53e7 WD |
1680 | rprintf(FINFO, |
1681 | "Deletions stopped due to --max-delete limit (%d skipped)\n", | |
1682 | deletion_count - max_delete); | |
1683 | io_error |= IOERR_DEL_LIMIT; | |
1684 | } | |
1685 | ||
6dff5992 WD |
1686 | if (verbose > 2) |
1687 | rprintf(FINFO,"generate_files finished\n"); | |
2f03f956 | 1688 | } |