Commit | Line | Data |
---|---|---|
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 | 25 | extern int verbose; |
c627d613 | 26 | extern int dry_run; |
8324bd5c | 27 | extern int daemon_log_format_has_i; |
2f03f956 | 28 | extern int preserve_times; |
82471e68 | 29 | extern int omit_dir_times; |
7b8356d0 | 30 | extern int am_root; |
794b0a03 | 31 | extern int am_server; |
c3e5e585 WD |
32 | extern int am_sender; |
33 | extern int am_generator; | |
2f03f956 AT |
34 | extern int preserve_uid; |
35 | extern int preserve_gid; | |
ee1df1cc | 36 | extern int force_delete; |
c786a7ae | 37 | extern int inplace; |
ee1df1cc | 38 | extern int recurse; |
40cc2851 | 39 | extern int max_delete; |
81b07870 | 40 | extern int keep_dirlinks; |
2f03f956 | 41 | extern int make_backups; |
c786a7ae | 42 | extern struct stats stats; |
ee1df1cc | 43 | extern char *backup_dir; |
c786a7ae | 44 | extern char *log_format; |
fe055c71 AT |
45 | |
46 | ||
c627d613 AT |
47 | /* |
48 | free a sums struct | |
49 | */ | |
2f03f956 | 50 | void free_sums(struct sum_struct *s) |
c627d613 | 51 | { |
298c10d5 AT |
52 | if (s->sums) free(s->sums); |
53 | free(s); | |
c627d613 AT |
54 | } |
55 | ||
56 | ||
40cc2851 WD |
57 | int deletion_count = 0; /* used to implement --max-delete */ |
58 | ||
3cb6f5d6 | 59 | /* |
7a2fd68b WD |
60 | * delete a file or directory. If force_delete is set then delete |
61 | * recursively | |
3cb6f5d6 | 62 | */ |
c786a7ae | 63 | int delete_file(char *fname, int mode, int flags) |
3cb6f5d6 AT |
64 | { |
65 | DIR *d; | |
66 | struct dirent *di; | |
67 | char buf[MAXPATHLEN]; | |
bcacc18b | 68 | STRUCT_STAT st; |
11781089 | 69 | int zap_dir; |
3cb6f5d6 | 70 | |
40cc2851 WD |
71 | if (max_delete && deletion_count >= max_delete) |
72 | return -1; | |
73 | ||
c786a7ae | 74 | if (!S_ISDIR(mode)) { |
dd096ae0 | 75 | if (robust_unlink(fname) == 0) { |
c786a7ae WD |
76 | if ((verbose || log_format) && !(flags & DEL_TERSE)) |
77 | log_delete(fname, mode); | |
40cc2851 | 78 | deletion_count++; |
dd096ae0 WD |
79 | return 0; |
80 | } | |
81 | if (errno == ENOENT) | |
a7ed6ca6 | 82 | return 0; |
d62bcc17 WD |
83 | rsyserr(FERROR, errno, "delete_file: unlink %s failed", |
84 | full_fname(fname)); | |
3cb6f5d6 AT |
85 | return -1; |
86 | } | |
87 | ||
4366d2c4 WD |
88 | zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse)) |
89 | && !(flags & DEL_NO_RECURSE); | |
11781089 WD |
90 | if (dry_run && zap_dir) |
91 | errno = ENOTEMPTY; | |
92 | else if (do_rmdir(fname) == 0) { | |
c786a7ae WD |
93 | if ((verbose || log_format) && !(flags & DEL_TERSE)) |
94 | log_delete(fname, mode); | |
40cc2851 | 95 | deletion_count++; |
a7ed6ca6 | 96 | return 0; |
dd096ae0 WD |
97 | } |
98 | if (errno == ENOENT) | |
99 | return 0; | |
11781089 | 100 | if (!zap_dir || (errno != ENOTEMPTY && errno != EEXIST)) { |
d62bcc17 WD |
101 | rsyserr(FERROR, errno, "delete_file: rmdir %s failed", |
102 | full_fname(fname)); | |
3cb6f5d6 AT |
103 | return -1; |
104 | } | |
105 | ||
106 | /* now we do a recsursive delete on the directory ... */ | |
a7ed6ca6 | 107 | if (!(d = opendir(fname))) { |
d62bcc17 WD |
108 | rsyserr(FERROR, errno, "delete_file: opendir %s failed", |
109 | full_fname(fname)); | |
3cb6f5d6 AT |
110 | return -1; |
111 | } | |
112 | ||
dd096ae0 | 113 | if (!(flags & DEL_TERSE)) { |
c786a7ae WD |
114 | if (verbose || log_format) |
115 | log_delete(fname, mode); | |
dd096ae0 WD |
116 | flags |= DEL_TERSE; |
117 | } | |
118 | ||
6a7cc46c | 119 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 120 | char *dname = d_name(di); |
a7ed6ca6 WD |
121 | if (dname[0] == '.' && (dname[1] == '\0' |
122 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3cb6f5d6 | 123 | continue; |
a7725e6d | 124 | pathjoin(buf, sizeof buf, fname, dname); |
dd096ae0 WD |
125 | |
126 | if (do_lstat(buf, &st) < 0) | |
127 | continue; | |
c786a7ae WD |
128 | if (verbose || log_format) |
129 | log_delete(buf, st.st_mode); | |
130 | if (delete_file(buf, st.st_mode, flags) != 0) { | |
3cb6f5d6 AT |
131 | closedir(d); |
132 | return -1; | |
133 | } | |
7a2fd68b | 134 | } |
6a7cc46c | 135 | if (errno) { |
d62bcc17 WD |
136 | rsyserr(FERROR, errno, "delete_file: readdir %s failed", |
137 | full_fname(fname)); | |
6a7cc46c S |
138 | closedir(d); |
139 | return -1; | |
140 | } | |
3cb6f5d6 AT |
141 | |
142 | closedir(d); | |
7a2fd68b | 143 | |
40cc2851 WD |
144 | if (max_delete && deletion_count >= max_delete) |
145 | return -1; | |
dd096ae0 | 146 | if (do_rmdir(fname) != 0 && errno != ENOENT) { |
d62bcc17 WD |
147 | rsyserr(FERROR, errno, "delete_file: rmdir %s failed", |
148 | full_fname(fname)); | |
3cb6f5d6 AT |
149 | return -1; |
150 | } | |
40cc2851 | 151 | deletion_count++; |
3cb6f5d6 AT |
152 | |
153 | return 0; | |
154 | } | |
c627d613 | 155 | |
2f03f956 | 156 | int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st, |
4ecc9e6b | 157 | int flags) |
c627d613 | 158 | { |
667e72a1 AT |
159 | int updated = 0; |
160 | STRUCT_STAT st2; | |
460f6b99 | 161 | int change_uid, change_gid; |
c627d613 | 162 | |
667e72a1 | 163 | if (!st) { |
f227ffe4 WD |
164 | if (dry_run) |
165 | return 1; | |
373ef160 | 166 | if (link_stat(fname, &st2, 0) < 0) { |
d62bcc17 WD |
167 | rsyserr(FERROR, errno, "stat %s failed", |
168 | full_fname(fname)); | |
667e72a1 AT |
169 | return 0; |
170 | } | |
171 | st = &st2; | |
172 | } | |
c627d613 | 173 | |
ee1df1cc | 174 | if (!preserve_times || S_ISLNK(st->st_mode) |
82471e68 WD |
175 | || (S_ISDIR(st->st_mode) |
176 | && (omit_dir_times || (make_backups && !backup_dir)))) | |
38443188 WD |
177 | flags |= PERMS_SKIP_MTIME; |
178 | if (!(flags & PERMS_SKIP_MTIME) | |
4ecc9e6b | 179 | && cmp_modtime(st->st_mtime, file->modtime) != 0) { |
82471e68 | 180 | if (set_modtime(fname,file->modtime) != 0) { |
d62bcc17 WD |
181 | rsyserr(FERROR, errno, "failed to set times on %s", |
182 | full_fname(fname)); | |
667e72a1 AT |
183 | return 0; |
184 | } | |
a174e1ed | 185 | updated = 1; |
667e72a1 AT |
186 | } |
187 | ||
460f6b99 | 188 | change_uid = am_root && preserve_uid && st->st_uid != file->uid; |
a60e2dca | 189 | change_gid = preserve_gid && file->gid != GID_NONE |
7352b873 | 190 | && st->st_gid != file->gid; |
4f5b0756 | 191 | #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK |
a41a1e87 WD |
192 | if (S_ISLNK(st->st_mode)) |
193 | ; | |
194 | else | |
195 | #endif | |
460f6b99 | 196 | if (change_uid || change_gid) { |
62c9e6b3 | 197 | if (verbose > 2) { |
ce37eb2d WD |
198 | if (change_uid) { |
199 | rprintf(FINFO, | |
4875d6b6 WD |
200 | "set uid of %s from %ld to %ld\n", |
201 | safe_fname(fname), | |
202 | (long)st->st_uid, (long)file->uid); | |
ce37eb2d WD |
203 | } |
204 | if (change_gid) { | |
205 | rprintf(FINFO, | |
4875d6b6 WD |
206 | "set gid of %s from %ld to %ld\n", |
207 | safe_fname(fname), | |
208 | (long)st->st_gid, (long)file->gid); | |
ce37eb2d WD |
209 | } |
210 | } | |
667e72a1 | 211 | if (do_lchown(fname, |
a174e1ed WD |
212 | change_uid ? file->uid : st->st_uid, |
213 | change_gid ? file->gid : st->st_gid) != 0) { | |
460f6b99 | 214 | /* shouldn't have attempted to change uid or gid |
a174e1ed | 215 | * unless have the privilege */ |
d62bcc17 | 216 | rsyserr(FERROR, errno, "%s %s failed", |
a174e1ed | 217 | change_uid ? "chown" : "chgrp", |
d62bcc17 | 218 | full_fname(fname)); |
460f6b99 | 219 | return 0; |
667e72a1 | 220 | } |
a5827a28 | 221 | /* a lchown had been done - we have to re-stat if the |
dd096ae0 WD |
222 | * destination had the setuid or setgid bits set due |
223 | * to the side effect of the chown call */ | |
a5827a28 | 224 | if (st->st_mode & (S_ISUID | S_ISGID)) { |
81b07870 WD |
225 | link_stat(fname, st, |
226 | keep_dirlinks && S_ISDIR(st->st_mode)); | |
a5827a28 | 227 | } |
460f6b99 | 228 | updated = 1; |
667e72a1 | 229 | } |
c627d613 | 230 | |
4f5b0756 | 231 | #ifdef HAVE_CHMOD |
972a3619 | 232 | if (!S_ISLNK(st->st_mode)) { |
b0d791bb | 233 | if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) { |
972a3619 | 234 | updated = 1; |
b0d791bb | 235 | if (do_chmod(fname,(file->mode & CHMOD_BITS)) != 0) { |
d62bcc17 WD |
236 | rsyserr(FERROR, errno, "failed to set permissions on %s", |
237 | full_fname(fname)); | |
972a3619 DD |
238 | return 0; |
239 | } | |
667e72a1 AT |
240 | } |
241 | } | |
c627d613 | 242 | #endif |
6a7cc46c | 243 | |
4ecc9e6b | 244 | if (verbose > 1 && flags & PERMS_REPORT) { |
8324bd5c WD |
245 | enum logcode code = daemon_log_format_has_i || dry_run |
246 | ? FCLIENT : FINFO; | |
667e72a1 | 247 | if (updated) |
8324bd5c | 248 | rprintf(code, "%s\n", safe_fname(fname)); |
667e72a1 | 249 | else |
8324bd5c | 250 | rprintf(code, "%s is uptodate\n", safe_fname(fname)); |
667e72a1 AT |
251 | } |
252 | return updated; | |
c627d613 AT |
253 | } |
254 | ||
255 | ||
34ccb63e AT |
256 | void sig_int(void) |
257 | { | |
d5a0b483 WD |
258 | /* KLUGE: if the user hits Ctrl-C while ssh is prompting |
259 | * for a password, then our cleanup's sending of a SIGUSR1 | |
260 | * signal to all our children may kill ssh before it has a | |
261 | * chance to restore the tty settings (i.e. turn echo back | |
262 | * on). By sleeping for a short time, ssh gets a bigger | |
263 | * chance to do the right thing. If child processes are | |
264 | * not ssh waiting for a password, then this tiny delay | |
265 | * shouldn't hurt anything. */ | |
266 | msleep(400); | |
65417579 | 267 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
268 | } |
269 | ||
270 | ||
c95da96a AT |
271 | /* finish off a file transfer, renaming the file and setting the permissions |
272 | and ownership */ | |
4ecc9e6b | 273 | void finish_transfer(char *fname, char *fnametmp, struct file_struct *file, |
997d9ea6 | 274 | int ok_to_set_time, int overwriting_basis) |
c95da96a | 275 | { |
62c9e6b3 WD |
276 | int ret; |
277 | ||
a3221d2a WD |
278 | if (inplace) { |
279 | if (verbose > 2) | |
4875d6b6 | 280 | rprintf(FINFO, "finishing %s\n", safe_fname(fname)); |
077e59b7 | 281 | goto do_set_perms; |
a3221d2a WD |
282 | } |
283 | ||
997d9ea6 | 284 | if (make_backups && overwriting_basis && !make_backup(fname)) |
e484f0cc WD |
285 | return; |
286 | ||
ebeacb36 WD |
287 | /* Change permissions before putting the file into place. */ |
288 | set_perms(fnametmp, file, NULL, ok_to_set_time ? 0 : PERMS_SKIP_MTIME); | |
289 | ||
c95da96a | 290 | /* move tmp file over real file */ |
4875d6b6 WD |
291 | if (verbose > 2) { |
292 | rprintf(FINFO, "renaming %s to %s\n", | |
293 | safe_fname(fnametmp), safe_fname(fname)); | |
294 | } | |
62c9e6b3 | 295 | ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS); |
3d061653 | 296 | if (ret < 0) { |
d62bcc17 | 297 | rsyserr(FERROR, errno, "%s %s -> \"%s\"", |
62c9e6b3 | 298 | ret == -2 ? "copy" : "rename", |
4875d6b6 | 299 | full_fname(fnametmp), safe_fname(fname)); |
c7c11a0d | 300 | do_unlink(fnametmp); |
077e59b7 | 301 | return; |
c95da96a | 302 | } |
ebeacb36 WD |
303 | if (ret == 0) { |
304 | /* The file was moved into place (not copied), so it's done. */ | |
305 | return; | |
306 | } | |
077e59b7 WD |
307 | do_set_perms: |
308 | set_perms(fname, file, NULL, ok_to_set_time ? 0 : PERMS_SKIP_MTIME); | |
c95da96a | 309 | } |
c3e5e585 WD |
310 | |
311 | const char *who_am_i(void) | |
312 | { | |
794b0a03 WD |
313 | if (am_sender < 0) |
314 | return am_server ? "server" : "client"; | |
ebeacb36 | 315 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
c3e5e585 | 316 | } |