$treeview $search $mathjax $extrastylesheet
librsync
2.3.0
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- the library for network deltas 00004 * 00005 * Copyright 2000, 2001, 2014, 2015 by Martin Pool <mbp@sourcefrog.net> 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 00022 /*= 00023 | Is it possible that software is not 00024 | like anything else, that it is meant 00025 | to be discarded: that the whole point 00026 | is to always see it as a soap bubble? 00027 | -- Alan Perlis 00028 */ 00029 00030 #include "config.h" 00031 #include <assert.h> 00032 #include <stdlib.h> 00033 #include <stdio.h> 00034 #include <string.h> 00035 #include "librsync.h" 00036 #include "whole.h" 00037 #include "sumset.h" 00038 #include "job.h" 00039 #include "buf.h" 00040 00041 /** Whole file IO buffer sizes. */ 00042 LIBRSYNC_EXPORT int rs_inbuflen = 0, rs_outbuflen = 0; 00043 00044 /** Run a job continuously, with input to/from the two specified files. 00045 * 00046 * The job should already be set up, and must be freed by the caller after 00047 * return. If rs_inbuflen or rs_outbuflen are set, they will override the 00048 * inbuflen and outbuflen arguments. 00049 * 00050 * \param in_file - input file, or NULL if there is no input. 00051 * 00052 * \param out_file - output file, or NULL if there is no output. 00053 * 00054 * \param inbuflen - recommended input buffer size to use. 00055 * 00056 * \param outbuflen - recommended output buffer size to use. 00057 * 00058 * \return RS_DONE if the job completed, or otherwise an error result. */ 00059 rs_result rs_whole_run(rs_job_t *job, FILE *in_file, FILE *out_file, 00060 int inbuflen, int outbuflen) 00061 { 00062 rs_buffers_t buf; 00063 rs_result result; 00064 rs_filebuf_t *in_fb = NULL, *out_fb = NULL; 00065 00066 /* Override buffer sizes if rs_inbuflen or rs_outbuflen are set. */ 00067 inbuflen = rs_inbuflen ? rs_inbuflen : inbuflen; 00068 outbuflen = rs_outbuflen ? rs_outbuflen : outbuflen; 00069 if (in_file) 00070 in_fb = rs_filebuf_new(in_file, inbuflen); 00071 if (out_file) 00072 out_fb = rs_filebuf_new(out_file, outbuflen); 00073 result = 00074 rs_job_drive(job, &buf, in_fb ? rs_infilebuf_fill : NULL, in_fb, 00075 out_fb ? rs_outfilebuf_drain : NULL, out_fb); 00076 if (in_fb) 00077 rs_filebuf_free(in_fb); 00078 if (out_fb) 00079 rs_filebuf_free(out_fb); 00080 return result; 00081 } 00082 00083 rs_result rs_sig_file(FILE *old_file, FILE *sig_file, size_t block_len, 00084 size_t strong_len, rs_magic_number sig_magic, 00085 rs_stats_t *stats) 00086 { 00087 rs_job_t *job; 00088 rs_result r; 00089 rs_long_t old_fsize = rs_file_size(old_file); 00090 00091 if ((r = 00092 rs_sig_args(old_fsize, &sig_magic, &block_len, 00093 &strong_len)) != RS_DONE) 00094 return r; 00095 job = rs_sig_begin(block_len, strong_len, sig_magic); 00096 /* Size inbuf for 4 blocks, outbuf for header + 4 blocksums. */ 00097 r = rs_whole_run(job, old_file, sig_file, 4 * block_len, 00098 12 + 4 * (4 + strong_len)); 00099 if (stats) 00100 memcpy(stats, &job->stats, sizeof *stats); 00101 rs_job_free(job); 00102 00103 return r; 00104 } 00105 00106 rs_result rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, 00107 rs_stats_t *stats) 00108 { 00109 rs_job_t *job; 00110 rs_result r; 00111 00112 job = rs_loadsig_begin(sumset); 00113 /* Set filesize used to estimate signature size. */ 00114 job->sig_fsize = rs_file_size(sig_file); 00115 /* Size inbuf for 1024x 16 byte blocksums. */ 00116 r = rs_whole_run(job, sig_file, NULL, 1024 * 16, 0); 00117 if (stats) 00118 memcpy(stats, &job->stats, sizeof *stats); 00119 rs_job_free(job); 00120 00121 return r; 00122 } 00123 00124 rs_result rs_delta_file(rs_signature_t *sig, FILE *new_file, FILE *delta_file, 00125 rs_stats_t *stats) 00126 { 00127 rs_job_t *job; 00128 rs_result r; 00129 00130 job = rs_delta_begin(sig); 00131 /* Size inbuf for 1 block, outbuf for literal cmd + 4 blocks. */ 00132 r = rs_whole_run(job, new_file, delta_file, sig->block_len, 00133 10 + 4 * sig->block_len); 00134 if (stats) 00135 memcpy(stats, &job->stats, sizeof *stats); 00136 rs_job_free(job); 00137 return r; 00138 } 00139 00140 rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file, 00141 rs_stats_t *stats) 00142 { 00143 rs_job_t *job; 00144 rs_result r; 00145 00146 job = rs_patch_begin(rs_file_copy_cb, basis_file); 00147 /* Default size inbuf and outbuf 64K. */ 00148 r = rs_whole_run(job, delta_file, new_file, 64 * 1024, 64 * 1024); 00149 if (stats) 00150 memcpy(stats, &job->stats, sizeof *stats); 00151 rs_job_free(job); 00152 return r; 00153 }