Document the changed name of --no-i-r.
[rsync/rsync.git] / rsync.c
... / ...
CommitLineData
1/*
2 * Routines common to more than one of the rsync processes.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2007 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23#if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
24#include <libcharset.h>
25#elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
26#include <langinfo.h>
27#endif
28
29extern int verbose;
30extern int dry_run;
31extern int preserve_acls;
32extern int preserve_xattrs;
33extern int preserve_perms;
34extern int preserve_executability;
35extern int preserve_times;
36extern int am_root;
37extern int am_server;
38extern int am_sender;
39extern int am_generator;
40extern int am_starting_up;
41extern int allow_8bit_chars;
42extern int protocol_version;
43extern int uid_ndx;
44extern int gid_ndx;
45extern int inc_recurse;
46extern int inplace;
47extern int flist_eof;
48extern int keep_dirlinks;
49extern int make_backups;
50extern struct file_list *cur_flist, *first_flist, *dir_flist;
51extern struct chmod_mode_struct *daemon_chmod_modes;
52#ifdef ICONV_OPTION
53extern char *iconv_opt;
54#endif
55
56#ifdef ICONV_CONST
57iconv_t ic_chck = (iconv_t)-1;
58# ifdef ICONV_OPTION
59iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
60# endif
61
62static const char *default_charset(void)
63{
64# if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
65 return locale_charset();
66# elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
67 return nl_langinfo(CODESET);
68# else
69 return ""; /* Works with (at the very least) gnu iconv... */
70# endif
71}
72
73void setup_iconv()
74{
75 const char *defset = default_charset();
76# ifdef ICONV_OPTION
77 const char *charset;
78 char *cp;
79# endif
80
81 if (!am_server && !allow_8bit_chars) {
82
83 /* It's OK if this fails... */
84 ic_chck = iconv_open(defset, defset);
85
86 if (verbose > 3) {
87 if (ic_chck == (iconv_t)-1) {
88 rprintf(FINFO,
89 "note: iconv_open(\"%s\", \"%s\") failed (%d)"
90 " -- using isprint() instead of iconv().\n",
91 defset, defset, errno);
92 } else {
93 rprintf(FINFO,
94 "note: iconv_open(\"%s\", \"%s\") succeeded.\n",
95 defset, defset);
96 }
97 }
98 }
99
100# ifdef ICONV_OPTION
101 if (!iconv_opt)
102 return;
103
104 if ((cp = strchr(iconv_opt, ',')) != NULL) {
105 if (am_server) /* A local transfer needs this. */
106 iconv_opt = cp + 1;
107 else
108 *cp = '\0';
109 }
110
111 if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0'))
112 charset = defset;
113 else
114 charset = iconv_opt;
115
116 if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) {
117 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
118 UTF8_CHARSET, charset);
119 exit_cleanup(RERR_UNSUPPORTED);
120 }
121
122 if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) {
123 rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
124 charset, UTF8_CHARSET);
125 exit_cleanup(RERR_UNSUPPORTED);
126 }
127
128 if (verbose > 1) {
129 rprintf(FINFO, "%s charset: %s\n",
130 am_server ? "server" : "client",
131 *charset ? charset : "[LOCALE]");
132 }
133# endif
134}
135
136# ifdef ICONV_OPTION
137
138/* This function converts the characters in the "in" xbuf into characters
139 * in the "out" xbuf. The "len" of the "in" xbuf is used starting from its
140 * "pos". The "size" of the "out" xbuf restricts how many characters can be
141 * stored, starting at its "pos+len" position. Note that the last byte of
142 * the buffer is never used, which reserves space for a terminating '\0'.
143 * We return a 0 on success or a -1 on error. An error also sets errno to
144 * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
145 * The "in" xbuf is altered to update "pos" and "len". The "out" xbuf has
146 * data appended, and its "len" incremented. If ICB_EXPAND_OUT is set in
147 * "flags", the "out" xbuf will also be allocated if empty, and expanded if
148 * too small (so E2BIG will not be returned). If ICB_INCLUDE_BAD is set in
149 * "flags", any badly-encoded chars are included verbatim in the "out" xbuf,
150 * so EILSEQ will not be returned. Likewise for ICB_INCLUDE_INCOMPLETE with
151 * respect to an incomplete multi-byte char at the end, which ensures that
152 * EINVAL is not returned. Anytime "in.pos" is 0 we will reset the iconv()
153 * state prior to processing the characters. */
154int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
155{
156 ICONV_CONST char *ibuf;
157 size_t icnt, ocnt;
158 char *obuf;
159
160 if (!out->size && flags & ICB_EXPAND_OUT)
161 alloc_xbuf(out, 1024);
162
163 if (!in->pos)
164 iconv(ic, NULL, 0, NULL, 0);
165
166 ibuf = in->buf + in->pos;
167 icnt = in->len;
168
169 obuf = out->buf + (out->pos + out->len);
170 ocnt = out->size - (out->pos + out->len) - 1;
171
172 while (icnt) {
173 while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
174 if (errno == EINTR)
175 continue;
176 if (errno == EINVAL) {
177 if (!(flags & ICB_INCLUDE_INCOMPLETE))
178 goto finish;
179 } else if (errno == EILSEQ) {
180 if (!(flags & ICB_INCLUDE_BAD))
181 goto finish;
182 } else {
183 size_t opos = obuf - out->buf;
184 if (!(flags & ICB_EXPAND_OUT)) {
185 errno = E2BIG;
186 goto finish;
187 }
188 realloc_xbuf(out, out->size + 1024);
189 obuf = out->buf + opos;
190 ocnt += 1024;
191 continue;
192 }
193 *obuf++ = *ibuf++;
194 ocnt--, icnt--;
195 }
196 }
197
198 errno = 0;
199
200 finish:
201 in->len = icnt;
202 in->pos = ibuf - in->buf;
203 out->len = obuf - out->buf - out->pos;
204
205 return errno ? -1 : 0;
206}
207# endif
208#endif
209
210int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
211 char *buf, int *len_ptr)
212{
213 int len, iflags = 0;
214 struct file_list *flist;
215 uchar fnamecmp_type = FNAMECMP_FNAME;
216 int ndx;
217
218 read_loop:
219 while (1) {
220 ndx = read_ndx(f_in);
221
222 if (ndx >= 0)
223 break;
224 if (ndx == NDX_DONE)
225 return ndx;
226 if (!inc_recurse || am_sender)
227 goto invalid_ndx;
228 if (ndx == NDX_FLIST_EOF) {
229 flist_eof = 1;
230 send_msg(MSG_FLIST_EOF, "", 0, 0);
231 continue;
232 }
233 ndx = NDX_FLIST_OFFSET - ndx;
234 if (ndx < 0 || ndx >= dir_flist->used) {
235 ndx = NDX_FLIST_OFFSET - ndx;
236 rprintf(FERROR,
237 "[%s] Invalid dir index: %d (%d - %d)\n",
238 who_am_i(), ndx, NDX_FLIST_OFFSET,
239 NDX_FLIST_OFFSET - dir_flist->used + 1);
240 exit_cleanup(RERR_PROTOCOL);
241 }
242
243 /* Send everything read from f_in to msg_fd_out. */
244 send_msg_int(MSG_FLIST, ndx);
245 start_flist_forward(f_in);
246 if (verbose > 3) {
247 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
248 who_am_i(), ndx);
249 }
250 flist = recv_file_list(f_in);
251 flist->parent_ndx = ndx;
252 stop_flist_forward();
253 }
254
255 iflags = protocol_version >= 29 ? read_shortint(f_in)
256 : ITEM_TRANSFER | ITEM_MISSING_DATA;
257
258 /* Honor the old-style keep-alive indicator. */
259 if (protocol_version < 30
260 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
261 if (am_sender)
262 maybe_send_keepalive();
263 goto read_loop;
264 }
265
266 if (!(flist = flist_for_ndx(ndx))) {
267 invalid_ndx:
268 rprintf(FERROR,
269 "Invalid file index: %d (%d - %d) with iflags %x [%s]\n",
270 ndx, first_flist->ndx_start - 1, first_flist->prev->ndx_end,
271 iflags, who_am_i());
272 exit_cleanup(RERR_PROTOCOL);
273 }
274 cur_flist = flist;
275
276 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
277 fnamecmp_type = read_byte(f_in);
278 *type_ptr = fnamecmp_type;
279
280 if (iflags & ITEM_XNAME_FOLLOWS) {
281 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
282 exit_cleanup(RERR_PROTOCOL);
283 } else {
284 *buf = '\0';
285 len = -1;
286 }
287 *len_ptr = len;
288
289 if (iflags & ITEM_TRANSFER) {
290 int i = ndx - cur_flist->ndx_start;
291 if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
292 rprintf(FERROR,
293 "received request to transfer non-regular file: %d [%s]\n",
294 ndx, who_am_i());
295 exit_cleanup(RERR_PROTOCOL);
296 }
297 }
298
299 *iflag_ptr = iflags;
300 return ndx;
301}
302
303/*
304 free a sums struct
305 */
306void free_sums(struct sum_struct *s)
307{
308 if (s->sums) free(s->sums);
309 free(s);
310}
311
312/* This is only called when we aren't preserving permissions. Figure out what
313 * the permissions should be and return them merged back into the mode. */
314mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
315 int exists)
316{
317 int new_mode;
318 /* If the file already exists, we'll return the local permissions,
319 * possibly tweaked by the --executability option. */
320 if (exists) {
321 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
322 if (preserve_executability && S_ISREG(flist_mode)) {
323 /* If the source file is executable, grant execute
324 * rights to everyone who can read, but ONLY if the
325 * file isn't already executable. */
326 if (!(flist_mode & 0111))
327 new_mode &= ~0111;
328 else if (!(stat_mode & 0111))
329 new_mode |= (new_mode & 0444) >> 2;
330 }
331 } else {
332 /* Apply destination default permissions and turn
333 * off special permissions. */
334 new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
335 }
336 return new_mode;
337}
338
339int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
340 const char *fnamecmp, int flags)
341{
342 int updated = 0;
343 statx sx2;
344 int change_uid, change_gid;
345 mode_t new_mode = file->mode;
346 int inherit;
347
348 if (!sxp) {
349 if (dry_run)
350 return 1;
351 if (link_stat(fname, &sx2.st, 0) < 0) {
352 rsyserr(FERROR, errno, "stat %s failed",
353 full_fname(fname));
354 return 0;
355 }
356#ifdef SUPPORT_ACLS
357 sx2.acc_acl = sx2.def_acl = NULL;
358#endif
359#ifdef SUPPORT_XATTRS
360 sx2.xattr = NULL;
361#endif
362 sxp = &sx2;
363 inherit = !preserve_perms;
364 } else
365 inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED;
366
367 if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) {
368 /* We just created this directory and its setgid
369 * bit is on, so make sure it stays on. */
370 new_mode |= S_ISGID;
371 }
372
373#ifdef SUPPORT_ACLS
374 if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
375 get_acl(fname, sxp);
376#endif
377
378#ifdef SUPPORT_XATTRS
379 if (preserve_xattrs && fnamecmp)
380 set_xattr(fname, file, fnamecmp, sxp);
381 if (am_root < 0)
382 set_stat_xattr(fname, file);
383#endif
384
385 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1))
386 flags |= ATTRS_SKIP_MTIME;
387 if (!(flags & ATTRS_SKIP_MTIME)
388 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
389 int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
390 if (ret < 0) {
391 rsyserr(FERROR, errno, "failed to set times on %s",
392 full_fname(fname));
393 goto cleanup;
394 }
395 if (ret == 0) /* ret == 1 if symlink could not be set */
396 updated = 1;
397 }
398
399 change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
400 change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
401 && sxp->st.st_gid != (gid_t)F_GROUP(file);
402#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
403 if (S_ISLNK(sxp->st.st_mode))
404 ;
405 else
406#endif
407 if (change_uid || change_gid) {
408 if (verbose > 2) {
409 if (change_uid) {
410 rprintf(FINFO,
411 "set uid of %s from %u to %u\n",
412 fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
413 }
414 if (change_gid) {
415 rprintf(FINFO,
416 "set gid of %s from %u to %u\n",
417 fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
418 }
419 }
420 if (am_root < 0) {
421 ;
422 } else if (do_lchown(fname,
423 change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid,
424 change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) {
425 /* shouldn't have attempted to change uid or gid
426 * unless have the privilege */
427 rsyserr(FERROR, errno, "%s %s failed",
428 change_uid ? "chown" : "chgrp",
429 full_fname(fname));
430 goto cleanup;
431 } else
432 /* a lchown had been done - we have to re-stat if the
433 * destination had the setuid or setgid bits set due
434 * to the side effect of the chown call */
435 if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
436 link_stat(fname, &sxp->st,
437 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
438 }
439 updated = 1;
440 }
441
442 if (daemon_chmod_modes && !S_ISLNK(new_mode))
443 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
444
445#ifdef SUPPORT_ACLS
446 /* It's OK to call set_acl() now, even for a dir, as the generator
447 * will enable owner-writability using chmod, if necessary.
448 *
449 * If set_acl() changes permission bits in the process of setting
450 * an access ACL, it changes sxp->st.st_mode so we know whether we
451 * need to chmod(). */
452 if (preserve_acls && !S_ISLNK(new_mode) && set_acl(fname, file, sxp) == 0)
453 updated = 1;
454#endif
455
456#ifdef HAVE_CHMOD
457 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
458 int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
459 if (ret < 0) {
460 rsyserr(FERROR, errno,
461 "failed to set permissions on %s",
462 full_fname(fname));
463 goto cleanup;
464 }
465 if (ret == 0) /* ret == 1 if symlink could not be set */
466 updated = 1;
467 }
468#endif
469
470 if (verbose > 1 && flags & ATTRS_REPORT) {
471 if (updated)
472 rprintf(FCLIENT, "%s\n", fname);
473 else
474 rprintf(FCLIENT, "%s is uptodate\n", fname);
475 }
476 cleanup:
477 if (sxp == &sx2) {
478#ifdef SUPPORT_ACLS
479 if (preserve_acls)
480 free_acl(&sx2);
481#endif
482#ifdef SUPPORT_XATTRS
483 if (preserve_xattrs)
484 free_xattr(&sx2);
485#endif
486 }
487 return updated;
488}
489
490RETSIGTYPE sig_int(UNUSED(int val))
491{
492 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
493 * for a password, then our cleanup's sending of a SIGUSR1
494 * signal to all our children may kill ssh before it has a
495 * chance to restore the tty settings (i.e. turn echo back
496 * on). By sleeping for a short time, ssh gets a bigger
497 * chance to do the right thing. If child processes are
498 * not ssh waiting for a password, then this tiny delay
499 * shouldn't hurt anything. */
500 msleep(400);
501 exit_cleanup(RERR_SIGNAL);
502}
503
504/* Finish off a file transfer: renaming the file and setting the file's
505 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
506 * NULL and the robust_rename() call is forced to copy the temp file, we
507 * stage the file into the partial-dir and then rename it into place. */
508void finish_transfer(const char *fname, const char *fnametmp,
509 const char *fnamecmp, const char *partialptr,
510 struct file_struct *file, int ok_to_set_time,
511 int overwriting_basis)
512{
513 int ret;
514
515 if (inplace) {
516 if (verbose > 2)
517 rprintf(FINFO, "finishing %s\n", fname);
518 fnametmp = fname;
519 goto do_set_file_attrs;
520 }
521
522 if (make_backups > 0 && overwriting_basis && !make_backup(fname))
523 return;
524
525 /* Change permissions before putting the file into place. */
526 set_file_attrs(fnametmp, file, NULL, fnamecmp,
527 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
528
529 /* move tmp file over real file */
530 if (verbose > 2)
531 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
532 ret = robust_rename(fnametmp, fname, partialptr,
533 file->mode & INITACCESSPERMS);
534 if (ret < 0) {
535 rsyserr(FERROR, errno, "%s %s -> \"%s\"",
536 ret == -2 ? "copy" : "rename",
537 full_fname(fnametmp), fname);
538 do_unlink(fnametmp);
539 return;
540 }
541 if (ret == 0) {
542 /* The file was moved into place (not copied), so it's done. */
543 return;
544 }
545 /* The file was copied, so tweak the perms of the copied file. If it
546 * was copied to partialptr, move it into its final destination. */
547 fnametmp = partialptr ? partialptr : fname;
548
549 do_set_file_attrs:
550 set_file_attrs(fnametmp, file, NULL, fnamecmp,
551 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
552
553 if (partialptr) {
554 if (do_rename(fnametmp, fname) < 0) {
555 rsyserr(FERROR, errno, "rename %s -> \"%s\"",
556 full_fname(fnametmp), fname);
557 } else
558 handle_partial_dir(partialptr, PDIR_DELETE);
559 }
560}
561
562struct file_list *flist_for_ndx(int ndx)
563{
564 struct file_list *flist = cur_flist;
565
566 if (!flist && !(flist = first_flist))
567 return NULL;
568
569 while (ndx < flist->ndx_start-1) {
570 if (flist == first_flist)
571 return NULL;
572 flist = flist->prev;
573 }
574 while (ndx > flist->ndx_end) {
575 if (!(flist = flist->next))
576 return NULL;
577 }
578 return flist;
579}
580
581const char *who_am_i(void)
582{
583 if (am_starting_up)
584 return am_server ? "server" : "client";
585 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
586}