From 9467c4874468c6fb6f0d03d123632324b4580028 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Tue, 27 Dec 2016 20:49:42 +1000 Subject: [PATCH] * imdb_init() : * renamed to imdb_create() * changed signature --- src/database.c | 28 +++++++++++++--------------- src/database.h | 14 +++++++++++--- src/imdb-write.c | 4 ++-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/database.c b/src/database.c index 5d892be..ffcf9c9 100644 --- a/src/database.c +++ b/src/database.c @@ -37,30 +37,28 @@ const char *imdb_hdr_fmt = "IMDB v%02u, CAPS: %s;"; -int imdb_init(imdb_db_t *db, const char *path) -{ +bool +imdb_create(const char *path) { ssize_t bytes = 0; unsigned char buf[IMDB_REC_LEN]; + const char *caps = "M-R"; + bool result = false; + int fd = -1; 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; - } + if ((fd = creat(path, 0644)) < 0) + return result; - snprintf((char *) buf, IMDB_REC_LEN, imdb_hdr_fmt, IMDB_VERSION, "M-R"); - DB_WRITE(db, buf, IMDB_REC_LEN, 0); + snprintf((char *) buf, IMDB_REC_LEN, imdb_hdr_fmt, IMDB_VERSION, caps); - close(db->fd); - db->fd = -1; + bytes = pwrite(fd, buf, IMDB_REC_LEN, 0); + if (bytes == IMDB_REC_LEN) + result = true; /* success */ - if (bytes != IMDB_REC_LEN) { - db->errstr = "Can't write database header"; - return -1; - } + close(fd); - return 0; + return result; } int imdb_open(imdb_db_t *db, const char *path, int write) diff --git a/src/database.h b/src/database.h index bf47cb3..136e35f 100644 --- a/src/database.h +++ b/src/database.h @@ -98,9 +98,17 @@ int imdb_write_rec(imdb_db_t *db, imdb_rec_t *rec); int imdb_read_blk (imdb_db_t *db, imdb_block_t *blk); int imdb_write_blk(imdb_db_t *db, imdb_block_t *blk); -int imdb_init (imdb_db_t *db, const char *path); -int imdb_open (imdb_db_t *db, const char *path, int write); -int imdb_close(imdb_db_t *db); +/** + * @brief Creates empty database at given path + * @param path Where to create database + * @returns true on success, and false on error + * @note See errno value for details + * @todo 2nd arg: caps + */ +bool imdb_create(const char *path); + +int imdb_open (imdb_db_t *db, const char *path, int write); +int imdb_close (imdb_db_t *db); float ratio_from_rec_data(unsigned char * const data); diff --git a/src/imdb-write.c b/src/imdb-write.c index 11e0116..eefe44a 100644 --- a/src/imdb-write.c +++ b/src/imdb-write.c @@ -92,8 +92,8 @@ int main(int argc, char **argv) } break; case init : - if (imdb_init(&db, db_path) == -1) { - fprintf(stderr, "database init: %s\n", db.errstr); + if (!imdb_create(db_path)) { + fprintf(stderr, "database init: %s\n", strerror(errno)); exit(EXIT_FAILURE); } break;