When logging PERMS_REPORT messages, send them with the FCLIENT code
[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;
8a9cfb24 24extern int log_before_transfer;
7f37167d
WD
25extern int log_format_has_i;
26extern int daemon_log_format_has_i;
27extern int am_server;
28extern int am_daemon;
2f03f956
AT
29extern int csum_length;
30extern struct stats stats;
31extern int io_error;
d6631cf3 32extern int protocol_version;
53f8519a 33extern int updating_basis_file;
8b115ac8 34extern int make_backups;
8a9cfb24 35extern int do_progress;
53f8519a 36extern int inplace;
8a9cfb24 37extern char *log_format;
a3221d2a 38extern struct stats stats;
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
102
103
e6e3f12f
S
104void send_files(struct file_list *flist, int f_out, int f_in)
105{
c1659c79 106 int fd = -1;
2f03f956 107 struct sum_struct *s;
41cc97ae 108 struct map_struct *mbuf = NULL;
2f03f956 109 STRUCT_STAT st;
ecc81fce 110 char *fname2, fname[MAXPATHLEN];
8a9cfb24 111 int iflags;
2f03f956
AT
112 struct file_struct *file;
113 int phase = 0;
1b7c47cb 114 struct stats initial_stats;
8b115ac8 115 int save_make_backups = make_backups;
7f37167d
WD
116 int itemizing = am_daemon ? daemon_log_format_has_i
117 : !am_server && log_format_has_i;
8a9cfb24 118 int i, j;
2f03f956
AT
119
120 if (verbose > 2)
e6e3f12f 121 rprintf(FINFO, "send_files starting\n");
2f03f956 122
2f03f956 123 while (1) {
3fef5364 124 unsigned int offset;
2f03f956
AT
125
126 i = read_int(f_in);
127 if (i == -1) {
e6e3f12f 128 if (phase == 0) {
2f03f956
AT
129 phase++;
130 csum_length = SUM_LENGTH;
e6e3f12f 131 write_int(f_out, -1);
2f03f956 132 if (verbose > 2)
e6e3f12f 133 rprintf(FINFO, "send_files phase=%d\n", phase);
9715c589
WD
134 /* For inplace: redo phase turns off the backup
135 * flag so that we do a regular inplace send. */
8b115ac8 136 make_backups = 0;
2f03f956
AT
137 continue;
138 }
139 break;
140 }
141
142 if (i < 0 || i >= flist->count) {
e6e3f12f 143 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
2f03f956 144 i, flist->count);
65417579 145 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
146 }
147
8a9cfb24 148 file = flist->files[i];
7f37167d
WD
149 if (file->dir.root) {
150 /* N.B. We're sure that this fits, so offset is OK. */
151 offset = strlcpy(fname, file->dir.root, sizeof fname);
152 if (!offset || fname[offset-1] != '/')
153 fname[offset++] = '/';
154 } else
155 offset = 0;
156 fname2 = f_name_to(file, fname + offset);
157
158 if (verbose > 2)
159 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
8a9cfb24 160
8a832465 161 if (protocol_version >= 29) {
742be97c 162 iflags = read_shortint(f_in);
8a9cfb24 163 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
7f37167d
WD
164 int see_item = itemizing && (iflags || verbose > 1);
165 write_int(f_out, i);
166 write_shortint(f_out, iflags);
8a9cfb24 167 if (am_server) {
7f37167d
WD
168 if (am_daemon && !dry_run && see_item)
169 log_recv(file, &stats, iflags);
170 } else if (see_item || iflags & ITEM_UPDATING
e844be4e
WD
171 || (S_ISDIR(file->mode)
172 && iflags & ITEM_REPORT_TIME))
7f37167d 173 log_recv(file, &stats, iflags);
8a9cfb24
WD
174 continue;
175 }
176 } else
177 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
178
b951e023
WD
179 if (inplace && protocol_version >= 29) {
180 uchar fnamecmp_type = read_byte(f_in);
181 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
182 } else
183 updating_basis_file = inplace && !make_backups;
184
8a9cfb24
WD
185 if (!S_ISREG(file->mode)) {
186 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
afd72c78
WD
187 who_am_i(), i);
188 exit_cleanup(RERR_PROTOCOL);
189 }
2f03f956 190
97feb557 191 stats.current_file_index = i;
2f03f956
AT
192 stats.num_transferred_files++;
193 stats.total_transferred_size += file->length;
194
a8ed4958 195 if (dry_run) { /* log the transfer */
8a832465 196 if (!am_server && log_format)
8a9cfb24 197 log_send(file, &stats, iflags);
e6e3f12f 198 write_int(f_out, i);
e844be4e 199 if (protocol_version >= 29)
742be97c 200 write_shortint(f_out, iflags);
2f03f956
AT
201 continue;
202 }
203
1b7c47cb
AT
204 initial_stats = stats;
205
efa95a18 206 if (!(s = receive_sums(f_in))) {
06c28400 207 io_error |= IOERR_GENERAL;
e6e3f12f 208 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
209 return;
210 }
76f79ba7 211
b9f592fb
WD
212 fd = do_open(fname, O_RDONLY, 0);
213 if (fd == -1) {
214 if (errno == ENOENT) {
215 enum logcode c = am_daemon
216 && protocol_version < 28 ? FERROR
217 : FINFO;
218 io_error |= IOERR_VANISHED;
219 rprintf(c, "file has vanished: %s\n",
220 full_fname(fname));
221 } else {
06c28400 222 io_error |= IOERR_GENERAL;
b9f592fb
WD
223 rsyserr(FERROR, errno,
224 "send_files failed to open %s",
225 full_fname(fname));
41cc97ae 226 }
b9f592fb
WD
227 free_sums(s);
228 continue;
229 }
e6e3f12f 230
b9f592fb
WD
231 /* map the local file */
232 if (do_fstat(fd, &st) != 0) {
233 io_error |= IOERR_GENERAL;
234 rsyserr(FERROR, errno, "fstat failed");
235 free_sums(s);
236 close(fd);
237 return;
238 }
11a5a3c7 239
96d910c7 240 if (st.st_size) {
9b919d59
WD
241 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
242 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
243 } else
244 mbuf = NULL;
6902ed17 245
b9f592fb
WD
246 if (verbose > 2) {
247 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
ecc81fce 248 safe_fname(fname), (double)st.st_size);
6902ed17 249 }
e6e3f12f 250
b9f592fb 251 write_int(f_out, i);
e844be4e 252 if (protocol_version >= 29)
742be97c 253 write_shortint(f_out, iflags);
b9f592fb
WD
254 write_sum_head(f_out, s);
255
ecc81fce
WD
256 if (verbose > 2) {
257 rprintf(FINFO, "calling match_sums %s\n",
258 safe_fname(fname));
11a5a3c7 259 }
83fff1aa 260
8a9cfb24
WD
261 if (log_before_transfer)
262 log_send(file, &initial_stats, iflags);
8a832465 263 else if (!am_server && verbose && do_progress)
ecc81fce
WD
264 rprintf(FINFO, "%s\n", safe_fname(fname2));
265
83fff1aa 266 set_compression(fname);
1b7c47cb 267
b9f592fb 268 match_sums(f_out, s, mbuf, st.st_size);
8a9cfb24
WD
269 if (!log_before_transfer)
270 log_send(file, &initial_stats, iflags);
6902ed17 271
b9f592fb
WD
272 if (mbuf) {
273 j = unmap_file(mbuf);
274 if (j) {
275 io_error |= IOERR_GENERAL;
276 rsyserr(FERROR, j,
277 "read errors mapping %s",
278 full_fname(fname));
6a7cc46c 279 }
6902ed17 280 }
b9f592fb 281 close(fd);
e6e3f12f 282
2f03f956 283 free_sums(s);
e6e3f12f 284
ecc81fce
WD
285 if (verbose > 2) {
286 rprintf(FINFO, "sender finished %s\n",
287 safe_fname(fname));
288 }
2f03f956 289 }
8b115ac8 290 make_backups = save_make_backups;
2f03f956
AT
291
292 if (verbose > 2)
e6e3f12f 293 rprintf(FINFO, "send files finished\n");
2f03f956
AT
294
295 match_report();
296
e6e3f12f 297 write_int(f_out, -1);
2f03f956 298}