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. 10
      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;";
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)

10
src/database.h

@ -98,7 +98,15 @@ 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);
/**
* @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);

4
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;

Loading…
Cancel
Save