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