summaryrefslogtreecommitdiff
path: root/src/rdiff.c
blob: 29ca8924712d71c7062071eaccab5e5200c98cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
 *
 * librsync -- the library for network deltas
 *
 * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

                              /*=
                               | .. after a year and a day, mourning is
                               | dangerous to the survivor and troublesome
                               | to the dead.
                               |              -- Harold Bloom
                               */

/** \file rdiff.c
 * Command-line network-delta tool.
 *
 * \todo Add a -z option to gzip/gunzip patches. This would be somewhat useful,
 * but more importantly a good test of the streaming API. Also add -I for
 * bzip2.
 *
 * \todo If built with debug support and we have mcheck, then turn it on.
 * (Optionally?)
 *
 * \todo popt doesn't handle single dashes very well at the moment: we'd like
 * to use them as arguments to indicate stdin/stdout, but it turns them into
 * options. I sent a patch to the popt maintainers; hopefully it will be fixed
 * in the future.
 *
 * \todo Add an option for delta to check whether the files are identical. */

#include "config.h"             /* IWYU pragma: keep */
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <popt.h>
#include <stdio.h>
#include "librsync.h"
#include "isprefix.h"

static int block_len = 0;
static int strong_len = 0;

static int show_stats = 0;

static int bzip2_level = 0;
static int gzip_level = 0;
static int file_force = 0;

enum {
    OPT_GZIP = 1069, OPT_BZIP2
};

char *rs_hash_name;
char *rs_rollsum_name;

static void rdiff_usage(const char *error, ...)
{
    va_list va;
    char buf[256];

    va_start(va, error);
    vsnprintf(buf, sizeof(buf), error, va);
    va_end(va);
    fprintf(stderr, "rdiff: %s\n\nTry `rdiff --help' for more information.\n",
            buf);
}

static void rdiff_no_more_args(poptContext opcon)
{
    if (poptGetArg(opcon)) {
        rdiff_usage("Too many arguments.");
        exit(RS_SYNTAX_ERROR);
    }
}

static void bad_option(poptContext opcon, int error)
{
    rdiff_usage("%s: %s", poptStrerror(error), poptBadOption(opcon, 0));
    exit(RS_SYNTAX_ERROR);
}

static void help(void)
{
    printf("Usage: rdiff [OPTIONS] signature [BASIS [SIGNATURE]]\n"
           "             [OPTIONS] delta SIGNATURE [NEWFILE [DELTA]]\n"
           "             [OPTIONS] patch BASIS [DELTA [NEWFILE]]\n" "\n"
           "Options:\n"
           "  -v, --verbose             Trace internal processing\n"
           "  -V, --version             Show program version\n"
           "  -?, --help                Show this help message\n"
           "  -s, --statistics          Show performance statistics\n"
           "  -f, --force               Force overwriting existing files\n"
           "Signature generation options:\n"
           "  -H, --hash=ALG            Hash algorithm: blake2 (default), md4\n"
           "  -R, --rollsum=ALG         Rollsum algorithm: rabinkarp (default), rollsum\n"
           "Delta-encoding options:\n"
           "  -b, --block-size=BYTES    Signature block size, 0 (default) for recommended\n"
           "  -S, --sum-size=BYTES      Signature strength, 0 (default) for max, -1 for min\n"
           "IO options:\n" "  -I, --input-size=BYTES    Input buffer size\n"
           "  -O, --output-size=BYTES   Output buffer size\n"
           "  -z, --gzip[=LEVEL]        gzip-compress deltas\n"
           "  -i, --bzip2[=LEVEL]       bzip2-compress deltas\n");
}

