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