nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit d5cedc6f28a60301bad21a16fdb047fdb82ff883
parent 3430edf9a8497ae4552d44b753a674da9167b577
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu,  9 Apr 2020 20:37:12 -0400

nserver: Add db.h

* nserver/.gitignore: Add nserver.db
* nserver/Makefile ($(PROGRAMS))
(tests): Link gdbm.
(clean): Remove nserver.db
* nserver/src/db.c (db_init): New function.
* nserver/src/db.h (db_init): New function declaration.
* nserver/tests/db_tests.c: Tests for db.h

Diffstat:
nserver/.gitignore | 2++
nserver/Makefile | 5+++--
nserver/src/db.c | 20++++++++++++++++++++
nserver/src/db.h | 11+++++++++++
nserver/tests/db_tests.c | 21+++++++++++++++++++++
5 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/nserver/.gitignore b/nserver/.gitignore @@ -1 +1,2 @@ tests/*_tests +nserver.db+ \ No newline at end of file diff --git a/nserver/Makefile b/nserver/Makefile @@ -28,7 +28,7 @@ $(TARGET): build $(OBJECTS) $(SO_TARGET): $(TARGET) $(OBJECTS) $(CC) -shared -o $@ $(OBJECTS) -$(PROGRAMS): LDLIBS += $(TARGET) -lm +$(PROGRAMS): LDLIBS += $(TARGET) -lm -L/usr/local/lib -lgdbm build: @mkdir -p build @@ -36,7 +36,7 @@ build: # The Unit Tests .PHONY: tests -tests: LDLIBS += $(TARGET) -lm +tests: LDLIBS += $(TARGET) -lm -L/usr/local/lib -lgdbm tests: $(TESTS) sh ./tests/runtests.sh @@ -46,6 +46,7 @@ clean: rm -f tests/tests.log find . -name "*.gc" -exec rm {} \; rm -rf `find . -name "*.dSYM" -print` + rm -rf nserver.db # The Install install: all diff --git a/nserver/src/db.c b/nserver/src/db.c @@ -0,0 +1,20 @@ +#include <db.h> + + +static const char *DB_FILE = "nserver.db"; + +int db_init() +{ + // Create DB if it's not already created. + GDBM_FILE gf = gdbm_open(DB_FILE, 0, GDBM_WRCREAT, + S_IRUSR|S_IWUSR, NULL); + check(gf != NULL, "unable to init db"); + + // Close the DB. + int rc = gdbm_close(gf); + check(rc == 0, "error closing db after init"); + + return 0; + error: + return -1; +} diff --git a/nserver/src/db.h b/nserver/src/db.h @@ -0,0 +1,11 @@ +#ifndef _db_h +#define _db_h + +#include <gdbm.h> +#include <dbg.h> +#include <sys/stat.h> + +int db_init(); + +#endif + diff --git a/nserver/tests/db_tests.c b/nserver/tests/db_tests.c @@ -0,0 +1,21 @@ +#include "minunit.h" +#include <db.h> + +char *test_db_init() +{ + int rc = db_init(); + mu_assert(rc == 0, "db init failed"); + + return NULL; +} + +char *all_tests() +{ + mu_suite_start(); + + mu_run_test(test_db_init); + + return NULL; +} + +RUN_TESTS(all_tests);