Added BITS_EQUAL() define.
[rsync/rsync.git] / rsync.c
CommitLineData
7a2fd68b 1/*
0f78b815
WD
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 *
e7c67065
WD
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.
0f78b815 21 */
c627d613 22
2f03f956 23#include "rsync.h"
c8563142 24#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
ceccbacc
WD
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
43a481dc 32
c627d613 33extern int verbose;
c627d613 34extern int dry_run;
9b499e95 35extern int preserve_perms;
81284832 36extern int preserve_executability;
2f03f956 37extern int preserve_times;
82471e68 38extern int omit_dir_times;
7b8356d0 39extern int am_root;
794b0a03 40extern int am_server;
c3e5e585
WD
41extern int am_sender;
42extern int am_generator;
ed9d969c 43extern int am_starting_up;
e0f4a661 44extern int allow_8bit_chars;
b675ba6f 45extern int protocol_version;
2f03f956
AT
46extern int preserve_uid;
47extern int preserve_gid;
c786a7ae 48extern int inplace;
81b07870 49extern int keep_dirlinks;
2f03f956 50extern int make_backups;
c5b7aa15 51extern mode_t orig_umask;
c786a7ae 52extern struct stats stats;
b675ba6f 53extern struct file_list *the_file_list;
94d3725c 54extern struct chmod_mode_struct *daemon_chmod_modes;
fe055c71 55
595251de 56#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
ceccbacc
WD
57iconv_t ic_chck = (iconv_t)-1;
58
2a62f5ee 59static const char *default_charset(void)
ceccbacc
WD
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
7fc87d2d 64 return nl_langinfo(CODESET);
ceccbacc
WD
65#else
66 return ""; /* Works with (at the very least) gnu iconv... */
67#endif
68}
69
70void setup_iconv()
71{
e0f4a661
WD
72 if (!am_server && !allow_8bit_chars) {
73 const char *defset = default_charset();
2d2f71fb 74
e0f4a661
WD
75 /* It's OK if this fails... */
76 ic_chck = iconv_open(defset, defset);
ceccbacc 77
e0f4a661
WD
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 }
7fc87d2d 89 }
ceccbacc
WD
90 }
91}
92#endif
fe055c71 93
d1c178dd
WD
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
c627d613
AT
145/*
146 free a sums struct
147 */
2f03f956 148void free_sums(struct sum_struct *s)
c627d613 149{
298c10d5
AT
150 if (s->sums) free(s->sums);
151 free(s);
c627d613
AT
152}
153
81284832
WD
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. */
67f8a41b 156mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists)
81284832 157{
67f8a41b 158 int new_mode;
9b499e95 159 /* If the file already exists, we'll return the local permissions,
81284832
WD
160 * possibly tweaked by the --executability option. */
161 if (exists) {
67f8a41b 162 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
81284832
WD
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))
67f8a41b
WD
168 new_mode &= ~0111;
169 else if (!(stat_mode & 0111))
170 new_mode |= (new_mode & 0444) >> 2;
81284832 171 }
67f8a41b
WD
172 } else {
173 /* Apply the umask and turn off special permissions. */
174 new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask));
175 }
67f8a41b 176 return new_mode;
81284832
WD
177}
178
36119f6e
WD
179int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
180 int flags)
c627d613 181{
667e72a1
AT
182 int updated = 0;
183 STRUCT_STAT st2;
460f6b99 184 int change_uid, change_gid;
519d55a9 185 mode_t new_mode = file->mode;
c627d613 186
667e72a1 187 if (!st) {
f227ffe4
WD
188 if (dry_run)
189 return 1;
373ef160 190 if (link_stat(fname, &st2, 0) < 0) {
d62bcc17
WD
191 rsyserr(FERROR, errno, "stat %s failed",
192 full_fname(fname));
667e72a1
AT
193 return 0;
194 }
195 st = &st2;
519d55a9 196 if (!preserve_perms && S_ISDIR(new_mode)
9b499e95
WD
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. */
519d55a9 200 new_mode |= S_ISGID;
9b499e95 201 }
667e72a1 202 }
c627d613 203
c8d34657 204 if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
36119f6e
WD
205 flags |= ATTRS_SKIP_MTIME;
206 if (!(flags & ATTRS_SKIP_MTIME)
0f86c74e 207 && cmp_time(st->st_mtime, file->modtime) != 0) {
c8d34657
WD
208 int ret = set_modtime(fname, file->modtime, st->st_mode);
209 if (ret < 0) {
d62bcc17
WD
210 rsyserr(FERROR, errno, "failed to set times on %s",
211 full_fname(fname));
667e72a1
AT
212 return 0;
213 }
c8d34657
WD
214 if (ret == 0) /* ret == 1 if symlink could not be set */
215 updated = 1;
667e72a1
AT
216 }
217
82ad07c4
WD
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);
4f5b0756 221#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
a41a1e87
WD
222 if (S_ISLNK(st->st_mode))
223 ;
224 else
225#endif
460f6b99 226 if (change_uid || change_gid) {
62c9e6b3 227 if (verbose > 2) {
ce37eb2d
WD
228 if (change_uid) {
229 rprintf(FINFO,
4875d6b6 230 "set uid of %s from %ld to %ld\n",
45c49b52 231 fname,
82ad07c4 232 (long)st->st_uid, (long)F_UID(file));
ce37eb2d
WD
233 }
234 if (change_gid) {
235 rprintf(FINFO,
4875d6b6 236 "set gid of %s from %ld to %ld\n",
45c49b52 237 fname,
82ad07c4 238 (long)st->st_gid, (long)F_GID(file));
ce37eb2d
WD
239 }
240 }
667e72a1 241 if (do_lchown(fname,
82ad07c4
WD
242 change_uid ? F_UID(file) : st->st_uid,
243 change_gid ? F_GID(file) : st->st_gid) != 0) {
460f6b99 244 /* shouldn't have attempted to change uid or gid
a174e1ed 245 * unless have the privilege */
d62bcc17 246 rsyserr(FERROR, errno, "%s %s failed",
a174e1ed 247 change_uid ? "chown" : "chgrp",
d62bcc17 248 full_fname(fname));
460f6b99 249 return 0;
667e72a1 250 }
a5827a28 251 /* a lchown had been done - we have to re-stat if the
dd096ae0
WD
252 * destination had the setuid or setgid bits set due
253 * to the side effect of the chown call */
a5827a28 254 if (st->st_mode & (S_ISUID | S_ISGID)) {
81b07870
WD
255 link_stat(fname, st,
256 keep_dirlinks && S_ISDIR(st->st_mode));
a5827a28 257 }
460f6b99 258 updated = 1;
667e72a1 259 }
c627d613 260
519d55a9
WD
261 if (daemon_chmod_modes && !S_ISLNK(new_mode))
262 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
4f5b0756 263#ifdef HAVE_CHMOD
519d55a9
WD
264 if ((st->st_mode & CHMOD_BITS) != (new_mode & CHMOD_BITS)) {
265 int ret = do_chmod(fname, new_mode);
c8d34657
WD
266 if (ret < 0) {
267 rsyserr(FERROR, errno,
268 "failed to set permissions on %s",
269 full_fname(fname));
270 return 0;
667e72a1 271 }
c8d34657
WD
272 if (ret == 0) /* ret == 1 if symlink could not be set */
273 updated = 1;
667e72a1 274 }
c627d613 275#endif
6a7cc46c 276
36119f6e 277 if (verbose > 1 && flags & ATTRS_REPORT) {
667e72a1 278 if (updated)
1925c344 279 rprintf(FCLIENT, "%s\n", fname);
667e72a1 280 else
1925c344 281 rprintf(FCLIENT, "%s is uptodate\n", fname);
667e72a1
AT
282 }
283 return updated;
c627d613
AT
284}
285
b8e9c234 286RETSIGTYPE sig_int(UNUSED(int val))
34ccb63e 287{
d5a0b483
WD
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);
65417579 297 exit_cleanup(RERR_SIGNAL);
c627d613
AT
298}
299
d8b1c923
WD
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)
c95da96a 307{
62c9e6b3
WD
308 int ret;
309
a3221d2a
WD
310 if (inplace) {
311 if (verbose > 2)
45c49b52 312 rprintf(FINFO, "finishing %s\n", fname);
d8b1c923 313 fnametmp = fname;
36119f6e 314 goto do_set_file_attrs;
a3221d2a
WD
315 }
316
997d9ea6 317 if (make_backups && overwriting_basis && !make_backup(fname))
e484f0cc
WD
318 return;
319
ebeacb36 320 /* Change permissions before putting the file into place. */
36119f6e
WD
321 set_file_attrs(fnametmp, file, NULL,
322 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
ebeacb36 323
c95da96a 324 /* move tmp file over real file */
45c49b52
WD
325 if (verbose > 2)
326 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
d8b1c923
WD
327 ret = robust_rename(fnametmp, fname, partialptr,
328 file->mode & INITACCESSPERMS);
3d061653 329 if (ret < 0) {
d62bcc17 330 rsyserr(FERROR, errno, "%s %s -> \"%s\"",
45c49b52
WD
331 ret == -2 ? "copy" : "rename",
332 full_fname(fnametmp), fname);
c7c11a0d 333 do_unlink(fnametmp);
077e59b7 334 return;
c95da96a 335 }
ebeacb36
WD
336 if (ret == 0) {
337 /* The file was moved into place (not copied), so it's done. */
338 return;
339 }
d8b1c923
WD
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
36119f6e 344 do_set_file_attrs:
d8b1c923 345 set_file_attrs(fnametmp, file, NULL,
36119f6e 346 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
d8b1c923
WD
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 }
c95da96a 355}
c3e5e585
WD
356
357const char *who_am_i(void)
358{
ed9d969c 359 if (am_starting_up)
794b0a03 360 return am_server ? "server" : "client";
ebeacb36 361 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
c3e5e585 362}