Init the checksum choices before the daemon auth.
[rsync.git] / rsync.c
blobcd288f57d430b3f1b8620310b271a0e2244ac687
1 /*
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.
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
30 extern int dry_run;
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;
38 extern int am_root;
39 extern int am_server;
40 extern int am_daemon;
41 extern int am_sender;
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;
50 extern int inplace;
51 extern int flist_eof;
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;
58 #ifdef ICONV_OPTION
59 extern char *iconv_opt;
60 #endif
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)
70 #ifdef ICONV_CONST
71 iconv_t ic_chck = (iconv_t)-1;
72 # ifdef ICONV_OPTION
73 iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
74 # endif
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);
82 # else
83 return ""; /* Works with (at the very least) gnu iconv... */
84 # endif
87 void setup_iconv(void)
89 const char *defset = default_charset();
90 # ifdef ICONV_OPTION
91 const char *charset;
92 char *cp;
93 # endif
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) {
101 rprintf(FINFO,
102 "msg checking via isprint()"
103 " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
104 defset, defset, errno);
105 } else {
106 rprintf(FINFO,
107 "msg checking charset: %s\n",
108 defset);
111 } else
112 ic_chck = (iconv_t)-1;
114 # ifdef ICONV_OPTION
115 if (!iconv_opt)
116 return;
118 if ((cp = strchr(iconv_opt, ',')) != NULL) {
119 if (am_server) /* A local transfer needs this. */
120 iconv_opt = cp + 1;
121 else
122 *cp = '\0';
125 if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0'))
126 charset = defset;
127 else
128 charset = iconv_opt;
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]");
146 # endif
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;
183 char *obuf;
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) {
191 errno = E2BIG;
192 return -1;
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;
201 icnt = in->len;
203 opos = out->pos + out->len;
204 if (flags & ICB_CIRCULAR_OUT) {
205 if (opos >= out->size) {
206 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;
210 } else {
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);
215 } else
216 ocnt = out->size - opos - 1;
217 obuf = out->buf + opos;
219 while (icnt) {
220 while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
221 if (errno == EINTR)
222 continue;
223 if (errno == EINVAL) {
224 if (!(flags & ICB_INCLUDE_INCOMPLETE))
225 goto finish;
226 if (!ocnt)
227 goto e2big;
228 } else if (errno == EILSEQ) {
229 if (!(flags & ICB_INCLUDE_BAD))
230 goto finish;
231 if (!ocnt)
232 goto e2big;
233 } else if (errno == E2BIG) {
234 size_t siz;
235 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);
244 obuf = out->buf;
245 ocnt = out->pos - 1;
246 continue;
248 if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
249 errno = E2BIG;
250 goto finish;
252 siz = ROUND_UP_1024(in->len * 2);
253 realloc_xbuf(out, out->size + siz);
254 obuf = out->buf + opos;
255 ocnt += siz;
256 continue;
257 } else {
258 rsyserr(FERROR, errno, "unexpected error from iconv()");
259 exit_cleanup(RERR_UNSUPPORTED);
261 *obuf++ = *ibuf++;
262 ocnt--, icnt--;
263 if (!icnt)
264 break;
268 errno = 0;
270 finish:
271 opos = obuf - out->buf;
272 if (flags & ICB_CIRCULAR_OUT && opos < out->pos)
273 opos += out->size;
274 out->len = opos - out->pos;
276 in->len = icnt;
277 in->pos = ibuf - in->buf;
279 return errno ? -1 : 0;
281 #endif
283 void send_protected_args(int fd, char *args[])
285 int i;
286 #ifdef ICONV_OPTION
287 int convert = ic_send != (iconv_t)-1;
288 xbuf outbuf, inbuf;
290 if (convert)
291 alloc_xbuf(&outbuf, 1024);
292 #endif
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);
298 do {
299 if (!args[i][0])
300 write_buf(fd, ".", 2);
301 #ifdef ICONV_OPTION
302 else if (convert) {
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);
308 outbuf.len = 0;
310 #endif
311 else
312 write_buf(fd, args[i], strlen(args[i]) + 1);
313 } while (args[++i]);
314 write_byte(fd, 0);
316 #ifdef ICONV_OPTION
317 if (convert)
318 free(outbuf.buf);
319 #endif
322 int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, uchar *type_ptr, char *buf, int *len_ptr)
324 int len, iflags = 0;
325 struct file_list *flist;
326 uchar fnamecmp_type = FNAMECMP_FNAME;
327 int ndx;
329 read_loop:
330 while (1) {
331 ndx = read_ndx(f_in);
333 if (ndx >= 0)
334 break;
335 if (ndx == NDX_DONE)
336 return ndx;
337 if (ndx == NDX_DEL_STATS) {
338 read_del_stats(f_in);
339 if (am_sender && am_server)
340 write_del_stats(f_out);
341 continue;
343 if (!inc_recurse || am_sender) {
344 int last;
345 if (first_flist)
346 last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
347 else
348 last = -1;
349 rprintf(FERROR,
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) {
355 flist_eof = 1;
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);
359 continue;
361 ndx = NDX_FLIST_OFFSET - ndx;
362 if (ndx < 0 || ndx >= dir_flist->used) {
363 ndx = NDX_FLIST_OFFSET - ndx;
364 rprintf(FERROR,
365 "Invalid dir index: %d (%d - %d) [%s]\n",
366 ndx, NDX_FLIST_OFFSET,
367 NDX_FLIST_OFFSET - dir_flist->used + 1,
368 who_am_i());
369 exit_cleanup(RERR_PROTOCOL);
372 if (DEBUG_GTE(FLIST, 2)) {
373 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
374 who_am_i(), ndx);
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) {
388 if (am_sender)
389 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
390 goto read_loop;
393 flist = flist_for_ndx(ndx, "read_ndx_and_attrs");
394 if (flist != cur_flist) {
395 cur_flist = flist;
396 if (am_sender) {
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);
413 len = strlen(buf);
415 } else {
416 *buf = '\0';
417 len = -1;
419 *len_ptr = len;
421 if (iflags & ITEM_TRANSFER) {
422 int i = ndx - cur_flist->ndx_start;
423 if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
424 rprintf(FERROR,
425 "received request to transfer non-regular file: %d [%s]\n",
426 ndx, who_am_i());
427 exit_cleanup(RERR_PROTOCOL);
431 *iflag_ptr = iflags;
432 return ndx;
436 free a sums struct
438 void free_sums(struct sum_struct *s)
440 if (s->sums) free(s->sums);
441 free(s);
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,
447 int exists)
449 int new_mode;
450 /* If the file already exists, we'll return the local permissions,
451 * possibly tweaked by the --executability option. */
452 if (exists) {
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))
459 new_mode &= ~0111;
460 else if (!(stat_mode & 0111))
461 new_mode |= (new_mode & 0444) >> 2;
463 } else {
464 /* Apply destination default permissions and turn
465 * off special permissions. */
466 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
468 return new_mode;
471 static int same_mtime(struct file_struct *file, STRUCT_STAT *st, int extra_accuracy)
473 #ifdef ST_MTIME_NSEC
474 uint32 f1_nsec = F_MOD_NSEC_or_0(file);
475 uint32 f2_nsec = (uint32)st->ST_MTIME_NSEC;
476 #else
477 uint32 f1_nsec = 0, f2_nsec = 0;
478 #endif
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)
489 int updated = 0;
490 stat_x sx2;
491 int change_uid, change_gid;
492 mode_t new_mode = file->mode;
493 int inherit;
495 if (!sxp) {
496 if (dry_run)
497 return 1;
498 if (link_stat(fname, &sx2.st, 0) < 0) {
499 rsyserr(FERROR_XFER, errno, "stat %s failed",
500 full_fname(fname));
501 return 0;
503 init_stat_x(&sx2);
504 sxp = &sx2;
505 inherit = !preserve_perms;
506 } else
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. */
512 new_mode |= S_ISGID;
515 if (daemon_chmod_modes && !S_ISLNK(new_mode))
516 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
518 #ifdef SUPPORT_ACLS
519 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
520 get_acl(fname, sxp);
521 #endif
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)) {
529 } else
530 #endif
531 if (change_uid || change_gid) {
532 if (DEBUG_GTE(OWN, 1)) {
533 if (change_uid) {
534 rprintf(FINFO,
535 "set uid of %s from %u to %u\n",
536 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
538 if (change_gid) {
539 rprintf(FINFO,
540 "set gid of %s from %u to %u\n",
541 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
544 if (am_root >= 0) {
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",
552 full_fname(fname));
553 goto cleanup;
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));
567 if (change_uid)
568 updated |= UPDATED_OWNER;
569 if (change_gid)
570 updated |= UPDATED_GROUP;
573 #ifdef SUPPORT_XATTRS
574 if (am_root < 0)
575 set_stat_xattr(fname, file, new_mode);
576 if (preserve_xattrs && fnamecmp)
577 set_xattr(fname, file, fnamecmp, sxp);
578 #endif
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;
583 else {
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;
592 if (sxp != &sx2)
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;
596 #ifdef ST_MTIME_NSEC
597 sx2.st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
598 #endif
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;
605 #ifdef ST_ATIME_NSEC
606 sx2.st.ST_ATIME_NSEC = 0;
607 #endif
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)) {
617 if (
618 #ifdef HAVE_GETATTRLIST
619 do_setattrlist_crtime(fname, file_crtime) == 0
620 #elif defined __CYGWIN__
621 do_SetFileTime(fname, file_crtime) == 0
622 #else
623 #error Unknown crtimes implementation
624 #endif
626 updated |= UPDATED_CRTIME;
629 #endif
630 if (updated & (UPDATED_MTIME|UPDATED_ATIME)) {
631 int ret = set_times(fname, &sx2.st);
632 if (ret < 0) {
633 rsyserr(FERROR_XFER, errno, "failed to set times on %s", full_fname(fname));
634 goto cleanup;
636 if (ret > 0) { /* ret == 1 if symlink could not be set */
637 updated &= ~(UPDATED_MTIME|UPDATED_ATIME);
638 file->flags |= FLAG_TIME_FAILED;
642 #ifdef SUPPORT_ACLS
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;
653 #endif
655 #ifdef HAVE_CHMOD
656 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
657 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
658 if (ret < 0) {
659 rsyserr(FERROR_XFER, errno,
660 "failed to set permissions on %s",
661 full_fname(fname));
662 goto cleanup;
664 if (ret == 0) /* ret == 1 if symlink could not be set */
665 updated |= UPDATED_MODE;
667 #endif
669 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
670 if (updated)
671 rprintf(FCLIENT, "%s\n", fname);
672 else
673 rprintf(FCLIENT, "%s is uptodate\n", fname);
675 cleanup:
676 if (sxp == &sx2)
677 free_stat_x(&sx2);
678 return updated;
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. */
694 msleep(400);
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)
699 exit_cleanup(0);
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;
710 return;
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)
726 int ret;
727 const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
729 if (inplace) {
730 if (DEBUG_GTE(RECV, 1))
731 rprintf(FINFO, "finishing %s\n", fname);
732 fnametmp = fname;
733 goto do_set_file_attrs;
736 if (make_backups > 0 && overwriting_basis) {
737 int ok = make_backup(fname, False);
738 if (!ok)
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);
752 if (ret < 0) {
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)
758 do_unlink(fnametmp);
759 return 0;
761 if (ret == 0) {
762 /* The file was moved into place (not copied), so it's done. */
763 return 1;
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;
769 do_set_file_attrs:
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);
777 return 0;
779 handle_partial_dir(temp_copy_name, PDIR_DELETE);
781 return 1;
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))
789 goto not_found;
791 while (ndx < flist->ndx_start-1) {
792 if (flist == first_flist)
793 goto not_found;
794 flist = flist->prev;
796 while (ndx >= flist->ndx_start + flist->used) {
797 if (!(flist = flist->next))
798 goto not_found;
800 return flist;
802 not_found:
803 if (fatal_error_loc) {
804 int first, last;
805 if (first_flist) {
806 first = first_flist->ndx_start - 1;
807 last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
808 } else {
809 first = 0;
810 last = -1;
812 rprintf(FERROR,
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);
817 return NULL;
820 const char *who_am_i(void)
822 if (am_starting_up)
823 return am_server ? "server" : "client";
824 return am_sender ? "sender"
825 : am_generator ? "generator"
826 : am_receiver ? "receiver"
827 : "Receiver"; /* pre-forked receiver */