librsync  2.0.1
whole.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- the library for network deltas
4  *
5  * Copyright 2000, 2001, 2014, 2015 by Martin Pool <mbp@sourcefrog.net>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22  /*
23  | Is it possible that software is not
24  | like anything else, that it is meant
25  | to be discarded: that the whole point
26  | is to always see it as a soap bubble?
27  | -- Alan Perlis
28  */
29 
30 
31 
32 #include "config.h"
33 
34 #include <assert.h>
35 #include <stdlib.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
42 
43 #include "librsync.h"
44 
45 #include "trace.h"
46 #include "fileutil.h"
47 #include "sumset.h"
48 #include "job.h"
49 #include "buf.h"
50 #include "whole.h"
51 #include "util.h"
52 
53 /**
54  * Run a job continuously, with input to/from the two specified files.
55  * The job should already be set up, and must be free by the caller
56  * after return.
57  *
58  * Buffers of ::rs_inbuflen and ::rs_outbuflen are allocated for
59  * temporary storage.
60  *
61  * \param in_file Source of input bytes, or NULL if the input buffer
62  * should not be filled.
63  *
64  * \return RS_DONE if the job completed, or otherwise an error result.
65  */
67 rs_whole_run(rs_job_t *job, FILE *in_file, FILE *out_file)
68 {
69  rs_buffers_t buf;
70  rs_result result;
71  rs_filebuf_t *in_fb = NULL, *out_fb = NULL;
72 
73  if (in_file)
74  in_fb = rs_filebuf_new(in_file, rs_inbuflen);
75 
76  if (out_file)
77  out_fb = rs_filebuf_new(out_file, rs_outbuflen);
78 
79  result = rs_job_drive(job, &buf,
80  in_fb ? rs_infilebuf_fill : NULL, in_fb,
81  out_fb ? rs_outfilebuf_drain : NULL, out_fb);
82 
83  if (in_fb)
84  rs_filebuf_free(in_fb);
85 
86  if (out_fb)
87  rs_filebuf_free(out_fb);
88 
89  return result;
90 }
91 
92 
93 
95 rs_sig_file(FILE *old_file, FILE *sig_file, size_t new_block_len,
96  size_t strong_len,
97  rs_magic_number sig_magic,
98  rs_stats_t *stats)
99 {
100  rs_job_t *job;
101  rs_result r;
102 
103  job = rs_sig_begin(new_block_len, strong_len, sig_magic);
104  r = rs_whole_run(job, old_file, sig_file);
105  if (stats)
106  memcpy(stats, &job->stats, sizeof *stats);
107  rs_job_free(job);
108 
109  return r;
110 }
111 
112 
113 rs_result
114 rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, rs_stats_t *stats)
115 {
116  rs_job_t *job;
117  rs_result r;
118 
119  job = rs_loadsig_begin(sumset);
120 
121  /* Estimate a number of signatures by file size */
122  rs_get_filesize(sig_file, &job->sig_fsize);
123 
124  r = rs_whole_run(job, sig_file, NULL);
125  if (stats)
126  memcpy(stats, &job->stats, sizeof *stats);
127  rs_job_free(job);
128 
129  return r;
130 }
131 
132 
133 
134 rs_result
135 rs_delta_file(rs_signature_t *sig, FILE *new_file, FILE *delta_file,
136  rs_stats_t *stats)
137 {
138  rs_job_t *job;
139  rs_result r;
140 
141  job = rs_delta_begin(sig);
142 
143  r = rs_whole_run(job, new_file, delta_file);
144 
145  if (stats)
146  memcpy(stats, &job->stats, sizeof *stats);
147 
148  rs_job_free(job);
149 
150  return r;
151 }
152 
153 
154 
155 rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file,
156  rs_stats_t *stats)
157 {
158  rs_job_t *job;
159  rs_result r;
160 
161  job = rs_patch_begin(rs_file_copy_cb, basis_file);
162 
163  r = rs_whole_run(job, delta_file, new_file);
164 
165  if (stats)
166  memcpy(stats, &job->stats, sizeof *stats);
167 
168  rs_job_free(job);
169 
170  return r;
171 }
Description of input and output buffers.
Definition: librsync.h:361
Definition: buf.c:69
rs_result rs_delta_file(rs_signature_t *, FILE *new_file, FILE *delta_file, rs_stats_t *)
Generate a delta between a signature and a new file, int a delta file.
Definition: whole.c:135
rs_job_t * rs_sig_begin(size_t new_block_len, size_t strong_sum_len, rs_magic_number sig_magic)
Start generating a signature.
Definition: mksum.c:143
int rs_inbuflen
Buffer sizes for file IO.
Definition: buf.c:66
rs_result rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, rs_stats_t *stats)
Load signatures from a signature file into memory.
Definition: whole.c:114
rs_stats_t stats
Encoding statistics.
Definition: job.h:75
rs_long_t sig_fsize
The size of the signature file if available.
Definition: job.h:51
Public header for librsync.
Signature of a whole file.
Definition: sumset.h:39
rs_result rs_sig_file(FILE *old_file, FILE *sig_file, size_t block_len, size_t strong_len, rs_magic_number sig_magic, rs_stats_t *stats)
Generate the signature of a basis file, and write it out to another.
Definition: whole.c:95
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:236
rs_job_t * rs_patch_begin(rs_copy_cb *copy_cb, void *copy_arg)
Apply a delta to a basis file to recreate the new file.
Definition: patch.c:287
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:193
rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file, rs_stats_t *)
Apply a patch, relative to a basis, into a new file.
Definition: whole.c:155
rs_job_t * rs_delta_begin(rs_signature_t *)
Prepare to compute a streaming delta.
Definition: delta.c:430
rs_result rs_file_copy_cb(void *arg, rs_long_t pos, size_t *len, void **buf)
rs_copy_cb that reads from a stdio file.
Definition: buf.c:206
rs_magic_number
A uint32 magic number, emitted in bigendian/network order at the start of librsync files...
Definition: librsync.h:76
rs_result rs_job_free(rs_job_t *job)
Deallocate job state.
Definition: job.c:90
rs_result rs_job_drive(rs_job_t *job, rs_buffers_t *buf, rs_driven_cb in_cb, void *in_opaque, rs_driven_cb out_cb, void *out_opaque)
Actively process a job, by making callbacks to fill and empty the buffers until the job is done...
Definition: job.c:215
rs_job_t * rs_loadsig_begin(rs_signature_t **)
Read a signature from a file into an rs_signature structure in memory.
Definition: readsums.c:151
The contents of this structure are private.
Definition: job.h:29