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