2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
24 extern int relative_paths;
25 extern int preserve_links;
27 extern int preserve_devices;
28 extern int preserve_hard_links;
29 extern int update_only;
30 extern int whole_file;
31 extern int block_size;
32 extern int csum_length;
33 extern int ignore_times;
35 extern int io_timeout;
36 extern int remote_version;
37 extern int always_checksum;
40 /* choose whether to skip a particular file */
41 static int skip_file(char *fname,
42 struct file_struct *file, STRUCT_STAT *st)
44 if (st->st_size != file->length) {
48 /* if always checksum is set then we use the checksum instead
49 of the file time to determine whether to sync */
50 if (always_checksum && S_ISREG(st->st_mode)) {
51 char sum[MD4_SUM_LENGTH];
52 file_checksum(fname,sum,st->st_size);
53 return (memcmp(sum,file->sum,csum_length) == 0);
64 return (st->st_mtime == file->modtime);
68 /* use a larger block size for really big files */
69 static int adapt_block_size(struct file_struct *file, int bsize)
73 if (bsize != BLOCK_SIZE) return bsize;
75 ret = file->length / (10000); /* rough heuristic */
76 ret = ret & ~15; /* multiple of 16 */
77 if (ret < bsize) ret = bsize;
78 if (ret > CHUNK_SIZE/2) ret = CHUNK_SIZE/2;
84 send a sums struct down a fd
86 static void send_sums(struct sum_struct *s,int f_out)
90 /* tell the other guy how many we are going to be doing and how many
91 bytes there are in the last chunk */
92 write_int(f_out,s?s->count:0);
93 write_int(f_out,s?s->n:block_size);
94 write_int(f_out,s?s->remainder:0);
96 for (i=0;i<s->count;i++) {
97 write_int(f_out,s->sums[i].sum1);
98 write_buf(f_out,s->sums[i].sum2,csum_length);
104 generate a stream of signatures/checksums that describe a buffer
106 generate approximately one checksum every n bytes
108 static struct sum_struct *generate_sums(struct map_struct *buf,OFF_T len,int n)
111 struct sum_struct *s;
114 int remainder = (len%block_len);
117 count = (len+(block_len-1))/block_len;
119 s = (struct sum_struct *)malloc(sizeof(*s));
120 if (!s) out_of_memory("generate_sums");
123 s->remainder = remainder;
133 rprintf(FINFO,"count=%d rem=%d n=%d flength=%d\n",
134 s->count,s->remainder,s->n,(int)s->flength);
136 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
137 if (!s->sums) out_of_memory("generate_sums");
139 for (i=0;i<count;i++) {
141 char *map = map_ptr(buf,offset,n1);
143 s->sums[i].sum1 = get_checksum1(map,n1);
144 get_checksum2(map,n1,s->sums[i].sum2);
146 s->sums[i].offset = offset;
151 rprintf(FINFO,"chunk[%d] offset=%d len=%d sum1=%08x\n",
152 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
162 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
166 struct map_struct *buf;
167 struct sum_struct *s;
169 struct file_struct *file = flist->files[i];
171 char fnamecmpbuf[MAXPATHLEN];
172 extern char *compare_dest;
173 extern int list_only;
175 if (list_only) return;
178 rprintf(FINFO,"recv_generator(%s,%d)\n",fname,i);
180 statret = link_stat(fname,&st);
182 if (S_ISDIR(file->mode)) {
184 if (statret == 0 && !S_ISDIR(st.st_mode)) {
185 if (robust_unlink(fname) != 0) {
186 rprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
191 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
192 if (!(relative_paths && errno==ENOENT &&
193 create_directory_path(fname)==0 &&
194 do_mkdir(fname,file->mode)==0)) {
195 rprintf(FERROR,"mkdir %s : %s (2)\n",
196 fname,strerror(errno));
199 if (set_perms(fname,file,NULL,0) && verbose)
200 rprintf(FINFO,"%s/\n",fname);
204 if (preserve_links && S_ISLNK(file->mode)) {
206 char lnk[MAXPATHLEN];
208 extern int safe_symlinks;
210 if (safe_symlinks && unsafe_symlink(file->link, fname)) {
212 rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n",
218 l = readlink(fname,lnk,MAXPATHLEN-1);
221 if (strcmp(lnk,file->link) == 0) {
222 set_perms(fname,file,&st,1);
228 if (do_symlink(file->link,fname) != 0) {
229 rprintf(FERROR,"link %s -> %s : %s\n",
230 fname,file->link,strerror(errno));
232 set_perms(fname,file,NULL,0);
234 rprintf(FINFO,"%s -> %s\n",
243 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
245 st.st_mode != file->mode ||
246 st.st_rdev != file->rdev) {
249 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
250 fname,(int)file->mode,(int)file->rdev);
251 if (do_mknod(fname,file->mode,file->rdev) != 0) {
252 rprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
254 set_perms(fname,file,NULL,0);
256 rprintf(FINFO,"%s\n",fname);
259 set_perms(fname,file,&st,1);
265 if (preserve_hard_links && check_hard_link(file)) {
267 rprintf(FINFO,"%s is a hard link\n",f_name(file));
271 if (!S_ISREG(file->mode)) {
272 rprintf(FINFO,"skipping non-regular file %s\n",fname);
278 if ((statret == -1) && (compare_dest != NULL)) {
279 /* try the file at compare_dest instead */
280 int saveerrno = errno;
281 slprintf(fnamecmpbuf,MAXPATHLEN,"%s/%s",compare_dest,fname);
282 statret = link_stat(fnamecmpbuf,&st);
283 if (!S_ISREG(st.st_mode))
288 fnamecmp = fnamecmpbuf;
292 if (errno == ENOENT) {
294 if (!dry_run) send_sums(NULL,f_out);
297 rprintf(FERROR,"recv_generator failed to open %s\n",fname);
302 if (!S_ISREG(st.st_mode)) {
303 if (delete_file(fname) != 0) {
307 /* now pretend the file didn't exist */
309 if (!dry_run) send_sums(NULL,f_out);
313 if (update_only && st.st_mtime > file->modtime && fnamecmp == fname) {
315 rprintf(FINFO,"%s is newer\n",fname);
319 if (skip_file(fname, file, &st)) {
320 set_perms(fname,file,&st,1);
331 send_sums(NULL,f_out);
336 fd = open(fnamecmp,O_RDONLY);
339 rprintf(FERROR,"failed to open %s : %s\n",fnamecmp,strerror(errno));
340 rprintf(FERROR,"skipping %s\n",fname);
344 if (st.st_size > 0) {
345 buf = map_file(fd,st.st_size);
351 rprintf(FINFO,"gen mapped %s of size %d\n",fnamecmp,(int)st.st_size);
353 s = generate_sums(buf,st.st_size,adapt_block_size(file, block_size));
356 rprintf(FINFO,"sending sums for %d\n",i);
362 if (buf) unmap_file(buf);
369 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
375 rprintf(FINFO,"generator starting pid=%d count=%d\n",
376 (int)getpid(),flist->count);
378 for (i = 0; i < flist->count; i++) {
379 struct file_struct *file = flist->files[i];
380 mode_t saved_mode = file->mode;
381 if (!file->basename) continue;
383 /* we need to ensure that any directories we create have writeable
384 permissions initially so that we can create the files within
385 them. This is then fixed after the files are transferred */
386 if (!am_root && S_ISDIR(file->mode)) {
387 file->mode |= S_IWUSR; /* user write */
390 recv_generator(local_name?local_name:f_name(file),
393 file->mode = saved_mode;
397 csum_length = SUM_LENGTH;
401 rprintf(FINFO,"generate_files phase=%d\n",phase);
405 /* we expect to just sit around now, so don't exit on a
406 timeout. If we really get a timeout then the other process should
410 if (remote_version >= 13) {
411 /* in newer versions of the protocol the files can cycle through
412 the system more than once to catch initial checksum errors */
413 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
414 struct file_struct *file = flist->files[i];
415 recv_generator(local_name?local_name:f_name(file),
421 rprintf(FINFO,"generate_files phase=%d\n",phase);