54 static int block_len = 0;
55 static int strong_len = 0;
57 static int show_stats = 0;
59 static int bzip2_level = 0;
60 static int gzip_level = 0;
61 static int file_force = 0;
64 OPT_GZIP = 1069, OPT_BZIP2
68 char *rs_rollsum_name;
70 static void rdiff_usage(
const char *error, ...)
76 vsnprintf(buf,
sizeof(buf), error, va);
78 fprintf(stderr,
"rdiff: %s\n\nTry `rdiff --help' for more information.\n",
82 static void rdiff_no_more_args(poptContext opcon)
84 if (poptGetArg(opcon)) {
85 rdiff_usage(
"Too many arguments.");
90 static void bad_option(poptContext opcon,
int error)
92 rdiff_usage(
"%s: %s", poptStrerror(error), poptBadOption(opcon, 0));
96 static void help(
void)
98 printf(
"Usage: rdiff [OPTIONS] signature [BASIS [SIGNATURE]]\n"
99 " [OPTIONS] delta SIGNATURE [NEWFILE [DELTA]]\n"
100 " [OPTIONS] patch BASIS [DELTA [NEWFILE]]\n" "\n"
102 " -v, --verbose Trace internal processing\n"
103 " -V, --version Show program version\n"
104 " -?, --help Show this help message\n"
105 " -s, --statistics Show performance statistics\n"
106 " -f, --force Force overwriting existing files\n"
107 "Signature generation options:\n"
108 " -H, --hash=ALG Hash algorithm: blake2 (default), md4\n"
109 " -R, --rollsum=ALG Rollsum algorithm: rabinkarp (default), rollsum\n"
110 "Delta-encoding options:\n"
111 " -b, --block-size=BYTES Signature block size, 0 (default) for recommended\n"
112 " -S, --sum-size=BYTES Signature strength, 0 (default) for max, -1 for min\n"
113 "IO options:\n" " -I, --input-size=BYTES Input buffer size\n"
114 " -O, --output-size=BYTES Output buffer size\n"
115 " -z, --gzip[=LEVEL] gzip-compress deltas\n"
116 " -i, --bzip2[=LEVEL] bzip2-compress deltas\n");
119 static void rdiff_show_version(
void)
121 char const *bzlib =
"", *zlib =
"", *trace =
"";
135 trace =
", trace disabled";
138 printf(
"rdiff (%s)\n"
139 "Copyright (C) 1997-2016 by Martin Pool, Andrew Tridgell and others.\n"
140 "http://librsync.sourcefrog.net/\n"
141 "Capabilities: %ld bit files%s%s%s\n" "\n"
142 "librsync comes with NO WARRANTY, to the extent permitted by law.\n"
143 "You may redistribute copies of librsync under the terms of the GNU\n"
144 "Lesser General Public License. For more information about these\n"
146 (
long)(8 *
sizeof(rs_long_t)), zlib, bzlib, trace);
149 static void rdiff_options(poptContext opcon)
154 while ((c = poptGetNextOpt(opcon)) != -1) {
160 rdiff_show_version();
164 fprintf(stderr,
"rdiff: Library does not support trace.\n");
171 if ((a = poptGetOptArg(opcon))) {
183 rdiff_usage(
"Sorry, compression is not implemented yet.");
187 bad_option(opcon, c);
195 FILE *basis_file, *sig_file;
200 basis_file =
rs_file_open(poptGetArg(opcon),
"rb", file_force);
201 sig_file =
rs_file_open(poptGetArg(opcon),
"wb", file_force);
203 rdiff_no_more_args(opcon);
205 if (!rs_hash_name || !strcmp(rs_hash_name,
"blake2")) {
207 }
else if (!strcmp(rs_hash_name,
"md4")) {
210 rdiff_usage(
"Unknown hash algorithm '%s'.", rs_hash_name);
213 if (!rs_rollsum_name || !strcmp(rs_rollsum_name,
"rabinkarp")) {
216 }
else if (strcmp(rs_rollsum_name,
"rollsum")) {
217 rdiff_usage(
"Unknown rollsum algorithm '%s'.", rs_rollsum_name);
222 rs_sig_file(basis_file, sig_file, block_len, strong_len, sig_magic,
236 static rs_result rdiff_delta(poptContext opcon)
238 FILE *sig_file, *new_file, *delta_file;
239 char const *sig_name;
244 if (!(sig_name = poptGetArg(opcon))) {
245 rdiff_usage(
"Usage for delta: "
246 "rdiff [OPTIONS] delta SIGNATURE [NEWFILE [DELTA]]");
251 new_file =
rs_file_open(poptGetArg(opcon),
"rb", file_force);
252 delta_file =
rs_file_open(poptGetArg(opcon),
"wb", file_force);
254 rdiff_no_more_args(opcon);
266 result =
rs_delta_file(sumset, new_file, delta_file, &stats);
282 static rs_result rdiff_patch(poptContext opcon)
285 FILE *basis_file, *delta_file, *new_file;
286 char const *basis_name;
290 if (!(basis_name = poptGetArg(opcon))) {
291 rdiff_usage(
"Usage for patch: "
292 "rdiff [OPTIONS] patch BASIS [DELTA [NEW]]");
296 basis_file =
rs_file_open(basis_name,
"rb", file_force);
297 delta_file =
rs_file_open(poptGetArg(opcon),
"rb", file_force);
298 new_file =
rs_file_open(poptGetArg(opcon),
"wb", file_force);
300 rdiff_no_more_args(opcon);
302 result =
rs_patch_file(basis_file, delta_file, new_file, &stats);
314 static rs_result rdiff_action(poptContext opcon)
318 action = poptGetArg(opcon);
320 else if (isprefix(action,
"signature"))
322 else if (isprefix(action,
"delta"))
323 return rdiff_delta(opcon);
324 else if (isprefix(action,
"patch"))
325 return rdiff_patch(opcon);
328 (
"You must specify an action: `signature', `delta', or `patch'.");
332 int main(
const int argc,
const char *argv[])
335 const struct poptOption opts[] = {
336 {
"verbose",
'v', POPT_ARG_NONE, 0,
'v'},
337 {
"version",
'V', POPT_ARG_NONE, 0,
'V'},
339 {
"output-size",
'O', POPT_ARG_INT, &rs_outbuflen},
340 {
"hash",
'H', POPT_ARG_STRING, &rs_hash_name},
341 {
"rollsum",
'R', POPT_ARG_STRING, &rs_rollsum_name},
342 {
"help",
'?', POPT_ARG_NONE, 0,
'h'},
343 {0,
'h', POPT_ARG_NONE, 0,
'h'},
344 {
"block-size",
'b', POPT_ARG_INT, &block_len},
345 {
"sum-size",
'S', POPT_ARG_INT, &strong_len},
346 {
"statistics",
's', POPT_ARG_NONE, &show_stats},
347 {
"stats", 0, POPT_ARG_NONE, &show_stats},
348 {
"gzip",
'z', POPT_ARG_NONE, 0, OPT_GZIP},
349 {
"bzip2",
'i', POPT_ARG_NONE, 0, OPT_BZIP2},
350 {
"force",
'f', POPT_ARG_NONE, &file_force},
357 opcon = poptGetContext(
"rdiff", argc, argv, opts, 0);
358 rdiff_options(opcon);
359 result = rdiff_action(opcon);
362 fprintf(stderr,
"rdiff: Failed, %s.\n",
rs_strerror(result));
364 poptFreeContext(opcon);
Command line syntax error.
LIBRSYNC_EXPORT 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 into a delta file.
A signature file using the BLAKE2 hash.
LIBRSYNC_EXPORT int rs_log_stats(rs_stats_t const *stats)
Write statistics into the current log as text.
LIBRSYNC_EXPORT 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.
LIBRSYNC_EXPORT int rs_inbuflen
Buffer sizes for file IO.
LIBRSYNC_EXPORT char const * rs_strerror(rs_result r)
Return an English description of a rs_result value.
A signature file with MD4 signatures.
Public header for librsync.
LIBRSYNC_EXPORT void rs_trace_set_level(rs_loglevel level)
Set the least important message severity that will be output.
LIBRSYNC_EXPORT int rs_file_close(FILE *file)
Close a file with special handling for stdin or stdout.
Signature of a whole file.
LIBRSYNC_EXPORT void rs_free_sumset(rs_signature_t *)
Deep deallocation of checksums.
LIBRSYNC_EXPORT rs_result rs_build_hash_table(rs_signature_t *sums)
Call this after loading a signature to index it.
Performance statistics from a librsync encoding or decoding operation.
rs_result
Return codes from nonblocking rsync operations.
LIBRSYNC_EXPORT int rs_supports_trace(void)
Check whether the library was compiled with debugging trace.
LIBRSYNC_EXPORT rs_result rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, rs_stats_t *stats)
Load signatures from a signature file into memory.
static rs_result rdiff_sig(poptContext opcon)
Generate signature from remaining command line arguments.
LIBRSYNC_EXPORT FILE * rs_file_open(char const *filename, char const *mode, int force)
Open a file with special handling for stdin or stdout.
LIBRSYNC_EXPORT 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.
rs_magic_number
A uint32 magic number, emitted in bigendian/network order at the start of librsync files...
LIBRSYNC_EXPORT void rs_signature_log_stats(rs_signature_t const *sig)
Log the rs_signature_delta match stats.
LIBRSYNC_EXPORT char const rs_librsync_version[]
Library version string.