-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibhashfile.h
More file actions
447 lines (417 loc) · 13.6 KB
/
libhashfile.h
File metadata and controls
447 lines (417 loc) · 13.6 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (c) 2011-2014 Vasily Tarasov
* Copyright (c) 2011 Will Buik
* Copyright (c) 2011-2014 Philip Shilane
* Copyright (c) 2011-2014 Erez Zadok
* Copyright (c) 2011-2014 Geoff Kuenning
* Copyright (c) 2011-2014 Stony Brook University
* Copyright (c) 2011-2014 Harvey Mudd College
* Copyright (c) 2011-2014 The Research Foundation of the State University of New York
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*/
#ifndef _HASHFILELIB_H
#define _HASHFILELIB_H
/*
* Format description for index files
*/
/*
* v.1:
*
* HEADER (struct header)
* FILE_HEADER 1 (struct file_header)
* <hash1><hash2>...<hashN_1>
* FILE_HEADER 2 (struct file_header)
* <hash1><hash2>...<hashN_2>
* ...
* FILE_HEADER M (struct file_header)
* <hash1><hash2>...<hashN_M>
*
* v.2:
*
* Same as v.1, but file header
* contains variable file name field
* instead of a fixed one (struct file_header_v2).
*
* v.3:
*
* In the beginning:
* HEADER (struct header_v3)
* FILE_HEADER 1 (struct file_header_v2)
*
* Then, if FIXED size chunking:
* <hash1><hash2>...<hashN_1>
* FILE_HEADER 2 (struct file_header_v2)
* <hash1><hash2>...<hashN_2>
* ...
* FILE_HEADER M (struct file_header_v2)
* <hash1><hash2>...<hashN_M>
*
* And if VARIABLE size chunking (chunk size added):
* <hash1><hash2>...<hashN_1>
* FILE_HEADER 2 (struct file_header_v2)
* <chunk_size1><hash1><chunk_size2><hash2>...<chunk_sizeN_2><hashN_2>
* ...
* FILE_HEADER M (struct file_header_v2)
* <chunk_size1><hash1><chunk_size2><hash2>...<chunk_sizeN_2><hashN_M>
*
* chunk_size is uint64_t
*
* v.4:
* Same as v.3, but FILE_HEADER is 'struct file_header_v3' now,
* which contains a lot of information returned by stat() call.
* Also, if a file is a symlink, then the file's path to which
* this symlink points to is stored in the file header.
*
* v.5:
* Same as v.4, but
* HEADER is 'struct header_v4' (contains the number of bytes scanned)
* FILE_HEADER is 'struct file_header_v4' (contains the number of 512B blocks
* allocated by file systems).
*
* v.6:
* Same as v.5, but compression ratio for each chunk is saved.
* As a result, the whole structure looks like that:
*
* In the beginning:
* HEADER (struct header_v4)
*
* Then, if FIXED size chunking:
* FILE_HEADER 1 (struct file_header_v4)
* <hash1><cratio1><hash2><cratio2>...<hashN_1><cratioN_1>
* FILE_HEADER 2 (struct file_header_v4)
* <hash1><cratio1><hash2><cratio2>...<hashN_1><cratioN_2>
* ...
* FILE_HEADER M (struct file_header_v4)
* <hash1><cratio1><hash2><cratio2>...<hashN_M><cratioN_M>
*
* And if VARIABLE size chunking (chunk size added):
* FILE_HEADER 1 (struct file_header_v4)
* <chunk_size1><hash1><cratio1><chunk_size2><hash2><cratio2>...<chunk_sizeN_1><hashN_1><cratioN_1>
* FILE_HEADER 2 (struct file_header_v4)
* <chunk_size1><hash1><cratio1><chunk_size2><hash2><cratio2>...<chunk_sizeN_2><hashN_1><cratioN_2>
* ...
* FILE_HEADER M (struct file_header_v4)
* <chunk_size1><hash1><cratio1><chunk_size2><hash2><cratio2>...<chunk_sizeN_M><hashN_M><cratioN_M>
*
* chunk_size is uint64_t
* cratio is uint8_t
*
* v.7:
* Same as v.7, but chunk_size is uint32_t. Notice, that this impacts
* only the file format; chunk_info structure still holds a chunk_size
* field of uint64_t type for compatibility with older versions.
* As a result, the whole structure looks like that:
*
* In the beginning:
* HEADER (struct header_v4)
*
* Then, if FIXED size chunking:
* FILE_HEADER 1 (struct file_header_v4)
* <hash1><cratio1><hash2><cratio2>...<hashN_1><cratioN_1>
* FILE_HEADER 2 (struct file_header_v4)
* <hash1><cratio1><hash2><cratio2>...<hashN_1><cratioN_2>
* ...
* FILE_HEADER M (struct file_header_v4)
* <hash1><cratio1><hash2><cratio2>...<hashN_M><cratioN_M>
*
* And if VARIABLE size chunking (chunk size added):
* FILE_HEADER 1 (struct file_header_v4)
* <chunk_size1><hash1><cratio1><chunk_size2><hash2><cratio2>...<chunk_sizeN_1><hashN_1><cratioN_1>
* FILE_HEADER 2 (struct file_header_v4)
* <chunk_size1><hash1><cratio1><chunk_size2><hash2><cratio2>...<chunk_sizeN_2><hashN_1><cratioN_2>
* ...
* FILE_HEADER M (struct file_header_v4)
* <chunk_size1><hash1><cratio1><chunk_size2><hash2><cratio2>...<chunk_sizeN_M><hashN_M><cratioN_M>
*
* chunk_size is uint32_t
* cratio is uint8_t
*
*/
#include <stdint.h>
#include <limits.h>
#include <sys/stat.h>
#define HASH_FILE_MAGIC 0xDEADDEAD
#define MAX_PATH_SIZE 4096
#define MAX_SYSID_LEN 4096
/*
* We enforce that version values
* should increase monotonically.
*/
#define HASH_FILE_VERSION1 0x1
#define HASH_FILE_VERSION2 0x2
#define HASH_FILE_VERSION3 0x3
#define HASH_FILE_VERSION4 0x4
#define HASH_FILE_VERSION5 0x5
#define HASH_FILE_VERSION6 0x6
#define HASH_FILE_VERSION7 0x7
enum chnking_method
{
FIXED = 1,
VARIABLE = 2
};
enum hshing_method
{
MD5_HASH = 1,
SHA256_HASH = 2,
MD5_48BIT_HASH = 3,
MURMUR_HASH = 4,
MD5_64BIT_HASH = 5,
SHA1_HASH = 6,
};
enum cmpr_method
{
NONE = 0,
ZLIB_DEF = 1
};
enum var_chnking_algo
{
RANDOM = 1,
SIMPLE_MATCH = 2,
RABIN = 3
};
struct var_random_chnk_params {
long double probability; /* probability to chunk a stream */
} __attribute__((packed));
struct var_simple_chnk_params {
uint32_t bits_to_compare;
uint64_t pattern;
} __attribute__((packed));
struct var_rabin_chnk_params {
uint32_t window_size; /* in bytes */
uint64_t prime;
uint64_t module;
uint32_t bits_to_compare;
uint64_t pattern;
} __attribute__((packed));
struct fixed_chnking_params
{
uint32_t chunk_size;
} __attribute__((packed));
struct var_chnking_params
{
enum var_chnking_algo algo;
union {
struct var_random_chnk_params rnd_params;
struct var_simple_chnk_params simple_params;
struct var_rabin_chnk_params rabin_params;
} algo_params;
uint32_t min_csize; /* in bytes */
uint32_t max_csize; /* in bytes */
} __attribute__((packed));
/*
* Header for version 1 and 2:
*
* We enforce that new fields should be added
* in the end of a header.
*/
struct header {
uint32_t magic;
uint32_t version;
uint64_t files;
char path_root[MAX_PATH_SIZE]; /* where the scan has started */
uint64_t chunks; /* number of chunks/indexes */
enum chnking_method chnk_method;
union {
struct fixed_chnking_params fixed_params;
struct var_chnking_params var_params;
} chnk_method_params;
enum hshing_method hsh_method;
uint32_t hash_size; /* in bits */
} __attribute__((packed));
/*
* Header for version 3 and 4:
*
* - sisid field added
* - start/end time added
*/
struct header_v3 {
uint32_t magic;
uint32_t version;
uint64_t files;
char path_root[MAX_PATH_SIZE]; /* where the scan has started */
uint64_t chunks; /* number of chunks/indexes */
enum chnking_method chnk_method;
union {
struct fixed_chnking_params fixed_params;
struct var_chnking_params var_params;
} chnk_method_params;
enum hshing_method hsh_method;
uint32_t hash_size; /* in bits */
char sysid[MAX_SYSID_LEN];
uint64_t start_time;
uint64_t end_time;
} __attribute__((packed));
/*
* Header for version 5
*
* bytes field added
*
*/
struct header_v4 {
uint32_t magic;
uint32_t version;
uint64_t files;
char path_root[MAX_PATH_SIZE]; /* where the scan has started */
uint64_t chunks; /* number of chunks/indexes */
enum chnking_method chnk_method;
union {
struct fixed_chnking_params fixed_params;
struct var_chnking_params var_params;
} chnk_method_params;
enum hshing_method hsh_method;
uint32_t hash_size; /* in bits */
char sysid[MAX_SYSID_LEN];
uint64_t start_time;
uint64_t end_time;
uint64_t bytes;
} __attribute__((packed));
struct file_header {
char path[MAX_PATH_SIZE];
uint64_t file_size; /* in bytes */
uint64_t chunks; /* chunks/indexes in this file */
} __attribute__((packed));
struct file_header_v2 {
uint64_t file_size; /* in bytes */
uint64_t chunks; /* chunks/indexes in this file */
uint32_t pathlen; /* length of the following path */
char path[0]; /* non-null terminated path
of pathlen */
} __attribute__((packed));
struct file_header_v3 {
uint64_t file_size; /* in bytes */
uint32_t uid; /* uid of file owner */
uint32_t gid; /* gid of file owner */
uint64_t perm; /* file mode */
uint64_t atime; /* file atime */
uint64_t mtime; /* file mtime */
uint64_t ctime; /* file ctime */
uint64_t hardlinks; /* number of hardlinks */
uint64_t deviceid; /* file device id */
uint64_t inodenum; /* file inode number */
uint64_t chunks; /* chunks/indexes in this file */
uint32_t pathlen; /* length of the following path */
uint32_t target_pathlen; /* length of the following target_path */
char path[0]; /* non-null terminated path
of pathlen */
char target_path[0]; /* non-null terminated
path of pathlen */
} __attribute__((packed));
struct file_header_v4 {
uint64_t file_size; /* in bytes */
uint64_t blocks; /* 512B blocks allocated by fs */
uint32_t uid; /* uid of file owner */
uint32_t gid; /* gid of file owner */
uint64_t perm; /* file mode */
uint64_t atime; /* file atime */
uint64_t mtime; /* file mtime */
uint64_t ctime; /* file ctime */
uint64_t hardlinks; /* number of hardlinks */
uint64_t deviceid; /* file device id */
uint64_t inodenum; /* file inode number */
uint64_t chunks; /* chunks/indexes in this file */
uint32_t pathlen; /* length of the following path */
uint32_t target_pathlen; /* length of the following target_path */
char path[0]; /* non-null terminated path
of pathlen */
char target_path[0]; /* non-null terminated
path of pathlen */
} __attribute__((packed));
struct abstract_file_header {
uint64_t file_size;
uint64_t blocks;
uint32_t uid;
uint32_t gid;
uint64_t perm;
uint64_t atime;
uint64_t mtime;
uint64_t ctime;
uint64_t hardlinks;
uint64_t deviceid;
uint64_t inodenum;
uint64_t chunks;
uint32_t pathlen;
char path[MAX_PATH_SIZE];
uint32_t target_pathlen;
char target_path[MAX_PATH_SIZE];
};
struct chunk_info {
uint8_t *hash; /* this points to static memory! */
uint64_t size;
uint8_t cratio;
};
#define CHUNK_SIZE_32BIT (sizeof(uint32_t))
#define CHUNK_SIZE_64BIT (sizeof(uint64_t))
enum openmode {
READ = 0,
WRITE = 1,
};
struct hashfile_handle {
int fd;
enum openmode omode;
struct header_v4 header;
struct abstract_file_header current_file;
/* offset of current file's header. Used only when we write a file. */
off_t current_file_header_offset;
struct chunk_info current_chunk_info;
uint64_t num_files_processed;
uint64_t num_hashes_processed_current_file;
};
struct hashfile_handle *hashfile_open(char *hashfile_name);
struct hashfile_handle *hashfile_open4write(char *hashfile_name,
enum chnking_method cmeth, enum hshing_method hmeth,
uint32_t hash_size, const char *root_path);
int hashfile_set_fxd_chnking_params(struct hashfile_handle *handle,
struct fixed_chnking_params *params);
int hashfile_set_var_chnking_params(struct hashfile_handle *handle,
struct var_chnking_params *params);
void hashfile_close(struct hashfile_handle *handle);
uint32_t hashfile_version(struct hashfile_handle *handle);
const char *hashfile_rootpath(struct hashfile_handle *handle);
/*
* hashfile_sysid(), hashfile_start_time(), hashfile_end_time(),
* hashfile_curfile_uid(), hashfile_curfile_gid(), and others
* return NULL (or 0) if hashfile version does not support
* corresponding header fields.
*/
const char *hashfile_sysid(struct hashfile_handle *handle);
uint64_t hashfile_start_time(struct hashfile_handle *handle);
uint64_t hashfile_end_time(struct hashfile_handle *handle);
uint64_t hashfile_numfiles(struct hashfile_handle *handle);
uint64_t hashfile_numchunks(struct hashfile_handle *handle);
uint64_t hashfile_numbytes(struct hashfile_handle *handle);
uint32_t hashfile_hash_size(struct hashfile_handle *handle);
enum chnking_method hashfile_chunking_method(struct hashfile_handle *handle);
int hashfile_chunking_method_str(struct hashfile_handle *handle,
char *buf, int size);
int hashfile_fxd_chunking_params(struct hashfile_handle *handle,
struct fixed_chnking_params *params);
int hashfile_var_chunking_params(struct hashfile_handle *handle,
struct var_chnking_params *params);
enum hshing_method hashfile_hashing_method(struct hashfile_handle *handle);
int hashfile_hashing_method_str(struct hashfile_handle *handle,
char *buf, int size);
int hashfile_next_file(struct hashfile_handle *handle);
int hashfile_add_file(struct hashfile_handle *handle, const char *file_path,
const struct stat *stat_buf, const char *target_path);
const struct chunk_info *hashfile_next_chunk(struct hashfile_handle *handle);
int hashfile_add_chunk(struct hashfile_handle *handle,
const struct chunk_info *ci);
const char *hashfile_curfile_path(struct hashfile_handle *handle);
uint64_t hashfile_curfile_numchunks(struct hashfile_handle *handle);
uint32_t hashfile_curfile_uid(struct hashfile_handle *handle);
uint32_t hashfile_curfile_gid(struct hashfile_handle *handle);
uint64_t hashfile_curfile_perm(struct hashfile_handle *handle);
uint64_t hashfile_curfile_atime(struct hashfile_handle *handle);
uint64_t hashfile_curfile_mtime(struct hashfile_handle *handle);
uint64_t hashfile_curfile_ctime(struct hashfile_handle *handle);
uint64_t hashfile_curfile_size(struct hashfile_handle *handle);
uint64_t hashfile_curfile_hardlinks(struct hashfile_handle *handle);
uint64_t hashfile_curfile_deviceid(struct hashfile_handle *handle);
uint64_t hashfile_curfile_inodenum(struct hashfile_handle *handle);
char *hashfile_curfile_linkpath(struct hashfile_handle *handle);
uint64_t hashfile_curfile_blocks(struct hashfile_handle *handle);
int hashfile_reset(struct hashfile_handle *handle);
#endif /*_HASHFILELIB_H */