$treeview $search $mathjax $extrastylesheet
librsync
2.0.2
$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 00032 #include <assert.h> 00033 #include <stdlib.h> 00034 #ifdef HAVE_UNISTD_H 00035 # include <unistd.h> 00036 #endif 00037 #include <stdio.h> 00038 #include <string.h> 00039 #include <errno.h> 00040 00041 #include "librsync.h" 00042 00043 #include "trace.h" 00044 #include "fileutil.h" 00045 #include "sumset.h" 00046 #include "job.h" 00047 #include "buf.h" 00048 #include "whole.h" 00049 #include "util.h" 00050 00051 /** Whole file IO buffer sizes. */ 00052 int rs_inbuflen = 0, rs_outbuflen = 0; 00053 00054 /** Run a job continuously, with input to/from the two specified files. 00055 * 00056 * The job should already be set up, and must be freed by the caller after 00057 * return. If rs_inbuflen or rs_outbuflen are set, they will override the 00058 * inbuflen and outbuflen arguments. 00059 * 00060 * \param in_file - input file, or NULL if there is no input. 00061 * 00062 * \param out_file - output file, or NULL if there is no output. 00063 * 00064 * \param inbuflen - recommended input buffer size to use. 00065 * 00066 * \param outbuflen - recommended output buffer size to use. 00067 * 00068 * \return RS_DONE if the job completed, or otherwise an error result. */ 00069 rs_result rs_whole_run(rs_job_t *job, FILE *in_file, FILE *out_file, 00070 int inbuflen, int outbuflen) 00071 { 00072 rs_buffers_t buf; 00073 rs_result result; 00074 rs_filebuf_t *in_fb = NULL, *out_fb = NULL; 00075 00076 /* Override buffer sizes if rs_inbuflen or rs_outbuflen are set. */ 00077 inbuflen = rs_inbuflen ? rs_inbuflen : inbuflen; 00078 outbuflen = rs_outbuflen ? rs_outbuflen : outbuflen; 00079 if (in_file) 00080 in_fb = rs_filebuf_new(in_file, inbuflen); 00081 if (out_file) 00082 out_fb = rs_filebuf_new(out_file, outbuflen); 00083 result = 00084 rs_job_drive(job, &buf, in_fb ? rs_infilebuf_fill : NULL, in_fb, 00085 out_fb ? rs_outfilebuf_drain : NULL, out_fb); 00086 if (in_fb) 00087 rs_filebuf_free(in_fb); 00088 if (out_fb) 00089 rs_filebuf_free(out_fb); 00090 return result; 00091 } 00092 00093 rs_result rs_sig_file(FILE *old_file, FILE *sig_file, size_t new_block_len, 00094 size_t strong_len, rs_magic_number sig_magic, 00095 rs_stats_t *stats) 00096 { 00097 rs_job_t *job; 00098 rs_result r; 00099 00100 job = rs_sig_begin(new_block_len, strong_len, sig_magic); 00101 /* Size inbuf for 4 blocks, outbuf for header + 4 blocksums. */ 00102 r = rs_whole_run(job, old_file, sig_file, 4 * new_block_len, 00103 12 + 4 * (4 + strong_len)); 00104 if (stats) 00105 memcpy(stats, &job->stats, sizeof *stats); 00106 rs_job_free(job); 00107 00108 return r; 00109 } 00110 00111 rs_result rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, 00112 rs_stats_t *stats) 00113 { 00114 rs_job_t *job; 00115 rs_result r; 00116 00117 job = rs_loadsig_begin(sumset); 00118 /* Estimate a number of signatures by file size */ 00119 rs_get_filesize(sig_file, &job->sig_fsize); 00120 /* Size inbuf for 1024x 16 byte blocksums. */ 00121 r = rs_whole_run(job, sig_file, NULL, 1024 * 16, 0); 00122 if (stats) 00123 memcpy(stats, &job->stats, sizeof *stats); 00124 rs_job_free(job); 00125 00126 return r; 00127 } 00128 00129 rs_result rs_delta_file(rs_signature_t *sig, FILE *new_file, FILE *delta_file, 00130 rs_stats_t *stats) 00131 { 00132 rs_job_t *job; 00133 rs_result r; 00134 00135 job = rs_delta_begin(sig); 00136 /* Size inbuf for 1 block, outbuf for literal cmd + 4 blocks. */ 00137 r = rs_whole_run(job, new_file, delta_file, sig->block_len, 00138 10 + 4 * sig->block_len); 00139 if (stats) 00140 memcpy(stats, &job->stats, sizeof *stats); 00141 rs_job_free(job); 00142 return r; 00143 } 00144 00145 rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file, 00146 rs_stats_t *stats) 00147 { 00148 rs_job_t *job; 00149 rs_result r; 00150 00151 job = rs_patch_begin(rs_file_copy_cb, basis_file); 00152 /* Default size inbuf and outbuf 64K. */ 00153 r = rs_whole_run(job, delta_file, new_file, 64 * 1024, 64 * 1024); 00154 if (stats) 00155 memcpy(stats, &job->stats, sizeof *stats); 00156 rs_job_free(job); 00157 return r; 00158 }