From b5602a0973c9647c52accadc7440bfdcac18a180 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 23 Mar 2018 15:51:17 +0100 Subject: [PATCH] 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 --- gossipd/gossip_store.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index 3be44017f..3149f5511 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -7,10 +7,12 @@ #include #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;