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; |
2f03f956 | 27 | extern int preserve_times; |
7b8356d0 | 28 | extern int am_root; |
c3e5e585 WD |
29 | extern int am_sender; |
30 | extern int am_generator; | |
2f03f956 AT |
31 | extern int preserve_uid; |
32 | extern int preserve_gid; | |
2f03f956 | 33 | extern int make_backups; |
fe055c71 AT |
34 | |
35 | ||
c627d613 AT |
36 | /* |
37 | free a sums struct | |
38 | */ | |
2f03f956 | 39 | void free_sums(struct sum_struct *s) |
c627d613 | 40 | { |
298c10d5 AT |
41 | if (s->sums) free(s->sums); |
42 | free(s); | |
c627d613 AT |
43 | } |
44 | ||
45 | ||
3cb6f5d6 | 46 | /* |
7a2fd68b WD |
47 | * delete a file or directory. If force_delete is set then delete |
48 | * recursively | |
3cb6f5d6 | 49 | */ |
2f03f956 | 50 | int delete_file(char *fname) |
3cb6f5d6 AT |
51 | { |
52 | DIR *d; | |
53 | struct dirent *di; | |
54 | char buf[MAXPATHLEN]; | |
55 | extern int force_delete; | |
bcacc18b | 56 | STRUCT_STAT st; |
3cb6f5d6 | 57 | int ret; |
05848a2c | 58 | extern int recurse; |
3cb6f5d6 | 59 | |
3cb6f5d6 | 60 | #if SUPPORT_LINKS |
bcacc18b | 61 | ret = do_lstat(fname, &st); |
3cb6f5d6 | 62 | #else |
bcacc18b | 63 | ret = do_stat(fname, &st); |
3cb6f5d6 | 64 | #endif |
a7ed6ca6 | 65 | if (ret) |
3cb6f5d6 | 66 | return -1; |
3cb6f5d6 AT |
67 | |
68 | if (!S_ISDIR(st.st_mode)) { | |
a7ed6ca6 WD |
69 | if (robust_unlink(fname) == 0 || errno == ENOENT) |
70 | return 0; | |
d62bcc17 WD |
71 | rsyserr(FERROR, errno, "delete_file: unlink %s failed", |
72 | full_fname(fname)); | |
3cb6f5d6 AT |
73 | return -1; |
74 | } | |
75 | ||
a7ed6ca6 WD |
76 | if (do_rmdir(fname) == 0 || errno == ENOENT) |
77 | return 0; | |
78 | if (!force_delete || !recurse | |
79 | || (errno != ENOTEMPTY && errno != EEXIST)) { | |
d62bcc17 WD |
80 | rsyserr(FERROR, errno, "delete_file: rmdir %s failed", |
81 | full_fname(fname)); | |
3cb6f5d6 AT |
82 | return -1; |
83 | } | |
84 | ||
85 | /* now we do a recsursive delete on the directory ... */ | |
a7ed6ca6 | 86 | if (!(d = opendir(fname))) { |
d62bcc17 WD |
87 | rsyserr(FERROR, errno, "delete_file: opendir %s failed", |
88 | full_fname(fname)); | |
3cb6f5d6 AT |
89 | return -1; |
90 | } | |
91 | ||
6a7cc46c | 92 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 93 | char *dname = d_name(di); |
a7ed6ca6 WD |
94 | if (dname[0] == '.' && (dname[1] == '\0' |
95 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3cb6f5d6 | 96 | continue; |
a7725e6d | 97 | pathjoin(buf, sizeof buf, fname, dname); |
3cb6f5d6 | 98 | if (verbose > 0) |
a7ed6ca6 | 99 | rprintf(FINFO, "deleting %s\n", buf); |
3cb6f5d6 AT |
100 | if (delete_file(buf) != 0) { |
101 | closedir(d); | |
102 | return -1; | |
103 | } | |
7a2fd68b | 104 | } |
6a7cc46c | 105 | if (errno) { |
d62bcc17 WD |
106 | rsyserr(FERROR, errno, "delete_file: readdir %s failed", |
107 | full_fname(fname)); | |
6a7cc46c S |
108 | closedir(d); |
109 | return -1; | |
110 | } | |
3cb6f5d6 AT |
111 | |
112 | closedir(d); | |
7a2fd68b | 113 | |
3cb6f5d6 | 114 | if (do_rmdir(fname) != 0) { |
d62bcc17 WD |
115 | rsyserr(FERROR, errno, "delete_file: rmdir %s failed", |
116 | full_fname(fname)); | |
3cb6f5d6 AT |
117 | return -1; |
118 | } | |
119 | ||
120 | return 0; | |
121 | } | |
c627d613 | 122 | |
2f03f956 | 123 | int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st, |
4ecc9e6b | 124 | int flags) |
c627d613 | 125 | { |
667e72a1 AT |
126 | int updated = 0; |
127 | STRUCT_STAT st2; | |
460f6b99 | 128 | int change_uid, change_gid; |
c627d613 | 129 | |
667e72a1 | 130 | if (dry_run) return 0; |
c627d613 | 131 | |
667e72a1 AT |
132 | if (!st) { |
133 | if (link_stat(fname,&st2) != 0) { | |
d62bcc17 WD |
134 | rsyserr(FERROR, errno, "stat %s failed", |
135 | full_fname(fname)); | |
667e72a1 AT |
136 | return 0; |
137 | } | |
138 | st = &st2; | |
139 | } | |
c627d613 | 140 | |
4ecc9e6b | 141 | if (!preserve_times || S_ISLNK(st->st_mode)) |
38443188 WD |
142 | flags |= PERMS_SKIP_MTIME; |
143 | if (!(flags & PERMS_SKIP_MTIME) | |
4ecc9e6b | 144 | && cmp_modtime(st->st_mtime, file->modtime) != 0) { |
8d249b63 | 145 | /* don't complain about not setting times on directories |
a174e1ed | 146 | * because some filesystems can't do it */ |
8d249b63 AT |
147 | if (set_modtime(fname,file->modtime) != 0 && |
148 | !S_ISDIR(st->st_mode)) { | |
d62bcc17 WD |
149 | rsyserr(FERROR, errno, "failed to set times on %s", |
150 | full_fname(fname)); | |
667e72a1 AT |
151 | return 0; |
152 | } | |
a174e1ed | 153 | updated = 1; |
667e72a1 AT |
154 | } |
155 | ||
460f6b99 | 156 | change_uid = am_root && preserve_uid && st->st_uid != file->uid; |
a60e2dca | 157 | change_gid = preserve_gid && file->gid != GID_NONE |
7352b873 | 158 | && st->st_gid != file->gid; |
460f6b99 | 159 | if (change_uid || change_gid) { |
62c9e6b3 | 160 | if (verbose > 2) { |
ce37eb2d WD |
161 | if (change_uid) { |
162 | rprintf(FINFO, | |
163 | "set uid of %s from %ld to %ld\n", | |
164 | fname, (long)st->st_uid, (long)file->uid); | |
165 | } | |
166 | if (change_gid) { | |
167 | rprintf(FINFO, | |
168 | "set gid of %s from %ld to %ld\n", | |
169 | fname, (long)st->st_gid, (long)file->gid); | |
170 | } | |
171 | } | |
667e72a1 | 172 | if (do_lchown(fname, |
a174e1ed WD |
173 | change_uid ? file->uid : st->st_uid, |
174 | change_gid ? file->gid : st->st_gid) != 0) { | |
460f6b99 | 175 | /* shouldn't have attempted to change uid or gid |
a174e1ed | 176 | * unless have the privilege */ |
d62bcc17 | 177 | rsyserr(FERROR, errno, "%s %s failed", |
a174e1ed | 178 | change_uid ? "chown" : "chgrp", |
d62bcc17 | 179 | full_fname(fname)); |
460f6b99 | 180 | return 0; |
667e72a1 | 181 | } |
a5827a28 | 182 | /* a lchown had been done - we have to re-stat if the |
a174e1ed WD |
183 | * destination had the setuid or setgid bits set due |
184 | * to the side effect of the chown call */ | |
a5827a28 AT |
185 | if (st->st_mode & (S_ISUID | S_ISGID)) { |
186 | link_stat(fname, st); | |
187 | } | |
460f6b99 | 188 | updated = 1; |
667e72a1 | 189 | } |
c627d613 AT |
190 | |
191 | #ifdef HAVE_CHMOD | |
972a3619 | 192 | if (!S_ISLNK(st->st_mode)) { |
b0d791bb | 193 | if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) { |
972a3619 | 194 | updated = 1; |
b0d791bb | 195 | if (do_chmod(fname,(file->mode & CHMOD_BITS)) != 0) { |
d62bcc17 WD |
196 | rsyserr(FERROR, errno, "failed to set permissions on %s", |
197 | full_fname(fname)); | |
972a3619 DD |
198 | return 0; |
199 | } | |
667e72a1 AT |
200 | } |
201 | } | |
c627d613 | 202 | #endif |
6a7cc46c | 203 | |
4ecc9e6b | 204 | if (verbose > 1 && flags & PERMS_REPORT) { |
667e72a1 AT |
205 | if (updated) |
206 | rprintf(FINFO,"%s\n",fname); | |
207 | else | |
208 | rprintf(FINFO,"%s is uptodate\n",fname); | |
209 | } | |
210 | return updated; | |
c627d613 AT |
211 | } |
212 | ||
213 | ||
34ccb63e AT |
214 | void sig_int(void) |
215 | { | |
d5a0b483 WD |
216 | /* KLUGE: if the user hits Ctrl-C while ssh is prompting |
217 | * for a password, then our cleanup's sending of a SIGUSR1 | |
218 | * signal to all our children may kill ssh before it has a | |
219 | * chance to restore the tty settings (i.e. turn echo back | |
220 | * on). By sleeping for a short time, ssh gets a bigger | |
221 | * chance to do the right thing. If child processes are | |
222 | * not ssh waiting for a password, then this tiny delay | |
223 | * shouldn't hurt anything. */ | |
224 | msleep(400); | |
65417579 | 225 | exit_cleanup(RERR_SIGNAL); |
c627d613 AT |
226 | } |
227 | ||
228 | ||
c95da96a AT |
229 | /* finish off a file transfer, renaming the file and setting the permissions |
230 | and ownership */ | |
4ecc9e6b WD |
231 | void finish_transfer(char *fname, char *fnametmp, struct file_struct *file, |
232 | int ok_to_set_time) | |
c95da96a | 233 | { |
62c9e6b3 WD |
234 | int ret; |
235 | ||
53dd3135 DD |
236 | if (make_backups && !make_backup(fname)) |
237 | return; | |
c95da96a AT |
238 | |
239 | /* move tmp file over real file */ | |
62c9e6b3 | 240 | ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS); |
3d061653 | 241 | if (ret < 0) { |
d62bcc17 | 242 | rsyserr(FERROR, errno, "%s %s -> \"%s\"", |
62c9e6b3 | 243 | ret == -2 ? "copy" : "rename", |
d62bcc17 | 244 | full_fname(fnametmp), fname); |
c7c11a0d | 245 | do_unlink(fnametmp); |
c95da96a | 246 | } else { |
4ecc9e6b | 247 | set_perms(fname, file, NULL, |
38443188 | 248 | ok_to_set_time ? 0 : PERMS_SKIP_MTIME); |
c95da96a AT |
249 | } |
250 | } | |
c3e5e585 WD |
251 | |
252 | const char *who_am_i(void) | |
253 | { | |
254 | return am_sender ? "sender" : am_generator ? "generator" : "receiver"; | |
255 | } |