sender.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Routines only used by the sending process.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2003-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. extern int verbose;
  23. extern int do_xfers;
  24. extern int am_server;
  25. extern int am_daemon;
  26. extern int inc_recurse;
  27. extern int log_before_transfer;
  28. extern int stdout_format_has_i;
  29. extern int logfile_format_has_i;
  30. extern int csum_length;
  31. extern int append_mode;
  32. extern int io_error;
  33. extern int allowed_lull;
  34. extern int preserve_xattrs;
  35. extern int protocol_version;
  36. extern int remove_source_files;
  37. extern int updating_basis_file;
  38. extern int make_backups;
  39. extern int do_progress;
  40. extern int inplace;
  41. extern int batch_fd;
  42. extern int write_batch;
  43. extern struct stats stats;
  44. extern struct file_list *cur_flist, *first_flist, *dir_flist;
  45. /**
  46. * @file
  47. *
  48. * The sender gets checksums from the generator, calculates deltas,
  49. * and transmits them to the receiver. The sender process runs on the
  50. * machine holding the source files.
  51. **/
  52. /**
  53. * Receive the checksums for a buffer
  54. **/
  55. static struct sum_struct *receive_sums(int f)
  56. {
  57. struct sum_struct *s;
  58. int32 i;
  59. int lull_mod = allowed_lull * 5;
  60. OFF_T offset = 0;
  61. if (!(s = new(struct sum_struct)))
  62. out_of_memory("receive_sums");
  63. read_sum_head(f, s);
  64. s->sums = NULL;
  65. if (verbose > 3) {
  66. rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
  67. (double)s->count, (long)s->blength, (long)s->remainder);
  68. }
  69. if (append_mode > 0) {
  70. s->flength = (OFF_T)s->count * s->blength;
  71. if (s->remainder)
  72. s->flength -= s->blength - s->remainder;
  73. return s;
  74. }
  75. if (s->count == 0)
  76. return(s);
  77. if (!(s->sums = new_array(struct sum_buf, s->count)))
  78. out_of_memory("receive_sums");
  79. for (i = 0; i < s->count; i++) {
  80. s->sums[i].sum1 = read_int(f);
  81. read_buf(f, s->sums[i].sum2, s->s2length);
  82. s->sums[i].offset = offset;
  83. s->sums[i].flags = 0;
  84. if (i == s->count-1 && s->remainder != 0)
  85. s->sums[i].len = s->remainder;
  86. else
  87. s->sums[i].len = s->blength;
  88. offset += s->sums[i].len;
  89. if (allowed_lull && !(i % lull_mod))
  90. maybe_send_keepalive();
  91. if (verbose > 3) {
  92. rprintf(FINFO,
  93. "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
  94. i, s->sums[i].len, (double)s->sums[i].offset,
  95. s->sums[i].sum1);
  96. }
  97. }
  98. s->flength = offset;
  99. return s;
  100. }
  101. void successful_send(int ndx)
  102. {
  103. char fname[MAXPATHLEN];
  104. struct file_struct *file;
  105. struct file_list *flist;
  106. if (!remove_source_files)
  107. return;
  108. flist = flist_for_ndx(ndx, "successful_send");
  109. file = flist->files[ndx - flist->ndx_start];
  110. if (!change_pathname(file, NULL, 0))
  111. return;
  112. f_name(file, fname);
  113. if (do_unlink(fname) == 0) {
  114. if (verbose > 1)
  115. rprintf(FINFO, "sender removed %s\n", fname);
  116. } else
  117. rsyserr(FERROR, errno, "sender failed to remove %s", fname);
  118. }
  119. static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
  120. const char *fname, struct file_struct *file,
  121. uchar fnamecmp_type, char *buf, int len)
  122. {
  123. write_ndx(f_out, ndx);
  124. if (protocol_version < 29)
  125. return;
  126. write_shortint(f_out, iflags);
  127. if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
  128. write_byte(f_out, fnamecmp_type);
  129. if (iflags & ITEM_XNAME_FOLLOWS)
  130. write_vstring(f_out, buf, len);
  131. #ifdef SUPPORT_XATTRS
  132. if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
  133. send_xattr_request(fname, file, f_out);
  134. #endif
  135. }
  136. void send_files(int f_in, int f_out)
  137. {
  138. int fd = -1;
  139. struct sum_struct *s;
  140. struct map_struct *mbuf = NULL;
  141. STRUCT_STAT st;
  142. char fname[MAXPATHLEN], xname[MAXPATHLEN];
  143. const char *path, *slash;
  144. uchar fnamecmp_type;
  145. int iflags, xlen;
  146. struct file_struct *file;
  147. int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
  148. struct stats initial_stats;
  149. int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
  150. enum logcode log_code = log_before_transfer ? FLOG : FINFO;
  151. int f_xfer = write_batch < 0 ? batch_fd : f_out;
  152. int save_io_error = io_error;
  153. int ndx, j;
  154. if (verbose > 2)
  155. rprintf(FINFO, "send_files starting\n");
  156. while (1) {
  157. if (inc_recurse)
  158. send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
  159. /* This call also sets cur_flist. */
  160. ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
  161. xname, &xlen);
  162. if (ndx == NDX_DONE) {
  163. if (inc_recurse && first_flist) {
  164. flist_free(first_flist);
  165. if (first_flist) {
  166. write_ndx(f_out, NDX_DONE);
  167. continue;
  168. }
  169. }
  170. if (++phase > max_phase)
  171. break;
  172. if (verbose > 2)
  173. rprintf(FINFO, "send_files phase=%d\n", phase);
  174. write_ndx(f_out, NDX_DONE);
  175. continue;
  176. }
  177. if (inc_recurse)
  178. send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
  179. if (ndx - cur_flist->ndx_start >= 0)
  180. file = cur_flist->files[ndx - cur_flist->ndx_start];
  181. else
  182. file = dir_flist->files[cur_flist->parent_ndx];
  183. if (F_PATHNAME(file)) {
  184. path = F_PATHNAME(file);
  185. slash = "/";
  186. } else {
  187. path = slash = "";
  188. }
  189. if (!change_pathname(file, NULL, 0))
  190. continue;
  191. f_name(file, fname);
  192. if (verbose > 2)
  193. rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
  194. #ifdef SUPPORT_XATTRS
  195. if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
  196. recv_xattr_request(file, f_in);
  197. #endif
  198. if (!(iflags & ITEM_TRANSFER)) {
  199. maybe_log_item(file, iflags, itemizing, xname);
  200. write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
  201. fnamecmp_type, xname, xlen);
  202. continue;
  203. }
  204. if (phase == 2) {
  205. rprintf(FERROR,
  206. "got transfer request in phase 2 [%s]\n",
  207. who_am_i());
  208. exit_cleanup(RERR_PROTOCOL);
  209. }
  210. if (file->flags & FLAG_FILE_SENT) {
  211. if (csum_length == SHORT_SUM_LENGTH) {
  212. /* For inplace: redo phase turns off the backup
  213. * flag so that we do a regular inplace send. */
  214. make_backups = -make_backups;
  215. append_mode = -append_mode;
  216. csum_length = SUM_LENGTH;
  217. }
  218. } else {
  219. if (csum_length != SHORT_SUM_LENGTH) {
  220. make_backups = -make_backups;
  221. append_mode = -append_mode;
  222. csum_length = SHORT_SUM_LENGTH;
  223. }
  224. }
  225. updating_basis_file = inplace && (protocol_version >= 29
  226. ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
  227. if (!am_server && do_progress)
  228. set_current_file_index(file, ndx);
  229. stats.num_transferred_files++;
  230. stats.total_transferred_size += F_LENGTH(file);
  231. if (!do_xfers) { /* log the transfer */
  232. log_item(FCLIENT, file, &stats, iflags, NULL);
  233. write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
  234. fnamecmp_type, xname, xlen);
  235. continue;
  236. }
  237. initial_stats = stats;
  238. if (!(s = receive_sums(f_in))) {
  239. io_error |= IOERR_GENERAL;
  240. rprintf(FERROR_XFER, "receive_sums failed\n");
  241. exit_cleanup(RERR_PROTOCOL);
  242. }
  243. fd = do_open(fname, O_RDONLY, 0);
  244. if (fd == -1) {
  245. if (errno == ENOENT) {
  246. enum logcode c = am_daemon
  247. && protocol_version < 28 ? FERROR
  248. : FWARNING;
  249. io_error |= IOERR_VANISHED;
  250. rprintf(c, "file has vanished: %s\n",
  251. full_fname(fname));
  252. } else {
  253. io_error |= IOERR_GENERAL;
  254. rsyserr(FERROR_XFER, errno,
  255. "send_files failed to open %s",
  256. full_fname(fname));
  257. }
  258. free_sums(s);
  259. if (protocol_version >= 30)
  260. send_msg_int(MSG_NO_SEND, ndx);
  261. continue;
  262. }
  263. /* map the local file */
  264. if (do_fstat(fd, &st) != 0) {
  265. io_error |= IOERR_GENERAL;
  266. rsyserr(FERROR_XFER, errno, "fstat failed");
  267. free_sums(s);
  268. close(fd);
  269. exit_cleanup(RERR_PROTOCOL);
  270. }
  271. if (st.st_size) {
  272. int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
  273. mbuf = map_file(fd, st.st_size, read_size, s->blength);
  274. } else
  275. mbuf = NULL;
  276. if (verbose > 2) {
  277. rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
  278. path,slash,fname, (double)st.st_size);
  279. }
  280. write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
  281. fnamecmp_type, xname, xlen);
  282. write_sum_head(f_xfer, s);
  283. if (verbose > 2)
  284. rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
  285. if (log_before_transfer)
  286. log_item(FCLIENT, file, &initial_stats, iflags, NULL);
  287. else if (!am_server && verbose && do_progress)
  288. rprintf(FCLIENT, "%s\n", fname);
  289. set_compression(fname);
  290. match_sums(f_xfer, s, mbuf, st.st_size);
  291. if (do_progress)
  292. end_progress(st.st_size);
  293. log_item(log_code, file, &initial_stats, iflags, NULL);
  294. if (mbuf) {
  295. j = unmap_file(mbuf);
  296. if (j) {
  297. io_error |= IOERR_GENERAL;
  298. rsyserr(FERROR_XFER, j,
  299. "read errors mapping %s",
  300. full_fname(fname));
  301. }
  302. }
  303. close(fd);
  304. free_sums(s);
  305. if (verbose > 2)
  306. rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
  307. /* Flag that we actually sent this entry. */
  308. file->flags |= FLAG_FILE_SENT;
  309. }
  310. if (make_backups < 0)
  311. make_backups = -make_backups;
  312. if (io_error != save_io_error && protocol_version >= 30)
  313. send_msg_int(MSG_IO_ERROR, io_error);
  314. if (verbose > 2)
  315. rprintf(FINFO, "send files finished\n");
  316. match_report();
  317. write_ndx(f_out, NDX_DONE);
  318. }