55static int block_len = 0;
56static int strong_len = 0;
58static int show_stats = 0;
60static int bzip2_level = 0;
61static int gzip_level = 0;
62static int file_force = 0;
65 OPT_GZIP = 1069, OPT_BZIP2
71static void rdiff_usage(
const char *error, ...)
77 vsnprintf(buf,
sizeof(buf), error, va);
79 fprintf(stderr,
"rdiff: %s\n\nTry `rdiff --help' for more information.\n",
83static void rdiff_no_more_args(poptContext opcon)
85 if (poptGetArg(opcon)) {
86 rdiff_usage(
"Too many arguments.");
91static void bad_option(poptContext opcon,
int error)
93 rdiff_usage(
"%s: %s", poptStrerror(error), poptBadOption(opcon, 0));
99 printf(
"Usage: rdiff [OPTIONS] signature [BASIS [SIGNATURE]]\n"
100 " [OPTIONS] delta SIGNATURE [NEWFILE [DELTA]]\n"
101 " [OPTIONS] patch BASIS [DELTA [NEWFILE]]\n" "\n"
103 " -v, --verbose Trace internal processing\n"
104 " -V, --version Show program version\n"
105 " -?, --help Show this help message\n"
106 " -s, --statistics Show performance statistics\n"
107 " -f, --force Force overwriting existing files\n"
108 "Signature generation options:\n"
109 " -H, --hash=ALG Hash algorithm: blake2 (default), md4\n"
110 " -R, --rollsum=ALG Rollsum algorithm: rabinkarp (default), rollsum\n"
111 "Delta-encoding options:\n"
112 " -b, --block-size=BYTES Signature block size, 0 (default) for recommended\n"
113 " -S, --sum-size=BYTES Signature strength, 0 (default) for max, -1 for min\n"
114 "IO options:\n" " -I, --input-size=BYTES Input buffer size\n"
115 " -O, --output-size=BYTES Output buffer size\n"
116 " -z, --gzip[=LEVEL] gzip-compress deltas\n"
117 " -i, --bzip2[=LEVEL] bzip2-compress deltas\n");
120static void rdiff_show_version(
void)
122 char const *bzlib =
"", *zlib =
"", *trace =
"";
136 trace =
", trace disabled";
139 printf(
"rdiff (%s)\n"
140 "Copyright (C) 1997-2016 by Martin Pool, Andrew Tridgell and others.\n"
141 "http://librsync.sourcefrog.net/\n"
142 "Capabilities: %ld bit files%s%s%s\n" "\n"
143 "librsync comes with NO WARRANTY, to the extent permitted by law.\n"
144 "You may redistribute copies of librsync under the terms of the GNU\n"
145 "Lesser General Public License. For more information about these\n"
147 (
long)(8 *
sizeof(rs_long_t)), zlib, bzlib, trace);
150static void rdiff_options(poptContext opcon)
155 while ((c = poptGetNextOpt(opcon)) != -1) {
161 rdiff_show_version();
165 fprintf(stderr,
"rdiff: Library does not support trace.\n");
172 if ((a = poptGetOptArg(opcon))) {
184 rdiff_usage(
"Sorry, compression is not implemented yet.");
188 bad_option(opcon, c);
196 FILE *basis_file, *sig_file;
201 basis_file =
rs_file_open(poptGetArg(opcon),
"rb", file_force);
202 sig_file =
rs_file_open(poptGetArg(opcon),
"wb", file_force);
204 rdiff_no_more_args(opcon);
206 if (!rs_hash_name || !strcmp(rs_hash_name,
"blake2")) {
208 }
else if (!strcmp(rs_hash_name,
"md4")) {
211 rdiff_usage(
"Unknown hash algorithm '%s'.", rs_hash_name);
214 if (!rs_rollsum_name || !strcmp(rs_rollsum_name,
"rabinkarp")) {
217 }
else if (strcmp(rs_rollsum_name,
"rollsum")) {
218 rdiff_usage(
"Unknown rollsum algorithm '%s'.", rs_rollsum_name);
223 rs_sig_file(basis_file, sig_file, block_len, strong_len, sig_magic,
237static rs_result rdiff_delta(poptContext opcon)
239 FILE *sig_file, *new_file, *delta_file;
240 char const *sig_name;
245 if (!(sig_name = poptGetArg(opcon))) {
246 rdiff_usage(
"Usage for delta: "
247 "rdiff [OPTIONS] delta SIGNATURE [NEWFILE [DELTA]]");
252 new_file =
rs_file_open(poptGetArg(opcon),
"rb", file_force);
253 delta_file =
rs_file_open(poptGetArg(opcon),
"wb", file_force);
255 rdiff_no_more_args(opcon);
267 result =
rs_delta_file(sumset, new_file, delta_file, &stats);
283static rs_result rdiff_patch(poptContext opcon)
286 FILE *basis_file, *delta_file, *new_file;
287 char const *basis_name;
291 if (!(basis_name = poptGetArg(opcon))) {
292 rdiff_usage(
"Usage for patch: "
293 "rdiff [OPTIONS] patch BASIS [DELTA [NEW]]");
297 basis_file =
rs_file_open(basis_name,
"rb", file_force);
298 delta_file =
rs_file_open(poptGetArg(opcon),
"rb", file_force);
299 new_file =
rs_file_open(poptGetArg(opcon),
"wb", file_force);
301 rdiff_no_more_args(opcon);
303 result =
rs_patch_file(basis_file, delta_file, new_file, &stats);
315static rs_result rdiff_action(poptContext opcon)
319 action = poptGetArg(opcon);
321 else if (
isprefix(action,
"signature"))
324 return rdiff_delta(opcon);
326 return rdiff_patch(opcon);
329 (
"You must specify an action: `signature', `delta', or `patch'.");
333int main(
const int argc,
const char *argv[])
336 const struct poptOption opts[] = {
337 {
"verbose",
'v', POPT_ARG_NONE, 0,
'v'},
338 {
"version",
'V', POPT_ARG_NONE, 0,
'V'},
340 {
"output-size",
'O', POPT_ARG_INT, &rs_outbuflen},
341 {
"hash",
'H', POPT_ARG_STRING, &rs_hash_name},
342 {
"rollsum",
'R', POPT_ARG_STRING, &rs_rollsum_name},
343 {
"help",
'?', POPT_ARG_NONE, 0,
'h'},
344 {0,
'h', POPT_ARG_NONE, 0,
'h'},
345 {
"block-size",
'b', POPT_ARG_INT, &block_len},
346 {
"sum-size",
'S', POPT_ARG_INT, &strong_len},
347 {
"statistics",
's', POPT_ARG_NONE, &show_stats},
348 {
"stats", 0, POPT_ARG_NONE, &show_stats},
349 {
"gzip",
'z', POPT_ARG_NONE, 0, OPT_GZIP},
350 {
"bzip2",
'i', POPT_ARG_NONE, 0, OPT_BZIP2},
351 {
"force",
'f', POPT_ARG_NONE, &file_force},
358 opcon = poptGetContext(
"rdiff", argc, argv, opts, 0);
359 rdiff_options(opcon);
360 result = rdiff_action(opcon);
363 fprintf(stderr,
"rdiff: Failed, %s.\n",
rs_strerror(result));
365 poptFreeContext(opcon);
String prefix text function.
int isprefix(char const *tip, char const *iceberg)
Return true if TIP is a prefix of ICEBERG.
Public header for librsync.
LIBRSYNC_EXPORT rs_result rs_build_hash_table(rs_signature_t *sums)
Call this after loading a signature to index it.
LIBRSYNC_EXPORT int rs_file_close(FILE *file)
Close 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.
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 void rs_signature_log_stats(rs_signature_t const *sig)
Log the rs_signature_delta match stats.
LIBRSYNC_EXPORT int rs_supports_trace(void)
Check whether the library was compiled with debugging trace.
LIBRSYNC_EXPORT void rs_free_sumset(rs_signature_t *)
Deep deallocation of checksums.
LIBRSYNC_EXPORT char const * rs_strerror(rs_result r)
Return an English description of a rs_result value.
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.
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.
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.
rs_result
Return codes from nonblocking rsync operations.
@ RS_UNIMPLEMENTED
Author is lazy.
@ RS_DONE
Completed successfully.
@ RS_SYNTAX_ERROR
Command line syntax error.
@ RS_LOG_DEBUG
Debug-level messages.
rs_magic_number
A uint32 magic number, emitted in bigendian/network order at the start of librsync files.
@ RS_BLAKE2_SIG_MAGIC
A signature file using the BLAKE2 hash.
@ RS_MD4_SIG_MAGIC
A signature file with MD4 signatures.
LIBRSYNC_EXPORT int rs_log_stats(rs_stats_t const *stats)
Write statistics into the current log as text.
LIBRSYNC_EXPORT int rs_inbuflen
Buffer sizes for file IO.
LIBRSYNC_EXPORT void rs_trace_set_level(rs_loglevel level)
Set the least important message severity that will be output.
LIBRSYNC_EXPORT char const rs_librsync_version[]
Library version string.
static rs_result rdiff_sig(poptContext opcon)
Generate signature from remaining command line arguments.
Signature of a whole file.
Performance statistics from a librsync encoding or decoding operation.