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-2022 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_mtimes
;
36 extern int omit_dir_times
;
37 extern int omit_link_times
;
42 extern int am_receiver
;
43 extern int am_generator
;
44 extern int am_starting_up
;
45 extern int allow_8bit_chars
;
46 extern int protocol_version
;
47 extern int got_kill_signal
;
48 extern int called_from_signal_handler
;
49 extern int inc_recurse
;
52 extern int file_old_total
;
53 extern int keep_dirlinks
;
54 extern int make_backups
;
55 extern int sanitize_paths
;
56 extern struct file_list
*cur_flist
, *first_flist
, *dir_flist
;
57 extern struct chmod_mode_struct
*daemon_chmod_modes
;
59 extern char *iconv_opt
;
62 #define UPDATED_OWNER (1<<0)
63 #define UPDATED_GROUP (1<<1)
64 #define UPDATED_MTIME (1<<2)
65 #define UPDATED_ATIME (1<<3)
66 #define UPDATED_ACLS (1<<4)
67 #define UPDATED_MODE (1<<5)
68 #define UPDATED_CRTIME (1<<6)
71 iconv_t ic_chck
= (iconv_t
)-1;
73 iconv_t ic_send
= (iconv_t
)-1, ic_recv
= (iconv_t
)-1;
76 static const char *default_charset(void)
78 # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
79 return locale_charset();
80 # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
81 return nl_langinfo(CODESET
);
83 return ""; /* Works with (at the very least) gnu iconv... */
87 void setup_iconv(void)
89 const char *defset
= default_charset();
95 if (!am_server
&& !allow_8bit_chars
) {
96 /* It's OK if this fails... */
97 ic_chck
= iconv_open(defset
, defset
);
99 if (DEBUG_GTE(ICONV
, 2)) {
100 if (ic_chck
== (iconv_t
)-1) {
102 "msg checking via isprint()"
103 " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
104 defset
, defset
, errno
);
107 "msg checking charset: %s\n",
112 ic_chck
= (iconv_t
)-1;
118 if ((cp
= strchr(iconv_opt
, ',')) != NULL
) {
119 if (am_server
) /* A local transfer needs this. */
125 if (!*iconv_opt
|| (*iconv_opt
== '.' && iconv_opt
[1] == '\0'))
130 if ((ic_send
= iconv_open(UTF8_CHARSET
, charset
)) == (iconv_t
)-1) {
131 rprintf(FERROR
, "iconv_open(\"%s\", \"%s\") failed\n",
132 UTF8_CHARSET
, charset
);
133 exit_cleanup(RERR_UNSUPPORTED
);
136 if ((ic_recv
= iconv_open(charset
, UTF8_CHARSET
)) == (iconv_t
)-1) {
137 rprintf(FERROR
, "iconv_open(\"%s\", \"%s\") failed\n",
138 charset
, UTF8_CHARSET
);
139 exit_cleanup(RERR_UNSUPPORTED
);
142 if (DEBUG_GTE(ICONV
, 1)) {
143 rprintf(FINFO
, "[%s] charset: %s\n",
144 who_am_i(), *charset
? charset
: "[LOCALE]");
149 /* This function converts the chars in the "in" xbuf into characters in the
150 * "out" xbuf. The ".len" chars of the "in" xbuf is used starting from its
151 * ".pos". The ".size" of the "out" xbuf restricts how many characters can
152 * be stored, starting at its ".pos+.len" position. Note that the last byte
153 * of the "out" xbuf is not used, which reserves space for a trailing '\0'
154 * (though it is up to the caller to store a trailing '\0', as needed).
156 * We return a 0 on success or a -1 on error. An error also sets errno to
157 * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
158 * The "in" xbuf is altered to update ".pos" and ".len". The "out" xbuf has
159 * data appended, and its ".len" incremented (see below for a ".size" note).
161 * If ICB_CIRCULAR_OUT is set in "flags", the chars going into the "out" xbuf
162 * can wrap around to the start, and the xbuf may have its ".size" reduced
163 * (presumably by 1 byte) if the iconv code doesn't have space to store a
164 * multi-byte character at the physical end of the ".buf" (though no reducing
165 * happens if ".pos" is <= 1, since there is no room to wrap around).
167 * If ICB_EXPAND_OUT is set in "flags", the "out" xbuf will be allocated if
168 * empty, and (as long as ICB_CIRCULAR_OUT is not set) expanded if too small.
169 * This prevents the return of E2BIG (except for a circular xbuf).
171 * If ICB_INCLUDE_BAD is set in "flags", any badly-encoded chars are included
172 * verbatim in the "out" xbuf, so EILSEQ will not be returned.
174 * If ICB_INCLUDE_INCOMPLETE is set in "flags", any incomplete multi-byte
175 * chars are included, which ensures that EINVAL is not returned.
177 * If ICB_INIT is set, the iconv() conversion state is initialized prior to
178 * processing the characters. */
179 int iconvbufs(iconv_t ic
, xbuf
*in
, xbuf
*out
, int flags
)
181 ICONV_CONST
char *ibuf
;
182 size_t icnt
, ocnt
, opos
;
185 if (!out
->size
&& flags
& ICB_EXPAND_OUT
) {
186 size_t siz
= ROUND_UP_1024(in
->len
* 2);
187 alloc_xbuf(out
, siz
);
188 } else if (out
->len
+1 >= out
->size
) {
189 /* There is no room to even start storing data. */
190 if (!(flags
& ICB_EXPAND_OUT
) || flags
& ICB_CIRCULAR_OUT
) {
194 realloc_xbuf(out
, out
->size
+ ROUND_UP_1024(in
->len
* 2));
197 if (flags
& ICB_INIT
)
198 iconv(ic
, NULL
, 0, NULL
, 0);
200 ibuf
= in
->buf
+ in
->pos
;
203 opos
= out
->pos
+ out
->len
;
204 if (flags
& ICB_CIRCULAR_OUT
) {
205 if (opos
>= out
->size
) {
207 /* We know that out->pos is not 0 due to the "no room" check
208 * above, so this can't go "negative". */
209 ocnt
= out
->pos
- opos
- 1;
211 /* Allow the use of all bytes to the physical end of the buffer
212 * unless pos is 0, in which case we reserve our trailing '\0'. */
213 ocnt
= out
->size
- opos
- (out
->pos
? 0 : 1);
216 ocnt
= out
->size
- opos
- 1;
217 obuf
= out
->buf
+ opos
;
220 while (iconv(ic
, &ibuf
, &icnt
, &obuf
, &ocnt
) == (size_t)-1) {
223 if (errno
== EINVAL
) {
224 if (!(flags
& ICB_INCLUDE_INCOMPLETE
))
228 } else if (errno
== EILSEQ
) {
229 if (!(flags
& ICB_INCLUDE_BAD
))
233 } else if (errno
== E2BIG
) {
236 opos
= obuf
- out
->buf
;
237 if (flags
& ICB_CIRCULAR_OUT
&& out
->pos
> 1 && opos
> out
->pos
) {
238 /* We are in a divided circular buffer at the physical
239 * end with room to wrap to the start. If iconv() refused
240 * to use one or more trailing bytes in the buffer, we
241 * set the size to ignore the unused bytes. */
242 if (opos
< out
->size
)
243 reduce_iobuf_size(out
, opos
);
248 if (!(flags
& ICB_EXPAND_OUT
) || flags
& ICB_CIRCULAR_OUT
) {
252 siz
= ROUND_UP_1024(in
->len
* 2);
253 realloc_xbuf(out
, out
->size
+ siz
);
254 obuf
= out
->buf
+ opos
;
258 rsyserr(FERROR
, errno
, "unexpected error from iconv()");
259 exit_cleanup(RERR_UNSUPPORTED
);
271 opos
= obuf
- out
->buf
;
272 if (flags
& ICB_CIRCULAR_OUT
&& opos
< out
->pos
)
274 out
->len
= opos
- out
->pos
;
277 in
->pos
= ibuf
- in
->buf
;
279 return errno
? -1 : 0;
283 void send_protected_args(int fd
, char *args
[])
287 int convert
= ic_send
!= (iconv_t
)-1;
291 alloc_xbuf(&outbuf
, 1024);
294 for (i
= 0; args
[i
]; i
++) {} /* find first NULL */
295 args
[i
] = "rsync"; /* set a new arg0 */
296 if (DEBUG_GTE(CMD
, 1))
297 print_child_argv("protected args:", args
+ i
+ 1);
300 write_buf(fd
, ".", 2);
303 INIT_XBUF_STRLEN(inbuf
, args
[i
]);
304 iconvbufs(ic_send
, &inbuf
, &outbuf
,
305 ICB_EXPAND_OUT
| ICB_INCLUDE_BAD
| ICB_INCLUDE_INCOMPLETE
| ICB_INIT
);
306 outbuf
.buf
[outbuf
.len
] = '\0';
307 write_buf(fd
, outbuf
.buf
, outbuf
.len
+ 1);
312 write_buf(fd
, args
[i
], strlen(args
[i
]) + 1);
322 int read_ndx_and_attrs(int f_in
, int f_out
, int *iflag_ptr
, uchar
*type_ptr
, char *buf
, int *len_ptr
)
325 struct file_list
*flist
;
326 uchar fnamecmp_type
= FNAMECMP_FNAME
;
331 ndx
= read_ndx(f_in
);
337 if (ndx
== NDX_DEL_STATS
) {
338 read_del_stats(f_in
);
339 if (am_sender
&& am_server
)
340 write_del_stats(f_out
);
343 if (!inc_recurse
|| am_sender
) {
346 last
= first_flist
->prev
->ndx_start
+ first_flist
->prev
->used
- 1;
350 "Invalid file index: %d (%d - %d) [%s]\n",
351 ndx
, NDX_DONE
, last
, who_am_i());
352 exit_cleanup(RERR_PROTOCOL
);
354 if (ndx
== NDX_FLIST_EOF
) {
356 if (DEBUG_GTE(FLIST
, 3))
357 rprintf(FINFO
, "[%s] flist_eof=1\n", who_am_i());
358 write_int(f_out
, NDX_FLIST_EOF
);
361 ndx
= NDX_FLIST_OFFSET
- ndx
;
362 if (ndx
< 0 || ndx
>= dir_flist
->used
) {
363 ndx
= NDX_FLIST_OFFSET
- ndx
;
365 "Invalid dir index: %d (%d - %d) [%s]\n",
366 ndx
, NDX_FLIST_OFFSET
,
367 NDX_FLIST_OFFSET
- dir_flist
->used
+ 1,
369 exit_cleanup(RERR_PROTOCOL
);
372 if (DEBUG_GTE(FLIST
, 2)) {
373 rprintf(FINFO
, "[%s] receiving flist for dir %d\n",
376 /* Send all the data we read for this flist to the generator. */
377 start_flist_forward(ndx
);
378 flist
= recv_file_list(f_in
, ndx
);
379 flist
->parent_ndx
= ndx
;
380 stop_flist_forward();
383 iflags
= protocol_version
>= 29 ? read_shortint(f_in
)
384 : ITEM_TRANSFER
| ITEM_MISSING_DATA
;
386 /* Support the protocol-29 keep-alive style. */
387 if (protocol_version
< 30 && ndx
== cur_flist
->used
&& iflags
== ITEM_IS_NEW
) {
389 maybe_send_keepalive(time(NULL
), MSK_ALLOW_FLUSH
);
393 flist
= flist_for_ndx(ndx
, "read_ndx_and_attrs");
394 if (flist
!= cur_flist
) {
397 file_old_total
= cur_flist
->used
;
398 for (flist
= first_flist
; flist
!= cur_flist
; flist
= flist
->next
)
399 file_old_total
+= flist
->used
;
403 if (iflags
& ITEM_BASIS_TYPE_FOLLOWS
)
404 fnamecmp_type
= read_byte(f_in
);
405 *type_ptr
= fnamecmp_type
;
407 if (iflags
& ITEM_XNAME_FOLLOWS
) {
408 if ((len
= read_vstring(f_in
, buf
, MAXPATHLEN
)) < 0)
409 exit_cleanup(RERR_PROTOCOL
);
411 if (sanitize_paths
) {
412 sanitize_path(buf
, buf
, "", 0, SP_DEFAULT
);
421 if (iflags
& ITEM_TRANSFER
) {
422 int i
= ndx
- cur_flist
->ndx_start
;
423 if (i
< 0 || !S_ISREG(cur_flist
->files
[i
]->mode
)) {
425 "received request to transfer non-regular file: %d [%s]\n",
427 exit_cleanup(RERR_PROTOCOL
);
438 void free_sums(struct sum_struct
*s
)
440 if (s
->sums
) free(s
->sums
);
444 /* This is only called when we aren't preserving permissions. Figure out what
445 * the permissions should be and return them merged back into the mode. */
446 mode_t
dest_mode(mode_t flist_mode
, mode_t stat_mode
, int dflt_perms
,
450 /* If the file already exists, we'll return the local permissions,
451 * possibly tweaked by the --executability option. */
453 new_mode
= (flist_mode
& ~CHMOD_BITS
) | (stat_mode
& CHMOD_BITS
);
454 if (preserve_executability
&& S_ISREG(flist_mode
)) {
455 /* If the source file is executable, grant execute
456 * rights to everyone who can read, but ONLY if the
457 * file isn't already executable. */
458 if (!(flist_mode
& 0111))
460 else if (!(stat_mode
& 0111))
461 new_mode
|= (new_mode
& 0444) >> 2;
464 /* Apply destination default permissions and turn
465 * off special permissions. */
466 new_mode
= flist_mode
& (~CHMOD_BITS
| dflt_perms
);
471 static int same_mtime(struct file_struct
*file
, STRUCT_STAT
*st
, int extra_accuracy
)
474 uint32 f1_nsec
= F_MOD_NSEC_or_0(file
);
475 uint32 f2_nsec
= (uint32
)st
->ST_MTIME_NSEC
;
477 uint32 f1_nsec
= 0, f2_nsec
= 0;
480 if (extra_accuracy
) /* ignore modify_window when setting the time after a transfer or checksum check */
481 return file
->modtime
== st
->st_mtime
&& f1_nsec
== f2_nsec
;
483 return same_time(file
->modtime
, f1_nsec
, st
->st_mtime
, f2_nsec
);
486 int set_file_attrs(const char *fname
, struct file_struct
*file
, stat_x
*sxp
,
487 const char *fnamecmp
, int flags
)
491 int change_uid
, change_gid
;
492 mode_t new_mode
= file
->mode
;
498 if (link_stat(fname
, &sx2
.st
, 0) < 0) {
499 rsyserr(FERROR_XFER
, errno
, "stat %s failed",
505 inherit
= !preserve_perms
;
507 inherit
= !preserve_perms
&& file
->flags
& FLAG_DIR_CREATED
;
509 if (inherit
&& S_ISDIR(new_mode
) && sxp
->st
.st_mode
& S_ISGID
) {
510 /* We just created this directory and its setgid
511 * bit is on, so make sure it stays on. */
515 if (daemon_chmod_modes
&& !S_ISLNK(new_mode
))
516 new_mode
= tweak_mode(new_mode
, daemon_chmod_modes
);
519 if (preserve_acls
&& !S_ISLNK(file
->mode
) && !ACL_READY(*sxp
))
523 change_uid
= am_root
&& uid_ndx
&& sxp
->st
.st_uid
!= (uid_t
)F_OWNER(file
);
524 change_gid
= gid_ndx
&& !(file
->flags
& FLAG_SKIP_GROUP
)
525 && sxp
->st
.st_gid
!= (gid_t
)F_GROUP(file
);
526 #ifndef CAN_CHOWN_SYMLINK
527 if (S_ISLNK(sxp
->st
.st_mode
)) {
531 if (change_uid
|| change_gid
) {
532 if (DEBUG_GTE(OWN
, 1)) {
535 "set uid of %s from %u to %u\n",
536 fname
, (unsigned)sxp
->st
.st_uid
, F_OWNER(file
));
540 "set gid of %s from %u to %u\n",
541 fname
, (unsigned)sxp
->st
.st_gid
, F_GROUP(file
));
545 uid_t uid
= change_uid
? (uid_t
)F_OWNER(file
) : sxp
->st
.st_uid
;
546 gid_t gid
= change_gid
? (gid_t
)F_GROUP(file
) : sxp
->st
.st_gid
;
547 if (do_lchown(fname
, uid
, gid
) != 0) {
548 /* We shouldn't have attempted to change uid
549 * or gid unless have the privilege. */
550 rsyserr(FERROR_XFER
, errno
, "%s %s failed",
551 change_uid
? "chown" : "chgrp",
555 if (uid
== (uid_t
)-1 && sxp
->st
.st_uid
!= (uid_t
)-1)
556 rprintf(FERROR_XFER
, "uid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname
));
557 if (gid
== (gid_t
)-1 && sxp
->st
.st_gid
!= (gid_t
)-1)
558 rprintf(FERROR_XFER
, "gid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname
));
559 /* A lchown had been done, so we need to re-stat if
560 * the destination had the setuid or setgid bits set
561 * (due to the side effect of the chown call). */
562 if (sxp
->st
.st_mode
& (S_ISUID
| S_ISGID
)) {
563 link_stat(fname
, &sxp
->st
,
564 keep_dirlinks
&& S_ISDIR(sxp
->st
.st_mode
));
568 updated
|= UPDATED_OWNER
;
570 updated
|= UPDATED_GROUP
;
573 #ifdef SUPPORT_XATTRS
575 set_stat_xattr(fname
, file
, new_mode
);
576 if (preserve_xattrs
&& fnamecmp
)
577 set_xattr(fname
, file
, fnamecmp
, sxp
);
580 if ((omit_dir_times
&& S_ISDIR(sxp
->st
.st_mode
))
581 || (omit_link_times
&& S_ISLNK(sxp
->st
.st_mode
)))
582 flags
|= ATTRS_SKIP_MTIME
| ATTRS_SKIP_ATIME
| ATTRS_SKIP_CRTIME
;
584 if (!preserve_mtimes
)
585 flags
|= ATTRS_SKIP_MTIME
;
586 if (!atimes_ndx
|| S_ISDIR(sxp
->st
.st_mode
))
587 flags
|= ATTRS_SKIP_ATIME
;
588 /* Don't set the creation date on the root folder of an HFS+ volume. */
589 if (sxp
->st
.st_ino
== 2 && S_ISDIR(sxp
->st
.st_mode
))
590 flags
|= ATTRS_SKIP_CRTIME
;
593 memcpy(&sx2
.st
, &sxp
->st
, sizeof sx2
.st
);
594 if (!(flags
& ATTRS_SKIP_MTIME
) && !same_mtime(file
, &sxp
->st
, flags
& ATTRS_ACCURATE_TIME
)) {
595 sx2
.st
.st_mtime
= file
->modtime
;
597 sx2
.st
.ST_MTIME_NSEC
= F_MOD_NSEC_or_0(file
);
599 updated
|= UPDATED_MTIME
;
601 if (!(flags
& ATTRS_SKIP_ATIME
)) {
602 time_t file_atime
= F_ATIME(file
);
603 if (flags
& ATTRS_ACCURATE_TIME
|| !same_time(sxp
->st
.st_atime
, 0, file_atime
, 0)) {
604 sx2
.st
.st_atime
= file_atime
;
606 sx2
.st
.ST_ATIME_NSEC
= 0;
608 updated
|= UPDATED_ATIME
;
611 #ifdef SUPPORT_CRTIMES
612 if (crtimes_ndx
&& !(flags
& ATTRS_SKIP_CRTIME
)) {
613 time_t file_crtime
= F_CRTIME(file
);
614 if (sxp
->crtime
== 0)
615 sxp
->crtime
= get_create_time(fname
, &sxp
->st
);
616 if (!same_time(sxp
->crtime
, 0L, file_crtime
, 0L)) {
618 #ifdef HAVE_GETATTRLIST
619 do_setattrlist_crtime(fname
, file_crtime
) == 0
620 #elif defined __CYGWIN__
621 do_SetFileTime(fname
, file_crtime
) == 0
623 #error Unknown crtimes implementation
626 updated
|= UPDATED_CRTIME
;
630 if (updated
& (UPDATED_MTIME
|UPDATED_ATIME
)) {
631 int ret
= set_times(fname
, &sx2
.st
);
633 rsyserr(FERROR_XFER
, errno
, "failed to set times on %s", full_fname(fname
));
636 if (ret
> 0) { /* ret == 1 if symlink could not be set */
637 updated
&= ~(UPDATED_MTIME
|UPDATED_ATIME
);
638 file
->flags
|= FLAG_TIME_FAILED
;
643 /* It's OK to call set_acl() now, even for a dir, as the generator
644 * will enable owner-writability using chmod, if necessary.
646 * If set_acl() changes permission bits in the process of setting
647 * an access ACL, it changes sxp->st.st_mode so we know whether we
648 * need to chmod(). */
649 if (preserve_acls
&& !S_ISLNK(new_mode
)) {
650 if (set_acl(fname
, file
, sxp
, new_mode
) > 0)
651 updated
|= UPDATED_ACLS
;
656 if (!BITS_EQUAL(sxp
->st
.st_mode
, new_mode
, CHMOD_BITS
)) {
657 int ret
= am_root
< 0 ? 0 : do_chmod(fname
, new_mode
);
659 rsyserr(FERROR_XFER
, errno
,
660 "failed to set permissions on %s",
664 if (ret
== 0) /* ret == 1 if symlink could not be set */
665 updated
|= UPDATED_MODE
;
669 if (INFO_GTE(NAME
, 2) && flags
& ATTRS_REPORT
) {
671 rprintf(FCLIENT
, "%s\n", fname
);
673 rprintf(FCLIENT
, "%s is uptodate\n", fname
);
681 /* This is only called for SIGINT, SIGHUP, and SIGTERM. */
682 void sig_int(int sig_num
)
684 called_from_signal_handler
= 1;
686 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
687 * for a password, then our cleanup's sending of a SIGUSR1
688 * signal to all our children may kill ssh before it has a
689 * chance to restore the tty settings (i.e. turn echo back
690 * on). By sleeping for a short time, ssh gets a bigger
691 * chance to do the right thing. If child processes are
692 * not ssh waiting for a password, then this tiny delay
693 * shouldn't hurt anything. */
696 /* If we're an rsync daemon listener (not a daemon server),
697 * we'll exit with status 0 if we received SIGTERM. */
698 if (am_daemon
&& !am_server
&& sig_num
== SIGTERM
)
701 /* If the signal arrived on the server side (or for the receiver
702 * process on the client), we want to try to do a controlled shutdown
703 * that lets the client side (generator process) know what happened.
704 * To do this, we set a flag and let the normal process handle the
705 * shutdown. We only attempt this if multiplexed IO is in effect and
706 * we didn't already set the flag. */
707 if (!got_kill_signal
&& (am_server
|| am_receiver
)) {
708 got_kill_signal
= sig_num
;
709 called_from_signal_handler
= 0;
713 exit_cleanup(RERR_SIGNAL
);
716 /* Finish off a file transfer: renaming the file and setting the file's
717 * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
718 * call is forced to copy the temp file and partialptr is both non-NULL and
719 * not an absolute path, we stage the file into the partial-dir and then
720 * rename it into place. This returns 1 on success or 0 on failure. */
721 int finish_transfer(const char *fname
, const char *fnametmp
,
722 const char *fnamecmp
, const char *partialptr
,
723 struct file_struct
*file
, int ok_to_set_time
,
724 int overwriting_basis
)
727 const char *temp_copy_name
= partialptr
&& *partialptr
!= '/' ? partialptr
: NULL
;
730 if (DEBUG_GTE(RECV
, 1))
731 rprintf(FINFO
, "finishing %s\n", fname
);
733 goto do_set_file_attrs
;
736 if (make_backups
> 0 && overwriting_basis
) {
737 int ok
= make_backup(fname
, False
);
739 exit_cleanup(RERR_FILEIO
);
740 if (ok
== 1 && fnamecmp
== fname
)
741 fnamecmp
= get_backup_name(fname
);
744 /* Change permissions before putting the file into place. */
745 set_file_attrs(fnametmp
, file
, NULL
, fnamecmp
,
746 ok_to_set_time
? ATTRS_ACCURATE_TIME
: ATTRS_SKIP_MTIME
| ATTRS_SKIP_ATIME
| ATTRS_SKIP_CRTIME
);
748 /* move tmp file over real file */
749 if (DEBUG_GTE(RECV
, 1))
750 rprintf(FINFO
, "renaming %s to %s\n", fnametmp
, fname
);
751 ret
= robust_rename(fnametmp
, fname
, temp_copy_name
, file
->mode
);
753 rsyserr(FERROR_XFER
, errno
, "%s %s -> \"%s\"",
754 ret
== -2 ? "copy" : "rename",
755 full_fname(fnametmp
), fname
);
756 if (!partialptr
|| (ret
== -2 && temp_copy_name
)
757 || robust_rename(fnametmp
, partialptr
, NULL
, file
->mode
) < 0)
762 /* The file was moved into place (not copied), so it's done. */
765 /* The file was copied, so tweak the perms of the copied file. If it
766 * was copied to partialptr, move it into its final destination. */
767 fnametmp
= temp_copy_name
? temp_copy_name
: fname
;
770 set_file_attrs(fnametmp
, file
, NULL
, fnamecmp
,
771 ok_to_set_time
? ATTRS_ACCURATE_TIME
: ATTRS_SKIP_MTIME
| ATTRS_SKIP_ATIME
| ATTRS_SKIP_CRTIME
);
773 if (temp_copy_name
) {
774 if (do_rename(fnametmp
, fname
) < 0) {
775 rsyserr(FERROR_XFER
, errno
, "rename %s -> \"%s\"",
776 full_fname(fnametmp
), fname
);
779 handle_partial_dir(temp_copy_name
, PDIR_DELETE
);
784 struct file_list
*flist_for_ndx(int ndx
, const char *fatal_error_loc
)
786 struct file_list
*flist
= cur_flist
;
788 if (!flist
&& !(flist
= first_flist
))
791 while (ndx
< flist
->ndx_start
-1) {
792 if (flist
== first_flist
)
796 while (ndx
>= flist
->ndx_start
+ flist
->used
) {
797 if (!(flist
= flist
->next
))
803 if (fatal_error_loc
) {
806 first
= first_flist
->ndx_start
- 1;
807 last
= first_flist
->prev
->ndx_start
+ first_flist
->prev
->used
- 1;
813 "File-list index %d not in %d - %d (%s) [%s]\n",
814 ndx
, first
, last
, fatal_error_loc
, who_am_i());
815 exit_cleanup(RERR_PROTOCOL
);
820 const char *who_am_i(void)
823 return am_server
? "server" : "client";
824 return am_sender
? "sender"
825 : am_generator
? "generator"
826 : am_receiver
? "receiver"
827 : "Receiver"; /* pre-forked receiver */