- Call SIGACTION() instead of signal().
[rsync/rsync.git] / rsync.c
CommitLineData
7a2fd68b 1/*
c627d613
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
7a2fd68b 4
c627d613
AT
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
7a2fd68b 9
c627d613
AT
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
7a2fd68b 14
c627d613
AT
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
2f03f956
AT
20/* this file contains code used by more than one part of the rsync
21 process */
c627d613 22
2f03f956 23#include "rsync.h"
43a481dc 24
c627d613 25extern int verbose;
c627d613 26extern int dry_run;
8324bd5c 27extern int daemon_log_format_has_i;
81284832 28extern int preserve_executability;
2f03f956 29extern int preserve_times;
82471e68 30extern int omit_dir_times;
81284832 31extern int orig_umask;
7b8356d0 32extern int am_root;
794b0a03 33extern int am_server;
c3e5e585
WD
34extern int am_sender;
35extern int am_generator;
ed9d969c 36extern int am_starting_up;
2f03f956
AT
37extern int preserve_uid;
38extern int preserve_gid;
c786a7ae 39extern int inplace;
81b07870 40extern int keep_dirlinks;
2f03f956 41extern int make_backups;
c786a7ae 42extern struct stats stats;
fe055c71
AT
43
44
c627d613
AT
45/*
46 free a sums struct
47 */
2f03f956 48void free_sums(struct sum_struct *s)
c627d613 49{
298c10d5
AT
50 if (s->sums) free(s->sums);
51 free(s);
c627d613
AT
52}
53
81284832
WD
54/* This is only called when we aren't preserving permissions. Figure out what
55 * the permissions should be and return them merged back into the mode. */
56mode_t dest_mode(mode_t flist_mode, mode_t dest_mode, int exists)
57{
58 /* If the file already exists we'll return the local permissions,
59 * possibly tweaked by the --executability option. */
60 if (exists) {
61 if (preserve_executability && S_ISREG(flist_mode)) {
62 /* If the source file is executable, grant execute
63 * rights to everyone who can read, but ONLY if the
64 * file isn't already executable. */
65 if (!(flist_mode & 0111))
66 dest_mode &= ~0111;
67 else if (!(dest_mode & 0111))
68 dest_mode |= (dest_mode & 0444) >> 2;
69 }
70 } else
71 dest_mode = flist_mode & ~orig_umask;
72 return (flist_mode & ~CHMOD_BITS) | (dest_mode & CHMOD_BITS);
73}
74
36119f6e
WD
75int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
76 int flags)
c627d613 77{
667e72a1
AT
78 int updated = 0;
79 STRUCT_STAT st2;
460f6b99 80 int change_uid, change_gid;
c627d613 81
667e72a1 82 if (!st) {
f227ffe4
WD
83 if (dry_run)
84 return 1;
373ef160 85 if (link_stat(fname, &st2, 0) < 0) {
d62bcc17
WD
86 rsyserr(FERROR, errno, "stat %s failed",
87 full_fname(fname));
667e72a1
AT
88 return 0;
89 }
90 st = &st2;
91 }
c627d613 92
c8d34657 93 if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
36119f6e
WD
94 flags |= ATTRS_SKIP_MTIME;
95 if (!(flags & ATTRS_SKIP_MTIME)
4ecc9e6b 96 && cmp_modtime(st->st_mtime, file->modtime) != 0) {
c8d34657
WD
97 int ret = set_modtime(fname, file->modtime, st->st_mode);
98 if (ret < 0) {
d62bcc17
WD
99 rsyserr(FERROR, errno, "failed to set times on %s",
100 full_fname(fname));
667e72a1
AT
101 return 0;
102 }
c8d34657
WD
103 if (ret == 0) /* ret == 1 if symlink could not be set */
104 updated = 1;
667e72a1
AT
105 }
106
7b6fa00f
WD
107 change_uid = am_root && preserve_uid && st->st_uid != file->uid;
108 change_gid = preserve_gid && file->gid != GID_NONE
109 && st->st_gid != file->gid;
4f5b0756 110#if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
a41a1e87
WD
111 if (S_ISLNK(st->st_mode))
112 ;
113 else
114#endif
460f6b99 115 if (change_uid || change_gid) {
62c9e6b3 116 if (verbose > 2) {
ce37eb2d
WD
117 if (change_uid) {
118 rprintf(FINFO,
4875d6b6 119 "set uid of %s from %ld to %ld\n",
45c49b52 120 fname,
7b6fa00f 121 (long)st->st_uid, (long)file->uid);
ce37eb2d
WD
122 }
123 if (change_gid) {
124 rprintf(FINFO,
4875d6b6 125 "set gid of %s from %ld to %ld\n",
45c49b52 126 fname,
7b6fa00f 127 (long)st->st_gid, (long)file->gid);
ce37eb2d
WD
128 }
129 }
667e72a1 130 if (do_lchown(fname,
7b6fa00f
WD
131 change_uid ? file->uid : st->st_uid,
132 change_gid ? file->gid : st->st_gid) != 0) {
460f6b99 133 /* shouldn't have attempted to change uid or gid
a174e1ed 134 * unless have the privilege */
d62bcc17 135 rsyserr(FERROR, errno, "%s %s failed",
a174e1ed 136 change_uid ? "chown" : "chgrp",
d62bcc17 137 full_fname(fname));
460f6b99 138 return 0;
667e72a1 139 }
a5827a28 140 /* a lchown had been done - we have to re-stat if the
dd096ae0
WD
141 * destination had the setuid or setgid bits set due
142 * to the side effect of the chown call */
a5827a28 143 if (st->st_mode & (S_ISUID | S_ISGID)) {
81b07870
WD
144 link_stat(fname, st,
145 keep_dirlinks && S_ISDIR(st->st_mode));
a5827a28 146 }
460f6b99 147 updated = 1;
667e72a1 148 }
c627d613 149
4f5b0756 150#ifdef HAVE_CHMOD
c8d34657
WD
151 if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
152 int ret = do_chmod(fname, file->mode);
153 if (ret < 0) {
154 rsyserr(FERROR, errno,
155 "failed to set permissions on %s",
156 full_fname(fname));
157 return 0;
667e72a1 158 }
c8d34657
WD
159 if (ret == 0) /* ret == 1 if symlink could not be set */
160 updated = 1;
667e72a1 161 }
c627d613 162#endif
6a7cc46c 163
36119f6e 164 if (verbose > 1 && flags & ATTRS_REPORT) {
8324bd5c
WD
165 enum logcode code = daemon_log_format_has_i || dry_run
166 ? FCLIENT : FINFO;
667e72a1 167 if (updated)
45c49b52 168 rprintf(code, "%s\n", fname);
667e72a1 169 else
45c49b52 170 rprintf(code, "%s is uptodate\n", fname);
667e72a1
AT
171 }
172 return updated;
c627d613
AT
173}
174
34ccb63e
AT
175void sig_int(void)
176{
d5a0b483
WD
177 /* KLUGE: if the user hits Ctrl-C while ssh is prompting
178 * for a password, then our cleanup's sending of a SIGUSR1
179 * signal to all our children may kill ssh before it has a
180 * chance to restore the tty settings (i.e. turn echo back
181 * on). By sleeping for a short time, ssh gets a bigger
182 * chance to do the right thing. If child processes are
183 * not ssh waiting for a password, then this tiny delay
184 * shouldn't hurt anything. */
185 msleep(400);
65417579 186 exit_cleanup(RERR_SIGNAL);
c627d613
AT
187}
188
d8b1c923
WD
189/* Finish off a file transfer: renaming the file and setting the file's
190 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
191 * NULL and the robust_rename() call is forced to copy the temp file, we
192 * stage the file into the partial-dir and then rename it into place. */
193void finish_transfer(char *fname, char *fnametmp, char *partialptr,
194 struct file_struct *file, int ok_to_set_time,
195 int overwriting_basis)
c95da96a 196{
62c9e6b3
WD
197 int ret;
198
a3221d2a
WD
199 if (inplace) {
200 if (verbose > 2)
45c49b52 201 rprintf(FINFO, "finishing %s\n", fname);
d8b1c923 202 fnametmp = fname;
36119f6e 203 goto do_set_file_attrs;
a3221d2a
WD
204 }
205
997d9ea6 206 if (make_backups && overwriting_basis && !make_backup(fname))
e484f0cc
WD
207 return;
208
ebeacb36 209 /* Change permissions before putting the file into place. */
36119f6e
WD
210 set_file_attrs(fnametmp, file, NULL,
211 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
ebeacb36 212
c95da96a 213 /* move tmp file over real file */
45c49b52
WD
214 if (verbose > 2)
215 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
d8b1c923
WD
216 ret = robust_rename(fnametmp, fname, partialptr,
217 file->mode & INITACCESSPERMS);
3d061653 218 if (ret < 0) {
d62bcc17 219 rsyserr(FERROR, errno, "%s %s -> \"%s\"",
45c49b52
WD
220 ret == -2 ? "copy" : "rename",
221 full_fname(fnametmp), fname);
c7c11a0d 222 do_unlink(fnametmp);
077e59b7 223 return;
c95da96a 224 }
ebeacb36
WD
225 if (ret == 0) {
226 /* The file was moved into place (not copied), so it's done. */
227 return;
228 }
d8b1c923
WD
229 /* The file was copied, so tweak the perms of the copied file. If it
230 * was copied to partialptr, move it into its final destination. */
231 fnametmp = partialptr ? partialptr : fname;
232
36119f6e 233 do_set_file_attrs:
d8b1c923 234 set_file_attrs(fnametmp, file, NULL,
36119f6e 235 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
d8b1c923
WD
236
237 if (partialptr) {
238 if (do_rename(fnametmp, fname) < 0) {
239 rsyserr(FERROR, errno, "rename %s -> \"%s\"",
240 full_fname(fnametmp), fname);
241 } else
242 handle_partial_dir(partialptr, PDIR_DELETE);
243 }
c95da96a 244}
c3e5e585
WD
245
246const char *who_am_i(void)
247{
ed9d969c 248 if (am_starting_up)
794b0a03 249 return am_server ? "server" : "client";
ebeacb36 250 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
c3e5e585 251}