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, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
24 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
27 #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
28 #include <libcharset.h>
29 #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
35 extern int preserve_perms;
36 extern int preserve_executability;
37 extern int preserve_times;
38 extern int omit_dir_times;
42 extern int am_generator;
43 extern int am_starting_up;
44 extern int allow_8bit_chars;
45 extern int protocol_version;
46 extern int preserve_uid;
47 extern int preserve_gid;
48 extern int incremental;
51 extern int keep_dirlinks;
52 extern int make_backups;
53 extern mode_t orig_umask;
54 extern struct file_list *cur_flist, *first_flist, *dir_flist;
55 extern struct chmod_mode_struct *daemon_chmod_modes;
57 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
58 iconv_t ic_chck = (iconv_t)-1;
60 static const char *default_charset(void)
62 #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
63 return locale_charset();
64 #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
65 return nl_langinfo(CODESET);
67 return ""; /* Works with (at the very least) gnu iconv... */
73 if (!am_server && !allow_8bit_chars) {
74 const char *defset = default_charset();
76 /* It's OK if this fails... */
77 ic_chck = iconv_open(defset, defset);
80 if (ic_chck == (iconv_t)-1) {
82 "note: iconv_open(\"%s\", \"%s\") failed (%d)"
83 " -- using isprint() instead of iconv().\n",
84 defset, defset, errno);
87 "note: iconv_open(\"%s\", \"%s\") succeeded.\n",
95 /* This is used by sender.c with a valid f_out, and by receive.c with
97 int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr,
98 uchar *type_ptr, char *buf, int *len_ptr)
101 struct file_list *flist;
102 uchar fnamecmp_type = FNAMECMP_FNAME;
103 int verbose_save, ndx;
107 ndx = read_ndx(f_in);
113 if (!incremental || am_sender)
115 if (ndx == NDX_FLIST_EOF) {
117 send_msg(MSG_FLIST_EOF, "", 0);
120 ndx = NDX_FLIST_OFFSET - ndx;
121 if (ndx < 0 || ndx >= dir_flist->count) {
122 ndx = NDX_FLIST_OFFSET - ndx;
124 "Invalid dir index: %d (%d - %d)\n",
125 ndx, NDX_FLIST_OFFSET,
126 NDX_FLIST_OFFSET - dir_flist->count);
127 exit_cleanup(RERR_PROTOCOL);
129 verbose_save = verbose;
130 verbose = 0; /* TODO allow verbose messages? */
132 /* Send everything read from f_in to msg_fd_out. */
133 send_msg_int(MSG_FLIST, ndx);
134 start_flist_forward(f_in);
135 flist = recv_file_list(f_in);
136 flist->parent_ndx = ndx;
137 stop_flist_forward();
139 verbose = verbose_save;
142 iflags = protocol_version >= 29 ? read_shortint(f_in)
143 : ITEM_TRANSFER | ITEM_MISSING_DATA;
145 /* Honor the old-style keep-alive indicator. */
146 if (protocol_version < 30
147 && ndx == cur_flist->count && iflags == ITEM_IS_NEW) {
149 maybe_send_keepalive();
153 if (!(flist = flist_for_ndx(ndx))) {
156 "Invalid file index: %d (%d - %d) with iflags %x [%s]\n",
157 ndx, first_flist->ndx_start + first_flist->ndx_start,
158 first_flist->prev->ndx_start + first_flist->ndx_start
159 + first_flist->prev->count - 1, iflags, who_am_i());
160 exit_cleanup(RERR_PROTOCOL);
164 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
165 fnamecmp_type = read_byte(f_in);
166 *type_ptr = fnamecmp_type;
168 if (iflags & ITEM_XNAME_FOLLOWS) {
169 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
170 exit_cleanup(RERR_PROTOCOL);
177 if (iflags & ITEM_TRANSFER) {
178 int i = ndx - cur_flist->ndx_start;
179 if (!S_ISREG(cur_flist->files[i]->mode)) {
181 "received request to transfer non-regular file: %d [%s]\n",
183 exit_cleanup(RERR_PROTOCOL);
185 } else if (f_out >= 0) {
186 write_ndx_and_attrs(f_out, ndx, iflags,
187 fnamecmp_type, buf, len);
197 void free_sums(struct sum_struct *s)
199 if (s->sums) free(s->sums);
203 /* This is only called when we aren't preserving permissions. Figure out what
204 * the permissions should be and return them merged back into the mode. */
205 mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists)
208 /* If the file already exists, we'll return the local permissions,
209 * possibly tweaked by the --executability option. */
211 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
212 if (preserve_executability && S_ISREG(flist_mode)) {
213 /* If the source file is executable, grant execute
214 * rights to everyone who can read, but ONLY if the
215 * file isn't already executable. */
216 if (!(flist_mode & 0111))
218 else if (!(stat_mode & 0111))
219 new_mode |= (new_mode & 0444) >> 2;
222 /* Apply the umask and turn off special permissions. */
223 new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask));
228 int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
233 int change_uid, change_gid;
234 mode_t new_mode = file->mode;
239 if (link_stat(fname, &st2, 0) < 0) {
240 rsyserr(FERROR, errno, "stat %s failed",
245 if (!preserve_perms && S_ISDIR(new_mode)
246 && st->st_mode & S_ISGID) {
247 /* We just created this directory and its setgid
248 * bit is on, so make sure it stays on. */
253 if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
254 flags |= ATTRS_SKIP_MTIME;
255 if (!(flags & ATTRS_SKIP_MTIME)
256 && cmp_time(st->st_mtime, file->modtime) != 0) {
257 int ret = set_modtime(fname, file->modtime, st->st_mode);
259 rsyserr(FERROR, errno, "failed to set times on %s",
263 if (ret == 0) /* ret == 1 if symlink could not be set */
267 change_uid = am_root && preserve_uid && st->st_uid != F_UID(file);
268 change_gid = preserve_gid && F_GID(file) != GID_NONE
269 && st->st_gid != F_GID(file);
270 #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
271 if (S_ISLNK(st->st_mode))
275 if (change_uid || change_gid) {
279 "set uid of %s from %ld to %ld\n",
281 (long)st->st_uid, (long)F_UID(file));
285 "set gid of %s from %ld to %ld\n",
287 (long)st->st_gid, (long)F_GID(file));
291 change_uid ? F_UID(file) : st->st_uid,
292 change_gid ? F_GID(file) : st->st_gid) != 0) {
293 /* shouldn't have attempted to change uid or gid
294 * unless have the privilege */
295 rsyserr(FERROR, errno, "%s %s failed",
296 change_uid ? "chown" : "chgrp",
300 /* a lchown had been done - we have to re-stat if the
301 * destination had the setuid or setgid bits set due
302 * to the side effect of the chown call */
303 if (st->st_mode & (S_ISUID | S_ISGID)) {
305 keep_dirlinks && S_ISDIR(st->st_mode));
310 if (daemon_chmod_modes && !S_ISLNK(new_mode))
311 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
313 if (!BITS_EQUAL(st->st_mode, new_mode, CHMOD_BITS)) {
314 int ret = do_chmod(fname, new_mode);
316 rsyserr(FERROR, errno,
317 "failed to set permissions on %s",
321 if (ret == 0) /* ret == 1 if symlink could not be set */
326 if (verbose > 1 && flags & ATTRS_REPORT) {
328 rprintf(FCLIENT, "%s\n", fname);
330 rprintf(FCLIENT, "%s is uptodate\n", fname);
335 RETSIGTYPE sig_int(UNUSED(int val))
337 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
338 * for a password, then our cleanup's sending of a SIGUSR1
339 * signal to all our children may kill ssh before it has a
340 * chance to restore the tty settings (i.e. turn echo back
341 * on). By sleeping for a short time, ssh gets a bigger
342 * chance to do the right thing. If child processes are
343 * not ssh waiting for a password, then this tiny delay
344 * shouldn't hurt anything. */
346 exit_cleanup(RERR_SIGNAL);
349 /* Finish off a file transfer: renaming the file and setting the file's
350 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
351 * NULL and the robust_rename() call is forced to copy the temp file, we
352 * stage the file into the partial-dir and then rename it into place. */
353 void finish_transfer(char *fname, char *fnametmp, char *partialptr,
354 struct file_struct *file, int ok_to_set_time,
355 int overwriting_basis)
361 rprintf(FINFO, "finishing %s\n", fname);
363 goto do_set_file_attrs;
366 if (make_backups > 0 && overwriting_basis && !make_backup(fname))
369 /* Change permissions before putting the file into place. */
370 set_file_attrs(fnametmp, file, NULL,
371 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
373 /* move tmp file over real file */
375 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
376 ret = robust_rename(fnametmp, fname, partialptr,
377 file->mode & INITACCESSPERMS);
379 rsyserr(FERROR, errno, "%s %s -> \"%s\"",
380 ret == -2 ? "copy" : "rename",
381 full_fname(fnametmp), fname);
386 /* The file was moved into place (not copied), so it's done. */
389 /* The file was copied, so tweak the perms of the copied file. If it
390 * was copied to partialptr, move it into its final destination. */
391 fnametmp = partialptr ? partialptr : fname;
394 set_file_attrs(fnametmp, file, NULL,
395 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
398 if (do_rename(fnametmp, fname) < 0) {
399 rsyserr(FERROR, errno, "rename %s -> \"%s\"",
400 full_fname(fnametmp), fname);
402 handle_partial_dir(partialptr, PDIR_DELETE);
406 struct file_list *flist_for_ndx(int ndx)
408 struct file_list *flist = cur_flist;
413 while (ndx < flist->ndx_start) {
414 if (flist == first_flist)
418 while (ndx >= flist->ndx_start + flist->count) {
419 if (!(flist = flist->next))
425 const char *who_am_i(void)
428 return am_server ? "server" : "client";
429 return am_sender ? "sender" : am_generator ? "generator" : "receiver";