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 | |
d3d07a5e | 6 | * Copyright (C) 2003-2008 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" |
1b42f628 | 23 | #include "ifuncs.h" |
ceccbacc WD |
24 | #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
25 | #include <libcharset.h> | |
26 | #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO | |
27 | #include <langinfo.h> | |
28 | #endif | |
43a481dc | 29 | |
c627d613 | 30 | extern int verbose; |
c627d613 | 31 | extern int dry_run; |
1c3344a1 | 32 | extern int preserve_acls; |
16edf865 | 33 | extern int preserve_xattrs; |
9b499e95 | 34 | extern int preserve_perms; |
81284832 | 35 | extern int preserve_executability; |
2f03f956 | 36 | extern int preserve_times; |
7b8356d0 | 37 | extern int am_root; |
794b0a03 | 38 | extern int am_server; |
c3e5e585 WD |
39 | extern int am_sender; |
40 | extern int am_generator; | |
ed9d969c | 41 | extern int am_starting_up; |
e0f4a661 | 42 | extern int allow_8bit_chars; |
b675ba6f | 43 | extern int protocol_version; |
0099e423 | 44 | extern int receiver_symlink_times; |
9b25ef35 WD |
45 | extern int uid_ndx; |
46 | extern int gid_ndx; | |
3ea6e0e7 | 47 | extern int inc_recurse; |
c786a7ae | 48 | extern int inplace; |
f3d6d480 | 49 | extern int flist_eof; |
81b07870 | 50 | extern int keep_dirlinks; |
2f03f956 | 51 | extern int make_backups; |
f3d6d480 | 52 | extern struct file_list *cur_flist, *first_flist, *dir_flist; |
94d3725c | 53 | extern struct chmod_mode_struct *daemon_chmod_modes; |
332cf6df WD |
54 | #ifdef ICONV_OPTION |
55 | extern char *iconv_opt; | |
56 | #endif | |
fe055c71 | 57 | |
332cf6df | 58 | #ifdef ICONV_CONST |
ceccbacc | 59 | iconv_t ic_chck = (iconv_t)-1; |
2ac97930 | 60 | # ifdef ICONV_OPTION |
332cf6df | 61 | iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1; |
2ac97930 | 62 | # endif |
ceccbacc | 63 | |
2a62f5ee | 64 | static const char *default_charset(void) |
ceccbacc | 65 | { |
2ac97930 | 66 | # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET |
ceccbacc | 67 | return locale_charset(); |
2ac97930 | 68 | # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO |
7fc87d2d | 69 | return nl_langinfo(CODESET); |
2ac97930 | 70 | # else |
ceccbacc | 71 | return ""; /* Works with (at the very least) gnu iconv... */ |
2ac97930 | 72 | # endif |
ceccbacc WD |
73 | } |
74 | ||
6191429b | 75 | void setup_iconv(void) |
ceccbacc | 76 | { |
332cf6df WD |
77 | const char *defset = default_charset(); |
78 | # ifdef ICONV_OPTION | |
79 | const char *charset; | |
80 | char *cp; | |
2ac97930 | 81 | # endif |
332cf6df | 82 | |
e0f4a661 | 83 | if (!am_server && !allow_8bit_chars) { |
2d2f71fb | 84 | |
e0f4a661 WD |
85 | /* It's OK if this fails... */ |
86 | ic_chck = iconv_open(defset, defset); | |
ceccbacc | 87 | |
e0f4a661 WD |
88 | if (verbose > 3) { |
89 | if (ic_chck == (iconv_t)-1) { | |
90 | rprintf(FINFO, | |
91 | "note: iconv_open(\"%s\", \"%s\") failed (%d)" | |
92 | " -- using isprint() instead of iconv().\n", | |
93 | defset, defset, errno); | |
94 | } else { | |
95 | rprintf(FINFO, | |
96 | "note: iconv_open(\"%s\", \"%s\") succeeded.\n", | |
97 | defset, defset); | |
98 | } | |
7fc87d2d | 99 | } |
ceccbacc | 100 | } |
332cf6df WD |
101 | |
102 | # ifdef ICONV_OPTION | |
103 | if (!iconv_opt) | |
104 | return; | |
105 | ||
106 | if ((cp = strchr(iconv_opt, ',')) != NULL) { | |
107 | if (am_server) /* A local transfer needs this. */ | |
108 | iconv_opt = cp + 1; | |
109 | else | |
110 | *cp = '\0'; | |
111 | } | |
112 | ||
113 | if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0')) | |
114 | charset = defset; | |
115 | else | |
116 | charset = iconv_opt; | |
117 | ||
118 | if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) { | |
119 | rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n", | |
120 | UTF8_CHARSET, charset); | |
121 | exit_cleanup(RERR_UNSUPPORTED); | |
122 | } | |
123 | ||
124 | if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) { | |
125 | rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n", | |
126 | charset, UTF8_CHARSET); | |
127 | exit_cleanup(RERR_UNSUPPORTED); | |
128 | } | |
129 | ||
332cf6df WD |
130 | if (verbose > 1) { |
131 | rprintf(FINFO, "%s charset: %s\n", | |
132 | am_server ? "server" : "client", | |
133 | *charset ? charset : "[LOCALE]"); | |
134 | } | |
135 | # endif | |
ceccbacc | 136 | } |
2ac97930 | 137 | |
2ac97930 WD |
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 | } | |
ceccbacc | 207 | #endif |
fe055c71 | 208 | |
16edf865 WD |
209 | int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr, |
210 | char *buf, int *len_ptr) | |
d1c178dd | 211 | { |
f3d6d480 WD |
212 | int len, iflags = 0; |
213 | struct file_list *flist; | |
d1c178dd | 214 | uchar fnamecmp_type = FNAMECMP_FNAME; |
b7386d23 | 215 | int ndx, save_verbose = verbose; |
f3d6d480 WD |
216 | |
217 | read_loop: | |
218 | while (1) { | |
9ae7a2cd | 219 | ndx = read_ndx(f_in); |
f3d6d480 WD |
220 | |
221 | if (ndx >= 0) | |
222 | break; | |
223 | if (ndx == NDX_DONE) | |
224 | return ndx; | |
3ea6e0e7 | 225 | if (!inc_recurse || am_sender) |
f3d6d480 WD |
226 | goto invalid_ndx; |
227 | if (ndx == NDX_FLIST_EOF) { | |
228 | flist_eof = 1; | |
332cf6df | 229 | send_msg(MSG_FLIST_EOF, "", 0, 0); |
f3d6d480 WD |
230 | continue; |
231 | } | |
232 | ndx = NDX_FLIST_OFFSET - ndx; | |
9decb4d2 | 233 | if (ndx < 0 || ndx >= dir_flist->used) { |
f3d6d480 WD |
234 | ndx = NDX_FLIST_OFFSET - ndx; |
235 | rprintf(FERROR, | |
59fd2a5e WD |
236 | "[%s] Invalid dir index: %d (%d - %d)\n", |
237 | who_am_i(), ndx, NDX_FLIST_OFFSET, | |
9decb4d2 | 238 | NDX_FLIST_OFFSET - dir_flist->used + 1); |
f3d6d480 WD |
239 | exit_cleanup(RERR_PROTOCOL); |
240 | } | |
f3d6d480 WD |
241 | |
242 | /* Send everything read from f_in to msg_fd_out. */ | |
1faa1a6d WD |
243 | if (verbose > 3) { |
244 | rprintf(FINFO, "[%s] receiving flist for dir %d\n", | |
245 | who_am_i(), ndx); | |
246 | } | |
b7386d23 WD |
247 | verbose = 0; |
248 | send_msg_int(MSG_FLIST, ndx); | |
249 | start_flist_forward(f_in); | |
f3d6d480 WD |
250 | flist = recv_file_list(f_in); |
251 | flist->parent_ndx = ndx; | |
252 | stop_flist_forward(); | |
b7386d23 | 253 | verbose = save_verbose; |
f3d6d480 WD |
254 | } |
255 | ||
256 | iflags = protocol_version >= 29 ? read_shortint(f_in) | |
d1c178dd WD |
257 | : ITEM_TRANSFER | ITEM_MISSING_DATA; |
258 | ||
f3d6d480 WD |
259 | /* Honor the old-style keep-alive indicator. */ |
260 | if (protocol_version < 30 | |
9decb4d2 | 261 | && ndx == cur_flist->used && iflags == ITEM_IS_NEW) { |
f3d6d480 WD |
262 | if (am_sender) |
263 | maybe_send_keepalive(); | |
264 | goto read_loop; | |
265 | } | |
266 | ||
267 | if (!(flist = flist_for_ndx(ndx))) { | |
65b4e4b2 | 268 | int start, used; |
f3d6d480 | 269 | invalid_ndx: |
65b4e4b2 WD |
270 | start = first_flist ? first_flist->ndx_start : 0; |
271 | used = first_flist ? first_flist->used : 0; | |
f3d6d480 WD |
272 | rprintf(FERROR, |
273 | "Invalid file index: %d (%d - %d) with iflags %x [%s]\n", | |
65b4e4b2 | 274 | ndx, start - 1, start + used -1, iflags, who_am_i()); |
d1c178dd WD |
275 | exit_cleanup(RERR_PROTOCOL); |
276 | } | |
f3d6d480 | 277 | cur_flist = flist; |
d1c178dd WD |
278 | |
279 | if (iflags & ITEM_BASIS_TYPE_FOLLOWS) | |
280 | fnamecmp_type = read_byte(f_in); | |
281 | *type_ptr = fnamecmp_type; | |
282 | ||
283 | if (iflags & ITEM_XNAME_FOLLOWS) { | |
284 | if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0) | |
285 | exit_cleanup(RERR_PROTOCOL); | |
286 | } else { | |
287 | *buf = '\0'; | |
288 | len = -1; | |
289 | } | |
290 | *len_ptr = len; | |
291 | ||
292 | if (iflags & ITEM_TRANSFER) { | |
f3d6d480 | 293 | int i = ndx - cur_flist->ndx_start; |
6755a7d7 | 294 | if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) { |
d1c178dd WD |
295 | rprintf(FERROR, |
296 | "received request to transfer non-regular file: %d [%s]\n", | |
297 | ndx, who_am_i()); | |
298 | exit_cleanup(RERR_PROTOCOL); | |
299 | } | |
d1c178dd WD |
300 | } |
301 | ||
f3d6d480 WD |
302 | *iflag_ptr = iflags; |
303 | return ndx; | |
d1c178dd WD |
304 | } |
305 | ||
c627d613 AT |
306 | /* |
307 | free a sums struct | |
308 | */ | |
2f03f956 | 309 | void free_sums(struct sum_struct *s) |
c627d613 | 310 | { |
298c10d5 AT |
311 | if (s->sums) free(s->sums); |
312 | free(s); | |
c627d613 AT |
313 | } |
314 | ||
81284832 WD |
315 | /* This is only called when we aren't preserving permissions. Figure out what |
316 | * the permissions should be and return them merged back into the mode. */ | |
1c3344a1 WD |
317 | mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms, |
318 | int exists) | |
81284832 | 319 | { |
67f8a41b | 320 | int new_mode; |
9b499e95 | 321 | /* If the file already exists, we'll return the local permissions, |
81284832 WD |
322 | * possibly tweaked by the --executability option. */ |
323 | if (exists) { | |
67f8a41b | 324 | new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS); |
81284832 WD |
325 | if (preserve_executability && S_ISREG(flist_mode)) { |
326 | /* If the source file is executable, grant execute | |
327 | * rights to everyone who can read, but ONLY if the | |
328 | * file isn't already executable. */ | |
329 | if (!(flist_mode & 0111)) | |
67f8a41b WD |
330 | new_mode &= ~0111; |
331 | else if (!(stat_mode & 0111)) | |
332 | new_mode |= (new_mode & 0444) >> 2; | |
81284832 | 333 | } |
67f8a41b | 334 | } else { |
1c3344a1 WD |
335 | /* Apply destination default permissions and turn |
336 | * off special permissions. */ | |
337 | new_mode = flist_mode & (~CHMOD_BITS | dflt_perms); | |
67f8a41b | 338 | } |
67f8a41b | 339 | return new_mode; |
81284832 WD |
340 | } |
341 | ||
13710874 | 342 | int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp, |
16edf865 | 343 | const char *fnamecmp, int flags) |
c627d613 | 344 | { |
667e72a1 | 345 | int updated = 0; |
13710874 | 346 | stat_x sx2; |
460f6b99 | 347 | int change_uid, change_gid; |
519d55a9 | 348 | mode_t new_mode = file->mode; |
2dc7b91d | 349 | int inherit; |
c627d613 | 350 | |
1c3344a1 | 351 | if (!sxp) { |
f227ffe4 WD |
352 | if (dry_run) |
353 | return 1; | |
1c3344a1 | 354 | if (link_stat(fname, &sx2.st, 0) < 0) { |
3f0211b6 | 355 | rsyserr(FERROR_XFER, errno, "stat %s failed", |
d62bcc17 | 356 | full_fname(fname)); |
667e72a1 AT |
357 | return 0; |
358 | } | |
1c3344a1 WD |
359 | #ifdef SUPPORT_ACLS |
360 | sx2.acc_acl = sx2.def_acl = NULL; | |
16edf865 WD |
361 | #endif |
362 | #ifdef SUPPORT_XATTRS | |
363 | sx2.xattr = NULL; | |
1c3344a1 | 364 | #endif |
1c3344a1 | 365 | sxp = &sx2; |
2dc7b91d WD |
366 | inherit = !preserve_perms; |
367 | } else | |
368 | inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED; | |
369 | ||
370 | if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) { | |
371 | /* We just created this directory and its setgid | |
372 | * bit is on, so make sure it stays on. */ | |
373 | new_mode |= S_ISGID; | |
667e72a1 | 374 | } |
c627d613 | 375 | |
e107b6b1 WD |
376 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
377 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); | |
378 | ||
1c3344a1 WD |
379 | #ifdef SUPPORT_ACLS |
380 | if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp)) | |
381 | get_acl(fname, sxp); | |
382 | #endif | |
383 | ||
16edf865 | 384 | #ifdef SUPPORT_XATTRS |
9439c0cb | 385 | if (am_root < 0) |
e107b6b1 | 386 | set_stat_xattr(fname, file, new_mode); |
4d7c8e6b WD |
387 | if (preserve_xattrs && fnamecmp) |
388 | set_xattr(fname, file, fnamecmp, sxp); | |
16edf865 WD |
389 | #endif |
390 | ||
f9998046 | 391 | if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1)) |
36119f6e WD |
392 | flags |= ATTRS_SKIP_MTIME; |
393 | if (!(flags & ATTRS_SKIP_MTIME) | |
1c3344a1 WD |
394 | && cmp_time(sxp->st.st_mtime, file->modtime) != 0) { |
395 | int ret = set_modtime(fname, file->modtime, sxp->st.st_mode); | |
c8d34657 | 396 | if (ret < 0) { |
3f0211b6 | 397 | rsyserr(FERROR_XFER, errno, "failed to set times on %s", |
d62bcc17 | 398 | full_fname(fname)); |
1c3344a1 | 399 | goto cleanup; |
667e72a1 | 400 | } |
c8d34657 WD |
401 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
402 | updated = 1; | |
0099e423 | 403 | else if (receiver_symlink_times) |
1ed56a05 | 404 | file->flags |= FLAG_TIME_FAILED; |
667e72a1 AT |
405 | } |
406 | ||
9b25ef35 WD |
407 | change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file); |
408 | change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP) | |
858d45f1 | 409 | && sxp->st.st_gid != (gid_t)F_GROUP(file); |
4f5b0756 | 410 | #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK |
2e52ba36 | 411 | if (S_ISLNK(sxp->st.st_mode)) { |
a41a1e87 | 412 | ; |
2e52ba36 | 413 | } else |
a41a1e87 | 414 | #endif |
460f6b99 | 415 | if (change_uid || change_gid) { |
62c9e6b3 | 416 | if (verbose > 2) { |
ce37eb2d WD |
417 | if (change_uid) { |
418 | rprintf(FINFO, | |
4ade505c WD |
419 | "set uid of %s from %u to %u\n", |
420 | fname, (unsigned)sxp->st.st_uid, F_OWNER(file)); | |
ce37eb2d WD |
421 | } |
422 | if (change_gid) { | |
423 | rprintf(FINFO, | |
4ade505c WD |
424 | "set gid of %s from %u to %u\n", |
425 | fname, (unsigned)sxp->st.st_gid, F_GROUP(file)); | |
ce37eb2d WD |
426 | } |
427 | } | |
87629cf2 WD |
428 | if (am_root >= 0) { |
429 | if (do_lchown(fname, | |
430 | change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid, | |
431 | change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) { | |
432 | /* We shouldn't have attempted to change uid | |
433 | * or gid unless have the privilege. */ | |
434 | rsyserr(FERROR_XFER, errno, "%s %s failed", | |
435 | change_uid ? "chown" : "chgrp", | |
436 | full_fname(fname)); | |
437 | goto cleanup; | |
438 | } | |
439 | /* A lchown had been done, so we need to re-stat if | |
440 | * the destination had the setuid or setgid bits set | |
441 | * (due to the side effect of the chown call). */ | |
442 | if (sxp->st.st_mode & (S_ISUID | S_ISGID)) { | |
443 | link_stat(fname, &sxp->st, | |
444 | keep_dirlinks && S_ISDIR(sxp->st.st_mode)); | |
445 | } | |
a5827a28 | 446 | } |
460f6b99 | 447 | updated = 1; |
667e72a1 | 448 | } |
c627d613 | 449 | |
1c3344a1 WD |
450 | #ifdef SUPPORT_ACLS |
451 | /* It's OK to call set_acl() now, even for a dir, as the generator | |
452 | * will enable owner-writability using chmod, if necessary. | |
453 | * | |
454 | * If set_acl() changes permission bits in the process of setting | |
455 | * an access ACL, it changes sxp->st.st_mode so we know whether we | |
456 | * need to chmod(). */ | |
457 | if (preserve_acls && !S_ISLNK(new_mode) && set_acl(fname, file, sxp) == 0) | |
458 | updated = 1; | |
459 | #endif | |
460 | ||
4f5b0756 | 461 | #ifdef HAVE_CHMOD |
1c3344a1 | 462 | if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) { |
9439c0cb | 463 | int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode); |
c8d34657 | 464 | if (ret < 0) { |
3f0211b6 | 465 | rsyserr(FERROR_XFER, errno, |
c8d34657 WD |
466 | "failed to set permissions on %s", |
467 | full_fname(fname)); | |
1c3344a1 | 468 | goto cleanup; |
667e72a1 | 469 | } |
c8d34657 WD |
470 | if (ret == 0) /* ret == 1 if symlink could not be set */ |
471 | updated = 1; | |
667e72a1 | 472 | } |
c627d613 | 473 | #endif |
6a7cc46c | 474 | |
36119f6e | 475 | if (verbose > 1 && flags & ATTRS_REPORT) { |
667e72a1 | 476 | if (updated) |
1925c344 | 477 | rprintf(FCLIENT, "%s\n", fname); |
667e72a1 | 478 | else |
1925c344 | 479 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
667e72a1 | 480 | } |
1c3344a1 | 481 | cleanup: |
16edf865 | 482 | if (sxp == &sx2) { |
1c3344a1 | 483 | #ifdef SUPPORT_ACLS |
16edf865 WD |
484 | if (preserve_acls) |
485 | free_acl(&sx2); | |
1c3344a1 | 486 | #endif |
16edf865 WD |
487 | #ifdef SUPPORT_XATTRS |
488 | if (preserve_xattrs) | |
489 | free_xattr(&sx2); | |
490 | #endif | |
491 | } | |
667e72a1 | 492 | return updated; |
c627d613 AT |
493 | } |
494 | ||
b8e9c234 | 495 | RETSIGTYPE sig_int(UNUSED(int val)) |
34ccb63e | 496 | { |
d5a0b483 WD |
497 | /* KLUGE: if the user hits Ctrl-C while ssh is prompting |
498 | * for a password, then our cleanup's sending of a SIGUSR1 | |
499 | * signal to all our children may kill ssh before it has a | |
500 | * chance to restore the tty settings (i.e. turn echo back | |
501 | * on). By sleeping for a short time, ssh gets a bigger | |
502 | * chance to do the right thing. If child processes are | |
503 | * not ssh waiting for a password, then this tiny delay | |
504 | * shouldn't hurt anything. */ | |
505 | msleep(400); | |
65417579 | 506 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
507 | } |
508 | ||
d8b1c923 | 509 | /* Finish off a file transfer: renaming the file and setting the file's |
83235dbc WD |
510 | * attributes (e.g. permissions, ownership, etc.). If the robust_rename() |
511 | * call is forced to copy the temp file and partialptr is both non-NULL and | |
512 | * not an absolute path, we stage the file into the partial-dir and then | |
513 | * rename it into place. This returns 1 on succcess or 0 on failure. */ | |
514 | int finish_transfer(const char *fname, const char *fnametmp, | |
515 | const char *fnamecmp, const char *partialptr, | |
516 | struct file_struct *file, int ok_to_set_time, | |
517 | int overwriting_basis) | |
c95da96a | 518 | { |
62c9e6b3 | 519 | int ret; |
83235dbc | 520 | const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL; |
62c9e6b3 | 521 | |
a3221d2a WD |
522 | if (inplace) { |
523 | if (verbose > 2) | |
45c49b52 | 524 | rprintf(FINFO, "finishing %s\n", fname); |
d8b1c923 | 525 | fnametmp = fname; |
36119f6e | 526 | goto do_set_file_attrs; |
a3221d2a WD |
527 | } |
528 | ||
f3d6d480 | 529 | if (make_backups > 0 && overwriting_basis && !make_backup(fname)) |
83235dbc | 530 | return 1; |
e484f0cc | 531 | |
ebeacb36 | 532 | /* Change permissions before putting the file into place. */ |
16edf865 | 533 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 534 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
ebeacb36 | 535 | |
c95da96a | 536 | /* move tmp file over real file */ |
45c49b52 WD |
537 | if (verbose > 2) |
538 | rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname); | |
83235dbc | 539 | ret = robust_rename(fnametmp, fname, temp_copy_name, |
d8b1c923 | 540 | file->mode & INITACCESSPERMS); |
3d061653 | 541 | if (ret < 0) { |
3f0211b6 | 542 | rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"", |
45c49b52 WD |
543 | ret == -2 ? "copy" : "rename", |
544 | full_fname(fnametmp), fname); | |
83235dbc WD |
545 | if (!partialptr || (ret == -2 && temp_copy_name) |
546 | || robust_rename(fnametmp, partialptr, NULL, | |
547 | file->mode & INITACCESSPERMS) < 0) | |
548 | do_unlink(fnametmp); | |
549 | return 0; | |
c95da96a | 550 | } |
ebeacb36 WD |
551 | if (ret == 0) { |
552 | /* The file was moved into place (not copied), so it's done. */ | |
83235dbc | 553 | return 1; |
ebeacb36 | 554 | } |
d8b1c923 WD |
555 | /* The file was copied, so tweak the perms of the copied file. If it |
556 | * was copied to partialptr, move it into its final destination. */ | |
83235dbc | 557 | fnametmp = temp_copy_name ? temp_copy_name : fname; |
d8b1c923 | 558 | |
36119f6e | 559 | do_set_file_attrs: |
16edf865 | 560 | set_file_attrs(fnametmp, file, NULL, fnamecmp, |
36119f6e | 561 | ok_to_set_time ? 0 : ATTRS_SKIP_MTIME); |
d8b1c923 | 562 | |
83235dbc | 563 | if (temp_copy_name) { |
d8b1c923 | 564 | if (do_rename(fnametmp, fname) < 0) { |
3f0211b6 | 565 | rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"", |
d8b1c923 | 566 | full_fname(fnametmp), fname); |
83235dbc WD |
567 | return 0; |
568 | } | |
569 | handle_partial_dir(temp_copy_name, PDIR_DELETE); | |
d8b1c923 | 570 | } |
83235dbc | 571 | return 1; |
c95da96a | 572 | } |
c3e5e585 | 573 | |
f3d6d480 WD |
574 | struct file_list *flist_for_ndx(int ndx) |
575 | { | |
576 | struct file_list *flist = cur_flist; | |
577 | ||
87a34ce5 | 578 | if (!flist && !(flist = first_flist)) |
f3d6d480 WD |
579 | return NULL; |
580 | ||
6755a7d7 | 581 | while (ndx < flist->ndx_start-1) { |
f3d6d480 WD |
582 | if (flist == first_flist) |
583 | return NULL; | |
584 | flist = flist->prev; | |
585 | } | |
65b4e4b2 | 586 | while (ndx >= flist->ndx_start + flist->used) { |
f3d6d480 WD |
587 | if (!(flist = flist->next)) |
588 | return NULL; | |
589 | } | |
590 | return flist; | |
591 | } | |
592 | ||
c3e5e585 WD |
593 | const char *who_am_i(void) |
594 | { | |
ed9d969c | 595 | if (am_starting_up) |
794b0a03 | 596 | return am_server ? "server" : "client"; |
ebeacb36 | 597 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
c3e5e585 | 598 | } |