No need to check MIN_FILECNT_LOOKAHEAD w/extra_flist_sending_enabled.
[rsync/rsync.git] / rsync.c
... / ...
CommitLineData
1/*
2 * Routines common to more than one of the rsync processes.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2009 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
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.
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 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23#include "ifuncs.h"
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
29
30extern int dry_run;
31extern int preserve_acls;
32extern int preserve_xattrs;
33extern int preserve_perms;
34extern int preserve_executability;
35extern int preserve_times;
36extern int am_root;
37extern int am_server;
38extern int am_sender;
39extern int am_generator;
40extern int am_starting_up;
41extern int allow_8bit_chars;
42extern int protocol_version;
43extern int uid_ndx;
44extern int gid_ndx;
45extern int inc_recurse;
46extern int inplace;
47extern int flist_eof;
48extern int file_old_total;
49extern int keep_dirlinks;
50extern int make_backups;
51extern struct file_list *cur_flist, *first_flist, *dir_flist;
52extern struct chmod_mode_struct *daemon_chmod_modes;
53#ifdef ICONV_OPTION
54extern char *iconv_opt;
55#endif
56
57#ifdef ICONV_CONST
58iconv_t ic_chck = (iconv_t)-1;
59# ifdef ICONV_OPTION
60iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
61# endif
62
63static const char *default_charset(void)
64{
65# if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
66 return locale_charset();
67# elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
68 return nl_langinfo(CODESET);
69# else
70 return ""; /* Works with (at the very least) gnu iconv... */
71# endif
72}
73
74void setup_iconv(void)
75{
76 const char *defset = default_charset();
77# ifdef ICONV_OPTION
78 const char *charset;
79 char *cp;
80# endif
81
82 if (!am_server && !allow_8bit_chars) {
83 /* It's OK if this fails... */
84 ic_chck = iconv_open(defset, defset);
85
86 if (DEBUG_GTE(ICONV, 2)) {
87 if (ic_chck == (iconv_t)-1) {
88 rprintf(FINFO,
89 "msg checking via isprint()"
90 " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
91 defset, defset, errno);
92 } else {
93 rprintf(FINFO,
94 "msg checking charset: %s\n",
95 defset);
96 }
97 }
98 } else
99 ic_chck = (iconv_t)-1;
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
129 if (DEBUG_GTE(ICONV, 1)) {
130 rprintf(FINFO, "[%s] charset: %s\n",
131 who_am_i(), *charset ? charset : "[LOCALE]");
132 }
133# endif
134}
135
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
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'.
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).
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
162 * EINVAL is not returned. If ICB_INIT is set, the iconv() conversion state
163 * is initialized prior to processing the characters. */
164int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
165{
166 ICONV_CONST char *ibuf;
167 size_t icnt, ocnt, opos;
168 char *obuf;
169
170 if (!out->size && flags & ICB_EXPAND_OUT)
171 alloc_xbuf(out, 1024);
172
173 if (flags & ICB_INIT)
174 iconv(ic, NULL, 0, NULL, 0);
175
176 ibuf = in->buf + in->pos;
177 icnt = in->len;
178
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;
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 {
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) {
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:
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
238 in->len = icnt;
239 in->pos = ibuf - in->buf;
240
241 return errno ? -1 : 0;
242}
243#endif
244
245void send_protected_args(int fd, char *args[])
246{
247 int i;
248#ifdef ICONV_OPTION
249 int convert = ic_send != (iconv_t)-1;
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 */
258 if (DEBUG_GTE(CMD, 1))
259 print_child_argv("protected args:", args + i + 1);
260 do {
261 if (!args[i][0])
262 write_buf(fd, ".", 2);
263#ifdef ICONV_OPTION
264 else if (convert) {
265 INIT_XBUF_STRLEN(inbuf, args[i]);
266 iconvbufs(ic_send, &inbuf, &outbuf,
267 ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT);
268 outbuf.buf[outbuf.len] = '\0';
269 write_buf(fd, outbuf.buf, outbuf.len + 1);
270 outbuf.len = 0;
271 }
272#endif
273 else
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
284int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, uchar *type_ptr,
285 char *buf, int *len_ptr)
286{
287 int len, iflags = 0;
288 struct file_list *flist;
289 uchar fnamecmp_type = FNAMECMP_FNAME;
290 int ndx;
291
292 read_loop:
293 while (1) {
294 ndx = read_ndx(f_in);
295
296 if (ndx >= 0)
297 break;
298 if (ndx == NDX_DONE)
299 return ndx;
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 }
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 }
317 if (ndx == NDX_FLIST_EOF) {
318 flist_eof = 1;
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);
322 continue;
323 }
324 ndx = NDX_FLIST_OFFSET - ndx;
325 if (ndx < 0 || ndx >= dir_flist->used) {
326 ndx = NDX_FLIST_OFFSET - ndx;
327 rprintf(FERROR,
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());
332 exit_cleanup(RERR_PROTOCOL);
333 }
334
335 if (DEBUG_GTE(FLIST, 2)) {
336 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
337 who_am_i(), ndx);
338 }
339 /* Send all the data we read for this flist to the generator. */
340 start_flist_forward(ndx);
341 flist = recv_file_list(f_in);
342 flist->parent_ndx = ndx;
343 stop_flist_forward();
344 }
345
346 iflags = protocol_version >= 29 ? read_shortint(f_in)
347 : ITEM_TRANSFER | ITEM_MISSING_DATA;
348
349 /* Honor the old-style keep-alive indicator. */
350 if (protocol_version < 30
351 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
352 if (am_sender)
353 maybe_send_keepalive();
354 goto read_loop;
355 }
356
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 }
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) {
381 int i = ndx - cur_flist->ndx_start;
382 if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
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 }
388 }
389
390 *iflag_ptr = iflags;
391 return ndx;
392}
393
394/*
395 free a sums struct
396 */
397void free_sums(struct sum_struct *s)
398{
399 if (s->sums) free(s->sums);
400 free(s);
401}
402
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. */
405mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
406 int exists)
407{
408 int new_mode;
409 /* If the file already exists, we'll return the local permissions,
410 * possibly tweaked by the --executability option. */
411 if (exists) {
412 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
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))
418 new_mode &= ~0111;
419 else if (!(stat_mode & 0111))
420 new_mode |= (new_mode & 0444) >> 2;
421 }
422 } else {
423 /* Apply destination default permissions and turn
424 * off special permissions. */
425 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
426 }
427 return new_mode;
428}
429
430int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
431 const char *fnamecmp, int flags)
432{
433 int updated = 0;
434 stat_x sx2;
435 int change_uid, change_gid;
436 mode_t new_mode = file->mode;
437 int inherit;
438
439 if (!sxp) {
440 if (dry_run)
441 return 1;
442 if (link_stat(fname, &sx2.st, 0) < 0) {
443 rsyserr(FERROR_XFER, errno, "stat %s failed",
444 full_fname(fname));
445 return 0;
446 }
447 init_stat_x(&sx2);
448 sxp = &sx2;
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;
457 }
458
459 if (daemon_chmod_modes && !S_ISLNK(new_mode))
460 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
461
462#ifdef SUPPORT_ACLS
463 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
464 get_acl(fname, sxp);
465#endif
466
467#ifdef SUPPORT_XATTRS
468 if (am_root < 0)
469 set_stat_xattr(fname, file, new_mode);
470 if (preserve_xattrs && fnamecmp)
471 set_xattr(fname, file, fnamecmp, sxp);
472#endif
473
474 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1))
475 flags |= ATTRS_SKIP_MTIME;
476 if (!(flags & ATTRS_SKIP_MTIME)
477 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
478 int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode);
479 if (ret < 0) {
480 rsyserr(FERROR_XFER, errno, "failed to set times on %s",
481 full_fname(fname));
482 goto cleanup;
483 }
484 if (ret == 0) /* ret == 1 if symlink could not be set */
485 updated = 1;
486 else
487 file->flags |= FLAG_TIME_FAILED;
488 }
489
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)
492 && sxp->st.st_gid != (gid_t)F_GROUP(file);
493#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
494 if (S_ISLNK(sxp->st.st_mode)) {
495 ;
496 } else
497#endif
498 if (change_uid || change_gid) {
499 if (DEBUG_GTE(OWN, 1)) {
500 if (change_uid) {
501 rprintf(FINFO,
502 "set uid of %s from %u to %u\n",
503 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
504 }
505 if (change_gid) {
506 rprintf(FINFO,
507 "set gid of %s from %u to %u\n",
508 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
509 }
510 }
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 }
529 }
530 updated = 1;
531 }
532
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(). */
540 if (preserve_acls && !S_ISLNK(new_mode)) {
541 if (set_acl(fname, file, sxp, new_mode) > 0)
542 updated = 1;
543 }
544#endif
545
546#ifdef HAVE_CHMOD
547 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
548 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
549 if (ret < 0) {
550 rsyserr(FERROR_XFER, errno,
551 "failed to set permissions on %s",
552 full_fname(fname));
553 goto cleanup;
554 }
555 if (ret == 0) /* ret == 1 if symlink could not be set */
556 updated = 1;
557 }
558#endif
559
560 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
561 if (updated)
562 rprintf(FCLIENT, "%s\n", fname);
563 else
564 rprintf(FCLIENT, "%s is uptodate\n", fname);
565 }
566 cleanup:
567 if (sxp == &sx2) {
568#ifdef SUPPORT_ACLS
569 if (preserve_acls)
570 free_acl(&sx2);
571#endif
572#ifdef SUPPORT_XATTRS
573 if (preserve_xattrs)
574 free_xattr(&sx2);
575#endif
576 }
577 return updated;
578}
579
580RETSIGTYPE sig_int(UNUSED(int val))
581{
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);
591 exit_cleanup(RERR_SIGNAL);
592}
593
594/* Finish off a file transfer: renaming the file and setting the file's
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)
603{
604 int ret;
605 const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
606
607 if (inplace) {
608 if (DEBUG_GTE(RECV, 1))
609 rprintf(FINFO, "finishing %s\n", fname);
610 fnametmp = fname;
611 goto do_set_file_attrs;
612 }
613
614 if (make_backups > 0 && overwriting_basis) {
615 int ok = make_backup(fname, False);
616 if (!ok)
617 return 1;
618 if (ok == 1 && fnamecmp == fname)
619 fnamecmp = get_backup_name(fname);
620 }
621
622 /* Change permissions before putting the file into place. */
623 set_file_attrs(fnametmp, file, NULL, fnamecmp,
624 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
625
626 /* move tmp file over real file */
627 if (DEBUG_GTE(RECV, 1))
628 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
629 ret = robust_rename(fnametmp, fname, temp_copy_name,
630 file->mode & INITACCESSPERMS);
631 if (ret < 0) {
632 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
633 ret == -2 ? "copy" : "rename",
634 full_fname(fnametmp), fname);
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;
640 }
641 if (ret == 0) {
642 /* The file was moved into place (not copied), so it's done. */
643 return 1;
644 }
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. */
647 fnametmp = temp_copy_name ? temp_copy_name : fname;
648
649 do_set_file_attrs:
650 set_file_attrs(fnametmp, file, NULL, fnamecmp,
651 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
652
653 if (temp_copy_name) {
654 if (do_rename(fnametmp, fname) < 0) {
655 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"",
656 full_fname(fnametmp), fname);
657 return 0;
658 }
659 handle_partial_dir(temp_copy_name, PDIR_DELETE);
660 }
661 return 1;
662}
663
664struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc)
665{
666 struct file_list *flist = cur_flist;
667
668 if (!flist && !(flist = first_flist))
669 goto not_found;
670
671 while (ndx < flist->ndx_start-1) {
672 if (flist == first_flist)
673 goto not_found;
674 flist = flist->prev;
675 }
676 while (ndx >= flist->ndx_start + flist->used) {
677 if (!(flist = flist->next))
678 goto not_found;
679 }
680 return flist;
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;
698}
699
700const char *who_am_i(void)
701{
702 if (am_starting_up)
703 return am_server ? "server" : "client";
704 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
705}