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