Commit | Line | Data |
---|---|---|
7a2fd68b | 1 | /* |
0f78b815 WD |
2 | * Routines common to more than one of the rsync processes. |
3 | * | |
4 | * Copyright (C) 1996 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
ba2133d6 | 6 | * Copyright (C) 2003-2007 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 3 of the License, or | |
11 | * (at your option) any later version. | |
0f78b815 WD |
12 | * |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
e7c67065 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
c627d613 | 21 | |
2f03f956 | 22 | #include "rsync.h" |
ceccbacc WD |
23 | #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
24 | #include <libcharset.h> | |
25 | #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO | |
26 | #include <langinfo.h> | |
27 | #endif | |
43a481dc | 28 | |
c627d613 | 29 | extern int verbose; |
c627d613 | 30 | extern int dry_run; |
1c3344a1 | 31 | extern int preserve_acls; |
16edf865 | 32 | extern int preserve_xattrs; |
9b499e95 | 33 | extern int preserve_perms; |
81284832 | 34 | extern int preserve_executability; |
2f03f956 | 35 | extern int preserve_times; |
7b8356d0 | 36 | extern int am_root; |
794b0a03 | 37 | extern int am_server; |
c3e5e585 WD |
38 | extern int am_sender; |
39 | extern int am_generator; | |
ed9d969c | 40 | extern int am_starting_up; |
e0f4a661 | 41 | extern int allow_8bit_chars; |
b675ba6f | 42 | extern int protocol_version; |
9b25ef35 WD |
43 | extern int uid_ndx; |
44 | extern int gid_ndx; | |
3ea6e0e7 | 45 | extern int inc_recurse; |
c786a7ae | 46 | extern int inplace; |
f3d6d480 | 47 | extern int flist_eof; |
81b07870 | 48 | extern int keep_dirlinks; |
2f03f956 | 49 | extern int make_backups; |
f3d6d480 | 50 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
94d3725c | 51 | extern struct chmod_mode_struct *daemon_chmod_modes; |
332cf6df WD |
52 | #ifdef ICONV_OPTION |
53 | extern char *iconv_opt; | |
54 | #endif | |
fe055c71 | 55 | |
332cf6df | 56 | #ifdef ICONV_CONST |
ceccbacc | 57 | iconv_t ic_chck = (iconv_t)-1; |
2ac97930 | 58 | # ifdef ICONV_OPTION |
332cf6df | 59 | iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1; |
2ac97930 | 60 | # endif |
ceccbacc | 61 | |
2a62f5ee | 62 | static const char *default_charset(void) |
ceccbacc | 63 | { |
2ac97930 | 64 | # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
ceccbacc | 65 | return locale_charset(); |
2ac97930 | 66 | # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO |
7fc87d2d | 67 | return nl_langinfo(CODESET); |
2ac97930 | 68 | # else |
ceccbacc | 69 | return ""; /* Works with (at the very least) gnu iconv... */ |
2ac97930 | 70 | # endif |
ceccbacc WD |
71 | } |
72 | ||
73 | void setup_iconv() | |
74 | { | |
332cf6df WD |
75 | const char *defset = default_charset(); |
76 | # ifdef ICONV_OPTION | |
77 | const char *charset; | |
78 | char *cp; | |
2ac97930 | 79 | # endif |
332cf6df | 80 | |
e0f4a661 | 81 | if (!am_server && !allow_8bit_chars) { |
2d2f71fb | 82 | |
e0f4a661 WD |
83 | /* It's OK if this fails... */ |
84 | ic_chck = iconv_open(defset, defset); | |
ceccbacc | 85 | |
e0f4a661 WD |
86 | if (verbose > 3) { |
87 | if (ic_chck == (iconv_t)-1) { | |
88 | rprintf(FINFO, | |
89 | "note: iconv_open(\"%s\", \"%s\") failed (%d)" | |
90 | " -- using isprint() instead of iconv().\n", | |
91 | defset, defset, errno); | |
92 | } else { | |
93 | rprintf(FINFO, | |
94 | "note: iconv_open(\"%s\", \"%s\") succeeded.\n", | |
95 | defset, defset); | |
96 | } | |
7fc87d2d | 97 | } |
ceccbacc | 98 | } |
332cf6df WD |
99 | |
100 | # ifdef ICONV_OPTION | |
101 | if (!iconv_opt) | |
102 | return; | |
103 | ||
104 | if ((cp = strchr(iconv_opt, ',')) != NULL) { | |
105 | if (am_server) /* A local transfer needs this. */ | |
106 | iconv_opt = cp + 1; | |
107 | else | |
108 | *cp = '\0'; | |
109 | } | |
110 | ||
111 | if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0')) | |
112 | charset = defset; | |
113 | else | |
114 | charset = iconv_opt; | |
115 | ||
116 | if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) { | |
117 | rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n", | |
118 | UTF8_CHARSET, charset); | |
119 | exit_cleanup(RERR_UNSUPPORTED); | |
120 | } | |
121 | ||
122 | if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) { | |
123 | rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n", | |
124 | charset, UTF8_CHARSET); | |
125 | exit_cleanup(RERR_UNSUPPORTED); | |
126 | } | |
127 | ||
332cf6df WD |
128 | if (verbose > 1) { |
129 | rprintf(FINFO, "%s charset: %s\n", | |
130 | am_server ? "server" : "client", | |
131 | *charset ? charset : "[LOCALE]"); | |
132 | } | |
133 | # endif | |
ceccbacc | 134 | } |
2ac97930 WD |
135 | |
136 | # ifdef ICONV_OPTION | |
137 | ||
138 | /* This function converts the characters in the "in" xbuf into characters | |
139 | * in the "out" xbuf. The "len" of the "in" xbuf is used starting from its | |
140 | * "pos". The "size" of the "out" xbuf restricts how many characters can be | |
141 | * stored, starting at its "pos+len" position. Note that the last byte of | |
142 | * the buffer is never used, which reserves space for a terminating '\0'. | |
143 | * We return a 0 on success or a -1 on error. An error also sets errno to | |
144 | * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0. | |
145 | * The "in" xbuf is altered to update "pos" and "len". The "out" xbuf has | |
146 | * data appended, and its "len" incremented. If ICB_EXPAND_OUT is set in | |
147 | * "flags", the "out" xbuf will also be allocated if empty, and expanded if | |
148 | * too small (so E2BIG will not be returned). If ICB_INCLUDE_BAD is set in | |
149 | * "flags", any badly-encoded chars are included verbatim in the "out" xbuf, | |
150 | * so EILSEQ will not be returned. Likewise for ICB_INCLUDE_INCOMPLETE with | |
151 | * respect to an incomplete multi-byte char at the end, which ensures that | |
152 | * EINVAL is not returned. Anytime "in.pos" is 0 we will reset the iconv() | |
153 | * state prior to processing the characters. */ | |
154 | int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags) | |
155 | { | |
156 | ICONV_CONST char *ibuf; | |
157 | size_t icnt, ocnt; | |
158 | char *obuf; | |
159 | ||
160 | if (!out->size && flags & ICB_EXPAND_OUT) | |
161 | alloc_xbuf(out, 1024); | |
162 | ||
163 | if (!in->pos) | |
164 | iconv(ic, NULL, 0, NULL, 0); | |
165 | ||
166 | ibuf = in->buf + in->pos; | |
167 | icnt = in->len; | |
168 | ||
169 | obuf = out->buf + (out->pos + out->len); | |
170 | ocnt = out->size - (out->pos + out->len) - 1; | |
171 | ||
172 | while (icnt) { | |
173 | while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) { | |
174 | if (errno == EINTR) | |
175 | continue; | |
176 | if (errno == EINVAL) { | |
177 | if (!(flags & ICB_INCLUDE_INCOMPLETE)) | |
178 | goto finish; | |
179 | } else if (errno == EILSEQ) { | |
180 | if (!(flags & ICB_INCLUDE_BAD)) | |
181 | goto finish; | |
182 | } else { | |
183 | size_t opos = obuf - out->buf; | |
184 | if (!(flags & ICB_EXPAND_OUT)) { | |
185 | errno = E2BIG; | |
186 | goto finish; | |
187 | } | |
188 | realloc_xbuf(out, out->size + 1024); | |
189 | obuf = out->buf + opos; | |
190 | ocnt += 1024; | |
191 | continue; | |
192 | } | |
193 | *obuf++ = *ibuf++; | |
194 | ocnt--, icnt--; | |
195 | } | |
196 | } | |
197 | ||
198 | errno = 0; | |
199 | ||
200 | finish: | |
201 | in->len = icnt; | |
202 | in->pos = ibuf - in->buf; | |
203 | out->len = obuf - out->buf - out->pos; | |
204 | ||
205 | return errno ? -1 : 0; | |
206 | } | |
207 | # endif | |
ceccbacc | 208 | #endif |
fe055c71 | 209 | |
16edf865 WD |
210 | int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr, |
211 | char *buf, int *len_ptr) | |
d1c178dd | 212 | { |
f3d6d480 WD |
213 | int len, iflags = 0; |
214 | struct file_list *flist; | |
d1c178dd | 215 | uchar fnamecmp_type = FNAMECMP_FNAME; |
19d4cac9 | 216 | int ndx; |
f3d6d480 WD |
217 | |
218 | read_loop: | |
219 | while (1) { | |
9ae7a2cd | 220 | ndx = read_ndx(f_in); |
f3d6d480 WD |
221 | |
222 | if (ndx >= 0) | |
223 | break; | |
224 | if (ndx == NDX_DONE) | |
225 | return ndx; | |
3ea6e0e7 | 226 | if (!inc_recurse || am_sender) |
f3d6d480 WD |
227 | goto invalid_ndx; |
228 | if (ndx == NDX_FLIST_EOF) { | |
229 | flist_eof = 1; | |
332cf6df | 230 | send_msg(MSG_FLIST_EOF, "", 0, 0); |
f3d6d480 WD |
231 | continue; |
232 | } | |
233 | ndx = NDX_FLIST_OFFSET - ndx; | |
9decb4d2 | 234 | if (ndx < 0 || ndx >= dir_flist->used) { |
f3d6d480 WD |
235 | ndx = NDX_FLIST_OFFSET - ndx; |
236 | rprintf(FERROR, | |
59fd2a5e WD |
237 | "[%s] Invalid dir index: %d (%d - %d)\n", |
238 | who_am_i(), ndx, NDX_FLIST_OFFSET, | |
9decb4d2 | 239 | NDX_FLIST_OFFSET - dir_flist->used + 1); |
f3d6d480 WD |
240 | exit_cleanup(RERR_PROTOCOL); |
241 | } | |
f3d6d480 WD |
242 | |
243 | /* Send everything read from f_in to msg_fd_out. */ | |
244 | send_msg_int(MSG_FLIST, ndx); | |
245 | start_flist_forward(f_in); | |
1faa1a6d WD |
246 | if (verbose > 3) { |
247 | rprintf(FINFO, "[%s] receiving flist for dir %d\n", | |
248 | who_am_i(), ndx); | |
249 | } | |
f3d6d480 WD |
250 | flist = recv_file_list(f_in); |
251 | flist->parent_ndx = ndx; | |
252 | stop_flist_forward(); | |
f3d6d480 WD |
253 | } |
254 | ||
255 | iflags = protocol_version >= 29 ? read_shortint(f_in) | |
d1c178dd WD |
256 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
257 | ||
f3d6d480 WD |
258 | /* Honor the old-style keep-alive indicator. */ |
259 | if (protocol_version < 30 | |
9decb4d2 | 260 | && ndx == cur_flist->used && iflags == ITEM_IS_NEW) { |
f3d6d480 WD |
261 | if (am_sender) |
262 | maybe_send_keepalive(); | |
263 | goto read_loop; | |
264 | } | |
265 | ||
266 | if (!(flist = flist_for_ndx(ndx))) { | |
267 | invalid_ndx: | |
268 | rprintf(FERROR, | |
269 | "Invalid file index: %d (%d - %d) with iflags %x [%s]\n", | |
6755a7d7 WD |
270 | ndx, first_flist->ndx_start - 1, first_flist->prev->ndx_end, |
271 | iflags, who_am_i()); | |
d1c178dd WD |
272 | exit_cleanup(RERR_PROTOCOL); |
273 | } | |
f3d6d480 | 274 | cur_flist = flist; |
d1c178dd WD |
275 | |
276 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) | |
277 | fnamecmp_type = read_byte(f_in); | |
278 | *type_ptr = fnamecmp_type; | |
279 | ||
280 | if (iflags & ITEM_XNAME_FOLLOWS) { | |
281 | if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0) | |
282 | exit_cleanup(RERR_PROTOCOL); | |
283 | } else { | |
284 | *buf = '\0'; | |
285 | len = -1; | |
286 | } | |
287 | *len_ptr = len; | |
288 | ||
289 | if (iflags & ITEM_TRANSFER) { | |
f3d6d480 | 290 | int i = ndx - cur_flist->ndx_start; |
6755a7d7 | 291 | if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) { |
d1c178dd WD |
292 | rprintf(FERROR, |
293 | "received request to transfer non-regular file: %d [%s]\n", | |
294 | ndx, who_am_i()); | |
295 | exit_cleanup(RERR_PROTOCOL); | |
296 | } | |
d1c178dd WD |
297 | } |
298 | ||
f3d6d480 WD |
299 | *iflag_ptr = iflags; |
300 | return ndx; | |
d1c178dd WD |
301 | } |
302 | ||
c627d613 AT |
303 | /* |
304 | free a sums struct | |
305 | */ | |
2f03f956 | 306 | void free_sums(struct sum_struct *s) |
c627d613 | 307 | { |
298c10d5 AT |
308 | if (s->sums) free(s->sums); |
309 | free(s); | |
c627d613 AT |
310 | } |
311 | ||
81284832 WD |
312 | /* This is only called when we aren't preserving permissions. Figure out what |
313 | * the permissions should be and return them merged back into the mode. */ | |
1c3344a1 WD |
314 | mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms, |
315 | int exists) | |
81284832 | 316 | { |
67f8a41b | 317 | int new_mode; |
9b499e95 | 318 | /* If the file already exists, we'll return the local permissions, |
81284832 WD |
319 | * possibly tweaked by the --executability option. */ |
320 | if (exists) { | |
67f8a41b | 321 | new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS); |
81284832 WD |
322 | if (preserve_executability && S_ISREG(flist_mode)) { |
323 | /* If the source file is executable, grant execute | |
324 | * rights to everyone who can read, but ONLY if the | |
325 | * file isn't already executable. */ | |
326 | if (!(flist_mode & 0111)) | |
67f8a41b WD |
327 | new_mode &= ~0111; |
328 | else if (!(stat_mode & 0111)) | |
329 | new_mode |= (new_mode & 0444) >> 2; | |
81284832 | 330 | } |
67f8a41b | 331 | } else { |
1c3344a1 WD |
332 | /* Apply destination default permissions and turn |
333 | * off special permissions. */ | |
334 | new_mode = flist_mode & (~CHMOD_BITS | dflt_perms); | |
67f8a41b | 335 | } |
67f8a41b | 336 | return new_mode; |
81284832 WD |
337 | } |
338 | ||
16edf865 WD |
339 | int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp, |
340 | const char *fnamecmp, int flags) | |
c627d613 | 341 | { |
667e72a1 | 342 | int updated = 0; |
1c3344a1 | 343 | statx sx2; |
460f6b99 | 344 | int change_uid, change_gid; |
519d55a9 | 345 | mode_t new_mode = file->mode; |
2dc7b91d | 346 | int inherit; |
c627d613 | 347 | |
1c3344a1 | 348 | if (!sxp) { |
f227ffe4 WD |
349 | if (dry_run) |
350 | return 1; | |
1c3344a1 | 351 | if (link_stat(fname, &sx2.st, 0) < 0) { |
d62bcc17 WD |
352 | rsyserr(FERROR, errno, "stat %s failed", |
353 | full_fname(fname)); | |
667e72a1 AT |
354 | return 0; |
355 | } | |
1c3344a1 WD |
356 | #ifdef SUPPORT_ACLS |
357 | sx2.acc_acl = sx2.def_acl = NULL; | |
16edf865 WD |
358 | #endif |
359 | #ifdef SUPPORT_XATTRS | |
360 | sx2.xattr = NULL; | |
1c3344a1 | 361 | #endif |
1c3344a1 | 362 | sxp = &sx2; |
2dc7b91d WD |
363 | inherit = !preserve_perms; |
364 | } else | |
365 | inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED; | |
366 | ||
367 | if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) { | |
368 | /* We just created this directory and its setgid | |
369 | * bit is on, so make sure it stays on. */ | |
370 | new_mode |= S_ISGID; | |
667e72a1 | 371 | } |
c627d613 | 372 | |
1c3344a1 WD |
373 | #ifdef SUPPORT_ACLS |
374 | if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp)) | |
375 | get_acl(fname, sxp); | |
376 | #endif | |
377 | ||
16edf865 WD |
378 | #ifdef SUPPORT_XATTRS |
379 | if (preserve_xattrs && fnamecmp) | |
380 | set_xattr(fname, file, fnamecmp, sxp); | |
9439c0cb WD |
381 | if (am_root < 0) |
382 | set_stat_xattr(fname, file); | |
16edf865 WD |
383 | #endif |
384 | ||
f9998046 | 385 | if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1)) |
36119f6e WD |
386 | flags |= ATTRS_SKIP_MTIME; |
387 | if (!(flags & ATTRS_SKIP_MTIME) | |
1c3344a1 WD |
388 | && cmp_time(sxp->st.st_mtime, file->modtime) != 0) { |
389 | int ret = set_modtime(fname, file->modtime, sxp->st.st_mode); | |
c8d34657 | 390 | if (ret < 0) { |
d62bcc17 WD |
391 | rsyserr(FERROR, errno, "failed to set times on %s", |
392 | full_fname(fname)); | |
1c3344a1 | 393 | goto cleanup; |
667e72a1 | 394 | } |
c8d34657 WD |
395 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
396 | updated = 1; | |
667e72a1 AT |
397 | } |
398 | ||
9b25ef35 WD |
399 | change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file); |
400 | change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP) | |
858d45f1 | 401 | && sxp->st.st_gid != (gid_t)F_GROUP(file); |
4f5b0756 | 402 | #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK |
1c3344a1 | 403 | if (S_ISLNK(sxp->st.st_mode)) |
a41a1e87 WD |
404 | ; |
405 | else | |
406 | #endif | |
460f6b99 | 407 | if (change_uid || change_gid) { |
62c9e6b3 | 408 | if (verbose > 2) { |
ce37eb2d WD |
409 | if (change_uid) { |
410 | rprintf(FINFO, | |
4ade505c WD |
411 | "set uid of %s from %u to %u\n", |
412 | fname, (unsigned)sxp->st.st_uid, F_OWNER(file)); | |
ce37eb2d WD |
413 | } |
414 | if (change_gid) { | |
415 | rprintf(FINFO, | |
4ade505c WD |
416 | "set gid of %s from %u to %u\n", |
417 | fname, (unsigned)sxp->st.st_gid, F_GROUP(file)); | |
ce37eb2d WD |
418 | } |
419 | } | |
9439c0cb WD |
420 | if (am_root < 0) { |
421 | ; | |
422 | } else if (do_lchown(fname, | |
858d45f1 WD |
423 | change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid, |
424 | change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) { | |
460f6b99 | 425 | /* shouldn't have attempted to change uid or gid |
a174e1ed | 426 | * unless have the privilege */ |
d62bcc17 | 427 | rsyserr(FERROR, errno, "%s %s failed", |
a174e1ed | 428 | change_uid ? "chown" : "chgrp", |
d62bcc17 | 429 | full_fname(fname)); |
1c3344a1 | 430 | goto cleanup; |
9439c0cb | 431 | } else |
a5827a28 | 432 | /* a lchown had been done - we have to re-stat if the |
dd096ae0 WD |
433 | * destination had the setuid or setgid bits set due |
434 | * to the side effect of the chown call */ | |
1c3344a1 WD |
435 | if (sxp->st.st_mode & (S_ISUID | S_ISGID)) { |
436 | link_stat(fname, &sxp->st, | |
437 | keep_dirlinks && S_ISDIR(sxp->st.st_mode)); | |
a5827a28 | 438 | } |
460f6b99 | 439 | updated = 1; |
667e72a1 | 440 | } |
c627d613 | 441 | |
519d55a9 WD |
442 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
443 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); | |
1c3344a1 WD |
444 | |
445 | #ifdef SUPPORT_ACLS | |
446 | /* It's OK to call set_acl() now, even for a dir, as the generator | |
447 | * will enable owner-writability using chmod, if necessary. | |
448 | * | |
449 | * If set_acl() changes permission bits in the process of setting | |
450 | * an access ACL, it changes sxp->st.st_mode so we know whether we | |
451 | * need to chmod(). */ | |
452 | if (preserve_acls && !S_ISLNK(new_mode) && set_acl(fname, file, sxp) == 0) | |
453 | updated = 1; | |
454 | #endif | |
455 | ||
4f5b0756 | 456 | #ifdef HAVE_CHMOD |
1c3344a1 | 457 | if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) { |
9439c0cb | 458 | int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode); |
c8d34657 WD |
459 | if (ret < 0) { |
460 | rsyserr(FERROR, errno, | |
461 | "failed to set permissions on %s", | |
462 | full_fname(fname)); | |
1c3344a1 | 463 | goto cleanup; |
667e72a1 | 464 | } |
c8d34657 WD |
465 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
466 | updated = 1; | |
667e72a1 | 467 | } |
c627d613 | 468 | #endif |
6a7cc46c | 469 | |
36119f6e | 470 | if (verbose > 1 && flags & ATTRS_REPORT) { |
667e72a1 | 471 | if (updated) |
1925c344 | 472 | rprintf(FCLIENT, "%s\n", fname); |
667e72a1 | 473 | else |
1925c344 | 474 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
667e72a1 | 475 | } |
1c3344a1 | 476 | cleanup: |
16edf865 | 477 | if (sxp == &sx2) { |
1c3344a1 | 478 | #ifdef SUPPORT_ACLS |
16edf865 WD |
479 | if (preserve_acls) |
480 | free_acl(&sx2); | |
1c3344a1 | 481 | #endif |
16edf865 WD |
482 | #ifdef SUPPORT_XATTRS |
483 | if (preserve_xattrs) | |
484 | free_xattr(&sx2); | |
485 | #endif | |
486 | } | |
667e72a1 | 487 | return updated; |
c627d613 AT |
488 | } |
489 | ||
b8e9c234 | 490 | RETSIGTYPE sig_int(UNUSED(int val)) |
34ccb63e | 491 | { |
d5a0b483 WD |
492 | /* KLUGE: if the user hits Ctrl-C while ssh is prompting |
493 | * for a password, then our cleanup's sending of a SIGUSR1 | |
494 | * signal to all our children may kill ssh before it has a | |
495 | * chance to restore the tty settings (i.e. turn echo back | |
496 | * on). By sleeping for a short time, ssh gets a bigger | |
497 | * chance to do the right thing. If child processes are | |
498 | * not ssh waiting for a password, then this tiny delay | |
499 | * shouldn't hurt anything. */ | |
500 | msleep(400); | |
65417579 | 501 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
502 | } |
503 | ||
d8b1c923 WD |
504 | /* Finish off a file transfer: renaming the file and setting the file's |
505 | * attributes (e.g. permissions, ownership, etc.). If partialptr is not | |
506 | * NULL and the robust_rename() call is forced to copy the temp file, we | |
507 | * stage the file into the partial-dir and then rename it into place. */ | |
16edf865 WD |
508 | void finish_transfer(const char *fname, const char *fnametmp, |
509 | const char *fnamecmp, const char *partialptr, | |
d8b1c923 WD |
510 | struct file_struct *file, int ok_to_set_time, |
511 | int overwriting_basis) | |
c95da96a | 512 | { |
62c9e6b3 WD |
513 | int ret; |
514 | ||
a3221d2a WD |
515 | if (inplace) { |
516 | if (verbose > 2) | |
45c49b52 | 517 | rprintf(FINFO, "finishing %s\n", fname); |
d8b1c923 | 518 | fnametmp = fname; |
36119f6e | 519 | goto do_set_file_attrs; |
a3221d2a WD |
520 | } |
521 | ||
f3d6d480 | 522 | if (make_backups > 0 && overwriting_basis && !make_backup(fname)) |
e484f0cc WD |
523 | return; |
524 | ||
ebeacb36 | 525 | /* Change permissions before putting the file into place. */ |
16edf865 | 526 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 527 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
ebeacb36 | 528 | |
c95da96a | 529 | /* move tmp file over real file */ |
45c49b52 WD |
530 | if (verbose > 2) |
531 | rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname); | |
d8b1c923 WD |
532 | ret = robust_rename(fnametmp, fname, partialptr, |
533 | file->mode & INITACCESSPERMS); | |
3d061653 | 534 | if (ret < 0) { |
d62bcc17 | 535 | rsyserr(FERROR, errno, "%s %s -> \"%s\"", |
45c49b52 WD |
536 | ret == -2 ? "copy" : "rename", |
537 | full_fname(fnametmp), fname); | |
c7c11a0d | 538 | do_unlink(fnametmp); |
077e59b7 | 539 | return; |
c95da96a | 540 | } |
ebeacb36 WD |
541 | if (ret == 0) { |
542 | /* The file was moved into place (not copied), so it's done. */ | |
543 | return; | |
544 | } | |
d8b1c923 WD |
545 | /* The file was copied, so tweak the perms of the copied file. If it |
546 | * was copied to partialptr, move it into its final destination. */ | |
547 | fnametmp = partialptr ? partialptr : fname; | |
548 | ||
36119f6e | 549 | do_set_file_attrs: |
16edf865 | 550 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 551 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
d8b1c923 WD |
552 | |
553 | if (partialptr) { | |
554 | if (do_rename(fnametmp, fname) < 0) { | |
555 | rsyserr(FERROR, errno, "rename %s -> \"%s\"", | |
556 | full_fname(fnametmp), fname); | |
557 | } else | |
558 | handle_partial_dir(partialptr, PDIR_DELETE); | |
559 | } | |
c95da96a | 560 | } |
c3e5e585 | 561 | |
f3d6d480 WD |
562 | struct file_list *flist_for_ndx(int ndx) |
563 | { | |
564 | struct file_list *flist = cur_flist; | |
565 | ||
87a34ce5 | 566 | if (!flist && !(flist = first_flist)) |
f3d6d480 WD |
567 | return NULL; |
568 | ||
6755a7d7 | 569 | while (ndx < flist->ndx_start-1) { |
f3d6d480 WD |
570 | if (flist == first_flist) |
571 | return NULL; | |
572 | flist = flist->prev; | |
573 | } | |
6755a7d7 | 574 | while (ndx > flist->ndx_end) { |
f3d6d480 WD |
575 | if (!(flist = flist->next)) |
576 | return NULL; | |
577 | } | |
578 | return flist; | |
579 | } | |
580 | ||
c3e5e585 WD |
581 | const char *who_am_i(void) |
582 | { | |
ed9d969c | 583 | if (am_starting_up) |
794b0a03 | 584 | return am_server ? "server" : "client"; |
ebeacb36 | 585 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
c3e5e585 | 586 | } |