commit 7c3a4a21592d3c11e418e00964caadaec81613a5
parent ba824c61fa56f57bb37251927c5b8d2b687167af
Author: sin <sin@2f30.org>
Date: Sun, 12 May 2019 19:30:52 +0100
Split pack/unpack from write/read
Diffstat:
M | state.c | | | 31 | ++++++++++++++----------------- |
1 file changed, 14 insertions(+), 17 deletions(-)
diff --git a/state.c b/state.c
@@ -36,42 +36,32 @@ struct shdr {
uint64_t flags;
};
-/* Read state header */
+/* Unpack state header */
static int
-unpackshdr(int fd, struct shdr *shdr)
+unpackshdr(unsigned char *buf, struct shdr *shdr)
{
- unsigned char buf[SHDRSIZE];
int n;
- if (xread(fd, buf, SHDRSIZE) != SHDRSIZE) {
- seterr("failed to read state header: %s", strerror(errno));
- return -1;
- }
-
n = unpack(buf, "q", &shdr->flags);
assert(n == SHDRSIZE);
return n;
}
-/* Write state header */
+/* Pack state header */
static int
-packshdr(int fd, struct shdr *shdr)
+packshdr(unsigned char *buf, struct shdr *shdr)
{
- unsigned char buf[SHDRSIZE];
int n;
n = pack(buf, "q", shdr->flags);
assert(n == SHDRSIZE);
- if (xwrite(fd, buf, n) != n) {
- seterr("failed to write state header: %s", strerror(errno));
- return -1;
- }
return n;
}
int
writestate(int fd, struct param *par)
{
+ unsigned char buf[SHDRSIZE];
struct shdr shdr;
/* Set version */
@@ -99,19 +89,26 @@ writestate(int fd, struct param *par)
return -1;
}
- if (packshdr(fd, &shdr) < 0)
+ packshdr(buf, &shdr);
+ if (xwrite(fd, buf, SHDRSIZE) != SHDRSIZE) {
+ seterr("failed to write state header: %s", strerror(errno));
return -1;
+ }
return 0;
}
int
readstate(int fd, struct param *par)
{
+ unsigned char buf[SHDRSIZE];
struct shdr shdr;
int algo;
- if (unpackshdr(fd, &shdr) < 0)
+ if (xread(fd, buf, SHDRSIZE) != SHDRSIZE) {
+ seterr("failed to read state header: %s", strerror(errno));
return -1;
+ }
+ unpackshdr(buf, &shdr);
/* If the major version is different, the format is incompatible */
if (((shdr.flags >> VMAJSHIFT) & VMAJMASK) != VMAJ) {