commit d6f7078aab3050629a63d9b9d92f30e1ca5a9aa1
parent dd6bb6c6b8298ebeff2d1882becb36580b969d6f
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Sat, 24 Jul 2021 15:04:18 +0000
Add cdb_init_at and cdb_init_fromfd, rename cdb_reader
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat:
7 files changed, 57 insertions(+), 33 deletions(-)
diff --git a/package/deps.mak b/package/deps.mak
@@ -295,11 +295,12 @@ src/libstddjb/cdb_find.o src/libstddjb/cdb_find.lo: src/libstddjb/cdb_find.c src
src/libstddjb/cdb_free.o src/libstddjb/cdb_free.lo: src/libstddjb/cdb_free.c src/include/skalibs/cdb.h src/include/skalibs/posixplz.h
src/libstddjb/cdb_hash.o src/libstddjb/cdb_hash.lo: src/libstddjb/cdb_hash.c src/libstddjb/cdb-internal.h
src/libstddjb/cdb_init.o src/libstddjb/cdb_init.lo: src/libstddjb/cdb_init.c src/include/skalibs/cdb.h src/include/skalibs/djbunix.h
+src/libstddjb/cdb_init_at.o src/libstddjb/cdb_init_at.lo: src/libstddjb/cdb_init_at.c src/include/skalibs/cdb.h src/include/skalibs/djbunix.h src/include/skalibs/unix-transactional.h
+src/libstddjb/cdb_init_fromfd.o src/libstddjb/cdb_init_fromfd.lo: src/libstddjb/cdb_init_fromfd.c src/include/skalibs/bsdsnowflake.h src/include/skalibs/cdb.h
src/libstddjb/cdb_p.o src/libstddjb/cdb_p.lo: src/libstddjb/cdb_p.c src/libstddjb/cdb-internal.h src/include/skalibs/cdb.h
-src/libstddjb/cdb_reader_zero.o src/libstddjb/cdb_reader_zero.lo: src/libstddjb/cdb_reader_zero.c src/include/skalibs/cdb.h
src/libstddjb/cdb_traverse_next.o src/libstddjb/cdb_traverse_next.lo: src/libstddjb/cdb_traverse_next.c src/libstddjb/cdb-internal.h src/include/skalibs/cdb.h src/include/skalibs/uint32.h
src/libstddjb/cdb_zero.o src/libstddjb/cdb_zero.lo: src/libstddjb/cdb_zero.c src/include/skalibs/cdb.h
-src/libstddjb/cdbmake.o src/libstddjb/cdbmake.lo: src/libstddjb/cdbmake.c src/include/skalibs/buffer.h src/include/skalibs/cdbmake.h src/include/skalibs/diuint32.h src/include/skalibs/genalloc.h src/include/skalibs/uint32.h
+src/libstddjb/cdbmake.o src/libstddjb/cdbmake.lo: src/libstddjb/cdbmake.c src/include/skalibs/buffer.h src/libstddjb/cdb-internal.h src/include/skalibs/cdbmake.h src/include/skalibs/diuint32.h src/include/skalibs/genalloc.h src/include/skalibs/uint32.h
src/libstddjb/child_spawn.o src/libstddjb/child_spawn.lo: src/libstddjb/child_spawn.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/strerr2.h src/include/skalibs/sysdeps.h src/include/skalibs/types.h
src/libstddjb/child_spawn0.o src/libstddjb/child_spawn0.lo: src/libstddjb/child_spawn0.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/strerr2.h src/include/skalibs/sysdeps.h
src/libstddjb/child_spawn1_internal.o src/libstddjb/child_spawn1_internal.lo: src/libstddjb/child_spawn1_internal.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/strerr2.h src/include/skalibs/sysdeps.h
diff --git 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
@@ -7,7 +7,7 @@
#include <skalibs/cdb.h>
#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
@@ -1,31 +1,17 @@
/* ISC license. */
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <stdint.h>
-#include <errno.h>
-
#include <skalibs/djbunix.h>
#include <skalibs/cdb.h>
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
@@ -0,0 +1,18 @@
+/* ISC license. */
+
+#include <skalibs/djbunix.h>
+#include <skalibs/cdb.h>
+#include <skalibs/unix-transactional.h>
+
+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
@@ -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_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
@@ -1,5 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/cdb.h>
-
-cdb_reader const cdb_reader_zero = CDB_READER_ZERO ;