xref: /dflybsd-src/sbin/hammer/test_dupkey.c (revision e586f31ca9899b49a4fc156613d9ecd853defcec)
1 /*
2  * This is a really simple stupid standalone program which will find two
3  * filenames with the same CRC, used to test the directory iterator.
4  *
5  * cc -I /usr/src/sys test_dupkey.c /usr/src/sys/libkern/crc32.c \
6  * /usr/src/sys/libkern/icrc32.c -o test_dupkey
7  *
8  * $DragonFly: src/sbin/hammer/test_dupkey.c,v 1.1 2008/06/26 04:07:57 dillon Exp $
9  */
10 
11 #include "hammer_util.h"
12 
13 static uint32_t namekey(const char *name);
14 static void randomname(char *name);
15 
16 uint32_t bitmap[0x80000000U / 32];
17 
18 int
19 main(int ac, char **av)
20 {
21 	char name[32];
22 	uint32_t key;
23 	uint32_t *ptr;
24 	uint32_t mask;
25 	uint32_t count;
26 	uint32_t saved;
27 
28 	srandom(0);	/* reproducable random sequence number */
29 	count = 0;
30 	for (;;) {
31 		randomname(name);
32 		key = namekey(name);
33 		ptr = &bitmap[key / 32];
34 		mask = 1 << (key & 31);
35 		if (*ptr & mask)
36 			break;
37 		*ptr |= mask;
38 		++count;
39 	}
40 	printf("duplicate found at count %d key %08x\n", count, key);
41 	printf("'%s' and", name);
42 	saved = key;
43 
44 	srandom(0);
45 	count = 0;
46 	for (;;) {
47 		randomname(name);
48 		key = namekey(name);
49 		if (saved == key)
50 			break;
51 		++count;
52 	}
53 	printf(" '%s'\n", name);
54 }
55 
56 static
57 uint32_t
58 namekey(const char *name)
59 {
60 	uint32_t key;
61 
62 	key = crc32(name, strlen(name)) & 0x7FFFFFFF;
63 	if (key == 0)
64 		key = 1;
65 	return(key);
66 }
67 
68 static
69 void
70 randomname(char *name)
71 {
72 	int len = random() % 16 + 8;
73 	int i;
74 
75 	for (i = 0; i < len; ++i)
76 		name[i] = random() % 26 + 'a';
77 	name[i] = 0;
78 }
79 
80 
81