-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshrinkmapper.c
317 lines (287 loc) · 7.05 KB
/
shrinkmapper.c
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
/*
* Compress existing SQLite databases.
* Copyright 2016 Darrick J. Wong.
* Licensed under the GPLv2.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sqlite3.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <lz4.h>
#include <lz4hc.h>
#include <zlib.h>
#include "filemapper.h"
#include "compdb.h"
#include "compress.h"
#define swap(a, b) \
do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
struct compdb_info {
enum compdb_type type;
int pagesize;
unsigned int freestart;
unsigned int freelen;
char in_file_header[16];
char out_file_header[16];
};
/* Figure out database parameters. */
static int
sniff(
const struct sqlite3_super *super,
struct compdb_info *cdb)
{
int is_sqlite;
int is_compr;
/* Is this really a database? */
is_sqlite = !memcmp(super->magic, SQLITE_FILE_HEADER,
sizeof(super->magic));
is_compr = !memcmp(super->magic, cdb->in_file_header,
sizeof(super->magic));
if ((!is_sqlite && !is_compr) ||
super->max_fraction != 64 || super->min_fraction != 32 ||
super->leaf_payload != 32 || ntohl(super->schema_format) > 4)
return EUCLEAN;
if (is_sqlite)
cdb->type = DB_REGULAR;
else if (is_compr)
cdb->type = DB_COMPRESSED;
/* Collect some stats. */
cdb->pagesize = ntohs(super->pagesize);
if (cdb->pagesize == 1)
cdb->pagesize = 65536;
cdb->freestart = ntohl(super->freelist_start);
cdb->freelen = ntohl(super->freelist_pages);
dbg_printf("%s(%d): pagesize=%d freestart=%llu:%llu\n", __func__, __LINE__,
cdb->pagesize, cdb->freestart, cdb->freelen);
return 0;
}
static inline int
NOOP_compress(
const char *source,
char *dest,
int sourceSize,
int maxDestSize)
{
return 0;
}
int
main(
int argc,
char *argv[])
{
struct sqlite3_super super;
struct compdb_info cdb;
struct stat sb;
char *name;
void *bin, *bout;
void *outp;
struct compdb_block_head *bhead;
struct compressor_type *inc;
struct compressor_type *outc;
struct compressor_type none_type;
int fdin, fdout;
int try_compress;
size_t outlen;
size_t page;
size_t nr_pages;
ssize_t ret;
if (argc == 2 && !strcmp(argv[1], "-l")) {
printf("Supported: NONE,%s\n", compdb_compressors());
return 0;
}
if (argc < 3 || argc > 5) {
printf("Usage: %s infile outfile [compressor] [compressor]\n", argv[0]);
return 1;
}
/* Open files */
fdin = open(argv[1], O_RDONLY);
if (fdin < 0) {
perror(argv[1]);
return 2;
}
fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fdout < 0) {
perror(argv[2]);
return 2;
}
ret = fstat(fdin, &sb);
if (ret) {
perror(argv[1]);
return 2;
}
/* Select compressor(s) */
if (argc >= 4 && argv[3][0] != 0)
name = argv[3];
else
name = NULL;
inc = compdb_find_compressor(name);
if (!inc) {
printf("%s: no such compressor?\n", name);
return 2;
}
snprintf(cdb.in_file_header, sizeof(cdb.in_file_header),
COMPDB_FILE_TEMPLATE, inc->name);
if (argc == 5 && argv[4][0] != 0)
name = argv[4];
else
name = NULL;
if (name && !strcmp(name, "NONE")) {
none_type.name = "none";
none_type.compress = NOOP_compress;
none_type.decompress = NULL;
outc = &none_type;
memcpy(cdb.out_file_header, SQLITE_FILE_HEADER,
sizeof(cdb.out_file_header));
} else {
outc = compdb_find_compressor(name);
if (!outc) {
printf("%s: no such compressor?\n", name);
return 2;
}
snprintf(cdb.out_file_header, sizeof(cdb.out_file_header),
COMPDB_FILE_TEMPLATE, outc->name);
}
dbg_printf("recompress %s(%s) -> %s(%s)\n", argv[1], inc->name,
argv[2], outc->name);
/* Verify superblock */
ret = pread(fdin, &super, sizeof(super), 0);
if (ret < 0) {
perror(argv[1]);
return 2;
} else if (ret < sizeof(super)) {
printf("%s: Short super read??\n", argv[1]);
return 2;
}
cdb.type = DB_UNKNOWN;
ret = sniff(&super, &cdb);
if (ret < 0) {
perror(argv[1]);
return 2;
}
if (cdb.type == DB_UNKNOWN) {
printf("%s: Unrecognized file type.\n", argv[1]);
return 2;
}
/* Allocate buffers */
bin = malloc(cdb.pagesize);
if (!bin) {
perror("malloc");
return 2;
}
bout = malloc(cdb.pagesize);
if (!bout) {
perror("malloc");
return 2;
}
/* Copy pages */
nr_pages = (sb.st_size + cdb.pagesize - 1) / cdb.pagesize;
for (page = 0; page < nr_pages; page++) {
/* Read buffer. */
dbg_printf("%s(%d) off=%zu len=%u\n", __func__, __LINE__,
page * cdb.pagesize, cdb.pagesize);
ret = pread(fdin, bin, cdb.pagesize, page * cdb.pagesize);
if (ret < 0) {
perror(argv[1]);
return 3;
} else if (ret < cdb.pagesize && page != nr_pages - 1) {
printf("%s: Short page %zu read?\n", argv[1], page);
return 3;
}
/* Transform buffer. */
outlen = cdb.pagesize;
outp = bin;
bhead = bin;
try_compress = 0;
if (page == 0) {
/* Do we need to change the header? */
if (cdb.type == DB_REGULAR || inc != outc)
memcpy(outp, cdb.out_file_header,
sizeof(cdb.out_file_header));
} else if (cdb.type == DB_COMPRESSED &&
!memcmp(bhead->magic, COMPDB_BLOCK_MAGIC,
sizeof(COMPDB_BLOCK_MAGIC)) &&
ntohl(bhead->offset) == page) {
if (inc == outc) {
/* Compressed page, send it along. */
outlen = ntohs(bhead->len) + sizeof(*bhead);
} else {
/* Changing compression types; decompress. */
ret = inc->decompress(bin + sizeof(*bhead),
bout,
ntohs(bhead->len),
cdb.pagesize);
if (ret <= 0) {
printf("%s: Decompression failed at "
" page %zu\n", argv[1], page);
return 3;
}
dbg_printf("%s(%d) off=%zu len=%d\n", __func__,
__LINE__, page * cdb.pagesize,
ntohs(bhead->len));
swap(bin, bout);
outp = bin;
try_compress = 1;
}
} else if (page < cdb.freestart ||
page >= cdb.freestart + cdb.freelen) {
/* Uncompressed; try to compress. */
try_compress = 1;
}
if (try_compress) {
/* Try to compress this page? */
ret = outc->compress(bin, bout + sizeof(*bhead),
cdb.pagesize,
cdb.pagesize - sizeof(*bhead));
if (ret > 0) {
bhead = bout;
memcpy(bhead->magic, COMPDB_BLOCK_MAGIC,
sizeof(bhead->magic));
bhead->len = htons(ret);
bhead->offset = htonl(page);
outp = bout;
outlen = ret + sizeof(*bhead);
}
}
/*
* Truncate to where the end of the compressed block
* should be so that XFS won't do speculative preallocation.
*/
ret = ftruncate(fdout, (page * cdb.pagesize) + outlen);
if (ret) {
perror(argv[2]);
return 2;
}
/* Write to disk. */
dbg_printf("%s(%d) off=%zu len=%zu\n", __func__, __LINE__,
page * cdb.pagesize, outlen);
ret = pwrite(fdout, outp, outlen, page * cdb.pagesize);
if (ret < 0) {
perror(argv[2]);
return 3;
} else if (ret < outlen) {
printf("%s: Short page %zu write?\n", argv[2], page);
return 3;
}
/*
* Truncate to the end of the block to avoid short reads.
*/
if (outlen != cdb.pagesize) {
ret = ftruncate(fdout, (page + 1) * cdb.pagesize);
if (ret) {
perror(argv[2]);
return 2;
}
}
}
free(bout);
free(bin);
close(fdout);
close(fdin);
return 0;
}