Transformed the push/pop functions for the redo-list into more
[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;
7f37167d 23extern int dry_run;
8715db2c
WD
24extern int am_server;
25extern int am_daemon;
8a9cfb24 26extern int log_before_transfer;
7f37167d
WD
27extern int log_format_has_i;
28extern int daemon_log_format_has_i;
2f03f956 29extern int csum_length;
2f03f956 30extern int io_error;
d6631cf3 31extern int protocol_version;
95306d9d 32extern int remove_sent_files;
53f8519a 33extern int updating_basis_file;
8b115ac8 34extern int make_backups;
8a9cfb24 35extern int do_progress;
53f8519a 36extern int inplace;
a3221d2a 37extern struct stats stats;
8715db2c 38extern char *log_format;
2f03f956
AT
39
40
0f9c48b1
MP
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 **/
fc0257c9 48
ce8149b6
MP
49/**
50 * Receive the checksums for a buffer
51 **/
2f03f956
AT
52static struct sum_struct *receive_sums(int f)
53{
54 struct sum_struct *s;
a1cbe76e 55 int32 i;
2f03f956
AT
56 OFF_T offset = 0;
57
a3221d2a
WD
58 if (!(s = new(struct sum_struct)))
59 out_of_memory("receive_sums");
2f03f956 60
fc0257c9
S
61 read_sum_head(f, s);
62
2f03f956
AT
63 s->sums = NULL;
64
da9d12f5 65 if (verbose > 3) {
6c495e0d
WD
66 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
67 (double)s->count, (long)s->blength, (long)s->remainder);
da9d12f5 68 }
2f03f956 69
e6e3f12f 70 if (s->count == 0)
2f03f956
AT
71 return(s);
72
a3221d2a
WD
73 if (!(s->sums = new_array(struct sum_buf, s->count)))
74 out_of_memory("receive_sums");
2f03f956 75
eed47b6b 76 for (i = 0; i < s->count; i++) {
2f03f956 77 s->sums[i].sum1 = read_int(f);
e6e3f12f 78 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
79
80 s->sums[i].offset = offset;
a3221d2a 81 s->sums[i].flags = 0;
2f03f956 82
eed47b6b 83 if (i == s->count-1 && s->remainder != 0)
2f03f956 84 s->sums[i].len = s->remainder;
a3221d2a 85 else
fc0257c9 86 s->sums[i].len = s->blength;
2f03f956
AT
87 offset += s->sums[i].len;
88
a3221d2a
WD
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 }
2f03f956
AT
95 }
96
97 s->flength = offset;
98
99 return s;
100}
101
95306d9d 102static struct file_list *the_flist;
2f03f956 103
95306d9d
WD
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}
2f03f956 128
e6e3f12f
S
129void send_files(struct file_list *flist, int f_out, int f_in)
130{
c1659c79 131 int fd = -1;
2f03f956 132 struct sum_struct *s;
41cc97ae 133 struct map_struct *mbuf = NULL;
2f03f956 134 STRUCT_STAT st;
ecc81fce 135 char *fname2, fname[MAXPATHLEN];
8a9cfb24 136 int iflags;
2f03f956
AT
137 struct file_struct *file;
138 int phase = 0;
1b7c47cb 139 struct stats initial_stats;
8b115ac8 140 int save_make_backups = make_backups;
7f37167d
WD
141 int itemizing = am_daemon ? daemon_log_format_has_i
142 : !am_server && log_format_has_i;
8a9cfb24 143 int i, j;
2f03f956
AT
144
145 if (verbose > 2)
e6e3f12f 146 rprintf(FINFO, "send_files starting\n");
2f03f956 147
95306d9d
WD
148 the_flist = flist;
149
2f03f956 150 while (1) {
3fef5364 151 unsigned int offset;
2f03f956
AT
152
153 i = read_int(f_in);
154 if (i == -1) {
e6e3f12f 155 if (phase == 0) {
2f03f956
AT
156 phase++;
157 csum_length = SUM_LENGTH;
e6e3f12f 158 write_int(f_out, -1);
2f03f956 159 if (verbose > 2)
e6e3f12f 160 rprintf(FINFO, "send_files phase=%d\n", phase);
9715c589
WD
161 /* For inplace: redo phase turns off the backup
162 * flag so that we do a regular inplace send. */
8b115ac8 163 make_backups = 0;
2f03f956
AT
164 continue;
165 }
166 break;
167 }
168
169 if (i < 0 || i >= flist->count) {
8e6cf5d1
WD
170 /* Handle the new keep-alive (no-op) packet. */
171 if (i == flist->count && protocol_version >= 29
172 && read_shortint(f_in) == ITEM_IS_NEW) {
173 write_int(f_out, i);
174 write_shortint(f_out, ITEM_IS_NEW);
175 continue;
176 }
e6e3f12f 177 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
2f03f956 178 i, flist->count);
65417579 179 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
180 }
181
8a9cfb24 182 file = flist->files[i];
7f37167d
WD
183 if (file->dir.root) {
184 /* N.B. We're sure that this fits, so offset is OK. */
185 offset = strlcpy(fname, file->dir.root, sizeof fname);
186 if (!offset || fname[offset-1] != '/')
187 fname[offset++] = '/';
188 } else
189 offset = 0;
190 fname2 = f_name_to(file, fname + offset);
191
192 if (verbose > 2)
193 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
8a9cfb24 194
8a832465 195 if (protocol_version >= 29) {
742be97c 196 iflags = read_shortint(f_in);
8a9cfb24 197 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
7f37167d
WD
198 int see_item = itemizing && (iflags || verbose > 1);
199 write_int(f_out, i);
200 write_shortint(f_out, iflags);
8a9cfb24 201 if (am_server) {
7f37167d 202 if (am_daemon && !dry_run && see_item)
9118a09c 203 log_send(file, &stats, iflags);
7f37167d 204 } else if (see_item || iflags & ITEM_UPDATING
e844be4e
WD
205 || (S_ISDIR(file->mode)
206 && iflags & ITEM_REPORT_TIME))
9118a09c 207 log_send(file, &stats, iflags);
8a9cfb24
WD
208 continue;
209 }
210 } else
211 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
212
b951e023 213 if (inplace && protocol_version >= 29) {
cfb691ac 214 updating_basis_file = !(iflags & ITEM_USING_ALT_BASIS);
b951e023
WD
215 } else
216 updating_basis_file = inplace && !make_backups;
217
8a9cfb24
WD
218 if (!S_ISREG(file->mode)) {
219 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
afd72c78
WD
220 who_am_i(), i);
221 exit_cleanup(RERR_PROTOCOL);
222 }
2f03f956 223
97feb557 224 stats.current_file_index = i;
2f03f956
AT
225 stats.num_transferred_files++;
226 stats.total_transferred_size += file->length;
227
a8ed4958 228 if (dry_run) { /* log the transfer */
8a832465 229 if (!am_server && log_format)
8a9cfb24 230 log_send(file, &stats, iflags);
e6e3f12f 231 write_int(f_out, i);
e844be4e 232 if (protocol_version >= 29)
742be97c 233 write_shortint(f_out, iflags);
2f03f956
AT
234 continue;
235 }
236
1b7c47cb
AT
237 initial_stats = stats;
238
efa95a18 239 if (!(s = receive_sums(f_in))) {
06c28400 240 io_error |= IOERR_GENERAL;
e6e3f12f 241 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
242 return;
243 }
76f79ba7 244
b9f592fb
WD
245 fd = do_open(fname, O_RDONLY, 0);
246 if (fd == -1) {
247 if (errno == ENOENT) {
248 enum logcode c = am_daemon
249 && protocol_version < 28 ? FERROR
250 : FINFO;
251 io_error |= IOERR_VANISHED;
252 rprintf(c, "file has vanished: %s\n",
253 full_fname(fname));
254 } else {
06c28400 255 io_error |= IOERR_GENERAL;
b9f592fb
WD
256 rsyserr(FERROR, errno,
257 "send_files failed to open %s",
258 full_fname(fname));
41cc97ae 259 }
b9f592fb
WD
260 free_sums(s);
261 continue;
262 }
e6e3f12f 263
b9f592fb
WD
264 /* map the local file */
265 if (do_fstat(fd, &st) != 0) {
266 io_error |= IOERR_GENERAL;
267 rsyserr(FERROR, errno, "fstat failed");
268 free_sums(s);
269 close(fd);
270 return;
271 }
11a5a3c7 272
96d910c7 273 if (st.st_size) {
9b919d59
WD
274 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
275 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
276 } else
277 mbuf = NULL;
6902ed17 278
b9f592fb
WD
279 if (verbose > 2) {
280 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
ecc81fce 281 safe_fname(fname), (double)st.st_size);
6902ed17 282 }
e6e3f12f 283
b9f592fb 284 write_int(f_out, i);
e844be4e 285 if (protocol_version >= 29)
742be97c 286 write_shortint(f_out, iflags);
b9f592fb
WD
287 write_sum_head(f_out, s);
288
ecc81fce
WD
289 if (verbose > 2) {
290 rprintf(FINFO, "calling match_sums %s\n",
291 safe_fname(fname));
11a5a3c7 292 }
83fff1aa 293
8a9cfb24
WD
294 if (log_before_transfer)
295 log_send(file, &initial_stats, iflags);
8a832465 296 else if (!am_server && verbose && do_progress)
ecc81fce
WD
297 rprintf(FINFO, "%s\n", safe_fname(fname2));
298
83fff1aa 299 set_compression(fname);
1b7c47cb 300
b9f592fb 301 match_sums(f_out, s, mbuf, st.st_size);
8a9cfb24
WD
302 if (!log_before_transfer)
303 log_send(file, &initial_stats, iflags);
6902ed17 304
b9f592fb
WD
305 if (mbuf) {
306 j = unmap_file(mbuf);
307 if (j) {
308 io_error |= IOERR_GENERAL;
309 rsyserr(FERROR, j,
310 "read errors mapping %s",
311 full_fname(fname));
6a7cc46c 312 }
6902ed17 313 }
b9f592fb 314 close(fd);
e6e3f12f 315
2f03f956 316 free_sums(s);
e6e3f12f 317
ecc81fce
WD
318 if (verbose > 2) {
319 rprintf(FINFO, "sender finished %s\n",
320 safe_fname(fname));
321 }
95306d9d
WD
322
323 /* Flag that we actually sent this entry. */
324 file->flags |= FLAG_SENT;
2f03f956 325 }
8b115ac8 326 make_backups = save_make_backups;
2f03f956
AT
327
328 if (verbose > 2)
e6e3f12f 329 rprintf(FINFO, "send files finished\n");
2f03f956
AT
330
331 match_report();
332
e6e3f12f 333 write_int(f_out, -1);
2f03f956 334}