annotate cdb.c @ 17:d9c11148df3b default tip

shh...
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 28 Aug 2025 11:02:58 +0100
parents 70f4df5a7283
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 /* Public domain. */
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 #include <sys/types.h>
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 #include <sys/stat.h>
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 #include <sys/mman.h>
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
6 #include <stdio.h>
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 #include "readwrite.h"
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
8 #include "strerr.h"
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 #include "error.h"
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 #include "seek.h"
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 #include "byte.h"
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
12 #include "alloc.h"
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
13 #include "buffer.h"
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 #include "cdb.h"
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
16
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
17 /*static Cdb c;*/
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
18
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
19 static char _buf[32];
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
20
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
21 Cdb* cdb_new(void)
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
22 {
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
23 return (Cdb *)(alloc(sizeof(Cdb)));
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
24 }
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
25
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
26 void cdb_clear(Cdb *c)
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28 if (c->map) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 munmap(c->map,c->size);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30 c->map = 0;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
34 /* uint32 cdb_len() { */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
35 /* return c.dlen; */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
36 /* } */
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
37
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
38 /* uint32 cdb_pos() { */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
39 /* return c.dpos; */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
40 /* } */
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
41
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
42 /* char *cdb_mmap() { */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
43 /* return c.map; */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
44 /* } */
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
45
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
46 /* uint32 cdb_msize() { */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
47 /* return c.size; */
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
48 /* } */
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
49
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
50 void cdb_findstart(Cdb *c)
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52 c->loop = 0;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
55 void cdb_init(Cdb *c,int fd)
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 struct stat st;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 char *x;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59
12
0e21568dec72 allocate a Cdb struct rather than using a single static one,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 1
diff changeset
60 cdb_clear(c);
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61 cdb_findstart(c);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62 c->fd = fd;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 if (fstat(fd,&st) == 0)
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
65 if (st.st_size <= 0xffffffff) {
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
66 sprintf(_buf,"%'u",st.st_size);
17
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 16
diff changeset
67 /*strerr_warn3("cdb_init ","fd size: ",_buf,0); */
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
69 sprintf(_buf,"%x",x);
17
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 16
diff changeset
70 /*strerr_warn3("cdb_init ","mmap addr: ",_buf,0); */
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 if (x + 1) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 c->size = st.st_size;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73 c->map = x;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
77
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
78 void cdb_init_nomap(Cdb *c,int fd)
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
79 {
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
80 struct stat st;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
81 char *x;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
82
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
83 cdb_clear(c);
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
84 cdb_findstart(c);
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
85 c->fd = fd;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
86 }
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
87
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
88
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
89 int cdb_read(Cdb *c,char *buf,unsigned int len,uint32 pos)
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
90 {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
91 if (c->map) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
92 if ((pos > c->size) || (c->size - pos < len)) goto FORMAT;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93 byte_copy(buf,len,c->map + pos);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
94 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
95 else {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
96 if (seek_set(c->fd,pos) == -1) return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
97 while (len > 0) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
98 int r;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
99 do
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
100 r = read(c->fd,buf,len);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
101 while ((r == -1) && (errno == error_intr));
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
102 if (r == -1) return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
103 if (r == 0) goto FORMAT;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
104 buf += r;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
105 len -= r;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
106 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
107 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
108 return 0;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
109
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
110 FORMAT:
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
111 errno = error_proto;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
112 return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
113 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
114
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
115 static int match(Cdb *c,char *key,unsigned int len,uint32 pos)
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
116 {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
117 char buf[32];
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
118 int n;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
119
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
120 while (len > 0) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
121 n = sizeof buf;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
122 if (n > len) n = len;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
123 if (cdb_read(c,buf,n,pos) == -1) return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
124 if (byte_diff(buf,n,key)) return 0;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
125 pos += n;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
126 key += n;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
127 len -= n;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
128 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
129 return 1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
130 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
131
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
132 int cdb_findnext(Cdb *c,char *key,unsigned int len)
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
133 {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
134 char buf[8];
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
135 uint32 pos;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
136 uint32 u;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
137
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
138 if (!c->loop) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
139 u = cdb_hash(key,len);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
140 if (cdb_read(c,buf,8,(u << 3) & 2047) == -1) return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
141 uint32_unpack(buf + 4,&c->hslots);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
142 if (!c->hslots) return 0;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
143 uint32_unpack(buf,&c->hpos);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
144 c->khash = u;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
145 u >>= 8;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
146 u %= c->hslots;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
147 u <<= 3;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
148 c->kpos = c->hpos + u;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
149 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
150
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
151 while (c->loop < c->hslots) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
152 if (cdb_read(c,buf,8,c->kpos) == -1) return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
153 uint32_unpack(buf + 4,&pos);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
154 if (!pos) return 0;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
155 c->loop += 1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
156 c->kpos += 8;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
157 if (c->kpos == c->hpos + (c->hslots << 3)) c->kpos = c->hpos;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
158 uint32_unpack(buf,&u);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
159 if (u == c->khash) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
160 if (cdb_read(c,buf,8,pos) == -1) return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
161 uint32_unpack(buf,&u);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
162 if (u == len)
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
163 switch(match(c,key,len,pos + 8)) {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
164 case -1:
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
165 return -1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
166 case 1:
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
167 uint32_unpack(buf + 4,&c->dlen);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
168 c->dpos = pos + 8 + len;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
169 return 1;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
170 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
171 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
172 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
173
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
174 return 0;
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
175 }
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
176
1
41b9b6a160d1 access functions,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
177 int cdb_find(Cdb *c,char *key,unsigned int len)
0
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
178 {
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
179 cdb_findstart(c);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
180 return cdb_findnext(c,key,len);
2cb46628feec from cdb-0.75 per http://cr.yp.to/cdb.html,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
181 }
16
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
182
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
183 char wbuf[1024];
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
184
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
185 int cdb_write(Cdb *c,int fd)
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
186 {
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
187 uint32 pos;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
188 uint32 len;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
189 int r;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
190
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
191 pos = cdb_datapos(c);
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
192 len = cdb_datalen(c);
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
193
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
194 while (len > 0) {
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
195 r = sizeof wbuf;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
196 if (r > len) r = len;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
197 if (cdb_read(c,wbuf,r,pos) == -1) return -1;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
198 if (buffer_put(buffer_1small,wbuf,r) == -1) return -2;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
199 pos += r;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
200 len -= r;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
201 }
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
202 if (buffer_flush(buffer_1small) == -1) return -2;
70f4df5a7283 add cdb_write,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
203 }