Moved write_ndx_and_attrs() too.
[rsync/rsync.git] / rsync.c
1 /*
2  * Routines common to more than one of the rsync processes.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 #include "rsync.h"
24 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
25 #include <iconv.h>
26 #endif
27 #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
28 #include <libcharset.h>
29 #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
30 #include <langinfo.h>
31 #endif
32
33 extern int verbose;
34 extern int dry_run;
35 extern int preserve_perms;
36 extern int preserve_executability;
37 extern int preserve_times;
38 extern int omit_dir_times;
39 extern int am_root;
40 extern int am_server;
41 extern int am_sender;
42 extern int am_generator;
43 extern int am_starting_up;
44 extern int allow_8bit_chars;
45 extern int protocol_version;
46 extern int preserve_uid;
47 extern int preserve_gid;
48 extern int inplace;
49 extern int keep_dirlinks;
50 extern int make_backups;
51 extern mode_t orig_umask;
52 extern struct stats stats;
53 extern struct file_list *the_file_list;
54 extern struct chmod_mode_struct *daemon_chmod_modes;
55
56 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
57 iconv_t ic_chck = (iconv_t)-1;
58
59 static const char *default_charset(void)
60 {
61 #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
62         return locale_charset();
63 #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
64         return nl_langinfo(CODESET);
65 #else
66         return ""; /* Works with (at the very least) gnu iconv... */
67 #endif
68 }
69
70 void setup_iconv()
71 {
72         if (!am_server && !allow_8bit_chars) {
73                 const char *defset = default_charset();
74
75                 /* It's OK if this fails... */
76                 ic_chck = iconv_open(defset, defset);
77
78                 if (verbose > 3) {
79                         if (ic_chck == (iconv_t)-1) {
80                                 rprintf(FINFO,
81                                         "note: iconv_open(\"%s\", \"%s\") failed (%d)"
82                                         " -- using isprint() instead of iconv().\n",
83                                         defset, defset, errno);
84                         } else {
85                                 rprintf(FINFO,
86                                         "note: iconv_open(\"%s\", \"%s\") succeeded.\n",
87                                         defset, defset);
88                         }
89                 }
90         }
91 }
92 #endif
93
94 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
95                                 uchar fnamecmp_type, char *buf, int len)
96 {
97         write_int(f_out, ndx);
98         if (protocol_version < 29)
99                 return;
100         write_shortint(f_out, iflags);
101         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
102                 write_byte(f_out, fnamecmp_type);
103         if (iflags & ITEM_XNAME_FOLLOWS)
104                 write_vstring(f_out, buf, len);
105 }
106
107 /* This is used by sender.c with a valid f_out, and by receive.c with
108  * f_out = -1. */
109 int read_item_attrs(int f_in, int f_out, int ndx, uchar *type_ptr,
110                     char *buf, int *len_ptr)
111 {
112         int len;
113         uchar fnamecmp_type = FNAMECMP_FNAME;
114         int iflags = protocol_version >= 29 ? read_shortint(f_in)
115                    : ITEM_TRANSFER | ITEM_MISSING_DATA;
116
117         /* Handle the new keep-alive (no-op) packet. */
118         if (ndx == the_file_list->count && iflags == ITEM_IS_NEW)
119                 ;
120         else if (ndx < 0 || ndx >= the_file_list->count) {
121                 rprintf(FERROR, "Invalid file index: %d (count=%d) [%s]\n",
122                         ndx, the_file_list->count, who_am_i());
123                 exit_cleanup(RERR_PROTOCOL);
124         } else if (iflags == ITEM_IS_NEW) {
125                 rprintf(FERROR, "Invalid itemized flag word: %x [%s]\n",
126                         iflags, who_am_i());
127                 exit_cleanup(RERR_PROTOCOL);
128         }
129
130         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
131                 fnamecmp_type = read_byte(f_in);
132         *type_ptr = fnamecmp_type;
133
134         if (iflags & ITEM_XNAME_FOLLOWS) {
135                 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
136                         exit_cleanup(RERR_PROTOCOL);
137         } else {
138                 *buf = '\0';
139                 len = -1;
140         }
141         *len_ptr = len;
142
143         if (iflags & ITEM_TRANSFER) {
144                 if (!S_ISREG(the_file_list->files[ndx]->mode)) {
145                         rprintf(FERROR,
146                                 "received request to transfer non-regular file: %d [%s]\n",
147                                 ndx, who_am_i());
148                         exit_cleanup(RERR_PROTOCOL);
149                 }
150         } else if (f_out >= 0) {
151                 write_ndx_and_attrs(f_out, ndx, iflags,
152                                     fnamecmp_type, buf, len);
153         }
154
155         return iflags;
156 }
157
158 /*
159   free a sums struct
160   */
161 void free_sums(struct sum_struct *s)
162 {
163         if (s->sums) free(s->sums);
164         free(s);
165 }
166
167 /* This is only called when we aren't preserving permissions.  Figure out what
168  * the permissions should be and return them merged back into the mode. */
169 mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists)
170 {
171         int new_mode;
172         /* If the file already exists, we'll return the local permissions,
173          * possibly tweaked by the --executability option. */
174         if (exists) {
175                 new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
176                 if (preserve_executability && S_ISREG(flist_mode)) {
177                         /* If the source file is executable, grant execute
178                          * rights to everyone who can read, but ONLY if the
179                          * file isn't already executable. */
180                         if (!(flist_mode & 0111))
181                                 new_mode &= ~0111;
182                         else if (!(stat_mode & 0111))
183                                 new_mode |= (new_mode & 0444) >> 2;
184                 }
185         } else {
186                 /* Apply the umask and turn off special permissions. */
187                 new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask));
188         }
189         return new_mode;
190 }
191
192 int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
193                    int flags)
194 {
195         int updated = 0;
196         STRUCT_STAT st2;
197         int change_uid, change_gid;
198         mode_t new_mode = file->mode;
199
200         if (!st) {
201                 if (dry_run)
202                         return 1;
203                 if (link_stat(fname, &st2, 0) < 0) {
204                         rsyserr(FERROR, errno, "stat %s failed",
205                                 full_fname(fname));
206                         return 0;
207                 }
208                 st = &st2;
209                 if (!preserve_perms && S_ISDIR(new_mode)
210                  && st->st_mode & S_ISGID) {
211                         /* We just created this directory and its setgid
212                          * bit is on, so make sure it stays on. */
213                         new_mode |= S_ISGID;
214                 }
215         }
216
217         if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
218                 flags |= ATTRS_SKIP_MTIME;
219         if (!(flags & ATTRS_SKIP_MTIME)
220             && cmp_time(st->st_mtime, file->modtime) != 0) {
221                 int ret = set_modtime(fname, file->modtime, st->st_mode);
222                 if (ret < 0) {
223                         rsyserr(FERROR, errno, "failed to set times on %s",
224                                 full_fname(fname));
225                         return 0;
226                 }
227                 if (ret == 0) /* ret == 1 if symlink could not be set */
228                         updated = 1;
229         }
230
231         change_uid = am_root && preserve_uid && st->st_uid != file->uid;
232         change_gid = preserve_gid && file->gid != GID_NONE
233                 && st->st_gid != file->gid;
234 #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
235         if (S_ISLNK(st->st_mode))
236                 ;
237         else
238 #endif
239         if (change_uid || change_gid) {
240                 if (verbose > 2) {
241                         if (change_uid) {
242                                 rprintf(FINFO,
243                                         "set uid of %s from %ld to %ld\n",
244                                         fname,
245                                         (long)st->st_uid, (long)file->uid);
246                         }
247                         if (change_gid) {
248                                 rprintf(FINFO,
249                                         "set gid of %s from %ld to %ld\n",
250                                         fname,
251                                         (long)st->st_gid, (long)file->gid);
252                         }
253                 }
254                 if (do_lchown(fname,
255                     change_uid ? file->uid : st->st_uid,
256                     change_gid ? file->gid : st->st_gid) != 0) {
257                         /* shouldn't have attempted to change uid or gid
258                          * unless have the privilege */
259                         rsyserr(FERROR, errno, "%s %s failed",
260                             change_uid ? "chown" : "chgrp",
261                             full_fname(fname));
262                         return 0;
263                 }
264                 /* a lchown had been done - we have to re-stat if the
265                  * destination had the setuid or setgid bits set due
266                  * to the side effect of the chown call */
267                 if (st->st_mode & (S_ISUID | S_ISGID)) {
268                         link_stat(fname, st,
269                                   keep_dirlinks && S_ISDIR(st->st_mode));
270                 }
271                 updated = 1;
272         }
273
274         if (daemon_chmod_modes && !S_ISLNK(new_mode))
275                 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
276 #ifdef HAVE_CHMOD
277         if ((st->st_mode & CHMOD_BITS) != (new_mode & CHMOD_BITS)) {
278                 int ret = do_chmod(fname, new_mode);
279                 if (ret < 0) {
280                         rsyserr(FERROR, errno,
281                                 "failed to set permissions on %s",
282                                 full_fname(fname));
283                         return 0;
284                 }
285                 if (ret == 0) /* ret == 1 if symlink could not be set */
286                         updated = 1;
287         }
288 #endif
289
290         if (verbose > 1 && flags & ATTRS_REPORT) {
291                 if (updated)
292                         rprintf(FCLIENT, "%s\n", fname);
293                 else
294                         rprintf(FCLIENT, "%s is uptodate\n", fname);
295         }
296         return updated;
297 }
298
299 RETSIGTYPE sig_int(UNUSED(int val))
300 {
301         /* KLUGE: if the user hits Ctrl-C while ssh is prompting
302          * for a password, then our cleanup's sending of a SIGUSR1
303          * signal to all our children may kill ssh before it has a
304          * chance to restore the tty settings (i.e. turn echo back
305          * on).  By sleeping for a short time, ssh gets a bigger
306          * chance to do the right thing.  If child processes are
307          * not ssh waiting for a password, then this tiny delay
308          * shouldn't hurt anything. */
309         msleep(400);
310         exit_cleanup(RERR_SIGNAL);
311 }
312
313 /* Finish off a file transfer: renaming the file and setting the file's
314  * attributes (e.g. permissions, ownership, etc.).  If partialptr is not
315  * NULL and the robust_rename() call is forced to copy the temp file, we
316  * stage the file into the partial-dir and then rename it into place. */
317 void finish_transfer(char *fname, char *fnametmp, char *partialptr,
318                      struct file_struct *file, int ok_to_set_time,
319                      int overwriting_basis)
320 {
321         int ret;
322
323         if (inplace) {
324                 if (verbose > 2)
325                         rprintf(FINFO, "finishing %s\n", fname);
326                 fnametmp = fname;
327                 goto do_set_file_attrs;
328         }
329
330         if (make_backups && overwriting_basis && !make_backup(fname))
331                 return;
332
333         /* Change permissions before putting the file into place. */
334         set_file_attrs(fnametmp, file, NULL,
335                        ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
336
337         /* move tmp file over real file */
338         if (verbose > 2)
339                 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
340         ret = robust_rename(fnametmp, fname, partialptr,
341                             file->mode & INITACCESSPERMS);
342         if (ret < 0) {
343                 rsyserr(FERROR, errno, "%s %s -> \"%s\"",
344                         ret == -2 ? "copy" : "rename",
345                         full_fname(fnametmp), fname);
346                 do_unlink(fnametmp);
347                 return;
348         }
349         if (ret == 0) {
350                 /* The file was moved into place (not copied), so it's done. */
351                 return;
352         }
353         /* The file was copied, so tweak the perms of the copied file.  If it
354          * was copied to partialptr, move it into its final destination. */
355         fnametmp = partialptr ? partialptr : fname;
356
357   do_set_file_attrs:
358         set_file_attrs(fnametmp, file, NULL,
359                        ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
360
361         if (partialptr) {
362                 if (do_rename(fnametmp, fname) < 0) {
363                         rsyserr(FERROR, errno, "rename %s -> \"%s\"",
364                                 full_fname(fnametmp), fname);
365                 } else
366                         handle_partial_dir(partialptr, PDIR_DELETE);
367         }
368 }
369
370 const char *who_am_i(void)
371 {
372         if (am_starting_up)
373                 return am_server ? "server" : "client";
374         return am_sender ? "sender" : am_generator ? "generator" : "receiver";
375 }