Browse Source

* imdb_init() :

* renamed to imdb_create()
  * changed signature
master
Alex 'AdUser' Z 8 years ago
parent
commit
9467c48744
  1. 28
      src/database.c
  2. 14
      src/database.h
  3. 4
      src/imdb-write.c

28
src/database.c

@ -37,30 +37,28 @@
const char *imdb_hdr_fmt = "IMDB v%02u, CAPS: %s;"; 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; ssize_t bytes = 0;
unsigned char buf[IMDB_REC_LEN]; 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); memset(buf, 0x0, sizeof(char) * IMDB_REC_LEN);
if ((db->fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) { if ((fd = creat(path, 0644)) < 0)
db->errstr = strerror(errno); return result;
return -1;
}
snprintf((char *) buf, IMDB_REC_LEN, imdb_hdr_fmt, IMDB_VERSION, "M-R"); snprintf((char *) buf, IMDB_REC_LEN, imdb_hdr_fmt, IMDB_VERSION, caps);
DB_WRITE(db, buf, IMDB_REC_LEN, 0);
close(db->fd); bytes = pwrite(fd, buf, IMDB_REC_LEN, 0);
db->fd = -1; if (bytes == IMDB_REC_LEN)
result = true; /* success */
if (bytes != IMDB_REC_LEN) { close(fd);
db->errstr = "Can't write database header";
return -1;
}
return 0; return result;
} }
int imdb_open(imdb_db_t *db, const char *path, int write) int imdb_open(imdb_db_t *db, const char *path, int write)

14
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_read_blk (imdb_db_t *db, imdb_block_t *blk);
int imdb_write_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); * @brief Creates empty database at given path
int imdb_close(imdb_db_t *db); * @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 float
ratio_from_rec_data(unsigned char * const data); ratio_from_rec_data(unsigned char * const data);

4
src/imdb-write.c

@ -92,8 +92,8 @@ int main(int argc, char **argv)
} }
break; break;
case init : case init :
if (imdb_init(&db, db_path) == -1) { if (!imdb_create(db_path)) {
fprintf(stderr, "database init: %s\n", db.errstr); fprintf(stderr, "database init: %s\n", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
break; break;

Loading…
Cancel
Save