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