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