| 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 | extern struct stats stats; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * @file |
| 35 | * |
| 36 | * The sender gets checksums from the generator, calculates deltas, |
| 37 | * and transmits them to the receiver. The sender process runs on the |
| 38 | * machine holding the source files. |
| 39 | **/ |
| 40 | void read_sum_head(int f, struct sum_struct *sum) |
| 41 | { |
| 42 | sum->count = read_int(f); |
| 43 | sum->blength = read_int(f); |
| 44 | if (protocol_version < 27) { |
| 45 | sum->s2length = csum_length; |
| 46 | } else { |
| 47 | sum->s2length = read_int(f); |
| 48 | if (sum->s2length > MD4_SUM_LENGTH) { |
| 49 | rprintf(FERROR, "Invalid checksum length %ld\n", |
| 50 | (long)sum->s2length); |
| 51 | exit_cleanup(RERR_PROTOCOL); |
| 52 | } |
| 53 | } |
| 54 | sum->remainder = read_int(f); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Receive the checksums for a buffer |
| 59 | **/ |
| 60 | static struct sum_struct *receive_sums(int f) |
| 61 | { |
| 62 | struct sum_struct *s; |
| 63 | int i; |
| 64 | OFF_T offset = 0; |
| 65 | |
| 66 | if (!(s = new(struct sum_struct))) |
| 67 | out_of_memory("receive_sums"); |
| 68 | |
| 69 | read_sum_head(f, s); |
| 70 | |
| 71 | s->sums = NULL; |
| 72 | |
| 73 | if (verbose > 3) { |
| 74 | rprintf(FINFO, "count=%ld n=%u rem=%u\n", |
| 75 | (long)s->count, s->blength, s->remainder); |
| 76 | } |
| 77 | |
| 78 | if (s->count == 0) |
| 79 | return(s); |
| 80 | |
| 81 | if (!(s->sums = new_array(struct sum_buf, s->count))) |
| 82 | out_of_memory("receive_sums"); |
| 83 | |
| 84 | for (i = 0; i < (int)s->count; i++) { |
| 85 | s->sums[i].sum1 = read_int(f); |
| 86 | read_buf(f, s->sums[i].sum2, s->s2length); |
| 87 | |
| 88 | s->sums[i].offset = offset; |
| 89 | s->sums[i].flags = 0; |
| 90 | |
| 91 | if (i == (int)s->count-1 && s->remainder != 0) |
| 92 | s->sums[i].len = s->remainder; |
| 93 | else |
| 94 | s->sums[i].len = s->blength; |
| 95 | offset += s->sums[i].len; |
| 96 | |
| 97 | if (verbose > 3) { |
| 98 | rprintf(FINFO, |
| 99 | "chunk[%d] len=%d offset=%.0f sum1=%08x\n", |
| 100 | i, s->sums[i].len, (double)s->sums[i].offset, |
| 101 | s->sums[i].sum1); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | s->flength = offset; |
| 106 | |
| 107 | return s; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | void send_files(struct file_list *flist, int f_out, int f_in) |
| 113 | { |
| 114 | int fd = -1; |
| 115 | struct sum_struct *s; |
| 116 | struct map_struct *mbuf = NULL; |
| 117 | STRUCT_STAT st; |
| 118 | char fname[MAXPATHLEN]; |
| 119 | int i; |
| 120 | struct file_struct *file; |
| 121 | int phase = 0; |
| 122 | struct stats initial_stats; |
| 123 | int j; |
| 124 | |
| 125 | if (verbose > 2) |
| 126 | rprintf(FINFO, "send_files starting\n"); |
| 127 | |
| 128 | while (1) { |
| 129 | unsigned int offset; |
| 130 | |
| 131 | i = read_int(f_in); |
| 132 | if (i == -1) { |
| 133 | if (phase == 0) { |
| 134 | phase++; |
| 135 | csum_length = SUM_LENGTH; |
| 136 | write_int(f_out, -1); |
| 137 | if (verbose > 2) |
| 138 | rprintf(FINFO, "send_files phase=%d\n", phase); |
| 139 | continue; |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | if (i < 0 || i >= flist->count) { |
| 145 | rprintf(FERROR, "Invalid file index %d (count=%d)\n", |
| 146 | i, flist->count); |
| 147 | exit_cleanup(RERR_PROTOCOL); |
| 148 | } |
| 149 | |
| 150 | file = flist->files[i]; |
| 151 | |
| 152 | stats.current_file_index = i; |
| 153 | stats.num_transferred_files++; |
| 154 | stats.total_transferred_size += file->length; |
| 155 | |
| 156 | if (file->basedir) { |
| 157 | /* N.B. We're sure that this fits, so offset is OK. */ |
| 158 | offset = strlcpy(fname, file->basedir, sizeof fname); |
| 159 | if (!offset || fname[offset-1] != '/') |
| 160 | fname[offset++] = '/'; |
| 161 | } else |
| 162 | offset = 0; |
| 163 | f_name_to(file, fname + offset); |
| 164 | |
| 165 | if (verbose > 2) |
| 166 | rprintf(FINFO, "send_files(%d, %s)\n", i, fname); |
| 167 | |
| 168 | if (dry_run) { |
| 169 | if (!am_server && verbose) { |
| 170 | rprintf(FINFO, "%s\n", fname+offset); |
| 171 | } |
| 172 | write_int(f_out, i); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | initial_stats = stats; |
| 177 | |
| 178 | s = receive_sums(f_in); |
| 179 | if (!s) { |
| 180 | io_error |= IOERR_GENERAL; |
| 181 | rprintf(FERROR, "receive_sums failed\n"); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | fd = do_open(fname, O_RDONLY, 0); |
| 186 | if (fd == -1) { |
| 187 | if (errno == ENOENT) { |
| 188 | enum logcode c = am_daemon |
| 189 | && protocol_version < 28 ? FERROR |
| 190 | : FINFO; |
| 191 | io_error |= IOERR_VANISHED; |
| 192 | rprintf(c, "file has vanished: %s\n", |
| 193 | full_fname(fname)); |
| 194 | } else { |
| 195 | io_error |= IOERR_GENERAL; |
| 196 | rsyserr(FERROR, errno, |
| 197 | "send_files failed to open %s", |
| 198 | full_fname(fname)); |
| 199 | } |
| 200 | free_sums(s); |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | /* map the local file */ |
| 205 | if (do_fstat(fd, &st) != 0) { |
| 206 | io_error |= IOERR_GENERAL; |
| 207 | rsyserr(FERROR, errno, "fstat failed"); |
| 208 | free_sums(s); |
| 209 | close(fd); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | mbuf = st.st_size ? map_file(fd, st.st_size) : NULL; |
| 214 | |
| 215 | if (verbose > 2) { |
| 216 | rprintf(FINFO, "send_files mapped %s of size %.0f\n", |
| 217 | fname, (double)st.st_size); |
| 218 | } |
| 219 | |
| 220 | write_int(f_out, i); |
| 221 | write_sum_head(f_out, s); |
| 222 | |
| 223 | if (verbose > 2) |
| 224 | rprintf(FINFO, "calling match_sums %s\n", fname); |
| 225 | |
| 226 | if (!am_server && verbose) { |
| 227 | rprintf(FINFO, "%s\n", fname+offset); |
| 228 | } |
| 229 | |
| 230 | set_compression(fname); |
| 231 | |
| 232 | match_sums(f_out, s, mbuf, st.st_size); |
| 233 | log_send(file, &initial_stats); |
| 234 | |
| 235 | if (mbuf) { |
| 236 | j = unmap_file(mbuf); |
| 237 | if (j) { |
| 238 | io_error |= IOERR_GENERAL; |
| 239 | rsyserr(FERROR, j, |
| 240 | "read errors mapping %s", |
| 241 | full_fname(fname)); |
| 242 | } |
| 243 | } |
| 244 | close(fd); |
| 245 | |
| 246 | free_sums(s); |
| 247 | |
| 248 | if (verbose > 2) |
| 249 | rprintf(FINFO, "sender finished %s\n", fname); |
| 250 | } |
| 251 | |
| 252 | if (verbose > 2) |
| 253 | rprintf(FINFO, "send files finished\n"); |
| 254 | |
| 255 | match_report(); |
| 256 | |
| 257 | write_int(f_out, -1); |
| 258 | } |