static void rdiff_show_version(void)
{
    char const *bzlib = "", *zlib = "", *trace = "";

#if 0
    /* Compression isn't implemented so don't mention it. */
#  ifdef HAVE_LIBZ
    zlib = ", gzip";
#  endif

#  ifdef HAVE_LIBBZ2
    bzlib = ", bzip2";
#  endif
#endif

#ifndef DO_RS_TRACE
    trace = ", trace disabled";
#endif

    printf("rdiff (%s)\n"
           "Copyright (C) 1997-2016 by Martin Pool, Andrew Tridgell and others.\n"
           "http://librsync.sourcefrog.net/\n"
           "Capabilities: %ld bit files%s%s%s\n" "\n"
           "librsync comes with NO WARRANTY, to the extent permitted by law.\n"
           "You may redistribute copies of librsync under the terms of the GNU\n"
           "Lesser General Public License.  For more information about these\n"
           "matters, see the files named COPYING.\n", rs_librsync_version,
           (long)(8 * sizeof(rs_long_t)), zlib, bzlib, trace);
}

static void rdiff_options(poptContext opcon)
{
    int c;
    char const *a;

    while ((c = poptGetNextOpt(opcon)) != -1) {
        switch (c) {
        case 'h':
            help();
            exit(RS_DONE);
        case 'V':
            rdiff_show_version();
            exit(RS_DONE);
        case 'v':
            if (!rs_supports_trace()) {
                fprintf(stderr, "rdiff: Library does not support trace.\n");
            }
            rs_trace_set_level(RS_LOG_DEBUG);
            break;

        case OPT_GZIP:
        case OPT_BZIP2:
            if ((a = poptGetOptArg(opcon))) {
                int l = atoi(a);
                if (c == OPT_GZIP)
                    gzip_level = l;
                else
                    bzip2_level = l;
            } else {
                if (c == OPT_GZIP)
                    gzip_level = -1;    /* library default */
                else
                    bzip2_level = 9;    /* demand the best */
            }
            rdiff_usage("Sorry, compression is not implemented yet.");
            exit(RS_UNIMPLEMENTED);

        default:
            bad_option(opcon, c);
        }
    }
}

/** Generate signature from remaining command line arguments. */
static rs_result rdiff_sig(poptContext opcon)
{
    FILE *basis_file, *sig_file;
    rs_stats_t stats;
    rs_result result;
    rs_magic_number sig_magic;

    basis_file = rs_file_open(poptGetArg(opcon), "rb", file_force);
    sig_file = rs_file_open(poptGetArg(opcon), "wb", file_force);

    rdiff_no_more_args(opcon);

    if (!rs_hash_name || !strcmp(rs_hash_name, "blake2")) {
        sig_magic = RS_BLAKE2_SIG_MAGIC;
    } else if (!strcmp(rs_hash_name, "md4")) {
        sig_magic = RS_MD4_SIG_MAGIC;
    } else {
        rdiff_usage("Unknown hash algorithm '%s'.", rs_hash_name);
        exit(RS_SYNTAX_ERROR);
    }
    if (!rs_rollsum_name || !strcmp(rs_rollsum_name, "rabinkarp")) {
        /* The RabinKarp magics are 0x10 greater than the rollsum magics. */
        sig_magic += 0x10;
    } else if (strcmp(rs_rollsum_name, "rollsum")) {
        rdiff_usage("Unknown rollsum algorithm '%s'.", rs_rollsum_name);
        exit(RS_SYNTAX_ERROR);
    }

    result =
        rs_sig_file(basis_file, sig_file, block_len, strong_len, sig_magic,
                    &stats);

    rs_file_close(sig_file);
    rs_file_close(basis_file);
    if (result != RS_DONE)
        return result;

    if (show_stats)
        rs_log_stats(&stats);

    return result;
}

