diff options
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/cdb_init.c | 23 | ||||
-rw-r--r-- | src/libstddjb/cdb_init_map.c | 26 | ||||
-rw-r--r-- | src/libstddjb/cdb_read.c | 14 |
3 files changed, 25 insertions, 38 deletions
diff --git a/src/libstddjb/cdb_init.c b/src/libstddjb/cdb_init.c new file mode 100644 index 0000000..2a1ffb3 --- /dev/null +++ b/src/libstddjb/cdb_init.c @@ -0,0 +1,23 @@ +/* ISC license. */ + +#include <skalibs/bsdsnowflake.h> + +#include <sys/stat.h> +#include <sys/mman.h> +#include <stdint.h> +#include <errno.h> + +#include <skalibs/cdb.h> + +int cdb_init (struct cdb *c, int fd) +{ + struct stat st ; + char *map ; + if (fstat(fd, &st) < 0) return -1 ; + if (st.st_size > UINT32_MAX) return (errno = EOVERFLOW, -1) ; + map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, -1) ; + if (map == MAP_FAILED) return -1 ; + c->map = map ; + c->size = st.st_size ; + return 0 ; +} diff --git a/src/libstddjb/cdb_init_map.c b/src/libstddjb/cdb_init_map.c deleted file mode 100644 index 55fb02a..0000000 --- a/src/libstddjb/cdb_init_map.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ISC license. */ - -#include <skalibs/bsdsnowflake.h> -#include <sys/stat.h> -#include <sys/mman.h> -#include <stdint.h> -#include <errno.h> -#include <skalibs/cdb.h> - -int cdb_init_map (struct cdb *c, int fd, int domap) -{ - if (domap) - { - struct stat st ; - char *map ; - if (fstat(fd, &st) < 0) return 0 ; - if (st.st_size > UINT32_MAX) return (errno = EOVERFLOW, 0) ; - map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0) ; - if (map == MAP_FAILED) return 0 ; - c->fd = -fd-2 ; - c->map = map ; - c->size = st.st_size ; - } - else c->fd = fd ; - return 1 ; -} diff --git a/src/libstddjb/cdb_read.c b/src/libstddjb/cdb_read.c index 1d2f491..1fb15da 100644 --- a/src/libstddjb/cdb_read.c +++ b/src/libstddjb/cdb_read.c @@ -1,24 +1,14 @@ /* ISC license. */ -#include <unistd.h> #include <string.h> #include <errno.h> -#include <skalibs/allreadwrite.h> #include <skalibs/cdb.h> #include <skalibs/posixishard.h> int cdb_read (struct cdb *c, char *buf, unsigned int len, uint32_t pos) { - if (c->map) - { - if ((pos > c->size) || (c->size - pos < len)) return (errno = EPROTO, -1) ; - memcpy(buf, c->map + pos, len) ; - } - else - { - if (lseek(c->fd, pos, SEEK_SET) < 0) return -1 ; - if (allread(c->fd, buf, len) < len) return -1 ; - } + if ((pos > c->size) || (c->size - pos < len)) return (errno = EPROTO, -1) ; + memcpy(buf, c->map + pos, len) ; return 0 ; } |