Changed rprintf() calls that included strerror() to use rsyserr().
[rsync/rsync.git] / rsync.c
1 /*
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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
20 /* this file contains code used by more than one part of the rsync
21    process */
22
23 #include "rsync.h"
24
25 extern int verbose;
26 extern int dry_run;
27 extern int preserve_times;
28 extern int am_root;
29 extern int am_sender;
30 extern int am_generator;
31 extern int preserve_uid;
32 extern int preserve_gid;
33 extern int make_backups;
34
35
36 /*
37   free a sums struct
38   */
39 void free_sums(struct sum_struct *s)
40 {
41         if (s->sums) free(s->sums);
42         free(s);
43 }
44
45
46 /*
47  * delete a file or directory. If force_delete is set then delete
48  * recursively
49  */
50 int delete_file(char *fname)
51 {
52         DIR *d;
53         struct dirent *di;
54         char buf[MAXPATHLEN];
55         extern int force_delete;
56         STRUCT_STAT st;
57         int ret;
58         extern int recurse;
59
60 #if SUPPORT_LINKS
61         ret = do_lstat(fname, &st);
62 #else
63         ret = do_stat(fname, &st);
64 #endif
65         if (ret)
66                 return -1;
67
68         if (!S_ISDIR(st.st_mode)) {
69                 if (robust_unlink(fname) == 0 || errno == ENOENT)
70                         return 0;
71                 rprintf(FERROR, "delete_file: unlink %s failed: %s\n",
72                         full_fname(fname), strerror(errno));
73                 return -1;
74         }
75
76         if (do_rmdir(fname) == 0 || errno == ENOENT)
77                 return 0;
78         if (!force_delete || !recurse
79             || (errno != ENOTEMPTY && errno != EEXIST)) {
80                 rprintf(FERROR, "delete_file: rmdir %s failed: %s\n",
81                         full_fname(fname), strerror(errno));
82                 return -1;
83         }
84
85         /* now we do a recsursive delete on the directory ... */
86         if (!(d = opendir(fname))) {
87                 rprintf(FERROR, "delete_file: opendir %s failed: %s\n",
88                         full_fname(fname), strerror(errno));
89                 return -1;
90         }
91
92         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
93                 char *dname = d_name(di);
94                 if (dname[0] == '.' && (dname[1] == '\0'
95                     || (dname[1] == '.' && dname[2] == '\0')))
96                         continue;
97                 pathjoin(buf, sizeof buf, fname, dname);
98                 if (verbose > 0)
99                         rprintf(FINFO, "deleting %s\n", buf);
100                 if (delete_file(buf) != 0) {
101                         closedir(d);
102                         return -1;
103                 }
104         }
105         if (errno) {
106                 rprintf(FERROR, "delete_file: readdir %s failed: %s\n",
107                         full_fname(fname), strerror(errno));
108                 closedir(d);
109                 return -1;
110         }
111
112         closedir(d);
113
114         if (do_rmdir(fname) != 0) {
115                 rprintf(FERROR, "delete_file: rmdir %s failed: %s\n",
116                         full_fname(fname), strerror(errno));
117                 return -1;
118         }
119
120         return 0;
121 }
122
123 int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st,
124               int flags)
125 {
126         int updated = 0;
127         STRUCT_STAT st2;
128         int change_uid, change_gid;
129
130         if (dry_run) return 0;
131
132         if (!st) {
133                 if (link_stat(fname,&st2) != 0) {
134                         rprintf(FERROR, "stat %s failed: %s\n",
135                                 full_fname(fname), strerror(errno));
136                         return 0;
137                 }
138                 st = &st2;
139         }
140
141         if (!preserve_times || S_ISLNK(st->st_mode))
142                 flags |= PERMS_SKIP_MTIME;
143         if (!(flags & PERMS_SKIP_MTIME)
144             && cmp_modtime(st->st_mtime, file->modtime) != 0) {
145                 /* don't complain about not setting times on directories
146                  * because some filesystems can't do it */
147                 if (set_modtime(fname,file->modtime) != 0 &&
148                     !S_ISDIR(st->st_mode)) {
149                         rprintf(FERROR, "failed to set times on %s: %s\n",
150                                 full_fname(fname), strerror(errno));
151                         return 0;
152                 }
153                 updated = 1;
154         }
155
156         change_uid = am_root && preserve_uid && st->st_uid != file->uid;
157         change_gid = preserve_gid && file->gid != GID_NONE
158                 && st->st_gid != file->gid;
159         if (change_uid || change_gid) {
160                 if (verbose > 2) {
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                 }
172                 if (do_lchown(fname,
173                     change_uid ? file->uid : st->st_uid,
174                     change_gid ? file->gid : st->st_gid) != 0) {
175                         /* shouldn't have attempted to change uid or gid
176                          * unless have the privilege */
177                         rprintf(FERROR, "%s %s failed: %s\n",
178                             change_uid ? "chown" : "chgrp",
179                             full_fname(fname), strerror(errno));
180                         return 0;
181                 }
182                 /* a lchown had been done - we have to re-stat if the
183                  * destination had the setuid or setgid bits set due
184                  * to the side effect of the chown call */
185                 if (st->st_mode & (S_ISUID | S_ISGID)) {
186                         link_stat(fname, st);
187                 }
188                 updated = 1;
189         }
190
191 #ifdef HAVE_CHMOD
192         if (!S_ISLNK(st->st_mode)) {
193                 if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
194                         updated = 1;
195                         if (do_chmod(fname,(file->mode & CHMOD_BITS)) != 0) {
196                                 rprintf(FERROR, "failed to set permissions on %s: %s\n",
197                                         full_fname(fname), strerror(errno));
198                                 return 0;
199                         }
200                 }
201         }
202 #endif
203
204         if (verbose > 1 && flags & PERMS_REPORT) {
205                 if (updated)
206                         rprintf(FINFO,"%s\n",fname);
207                 else
208                         rprintf(FINFO,"%s is uptodate\n",fname);
209         }
210         return updated;
211 }
212
213
214 void sig_int(void)
215 {
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);
225         exit_cleanup(RERR_SIGNAL);
226 }
227
228
229 /* finish off a file transfer, renaming the file and setting the permissions
230    and ownership */
231 void finish_transfer(char *fname, char *fnametmp, struct file_struct *file,
232                      int ok_to_set_time)
233 {
234         int ret;
235
236         if (make_backups && !make_backup(fname))
237                 return;
238
239         /* move tmp file over real file */
240         ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS);
241         if (ret < 0) {
242                 rprintf(FERROR, "%s %s -> \"%s\": %s\n",
243                     ret == -2 ? "copy" : "rename",
244                     full_fname(fnametmp), fname, strerror(errno));
245                 do_unlink(fnametmp);
246         } else {
247                 set_perms(fname, file, NULL,
248                           ok_to_set_time ? 0 : PERMS_SKIP_MTIME);
249         }
250 }
251
252 const char *who_am_i(void)
253 {
254     return am_sender ? "sender" : am_generator ? "generator" : "receiver";
255 }