From d6f7078aab3050629a63d9b9d92f30e1ca5a9aa1 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sat, 24 Jul 2021 15:04:18 +0000 Subject: Add cdb_init_at and cdb_init_fromfd, rename cdb_reader Signed-off-by: Laurent Bercot --- src/include/skalibs/cdb.h | 13 +++++++------ src/libstddjb/cdb_find.c | 2 +- src/libstddjb/cdb_init.c | 24 +++++------------------- src/libstddjb/cdb_init_at.c | 18 ++++++++++++++++++ src/libstddjb/cdb_init_fromfd.c | 23 +++++++++++++++++++++++ src/libstddjb/cdb_reader_zero.c | 5 ----- 6 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 src/libstddjb/cdb_init_at.c create mode 100644 src/libstddjb/cdb_init_fromfd.c delete mode 100644 src/libstddjb/cdb_reader_zero.c (limited to 'src') diff --git a/src/include/skalibs/cdb.h b/src/include/skalibs/cdb.h index 9b05b22..73a13ce 100644 --- a/src/include/skalibs/cdb.h +++ b/src/include/skalibs/cdb.h @@ -16,8 +16,8 @@ struct cdb_s #define CDB_ZERO { .map = 0, .size = 0 } extern cdb const cdb_zero ; -typedef struct cdb_reader_s cdb_reader, *cdb_reader_ref ; -struct cdb_reader_s +typedef struct cdb_find_state_s cdb_find_state, *cdb_find_state_ref ; +struct cdb_find_state_s { uint32_t loop ; uint32_t khash ; @@ -25,8 +25,7 @@ struct cdb_reader_s uint32_t hpos ; uint32_t hslots ; } ; -#define CDB_READER_ZERO { .loop = 0, .khash = 0, .kpos = 0, .hpos = 0, .hslots = 0 } -extern cdb_reader const cdb_reader_zero ; +#define CDB_FIND_STATE_ZERO { .loop = 0, .khash = 0, .kpos = 0, .hpos = 0, .hslots = 0 } typedef struct cdb_data_s cdb_data, *cdb_data_ref ; struct cdb_data_s @@ -37,12 +36,14 @@ struct cdb_data_s extern void cdb_free (cdb *) ; extern int cdb_init (cdb *, char const *) ; +extern int cdb_init_at (cdb *, int, char const *) ; +extern int cdb_init_fromfd (cdb *, int) ; #define cdb_findstart(d) ((d)->loop = 0) -extern int cdb_find (cdb const *, cdb_reader *, cdb_data *, char const *, uint32_t) ; +extern int cdb_find (cdb const *, cdb_data *, char const *, uint32_t, cdb_find_state *) ; #define CDB_TRAVERSE_INIT() 2048 -#define cdb_traverse_init(pos) (*pos = 2048) +#define cdb_traverse_init(pos) (*(pos) = 2048) extern int cdb_traverse_next (cdb const *, cdb_data *, cdb_data *, uint32_t *) ; #endif diff --git a/src/libstddjb/cdb_find.c b/src/libstddjb/cdb_find.c index a9c66e3..af642cc 100644 --- a/src/libstddjb/cdb_find.c +++ b/src/libstddjb/cdb_find.c @@ -7,7 +7,7 @@ #include #include "cdb-internal.h" -int cdb_find (cdb const *c, cdb_reader *d, cdb_data *out, char const *key, uint32_t len) +int cdb_find (cdb const *c, cdb_data *out, char const *key, uint32_t len, cdb_find_state *d) { if (!d->loop) { diff --git a/src/libstddjb/cdb_init.c b/src/libstddjb/cdb_init.c index 7a6c2f6..fad6a2c 100644 --- a/src/libstddjb/cdb_init.c +++ b/src/libstddjb/cdb_init.c @@ -1,31 +1,17 @@ /* ISC license. */ -#include -#include -#include -#include - #include #include int cdb_init (cdb *c, char const *file) { - char *map ; - struct stat st ; int fd = openc_read(file) ; if (fd < 0) return 0 ; - if (fstat(fd, &st) < 0) goto err ; - if (st.st_size > UINT32_MAX) goto eoverf ; - map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0) ; - if (map == MAP_FAILED) goto err ; - c->map = map ; - c->size = st.st_size ; + if (!cdb_init_fromfd(c, fd)) + { + fd_close(fd) ; + return 0 ; + } fd_close(fd) ; return 1 ; - - eoverf: - errno = EOVERFLOW ; - err: - fd_close(fd) ; - return 0 ; } diff --git a/src/libstddjb/cdb_init_at.c b/src/libstddjb/cdb_init_at.c new file mode 100644 index 0000000..faea790 --- /dev/null +++ b/src/libstddjb/cdb_init_at.c @@ -0,0 +1,18 @@ +/* ISC license. */ + +#include +#include +#include + +int cdb_init_at (cdb *c, int dirfd, char const *file) +{ + int fd = open_readat(dirfd, file) ; + if (fd < 0) return 0 ; + if (!cdb_init_fromfd(c, fd)) + { + fd_close(fd) ; + return 0 ; + } + fd_close(fd) ; + return 1 ; +} diff --git a/src/libstddjb/cdb_init_fromfd.c b/src/libstddjb/cdb_init_fromfd.c new file mode 100644 index 0000000..7446f4a --- /dev/null +++ b/src/libstddjb/cdb_init_fromfd.c @@ -0,0 +1,23 @@ +/* ISC license. */ + +#include + +#include +#include +#include +#include + +#include + +int cdb_init_fromfd (cdb *c, int fd) +{ + char *map ; + struct stat st ; + 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->map = map ; + c->size = st.st_size ; + return 1 ; +} diff --git a/src/libstddjb/cdb_reader_zero.c b/src/libstddjb/cdb_reader_zero.c deleted file mode 100644 index 1dbb275..0000000 --- a/src/libstddjb/cdb_reader_zero.c +++ /dev/null @@ -1,5 +0,0 @@ -/* ISC license. */ - -#include - -cdb_reader const cdb_reader_zero = CDB_READER_ZERO ; -- cgit v1.2.3