Skip to content

Commit

Permalink
Add database version check and update magic number
Browse files Browse the repository at this point in the history
  • Loading branch information
horta committed Nov 30, 2023
1 parent dcc55d6 commit e46e04a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
11 changes: 8 additions & 3 deletions c-core/database_reader.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "database_reader.h"
#include "database_version.h"
#include "defer_return.h"
#include "error.h"
#include "imm/imm.h"
#include "lip/1darray/1darray.h"
#include "magic_number.h"
#include "rc.h"
Expand Down Expand Up @@ -30,12 +30,17 @@ int database_reader_open(struct database_reader *x, char const *filename)
if ((rc = unpack_mapsize(&x->file, 2))) defer_return(rc);

if ((rc = unpack_key(&x->file, "header"))) defer_return(rc);
if ((rc = unpack_mapsize(&x->file, 7))) defer_return(rc);
if ((rc = unpack_mapsize(&x->file, 8))) defer_return(rc);

int magic_number = 0;
if ((rc = unpack_key(&x->file, "magic_number"))) defer_return(rc);
if ((rc = unpack_int(&x->file, &magic_number))) defer_return(rc);
if (magic_number != MAGIC_NUMBER) defer_return(error(DCP_EFDATA));
if (magic_number != MAGIC_NUMBER) defer_return(error(DCP_ENOTDBFILE));

int version = 0;
if ((rc = unpack_key(&x->file, "version"))) defer_return(rc);
if ((rc = unpack_int(&x->file, &version))) defer_return(rc);
if (version != DATABASE_VERSION) defer_return(error(DCP_EDBVERSION));

if ((rc = unpack_key(&x->file, "entry_dist"))) defer_return(rc);
if ((rc = unpack_int(&x->file, &x->entry_dist))) defer_return(rc);
Expand Down
6 changes: 6 additions & 0 deletions c-core/database_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DATABASE_VERSION_H
#define DATABASE_VERSION_H

#define DATABASE_VERSION 0x01

#endif
7 changes: 5 additions & 2 deletions c-core/database_writer.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "database_writer.h"
#include "database_version.h"
#include "defer_return.h"
#include "entry_dist.h"
#include "error.h"
#include "fs.h"
#include "lip/1darray/1darray.h"
Expand Down Expand Up @@ -72,7 +72,7 @@ static int pack_header(struct database_writer *db)
struct lip_file *stream = &db->file;

if ((rc = pack_key(stream, "header"))) return rc;
if ((rc = pack_mapsize(stream, 7))) return rc;
if ((rc = pack_mapsize(stream, 8))) return rc;

FILE *src = lip_file_ptr(&db->tmp.header);
FILE *dst = lip_file_ptr(stream);
Expand Down Expand Up @@ -134,6 +134,9 @@ int database_writer_open(struct database_writer *x, FILE *restrict fp)
if ((rc = pack_key(stream, "magic_number"))) defer_return(rc);
if ((rc = pack_int(stream, MAGIC_NUMBER))) defer_return(rc);

if ((rc = pack_key(stream, "version"))) defer_return(rc);
if ((rc = pack_int(stream, DATABASE_VERSION))) defer_return(rc);

if ((rc = pack_key(stream, "entry_dist"))) defer_return(rc);
if ((rc = pack_int(stream, p->entry_dist))) defer_return(rc);

Expand Down
2 changes: 1 addition & 1 deletion c-core/magic_number.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef MAGIC_NUMBER_H
#define MAGIC_NUMBER_H

#define MAGIC_NUMBER 0xC6F0
#define MAGIC_NUMBER 0xC6F1

#endif
2 changes: 2 additions & 0 deletions c-core/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ enum rc
DCP_EINVALSIZE = 65,
DCP_EENDOFFILE = 66,
DCP_EENDOFNODES = 67,
DCP_EDBVERSION = 68,
DCP_ENOTDBFILE = 69,
};
// clang-format on

Expand Down
2 changes: 2 additions & 0 deletions c-core/string_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static char const *msg[] = {
[DCP_EINVALSIZE] = "invalid size",
[DCP_EENDOFFILE] = "unexpected end of file",
[DCP_EENDOFNODES] = "unexpected end of nodes",
[DCP_EDBVERSION] = "unsupported database version",
[DCP_ENOTDBFILE] = "not a database file",
};

char const *string_error(int error_code)
Expand Down
2 changes: 1 addition & 1 deletion c-core/test_press.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main(void)
eq(press_close(press), 0);
press_del(press);

eq(filesize(DBFILE), 3536720);
eq(filesize(DBFILE), 3536729);

fs_rmfile(DBFILE);
return lfails;
Expand Down

0 comments on commit e46e04a

Please sign in to comment.