Update MAINTAINER_TZ_OFFSET on release.
[rsync.git] / rsync.c
blobb130aba56fb63e093e7508284b26d9dfc23a969f
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) {
441 free(s->sums);
442 free(s->sum2_array);
444 free(s);
447 /* This is only called when we aren't preserving permissions. Figure out what
448 * the permissions should be and return them merged back into the mode. */
449 mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
450 int exists)
452 int new_mode;
453 /* If the file already exists, we'll return the local permissions,
454 * possibly tweaked by the --executability option. */
455 if (exists) {
456 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
457 if (preserve_executability && S_ISREG(flist_mode)) {
458 /* If the source file is executable, grant execute
459 * rights to everyone who can read, but ONLY if the
460 * file isn't already executable. */
461 if (!(flist_mode & 0111))
462 new_mode &= ~0111;
463 else if (!(stat_mode & 0111))
464 new_mode |= (new_mode & 0444) >> 2;
466 } else {
467 /* Apply destination default permissions and turn
468 * off special permissions. */
469 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
471 return new_mode;
474 static int same_mtime(struct file_struct *file, STRUCT_STAT *st, int extra_accuracy)
476 #ifdef ST_MTIME_NSEC
477 uint32 f1_nsec = F_MOD_NSEC_or_0(file);
478 uint32 f2_nsec = (uint32)st->ST_MTIME_NSEC;
479 #else
480 uint32 f1_nsec = 0, f2_nsec = 0;
481 #endif
483 if (extra_accuracy) /* ignore modify_window when setting the time after a transfer or checksum check */
484 return file->modtime == st->st_mtime && f1_nsec == f2_nsec;
486 return same_time(file->modtime, f1_nsec, st->st_mtime , f2_nsec);
489 int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
490 const char *fnamecmp, int flags)
492 int updated = 0;
493 stat_x sx2;
494 int change_uid, change_gid;
495 mode_t new_mode = file->mode;
496 int inherit;
498 if (!sxp) {
499 if (dry_run)
500 return 1;
501 if (link_stat(fname, &sx2.st, 0) < 0) {
502 rsyserr(FERROR_XFER, errno, "stat %s failed",
503 full_fname(fname));
504 return 0;
506 init_stat_x(&sx2);
507 sxp = &sx2;
508 inherit = !preserve_perms;
509 } else
510 inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED;
512 if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) {
513 /* We just created this directory and its setgid
514 * bit is on, so make sure it stays on. */
515 new_mode |= S_ISGID;
518 if (daemon_chmod_modes && !S_ISLNK(new_mode))
519 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
521 #ifdef SUPPORT_ACLS
522 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
523 get_acl(fname, sxp);
524 #endif
526 change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
527 change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
528 && sxp->st.st_gid != (gid_t)F_GROUP(file);
529 #ifndef CAN_CHOWN_SYMLINK
530 if (S_ISLNK(sxp->st.st_mode)) {
532 } else
533 #endif
534 if (change_uid || change_gid) {
535 if (DEBUG_GTE(OWN, 1)) {
536 if (change_uid) {
537 rprintf(FINFO,
538 "set uid of %s from %u to %u\n",
539 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
541 if (change_gid) {
542 rprintf(FINFO,
543 "set gid of %s from %u to %u\n",
544 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
547 if (am_root >= 0) {
548 uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid;
549 gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid;
550 if (do_lchown(fname, uid, gid) != 0) {
551 /* We shouldn't have attempted to change uid
552 * or gid unless have the privilege. */
553 rsyserr(FERROR_XFER, errno, "%s %s failed",
554 change_uid ? "chown" : "chgrp",
555 full_fname(fname));
556 goto cleanup;
558 if (uid == (uid_t)-1 && sxp->st.st_uid != (uid_t)-1)
559 rprintf(FERROR_XFER, "uid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname));
560 if (gid == (gid_t)-1 && sxp->st.st_gid != (gid_t)-1)
561 rprintf(FERROR_XFER, "gid 4294967295 (-1) is impossible to set on %s\n", full_fname(fname));
562 /* A lchown had been done, so we need to re-stat if
563 * the destination had the setuid or setgid bits set
564 * (due to the side effect of the chown call). */
565 if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
566 link_stat(fname, &sxp->st,
567 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
570 if (change_uid)
571 updated |= UPDATED_OWNER;
572 if (change_gid)
573 updated |= UPDATED_GROUP;
576 #ifdef SUPPORT_XATTRS
577 if (am_root < 0)
578 set_stat_xattr(fname, file, new_mode);
579 if (preserve_xattrs && fnamecmp)
580 set_xattr(fname, file, fnamecmp, sxp);
581 #endif
583 if ((omit_dir_times && S_ISDIR(sxp->st.st_mode))
584 || (omit_link_times && S_ISLNK(sxp->st.st_mode)))
585 flags |= ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME;
586 else {
587 if (!preserve_mtimes)
588 flags |= ATTRS_SKIP_MTIME;
589 if (!atimes_ndx || S_ISDIR(sxp->st.st_mode))
590 flags |= ATTRS_SKIP_ATIME;
591 /* Don't set the creation date on the root folder of an HFS+ volume. */
592 if (sxp->st.st_ino == 2 && S_ISDIR(sxp->st.st_mode))
593 flags |= ATTRS_SKIP_CRTIME;
595 if (sxp != &sx2)
596 memcpy(&sx2.st, &sxp->st, sizeof sx2.st);
597 if (!(flags & ATTRS_SKIP_MTIME) && !same_mtime(file, &sxp->st, flags & ATTRS_ACCURATE_TIME)) {
598 sx2.st.st_mtime = file->modtime;
599 #ifdef ST_MTIME_NSEC
600 sx2.st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
601 #endif
602 updated |= UPDATED_MTIME;
604 if (!(flags & ATTRS_SKIP_ATIME)) {
605 time_t file_atime = F_ATIME(file);
606 if (flags & ATTRS_ACCURATE_TIME || !same_time(sxp->st.st_atime, 0, file_atime, 0)) {
607 sx2.st.st_atime = file_atime;
608 #ifdef ST_ATIME_NSEC
609 sx2.st.ST_ATIME_NSEC = 0;
610 #endif
611 updated |= UPDATED_ATIME;
614 #ifdef SUPPORT_CRTIMES
615 if (crtimes_ndx && !(flags & ATTRS_SKIP_CRTIME)) {
616 time_t file_crtime = F_CRTIME(file);
617 if (sxp->crtime == 0)
618 sxp->crtime = get_create_time(fname, &sxp->st);
619 if (!same_time(sxp->crtime, 0L, file_crtime, 0L)) {
620 if (
621 #ifdef HAVE_GETATTRLIST
622 do_setattrlist_crtime(fname, file_crtime) == 0
623 #elif defined __CYGWIN__
624 do_SetFileTime(fname, file_crtime) == 0
625 #else
626 #error Unknown crtimes implementation
627 #endif
629 updated |= UPDATED_CRTIME;
632 #endif
633 if (updated & (UPDATED_MTIME|UPDATED_ATIME)) {
634 int ret = set_times(fname, &sx2.st);
635 if (ret < 0) {
636 rsyserr(FERROR_XFER, errno, "failed to set times on %s", full_fname(fname));
637 goto cleanup;
639 if (ret > 0) { /* ret == 1 if symlink could not be set */
640 updated &= ~(UPDATED_MTIME|UPDATED_ATIME);
641 file->flags |= FLAG_TIME_FAILED;
645 #ifdef SUPPORT_ACLS
646 /* It's OK to call set_acl() now, even for a dir, as the generator
647 * will enable owner-writability using chmod, if necessary.
649 * If set_acl() changes permission bits in the process of setting
650 * an access ACL, it changes sxp->st.st_mode so we know whether we
651 * need to chmod(). */
652 if (preserve_acls && !S_ISLNK(new_mode)) {
653 if (set_acl(fname, file, sxp, new_mode) > 0)
654 updated |= UPDATED_ACLS;
656 #endif
658 #ifdef HAVE_CHMOD
659 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
660 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
661 if (ret < 0) {
662 rsyserr(FERROR_XFER, errno,
663 "failed to set permissions on %s",
664 full_fname(fname));
665 goto cleanup;
667 if (ret == 0) /* ret == 1 if symlink could not be set */
668 updated |= UPDATED_MODE;
670 #endif
672 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
673 if (updated)
674 rprintf(FCLIENT, "%s\n", fname);
675 else
676 rprintf(FCLIENT, "%s is uptodate\n", fname);
678 cleanup:
679 if (sxp == &sx2)
680 free_stat_x(&sx2);
681 return updated;
684 /* This is only called for SIGINT, SIGHUP, and SIGTERM. */
685 void sig_int(int sig_num)
687 called_from_signal_handler = 1;
689 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
690 * for a password, then our cleanup's sending of a SIGUSR1
691 * signal to all our children may kill ssh before it has a
692 * chance to restore the tty settings (i.e. turn echo back
693 * on). By sleeping for a short time, ssh gets a bigger
694 * chance to do the right thing. If child processes are
695 * not ssh waiting for a password, then this tiny delay
696 * shouldn't hurt anything. */
697 msleep(400);
699 /* If we're an rsync daemon listener (not a daemon server),
700 * we'll exit with status 0 if we received SIGTERM. */
701 if (am_daemon && !am_server && sig_num == SIGTERM)
702 exit_cleanup(0);
704 /* If the signal arrived on the server side (or for the receiver
705 * process on the client), we want to try to do a controlled shutdown
706 * that lets the client side (generator process) know what happened.
707 * To do this, we set a flag and let the normal process handle the
708 * shutdown. We only attempt this if multiplexed IO is in effect and
709 * we didn't already set the flag. */
710 if (!got_kill_signal && (am_server || am_receiver)) {
711 got_kill_signal = sig_num;
712 called_from_signal_handler = 0;
713 return;
716 exit_cleanup(RERR_SIGNAL);
719 /* Finish off a file transfer: renaming the file and setting the file's
720 * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
721 * call is forced to copy the temp file and partialptr is both non-NULL and
722 * not an absolute path, we stage the file into the partial-dir and then
723 * rename it into place. This returns 1 on success or 0 on failure. */
724 int finish_transfer(const char *fname, const char *fnametmp,
725 const char *fnamecmp, const char *partialptr,
726 struct file_struct *file, int ok_to_set_time,
727 int overwriting_basis)
729 int ret;
730 const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
732 if (inplace) {
733 if (DEBUG_GTE(RECV, 1))
734 rprintf(FINFO, "finishing %s\n", fname);
735 fnametmp = fname;
736 goto do_set_file_attrs;
739 if (make_backups > 0 && overwriting_basis) {
740 int ok = make_backup(fname, False);
741 if (!ok)
742 exit_cleanup(RERR_FILEIO);
743 if (ok == 1 && fnamecmp == fname)
744 fnamecmp = get_backup_name(fname);
747 /* Change permissions before putting the file into place. */
748 set_file_attrs(fnametmp, file, NULL, fnamecmp,
749 ok_to_set_time ? ATTRS_ACCURATE_TIME : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME);
751 /* move tmp file over real file */
752 if (DEBUG_GTE(RECV, 1))
753 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
754 ret = robust_rename(fnametmp, fname, temp_copy_name, file->mode);
755 if (ret < 0) {
756 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
757 ret == -2 ? "copy" : "rename",
758 full_fname(fnametmp), fname);
759 if (!partialptr || (ret == -2 && temp_copy_name)
760 || robust_rename(fnametmp, partialptr, NULL, file->mode) < 0)
761 do_unlink(fnametmp);
762 return 0;
764 if (ret == 0) {
765 /* The file was moved into place (not copied), so it's done. */
766 return 1;
768 /* The file was copied, so tweak the perms of the copied file. If it
769 * was copied to partialptr, move it into its final destination. */
770 fnametmp = temp_copy_name ? temp_copy_name : fname;
772 do_set_file_attrs:
773 set_file_attrs(fnametmp, file, NULL, fnamecmp,
774 ok_to_set_time ? ATTRS_ACCURATE_TIME : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME);
776 if (temp_copy_name) {
777 if (do_rename(fnametmp, fname) < 0) {
778 rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"",
779 full_fname(fnametmp), fname);
780 return 0;
782 handle_partial_dir(temp_copy_name, PDIR_DELETE);
784 return 1;
787 struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc)
789 struct file_list *flist = cur_flist;
791 if (!flist && !(flist = first_flist))
792 goto not_found;
794 while (ndx < flist->ndx_start-1) {
795 if (flist == first_flist)
796 goto not_found;
797 flist = flist->prev;
799 while (ndx >= flist->ndx_start + flist->used) {
800 if (!(flist = flist->next))
801 goto not_found;
803 return flist;
805 not_found:
806 if (fatal_error_loc) {
807 int first, last;
808 if (first_flist) {
809 first = first_flist->ndx_start - 1;
810 last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
811 } else {
812 first = 0;
813 last = -1;
815 rprintf(FERROR,
816 "File-list index %d not in %d - %d (%s) [%s]\n",
817 ndx, first, last, fatal_error_loc, who_am_i());
818 exit_cleanup(RERR_PROTOCOL);
820 return NULL;
823 const char *who_am_i(void)
825 if (am_starting_up)
826 return am_server ? "server" : "client";
827 return am_sender ? "sender"
828 : am_generator ? "generator"
829 : am_receiver ? "receiver"
830 : "Receiver"; /* pre-forked receiver */