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