static rs_result rdiff_delta(poptContext opcon)
{
    FILE *sig_file, *new_file, *delta_file;
    char const *sig_name;
    rs_result result;
    rs_signature_t *sumset;
    rs_stats_t stats;

    if (!(sig_name = poptGetArg(opcon))) {
        rdiff_usage("Usage for delta: "
                    "rdiff [OPTIONS] delta SIGNATURE [NEWFILE [DELTA]]");
        exit(RS_SYNTAX_ERROR);
    }

    sig_file = rs_file_open(sig_name, "rb", file_force);
    new_file = rs_file_open(poptGetArg(opcon), "rb", file_force);
    delta_file = rs_file_open(poptGetArg(opcon), "wb", file_force);

    rdiff_no_more_args(opcon);

    result = rs_loadsig_file(sig_file, &sumset, &stats);
    if (result != RS_DONE)
        return result;

    if (show_stats)
        rs_log_stats(&stats);

    if ((result = rs_build_hash_table(sumset)) != RS_DONE)
        return result;

    result = rs_delta_file(sumset, new_file, delta_file, &stats);

    rs_file_close(delta_file);
    rs_file_close(new_file);
    rs_file_close(sig_file);

    if (show_stats) {
        rs_signature_log_stats(sumset);
        rs_log_stats(&stats);
    }

    rs_free_sumset(sumset);

    return result;
}

static rs_result rdiff_patch(poptContext opcon)
{
    /* patch BASIS [DELTA [NEWFILE]] */
    FILE *basis_file, *delta_file, *new_file;
    char const *basis_name;
    rs_stats_t stats;
    rs_result result;

    if (!(basis_name = poptGetArg(opcon))) {
        rdiff_usage("Usage for patch: "
                    "rdiff [OPTIONS] patch BASIS [DELTA [NEW]]");
        exit(RS_SYNTAX_ERROR);
    }

    basis_file = rs_file_open(basis_name, "rb", file_force);
    delta_file = rs_file_open(poptGetArg(opcon), "rb", file_force);
    new_file = rs_file_open(poptGetArg(opcon), "wb", file_force);

    rdiff_no_more_args(opcon);

    result = rs_patch_file(basis_file, delta_file, new_file, &stats);

    rs_file_close(new_file);
    rs_file_close(delta_file);
    rs_file_close(basis_file);

    if (show_stats)
        rs_log_stats(&stats);

    return result;
}

static rs_result rdiff_action(poptContext opcon)
{
    const char *action;

    action = poptGetArg(opcon);
    if (!action) ;
    else if (isprefix(action, "signature"))
        return rdiff_sig(opcon);
    else if (isprefix(action, "delta"))
        return rdiff_delta(opcon);
    else if (isprefix(action, "patch"))
        return rdiff_patch(opcon);

    rdiff_usage
        ("You must specify an action: `signature', `delta', or `patch'.");
    exit(RS_SYNTAX_ERROR);
}

int main(const int argc, const char *argv[])
{
    /* Initialize opts at runtime to avoid unknown address values. */
    const struct poptOption opts[] = {
        {"verbose", 'v', POPT_ARG_NONE, 0, 'v'},
        {"version", 'V', POPT_ARG_NONE, 0, 'V'},
        {"input-size", 'I', POPT_ARG_INT, &rs_inbuflen},
        {"output-size", 'O', POPT_ARG_INT, &rs_outbuflen},
        {"hash", 'H', POPT_ARG_STRING, &rs_hash_name},
        {"rollsum", 'R', POPT_ARG_STRING, &rs_rollsum_name},
        {"help", '?', POPT_ARG_NONE, 0, 'h'},
        {0, 'h', POPT_ARG_NONE, 0, 'h'},
        {"block-size", 'b', POPT_ARG_INT, &block_len},
        {"sum-size", 'S', POPT_ARG_INT, &strong_len},
        {"statistics", 's', POPT_ARG_NONE, &show_stats},
        {"stats", 0, POPT_ARG_NONE, &show_stats},
        {"gzip", 'z', POPT_ARG_NONE, 0, OPT_GZIP},
        {"bzip2", 'i', POPT_ARG_NONE, 0, OPT_BZIP2},
        {"force", 'f', POPT_ARG_NONE, &file_force},
        {0}
    };

    poptContext opcon;
    rs_result result;

    opcon = poptGetContext("rdiff", argc, argv, opts, 0);
    rdiff_options(opcon);
    result = rdiff_action(opcon);

    if (result != RS_DONE)
        fprintf(stderr, "rdiff: Failed, %s.\n", rs_strerror(result));

    poptFreeContext(opcon);
    return result;
}