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