Mercurial > hg > cdb
comparison cdb.c @ 16:70f4df5a7283
add cdb_write,
log basic mmap outcomes
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Thu, 27 Feb 2025 18:26:29 +0000 |
| parents | 0e21568dec72 |
| children | d9c11148df3b |
comparison
equal
deleted
inserted
replaced
| 15:4cc3224ebf41 | 16:70f4df5a7283 |
|---|---|
| 1 /* Public domain. */ | 1 /* Public domain. */ |
| 2 | 2 |
| 3 #include <sys/types.h> | 3 #include <sys/types.h> |
| 4 #include <sys/stat.h> | 4 #include <sys/stat.h> |
| 5 #include <sys/mman.h> | 5 #include <sys/mman.h> |
| 6 #include <stdio.h> | |
| 6 #include "readwrite.h" | 7 #include "readwrite.h" |
| 8 #include "strerr.h" | |
| 7 #include "error.h" | 9 #include "error.h" |
| 8 #include "seek.h" | 10 #include "seek.h" |
| 9 #include "byte.h" | 11 #include "byte.h" |
| 10 #include "alloc.h" | 12 #include "alloc.h" |
| 13 #include "buffer.h" | |
| 11 #include "cdb.h" | 14 #include "cdb.h" |
| 12 | 15 |
| 13 | 16 |
| 14 /*static Cdb c;*/ | 17 /*static Cdb c;*/ |
| 18 | |
| 19 static char _buf[32]; | |
| 15 | 20 |
| 16 Cdb* cdb_new(void) | 21 Cdb* cdb_new(void) |
| 17 { | 22 { |
| 18 return (Cdb *)(alloc(sizeof(Cdb))); | 23 return (Cdb *)(alloc(sizeof(Cdb))); |
| 19 } | 24 } |
| 56 cdb_findstart(c); | 61 cdb_findstart(c); |
| 57 c->fd = fd; | 62 c->fd = fd; |
| 58 | 63 |
| 59 if (fstat(fd,&st) == 0) | 64 if (fstat(fd,&st) == 0) |
| 60 if (st.st_size <= 0xffffffff) { | 65 if (st.st_size <= 0xffffffff) { |
| 66 sprintf(_buf,"%'u",st.st_size); | |
| 67 strerr_warn3("cdb_init ","fd size: ",_buf,0); | |
| 61 x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0); | 68 x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0); |
| 69 sprintf(_buf,"%x",x); | |
| 70 strerr_warn3("cdb_init ","mmap addr: ",_buf,0); | |
| 62 if (x + 1) { | 71 if (x + 1) { |
| 63 c->size = st.st_size; | 72 c->size = st.st_size; |
| 64 c->map = x; | 73 c->map = x; |
| 65 } | 74 } |
| 66 } | 75 } |
| 67 } | 76 } |
| 77 | |
| 78 void cdb_init_nomap(Cdb *c,int fd) | |
| 79 { | |
| 80 struct stat st; | |
| 81 char *x; | |
| 82 | |
| 83 cdb_clear(c); | |
| 84 cdb_findstart(c); | |
| 85 c->fd = fd; | |
| 86 } | |
| 87 | |
| 68 | 88 |
| 69 int cdb_read(Cdb *c,char *buf,unsigned int len,uint32 pos) | 89 int cdb_read(Cdb *c,char *buf,unsigned int len,uint32 pos) |
| 70 { | 90 { |
| 71 if (c->map) { | 91 if (c->map) { |
| 72 if ((pos > c->size) || (c->size - pos < len)) goto FORMAT; | 92 if ((pos > c->size) || (c->size - pos < len)) goto FORMAT; |
| 157 int cdb_find(Cdb *c,char *key,unsigned int len) | 177 int cdb_find(Cdb *c,char *key,unsigned int len) |
| 158 { | 178 { |
| 159 cdb_findstart(c); | 179 cdb_findstart(c); |
| 160 return cdb_findnext(c,key,len); | 180 return cdb_findnext(c,key,len); |
| 161 } | 181 } |
| 182 | |
| 183 char wbuf[1024]; | |
| 184 | |
| 185 int cdb_write(Cdb *c,int fd) | |
| 186 { | |
| 187 uint32 pos; | |
| 188 uint32 len; | |
| 189 int r; | |
| 190 | |
| 191 pos = cdb_datapos(c); | |
| 192 len = cdb_datalen(c); | |
| 193 | |
| 194 while (len > 0) { | |
| 195 r = sizeof wbuf; | |
| 196 if (r > len) r = len; | |
| 197 if (cdb_read(c,wbuf,r,pos) == -1) return -1; | |
| 198 if (buffer_put(buffer_1small,wbuf,r) == -1) return -2; | |
| 199 pos += r; | |
| 200 len -= r; | |
| 201 } | |
| 202 if (buffer_flush(buffer_1small) == -1) return -2; | |
| 203 } |
