sender.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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-2008 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 dry_run;
  24. extern int do_xfers;
  25. extern int am_server;
  26. extern int am_daemon;
  27. extern int inc_recurse;
  28. extern int log_before_transfer;
  29. extern int stdout_format_has_i;
  30. extern int logfile_format_has_i;
  31. extern int csum_length;
  32. extern int append_mode;
  33. extern int io_error;
  34. extern int allowed_lull;
  35. extern int preserve_xattrs;
  36. extern int protocol_version;
  37. extern int remove_source_files;
  38. extern int updating_basis_file;
  39. extern int make_backups;
  40. extern int do_progress;
  41. extern int inplace;
  42. extern int batch_fd;
  43. extern int write_batch;
  44. extern struct stats stats;
  45. extern struct file_list *cur_flist, *first_flist, *dir_flist;
  46. /**
  47. * @file
  48. *
  49. * The sender gets checksums from the generator, calculates deltas,
  50. * and transmits them to the receiver. The sender process runs on the
  51. * machine holding the source files.
  52. **/
  53. /**
  54. * Receive the checksums for a buffer
  55. **/
  56. static struct sum_struct *receive_sums(int f)
  57. {
  58. struct sum_struct *s;
  59. int32 i;
  60. int lull_mod = allowed_lull * 5;
  61. OFF_T offset = 0;
  62. if (!(s = new(struct sum_struct)))
  63. out_of_memory("receive_sums");
  64. read_sum_head(f, s);
  65. s->sums = NULL;
  66. if (verbose > 3) {
  67. rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
  68. (double)s->count, (long)s->blength, (long)s->remainder);
  69. }
  70. if (append_mode > 0) {
  71. s->flength = (OFF_T)s->count * s->blength;
  72. if (s->remainder)
  73. s->flength -= s->blength - s->remainder;
  74. return s;
  75. }
  76. if (s->count == 0)
  77. return(s);
  78. if (!(s->sums = new_array(struct sum_buf, s->count)))
  79. out_of_memory("receive_sums");
  80. for (i = 0; i < s->count; i++) {
  81. s->sums[i].sum1 = read_int(f);
  82. read_buf(f, s->sums[i].sum2, s->s2length);
  83. s->sums[i].offset = offset;
  84. s->sums[i].flags = 0;
  85. if (i == s->count-1 && s->remainder != 0)
  86. s->sums[i].len = s->remainder;
  87. else
  88. s->sums[i].len = s->blength;
  89. offset += s->sums[i].len;
  90. if (allowed_lull && !(i % lull_mod))
  91. maybe_send_keepalive();
  92. if (verbose > 3) {
  93. rprintf(FINFO,
  94. "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
  95. i, s->sums[i].len, (double)s->sums[i].offset,
  96. s->sums[i].sum1);
  97. }
  98. }
  99. s->flength = offset;
  100. return s;
  101. }
  102. void successful_send(int ndx)
  103. {
  104. char fname[MAXPATHLEN];
  105. struct file_struct *file;
  106. struct file_list *flist;
  107. if (!remove_source_files)
  108. return;
  109. flist = flist_for_ndx(ndx, "successful_send");
  110. file = flist->files[ndx - flist->ndx_start];
  111. if (!change_pathname(file, NULL, 0))
  112. return;
  113. f_name(file, fname);
  114. if (do_unlink(fname) == 0) {
  115. if (verbose > 1)
  116. rprintf(FINFO, "sender removed %s\n", fname);
  117. } else
  118. rsyserr(FERROR, errno, "sender failed to remove %s", fname);
  119. }
  120. static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
  121. const char *fname, struct file_struct *file,
  122. uchar fnamecmp_type, char *buf, int len)
  123. {
  124. write_ndx(f_out, ndx);
  125. if (protocol_version < 29)
  126. return;
  127. write_shortint(f_out, iflags);
  128. if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
  129. write_byte(f_out, fnamecmp_type);
  130. if (iflags & ITEM_XNAME_FOLLOWS)
  131. write_vstring(f_out, buf, len);
  132. #ifdef SUPPORT_XATTRS
  133. if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
  134. send_xattr_request(fname, file, f_out);
  135. #endif
  136. }
  137. void send_files(int f_in, int f_out)
  138. {
  139. int fd = -1;
  140. struct sum_struct *s;
  141. struct map_struct *mbuf = NULL;
  142. STRUCT_STAT st;
  143. char fname[MAXPATHLEN], xname[MAXPATHLEN];
  144. const char *path, *slash;
  145. uchar fnamecmp_type;
  146. int iflags, xlen;
  147. struct file_struct *file;
  148. int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
  149. struct stats initial_stats;
  150. int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
  151. enum logcode log_code = log_before_transfer ? FLOG : FINFO;
  152. int f_xfer = write_batch < 0 ? batch_fd : f_out;
  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 && !dry_run)
  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 (verbose > 2)
  313. rprintf(FINFO, "send files finished\n");
  314. match_report();
  315. write_ndx(f_out, NDX_DONE);
  316. }