blob: adfe7d55509485181c7b78150bbee10cba7ba077 [file] [log] [blame]
J.W. Schultze6e3f122003-08-15 00:57:27 +00001/*
Andrew Tridgell2f03f951998-07-25 02:25:22 +00002 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
J.W. Schultze6e3f122003-08-15 00:57:27 +00004
Andrew Tridgell2f03f951998-07-25 02:25:22 +00005 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.
J.W. Schultze6e3f122003-08-15 00:57:27 +00009
Andrew Tridgell2f03f951998-07-25 02:25:22 +000010 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.
J.W. Schultze6e3f122003-08-15 00:57:27 +000014
Andrew Tridgell2f03f951998-07-25 02:25:22 +000015 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;
Wayne Davison7f371672005-02-19 22:16:50 +000023extern int dry_run;
Wayne Davison8715db22005-02-20 00:16:23 +000024extern int am_server;
25extern int am_daemon;
Wayne Davison8a9cfb22005-02-16 08:10:41 +000026extern int log_before_transfer;
Wayne Davison7f371672005-02-19 22:16:50 +000027extern int log_format_has_i;
28extern int daemon_log_format_has_i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000029extern int csum_length;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000030extern int io_error;
Wayne Davisond6631cf2004-01-15 08:56:33 +000031extern int protocol_version;
Wayne Davison95306d92005-02-20 01:04:54 +000032extern int remove_sent_files;
Wayne Davison53f85192005-01-17 23:11:26 +000033extern int updating_basis_file;
Wayne Davison8b115ac2004-09-07 21:32:36 +000034extern int make_backups;
Wayne Davison8a9cfb22005-02-16 08:10:41 +000035extern int do_progress;
Wayne Davison53f85192005-01-17 23:11:26 +000036extern int inplace;
Wayne Davisona3221d22004-07-16 20:06:24 +000037extern struct stats stats;
Wayne Davison5f406152005-03-04 15:57:14 +000038extern struct file_list *the_file_list;
Wayne Davison8715db22005-02-20 00:16:23 +000039extern char *log_format;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000040
41
Martin Pool0f9c48b2002-04-08 08:34:03 +000042/**
43 * @file
44 *
45 * The sender gets checksums from the generator, calculates deltas,
46 * and transmits them to the receiver. The sender process runs on the
47 * machine holding the source files.
48 **/
J.W. Schultzfc0257c2003-04-10 01:13:30 +000049
Martin Poolce8149b2002-04-09 06:03:50 +000050/**
51 * Receive the checksums for a buffer
52 **/
Andrew Tridgell2f03f951998-07-25 02:25:22 +000053static struct sum_struct *receive_sums(int f)
54{
55 struct sum_struct *s;
Wayne Davisona1cbe762005-02-14 22:47:42 +000056 int32 i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000057 OFF_T offset = 0;
58
Wayne Davisona3221d22004-07-16 20:06:24 +000059 if (!(s = new(struct sum_struct)))
60 out_of_memory("receive_sums");
Andrew Tridgell2f03f951998-07-25 02:25:22 +000061
J.W. Schultzfc0257c2003-04-10 01:13:30 +000062 read_sum_head(f, s);
63
Andrew Tridgell2f03f951998-07-25 02:25:22 +000064 s->sums = NULL;
65
Wayne Davisonda9d12f2004-01-03 18:37:41 +000066 if (verbose > 3) {
Wayne Davison6c495e02005-01-01 21:08:17 +000067 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
68 (double)s->count, (long)s->blength, (long)s->remainder);
Wayne Davisonda9d12f2004-01-03 18:37:41 +000069 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +000070
J.W. Schultze6e3f122003-08-15 00:57:27 +000071 if (s->count == 0)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000072 return(s);
73
Wayne Davisona3221d22004-07-16 20:06:24 +000074 if (!(s->sums = new_array(struct sum_buf, s->count)))
75 out_of_memory("receive_sums");
Andrew Tridgell2f03f951998-07-25 02:25:22 +000076
Wayne Davisoneed47b62005-02-11 20:46:55 +000077 for (i = 0; i < s->count; i++) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +000078 s->sums[i].sum1 = read_int(f);
J.W. Schultze6e3f122003-08-15 00:57:27 +000079 read_buf(f, s->sums[i].sum2, s->s2length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000080
81 s->sums[i].offset = offset;
Wayne Davisona3221d22004-07-16 20:06:24 +000082 s->sums[i].flags = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000083
Wayne Davisoneed47b62005-02-11 20:46:55 +000084 if (i == s->count-1 && s->remainder != 0)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000085 s->sums[i].len = s->remainder;
Wayne Davisona3221d22004-07-16 20:06:24 +000086 else
J.W. Schultzfc0257c2003-04-10 01:13:30 +000087 s->sums[i].len = s->blength;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000088 offset += s->sums[i].len;
89
Wayne Davisona3221d22004-07-16 20:06:24 +000090 if (verbose > 3) {
91 rprintf(FINFO,
92 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
93 i, s->sums[i].len, (double)s->sums[i].offset,
94 s->sums[i].sum1);
95 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +000096 }
97
98 s->flength = offset;
99
100 return s;
101}
102
Wayne Davison5f406152005-03-04 15:57:14 +0000103void successful_send(int ndx)
Wayne Davison95306d92005-02-20 01:04:54 +0000104{
105 char fname[MAXPATHLEN];
106 struct file_struct *file;
107 unsigned int offset;
108
Wayne Davison5f406152005-03-04 15:57:14 +0000109 if (ndx < 0 || ndx >= the_file_list->count)
Wayne Davison95306d92005-02-20 01:04:54 +0000110 return;
111
Wayne Davison5f406152005-03-04 15:57:14 +0000112 file = the_file_list->files[ndx];
Wayne Davison95306d92005-02-20 01:04:54 +0000113 /* The generator might tell us about symlinks we didn't send. */
114 if (!(file->flags & FLAG_SENT) && !S_ISLNK(file->mode))
115 return;
116 if (file->dir.root) {
117 offset = stringjoin(fname, sizeof fname,
118 file->dir.root, "/", NULL);
119 } else
120 offset = 0;
121 f_name_to(file, fname + offset);
122 if (remove_sent_files && do_unlink(fname) == 0 && verbose > 1) {
123 rprintf(FINFO, "sender removed %s\n",
124 safe_fname(fname + offset));
125 }
126}
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000127
Wayne Davison6087ef22005-03-10 00:05:58 +0000128static void write_item_attrs(int f_out, int ndx, int iflags,
129 uchar fnamecmp_type, char *buf, int len)
Wayne Davison4d53c4d2005-03-09 18:54:19 +0000130{
131 write_int(f_out, ndx);
132 if (protocol_version < 29)
133 return;
134 write_shortint(f_out, iflags);
135 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
136 write_byte(f_out, fnamecmp_type);
137 if (iflags & ITEM_XNAME_FOLLOWS) {
138 if (len < 0)
139 len = strlen(buf);
140 write_vstring(f_out, buf, len);
141 }
142}
143
Wayne Davison165e6d42005-03-04 16:53:26 +0000144/* This is also used by receive.c with f_out = -1. */
Wayne Davison6087ef22005-03-10 00:05:58 +0000145int read_item_attrs(int f_in, int f_out, int ndx, uchar *type_ptr,
146 char *buf, int *len_ptr)
Wayne Davison165e6d42005-03-04 16:53:26 +0000147{
Wayne Davisonfad3dc42005-03-05 00:21:59 +0000148 int len;
Wayne Davison4d53c4d2005-03-09 18:54:19 +0000149 uchar fnamecmp_type = FNAMECMP_FNAME;
Wayne Davison165e6d42005-03-04 16:53:26 +0000150 int iflags = protocol_version >= 29 ? read_shortint(f_in)
Wayne Davisonfad3dc42005-03-05 00:21:59 +0000151 : ITEM_TRANSFER | ITEM_MISSING_DATA;
152 int isave = iflags; /* XXX remove soon */
Wayne Davison165e6d42005-03-04 16:53:26 +0000153
154 /* Handle the new keep-alive (no-op) packet. */
155 if (ndx == the_file_list->count && iflags == ITEM_IS_NEW)
156 ;
157 else if (ndx < 0 || ndx >= the_file_list->count) {
158 rprintf(FERROR, "Invalid file index %d (count=%d) [%s]\n",
159 ndx, the_file_list->count, who_am_i());
160 exit_cleanup(RERR_PROTOCOL);
161 } else if (iflags == ITEM_IS_NEW) {
162 rprintf(FERROR, "Invalid itemized flag word [%s]\n",
163 who_am_i());
164 exit_cleanup(RERR_PROTOCOL);
165 }
166
Wayne Davison4d53c4d2005-03-09 18:54:19 +0000167 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
168 fnamecmp_type = read_byte(f_in);
169 *type_ptr = fnamecmp_type;
170
171 if (iflags & ITEM_XNAME_FOLLOWS) {
172 if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
173 exit_cleanup(RERR_PROTOCOL);
174 } else {
Wayne Davisonfad3dc42005-03-05 00:21:59 +0000175 *buf = '\0';
176 len = -1;
177 }
Wayne Davison6087ef22005-03-10 00:05:58 +0000178 *len_ptr = len;
Wayne Davison85aa57a2005-03-04 18:01:16 +0000179
Wayne Davisonc2b11ba2005-03-06 23:37:42 +0000180 /* XXX Temporary backward compatibility when talking to 2.6.4pre[12] */
181 if (protocol_version >= 29 && iflags & ITEM_TRANSFER
182 && !S_ISREG(the_file_list->files[ndx]->mode)) {
183 iflags &= ~ITEM_TRANSFER;
184 iflags |= ITEM_LOCAL_CHANGE;
Wayne Davisonfad3dc42005-03-05 00:21:59 +0000185 }
186
187 if (iflags & ITEM_TRANSFER) {
188 if (!S_ISREG(the_file_list->files[ndx]->mode)) {
189 rprintf(FERROR,
190 "received index of non-regular file: %d [%s]\n",
191 ndx, who_am_i());
192 exit_cleanup(RERR_PROTOCOL);
193 }
194 } else if (f_out >= 0) {
Wayne Davison4d53c4d2005-03-09 18:54:19 +0000195 write_item_attrs(f_out, ndx, isave /*XXX iflags */,
Wayne Davison6087ef22005-03-10 00:05:58 +0000196 fnamecmp_type, buf, len);
Wayne Davison165e6d42005-03-04 16:53:26 +0000197 }
198
199 return iflags;
200}
201
J.W. Schultze6e3f122003-08-15 00:57:27 +0000202void send_files(struct file_list *flist, int f_out, int f_in)
203{
Martin Poolc1659c72001-08-31 09:27:35 +0000204 int fd = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000205 struct sum_struct *s;
Wayne Davison41cc97a2004-05-11 19:53:16 +0000206 struct map_struct *mbuf = NULL;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000207 STRUCT_STAT st;
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000208 char *fname2, fname[MAXPATHLEN];
Wayne Davison6087ef22005-03-10 00:05:58 +0000209 char xname[MAXPATHLEN];
Wayne Davison4d53c4d2005-03-09 18:54:19 +0000210 uchar fnamecmp_type;
Wayne Davison6087ef22005-03-10 00:05:58 +0000211 int iflags, xlen;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000212 struct file_struct *file;
213 int phase = 0;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000214 struct stats initial_stats;
Wayne Davison8b115ac2004-09-07 21:32:36 +0000215 int save_make_backups = make_backups;
Wayne Davison7f371672005-02-19 22:16:50 +0000216 int itemizing = am_daemon ? daemon_log_format_has_i
217 : !am_server && log_format_has_i;
Wayne Davison8a9cfb22005-02-16 08:10:41 +0000218 int i, j;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000219
220 if (verbose > 2)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000221 rprintf(FINFO, "send_files starting\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000222
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000223 while (1) {
Wayne Davison3fef5362004-01-22 04:38:18 +0000224 unsigned int offset;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000225
226 i = read_int(f_in);
227 if (i == -1) {
J.W. Schultze6e3f122003-08-15 00:57:27 +0000228 if (phase == 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000229 phase++;
230 csum_length = SUM_LENGTH;
J.W. Schultze6e3f122003-08-15 00:57:27 +0000231 write_int(f_out, -1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000232 if (verbose > 2)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000233 rprintf(FINFO, "send_files phase=%d\n", phase);
Wayne Davison9715c582004-09-20 04:17:42 +0000234 /* For inplace: redo phase turns off the backup
235 * flag so that we do a regular inplace send. */
Wayne Davison8b115ac2004-09-07 21:32:36 +0000236 make_backups = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000237 continue;
238 }
239 break;
240 }
241
Wayne Davison6087ef22005-03-10 00:05:58 +0000242 iflags = read_item_attrs(f_in, f_out, i, &fnamecmp_type,
243 xname, &xlen);
Wayne Davison165e6d42005-03-04 16:53:26 +0000244 if (iflags == ITEM_IS_NEW) /* no-op packet */
245 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000246
Wayne Davison8a9cfb22005-02-16 08:10:41 +0000247 file = flist->files[i];
Wayne Davison7f371672005-02-19 22:16:50 +0000248 if (file->dir.root) {
249 /* N.B. We're sure that this fits, so offset is OK. */
250 offset = strlcpy(fname, file->dir.root, sizeof fname);
251 if (!offset || fname[offset-1] != '/')
252 fname[offset++] = '/';
253 } else
254 offset = 0;
255 fname2 = f_name_to(file, fname + offset);
256
257 if (verbose > 2)
258 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
Wayne Davison8a9cfb22005-02-16 08:10:41 +0000259
Wayne Davisonfad3dc42005-03-05 00:21:59 +0000260 if (!(iflags & ITEM_TRANSFER)) {
Wayne Davison6087ef22005-03-10 00:05:58 +0000261 maybe_log_item(file, iflags, itemizing, xname);
Wayne Davison165e6d42005-03-04 16:53:26 +0000262 continue;
263 }
Wayne Davison8a9cfb22005-02-16 08:10:41 +0000264
Wayne Davison6087ef22005-03-10 00:05:58 +0000265 updating_basis_file = inplace && (protocol_version >= 29
266 ? fnamecmp_type == FNAMECMP_FNAME : !make_backups);
Wayne Davisonb951e022005-01-24 17:19:04 +0000267
Wayne Davison97feb552004-01-13 18:22:43 +0000268 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000269 stats.num_transferred_files++;
270 stats.total_transferred_size += file->length;
271
Wayne Davisona8ed4952005-02-15 07:20:04 +0000272 if (dry_run) { /* log the transfer */
Wayne Davison8a832462005-02-16 17:02:16 +0000273 if (!am_server && log_format)
Wayne Davisonb694f8a2005-03-04 16:08:02 +0000274 log_item(file, &stats, iflags, NULL);
Wayne Davison6087ef22005-03-10 00:05:58 +0000275 write_item_attrs(f_out, i, iflags, fnamecmp_type,
276 xname, xlen);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000277 continue;
278 }
279
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000280 initial_stats = stats;
281
Wayne Davisonefa95a12004-07-20 21:35:46 +0000282 if (!(s = receive_sums(f_in))) {
Wayne Davison06c28402003-12-15 08:10:31 +0000283 io_error |= IOERR_GENERAL;
J.W. Schultze6e3f122003-08-15 00:57:27 +0000284 rprintf(FERROR, "receive_sums failed\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000285 return;
286 }
Martin Pool76f79ba2002-01-23 05:51:06 +0000287
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000288 fd = do_open(fname, O_RDONLY, 0);
289 if (fd == -1) {
290 if (errno == ENOENT) {
291 enum logcode c = am_daemon
292 && protocol_version < 28 ? FERROR
293 : FINFO;
294 io_error |= IOERR_VANISHED;
295 rprintf(c, "file has vanished: %s\n",
296 full_fname(fname));
297 } else {
Wayne Davison06c28402003-12-15 08:10:31 +0000298 io_error |= IOERR_GENERAL;
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000299 rsyserr(FERROR, errno,
300 "send_files failed to open %s",
301 full_fname(fname));
Martin Poolc1659c72001-08-31 09:27:35 +0000302 }
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000303 free_sums(s);
304 continue;
Martin Pool6902ed12001-08-14 02:04:47 +0000305 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000306
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000307 /* map the local file */
308 if (do_fstat(fd, &st) != 0) {
309 io_error |= IOERR_GENERAL;
310 rsyserr(FERROR, errno, "fstat failed");
311 free_sums(s);
312 close(fd);
313 return;
314 }
315
Wayne Davison96d910c2004-08-03 08:05:27 +0000316 if (st.st_size) {
Wayne Davison9b919d52005-01-17 22:51:29 +0000317 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
318 mbuf = map_file(fd, st.st_size, read_size, s->blength);
Wayne Davison96d910c2004-08-03 08:05:27 +0000319 } else
320 mbuf = NULL;
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000321
322 if (verbose > 2) {
323 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000324 safe_fname(fname), (double)st.st_size);
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000325 }
326
Wayne Davison6087ef22005-03-10 00:05:58 +0000327 write_item_attrs(f_out, i, iflags, fnamecmp_type, xname, xlen);
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000328 write_sum_head(f_out, s);
329
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000330 if (verbose > 2) {
331 rprintf(FINFO, "calling match_sums %s\n",
332 safe_fname(fname));
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000333 }
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000334
Wayne Davison8a9cfb22005-02-16 08:10:41 +0000335 if (log_before_transfer)
Wayne Davisonb694f8a2005-03-04 16:08:02 +0000336 log_item(file, &initial_stats, iflags, NULL);
Wayne Davison8a832462005-02-16 17:02:16 +0000337 else if (!am_server && verbose && do_progress)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000338 rprintf(FINFO, "%s\n", safe_fname(fname2));
339
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000340 set_compression(fname);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000341
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000342 match_sums(f_out, s, mbuf, st.st_size);
Wayne Davison0394e342005-03-05 17:51:23 +0000343 if (do_progress)
344 end_progress(st.st_size);
345
Wayne Davison8a9cfb22005-02-16 08:10:41 +0000346 if (!log_before_transfer)
Wayne Davisonb694f8a2005-03-04 16:08:02 +0000347 log_item(file, &initial_stats, iflags, NULL);
Martin Pool6902ed12001-08-14 02:04:47 +0000348
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000349 if (mbuf) {
350 j = unmap_file(mbuf);
351 if (j) {
352 io_error |= IOERR_GENERAL;
353 rsyserr(FERROR, j,
354 "read errors mapping %s",
355 full_fname(fname));
J.W. Schultze6e3f122003-08-15 00:57:27 +0000356 }
J.W. Schultze6e3f122003-08-15 00:57:27 +0000357 }
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000358 close(fd);
J.W. Schultze6e3f122003-08-15 00:57:27 +0000359
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000360 free_sums(s);
J.W. Schultze6e3f122003-08-15 00:57:27 +0000361
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000362 if (verbose > 2) {
363 rprintf(FINFO, "sender finished %s\n",
364 safe_fname(fname));
365 }
Wayne Davison95306d92005-02-20 01:04:54 +0000366
367 /* Flag that we actually sent this entry. */
368 file->flags |= FLAG_SENT;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000369 }
Wayne Davison8b115ac2004-09-07 21:32:36 +0000370 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000371
372 if (verbose > 2)
J.W. Schultze6e3f122003-08-15 00:57:27 +0000373 rprintf(FINFO, "send files finished\n");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000374
375 match_report();
376
J.W. Schultze6e3f122003-08-15 00:57:27 +0000377 write_int(f_out, -1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000378}