hammer utility - Add force support to cleanup
[dragonfly.git] / sbin / hammer / test_dupkey.c
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 -o test_dupkey
6  *
7  * $DragonFly: src/sbin/hammer/test_dupkey.c,v 1.1 2008/06/26 04:07:57 dillon Exp $
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include "hammer_util.h"
14
15 static u_int32_t namekey(const char *name);
16 static void randomname(char *name);
17
18 u_int32_t bitmap[0x80000000U / 32];
19
20 int
21 main(int ac, char **av)
22 {
23         char name[32];
24         u_int32_t key;
25         u_int32_t *ptr;
26         u_int32_t mask;
27         u_int32_t count;
28         u_int32_t saved;
29
30         srandom(0);     /* reproducable random sequence number */
31         count = 0;
32         for (;;) {
33                 randomname(name);
34                 key = namekey(name);
35                 ptr = &bitmap[key / 32];
36                 mask = 1 << (key & 31);
37                 if (*ptr & mask) {
38                         break;
39                 }
40                 *ptr |= mask;
41                 ++count;
42         }
43         printf("duplicate found at count %d key %08x\n", count, key);
44         printf("'%s' and", name);
45         saved = key;
46
47         srandom(0);
48         count = 0;
49         for (;;) {
50                 randomname(name);
51                 key = namekey(name);
52                 if (saved == key)
53                         break;
54                 ++count;
55         }
56         printf(" '%s'\n", name);
57 }
58
59 static
60 u_int32_t
61 namekey(const char *name)
62 {
63         u_int32_t key;
64
65         key = crc32(name, strlen(name)) & 0x7FFFFFFF;
66         if (key == 0)
67                 key = 1;
68         return(key);
69 }
70
71 static
72 void
73 randomname(char *name)
74 {
75         int len = random() % 16 + 8;
76         int i;
77
78         for (i = 0; i < len; ++i)
79                 name[i] = random() % 26 + 'a';
80         name[i] = 0;
81 }
82
83