- Handle the new incremental-recursion mode.
[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, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23#include "rsync.h"
24#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
25#include <iconv.h>
26#endif
27#if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
28#include <libcharset.h>
29#elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
30#include <langinfo.h>
31#endif
32
33extern int verbose;
34extern int dry_run;
35extern int preserve_perms;
36extern int preserve_executability;
37extern int preserve_times;
38extern int omit_dir_times;
39extern int am_root;
40extern int am_server;
41extern int am_sender;
42extern int am_generator;
43extern int am_starting_up;
44extern int allow_8bit_chars;
45extern int protocol_version;
46extern int preserve_uid;
47extern int preserve_gid;
48extern int inplace;
49extern int keep_dirlinks;
50extern int make_backups;
51extern mode_t orig_umask;
52extern struct stats stats;
53extern struct file_list *the_file_list;
54extern struct chmod_mode_struct *daemon_chmod_modes;
55
56#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
57iconv_t ic_chck = (iconv_t)-1;
58
59static const char *default_charset(void)
60{
61#if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
62 return locale_charset();
63#elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
64 return nl_langinfo(CODESET);
65#else
66 return ""; /* Works with (at the very least) gnu iconv... */
67#endif
68}
69
70void setup_iconv()
71{
72 if (!am_server && !allow_8bit_chars) {
73 const char *defset = default_charset();
74
75 /* It's OK if this fails... */
76 ic_chck = iconv_open(defset, defset);
77
78 if (verbose > 3) {
79 if (ic_chck == (iconv_t)-1) {
80 rprintf(FINFO,
81 "note: iconv_open(\"%s\", \"%s\") failed (%d)"
82 " -- using isprint() instead of iconv().\n",
83 defset, defset, errno);
84 } else {
85 rprintf(FINFO,
86 "note: iconv_open(\"%s\", \"%s\") succeeded.\n",
87 defset, defset);
88 }
89 }
90 }
91}
92#endif
93
94/* This is used by sender.c with a valid f_out, and by receive.c with
95 * f_out = -1. */
96int read_item_attrs(int f_in, int f_out, int ndx, uchar *type_ptr,
97 char *buf, int *len_ptr)
98{
99 int len;
100 uchar fnamecmp_type = FNAMECMP_FNAME;
101 int iflags = protocol_version >= 29 ? read_shortint(f_in)
102 : ITEM_TRANSFER | ITEM_MISSING_DATA;
103
104 /* Handle the new keep-alive (no-op) packet. */
105 if (ndx == the_file_list->count && iflags == ITEM_IS_NEW)
106 ;
107 else if (ndx < 0 || ndx >= the_file_list->count) {
108 rprintf(FERROR, "Invalid file index: %d (count=%d) [%s]\n",
109 ndx, the_file_list->count, who_am_i());
110 exit_cleanup(RERR_PROTOCOL);
111 } else if (iflags == ITEM_IS_NEW) {
112 rprintf(FERROR, "Invalid itemized flag word: %x [%s]\n",
113 iflags, who_am_i());
114 exit_cleanup(RERR_PROTOCOL);
115 }
116
117 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
118 fnamecmp_type = read_byte(f_in);
119 *type_ptr = fnamecmp_type;
120
121 if (iflags & ITEM_XNAME_FOLLOWS) {
122 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
123 exit_cleanup(RERR_PROTOCOL);
124 } else {
125 *buf = '\0';
126 len = -1;
127 }
128 *len_ptr = len;
129
130 if (iflags & ITEM_TRANSFER) {
131 if (!S_ISREG(the_file_list->files[ndx]->mode)) {
132 rprintf(FERROR,
133 "received request to transfer non-regular file: %d [%s]\n",
134 ndx, who_am_i());
135 exit_cleanup(RERR_PROTOCOL);
136 }
137 } else if (f_out >= 0) {
138 write_ndx_and_attrs(f_out, ndx, iflags,
139 fnamecmp_type, buf, len);
140 }
141
142 return iflags;
143}
144
145/*
146 free a sums struct
147 */
148void free_sums(struct sum_struct *s)
149{
150 if (s->sums) free(s->sums);
151 free(s);
152}
153
154/* This is only called when we aren't preserving permissions. Figure out what
155 * the permissions should be and return them merged back into the mode. */
156mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists)
157{
158 int new_mode;
159 /* If the file already exists, we'll return the local permissions,
160 * possibly tweaked by the --executability option. */
161 if (exists) {
162 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
163 if (preserve_executability && S_ISREG(flist_mode)) {
164 /* If the source file is executable, grant execute
165 * rights to everyone who can read, but ONLY if the
166 * file isn't already executable. */
167 if (!(flist_mode & 0111))
168 new_mode &= ~0111;
169 else if (!(stat_mode & 0111))
170 new_mode |= (new_mode & 0444) >> 2;
171 }
172 } else {
173 /* Apply the umask and turn off special permissions. */
174 new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask));
175 }
176 return new_mode;
177}
178
179int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
180 int flags)
181{
182 int updated = 0;
183 STRUCT_STAT st2;
184 int change_uid, change_gid;
185 mode_t new_mode = file->mode;
186
187 if (!st) {
188 if (dry_run)
189 return 1;
190 if (link_stat(fname, &st2, 0) < 0) {
191 rsyserr(FERROR, errno, "stat %s failed",
192 full_fname(fname));
193 return 0;
194 }
195 st = &st2;
196 if (!preserve_perms && S_ISDIR(new_mode)
197 && st->st_mode & S_ISGID) {
198 /* We just created this directory and its setgid
199 * bit is on, so make sure it stays on. */
200 new_mode |= S_ISGID;
201 }
202 }
203
204 if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
205 flags |= ATTRS_SKIP_MTIME;
206 if (!(flags & ATTRS_SKIP_MTIME)
207 && cmp_time(st->st_mtime, file->modtime) != 0) {
208 int ret = set_modtime(fname, file->modtime, st->st_mode);
209 if (ret < 0) {
210 rsyserr(FERROR, errno, "failed to set times on %s",
211 full_fname(fname));
212 return 0;
213 }
214 if (ret == 0) /* ret == 1 if symlink could not be set */
215 updated = 1;
216 }
217
218 change_uid = am_root && preserve_uid && st->st_uid != F_UID(file);
219 change_gid = preserve_gid && F_GID(file) != GID_NONE
220 && st->st_gid != F_GID(file);
221#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
222 if (S_ISLNK(st->st_mode))
223 ;
224 else
225#endif
226 if (change_uid || change_gid) {
227 if (verbose > 2) {
228 if (change_uid) {
229 rprintf(FINFO,
230 "set uid of %s from %ld to %ld\n",
231 fname,
232 (long)st->st_uid, (long)F_UID(file));
233 }
234 if (change_gid) {
235 rprintf(FINFO,
236 "set gid of %s from %ld to %ld\n",
237 fname,
238 (long)st->st_gid, (long)F_GID(file));
239 }
240 }
241 if (do_lchown(fname,
242 change_uid ? F_UID(file) : st->st_uid,
243 change_gid ? F_GID(file) : st->st_gid) != 0) {
244 /* shouldn't have attempted to change uid or gid
245 * unless have the privilege */
246 rsyserr(FERROR, errno, "%s %s failed",
247 change_uid ? "chown" : "chgrp",
248 full_fname(fname));
249 return 0;
250 }
251 /* a lchown had been done - we have to re-stat if the
252 * destination had the setuid or setgid bits set due
253 * to the side effect of the chown call */
254 if (st->st_mode & (S_ISUID | S_ISGID)) {
255 link_stat(fname, st,
256 keep_dirlinks && S_ISDIR(st->st_mode));
257 }
258 updated = 1;
259 }
260
261 if (daemon_chmod_modes && !S_ISLNK(new_mode))
262 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
263#ifdef HAVE_CHMOD
264 if (!BITS_EQUAL(st->st_mode, new_mode, CHMOD_BITS)) {
265 int ret = do_chmod(fname, new_mode);
266 if (ret < 0) {
267 rsyserr(FERROR, errno,
268 "failed to set permissions on %s",
269 full_fname(fname));
270 return 0;
271 }
272 if (ret == 0) /* ret == 1 if symlink could not be set */
273 updated = 1;
274 }
275#endif
276
277 if (verbose > 1 && flags & ATTRS_REPORT) {
278 if (updated)
279 rprintf(FCLIENT, "%s\n", fname);
280 else
281 rprintf(FCLIENT, "%s is uptodate\n", fname);
282 }
283 return updated;
284}
285
286RETSIGTYPE sig_int(UNUSED(int val))
287{
288 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
289 * for a password, then our cleanup's sending of a SIGUSR1
290 * signal to all our children may kill ssh before it has a
291 * chance to restore the tty settings (i.e. turn echo back
292 * on). By sleeping for a short time, ssh gets a bigger
293 * chance to do the right thing. If child processes are
294 * not ssh waiting for a password, then this tiny delay
295 * shouldn't hurt anything. */
296 msleep(400);
297 exit_cleanup(RERR_SIGNAL);
298}
299
300/* Finish off a file transfer: renaming the file and setting the file's
301 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
302 * NULL and the robust_rename() call is forced to copy the temp file, we
303 * stage the file into the partial-dir and then rename it into place. */
304void finish_transfer(char *fname, char *fnametmp, char *partialptr,
305 struct file_struct *file, int ok_to_set_time,
306 int overwriting_basis)
307{
308 int ret;
309
310 if (inplace) {
311 if (verbose > 2)
312 rprintf(FINFO, "finishing %s\n", fname);
313 fnametmp = fname;
314 goto do_set_file_attrs;
315 }
316
317 if (make_backups && overwriting_basis && !make_backup(fname))
318 return;
319
320 /* Change permissions before putting the file into place. */
321 set_file_attrs(fnametmp, file, NULL,
322 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
323
324 /* move tmp file over real file */
325 if (verbose > 2)
326 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
327 ret = robust_rename(fnametmp, fname, partialptr,
328 file->mode & INITACCESSPERMS);
329 if (ret < 0) {
330 rsyserr(FERROR, errno, "%s %s -> \"%s\"",
331 ret == -2 ? "copy" : "rename",
332 full_fname(fnametmp), fname);
333 do_unlink(fnametmp);
334 return;
335 }
336 if (ret == 0) {
337 /* The file was moved into place (not copied), so it's done. */
338 return;
339 }
340 /* The file was copied, so tweak the perms of the copied file. If it
341 * was copied to partialptr, move it into its final destination. */
342 fnametmp = partialptr ? partialptr : fname;
343
344 do_set_file_attrs:
345 set_file_attrs(fnametmp, file, NULL,
346 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
347
348 if (partialptr) {
349 if (do_rename(fnametmp, fname) < 0) {
350 rsyserr(FERROR, errno, "rename %s -> \"%s\"",
351 full_fname(fnametmp), fname);
352 } else
353 handle_partial_dir(partialptr, PDIR_DELETE);
354 }
355}
356
357const char *who_am_i(void)
358{
359 if (am_starting_up)
360 return am_server ? "server" : "client";
361 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
362}