From d79d7f65321c8e88f1ba0f97a1820b7b969cfe4f Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 26 Mar 2015 17:29:15 +1000 Subject: [PATCH] + imdb_init() --- src/database.c | 24 ++++++++++++++++++++++++ src/database.h | 1 + 2 files changed, 25 insertions(+) diff --git a/src/database.c b/src/database.c index 10d83b3..d649852 100644 --- a/src/database.c +++ b/src/database.c @@ -47,6 +47,30 @@ const char *imdb_hdr_fmt = "IMDB v%02u, CAPS: %s;"; +int imdb_init(imdb_t *db, const char *path) +{ + ssize_t bytes = 0; + unsigned char buf[IMDB_REC_LEN]; + + memset(buf, 0x0, sizeof(char) * IMDB_REC_LEN); + + if ((db->fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) { + db->errstr = strerror(errno); + return -1; + } + + snprintf((char *) buf, IMDB_REC_LEN, imdb_hdr_fmt, IMDB_VERSION, "M-R"); + DB_SEEK(db, 0); + DB_WRITE(db, buf, IMDB_REC_LEN); + + if (bytes != IMDB_REC_LEN) { + db->errstr = "Can't write database header"; + return -1; + } + + return 0; +} + int imdb_open(imdb_t *db, const char *path) { int init = 0; diff --git a/src/database.h b/src/database.h index a63ad51..40406a6 100644 --- a/src/database.h +++ b/src/database.h @@ -83,6 +83,7 @@ extern int imdb_write_blk(imdb_t *db, imdb_block_t *blk); extern int imdb_read_list (imdb_t *db, imdb_rec_t *list, size_t list_len); extern int imdb_write_list(imdb_t *db, imdb_rec_t *list, size_t list_len); +extern int imdb_init (imdb_t *db, const char *path); extern int imdb_open (imdb_t *db, const char *path); extern int imdb_close(imdb_t *db);