File: hashstats.c

package info (click to toggle)
duperemove 0.11.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 844 kB
  • sloc: ansic: 11,586; makefile: 110
file content (369 lines) | stat: -rw-r--r-- 8,748 bytes parent folder | download
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
/*
 * hashstats.c
 *
 * Copyright (C) 2014 SUSE.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * 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.
 */

#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <inttypes.h>

#include "rbtree.h"
#include "list.h"
#include "csum.h"
#include "filerec.h"
#include "hash-tree.h"
#include "util.h"
#include "dbfile.h"

#include "bswap.h"

extern sqlite3 *gdb;

unsigned int blocksize;
int v2_hashfile = 0;
static int version_only = 0;
static int print_all_hashes = 0;
static int print_blocks = 0;
static int num_to_print = 10;
static int print_file_list = 0;
static char *serialize_fname = NULL;
static struct dbfile_config dbfile_cfg;

static sqlite3_stmt *top_hashes_stmt = NULL;
static sqlite3_stmt *files_count_stmt = NULL;
static sqlite3_stmt *find_blocks_stmt = NULL;

/* dirty hack so we don't have to add file_scan.o to hashstats */
int add_file_db(const char *filename, uint64_t inum, uint64_t subvolid,
		uint64_t size, uint64_t mtime, unsigned int seq, int *delete)
{
	return 0;
}

static int prepare_statements(void)
{
	int ret;

#define	FIND_TOP_HASHES							\
"select digest, count(digest) from hashes group by digest having (count(digest) > 1) order by (count(digest)) desc;"
	ret = sqlite3_prepare_v2(gdb, FIND_TOP_HASHES, -1, &top_hashes_stmt,
				 NULL);
	if (ret) {
		fprintf(stderr, "error %d while prepping hash search stmt: %s\n",
			ret, sqlite3_errstr(ret));
		return ret;
	}

#define	FIND_FILES_COUNT						\
"select count (distinct files.filename) from files INNER JOIN hashes on hashes.digest = ?1 AND files.subvol=hashes.subvol AND files.ino=hashes.ino;"
	ret = sqlite3_prepare_v2(gdb, FIND_FILES_COUNT, -1, &files_count_stmt,
				 NULL);
	if (ret) {
		fprintf(stderr, "error %d while preparing file count stmt: %s\n",
			ret, sqlite3_errstr(ret));
		return ret;
	}

#define	FIND_BLOCKS							\
"select files.filename, hashes.loff, hashes.flags from files INNER JOIN hashes on hashes.digest = ?1 AND files.subvol=hashes.subvol AND files.ino=hashes.ino;"

	ret = sqlite3_prepare_v2(gdb, FIND_BLOCKS, -1, &find_blocks_stmt, NULL);
	if (ret) {
		fprintf(stderr, "error %d while prepping find blocks stmt: %s\n",
			ret, sqlite3_errstr(ret));
		return ret;
	}
	return 0;
}

static void finalize_statements(void)
{
	sqlite3_finalize(top_hashes_stmt);
	sqlite3_finalize(files_count_stmt);
	sqlite3_finalize(find_blocks_stmt);
}

static void printf_file_block_flags(unsigned int flags)
{
	if (!flags)
		return;

	printf("( ");
	if (flags & FILE_BLOCK_SKIP_COMPARE)
		printf("skip_compare ");
	if (flags & FILE_BLOCK_DEDUPED)
		printf("deduped ");
	if (flags & FILE_BLOCK_HOLE)
		printf("hole ");
	if (flags & FILE_BLOCK_PARTIAL)
		printf("partial ");
	printf(")");
}

static int print_all_blocks(unsigned char *digest)
{
	int ret;
	uint64_t loff;
	unsigned int flags;
	const unsigned char *filename;

	ret = sqlite3_bind_blob(find_blocks_stmt, 1, digest, digest_len,
				SQLITE_STATIC);
	if (ret) {
		fprintf(stderr, "Error %d binding digest for blocks: %s\n", ret,
			sqlite3_errstr(ret));
		return ret;
	}

	while ((ret = sqlite3_step(find_blocks_stmt)) == SQLITE_ROW) {
		filename = sqlite3_column_text(find_blocks_stmt, 0);
		loff = sqlite3_column_int64(find_blocks_stmt, 1);
		flags = sqlite3_column_int(find_blocks_stmt, 2);

		printf("  %s\tloff: %llu lblock: %llu "
		       "flags: 0x%x ", filename,
		       (unsigned long long)loff,
		       (unsigned long long)loff / blocksize,
		       flags);
		printf_file_block_flags(flags);
		printf("\n");
	}
	if (ret != SQLITE_DONE) {
		fprintf(stderr,
			"error %d running block stmt: %s\n",
			ret, sqlite3_errstr(ret));
		return ret;
	}

	sqlite3_reset(find_blocks_stmt);

	return 0;
}

