| 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 csum_length; |
| 24 | extern struct stats stats; |
| 25 | extern int io_error; |
| 26 | extern int dry_run; |
| 27 | extern int am_server; |
| 28 | extern int am_daemon; |
| 29 | extern int protocol_version; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * @file |
| 34 | * |
| 35 | * The sender gets checksums from the generator, calculates deltas, |
| 36 | * and transmits them to the receiver. The sender process runs on the |
| 37 | * machine holding the source files. |
| 38 | **/ |
| 39 | void read_sum_head(int f, struct sum_struct *sum) |
| 40 | { |
| 41 | sum->count = read_int(f); |
| 42 | sum->blength = read_int(f); |
| 43 | if (protocol_version < 27) { |
| 44 | sum->s2length = csum_length; |
| 45 | } else { |
| 46 | sum->s2length = read_int(f); |
| 47 | if (sum->s2length > MD4_SUM_LENGTH) { |
| 48 | rprintf(FERROR, "Invalid checksum length %ld\n", |
| 49 | (long)sum->s2length); |
| 50 | exit_cleanup(RERR_PROTOCOL); |
| 51 | } |
| 52 | } |
| 53 | sum->remainder = read_int(f); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Receive the checksums for a buffer |
| 58 | **/ |
| 59 | static struct sum_struct *receive_sums(int f) |
| 60 | { |
| 61 | struct sum_struct *s; |
| 62 | int i; |
| 63 | OFF_T offset = 0; |
| 64 | |
| 65 | s = new(struct sum_struct); |
| 66 | if (!s) out_of_memory("receive_sums"); |
| 67 | |
| 68 | read_sum_head(f, s); |
| 69 | |
| 70 | s->sums = NULL; |
| 71 | |
| 72 | if (verbose > 3) { |
| 73 | rprintf(FINFO, "count=%ld n=%u rem=%u\n", |
| 74 | (long)s->count, s->blength, s->remainder); |
| 75 | } |
| 76 | |
| 77 | if (s->count == 0) |
| 78 | return(s); |
| 79 | |
| 80 | s->sums = new_array(struct sum_buf, s->count); |
| 81 | if (!s->sums) out_of_memory("receive_sums"); |
| 82 | |
| 83 | for (i = 0; i < (int) s->count; i++) { |
| 84 | s->sums[i].sum1 = read_int(f); |
| 85 | read_buf(f, s->sums[i].sum2, s->s2length); |
| 86 | |
| 87 | s->sums[i].offset = offset; |
| 88 | s->sums[i].i = i; |
| 89 | |
| 90 | if (i == (int) s->count-1 && s->remainder != 0) { |
| 91 | s->sums[i].len = s->remainder; |
| 92 | } else { |
| 93 | s->sums[i].len = s->blength; |
| 94 | } |
| 95 | offset += s->sums[i].len; |
| 96 | |
| 97 | if (verbose > 3) |
| 98 | rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n", |
| 99 | i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1); |
| 100 | } |
| 101 | |
| 102 | s->flength = offset; |
| 103 | |
| 104 | return s; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | void send_files(struct file_list *flist, int f_out, int f_in) |
| 110 | { |
| 111 | int fd = -1; |
| 112 | struct sum_struct *s; |
| 113 | struct map_struct *buf = NULL; |
| 114 | STRUCT_STAT st; |
| 115 | char fname[MAXPATHLEN]; |
| 116 | int i; |
| 117 | struct file_struct *file; |
| 118 | int phase = 0; |
| 119 | extern struct stats stats; |
| 120 | struct stats initial_stats; |
| 121 | extern int write_batch; |
| 122 | extern int read_batch; |
| 123 | int checksums_match; |
| 124 | int buff_len; |
| 125 | char buff[CHUNK_SIZE]; |
| 126 | int j; |
| 127 | int done; |
| 128 | |
| 129 | if (verbose > 2) |
| 130 | rprintf(FINFO, "send_files starting\n"); |
| 131 | |
| 132 | while (1) { |
| 133 | unsigned int offset; |
| 134 | |
| 135 | i = read_int(f_in); |
| 136 | if (i == -1) { |
| 137 | if (phase == 0) { |
| 138 | phase++; |
| 139 | csum_length = SUM_LENGTH; |
| 140 | write_int(f_out, -1); |
| 141 | if (verbose > 2) |
| 142 | rprintf(FINFO, "send_files phase=%d\n", phase); |
| 143 | continue; |
| 144 | } |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (i < 0 || i >= flist->count) { |
| 149 | rprintf(FERROR, "Invalid file index %d (count=%d)\n", |
| 150 | i, flist->count); |
| 151 | exit_cleanup(RERR_PROTOCOL); |
| 152 | } |
| 153 | |
| 154 | file = flist->files[i]; |
| 155 | |
| 156 | stats.current_file_index = i; |
| 157 | stats.num_transferred_files++; |
| 158 | stats.total_transferred_size += file->length; |
| 159 | |
| 160 | fname[0] = 0; |
| 161 | if (file->basedir) { |
| 162 | offset = stringjoin(fname, sizeof fname, |
| 163 | file->basedir, "/", NULL); |
| 164 | } else |
| 165 | offset = 0; |
| 166 | f_name_to(file, fname + offset); |
| 167 | |
| 168 | if (verbose > 2) |
| 169 | rprintf(FINFO, "send_files(%d, %s)\n", i, fname); |
| 170 | |
| 171 | if (dry_run) { |
| 172 | if (!am_server && verbose) { /* log transfer */ |
| 173 | rprintf(FINFO, "%s\n", fname+offset); |
| 174 | } |
| 175 | write_int(f_out, i); |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | initial_stats = stats; |
| 180 | |
| 181 | s = receive_sums(f_in); |
| 182 | if (!s) { |
| 183 | io_error |= IOERR_GENERAL; |
| 184 | rprintf(FERROR, "receive_sums failed\n"); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (write_batch) |
| 189 | write_batch_csum_info(&i, s); |
| 190 | |
| 191 | if (!read_batch) { |
| 192 | fd = do_open(fname, O_RDONLY, 0); |
| 193 | if (fd == -1) { |
| 194 | if (errno == ENOENT) { |
| 195 | enum logcode c = am_daemon |
| 196 | && protocol_version < 28 ? FERROR |
| 197 | : FINFO; |
| 198 | io_error |= IOERR_VANISHED; |
| 199 | rprintf(c, "file has vanished: %s\n", |
| 200 | full_fname(fname)); |
| 201 | } else { |
| 202 | io_error |= IOERR_GENERAL; |
| 203 | rprintf(FERROR, "send_files failed to open %s: %s\n", |
| 204 | full_fname(fname), strerror(errno)); |
| 205 | } |
| 206 | free_sums(s); |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | /* map the local file */ |
| 211 | if (do_fstat(fd, &st) != 0) { |
| 212 | io_error |= IOERR_GENERAL; |
| 213 | rprintf(FERROR, "fstat failed: %s\n", strerror(errno)); |
| 214 | free_sums(s); |
| 215 | close(fd); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | if (st.st_size > 0) { |
| 220 | buf = map_file(fd, st.st_size); |
| 221 | } else { |
| 222 | buf = NULL; |
| 223 | } |
| 224 | |
| 225 | if (verbose > 2) |
| 226 | rprintf(FINFO, "send_files mapped %s of size %.0f\n", |
| 227 | fname, (double)st.st_size); |
| 228 | |
| 229 | write_int(f_out, i); |
| 230 | |
| 231 | if (write_batch) |
| 232 | write_batch_delta_file((char *)&i, sizeof i); |
| 233 | |
| 234 | write_sum_head(f_out, s); |
| 235 | } |
| 236 | |
| 237 | if (verbose > 2 && !read_batch) |
| 238 | rprintf(FINFO, "calling match_sums %s\n", fname); |
| 239 | |
| 240 | if (!am_server && verbose) { /* log transfer */ |
| 241 | rprintf(FINFO, "%s\n", fname+offset); |
| 242 | } |
| 243 | |
| 244 | set_compression(fname); |
| 245 | |
| 246 | if (read_batch) { |
| 247 | /* read checksums originally computed on sender side */ |
| 248 | read_batch_csum_info(i, s, &checksums_match); |
| 249 | if (checksums_match) { |
| 250 | read_batch_delta_file((char*)&j, sizeof (int)); |
| 251 | if (j != i) { /* if flist index entries don't match*/ |
| 252 | rprintf(FINFO, "index mismatch in send_files\n"); |
| 253 | rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i); |
| 254 | close_batch_delta_file(); |
| 255 | close_batch_csums_file(); |
| 256 | exit_cleanup(1); |
| 257 | } else { |
| 258 | write_int(f_out, j); |
| 259 | write_sum_head(f_out, s); |
| 260 | done = 0; |
| 261 | while (!done) { |
| 262 | read_batch_delta_file((char*)&buff_len, sizeof (int)); |
| 263 | write_int(f_out, buff_len); |
| 264 | if (buff_len == 0) { |
| 265 | done = 1; |
| 266 | } else { |
| 267 | if (buff_len > 0) { |
| 268 | read_batch_delta_file(buff, buff_len); |
| 269 | write_buf(f_out, buff, buff_len); |
| 270 | } |
| 271 | } |
| 272 | } /* end while */ |
| 273 | read_batch_delta_file( buff, MD4_SUM_LENGTH); |
| 274 | write_buf(f_out, buff, MD4_SUM_LENGTH); |
| 275 | |
| 276 | } /* j=i */ |
| 277 | } else { /* not checksum match */ |
| 278 | rprintf (FINFO, "readbatch & checksums don't match\n"); |
| 279 | rprintf (FINFO, "filename=%s is being skipped\n", fname); |
| 280 | continue; |
| 281 | } |
| 282 | } else { |
| 283 | match_sums(f_out, s, buf, st.st_size); |
| 284 | log_send(file, &initial_stats); |
| 285 | } |
| 286 | |
| 287 | if (!read_batch) { |
| 288 | if (buf) { |
| 289 | j = unmap_file(buf); |
| 290 | if (j) { |
| 291 | io_error |= IOERR_GENERAL; |
| 292 | rprintf(FERROR, |
| 293 | "read errors mapping %s: (%d) %s\n", |
| 294 | full_fname(fname), j, strerror(j)); |
| 295 | } |
| 296 | } |
| 297 | close(fd); |
| 298 | } |
| 299 | |
| 300 | free_sums(s); |
| 301 | |
| 302 | if (verbose > 2) |
| 303 | rprintf(FINFO, "sender finished %s\n", fname); |
| 304 | } |
| 305 | |
| 306 | if (verbose > 2) |
| 307 | rprintf(FINFO, "send files finished\n"); |
| 308 | |
| 309 | match_report(); |
| 310 | |
| 311 | write_int(f_out, -1); |
| 312 | if (write_batch || read_batch) { |
| 313 | close_batch_csums_file(); |
| 314 | close_batch_delta_file(); |
| 315 | } |
| 316 | |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |