Browse Source

* imdb_db_t->write

master
Alex 'AdUser' Z 10 years ago
parent
commit
21ff9dbeee
  1. 8
      src/database.c
  2. 4
      src/database.h
  3. 2
      src/query.c
  4. 2
      src/sampler.c

8
src/database.c

@ -60,6 +60,9 @@ int imdb_init(imdb_db_t *db, const char *path)
DB_SEEK(db, 0); DB_SEEK(db, 0);
DB_WRITE(db, buf, IMDB_REC_LEN); DB_WRITE(db, buf, IMDB_REC_LEN);
close(db->fd);
db->fd = -1;
if (bytes != IMDB_REC_LEN) { if (bytes != IMDB_REC_LEN) {
db->errstr = "Can't write database header"; db->errstr = "Can't write database header";
return -1; return -1;
@ -68,7 +71,7 @@ int imdb_init(imdb_db_t *db, const char *path)
return 0; 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; ssize_t bytes = 0;
struct stat st; struct stat st;
@ -87,10 +90,11 @@ int imdb_open(imdb_db_t *db, const char *path)
} }
errno = 0; 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); db->errstr = strerror(errno);
return -1; return -1;
} }
db->write = write;
db->path = path; db->path = path;
memset(buf, 0x0, IMDB_REC_LEN); memset(buf, 0x0, IMDB_REC_LEN);

4
src/database.h

@ -3,10 +3,10 @@
#define IMDB_REC_LEN 48 #define IMDB_REC_LEN 48
#define IMDB_VERSION 2 #define IMDB_VERSION 2
#define OPEN_FLAGS O_RDWR
typedef struct { typedef struct {
int fd; int fd;
int write;
const char *path; const char *path;
const char *errstr; const char *errstr;
unsigned char caps[8]; 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_write_blk(imdb_db_t *db, imdb_block_t *blk);
extern int imdb_init (imdb_db_t *db, const char *path); 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); extern int imdb_close(imdb_db_t *db);
float float

2
src/query.c

@ -227,7 +227,7 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS); 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); printf("database open: %s\n", db.errstr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

2
src/sampler.c

@ -25,7 +25,7 @@ int main(int argc, char **argv)
usage(); usage();
} }
if (imdb_open(&db, argv[1]) != 0) { if (imdb_open(&db, argv[1], 1) != 0) {
puts(db.errstr); puts(db.errstr);
usage(); usage();
} }

Loading…
Cancel
Save