2 * Routines common to more than one of the rsync processes.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2020 Wayne Davison
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.
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.
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.
24 #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
25 #include <libcharset.h>
26 #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
31 extern int preserve_acls
;
32 extern int preserve_xattrs
;
33 extern int preserve_perms
;
34 extern int preserve_executability
;
35 extern int preserve_times
;
40 extern int am_receiver
;
41 extern int am_generator
;
42 extern int am_starting_up
;
43 extern int allow_8bit_chars
;
44 extern int protocol_version
;
45 extern int got_kill_signal
;
46 extern int called_from_signal_handler
;
47 extern int inc_recurse
;
50 extern int file_old_total
;
51 extern int keep_dirlinks
;
52 extern int make_backups
;
53 extern int sanitize_paths
;
54 extern struct file_list
*cur_flist
, *first_flist
, *dir_flist
;
55 extern struct chmod_mode_struct
*daemon_chmod_modes
;
57 extern char *iconv_opt
;
60 #define UPDATED_OWNER (1<<0)
61 #define UPDATED_GROUP (1<<1)
62 #define UPDATED_MTIME (1<<2)
63 #define UPDATED_ATIME (1<<3)
64 #define UPDATED_ACLS (1<<4)
65 #define UPDATED_MODE (1<<5)
67 #define UPDATED_TIMES (UPDATED_MTIME|UPDATED_ATIME)
70 iconv_t ic_chck
= (iconv_t
)-1;
72 iconv_t ic_send
= (iconv_t
)-1, ic_recv
= (iconv_t
)-1;
75 static const char *default_charset(void)
77 # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
78 return locale_charset();
79 # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
80 return nl_langinfo(CODESET
);
82 return ""; /* Works with (at the very least) gnu iconv... */
86 void setup_iconv(void)
88 const char *defset
= default_charset();
94 if (!am_server
&& !allow_8bit_chars
) {
95 /* It's OK if this fails... */
96 ic_chck
= iconv_open(defset
, defset
);
98 if (DEBUG_GTE(ICONV
, 2)) {
99 if (ic_chck
== (iconv_t
)-1) {
101 "msg checking via isprint()"
102 " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
103 defset
, defset
, errno
);
106 "msg checking charset: %s\n",
111 ic_chck
= (iconv_t
)-1;
117 if ((cp
= strchr(iconv_opt
, ',')) != NULL
) {
118 if (am_server
) /* A local transfer needs this. */
124 if (!*iconv_opt
|| (*iconv_opt
== '.' && iconv_opt
[1] == '\0'))
129 if ((ic_send
= iconv_open(UTF8_CHARSET
, charset
)) == (iconv_t
)-1) {
130 rprintf(FERROR
, "iconv_open(\"%s\", \"%s\") failed\n",
131 UTF8_CHARSET
, charset
);
132 exit_cleanup(RERR_UNSUPPORTED
);
135 if ((ic_recv
= iconv_open(charset
, UTF8_CHARSET
)) == (iconv_t
)-1) {
136 rprintf(FERROR
, "iconv_open(\"%s\", \"%s\") failed\n",
137 charset
, UTF8_CHARSET
);
138 exit_cleanup(RERR_UNSUPPORTED
);
141 if (DEBUG_GTE(ICONV
, 1)) {
142 rprintf(FINFO
, "[%s] charset: %s\n",
143 who_am_i(), *charset
? charset
: "[LOCALE]");
148 /* This function converts the chars in the "in" xbuf into characters in the
149 * "out" xbuf. The ".len" chars of the "in" xbuf is used starting from its
150 * ".pos". The ".size" of the "out" xbuf restricts how many characters can
151 * be stored, starting at its ".pos+.len" position. Note that the last byte
152 * of the "out" xbuf is not used, which reserves space for a trailing '\0'
153 * (though it is up to the caller to store a trailing '\0', as needed).
155 * We return a 0 on success or a -1 on error. An error also sets errno to
156 * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
157 * The "in" xbuf is altered to update ".pos" and ".len". The "out" xbuf has
158 * data appended, and its ".len" incremented (see below for a ".size" note).
160 * If ICB_CIRCULAR_OUT is set in "flags", the chars going into the "out" xbuf
161 * can wrap around to the start, and the xbuf may have its ".size" reduced
162 * (presumably by 1 byte) if the iconv code doesn't have space to store a
163 * multi-byte character at the physical end of the ".buf" (though no reducing
164 * happens if ".pos" is <= 1, since there is no room to wrap around).
166 * If ICB_EXPAND_OUT is set in "flags", the "out" xbuf will be allocated if
167 * empty, and (as long as ICB_CIRCULAR_OUT is not set) expanded if too small.
168 * This prevents the return of E2BIG (except for a circular xbuf).
170 * If ICB_INCLUDE_BAD is set in "flags", any badly-encoded chars are included
171 * verbatim in the "out" xbuf, so EILSEQ will not be returned.
173 * If ICB_INCLUDE_INCOMPLETE is set in "flags", any incomplete multi-byte
174 * chars are included, which ensures that EINVAL is not returned.
176 * If ICB_INIT is set, the iconv() conversion state is initialized prior to
177 * processing the characters. */
178 int iconvbufs(iconv_t ic
, xbuf
*in
, xbuf
*out
, int flags
)
180 ICONV_CONST
char *ibuf
;
181 size_t icnt
, ocnt
, opos
;
184 if (!out
->size
&& flags
& ICB_EXPAND_OUT
) {
185 size_t siz
= ROUND_UP_1024(in
->len
* 2);
186 alloc_xbuf(out
, siz
);
187 } else if (out
->len
+1 >= out
->size
) {
188 /* There is no room to even start storing data. */
189 if (!(flags
& ICB_EXPAND_OUT
) || flags
& ICB_CIRCULAR_OUT
) {
193 realloc_xbuf(out
, out
->size
+ ROUND_UP_1024(in
->len
* 2));
196 if (flags
& ICB_INIT
)
197 iconv(ic
, NULL
, 0, NULL
, 0);
199 ibuf
= in
->buf
+ in
->pos
;
202 opos
= out
->pos
+ out
->len
;
203 if (flags
& ICB_CIRCULAR_OUT
) {
204 if (opos
>= out
->size
) {
206 /* We know that out->pos is not 0 due to the "no room" check
207 * above, so this can't go "negative". */
208 ocnt
= out
->pos
- opos
- 1;
210 /* Allow the use of all bytes to the physical end of the buffer
211 * unless pos is 0, in which case we reserve our trailing '\0'. */
212 ocnt
= out
->size
- opos
- (out
->pos
? 0 : 1);
215 ocnt
= out
->size
- opos
- 1;
216 obuf
= out
->buf
+ opos
;
219 while (iconv(ic
, &ibuf
, &icnt
, &obuf
, &ocnt
) == (size_t)-1) {
222 if (errno
== EINVAL
) {
223 if (!(flags
& ICB_INCLUDE_INCOMPLETE
))
227 } else if (errno
== EILSEQ
) {
228 if (!(flags
& ICB_INCLUDE_BAD
))
232 } else if (errno
== E2BIG
) {
235 opos
= obuf
- out
->buf
;
236 if (flags
& ICB_CIRCULAR_OUT
&& out
->pos
> 1 && opos
> out
->pos
) {
237 /* We are in a divided circular buffer at the physical
238 * end with room to wrap to the start. If iconv() refused
239 * to use one or more trailing bytes in the buffer, we
240 * set the size to ignore the unused bytes. */
241 if (opos
< out
->size
)
242 reduce_iobuf_size(out
, opos
);
247 if (!(flags
& ICB_EXPAND_OUT
) || flags
& ICB_CIRCULAR_OUT
) {
251 siz
= ROUND_UP_1024(in
->len
* 2);
252 realloc_xbuf(out
, out
->size
+ siz
);
253 obuf
= out
->buf
+ opos
;
257 rsyserr(FERROR
, errno
, "unexpected error from iconv()");
258 exit_cleanup(RERR_UNSUPPORTED
);
270 opos
= obuf
- out
->buf
;
271 if (flags
& ICB_CIRCULAR_OUT
&& opos
< out
->pos
)
273 out
->len
= opos
- out
->pos
;
276 in
->pos
= ibuf
- in
->buf
;
278 return errno
? -1 : 0;
282 void send_protected_args(int fd
, char *args
[])
286 int convert
= ic_send
!= (iconv_t
)-1;
290 alloc_xbuf(&outbuf
, 1024);
293 for (i
= 0; args
[i
]; i
++) {} /* find first NULL */
294 args
[i
] = "rsync"; /* set a new arg0 */
295 if (DEBUG_GTE(CMD
, 1))
296 print_child_argv("protected args:", args
+ i
+ 1);
299 write_buf(fd
, ".", 2);
302 INIT_XBUF_STRLEN(inbuf
, args
[i
]);
303 iconvbufs(ic_send
, &inbuf
, &outbuf
,
304 ICB_EXPAND_OUT
| ICB_INCLUDE_BAD
| ICB_INCLUDE_INCOMPLETE
| ICB_INIT
);
305 outbuf
.buf
[outbuf
.len
] = '\0';
306 write_buf(fd
, outbuf
.buf
, outbuf
.len
+ 1);
311 write_buf(fd
, args
[i
], strlen(args
[i
]) + 1);
321 int read_ndx_and_attrs(int f_in
, int f_out
, int *iflag_ptr
, uchar
*type_ptr
, char *buf
, int *len_ptr
)
324 struct file_list
*flist
;
325 uchar fnamecmp_type
= FNAMECMP_FNAME
;
330 ndx
= read_ndx(f_in
);
336 if (ndx
== NDX_DEL_STATS
) {
337 read_del_stats(f_in
);
338 if (am_sender
&& am_server
)
339 write_del_stats(f_out
);
342 if (!inc_recurse
|| am_sender
) {
345 last
= first_flist
->prev
->ndx_start
+ first_flist
->prev
->used
- 1;
349 "Invalid file index: %d (%d - %d) [%s]\n",
350 ndx
, NDX_DONE
, last
, who_am_i());
351 exit_cleanup(RERR_PROTOCOL
);
353 if (ndx
== NDX_FLIST_EOF
) {
355 if (DEBUG_GTE(FLIST
, 3))
356 rprintf(FINFO
, "[%s] flist_eof=1\n", who_am_i());
357 write_int(f_out
, NDX_FLIST_EOF
);
360 ndx
= NDX_FLIST_OFFSET
- ndx
;
361 if (ndx
< 0 || ndx
>= dir_flist
->used
) {
362 ndx
= NDX_FLIST_OFFSET
- ndx
;
364 "Invalid dir index: %d (%d - %d) [%s]\n",
365 ndx
, NDX_FLIST_OFFSET
,
366 NDX_FLIST_OFFSET
- dir_flist
->used
+ 1,
368 exit_cleanup(RERR_PROTOCOL
);
371 if (DEBUG_GTE(FLIST
, 2)) {
372 rprintf(FINFO
, "[%s] receiving flist for dir %d\n",
375 /* Send all the data we read for this flist to the generator. */
376 start_flist_forward(ndx
);
377 flist
= recv_file_list(f_in
, ndx
);
378 flist
->parent_ndx
= ndx
;
379 stop_flist_forward();
382 iflags
= protocol_version
>= 29 ? read_shortint(f_in
)
383 : ITEM_TRANSFER
| ITEM_MISSING_DATA
;
385 /* Support the protocol-29 keep-alive style. */
386 if (protocol_version
< 30 && ndx
== cur_flist
->used
&& iflags
== ITEM_IS_NEW
) {
388 maybe_send_keepalive(time(NULL
), MSK_ALLOW_FLUSH
);
392 flist
= flist_for_ndx(ndx
, "read_ndx_and_attrs");
393 if (flist
!= cur_flist
) {
396 file_old_total
= cur_flist
->used
;
397 for (flist
= first_flist
; flist
!= cur_flist
; flist
= flist
->next
)
398 file_old_total
+= flist
->used
;
402 if (iflags
& ITEM_BASIS_TYPE_FOLLOWS
)
403 fnamecmp_type
= read_byte(f_in
);
404 *type_ptr
= fnamecmp_type
;
406 if (iflags
& ITEM_XNAME_FOLLOWS
) {
407 if ((len
= read_vstring(f_in
, buf
, MAXPATHLEN
)) < 0)
408 exit_cleanup(RERR_PROTOCOL
);
410 if (sanitize_paths
) {
411 sanitize_path(buf
, buf
, "", 0, SP_DEFAULT
);
420 if (iflags
& ITEM_TRANSFER
) {
421 int i
= ndx
- cur_flist
->ndx_start
;
422 if (i
< 0 || !S_ISREG(cur_flist
->files
[i
]->mode
)) {
424 "received request to transfer non-regular file: %d [%s]\n",
426 exit_cleanup(RERR_PROTOCOL
);
437 void free_sums(struct sum_struct
*s
)
439 if (s
->sums
) free(s
->sums
);
443 /* This is only called when we aren't preserving permissions. Figure out what
444 * the permissions should be and return them merged back into the mode. */
445 mode_t
dest_mode(mode_t flist_mode
, mode_t stat_mode
, int dflt_perms
,
449 /* If the file already exists, we'll return the local permissions,
450 * possibly tweaked by the --executability option. */
452 new_mode
= (flist_mode
& ~CHMOD_BITS
) | (stat_mode
& CHMOD_BITS
);
453 if (preserve_executability
&& S_ISREG(flist_mode
)) {
454 /* If the source file is executable, grant execute
455 * rights to everyone who can read, but ONLY if the
456 * file isn't already executable. */
457 if (!(flist_mode
& 0111))
459 else if (!(stat_mode
& 0111))
460 new_mode
|= (new_mode
& 0444) >> 2;
463 /* Apply destination default permissions and turn
464 * off special permissions. */
465 new_mode
= flist_mode
& (~CHMOD_BITS
| dflt_perms
);
470 static int same_mtime(struct file_struct
*file
, STRUCT_STAT
*st
, int extra_accuracy
)
473 uint32 f1_nsec
= F_MOD_NSEC_or_0(file
);
474 uint32 f2_nsec
= (uint32
)st
->ST_MTIME_NSEC
;
476 uint32 f1_nsec
= 0, f2_nsec
= 0;
479 if (extra_accuracy
) /* ignore modify_window when setting the time after a transfer or checksum check */
480 return file
->modtime
== st
->st_mtime
&& f1_nsec
== f2_nsec
;
482 return same_time(file
->modtime
, f1_nsec
, st
->st_mtime
, f2_nsec
);
485 int set_file_attrs(const char *fname
, struct file_struct
*file
, stat_x
*sxp
,
486 const char *fnamecmp
, int flags
)
490 int change_uid
, change_gid
;
491 mode_t new_mode
= file
->mode
;
497 if (link_stat(fname
, &sx2
.st
, 0) < 0) {
498 rsyserr(FERROR_XFER
, errno
, "stat %s failed",
504 inherit
= !preserve_perms
;
506 inherit
= !preserve_perms
&& file
->flags
& FLAG_DIR_CREATED
;
508 if (inherit
&& S_ISDIR(new_mode
) && sxp
->st
.st_mode
& S_ISGID
) {
509 /* We just created this directory and its setgid
510 * bit is on, so make sure it stays on. */
514 if (daemon_chmod_modes
&& !S_ISLNK(new_mode
))
515 new_mode
= tweak_mode(new_mode
, daemon_chmod_modes
);
518 if (preserve_acls
&& !S_ISLNK(file
->mode
) && !ACL_READY(*sxp
))
522 change_uid
= am_root
&& uid_ndx
&& sxp
->st
.st_uid
!= (uid_t
)F_OWNER(file
);
523 change_gid
= gid_ndx
&& !(file
->flags
& FLAG_SKIP_GROUP
)
524 && sxp
->st
.st_gid
!= (gid_t
)F_GROUP(file
);
525 #ifndef CAN_CHOWN_SYMLINK
526 if (S_ISLNK(sxp
->st
.st_mode
)) {
530 if (change_uid
|| change_gid
) {
531 if (DEBUG_GTE(OWN
, 1)) {
534 "set uid of %s from %u to %u\n",
535 fname
, (unsigned)sxp
->st
.st_uid
, F_OWNER(file
));
539 "set gid of %s from %u to %u\n",
540 fname
, (unsigned)sxp
->st
.st_gid
, F_GROUP(file
));
544 uid_t uid
= change_uid
? (uid_t
)F_OWNER(file
) : sxp
->st
.st_uid
;
545 gid_t gid
= change_gid
? (gid_t
)F_GROUP(file
) : sxp
->st
.st_gid
;
546 if (do_lchown(fname
, uid
, gid
) != 0) {
547 /* We shouldn't have attempted to change uid
548 * or gid unless have the privilege. */
549 rsyserr(FERROR_XFER
, errno
, "%s %s failed",
550 change_uid
? "chown" : "chgrp",
554 if (uid
== (uid_t
)-1 && sxp
->st
.st_uid
!= (uid_t
)-1)
555 rprintf(FERROR_XFER
, "uid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname
));
556 if (gid
== (gid_t
)-1 && sxp
->st
.st_gid
!= (gid_t
)-1)
557 rprintf(FERROR_XFER
, "gid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname
));
558 /* A lchown had been done, so we need to re-stat if
559 * the destination had the setuid or setgid bits set
560 * (due to the side effect of the chown call). */
561 if (sxp
->st
.st_mode
& (S_ISUID
| S_ISGID
)) {
562 link_stat(fname
, &sxp
->st
,
563 keep_dirlinks
&& S_ISDIR(sxp
->st
.st_mode
));
567 updated
|= UPDATED_OWNER
;
569 updated
|= UPDATED_GROUP
;
572 #ifdef SUPPORT_XATTRS
574 set_stat_xattr(fname
, file
, new_mode
);
575 if (preserve_xattrs
&& fnamecmp
)
576 set_xattr(fname
, file
, fnamecmp
, sxp
);
580 || (!(preserve_times
& PRESERVE_DIR_TIMES
) && S_ISDIR(sxp
->st
.st_mode
))
581 || (!(preserve_times
& PRESERVE_LINK_TIMES
) && S_ISLNK(sxp
->st
.st_mode
)))
582 flags
|= ATTRS_SKIP_MTIME
| ATTRS_SKIP_ATIME
;
583 else if (sxp
!= &sx2
)
584 memcpy(&sx2
.st
, &sxp
->st
, sizeof (sx2
.st
));
585 if (!atimes_ndx
|| S_ISDIR(sxp
->st
.st_mode
))
586 flags
|= ATTRS_SKIP_ATIME
;
587 if (!(flags
& ATTRS_SKIP_MTIME
) && !same_mtime(file
, &sxp
->st
, flags
& ATTRS_ACCURATE_TIME
)) {
588 sx2
.st
.st_mtime
= file
->modtime
;
590 sx2
.st
.ST_MTIME_NSEC
= F_MOD_NSEC_or_0(file
);
592 updated
|= UPDATED_MTIME
;
594 if (!(flags
& ATTRS_SKIP_ATIME
)) {
595 time_t file_atime
= F_ATIME(file
);
596 if (flags
& ATTRS_ACCURATE_TIME
|| !same_time(sxp
->st
.st_atime
, 0, file_atime
, 0)) {
597 sx2
.st
.st_atime
= file_atime
;
599 sx2
.st
.ST_ATIME_NSEC
= 0;
601 updated
|= UPDATED_ATIME
;
604 if (updated
& UPDATED_TIMES
) {
605 int ret
= set_times(fname
, &sx2
.st
);
607 rsyserr(FERROR_XFER
, errno
, "failed to set times on %s",
611 if (ret
> 0) { /* ret == 1 if symlink could not be set */
612 updated
&= ~UPDATED_TIMES
;
613 file
->flags
|= FLAG_TIME_FAILED
;
618 /* It's OK to call set_acl() now, even for a dir, as the generator
619 * will enable owner-writability using chmod, if necessary.
621 * If set_acl() changes permission bits in the process of setting
622 * an access ACL, it changes sxp->st.st_mode so we know whether we
623 * need to chmod(). */
624 if (preserve_acls
&& !S_ISLNK(new_mode
)) {
625 if (set_acl(fname
, file
, sxp
, new_mode
) > 0)
626 updated
|= UPDATED_ACLS
;
631 if (!BITS_EQUAL(sxp
->st
.st_mode
, new_mode
, CHMOD_BITS
)) {
632 int ret
= am_root
< 0 ? 0 : do_chmod(fname
, new_mode
);
634 rsyserr(FERROR_XFER
, errno
,
635 "failed to set permissions on %s",
639 if (ret
== 0) /* ret == 1 if symlink could not be set */
640 updated
|= UPDATED_MODE
;
644 if (INFO_GTE(NAME
, 2) && flags
& ATTRS_REPORT
) {
646 rprintf(FCLIENT
, "%s\n", fname
);
648 rprintf(FCLIENT
, "%s is uptodate\n", fname
);
656 /* This is only called for SIGINT, SIGHUP, and SIGTERM. */
657 void sig_int(int sig_num
)
659 called_from_signal_handler
= 1;
661 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
662 * for a password, then our cleanup's sending of a SIGUSR1
663 * signal to all our children may kill ssh before it has a
664 * chance to restore the tty settings (i.e. turn echo back
665 * on). By sleeping for a short time, ssh gets a bigger
666 * chance to do the right thing. If child processes are
667 * not ssh waiting for a password, then this tiny delay
668 * shouldn't hurt anything. */
671 /* If we're an rsync daemon listener (not a daemon server),
672 * we'll exit with status 0 if we received SIGTERM. */
673 if (am_daemon
&& !am_server
&& sig_num
== SIGTERM
)
676 /* If the signal arrived on the server side (or for the receiver
677 * process on the client), we want to try to do a controlled shutdown
678 * that lets the client side (generator process) know what happened.
679 * To do this, we set a flag and let the normal process handle the
680 * shutdown. We only attempt this if multiplexed IO is in effect and
681 * we didn't already set the flag. */
682 if (!got_kill_signal
&& (am_server
|| am_receiver
)) {
683 got_kill_signal
= sig_num
;
684 called_from_signal_handler
= 0;
688 exit_cleanup(RERR_SIGNAL
);
691 /* Finish off a file transfer: renaming the file and setting the file's
692 * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
693 * call is forced to copy the temp file and partialptr is both non-NULL and
694 * not an absolute path, we stage the file into the partial-dir and then
695 * rename it into place. This returns 1 on success or 0 on failure. */
696 int finish_transfer(const char *fname
, const char *fnametmp
,
697 const char *fnamecmp
, const char *partialptr
,
698 struct file_struct
*file
, int ok_to_set_time
,
699 int overwriting_basis
)
702 const char *temp_copy_name
= partialptr
&& *partialptr
!= '/' ? partialptr
: NULL
;
705 if (DEBUG_GTE(RECV
, 1))
706 rprintf(FINFO
, "finishing %s\n", fname
);
708 goto do_set_file_attrs
;
711 if (make_backups
> 0 && overwriting_basis
) {
712 int ok
= make_backup(fname
, False
);
714 exit_cleanup(RERR_FILEIO
);
715 if (ok
== 1 && fnamecmp
== fname
)
716 fnamecmp
= get_backup_name(fname
);
719 /* Change permissions before putting the file into place. */
720 set_file_attrs(fnametmp
, file
, NULL
, fnamecmp
,
721 ok_to_set_time
? ATTRS_ACCURATE_TIME
: ATTRS_SKIP_MTIME
| ATTRS_SKIP_ATIME
);
723 /* move tmp file over real file */
724 if (DEBUG_GTE(RECV
, 1))
725 rprintf(FINFO
, "renaming %s to %s\n", fnametmp
, fname
);
726 ret
= robust_rename(fnametmp
, fname
, temp_copy_name
, file
->mode
);
728 rsyserr(FERROR_XFER
, errno
, "%s %s -> \"%s\"",
729 ret
== -2 ? "copy" : "rename",
730 full_fname(fnametmp
), fname
);
731 if (!partialptr
|| (ret
== -2 && temp_copy_name
)
732 || robust_rename(fnametmp
, partialptr
, NULL
, file
->mode
) < 0)
737 /* The file was moved into place (not copied), so it's done. */
740 /* The file was copied, so tweak the perms of the copied file. If it
741 * was copied to partialptr, move it into its final destination. */
742 fnametmp
= temp_copy_name
? temp_copy_name
: fname
;
745 set_file_attrs(fnametmp
, file
, NULL
, fnamecmp
,
746 ok_to_set_time
? ATTRS_ACCURATE_TIME
: ATTRS_SKIP_MTIME
| ATTRS_SKIP_ATIME
);
748 if (temp_copy_name
) {
749 if (do_rename(fnametmp
, fname
) < 0) {
750 rsyserr(FERROR_XFER
, errno
, "rename %s -> \"%s\"",
751 full_fname(fnametmp
), fname
);
754 handle_partial_dir(temp_copy_name
, PDIR_DELETE
);
759 struct file_list
*flist_for_ndx(int ndx
, const char *fatal_error_loc
)
761 struct file_list
*flist
= cur_flist
;
763 if (!flist
&& !(flist
= first_flist
))
766 while (ndx
< flist
->ndx_start
-1) {
767 if (flist
== first_flist
)
771 while (ndx
>= flist
->ndx_start
+ flist
->used
) {
772 if (!(flist
= flist
->next
))
778 if (fatal_error_loc
) {
781 first
= first_flist
->ndx_start
- 1;
782 last
= first_flist
->prev
->ndx_start
+ first_flist
->prev
->used
- 1;
788 "File-list index %d not in %d - %d (%s) [%s]\n",
789 ndx
, first
, last
, fatal_error_loc
, who_am_i());
790 exit_cleanup(RERR_PROTOCOL
);
795 const char *who_am_i(void)
798 return am_server
? "server" : "client";
799 return am_sender
? "sender"
800 : am_generator
? "generator"
801 : am_receiver
? "receiver"
802 : "Receiver"; /* pre-forked receiver */