- The itemized flags are now 2 bytes instead of 1.
[rsync/rsync.git] / sender.c
1 /*
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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
22 extern int verbose;
23 extern int log_before_transfer;
24 extern int csum_length;
25 extern struct stats stats;
26 extern int io_error;
27 extern int dry_run;
28 extern int am_server;
29 extern int am_daemon;
30 extern int protocol_version;
31 extern int updating_basis_file;
32 extern int make_backups;
33 extern int do_progress;
34 extern int inplace;
35 extern char *log_format;
36 extern struct stats stats;
37
38
39 /**
40  * @file
41  *
42  * The sender gets checksums from the generator, calculates deltas,
43  * and transmits them to the receiver.  The sender process runs on the
44  * machine holding the source files.
45  **/
46
47 /**
48  * Receive the checksums for a buffer
49  **/
50 static struct sum_struct *receive_sums(int f)
51 {
52         struct sum_struct *s;
53         int32 i;
54         OFF_T offset = 0;
55
56         if (!(s = new(struct sum_struct)))
57                 out_of_memory("receive_sums");
58
59         read_sum_head(f, s);
60
61         s->sums = NULL;
62
63         if (verbose > 3) {
64                 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
65                         (double)s->count, (long)s->blength, (long)s->remainder);
66         }
67
68         if (s->count == 0)
69                 return(s);
70
71         if (!(s->sums = new_array(struct sum_buf, s->count)))
72                 out_of_memory("receive_sums");
73
74         for (i = 0; i < s->count; i++) {
75                 s->sums[i].sum1 = read_int(f);
76                 read_buf(f, s->sums[i].sum2, s->s2length);
77
78                 s->sums[i].offset = offset;
79                 s->sums[i].flags = 0;
80
81                 if (i == s->count-1 && s->remainder != 0)
82                         s->sums[i].len = s->remainder;
83                 else
84                         s->sums[i].len = s->blength;
85                 offset += s->sums[i].len;
86
87                 if (verbose > 3) {
88                         rprintf(FINFO,
89                                 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
90                                 i, s->sums[i].len, (double)s->sums[i].offset,
91                                 s->sums[i].sum1);
92                 }
93         }
94
95         s->flength = offset;
96
97         return s;
98 }
99
100
101
102 void send_files(struct file_list *flist, int f_out, int f_in)
103 {
104         int fd = -1;
105         struct sum_struct *s;
106         struct map_struct *mbuf = NULL;
107         STRUCT_STAT st;
108         char *fname2, fname[MAXPATHLEN];
109         int iflags;
110         struct file_struct *file;
111         int phase = 0;
112         struct stats initial_stats;
113         int save_make_backups = make_backups;
114         int i, j;
115
116         if (verbose > 2)
117                 rprintf(FINFO, "send_files starting\n");
118
119         while (1) {
120                 unsigned int offset;
121
122                 i = read_int(f_in);
123                 if (i == -1) {
124                         if (phase == 0) {
125                                 phase++;
126                                 csum_length = SUM_LENGTH;
127                                 write_int(f_out, -1);
128                                 if (verbose > 2)
129                                         rprintf(FINFO, "send_files phase=%d\n", phase);
130                                 /* For inplace: redo phase turns off the backup
131                                  * flag so that we do a regular inplace send. */
132                                 make_backups = 0;
133                                 continue;
134                         }
135                         break;
136                 }
137
138                 if (i < 0 || i >= flist->count) {
139                         rprintf(FERROR, "Invalid file index %d (count=%d)\n",
140                                 i, flist->count);
141                         exit_cleanup(RERR_PROTOCOL);
142                 }
143
144                 file = flist->files[i];
145
146                 if (protocol_version >= 29) {
147                         iflags = read_byte(f_in);
148                         iflags |= read_byte(f_in) << 8;
149                         if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
150                                 if (am_server) {
151                                         write_int(f_out, i);
152                                         write_byte(f_out, iflags);
153                                         write_byte(f_out, iflags >> 8);
154                                 } else
155                                         log_send(file, &stats, iflags);
156                                 continue;
157                         }
158                 } else
159                         iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
160
161                 if (inplace && protocol_version >= 29) {
162                         uchar fnamecmp_type = read_byte(f_in);
163                         updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
164                 } else
165                         updating_basis_file = inplace && !make_backups;
166
167                 if (!S_ISREG(file->mode)) {
168                         rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
169                                 who_am_i(), i);
170                         exit_cleanup(RERR_PROTOCOL);
171                 }
172
173                 stats.current_file_index = i;
174                 stats.num_transferred_files++;
175                 stats.total_transferred_size += file->length;
176
177                 if (file->dir.root) {
178                         /* N.B. We're sure that this fits, so offset is OK. */
179                         offset = strlcpy(fname, file->dir.root, sizeof fname);
180                         if (!offset || fname[offset-1] != '/')
181                                 fname[offset++] = '/';
182                 } else
183                         offset = 0;
184                 fname2 = f_name_to(file, fname + offset);
185
186                 if (verbose > 2)
187                         rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
188
189                 if (dry_run) { /* log the transfer */
190                         if (!am_server && log_format)
191                                 log_send(file, &stats, iflags);
192                         write_int(f_out, i);
193                         if (protocol_version >= 29) {
194                                 write_byte(f_out, iflags);
195                                 write_byte(f_out, iflags >> 8);
196                         }
197                         continue;
198                 }
199
200                 initial_stats = stats;
201
202                 if (!(s = receive_sums(f_in))) {
203                         io_error |= IOERR_GENERAL;
204                         rprintf(FERROR, "receive_sums failed\n");
205                         return;
206                 }
207
208                 fd = do_open(fname, O_RDONLY, 0);
209                 if (fd == -1) {
210                         if (errno == ENOENT) {
211                                 enum logcode c = am_daemon
212                                     && protocol_version < 28 ? FERROR
213                                                              : FINFO;
214                                 io_error |= IOERR_VANISHED;
215                                 rprintf(c, "file has vanished: %s\n",
216                                         full_fname(fname));
217                         } else {
218                                 io_error |= IOERR_GENERAL;
219                                 rsyserr(FERROR, errno,
220                                         "send_files failed to open %s",
221                                         full_fname(fname));
222                         }
223                         free_sums(s);
224                         continue;
225                 }
226
227                 /* map the local file */
228                 if (do_fstat(fd, &st) != 0) {
229                         io_error |= IOERR_GENERAL;
230                         rsyserr(FERROR, errno, "fstat failed");
231                         free_sums(s);
232                         close(fd);
233                         return;
234                 }
235
236                 if (st.st_size) {
237                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
238                         mbuf = map_file(fd, st.st_size, read_size, s->blength);
239                 } else
240                         mbuf = NULL;
241
242                 if (verbose > 2) {
243                         rprintf(FINFO, "send_files mapped %s of size %.0f\n",
244                                 safe_fname(fname), (double)st.st_size);
245                 }
246
247                 write_int(f_out, i);
248                 if (protocol_version >= 29) {
249                         write_byte(f_out, iflags);
250                         write_byte(f_out, iflags >> 8);
251                 }
252                 write_sum_head(f_out, s);
253
254                 if (verbose > 2) {
255                         rprintf(FINFO, "calling match_sums %s\n",
256                                 safe_fname(fname));
257                 }
258
259                 if (log_before_transfer)
260                         log_send(file, &initial_stats, iflags);
261                 else if (!am_server && verbose && do_progress)
262                         rprintf(FINFO, "%s\n", safe_fname(fname2));
263
264                 set_compression(fname);
265
266                 match_sums(f_out, s, mbuf, st.st_size);
267                 if (!log_before_transfer)
268                         log_send(file, &initial_stats, iflags);
269
270                 if (mbuf) {
271                         j = unmap_file(mbuf);
272                         if (j) {
273                                 io_error |= IOERR_GENERAL;
274                                 rsyserr(FERROR, j,
275                                         "read errors mapping %s",
276                                         full_fname(fname));
277                         }
278                 }
279                 close(fd);
280
281                 free_sums(s);
282
283                 if (verbose > 2) {
284                         rprintf(FINFO, "sender finished %s\n",
285                                 safe_fname(fname));
286                 }
287         }
288         make_backups = save_make_backups;
289
290         if (verbose > 2)
291                 rprintf(FINFO, "send files finished\n");
292
293         match_report();
294
295         write_int(f_out, -1);
296 }