We now look for the ITEM_USING_ALT_BASIS flag for an --inplace
[rsync/rsync.git] / sender.c
... / ...
CommitLineData
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#include "rsync.h"
21
22extern int verbose;
23extern int dry_run;
24extern int am_server;
25extern int am_daemon;
26extern int log_before_transfer;
27extern int log_format_has_i;
28extern int daemon_log_format_has_i;
29extern int csum_length;
30extern int io_error;
31extern int protocol_version;
32extern int remove_sent_files;
33extern int updating_basis_file;
34extern int make_backups;
35extern int do_progress;
36extern int inplace;
37extern struct stats stats;
38extern char *log_format;
39
40
41/**
42 * @file
43 *
44 * The sender gets checksums from the generator, calculates deltas,
45 * and transmits them to the receiver. The sender process runs on the
46 * machine holding the source files.
47 **/
48
49/**
50 * Receive the checksums for a buffer
51 **/
52static struct sum_struct *receive_sums(int f)
53{
54 struct sum_struct *s;
55 int32 i;
56 OFF_T offset = 0;
57
58 if (!(s = new(struct sum_struct)))
59 out_of_memory("receive_sums");
60
61 read_sum_head(f, s);
62
63 s->sums = NULL;
64
65 if (verbose > 3) {
66 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
67 (double)s->count, (long)s->blength, (long)s->remainder);
68 }
69
70 if (s->count == 0)
71 return(s);
72
73 if (!(s->sums = new_array(struct sum_buf, s->count)))
74 out_of_memory("receive_sums");
75
76 for (i = 0; i < s->count; i++) {
77 s->sums[i].sum1 = read_int(f);
78 read_buf(f, s->sums[i].sum2, s->s2length);
79
80 s->sums[i].offset = offset;
81 s->sums[i].flags = 0;
82
83 if (i == s->count-1 && s->remainder != 0)
84 s->sums[i].len = s->remainder;
85 else
86 s->sums[i].len = s->blength;
87 offset += s->sums[i].len;
88
89 if (verbose > 3) {
90 rprintf(FINFO,
91 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
92 i, s->sums[i].len, (double)s->sums[i].offset,
93 s->sums[i].sum1);
94 }
95 }
96
97 s->flength = offset;
98
99 return s;
100}
101
102static struct file_list *the_flist;
103
104void successful_send(int i)
105{
106 char fname[MAXPATHLEN];
107 struct file_struct *file;
108 unsigned int offset;
109
110 if (!the_flist || i < 0 || i >= the_flist->count)
111 return;
112
113 file = the_flist->files[i];
114 /* The generator might tell us about symlinks we didn't send. */
115 if (!(file->flags & FLAG_SENT) && !S_ISLNK(file->mode))
116 return;
117 if (file->dir.root) {
118 offset = stringjoin(fname, sizeof fname,
119 file->dir.root, "/", NULL);
120 } else
121 offset = 0;
122 f_name_to(file, fname + offset);
123 if (remove_sent_files && do_unlink(fname) == 0 && verbose > 1) {
124 rprintf(FINFO, "sender removed %s\n",
125 safe_fname(fname + offset));
126 }
127}
128
129void send_files(struct file_list *flist, int f_out, int f_in)
130{
131 int fd = -1;
132 struct sum_struct *s;
133 struct map_struct *mbuf = NULL;
134 STRUCT_STAT st;
135 char *fname2, fname[MAXPATHLEN];
136 int iflags;
137 struct file_struct *file;
138 int phase = 0;
139 struct stats initial_stats;
140 int save_make_backups = make_backups;
141 int itemizing = am_daemon ? daemon_log_format_has_i
142 : !am_server && log_format_has_i;
143 int i, j;
144
145 if (verbose > 2)
146 rprintf(FINFO, "send_files starting\n");
147
148 the_flist = flist;
149
150 while (1) {
151 unsigned int offset;
152
153 i = read_int(f_in);
154 if (i == -1) {
155 if (phase == 0) {
156 phase++;
157 csum_length = SUM_LENGTH;
158 write_int(f_out, -1);
159 if (verbose > 2)
160 rprintf(FINFO, "send_files phase=%d\n", phase);
161 /* For inplace: redo phase turns off the backup
162 * flag so that we do a regular inplace send. */
163 make_backups = 0;
164 continue;
165 }
166 break;
167 }
168
169 if (i < 0 || i >= flist->count) {
170 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
171 i, flist->count);
172 exit_cleanup(RERR_PROTOCOL);
173 }
174
175 file = flist->files[i];
176 if (file->dir.root) {
177 /* N.B. We're sure that this fits, so offset is OK. */
178 offset = strlcpy(fname, file->dir.root, sizeof fname);
179 if (!offset || fname[offset-1] != '/')
180 fname[offset++] = '/';
181 } else
182 offset = 0;
183 fname2 = f_name_to(file, fname + offset);
184
185 if (verbose > 2)
186 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
187
188 if (protocol_version >= 29) {
189 iflags = read_shortint(f_in);
190 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
191 int see_item = itemizing && (iflags || verbose > 1);
192 write_int(f_out, i);
193 write_shortint(f_out, iflags);
194 if (am_server) {
195 if (am_daemon && !dry_run && see_item)
196 log_send(file, &stats, iflags);
197 } else if (see_item || iflags & ITEM_UPDATING
198 || (S_ISDIR(file->mode)
199 && iflags & ITEM_REPORT_TIME))
200 log_send(file, &stats, iflags);
201 continue;
202 }
203 } else
204 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
205
206 if (inplace && protocol_version >= 29) {
207 updating_basis_file = iflags & ITEM_USING_ALT_BASIS;
208 } else
209 updating_basis_file = inplace && !make_backups;
210
211 if (!S_ISREG(file->mode)) {
212 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
213 who_am_i(), i);
214 exit_cleanup(RERR_PROTOCOL);
215 }
216
217 stats.current_file_index = i;
218 stats.num_transferred_files++;
219 stats.total_transferred_size += file->length;
220
221 if (dry_run) { /* log the transfer */
222 if (!am_server && log_format)
223 log_send(file, &stats, iflags);
224 write_int(f_out, i);
225 if (protocol_version >= 29)
226 write_shortint(f_out, iflags);
227 continue;
228 }
229
230 initial_stats = stats;
231
232 if (!(s = receive_sums(f_in))) {
233 io_error |= IOERR_GENERAL;
234 rprintf(FERROR, "receive_sums failed\n");
235 return;
236 }
237
238 fd = do_open(fname, O_RDONLY, 0);
239 if (fd == -1) {
240 if (errno == ENOENT) {
241 enum logcode c = am_daemon
242 && protocol_version < 28 ? FERROR
243 : FINFO;
244 io_error |= IOERR_VANISHED;
245 rprintf(c, "file has vanished: %s\n",
246 full_fname(fname));
247 } else {
248 io_error |= IOERR_GENERAL;
249 rsyserr(FERROR, errno,
250 "send_files failed to open %s",
251 full_fname(fname));
252 }
253 free_sums(s);
254 continue;
255 }
256
257 /* map the local file */
258 if (do_fstat(fd, &st) != 0) {
259 io_error |= IOERR_GENERAL;
260 rsyserr(FERROR, errno, "fstat failed");
261 free_sums(s);
262 close(fd);
263 return;
264 }
265
266 if (st.st_size) {
267 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
268 mbuf = map_file(fd, st.st_size, read_size, s->blength);
269 } else
270 mbuf = NULL;
271
272 if (verbose > 2) {
273 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
274 safe_fname(fname), (double)st.st_size);
275 }
276
277 write_int(f_out, i);
278 if (protocol_version >= 29)
279 write_shortint(f_out, iflags);
280 write_sum_head(f_out, s);
281
282 if (verbose > 2) {
283 rprintf(FINFO, "calling match_sums %s\n",
284 safe_fname(fname));
285 }
286
287 if (log_before_transfer)
288 log_send(file, &initial_stats, iflags);
289 else if (!am_server && verbose && do_progress)
290 rprintf(FINFO, "%s\n", safe_fname(fname2));
291
292 set_compression(fname);
293
294 match_sums(f_out, s, mbuf, st.st_size);
295 if (!log_before_transfer)
296 log_send(file, &initial_stats, iflags);
297
298 if (mbuf) {
299 j = unmap_file(mbuf);
300 if (j) {
301 io_error |= IOERR_GENERAL;
302 rsyserr(FERROR, j,
303 "read errors mapping %s",
304 full_fname(fname));
305 }
306 }
307 close(fd);
308
309 free_sums(s);
310
311 if (verbose > 2) {
312 rprintf(FINFO, "sender finished %s\n",
313 safe_fname(fname));
314 }
315
316 /* Flag that we actually sent this entry. */
317 file->flags |= FLAG_SENT;
318 }
319 make_backups = save_make_backups;
320
321 if (verbose > 2)
322 rprintf(FINFO, "send files finished\n");
323
324 match_report();
325
326 write_int(f_out, -1);
327}