static void print_by_size(void)
{
	int ret;
	int header_printed = 0;
	unsigned char *digest;
	uint64_t count, files_count;

	if (print_all_hashes)
		printf("Print all hashes ");
	else
		printf("Print top %d hashes ", num_to_print);

	printf("(this may take some time)\n");

	while ((ret = sqlite3_step(top_hashes_stmt)) == SQLITE_ROW) {
		digest = (unsigned char *)sqlite3_column_blob(top_hashes_stmt, 0);
		count = sqlite3_column_int64(top_hashes_stmt, 1);

		ret = sqlite3_bind_blob(files_count_stmt, 1, digest, digest_len,
					SQLITE_STATIC);
		if (ret) {
			fprintf(stderr, "Error %d binding digest: %s\n", ret,
				sqlite3_errstr(ret));
			return;
		}

		ret = sqlite3_step(files_count_stmt);
		if (ret != SQLITE_ROW && ret != SQLITE_DONE) {
			fprintf(stderr, "error %d, file count search: %s\n",
				ret, sqlite3_errstr(ret));
			return;
		}

		files_count = sqlite3_column_int64(files_count_stmt, 0);

		if (!header_printed) {
			printf("Hash, # Blocks, # Files\n");
			header_printed = 1;
		}

		debug_print_digest(stdout, digest);
		printf(", %"PRIu64", %"PRIu64"\n", count, files_count);

		sqlite3_reset(files_count_stmt);

		if (print_blocks) {
			ret = print_all_blocks(digest);
			if (ret)
				return;
		}

		if (!print_all_hashes && --num_to_print == 0) {
			ret = SQLITE_DONE;
			break;
		}
	}
	if (ret != SQLITE_DONE) {
		fprintf(stderr, "error %d retrieving hashes from table: %s\n",
			ret, sqlite3_errstr(ret));
	}
}

static int print_files_cb(void *priv, int argc, char **argv, char **column)
{
	int i;
	for(i = 0; i < argc; i++)
		printf("%s\t", argv[i]);
	printf("\n");
	return 0;
}

static void print_filerecs(void)
{
	int ret;
	char *errorstr;

#define	LIST_FILES							\
"select ino, subvol, blocks, size, filename from files;"

	printf("Showing %"PRIu64" files.\nInode\tSubvol ID\tBlocks Stored\tSize\tFilename\n",
		dbfile_cfg.num_files);

	ret = sqlite3_exec(gdb, LIST_FILES, print_files_cb, gdb, &errorstr);
	if (ret) {
		fprintf(stderr, "error %d, executing file search: %s\n", ret,
			errorstr);
		return;
	}
}

static void print_file_info(void)
{
	printf("Raw header info for \"%s\":\n", serialize_fname);
	printf("  version: %d.%d\tblock_size: %u\n", dbfile_cfg.major,
	       dbfile_cfg.minor, dbfile_cfg.blocksize);
	printf("  num_files: %"PRIu64"\tnum_hashes: %"PRIu64"\n",
	       dbfile_cfg.num_files, dbfile_cfg.num_hashes);
}

static void usage(const char *prog)
{
	printf("hashstats %s\n", VERSTRING);
	if (version_only)
		return;

	printf("Print information about duperemove hashes.\n\n");
	printf("Usage: %s [-n NUM] [-a] [-b] [-l] hashfile\n", prog);
	printf("Where \"hashfile\" is a file generated by running duperemove\n");
	printf("with the '--write-hashes' option. By default a list of hashes\n");
	printf("with the most shared blocks are printed.\n");
	printf("\n\t<switches>\n");
	printf("\t-n NUM\t\tPrint top N hashes, sorted by bucket size.\n");
	printf("\t      \t\tDefault is 10.\n");
	printf("\t-a\t\tPrint all hashes (overrides '-n', above)\n");
	printf("\t-b\t\tPrint info on each block within our hash buckets\n");
	printf("\t-l\t\tPrint a list of all files\n");
	printf("\t--help\t\tPrints this help text.\n");
}

enum {
	HELP_OPTION = CHAR_MAX + 1,
	VERSION_OPTION,
	HASH_OPTION,
};

static int parse_options(int argc, char **argv)
{
	int c;
	static struct option long_ops[] = {
		{ "help", 0, 0, HELP_OPTION },
		{ "version", 0, 0, VERSION_OPTION },
		{ 0, 0, 0, 0}
	};

	if (argc < 2)
		return 1;

	while ((c = getopt_long(argc, argv, "labn:?", long_ops, NULL))
	       != -1) {
		switch (c) {
		case 'l':
			print_file_list = 1;
			break;
		case 'a':
			print_all_hashes = 1;
			break;
		case 'b':
			print_blocks = 1;
			break;
		case 'n':
			num_to_print = atoi(optarg);
			break;
		case VERSION_OPTION:
			version_only = 1;
			return 1;
		case HELP_OPTION:
		case '?':
		default:
			version_only = 0;
			return 1;
		}
	}

	if ((argc - optind) != 1)
		return 1;

	serialize_fname = argv[optind];

	return 0;
}

int main(int argc, char **argv)
{
	int ret;
	struct hash_tree tree;

	init_filerec();
	init_hash_tree(&tree);

	if (parse_options(argc, argv)) {
		usage(argv[0]);
		return EINVAL;
	}

	if (init_csum_module(DEFAULT_HASH_STR))
		return ENOMEM;

	ret = dbfile_open(serialize_fname, &dbfile_cfg);
	if (ret)
		return ret;

	blocksize = dbfile_cfg.blocksize;

	ret = prepare_statements();
	if (ret)
		return ret;

	print_file_info();

	if (num_to_print || print_all_hashes)
		print_by_size();

	if (print_file_list)
		print_filerecs();

	finalize_statements();

	dbfile_close();

	return ret;
}