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