common: expose gossip_store "header and type" single-read struct.

gossip_store.c uses this to avoid two reads, and we want to use it
elsewhere too.

Also fix old comment on gossip_store_readhdr().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2026-02-12 09:25:10 +10:30
parent 1fb4da075f
commit a966dd71ad
2 changed files with 13 additions and 16 deletions

View File

@@ -3,25 +3,17 @@
#include <common/gossip_store_wiregen.h>
#include <unistd.h>
/* We cheat and read first two bytes of message too. */
struct hdr_and_type {
struct gossip_hdr hdr;
be16 type;
};
/* Beware padding! */
#define HDR_AND_TYPE_SIZE (sizeof(struct gossip_hdr) + sizeof(u16))
bool gossip_store_readhdr(int gossip_store_fd, size_t off,
size_t *len,
u32 *timestamp,
u16 *flags,
u16 *type)
{
struct hdr_and_type buf;
struct gossip_hdr_and_type buf;
int r;
r = pread(gossip_store_fd, &buf, HDR_AND_TYPE_SIZE, off);
if (r != HDR_AND_TYPE_SIZE)
r = pread(gossip_store_fd, &buf, GOSSIP_HDR_AND_TYPE_SIZE, off);
if (r != GOSSIP_HDR_AND_TYPE_SIZE)
return false;
if (!(buf.hdr.flags & CPU_TO_BE16(GOSSIP_STORE_COMPLETED_BIT)))
return false;

View File

@@ -50,6 +50,14 @@ struct gossip_hdr {
beint32_t timestamp; /* timestamp of msg. */
};
/* Useful to read gossip_hdr and type of msg. */
struct gossip_hdr_and_type {
struct gossip_hdr hdr;
be16 type;
};
/* Beware padding! */
#define GOSSIP_HDR_AND_TYPE_SIZE (sizeof(struct gossip_hdr) + sizeof(u16))
/**
* Direct store accessor: read gossip msg hdr from store.
* @gossip_store_fd: the readable file descriptor
@@ -59,11 +67,8 @@ struct gossip_hdr {
* @flags (out): if non-NULL, set to the flags.
* @type (out): if non-NULL, set to the msg type.
*
* Returns false if there are no more gossip msgs. If you
* want to read the message, use gossip_store_next, if you
* want to skip, simply add sizeof(gossip_hdr) + *len to *off.
* Note: it's possible that entire record isn't there yet,
* so gossip_store_next can fail.
* Returns false if there are no more gossip msgs, or message
* is incomplete. To iterate, simply add sizeof(gossip_hdr) + *len to off.
*/
bool gossip_store_readhdr(int gossip_store_fd, size_t off,
size_t *len,