1 /* -*- c-file-style: "linux" -*-
3 rsync -- fast file replication program
5 Copyright (C) 1996-2000 by Andrew Tridgell
6 Copyright (C) Paul Mackerras 1996
7 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 extern int relative_paths;
29 extern int preserve_links;
31 extern int preserve_devices;
32 extern int preserve_hard_links;
33 extern int update_only;
34 extern int opt_ignore_existing;
35 extern int block_size;
36 extern int csum_length;
37 extern int ignore_times;
39 extern int io_timeout;
40 extern int remote_version;
41 extern int always_checksum;
42 extern int modify_window;
43 extern char *compare_dest;
46 /* choose whether to skip a particular file */
47 static int skip_file(char *fname,
48 struct file_struct *file, STRUCT_STAT *st)
50 if (st->st_size != file->length) {
54 /* if always checksum is set then we use the checksum instead
55 of the file time to determine whether to sync */
56 if (always_checksum && S_ISREG(st->st_mode)) {
57 char sum[MD4_SUM_LENGTH];
58 char fnamecmpdest[MAXPATHLEN];
60 if (compare_dest != NULL) {
61 if (access(fname, 0) != 0) {
62 snprintf(fnamecmpdest,MAXPATHLEN,"%s/%s",
67 file_checksum(fname,sum,st->st_size);
68 if (remote_version < 21) {
69 return (memcmp(sum,file->sum,2) == 0);
71 return (memcmp(sum,file->sum,MD4_SUM_LENGTH) == 0);
83 return (cmp_modtime(st->st_mtime,file->modtime) == 0);
87 /* use a larger block size for really big files */
88 static int adapt_block_size(struct file_struct *file, int bsize)
92 if (bsize != BLOCK_SIZE) return bsize;
94 ret = file->length / (10000); /* rough heuristic */
95 ret = ret & ~15; /* multiple of 16 */
96 if (ret < bsize) ret = bsize;
97 if (ret > CHUNK_SIZE/2) ret = CHUNK_SIZE/2;
103 send a sums struct down a fd
105 static void send_sums(struct sum_struct *s, int f_out)
110 /* tell the other guy how many we are going to be
111 doing and how many bytes there are in the last
113 write_int(f_out, s->count);
114 write_int(f_out, s->n);
115 write_int(f_out, s->remainder);
117 for (i = 0; i < s->count; i++) {
118 write_int(f_out, s->sums[i].sum1);
119 write_buf(f_out, s->sums[i].sum2, csum_length);
122 /* we don't have checksums */
124 write_int(f_out, block_size);
131 * Perhaps we want to just send an empty checksum set for this file,
132 * which will force the whole thing to be literally transferred.
134 * When do we do this? If the user's explicitly said they
135 * want the whole thing, or if { they haven't explicitly
136 * requested a delta, and it's local but not batch mode.}
139 static BOOL disable_deltas_p(void)
141 extern int whole_file, no_whole_file;
142 extern int local_server;
143 extern int write_batch;
145 assert(whole_file == 0 || whole_file == 1);
147 /* whole_file and no_whole_file are never both on at the same time */
151 else if (no_whole_file)
153 else if (write_batch)
161 generate a stream of signatures/checksums that describe a buffer
163 generate approximately one checksum every n bytes
165 static struct sum_struct *generate_sums(struct map_struct *buf,OFF_T len,int n)
168 struct sum_struct *s;
171 int remainder = (len%block_len);
174 count = (len+(block_len-1))/block_len;
176 s = (struct sum_struct *)malloc(sizeof(*s));
177 if (!s) out_of_memory("generate_sums");
180 s->remainder = remainder;
190 rprintf(FINFO,"count=%d rem=%d n=%d flength=%.0f\n",
191 s->count,s->remainder,s->n,(double)s->flength);
193 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
194 if (!s->sums) out_of_memory("generate_sums");
196 for (i=0;i<count;i++) {
198 char *map = map_ptr(buf,offset,n1);
200 s->sums[i].sum1 = get_checksum1(map,n1);
201 get_checksum2(map,n1,s->sums[i].sum2);
203 s->sums[i].offset = offset;
208 rprintf(FINFO,"chunk[%d] offset=%.0f len=%d sum1=%08x\n",
209 i,(double)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
221 * Acts on file number I from FLIST, whose name is fname.
223 * First fixes up permissions, then generates checksums for the file.
225 * (This comment was added later by mbp who was trying to work it out;
226 * it might be wrong.)
228 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
232 struct map_struct *buf;
233 struct sum_struct *s;
235 struct file_struct *file = flist->files[i];
237 char fnamecmpbuf[MAXPATHLEN];
238 extern char *compare_dest;
239 extern int list_only;
240 extern int preserve_perms;
241 extern int only_existing;
243 if (list_only) return;
246 rprintf(FINFO,"recv_generator(%s,%d)\n",fname,i);
248 statret = link_stat(fname,&st);
250 if (only_existing && statret == -1 && errno == ENOENT) {
251 /* we only want to update existing files */
252 if (verbose > 1) rprintf(FINFO, "not creating new file \"%s\"\n",fname);
258 (S_ISDIR(st.st_mode) == S_ISDIR(file->mode))) {
259 /* if the file exists already and we aren't perserving
260 presmissions then act as though the remote end sent
261 us the file permissions we already have */
262 file->mode = (file->mode & _S_IFMT) | (st.st_mode & ~_S_IFMT);
265 if (S_ISDIR(file->mode)) {
266 /* The file to be received is a directory, so we need
267 * to prepare appropriately. If there is already a
268 * file of that name and it is *not* a directory, then
269 * we need to delete it. If it doesn't exist, then
270 * recursively create it. */
272 if (dry_run) return; /* XXXX -- might cause inaccuracies?? -- mbp */
273 if (statret == 0 && !S_ISDIR(st.st_mode)) {
274 if (robust_unlink(fname) != 0) {
275 rprintf(FERROR, RSYNC_NAME
276 ": recv_generator: unlink \"%s\" to make room for directory: %s\n",
277 fname,strerror(errno));
282 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
283 if (!(relative_paths && errno==ENOENT &&
284 create_directory_path(fname)==0 &&
285 do_mkdir(fname,file->mode)==0)) {
286 rprintf(FERROR, RSYNC_NAME ": recv_generator: mkdir \"%s\": %s (2)\n",
287 fname,strerror(errno));
290 /* f_out is set to -1 when doing final directory
291 permission and modification time repair */
292 if (set_perms(fname,file,NULL,0) && verbose && (f_out != -1))
293 rprintf(FINFO,"%s/\n",fname);
297 if (preserve_links && S_ISLNK(file->mode)) {
299 char lnk[MAXPATHLEN];
301 extern int safe_symlinks;
303 if (safe_symlinks && unsafe_symlink(file->link, fname)) {
305 rprintf(FINFO,"ignoring unsafe symlink \"%s\" -> \"%s\"\n",
311 l = readlink(fname,lnk,MAXPATHLEN-1);
314 /* A link already pointing to the
315 * right place -- no further action
317 if (strcmp(lnk,file->link) == 0) {
318 set_perms(fname,file,&st,1);
322 /* Not a symlink, so delete whatever's
323 * already there and put a new symlink
327 if (do_symlink(file->link,fname) != 0) {
328 rprintf(FERROR,RSYNC_NAME": symlink \"%s\" -> \"%s\": %s\n",
329 fname,file->link,strerror(errno));
331 set_perms(fname,file,NULL,0);
333 rprintf(FINFO,"%s -> %s\n", fname,file->link);
341 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
343 st.st_mode != file->mode ||
344 st.st_rdev != file->rdev) {
347 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
348 fname,(int)file->mode,(int)file->rdev);
349 if (do_mknod(fname,file->mode,file->rdev) != 0) {
350 rprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
352 set_perms(fname,file,NULL,0);
354 rprintf(FINFO,"%s\n",fname);
357 set_perms(fname,file,&st,1);
363 if (preserve_hard_links && check_hard_link(file)) {
365 rprintf(FINFO, "recv_generator: \"%s\" is a hard link\n",f_name(file));
369 if (!S_ISREG(file->mode)) {
370 rprintf(FINFO, "skipping non-regular file \"%s\"\n",fname);
376 if ((statret == -1) && (compare_dest != NULL)) {
377 /* try the file at compare_dest instead */
378 int saveerrno = errno;
379 snprintf(fnamecmpbuf,MAXPATHLEN,"%s/%s",compare_dest,fname);
380 statret = link_stat(fnamecmpbuf,&st);
381 if (!S_ISREG(st.st_mode))
386 fnamecmp = fnamecmpbuf;
390 if (errno == ENOENT) {
392 if (!dry_run) send_sums(NULL,f_out);
395 rprintf(FERROR, RSYNC_NAME
396 ": recv_generator failed to open \"%s\": %s\n",
397 fname, strerror(errno));
402 if (!S_ISREG(st.st_mode)) {
403 if (delete_file(fname) != 0) {
407 /* now pretend the file didn't exist */
409 if (!dry_run) send_sums(NULL,f_out);
413 if (opt_ignore_existing && fnamecmp == fname) {
415 rprintf(FINFO,"%s exists\n",fname);
419 if (update_only && cmp_modtime(st.st_mtime,file->modtime)>0 && fnamecmp == fname) {
421 rprintf(FINFO,"%s is newer\n",fname);
425 if (skip_file(fname, file, &st)) {
426 if (fnamecmp == fname)
427 set_perms(fname,file,&st,1);
436 if (disable_deltas_p()) {
438 send_sums(NULL,f_out);
443 fd = do_open(fnamecmp, O_RDONLY, 0);
446 rprintf(FERROR,RSYNC_NAME": failed to open \"%s\", continuing : %s\n",fnamecmp,strerror(errno));
447 /* pretend the file didn't exist */
449 send_sums(NULL,f_out);
453 if (st.st_size > 0) {
454 buf = map_file(fd,st.st_size);
460 rprintf(FINFO,"gen mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
462 s = generate_sums(buf,st.st_size,adapt_block_size(file, block_size));
465 rprintf(FINFO,"sending sums for %d\n",i);
471 if (buf) unmap_file(buf);
478 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
484 rprintf(FINFO,"generator starting pid=%d count=%d\n",
485 (int)getpid(),flist->count);
487 /* we expect to just sit around now, so don't exit on a
488 timeout. If we really get a timeout then the other process should
492 for (i = 0; i < flist->count; i++) {
493 struct file_struct *file = flist->files[i];
494 mode_t saved_mode = file->mode;
495 if (!file->basename) continue;
497 /* we need to ensure that any directories we create have writeable
498 permissions initially so that we can create the files within
499 them. This is then fixed after the files are transferred */
500 if (!am_root && S_ISDIR(file->mode)) {
501 file->mode |= S_IWUSR; /* user write */
502 /* XXX: Could this be causing a problem on SCO? Perhaps their
503 * handling of permissions is strange? */
506 recv_generator(local_name?local_name:f_name(file),
509 file->mode = saved_mode;
513 csum_length = SUM_LENGTH;
517 rprintf(FINFO,"generate_files phase=%d\n",phase);
521 if (remote_version >= 13) {
522 /* in newer versions of the protocol the files can cycle through
523 the system more than once to catch initial checksum errors */
524 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
525 struct file_struct *file = flist->files[i];
526 recv_generator(local_name?local_name:f_name(file),
532 rprintf(FINFO,"generate_files phase=%d\n",phase);