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