gossip: Add version byte to the gossip_store

If we have a non-empty file and the version doesn't match, then we truncate and
write our own version. If the file is empty we write our version and the
truncate becomes a no-op

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2018-03-23 15:51:17 +01:00 committed by Rusty Russell
parent 0a5ea76d77
commit b5602a0973

View File

@ -7,10 +7,12 @@
#include <unistd.h>
#define GOSSIP_STORE_FILENAME "gossip_store"
static u8 gossip_store_version = 0x01;
struct gossip_store {
int fd;
off_t read_pos, write_pos;
u8 version;
};
static void gossip_store_destroy(struct gossip_store *gs)
@ -22,9 +24,21 @@ struct gossip_store *gossip_store_new(const tal_t *ctx)
{
struct gossip_store *gs = tal(ctx, struct gossip_store);
gs->fd = open(GOSSIP_STORE_FILENAME, O_RDWR|O_APPEND|O_CREAT, 0600);
gs->read_pos = 0;
gs->read_pos = 1;
gs->write_pos = lseek(gs->fd, 0, SEEK_END);
/* Try to read the version, write it if this is a new file, or truncate
* if the version doesn't match */
if (pread(gs->fd, &gs->version, sizeof(gs->version), 0) != 1 ||
gs->version != gossip_store_version) {
status_trace("Truncating gossip_store, either it was empty or "
"the version was not supported.");
gs->version = gossip_store_version;
gs->write_pos = 1;
pwrite(gs->fd, &gossip_store_version, sizeof(gossip_store_version), 0);
ftruncate(gs->fd, gs->write_pos);
}
tal_add_destructor(gs, gossip_store_destroy);
return gs;