Add --omit-link-times and use CAN_SET_SYMLINK_TIMES less.
[rsync/rsync.git] / rsync.c
CommitLineData
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
b3bf9b9d 6 * Copyright (C) 2003-2009 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 30extern int dry_run;
1c3344a1 31extern int preserve_acls;
16edf865 32extern int preserve_xattrs;
9b499e95 33extern int preserve_perms;
81284832 34extern int preserve_executability;
2f03f956 35extern int preserve_times;
7b8356d0 36extern int am_root;
794b0a03 37extern int am_server;
c3e5e585 38extern int am_sender;
82b2a31a 39extern int am_receiver;
c3e5e585 40extern int am_generator;
ed9d969c 41extern int am_starting_up;
e0f4a661 42extern int allow_8bit_chars;
b675ba6f 43extern int protocol_version;
9b25ef35
WD
44extern int uid_ndx;
45extern int gid_ndx;
3ea6e0e7 46extern int inc_recurse;
c786a7ae 47extern int inplace;
f3d6d480 48extern int flist_eof;
ce827c3e 49extern int file_old_total;
81b07870 50extern int keep_dirlinks;
2f03f956 51extern int make_backups;
f3d6d480 52extern struct file_list *cur_flist, *first_flist, *dir_flist;
94d3725c 53extern struct chmod_mode_struct *daemon_chmod_modes;
332cf6df
WD
54#ifdef ICONV_OPTION
55extern char *iconv_opt;
56#endif
fe055c71 57
332cf6df 58#ifdef ICONV_CONST
ceccbacc 59iconv_t ic_chck = (iconv_t)-1;
2ac97930 60# ifdef ICONV_OPTION
332cf6df 61iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
2ac97930 62# endif
ceccbacc 63
2a62f5ee 64static 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 75void 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) {
e0f4a661
WD
84 /* It's OK if this fails... */
85 ic_chck = iconv_open(defset, defset);
ceccbacc 86
c9f540f3 87 if (DEBUG_GTE(ICONV, 2)) {
e0f4a661
WD
88 if (ic_chck == (iconv_t)-1) {
89 rprintf(FINFO,
c9f540f3
WD
90 "msg checking via isprint()"
91 " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
e0f4a661
WD
92 defset, defset, errno);
93 } else {
94 rprintf(FINFO,
c9f540f3
WD
95 "msg checking charset: %s\n",
96 defset);
e0f4a661 97 }
7fc87d2d 98 }
c9f540f3
WD
99 } else
100 ic_chck = (iconv_t)-1;
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
c9f540f3
WD
130 if (DEBUG_GTE(ICONV, 1)) {
131 rprintf(FINFO, "[%s] charset: %s\n",
132 who_am_i(), *charset ? charset : "[LOCALE]");
332cf6df
WD
133 }
134# endif
ceccbacc 135}
2ac97930 136
907e6a32
WD
137/* This function converts the chars in the "in" xbuf into characters in the
138 * "out" xbuf. The ".len" chars of the "in" xbuf is used starting from its
139 * ".pos". The ".size" of the "out" xbuf restricts how many characters can
140 * be stored, starting at its ".pos+.len" position. Note that the last byte
141 * of the "out" xbuf is not used, which reserves space for a trailing '\0'
142 * (though it is up to the caller to store a trailing '\0', as needed).
143 *
2ac97930
WD
144 * We return a 0 on success or a -1 on error. An error also sets errno to
145 * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
907e6a32
WD
146 * The "in" xbuf is altered to update ".pos" and ".len". The "out" xbuf has
147 * data appended, and its ".len" incremented (see below for a ".size" note).
148 *
149 * If ICB_CIRCULAR_OUT is set in "flags", the chars going into the "out" xbuf
150 * can wrap around to the start, and the xbuf may have its ".size" reduced
151 * (presumably by 1 byte) if the iconv code doesn't have space to store a
152 * multi-byte character at the physical end of the ".buf" (though no reducing
d03c0b1e 153 * happens if ".pos" is <= 1, since there is no room to wrap around).
907e6a32
WD
154 *
155 * If ICB_EXPAND_OUT is set in "flags", the "out" xbuf will be allocated if
156 * empty, and (as long as ICB_CIRCULAR_OUT is not set) expanded if too small.
157 * This prevents the return of E2BIG (except for a circular xbuf).
158 *
159 * If ICB_INCLUDE_BAD is set in "flags", any badly-encoded chars are included
160 * verbatim in the "out" xbuf, so EILSEQ will not be returned.
161 *
162 * If ICB_INCLUDE_INCOMPLETE is set in "flags", any incomplete multi-byte
163 * chars are included, which ensures that EINVAL is not returned.
164 *
165 * If ICB_INIT is set, the iconv() conversion state is initialized prior to
166 * processing the characters. */
2ac97930
WD
167int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
168{
169 ICONV_CONST char *ibuf;
20caffd2 170 size_t icnt, ocnt, opos;
2ac97930
WD
171 char *obuf;
172
907e6a32
WD
173 if (!out->size && flags & ICB_EXPAND_OUT) {
174 size_t siz = ROUND_UP_1024(in->len * 2);
175 alloc_xbuf(out, siz);
176 } else if (out->len+1 >= out->size) {
177 /* There is no room to even start storing data. */
178 if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
179 errno = E2BIG;
180 return -1;
181 }
182 realloc_xbuf(out, out->size + ROUND_UP_1024(in->len * 2));
183 }
2ac97930 184
d8a7290f 185 if (flags & ICB_INIT)
2ac97930
WD
186 iconv(ic, NULL, 0, NULL, 0);
187
188 ibuf = in->buf + in->pos;
189 icnt = in->len;
190
20caffd2
WD
191 opos = out->pos + out->len;
192 if (flags & ICB_CIRCULAR_OUT) {
193 if (opos >= out->size) {
194 opos -= out->size;
907e6a32
WD
195 /* We know that out->pos is not 0 due to the "no room" check
196 * above, so this can't go "negative". */
20caffd2
WD
197 ocnt = out->pos - opos - 1;
198 } else {
907e6a32
WD
199 /* Allow the use of all bytes to the physical end of the buffer
200 * unless pos is 0, in which case we reserve our trailing '\0'. */
201 ocnt = out->size - opos - (out->pos ? 0 : 1);
20caffd2
WD
202 }
203 } else
204 ocnt = out->size - opos - 1;
205 obuf = out->buf + opos;
2ac97930
WD
206
207 while (icnt) {
208 while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
209 if (errno == EINTR)
210 continue;
211 if (errno == EINVAL) {
212 if (!(flags & ICB_INCLUDE_INCOMPLETE))
213 goto finish;
214 } else if (errno == EILSEQ) {
215 if (!(flags & ICB_INCLUDE_BAD))
216 goto finish;
907e6a32
WD
217 } else if (errno == E2BIG) {
218 size_t siz;
20caffd2 219 opos = obuf - out->buf;
907e6a32
WD
220 if (flags & ICB_CIRCULAR_OUT && out->pos > 1 && opos > out->pos) {
221 /* We are in a divided circular buffer at the physical
222 * end with room to wrap to the start. If iconv() refused
223 * to use one or more trailing bytes in the buffer, we
224 * set the size to ignore the unused bytes. */
225 if (opos < out->size)
226 reduce_iobuf_size(out, opos);
227 obuf = out->buf;
228 ocnt = out->pos - 1;
229 continue;
20caffd2
WD
230 }
231 if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
2ac97930
WD
232 errno = E2BIG;
233 goto finish;
234 }
907e6a32
WD
235 siz = ROUND_UP_1024(in->len * 2);
236 realloc_xbuf(out, out->size + siz);
2ac97930 237 obuf = out->buf + opos;
907e6a32 238 ocnt += siz;
2ac97930 239 continue;
907e6a32
WD
240 } else {
241 rsyserr(FERROR, errno, "unexpected error from iconv()");
242 exit_cleanup(RERR_UNSUPPORTED);
2ac97930
WD
243 }
244 *obuf++ = *ibuf++;
245 ocnt--, icnt--;
246 }
247 }
248
249 errno = 0;
250
251 finish:
20caffd2 252 opos = obuf - out->buf;
907e6a32
WD
253 if (flags & ICB_CIRCULAR_OUT && opos < out->pos)
254 opos += out->size;
20caffd2
WD
255 out->len = opos - out->pos;
256
2ac97930
WD
257 in->len = icnt;
258 in->pos = ibuf - in->buf;
2ac97930
WD
259
260 return errno ? -1 : 0;
261}
ceccbacc 262#endif
fe055c71 263
53936ef9
WD
264void send_protected_args(int fd, char *args[])
265{
7abcfd85 266 int i;
53936ef9 267#ifdef ICONV_OPTION
7abcfd85 268 int convert = ic_send != (iconv_t)-1;
53936ef9
WD
269 xbuf outbuf, inbuf;
270
271 if (convert)
272 alloc_xbuf(&outbuf, 1024);
273#endif
274
275 for (i = 0; args[i]; i++) {} /* find first NULL */
276 args[i] = "rsync"; /* set a new arg0 */
951e826b 277 if (DEBUG_GTE(CMD, 1))
53936ef9
WD
278 print_child_argv("protected args:", args + i + 1);
279 do {
08b7c3ed
WD
280 if (!args[i][0])
281 write_buf(fd, ".", 2);
53936ef9 282#ifdef ICONV_OPTION
08b7c3ed 283 else if (convert) {
53936ef9
WD
284 INIT_XBUF_STRLEN(inbuf, args[i]);
285 iconvbufs(ic_send, &inbuf, &outbuf,
d8a7290f 286 ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT);
53936ef9
WD
287 outbuf.buf[outbuf.len] = '\0';
288 write_buf(fd, outbuf.buf, outbuf.len + 1);
289 outbuf.len = 0;
08b7c3ed 290 }
53936ef9 291#endif
08b7c3ed 292 else
53936ef9
WD
293 write_buf(fd, args[i], strlen(args[i]) + 1);
294 } while (args[++i]);
295 write_byte(fd, 0);
296
297#ifdef ICONV_OPTION
298 if (convert)
299 free(outbuf.buf);
300#endif
301}
302
20caffd2 303int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, uchar *type_ptr,
16edf865 304 char *buf, int *len_ptr)
d1c178dd 305{
f3d6d480
WD
306 int len, iflags = 0;
307 struct file_list *flist;
d1c178dd 308 uchar fnamecmp_type = FNAMECMP_FNAME;
951e826b 309 int ndx;
f3d6d480
WD
310
311 read_loop:
312 while (1) {
9ae7a2cd 313 ndx = read_ndx(f_in);
f3d6d480 314
8b3e6052 315 if (ndx >= 0)
f3d6d480
WD
316 break;
317 if (ndx == NDX_DONE)
318 return ndx;
20caffd2
WD
319 if (ndx == NDX_DEL_STATS) {
320 read_del_stats(f_in);
321 if (am_sender && am_server)
322 write_del_stats(f_out);
323 continue;
324 }
e982d591
WD
325 if (!inc_recurse || am_sender) {
326 int last;
327 if (first_flist)
328 last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
329 else
330 last = -1;
331 rprintf(FERROR,
332 "Invalid file index: %d (%d - %d) [%s]\n",
333 ndx, NDX_DONE, last, who_am_i());
334 exit_cleanup(RERR_PROTOCOL);
335 }
f3d6d480
WD
336 if (ndx == NDX_FLIST_EOF) {
337 flist_eof = 1;
20caffd2
WD
338 if (DEBUG_GTE(FLIST, 3))
339 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
340 write_int(f_out, NDX_FLIST_EOF);
f3d6d480
WD
341 continue;
342 }
343 ndx = NDX_FLIST_OFFSET - ndx;
9decb4d2 344 if (ndx < 0 || ndx >= dir_flist->used) {
f3d6d480
WD
345 ndx = NDX_FLIST_OFFSET - ndx;
346 rprintf(FERROR,
e982d591
WD
347 "Invalid dir index: %d (%d - %d) [%s]\n",
348 ndx, NDX_FLIST_OFFSET,
349 NDX_FLIST_OFFSET - dir_flist->used + 1,
350 who_am_i());
f3d6d480
WD
351 exit_cleanup(RERR_PROTOCOL);
352 }
f3d6d480 353
951e826b 354 if (DEBUG_GTE(FLIST, 2)) {
1faa1a6d
WD
355 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
356 who_am_i(), ndx);
357 }
20caffd2
WD
358 /* Send all the data we read for this flist to the generator. */
359 start_flist_forward(ndx);
f3d6d480
WD
360 flist = recv_file_list(f_in);
361 flist->parent_ndx = ndx;
362 stop_flist_forward();
f3d6d480
WD
363 }
364
365 iflags = protocol_version >= 29 ? read_shortint(f_in)
d1c178dd
WD
366 : ITEM_TRANSFER | ITEM_MISSING_DATA;
367
92d02148
WD
368 /* Support the protocol-29 keep-alive style. */
369 if (protocol_version < 30 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
f3d6d480 370 if (am_sender)
05c36015 371 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
f3d6d480
WD
372 goto read_loop;
373 }
374
ce827c3e
WD
375 flist = flist_for_ndx(ndx, "read_ndx_and_attrs");
376 if (flist != cur_flist) {
377 cur_flist = flist;
378 if (am_sender) {
379 file_old_total = cur_flist->used;
380 for (flist = first_flist; flist != cur_flist; flist = flist->next)
381 file_old_total += flist->used;
382 }
383 }
d1c178dd
WD
384
385 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
386 fnamecmp_type = read_byte(f_in);
387 *type_ptr = fnamecmp_type;
388
389 if (iflags & ITEM_XNAME_FOLLOWS) {
390 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
391 exit_cleanup(RERR_PROTOCOL);
392 } else {
393 *buf = '\0';
394 len = -1;
395 }
396 *len_ptr = len;
397
398 if (iflags & ITEM_TRANSFER) {
f3d6d480 399 int i = ndx - cur_flist->ndx_start;
6755a7d7 400 if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
d1c178dd
WD
401 rprintf(FERROR,
402 "received request to transfer non-regular file: %d [%s]\n",
403 ndx, who_am_i());
404 exit_cleanup(RERR_PROTOCOL);
405 }
d1c178dd
WD
406 }
407
f3d6d480
WD
408 *iflag_ptr = iflags;
409 return ndx;
d1c178dd
WD
410}
411
c627d613
AT
412/*
413 free a sums struct
414 */
2f03f956 415void free_sums(struct sum_struct *s)
c627d613 416{
298c10d5
AT
417 if (s->sums) free(s->sums);
418 free(s);
c627d613
AT
419}
420
81284832
WD
421/* This is only called when we aren't preserving permissions. Figure out what
422 * the permissions should be and return them merged back into the mode. */
1c3344a1
WD
423mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
424 int exists)
81284832 425{
67f8a41b 426 int new_mode;
9b499e95 427 /* If the file already exists, we'll return the local permissions,
81284832
WD
428 * possibly tweaked by the --executability option. */
429 if (exists) {
67f8a41b 430 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
81284832
WD
431 if (preserve_executability && S_ISREG(flist_mode)) {
432 /* If the source file is executable, grant execute
433 * rights to everyone who can read, but ONLY if the
434 * file isn't already executable. */
435 if (!(flist_mode & 0111))
67f8a41b
WD
436 new_mode &= ~0111;
437 else if (!(stat_mode & 0111))
438 new_mode |= (new_mode & 0444) >> 2;
81284832 439 }
67f8a41b 440 } else {
1c3344a1
WD
441 /* Apply destination default permissions and turn
442 * off special permissions. */
443 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
67f8a41b 444 }
67f8a41b 445 return new_mode;
81284832
WD
446}
447
13710874 448int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
16edf865 449 const char *fnamecmp, int flags)
c627d613 450{
667e72a1 451 int updated = 0;
13710874 452 stat_x sx2;
460f6b99 453 int change_uid, change_gid;
519d55a9 454 mode_t new_mode = file->mode;
2dc7b91d 455 int inherit;
c627d613 456
1c3344a1 457 if (!sxp) {
f227ffe4
WD
458 if (dry_run)
459 return 1;
1c3344a1 460 if (link_stat(fname, &sx2.st, 0) < 0) {
3f0211b6 461 rsyserr(FERROR_XFER, errno, "stat %s failed",
d62bcc17 462 full_fname(fname));
667e72a1
AT
463 return 0;
464 }
09ca0d15 465 init_stat_x(&sx2);
1c3344a1 466 sxp = &sx2;
2dc7b91d
WD
467 inherit = !preserve_perms;
468 } else
469 inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED;
470
471 if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) {
472 /* We just created this directory and its setgid
473 * bit is on, so make sure it stays on. */
474 new_mode |= S_ISGID;
667e72a1 475 }
c627d613 476
e107b6b1
WD
477 if (daemon_chmod_modes && !S_ISLNK(new_mode))
478 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
479
1c3344a1
WD
480#ifdef SUPPORT_ACLS
481 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
482 get_acl(fname, sxp);
483#endif
484
16edf865 485#ifdef SUPPORT_XATTRS
9439c0cb 486 if (am_root < 0)
e107b6b1 487 set_stat_xattr(fname, file, new_mode);
4d7c8e6b
WD
488 if (preserve_xattrs && fnamecmp)
489 set_xattr(fname, file, fnamecmp, sxp);
16edf865
WD
490#endif
491
2624e005
WD
492 if (!preserve_times
493 || (!(preserve_times & PRESERVE_DIR_TIMES) && S_ISDIR(sxp->st.st_mode))
494 || (!(preserve_times & PRESERVE_LINK_TIMES) && S_ISLNK(sxp->st.st_mode)))
36119f6e
WD
495 flags |= ATTRS_SKIP_MTIME;
496 if (!(flags & ATTRS_SKIP_MTIME)
1c3344a1 497 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
2624e005 498 int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), file->mode);
c8d34657 499 if (ret < 0) {
3f0211b6 500 rsyserr(FERROR_XFER, errno, "failed to set times on %s",
d62bcc17 501 full_fname(fname));
1c3344a1 502 goto cleanup;
667e72a1 503 }
c8d34657
WD
504 if (ret == 0) /* ret == 1 if symlink could not be set */
505 updated = 1;
ee39281d 506 else
1ed56a05 507 file->flags |= FLAG_TIME_FAILED;
667e72a1
AT
508 }
509
9b25ef35
WD
510 change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
511 change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
858d45f1 512 && sxp->st.st_gid != (gid_t)F_GROUP(file);
4f5b0756 513#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
2e52ba36 514 if (S_ISLNK(sxp->st.st_mode)) {
a41a1e87 515 ;
2e52ba36 516 } else
a41a1e87 517#endif
460f6b99 518 if (change_uid || change_gid) {
951e826b 519 if (DEBUG_GTE(OWN, 1)) {
ce37eb2d
WD
520 if (change_uid) {
521 rprintf(FINFO,
4ade505c
WD
522 "set uid of %s from %u to %u\n",
523 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
ce37eb2d
WD
524 }
525 if (change_gid) {
526 rprintf(FINFO,
4ade505c
WD
527 "set gid of %s from %u to %u\n",
528 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
ce37eb2d
WD
529 }
530 }
87629cf2
WD
531 if (am_root >= 0) {
532 if (do_lchown(fname,
533 change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid,
534 change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) {
535 /* We shouldn't have attempted to change uid
536 * or gid unless have the privilege. */
537 rsyserr(FERROR_XFER, errno, "%s %s failed",
538 change_uid ? "chown" : "chgrp",
539 full_fname(fname));
540 goto cleanup;
541 }
542 /* A lchown had been done, so we need to re-stat if
543 * the destination had the setuid or setgid bits set
544 * (due to the side effect of the chown call). */
545 if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
546 link_stat(fname, &sxp->st,
547 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
548 }
a5827a28 549 }
460f6b99 550 updated = 1;
667e72a1 551 }
c627d613 552
1c3344a1
WD
553#ifdef SUPPORT_ACLS
554 /* It's OK to call set_acl() now, even for a dir, as the generator
555 * will enable owner-writability using chmod, if necessary.
556 *
557 * If set_acl() changes permission bits in the process of setting
558 * an access ACL, it changes sxp->st.st_mode so we know whether we
559 * need to chmod(). */
ee1c00fe
WD
560 if (preserve_acls && !S_ISLNK(new_mode)) {
561 if (set_acl(fname, file, sxp, new_mode) > 0)
562 updated = 1;
563 }
1c3344a1
WD
564#endif
565
4f5b0756 566#ifdef HAVE_CHMOD
1c3344a1 567 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
9439c0cb 568 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
c8d34657 569 if (ret < 0) {
3f0211b6 570 rsyserr(FERROR_XFER, errno,
c8d34657
WD
571 "failed to set permissions on %s",
572 full_fname(fname));
1c3344a1 573 goto cleanup;
667e72a1 574 }
c8d34657
WD
575 if (ret == 0) /* ret == 1 if symlink could not be set */
576 updated = 1;
667e72a1 577 }
c627d613 578#endif
6a7cc46c 579
951e826b 580 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
667e72a1 581 if (updated)
1925c344 582 rprintf(FCLIENT, "%s\n", fname);
667e72a1 583 else
1925c344 584 rprintf(FCLIENT, "%s is uptodate\n", fname);
667e72a1 585 }
1c3344a1 586 cleanup:
16edf865 587 if (sxp == &sx2) {
1c3344a1 588#ifdef SUPPORT_ACLS
16edf865
WD
589 if (preserve_acls)
590 free_acl(&sx2);
1c3344a1 591#endif
16edf865
WD
592#ifdef SUPPORT_XATTRS
593 if (preserve_xattrs)
594 free_xattr(&sx2);
595#endif
596 }
667e72a1 597 return updated;
c627d613
AT
598}
599
b8e9c234 600RETSIGTYPE sig_int(UNUSED(int val))
34ccb63e 601{
d5a0b483
WD
602 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
603 * for a password, then our cleanup's sending of a SIGUSR1
604 * signal to all our children may kill ssh before it has a
605 * chance to restore the tty settings (i.e. turn echo back
606 * on). By sleeping for a short time, ssh gets a bigger
607 * chance to do the right thing. If child processes are
608 * not ssh waiting for a password, then this tiny delay
609 * shouldn't hurt anything. */
610 msleep(400);
65417579 611 exit_cleanup(RERR_SIGNAL);
c627d613
AT
612}
613
d8b1c923 614/* Finish off a file transfer: renaming the file and setting the file's
83235dbc
WD
615 * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
616 * call is forced to copy the temp file and partialptr is both non-NULL and
617 * not an absolute path, we stage the file into the partial-dir and then
618 * rename it into place. This returns 1 on succcess or 0 on failure. */
619int finish_transfer(const char *fname, const char *fnametmp,
620 const char *fnamecmp, const char *partialptr,
621 struct file_struct *file, int ok_to_set_time,
622 int overwriting_basis)
c95da96a 623{
62c9e6b3 624 int ret;
83235dbc 625 const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
62c9e6b3 626
a3221d2a 627 if (inplace) {
951e826b 628 if (DEBUG_GTE(RECV, 1))
45c49b52 629 rprintf(FINFO, "finishing %s\n", fname);
d8b1c923 630 fnametmp = fname;
36119f6e 631 goto do_set_file_attrs;
a3221d2a
WD
632 }
633
e9489cd6 634 if (make_backups > 0 && overwriting_basis) {
21cddef2
WD
635 int ok = make_backup(fname, False);
636 if (!ok)
e9489cd6 637 return 1;
21cddef2 638 if (ok == 1 && fnamecmp == fname)
4337eeb7 639 fnamecmp = get_backup_name(fname);
e9489cd6 640 }
e484f0cc 641
ebeacb36 642 /* Change permissions before putting the file into place. */
16edf865 643 set_file_attrs(fnametmp, file, NULL, fnamecmp,
36119f6e 644 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
ebeacb36 645
c95da96a 646 /* move tmp file over real file */
951e826b 647 if (DEBUG_GTE(RECV, 1))
45c49b52 648 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
3b22184d 649 ret = robust_rename(fnametmp, fname, temp_copy_name, file->mode);
3d061653 650 if (ret < 0) {
3f0211b6 651 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
45c49b52
WD
652 ret == -2 ? "copy" : "rename",
653 full_fname(fnametmp), fname);
83235dbc 654 if (!partialptr || (ret == -2 && temp_copy_name)
3b22184d 655 || robust_rename(fnametmp, partialptr, NULL, file->mode) < 0)
83235dbc
WD
656 do_unlink(fnametmp);
657 return 0;
c95da96a 658 }
ebeacb36
WD
659 if (ret == 0) {
660 /* The file was moved into place (not copied), so it's done. */
83235dbc 661 return 1;
ebeacb36 662 }
d8b1c923
WD
663 /* The file was copied, so tweak the perms of the copied file. If it
664 * was copied to partialptr, move it into its final destination. */
83235dbc 665 fnametmp = temp_copy_name ? temp_copy_name : fname;
d8b1c923 666
36119f6e 667 do_set_file_attrs:
16edf865 668 set_file_attrs(fnametmp, file, NULL, fnamecmp,
36119f6e 669 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
d8b1c923 670
83235dbc 671 if (temp_copy_name) {
d8b1c923 672 if (do_rename(fnametmp, fname) < 0) {
3f0211b6 673 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"",
d8b1c923 674 full_fname(fnametmp), fname);
83235dbc
WD
675 return 0;
676 }
677 handle_partial_dir(temp_copy_name, PDIR_DELETE);
d8b1c923 678 }
83235dbc 679 return 1;
c95da96a 680}
c3e5e585 681
e982d591 682struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc)
f3d6d480
WD
683{
684 struct file_list *flist = cur_flist;
685
87a34ce5 686 if (!flist && !(flist = first_flist))
e982d591 687 goto not_found;
f3d6d480 688
6755a7d7 689 while (ndx < flist->ndx_start-1) {
f3d6d480 690 if (flist == first_flist)
e982d591 691 goto not_found;
f3d6d480
WD
692 flist = flist->prev;
693 }
65b4e4b2 694 while (ndx >= flist->ndx_start + flist->used) {
f3d6d480 695 if (!(flist = flist->next))
e982d591 696 goto not_found;
f3d6d480
WD
697 }
698 return flist;
e982d591
WD
699
700 not_found:
701 if (fatal_error_loc) {
702 int first, last;
703 if (first_flist) {
704 first = first_flist->ndx_start - 1;
705 last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
706 } else {
707 first = 0;
708 last = -1;
709 }
710 rprintf(FERROR,
711 "File-list index %d not in %d - %d (%s) [%s]\n",
712 ndx, first, last, fatal_error_loc, who_am_i());
713 exit_cleanup(RERR_PROTOCOL);
714 }
715 return NULL;
f3d6d480
WD
716}
717
c3e5e585
WD
718const char *who_am_i(void)
719{
ed9d969c 720 if (am_starting_up)
794b0a03 721 return am_server ? "server" : "client";
82b2a31a
WD
722 return am_sender ? "sender"
723 : am_generator ? "generator"
724 : am_receiver ? "receiver"
725 : "Receiver"; /* pre-forked receiver */
c3e5e585 726}