summaryrefslogtreecommitdiff
path: root/rsync/rdiff.c
blob: e08095aabef5cea7e8735c1d8c43d15022fca326 (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
/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
 *
 * librsync -- the library for network deltas
 * $Id$
 * 
 * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@samba.org>
 * 
 * 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
                               */

/*
 * 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.
 *
 * If built with debug support and we have mcheck, then turn it on.
 * (Optionally?)
 */

#include <config_rsync.h>

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <popt.h>

#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif

#ifdef HAVE_BZLIB_H
#include <bzlib.h>
#endif

#include "rsync.h"
#include "fileutil.h"
#include "util.h"
#include "trace.h"
#include "isprefix.h"


#define PROGRAM "rdiff"

static size_t block_len = RS_DEFAULT_BLOCK_LEN;
static size_t strong_len = RS_DEFAULT_STRONG_LEN;

static int show_stats = 0;

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


enum {
    OPT_GZIP = 1069, OPT_BZIP2
};

extern int rs_roll_paranoia;

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 },
    { "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",         0,  POPT_ARG_NONE, 0,             OPT_GZIP },
    { "bzip2",        0,  POPT_ARG_NONE, 0,             OPT_BZIP2 },
    { "paranoia",     0,  POPT_ARG_NONE, &rs_roll_paranoia },
    { 0 }
};


static void rdiff_usage(const char *error)
{
    fprintf(stderr, "%s\n"
            "Try `%s --help' for more information.\n",
            error, PROGRAM);
}


static void rdiff_no_more_args(poptContext opcon)
{
    if (poptGetArg(opcon)) {
        rdiff_usage("rdiff: too many arguments");
        exit(RS_SYNTAX_ERROR);
    }
}


static void bad_option(poptContext opcon, int error)
{
    char       msgbuf[1000];
    
    snprintf(msgbuf, sizeof msgbuf-1, "%s: %s: %s",
             PROGRAM, poptStrerror(error), poptBadOption(opcon, 0));
    rdiff_usage(msgbuf);
    
    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"
           "Delta-encoding options:\n"
           "  -b, --block-size=BYTES    Signature block size\n"
           "  -S, --sum-size=BYTES      Set signature strength\n"
           "      --paranoia            Verify all rolling checksums\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)
{
    /*
     * This little declaration is dedicated to Stephen Kapp and Reaper
     * Technologies, who by all appearances redistributed a modified but
     * unacknowledged version of GNU Keyring in violation of the licence
     * and all laws of politeness and good taste.
     */
    char const *bzlib = "", *zlib = "", *trace = "";
    
#ifdef HAVE_LIBZ
    zlib = ", gzip";
#endif

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

#ifndef DO_RS_TRACE
    trace = ", trace disabled";
#endif
   
    printf("rdiff (%s) [%s]\n"
           "Copyright (C) 1997-2001 by Martin Pool, Andrew Tridgell and others.\n"
           "http://rproxy.samba.org/\n"
           "Capabilities: %d 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, RS_CANONICAL_HOST,
           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()) {
                rs_error("library does not support trace");
            }
            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 */
            }
            rs_error("sorry, compression is not really 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;
    
    basis_file = rs_file_open(poptGetArg(opcon), "rb");
    sig_file = rs_file_open(poptGetArg(opcon), "wb");

    rdiff_no_more_args(opcon);
    
    result = rs_sig_file(basis_file, sig_file, block_len, strong_len, &stats);
    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]]");
        return RS_SYNTAX_ERROR;
    }

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

    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);

    if (show_stats) 
        rs_log_stats(&stats);

    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]]");
        return RS_SYNTAX_ERROR;
    }

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

    rdiff_no_more_args(opcon);

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

    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("rdiff: You must specify an action: `signature', `delta', or `patch'.");
    return RS_SYNTAX_ERROR;
}


int main(const int argc, const char *argv[])
{
    poptContext     opcon;
    rs_result       result;

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

    if (result != RS_DONE)
        rs_log(RS_LOG_ERR|RS_LOG_NONAME, "%s", rs_strerror(result));

    return result;
}