Browse Source

gossip: Separate writing to gossip_store fd from append

We write both when coming from outside, as well as when compacting, so we
extract the write functionality to use it in both cases.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 6 years ago
parent
commit
60efa314fe
  1. 42
      gossipd/gossip_store.c

42
gossipd/gossip_store.c

@ -74,33 +74,45 @@ struct gossip_store *gossip_store_new(const tal_t *ctx,
"Writing version to store: %s", strerror(errno));
return gs;
}
/**
* Write an incoming message to the `gossip_store`
* Given a message and a file descriptor append the message to it.
*
* @param gs The gossip_store to write to
* @param msg The message to write
* @return The newly created and initialized `gossip_store`
* Returns true on success, false on error.
*/
static void gossip_store_append(struct gossip_store *gs, const u8 *msg)
static bool gossip_store_file_append(int fd, const u8 *msg)
{
u32 msglen = tal_len(msg);
beint32_t checksum, belen = cpu_to_be32(msglen);
size_t stale;
/* Only give error message once. */
if (gs->fd == -1)
return;
if (fd == -1)
return true;
checksum = cpu_to_be32(crc32c(0, msg, msglen));
/* FORTIFY_SOURCE gets upset if we don't check return. */
if (write(gs->fd, &belen, sizeof(belen)) != sizeof(belen) ||
write(gs->fd, &checksum, sizeof(checksum)) != sizeof(checksum) ||
write(gs->fd, msg, msglen) != msglen) {
status_broken("Failed writing to gossip store: %s",
strerror(errno));
gs->fd = -1;
return (write(fd, &belen, sizeof(belen)) == sizeof(belen) &&
write(fd, &checksum, sizeof(checksum)) == sizeof(checksum) &&
write(fd, msg, msglen) == msglen);
}
/**
* Write an incoming message to the `gossip_store`
*
* @param gs The gossip_store to write to
* @param msg The message to write
* @return The newly created and initialized `gossip_store`
*/
static void gossip_store_append(struct gossip_store *gs, const u8 *msg)
{
size_t stale;
if (!gossip_store_file_append(gs->fd, msg)) {
status_broken("Failed writing to gossip store: %s",
strerror(errno));
gs->fd = -1;
return;
}
gs->count++;
stale = gs->count - gs->broadcast->count;

Loading…
Cancel
Save