Use the latest F_*() accessors.
[rsync/rsync.git] / sender.c
CommitLineData
e6e3f12f 1/*
0f78b815
WD
2 * Routines only used by the sending process.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
e7c67065
WD
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 21 */
2f03f956
AT
22
23#include "rsync.h"
24
25extern int verbose;
a0545709 26extern int do_xfers;
8715db2c
WD
27extern int am_server;
28extern int am_daemon;
8a9cfb24 29extern int log_before_transfer;
2fedf3d5 30extern int stdout_format_has_i;
ecc7623e 31extern int logfile_format_has_i;
2f03f956 32extern int csum_length;
6cc11982 33extern int append_mode;
2f03f956 34extern int io_error;
605fed4b 35extern int allowed_lull;
d6631cf3 36extern int protocol_version;
47c11975 37extern int remove_source_files;
53f8519a 38extern int updating_basis_file;
8b115ac8 39extern int make_backups;
8a9cfb24 40extern int do_progress;
53f8519a 41extern int inplace;
a0545709
WD
42extern int batch_fd;
43extern int write_batch;
a3221d2a 44extern struct stats stats;
5f40615c 45extern struct file_list *the_file_list;
2fedf3d5 46extern char *stdout_format;
2f03f956
AT
47
48
0f9c48b1
MP
49/**
50 * @file
51 *
52 * The sender gets checksums from the generator, calculates deltas,
53 * and transmits them to the receiver. The sender process runs on the
54 * machine holding the source files.
55 **/
fc0257c9 56
ce8149b6
MP
57/**
58 * Receive the checksums for a buffer
59 **/
2f03f956
AT
60static struct sum_struct *receive_sums(int f)
61{
62 struct sum_struct *s;
a1cbe76e 63 int32 i;
605fed4b 64 int lull_mod = allowed_lull * 5;
2f03f956
AT
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 74 if (verbose > 3) {
6c495e0d
WD
75 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
76 (double)s->count, (long)s->blength, (long)s->remainder);
da9d12f5 77 }
2f03f956 78
6cc11982
WD
79 if (append_mode) {
80 s->flength = (OFF_T)s->count * s->blength;
81 if (s->remainder)
82 s->flength -= s->blength - s->remainder;
83 return s;
84 }
85
e6e3f12f 86 if (s->count == 0)
2f03f956
AT
87 return(s);
88
a3221d2a
WD
89 if (!(s->sums = new_array(struct sum_buf, s->count)))
90 out_of_memory("receive_sums");
2f03f956 91
eed47b6b 92 for (i = 0; i < s->count; i++) {
2f03f956 93 s->sums[i].sum1 = read_int(f);
e6e3f12f 94 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
95
96 s->sums[i].offset = offset;
a3221d2a 97 s->sums[i].flags = 0;
2f03f956 98
eed47b6b 99 if (i == s->count-1 && s->remainder != 0)
2f03f956 100 s->sums[i].len = s->remainder;
a3221d2a 101 else
fc0257c9 102 s->sums[i].len = s->blength;
2f03f956
AT
103 offset += s->sums[i].len;
104
605fed4b
WD
105 if (allowed_lull && !(i % lull_mod))
106 maybe_send_keepalive();
107
a3221d2a
WD
108 if (verbose > 3) {
109 rprintf(FINFO,
110 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
111 i, s->sums[i].len, (double)s->sums[i].offset,
112 s->sums[i].sum1);
113 }
2f03f956
AT
114 }
115
116 s->flength = offset;
117
118 return s;
119}
120
5f40615c 121void successful_send(int ndx)
95306d9d
WD
122{
123 char fname[MAXPATHLEN];
124 struct file_struct *file;
125 unsigned int offset;
126
5f40615c 127 if (ndx < 0 || ndx >= the_file_list->count)
95306d9d
WD
128 return;
129
5f40615c 130 file = the_file_list->files[ndx];
95306d9d
WD
131 if (file->dir.root) {
132 offset = stringjoin(fname, sizeof fname,
133 file->dir.root, "/", NULL);
134 } else
135 offset = 0;
6cbde57d 136 f_name(file, fname + offset);
acee1ad8
WD
137 if (remove_source_files) {
138 if (do_unlink(fname) == 0) {
139 if (verbose > 1)
140 rprintf(FINFO, "sender removed %s\n", fname + offset);
141 } else
142 rsyserr(FERROR, errno, "sender failed to remove %s", fname + offset);
143 }
95306d9d 144}
2f03f956 145
d144e43b
WD
146void write_ndx_and_attrs(int f_out, int ndx, int iflags,
147 uchar fnamecmp_type, char *buf, int len)
148{
149 write_int(f_out, ndx);
150 if (protocol_version < 29)
151 return;
152 write_shortint(f_out, iflags);
153 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
154 write_byte(f_out, fnamecmp_type);
155 if (iflags & ITEM_XNAME_FOLLOWS)
156 write_vstring(f_out, buf, len);
157}
158
e6e3f12f
S
159void send_files(struct file_list *flist, int f_out, int f_in)
160{
c1659c79 161 int fd = -1;
2f03f956 162 struct sum_struct *s;
41cc97ae 163 struct map_struct *mbuf = NULL;
2f03f956 164 STRUCT_STAT st;
ecc81fce 165 char *fname2, fname[MAXPATHLEN];
6087ef2a 166 char xname[MAXPATHLEN];
4d53c4dd 167 uchar fnamecmp_type;
6087ef2a 168 int iflags, xlen;
2f03f956 169 struct file_struct *file;
11290705 170 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
1b7c47cb 171 struct stats initial_stats;
8b115ac8 172 int save_make_backups = make_backups;
2fedf3d5
WD
173 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
174 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
a0545709 175 int f_xfer = write_batch < 0 ? batch_fd : f_out;
8a9cfb24 176 int i, j;
2f03f956
AT
177
178 if (verbose > 2)
e6e3f12f 179 rprintf(FINFO, "send_files starting\n");
2f03f956 180
2f03f956 181 while (1) {
3fef5364 182 unsigned int offset;
2f03f956
AT
183
184 i = read_int(f_in);
82ad07c4 185 if (i == NDX_DONE) {
11290705 186 if (++phase > max_phase)
8b48bf11 187 break;
8b48bf11
WD
188 csum_length = SUM_LENGTH;
189 if (verbose > 2)
190 rprintf(FINFO, "send_files phase=%d\n", phase);
82ad07c4 191 write_int(f_out, NDX_DONE);
8b48bf11
WD
192 /* For inplace: redo phase turns off the backup
193 * flag so that we do a regular inplace send. */
194 make_backups = 0;
6cc11982 195 append_mode = 0;
8b48bf11 196 continue;
2f03f956
AT
197 }
198
e732fb0c 199 iflags = read_item_attrs(f_in, f_out, i, &fnamecmp_type,
6087ef2a 200 xname, &xlen);
165e6d44
WD
201 if (iflags == ITEM_IS_NEW) /* no-op packet */
202 continue;
2f03f956 203
8a9cfb24 204 file = flist->files[i];
7f37167d
WD
205 if (file->dir.root) {
206 /* N.B. We're sure that this fits, so offset is OK. */
207 offset = strlcpy(fname, file->dir.root, sizeof fname);
208 if (!offset || fname[offset-1] != '/')
209 fname[offset++] = '/';
210 } else
211 offset = 0;
6cbde57d 212 fname2 = f_name(file, fname + offset);
7f37167d
WD
213
214 if (verbose > 2)
215 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
8a9cfb24 216
fad3dc42 217 if (!(iflags & ITEM_TRANSFER)) {
6087ef2a 218 maybe_log_item(file, iflags, itemizing, xname);
165e6d44
WD
219 continue;
220 }
11290705
WD
221 if (phase == 2) {
222 rprintf(FERROR,
223 "got transfer request in phase 2 [%s]\n",
224 who_am_i());
225 exit_cleanup(RERR_PROTOCOL);
226 }
8a9cfb24 227
6087ef2a
WD
228 updating_basis_file = inplace && (protocol_version >= 29
229 ? fnamecmp_type == FNAMECMP_FNAME : !make_backups);
b951e023 230
97feb557 231 stats.current_file_index = i;
2f03f956 232 stats.num_transferred_files++;
112d728f 233 stats.total_transferred_size += F_LENGTH(file);
2f03f956 234
a0545709 235 if (!do_xfers) { /* log the transfer */
85909931 236 log_item(FCLIENT, file, &stats, iflags, NULL);
e732fb0c 237 write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type,
927c8068 238 xname, xlen);
2f03f956
AT
239 continue;
240 }
241
1b7c47cb
AT
242 initial_stats = stats;
243
efa95a18 244 if (!(s = receive_sums(f_in))) {
06c28400 245 io_error |= IOERR_GENERAL;
e6e3f12f 246 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
247 return;
248 }
76f79ba7 249
b9f592fb
WD
250 fd = do_open(fname, O_RDONLY, 0);
251 if (fd == -1) {
252 if (errno == ENOENT) {
253 enum logcode c = am_daemon
254 && protocol_version < 28 ? FERROR
255 : FINFO;
256 io_error |= IOERR_VANISHED;
257 rprintf(c, "file has vanished: %s\n",
258 full_fname(fname));
259 } else {
06c28400 260 io_error |= IOERR_GENERAL;
b9f592fb
WD
261 rsyserr(FERROR, errno,
262 "send_files failed to open %s",
263 full_fname(fname));
41cc97ae 264 }
b9f592fb
WD
265 free_sums(s);
266 continue;
267 }
e6e3f12f 268
b9f592fb
WD
269 /* map the local file */
270 if (do_fstat(fd, &st) != 0) {
271 io_error |= IOERR_GENERAL;
272 rsyserr(FERROR, errno, "fstat failed");
273 free_sums(s);
274 close(fd);
275 return;
276 }
11a5a3c7 277
96d910c7 278 if (st.st_size) {
9b919d59
WD
279 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
280 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
281 } else
282 mbuf = NULL;
6902ed17 283
b9f592fb
WD
284 if (verbose > 2) {
285 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
45c49b52 286 fname, (double)st.st_size);
6902ed17 287 }
e6e3f12f 288
e732fb0c 289 write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type,
927c8068 290 xname, xlen);
a0545709 291 write_sum_head(f_xfer, s);
b9f592fb 292
45c49b52
WD
293 if (verbose > 2)
294 rprintf(FINFO, "calling match_sums %s\n", fname);
83fff1aa 295
8a9cfb24 296 if (log_before_transfer)
85909931 297 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
8a832465 298 else if (!am_server && verbose && do_progress)
85909931 299 rprintf(FCLIENT, "%s\n", fname2);
ecc81fce 300
83fff1aa 301 set_compression(fname);
1b7c47cb 302
a0545709 303 match_sums(f_xfer, s, mbuf, st.st_size);
0394e34a
WD
304 if (do_progress)
305 end_progress(st.st_size);
306
2fedf3d5 307 log_item(log_code, file, &initial_stats, iflags, NULL);
6902ed17 308
b9f592fb
WD
309 if (mbuf) {
310 j = unmap_file(mbuf);
311 if (j) {
312 io_error |= IOERR_GENERAL;
313 rsyserr(FERROR, j,
314 "read errors mapping %s",
315 full_fname(fname));
6a7cc46c 316 }
6902ed17 317 }
b9f592fb 318 close(fd);
e6e3f12f 319
2f03f956 320 free_sums(s);
e6e3f12f 321
45c49b52
WD
322 if (verbose > 2)
323 rprintf(FINFO, "sender finished %s\n", fname);
95306d9d
WD
324
325 /* Flag that we actually sent this entry. */
326 file->flags |= FLAG_SENT;
2f03f956 327 }
8b115ac8 328 make_backups = save_make_backups;
2f03f956
AT
329
330 if (verbose > 2)
e6e3f12f 331 rprintf(FINFO, "send files finished\n");
2f03f956
AT
332
333 match_report();
334
82ad07c4 335 write_int(f_out, NDX_DONE);
2f03f956 336}