Convert to sqlite3 module, from pysqlite2.
[cumulus.git] / chunker-standalone.cc
1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2  * Copyright (C) 2013 The Cumulus Developers
3  * See the AUTHORS file for a list of contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 /* Small utility program for computing chunk breakpoints for subfile
21  * signatures.  This can be used by the Python database rebuilder; while the
22  * Python code can compute chunk breakpoints the C++ version runs much more
23  * quickly.
24  *
25  * Protocol: The input is binary, consisting of a 4-byte record, giving the
26  * length of a data buffer in network byte order, followed by the raw data.
27  * The output is line-oriented: each line consists of whitespace-separated
28  * integers giving the computed breakpoints.  An input with a specified length
29  * of zero ends the computation. */
30
31 #include <arpa/inet.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35
36 #include "third_party/chunk.h"
37
38 #define MAX_BUFSIZE (1 << 24)
39
40 int main(int, char **)
41 {
42     char *buf = new char[MAX_BUFSIZE];
43     size_t *breakpoints = new size_t[chunk_compute_max_num_breaks(MAX_BUFSIZE)];
44
45     while (true) {
46         int32_t blocklen;
47         if (fread(&blocklen, 4, 1, stdin) != 1) {
48             /* Unexpected end of input or other error. */
49             return 1;
50         }
51
52         blocklen = ntohl(blocklen);
53         if (blocklen == 0)
54             return 0;
55         if (blocklen < 0 || blocklen > MAX_BUFSIZE)
56             return 1;
57
58         if (fread(buf, 1, blocklen, stdin) != static_cast<size_t>(blocklen))
59             return 1;
60
61         int num_breakpoints = chunk_compute_breaks(buf, blocklen, breakpoints);
62         for (int i = 0; i < num_breakpoints; i++) {
63             printf("%zd%c", breakpoints[i],
64                    i == num_breakpoints - 1 ? '\n' : ' ');
65         }
66         fflush(stdout);
67     }
68 }