-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlistdb.c
367 lines (337 loc) · 9.74 KB
/
listdb.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
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
/*
* Lessfs: A data deduplicating filesystem.
* Copyright (C) 2008 Mark Ruijter <[email protected]>
*
* This program is free software.
* You can redistribute lessfs and/or modify it under the terms of either
* (1) the GNU General Public License; either version 3 of the License,
* or (at your option) any later version as published by
* the Free Software Foundation; or (2) obtain a commercial license
* by contacting the Author.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <fuse.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <tcutil.h>
#include <tchdb.h>
#include <tcbdb.h>
#include <stdbool.h>
#include "lib_safe.h"
#include "lib_cfg.h"
#include "lib_str.h"
#include "retcodes.h"
#ifdef LZO
#include "lib_lzo.h"
#endif
#include "lib_qlz.h"
#include "lib_tc.h"
#ifdef SHA3
#include "lib_BMW_SHA3api_ref.h"
#endif
#include "file_io.h"
#include "commons.h"
#ifdef i386
#define ITERATIONS 30
#else
#define ITERATIONS 500
#endif
u_int32_t db_flags, env_flags;
#define die_dataerr(f...) { LFATAL(f); exit(EXIT_DATAERR); }
#define die_syserr() { LFATAL("Fatal system error : %s",strerror(errno)); exit(EXIT_SYSTEM); }
void usage(char *fname)
{
fprintf(stderr, "%s /data_path /meta_patch\n", fname);
}
char *ascii_hash(unsigned char *bhash)
{
char *ascii_hash = NULL;
int n;
char *p1 = NULL, *p2 = NULL;
for (n = 0; n < config->hashlen; n++) {
p1 = as_sprintf("%02X", bhash[n]);
if (n == 0) {
ascii_hash = s_strdup(p1);
} else {
p2 = s_strdup(ascii_hash);
free(ascii_hash);
ascii_hash = as_sprintf("%s%s", p2, p1);
free(p2);
}
free(p1);
}
return (ascii_hash);
}
void listdbu()
{
char *asc_hash;
char *key, *value;
int size;
unsigned long long counter;
/* traverse records */
tchdbiterinit(dbu);
while ((key = tchdbiternext2(dbu)) != NULL) {
value = tchdbget(dbu, key, config->hashlen, &size);
asc_hash = ascii_hash((unsigned char *) key);
memcpy(&counter, value, sizeof(counter));
printf("%s : %llu\n", asc_hash, counter);
free(asc_hash);
free(value);
free(key);
}
}
void flistdbu()
{
char *asc_hash;
char *key;
int size;
INUSE *inuse;
unsigned long rsize;
unsigned long long nextoffet;
/* traverse records */
tchdbiterinit(dbu);
while ((key = tchdbiternext2(dbu)) != NULL) {
if ( 0 == memcmp(config->nexthash,key,config->hashlen)) {
inuse = tchdbget(dbu, key, config->hashlen, &size);
memcpy(&nextoffet,inuse,sizeof(unsigned long long));
printf("\nnextoffset = %llu\n\n", nextoffset);
} else {
inuse = tchdbget(dbu, key, config->hashlen, &size);
asc_hash = ascii_hash((unsigned char *) key);
printf("%s : %llu\n", asc_hash, inuse->inuse);
printf("offset : %llu\n",inuse->offset);
printf("size : %lu\n", inuse->size);
rsize=round_512(inuse->size);
printf("round size : %lu\n\n", rsize);
free(asc_hash);
free(inuse);
}
free(key);
}
}
void listdta()
{
char *asc_hash;
char *key;
/* traverse records */
tchdbiterinit(dbdta);
while ((key = tchdbiternext2(dbdta)) != NULL) {
//value = tchdbget(dbu, key, TIGERLEN, &size);
asc_hash = ascii_hash((unsigned char *) key);
printf("%s\n", asc_hash);
free(asc_hash);
//free(value);
free(key);
}
}
void listdbb()
{
char *asc_hash = NULL;
char *key, *value;
int size;
int vsize;
unsigned long long inode;
unsigned long long blocknr;
/* traverse records */
tchdbiterinit(dbb);
while ((key = tchdbiternext(dbb, &size)) != NULL) {
value = tchdbget(dbb, key, size, &vsize);
asc_hash = ascii_hash((unsigned char *) value);
memcpy(&inode, key, sizeof(unsigned long long));
memcpy(&blocknr, key + sizeof(unsigned long long),
sizeof(unsigned long long));
printf("%llu-%llu : %s\n", inode, blocknr, asc_hash);
free(asc_hash);
free(value);
free(key);
}
}
/* List the symlink database */
void list_symlinks()
{
char *key, *value;
int size;
int sp;
unsigned long long inode;
/* traverse records */
tchdbiterinit(dbs);
while ((key = tchdbiternext(dbs, &size)) != NULL) {
value = tchdbget(dbs, key, size, &sp);
memcpy(&inode, key, sizeof(unsigned long long));
printf("%llu : %s\n", inode, value);
free(value);
free(key);
}
}
void list_hardlinks()
{
char *key, *value;
int size;
int ksize;
unsigned long long inode;
DINOINO dinoino;
BDBCUR *cur;
/* traverse records */
cur = tcbdbcurnew(dbl);
tcbdbcurfirst(cur);
while ((key = tcbdbcurkey(cur, &ksize)) != NULL) {
value = tcbdbcurval(cur, &size);
if (ksize == sizeof(DINOINO)) {
memcpy(&dinoino, key, sizeof(DINOINO));
printf("dinoino %llu-%llu : inode %s\n", dinoino.dirnode,
dinoino.inode, value);
} else {
memcpy(&inode, key, sizeof(unsigned long long));
memcpy(&dinoino, value, sizeof(DINOINO));
printf("inode %llu : %llu-%llu dinoino\n", inode,
dinoino.dirnode, dinoino.inode);
}
free(value);
free(key);
tcbdbcurnext(cur);
}
tcbdbcurdel(cur);
}
void listdbp()
{
char *key, *value;
int size;
int ksize;
DDSTAT *ddstat;
DBT *data;
unsigned long long inode;
char *nfi = "NFI";
CRYPTO *crypto;
/* traverse records */
tchdbiterinit(dbp);
while ((key = tchdbiternext(dbp, &ksize)) != NULL) {
if (0 == memcmp(key, nfi, 3)) {
value = tchdbget(dbp, key, strlen(key), &size);
memcpy(&inode, value, sizeof(unsigned long long));
printf("%s : %llu\n", key, inode);
free(value);
} else {
memcpy(&inode, key, sizeof(unsigned long long));
data = search_dbdata(dbp, &inode, sizeof(unsigned long long));
if (inode == 0) {
crypto = (CRYPTO *) data->data;
} else {
ddstat = value_to_ddstat(data);
#ifdef x86_64
printf
("ddstat->filename %s \n ->inode %lu -> size %lu -> real_size %llu time %lu\n",
ddstat->filename, ddstat->stbuf.st_ino,
ddstat->stbuf.st_size, ddstat->real_size, ddstat->stbuf.st_atim.tv_sec);
#else
printf
("ddstat->filename %s \n ->inode %llu -> size %llu -> real_size %llu time %lu\n",
ddstat->filename, ddstat->stbuf.st_ino,
ddstat->stbuf.st_size, ddstat->real_size, ddstat->stbuf.st_atim.tv_sec);
#endif
if (S_ISDIR(ddstat->stbuf.st_mode)) {
printf(" ->filename %s is a directory\n",
ddstat->filename);
}
ddstatfree(ddstat);
}
DBTfree(data);
}
free(key);
}
}
void listfree()
{
unsigned long long mbytes;
unsigned long long offset;
BDBCUR *cur;
unsigned long long *dbkey;
unsigned long long *dboffset;
int dbsize;
cur = tcbdbcurnew(freelist);
tcbdbcurfirst(cur);
while ((dbkey = tcbdbcurkey(cur, &dbsize)) != NULL) {
if ((dboffset = tcbdbcurval(cur, &dbsize)) == NULL){
fprintf(stderr,"No value for key");
exit(EXIT_SYSTEM);
}
memcpy(&offset, dboffset, sizeof(unsigned long long));
memcpy(&mbytes, dbkey, sizeof(unsigned long long));
printf("offset = %llu : blocks = %llu : bytes = %llu\n", offset, mbytes, mbytes*512);
free(dboffset);
free(dbkey);
tcbdbcurnext(cur);
}
tcbdbcurdel(cur);
return;
}
void listdirent()
{
BDBCUR *cur;
char *key, *value;
int size;
unsigned long long dir;
unsigned long long ent;
/* traverse records */
cur = tcbdbcurnew(dbdirent);
tcbdbcurfirst(cur);
while ((key = tcbdbcurkey2(cur)) != NULL) {
memcpy(&dir, key, sizeof(dir));
value = tcbdbcurval(cur, &size);;
if (value) {
memcpy(&ent, value, sizeof(ent));
printf("%llu:%llu\n", dir, ent);
free(value);
}
free(key);
tcbdbcurnext(cur);
}
tcbdbcurdel(cur);
}
int main(int argc, char *argv[])
{
if (argc < 2)
usage(argv[0]);
if (-1 == r_env_cfg(argv[1]))
usage(argv[0]);
parseconfig(0);
fuse_get_context()->uid = 0;
fuse_get_context()->gid = 0;
printf("\n\ndbp\n");
listdbp();
printf("\n\ndbu\n");
if ( NULL != config->blockdatabs ) {
listdbu();
printf("\n\ndbdta\n");
listdta();
} else {
flistdbu();
listfree();
}
printf("\n\ndbb\n");
listdbb();
printf("\n\ndbdirent\n");
listdirent();
printf("\n\ndbs (symlinks)\n");
list_symlinks();
printf("\n\ndbl (hardlinks)\n");
list_hardlinks();
tc_close(1);
exit(0);
}