If the file did not transfer correctly, only save it if --partial was
[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_server;
30extern int am_sender;
31extern int am_generator;
2f03f956
AT
32extern int preserve_uid;
33extern int preserve_gid;
34extern int preserve_perms;
35extern int make_backups;
fe055c71
AT
36
37
c627d613
AT
38/*
39 free a sums struct
40 */
2f03f956 41void free_sums(struct sum_struct *s)
c627d613 42{
298c10d5
AT
43 if (s->sums) free(s->sums);
44 free(s);
c627d613
AT
45}
46
47
3cb6f5d6 48/*
7a2fd68b
WD
49 * delete a file or directory. If force_delete is set then delete
50 * recursively
3cb6f5d6 51 */
2f03f956 52int delete_file(char *fname)
3cb6f5d6
AT
53{
54 DIR *d;
55 struct dirent *di;
56 char buf[MAXPATHLEN];
57 extern int force_delete;
bcacc18b 58 STRUCT_STAT st;
3cb6f5d6 59 int ret;
05848a2c 60 extern int recurse;
3cb6f5d6 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;
ea42541f
WD
73 rprintf(FERROR, "delete_file: unlink %s failed: %s\n",
74 full_fname(fname), strerror(errno));
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)) {
ea42541f
WD
82 rprintf(FERROR, "delete_file: rmdir %s failed: %s\n",
83 full_fname(fname), strerror(errno));
3cb6f5d6
AT
84 return -1;
85 }
86
87 /* now we do a recsursive delete on the directory ... */
a7ed6ca6 88 if (!(d = opendir(fname))) {
ea42541f
WD
89 rprintf(FERROR, "delete_file: opendir %s failed: %s\n",
90 full_fname(fname), strerror(errno));
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)
a7ed6ca6 101 rprintf(FINFO, "deleting %s\n", buf);
3cb6f5d6
AT
102 if (delete_file(buf) != 0) {
103 closedir(d);
104 return -1;
105 }
7a2fd68b 106 }
6a7cc46c 107 if (errno) {
7a2fd68b
WD
108 rprintf(FERROR, "delete_file: readdir %s failed: %s\n",
109 full_fname(fname), strerror(errno));
6a7cc46c
S
110 closedir(d);
111 return -1;
112 }
3cb6f5d6
AT
113
114 closedir(d);
7a2fd68b 115
3cb6f5d6 116 if (do_rmdir(fname) != 0) {
ea42541f
WD
117 rprintf(FERROR, "delete_file: rmdir %s failed: %s\n",
118 full_fname(fname), strerror(errno));
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,
6a7cc46c 126 int report)
c627d613 127{
667e72a1
AT
128 int updated = 0;
129 STRUCT_STAT st2;
460f6b99 130 int change_uid, change_gid;
c627d613 131
667e72a1 132 if (dry_run) return 0;
c627d613 133
667e72a1
AT
134 if (!st) {
135 if (link_stat(fname,&st2) != 0) {
ea42541f
WD
136 rprintf(FERROR, "stat %s failed: %s\n",
137 full_fname(fname), strerror(errno));
667e72a1
AT
138 return 0;
139 }
140 st = &st2;
141 }
c627d613 142
667e72a1 143 if (preserve_times && !S_ISLNK(st->st_mode) &&
5b56cc19 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)) {
ea42541f
WD
149 rprintf(FERROR, "failed to set times on %s: %s\n",
150 full_fname(fname), strerror(errno));
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 */
baa4212a 177 rprintf(FERROR, "%s %s failed: %s\n",
a174e1ed
WD
178 change_uid ? "chown" : "chgrp",
179 full_fname(fname), strerror(errno));
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) {
ea42541f
WD
196 rprintf(FERROR, "failed to set permissions on %s: %s\n",
197 full_fname(fname), strerror(errno));
972a3619
DD
198 return 0;
199 }
667e72a1
AT
200 }
201 }
c627d613 202#endif
6a7cc46c 203
667e72a1
AT
204 if (verbose > 1 && report) {
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
214void 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 */
2f03f956 231void finish_transfer(char *fname, char *fnametmp, struct file_struct *file)
c95da96a 232{
62c9e6b3
WD
233 int ret;
234
53dd3135
DD
235 if (make_backups && !make_backup(fname))
236 return;
c95da96a
AT
237
238 /* move tmp file over real file */
62c9e6b3 239 ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS);
3d061653 240 if (ret < 0) {
62c9e6b3
WD
241 rprintf(FERROR, "%s %s -> \"%s\": %s\n",
242 ret == -2 ? "copy" : "rename",
243 full_fname(fnametmp), fname, strerror(errno));
c7c11a0d 244 do_unlink(fnametmp);
c95da96a
AT
245 } else {
246 set_perms(fname,file,NULL,0);
247 }
248}
c3e5e585
WD
249
250const char *who_am_i(void)
251{
252 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
253}