Moved write_ndx_and_attrs() to sender.c and made it public.
[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
e6e3f12f
S
146void send_files(struct file_list *flist, int f_out, int f_in)
147{
c1659c79 148 int fd = -1;
2f03f956 149 struct sum_struct *s;
41cc97ae 150 struct map_struct *mbuf = NULL;
2f03f956 151 STRUCT_STAT st;
ecc81fce 152 char *fname2, fname[MAXPATHLEN];
6087ef2a 153 char xname[MAXPATHLEN];
4d53c4dd 154 uchar fnamecmp_type;
6087ef2a 155 int iflags, xlen;
2f03f956 156 struct file_struct *file;
11290705 157 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
1b7c47cb 158 struct stats initial_stats;
8b115ac8 159 int save_make_backups = make_backups;
2fedf3d5
WD
160 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
161 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
a0545709 162 int f_xfer = write_batch < 0 ? batch_fd : f_out;
8a9cfb24 163 int i, j;
2f03f956
AT
164
165 if (verbose > 2)
e6e3f12f 166 rprintf(FINFO, "send_files starting\n");
2f03f956 167
2f03f956 168 while (1) {
3fef5364 169 unsigned int offset;
2f03f956
AT
170
171 i = read_int(f_in);
172 if (i == -1) {
11290705 173 if (++phase > max_phase)
8b48bf11 174 break;
8b48bf11
WD
175 csum_length = SUM_LENGTH;
176 if (verbose > 2)
177 rprintf(FINFO, "send_files phase=%d\n", phase);
178 write_int(f_out, -1);
179 /* For inplace: redo phase turns off the backup
180 * flag so that we do a regular inplace send. */
181 make_backups = 0;
6cc11982 182 append_mode = 0;
8b48bf11 183 continue;
2f03f956
AT
184 }
185
e732fb0c 186 iflags = read_item_attrs(f_in, f_out, i, &fnamecmp_type,
6087ef2a 187 xname, &xlen);
165e6d44
WD
188 if (iflags == ITEM_IS_NEW) /* no-op packet */
189 continue;
2f03f956 190
8a9cfb24 191 file = flist->files[i];
7f37167d
WD
192 if (file->dir.root) {
193 /* N.B. We're sure that this fits, so offset is OK. */
194 offset = strlcpy(fname, file->dir.root, sizeof fname);
195 if (!offset || fname[offset-1] != '/')
196 fname[offset++] = '/';
197 } else
198 offset = 0;
6cbde57d 199 fname2 = f_name(file, fname + offset);
7f37167d
WD
200
201 if (verbose > 2)
202 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
8a9cfb24 203
fad3dc42 204 if (!(iflags & ITEM_TRANSFER)) {
6087ef2a 205 maybe_log_item(file, iflags, itemizing, xname);
165e6d44
WD
206 continue;
207 }
11290705
WD
208 if (phase == 2) {
209 rprintf(FERROR,
210 "got transfer request in phase 2 [%s]\n",
211 who_am_i());
212 exit_cleanup(RERR_PROTOCOL);
213 }
8a9cfb24 214
6087ef2a
WD
215 updating_basis_file = inplace && (protocol_version >= 29
216 ? fnamecmp_type == FNAMECMP_FNAME : !make_backups);
b951e023 217
97feb557 218 stats.current_file_index = i;
2f03f956
AT
219 stats.num_transferred_files++;
220 stats.total_transferred_size += file->length;
221
a0545709 222 if (!do_xfers) { /* log the transfer */
85909931 223 log_item(FCLIENT, file, &stats, iflags, NULL);
e732fb0c 224 write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type,
927c8068 225 xname, xlen);
2f03f956
AT
226 continue;
227 }
228
1b7c47cb
AT
229 initial_stats = stats;
230
efa95a18 231 if (!(s = receive_sums(f_in))) {
06c28400 232 io_error |= IOERR_GENERAL;
e6e3f12f 233 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
234 return;
235 }
76f79ba7 236
b9f592fb
WD
237 fd = do_open(fname, O_RDONLY, 0);
238 if (fd == -1) {
239 if (errno == ENOENT) {
240 enum logcode c = am_daemon
241 && protocol_version < 28 ? FERROR
242 : FINFO;
243 io_error |= IOERR_VANISHED;
244 rprintf(c, "file has vanished: %s\n",
245 full_fname(fname));
246 } else {
06c28400 247 io_error |= IOERR_GENERAL;
b9f592fb
WD
248 rsyserr(FERROR, errno,
249 "send_files failed to open %s",
250 full_fname(fname));
41cc97ae 251 }
b9f592fb
WD
252 free_sums(s);
253 continue;
254 }
e6e3f12f 255
b9f592fb
WD
256 /* map the local file */
257 if (do_fstat(fd, &st) != 0) {
258 io_error |= IOERR_GENERAL;
259 rsyserr(FERROR, errno, "fstat failed");
260 free_sums(s);
261 close(fd);
262 return;
263 }
11a5a3c7 264
96d910c7 265 if (st.st_size) {
9b919d59
WD
266 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
267 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
268 } else
269 mbuf = NULL;
6902ed17 270
b9f592fb
WD
271 if (verbose > 2) {
272 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
45c49b52 273 fname, (double)st.st_size);
6902ed17 274 }
e6e3f12f 275
e732fb0c 276 write_ndx_and_attrs(f_out, i, iflags, fnamecmp_type,
927c8068 277 xname, xlen);
a0545709 278 write_sum_head(f_xfer, s);
b9f592fb 279
45c49b52
WD
280 if (verbose > 2)
281 rprintf(FINFO, "calling match_sums %s\n", fname);
83fff1aa 282
8a9cfb24 283 if (log_before_transfer)
85909931 284 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
8a832465 285 else if (!am_server && verbose && do_progress)
85909931 286 rprintf(FCLIENT, "%s\n", fname2);
ecc81fce 287
83fff1aa 288 set_compression(fname);
1b7c47cb 289
a0545709 290 match_sums(f_xfer, s, mbuf, st.st_size);
0394e34a
WD
291 if (do_progress)
292 end_progress(st.st_size);
293
2fedf3d5 294 log_item(log_code, file, &initial_stats, iflags, NULL);
6902ed17 295
b9f592fb
WD
296 if (mbuf) {
297 j = unmap_file(mbuf);
298 if (j) {
299 io_error |= IOERR_GENERAL;
300 rsyserr(FERROR, j,
301 "read errors mapping %s",
302 full_fname(fname));
6a7cc46c 303 }
6902ed17 304 }
b9f592fb 305 close(fd);
e6e3f12f 306
2f03f956 307 free_sums(s);
e6e3f12f 308
45c49b52
WD
309 if (verbose > 2)
310 rprintf(FINFO, "sender finished %s\n", fname);
95306d9d
WD
311
312 /* Flag that we actually sent this entry. */
313 file->flags |= FLAG_SENT;
2f03f956 314 }
8b115ac8 315 make_backups = save_make_backups;
2f03f956
AT
316
317 if (verbose > 2)
e6e3f12f 318 rprintf(FINFO, "send files finished\n");
2f03f956
AT
319
320 match_report();
321
e6e3f12f 322 write_int(f_out, -1);
2f03f956 323}