From 21ff9dbeeec34ead2ceeb7cecfcf9fe025b4e857 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 9 Apr 2015 15:56:56 +1000 Subject: [PATCH] * imdb_db_t->write --- src/database.c | 10 +++++++--- src/database.h | 4 ++-- src/query.c | 2 +- src/sampler.c | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/database.c b/src/database.c index 30cee2c..42c3356 100644 --- a/src/database.c +++ b/src/database.c @@ -60,6 +60,9 @@ int imdb_init(imdb_db_t *db, const char *path) DB_SEEK(db, 0); DB_WRITE(db, buf, IMDB_REC_LEN); + close(db->fd); + db->fd = -1; + if (bytes != IMDB_REC_LEN) { db->errstr = "Can't write database header"; return -1; @@ -68,7 +71,7 @@ int imdb_init(imdb_db_t *db, const char *path) return 0; } -int imdb_open(imdb_db_t *db, const char *path) +int imdb_open(imdb_db_t *db, const char *path, int write) { ssize_t bytes = 0; struct stat st; @@ -87,11 +90,12 @@ int imdb_open(imdb_db_t *db, const char *path) } errno = 0; - if ((db->fd = open(path, OPEN_FLAGS)) == -1) { + if ((db->fd = open(path, write ? O_RDWR : O_RDONLY)) == -1) { db->errstr = strerror(errno); return -1; } - db->path = path; + db->write = write; + db->path = path; memset(buf, 0x0, IMDB_REC_LEN); DB_SEEK(db, 0); diff --git a/src/database.h b/src/database.h index 7f021db..4e80774 100644 --- a/src/database.h +++ b/src/database.h @@ -3,10 +3,10 @@ #define IMDB_REC_LEN 48 #define IMDB_VERSION 2 -#define OPEN_FLAGS O_RDWR typedef struct { int fd; + int write; const char *path; const char *errstr; unsigned char caps[8]; @@ -99,7 +99,7 @@ extern int imdb_read_blk (imdb_db_t *db, imdb_block_t *blk); extern int imdb_write_blk(imdb_db_t *db, imdb_block_t *blk); extern int imdb_init (imdb_db_t *db, const char *path); -extern int imdb_open (imdb_db_t *db, const char *path); +extern int imdb_open (imdb_db_t *db, const char *path, int write); extern int imdb_close(imdb_db_t *db); float diff --git a/src/query.c b/src/query.c index b622eb0..2cef990 100644 --- a/src/query.c +++ b/src/query.c @@ -227,7 +227,7 @@ int main(int argc, char **argv) exit(EXIT_SUCCESS); } - if (imdb_open(&db, db_path) == -1) { + if (imdb_open(&db, db_path, 0) == -1) { printf("database open: %s\n", db.errstr); exit(EXIT_FAILURE); } diff --git a/src/sampler.c b/src/sampler.c index 9d18360..be7eedc 100644 --- a/src/sampler.c +++ b/src/sampler.c @@ -25,7 +25,7 @@ int main(int argc, char **argv) usage(); } - if (imdb_open(&db, argv[1]) != 0) { + if (imdb_open(&db, argv[1], 1) != 0) { puts(db.errstr); usage(); }