Mention the change in restrictions for --inplace. Also talk about
[rsync/rsync.git] / sender.c
CommitLineData
e6e3f12f 1/*
2f03f956
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
e6e3f12f 4
2f03f956
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.
e6e3f12f 9
2f03f956
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.
e6e3f12f 14
2f03f956
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
20#include "rsync.h"
21
22extern int verbose;
2f03f956
AT
23extern int csum_length;
24extern struct stats stats;
25extern int io_error;
26extern int dry_run;
27extern int am_server;
d6631cf3
WD
28extern int am_daemon;
29extern int protocol_version;
8b115ac8 30extern int make_backups;
a3221d2a 31extern struct stats stats;
2f03f956
AT
32
33
0f9c48b1
MP
34/**
35 * @file
36 *
37 * The sender gets checksums from the generator, calculates deltas,
38 * and transmits them to the receiver. The sender process runs on the
39 * machine holding the source files.
40 **/
fc0257c9 41
ce8149b6
MP
42/**
43 * Receive the checksums for a buffer
44 **/
2f03f956
AT
45static struct sum_struct *receive_sums(int f)
46{
47 struct sum_struct *s;
48 int i;
49 OFF_T offset = 0;
50
a3221d2a
WD
51 if (!(s = new(struct sum_struct)))
52 out_of_memory("receive_sums");
2f03f956 53
fc0257c9
S
54 read_sum_head(f, s);
55
2f03f956
AT
56 s->sums = NULL;
57
da9d12f5 58 if (verbose > 3) {
6c495e0d
WD
59 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
60 (double)s->count, (long)s->blength, (long)s->remainder);
da9d12f5 61 }
2f03f956 62
e6e3f12f 63 if (s->count == 0)
2f03f956
AT
64 return(s);
65
a3221d2a
WD
66 if (!(s->sums = new_array(struct sum_buf, s->count)))
67 out_of_memory("receive_sums");
2f03f956 68
a3221d2a 69 for (i = 0; i < (int)s->count; i++) {
2f03f956 70 s->sums[i].sum1 = read_int(f);
e6e3f12f 71 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
72
73 s->sums[i].offset = offset;
a3221d2a 74 s->sums[i].flags = 0;
2f03f956 75
a3221d2a 76 if (i == (int)s->count-1 && s->remainder != 0)
2f03f956 77 s->sums[i].len = s->remainder;
a3221d2a 78 else
fc0257c9 79 s->sums[i].len = s->blength;
2f03f956
AT
80 offset += s->sums[i].len;
81
a3221d2a
WD
82 if (verbose > 3) {
83 rprintf(FINFO,
84 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
85 i, s->sums[i].len, (double)s->sums[i].offset,
86 s->sums[i].sum1);
87 }
2f03f956
AT
88 }
89
90 s->flength = offset;
91
92 return s;
93}
94
95
96
e6e3f12f
S
97void send_files(struct file_list *flist, int f_out, int f_in)
98{
c1659c79 99 int fd = -1;
2f03f956 100 struct sum_struct *s;
41cc97ae 101 struct map_struct *mbuf = NULL;
2f03f956 102 STRUCT_STAT st;
ecc81fce 103 char *fname2, fname[MAXPATHLEN];
2f03f956
AT
104 int i;
105 struct file_struct *file;
106 int phase = 0;
1b7c47cb 107 struct stats initial_stats;
8b115ac8 108 int save_make_backups = make_backups;
64c3523a 109 int j;
2f03f956
AT
110
111 if (verbose > 2)
e6e3f12f 112 rprintf(FINFO, "send_files starting\n");
2f03f956 113
2f03f956 114 while (1) {
3fef5364 115 unsigned int offset;
2f03f956
AT
116
117 i = read_int(f_in);
118 if (i == -1) {
e6e3f12f 119 if (phase == 0) {
2f03f956
AT
120 phase++;
121 csum_length = SUM_LENGTH;
e6e3f12f 122 write_int(f_out, -1);
2f03f956 123 if (verbose > 2)
e6e3f12f 124 rprintf(FINFO, "send_files phase=%d\n", phase);
9715c589
WD
125 /* For inplace: redo phase turns off the backup
126 * flag so that we do a regular inplace send. */
8b115ac8 127 make_backups = 0;
2f03f956
AT
128 continue;
129 }
130 break;
131 }
132
133 if (i < 0 || i >= flist->count) {
e6e3f12f 134 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
2f03f956 135 i, flist->count);
65417579 136 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
137 }
138
139 file = flist->files[i];
140
97feb557 141 stats.current_file_index = i;
2f03f956
AT
142 stats.num_transferred_files++;
143 stats.total_transferred_size += file->length;
144
2f03f956 145 if (file->basedir) {
421c2a24
WD
146 /* N.B. We're sure that this fits, so offset is OK. */
147 offset = strlcpy(fname, file->basedir, sizeof fname);
148 if (!offset || fname[offset-1] != '/')
149 fname[offset++] = '/';
3fef5364
WD
150 } else
151 offset = 0;
ecc81fce 152 fname2 = f_name_to(file, fname + offset);
e6e3f12f
S
153
154 if (verbose > 2)
155 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
156
157 if (dry_run) {
ecc81fce
WD
158 if (!am_server && verbose) /* log the transfer */
159 rprintf(FINFO, "%s\n", safe_fname(fname2));
e6e3f12f 160 write_int(f_out, i);
2f03f956
AT
161 continue;
162 }
163
1b7c47cb
AT
164 initial_stats = stats;
165
efa95a18 166 if (!(s = receive_sums(f_in))) {
06c28400 167 io_error |= IOERR_GENERAL;
e6e3f12f 168 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
169 return;
170 }
76f79ba7 171
b9f592fb
WD
172 fd = do_open(fname, O_RDONLY, 0);
173 if (fd == -1) {
174 if (errno == ENOENT) {
175 enum logcode c = am_daemon
176 && protocol_version < 28 ? FERROR
177 : FINFO;
178 io_error |= IOERR_VANISHED;
179 rprintf(c, "file has vanished: %s\n",
180 full_fname(fname));
181 } else {
06c28400 182 io_error |= IOERR_GENERAL;
b9f592fb
WD
183 rsyserr(FERROR, errno,
184 "send_files failed to open %s",
185 full_fname(fname));
41cc97ae 186 }
b9f592fb
WD
187 free_sums(s);
188 continue;
189 }
e6e3f12f 190
b9f592fb
WD
191 /* map the local file */
192 if (do_fstat(fd, &st) != 0) {
193 io_error |= IOERR_GENERAL;
194 rsyserr(FERROR, errno, "fstat failed");
195 free_sums(s);
196 close(fd);
197 return;
198 }
11a5a3c7 199
96d910c7 200 if (st.st_size) {
9b919d59
WD
201 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
202 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
203 } else
204 mbuf = NULL;
6902ed17 205
b9f592fb
WD
206 if (verbose > 2) {
207 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
ecc81fce 208 safe_fname(fname), (double)st.st_size);
6902ed17 209 }
e6e3f12f 210
b9f592fb
WD
211 write_int(f_out, i);
212 write_sum_head(f_out, s);
213
ecc81fce
WD
214 if (verbose > 2) {
215 rprintf(FINFO, "calling match_sums %s\n",
216 safe_fname(fname));
11a5a3c7 217 }
83fff1aa 218
ecc81fce
WD
219 if (!am_server && verbose) /* log the transfer */
220 rprintf(FINFO, "%s\n", safe_fname(fname2));
221
83fff1aa 222 set_compression(fname);
1b7c47cb 223
b9f592fb
WD
224 match_sums(f_out, s, mbuf, st.st_size);
225 log_send(file, &initial_stats);
6902ed17 226
b9f592fb
WD
227 if (mbuf) {
228 j = unmap_file(mbuf);
229 if (j) {
230 io_error |= IOERR_GENERAL;
231 rsyserr(FERROR, j,
232 "read errors mapping %s",
233 full_fname(fname));
6a7cc46c 234 }
6902ed17 235 }
b9f592fb 236 close(fd);
e6e3f12f 237
2f03f956 238 free_sums(s);
e6e3f12f 239
ecc81fce
WD
240 if (verbose > 2) {
241 rprintf(FINFO, "sender finished %s\n",
242 safe_fname(fname));
243 }
2f03f956 244 }
8b115ac8 245 make_backups = save_make_backups;
2f03f956
AT
246
247 if (verbose > 2)
e6e3f12f 248 rprintf(FINFO, "send files finished\n");
2f03f956
AT
249
250 match_report();
251
e6e3f12f 252 write_int(f_out, -1);
2f03f956 253}