commit 0b71c907f1b957781c5850a7445e3fd6abbdfb43
parent 51940c0f0f8def58517f3ef932b9b657cf556f18
Author: sin <sin@2f30.org>
Date: Sun, 12 May 2019 20:14:09 +0100
Split some more pack/unpack from write/read
Diffstat:
M | bstorage.c | | | 63 | ++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 34 insertions(+), 29 deletions(-)
diff --git a/bstorage.c b/bstorage.c
@@ -154,18 +154,11 @@ packbhdr(unsigned char *buf, struct bhdr *bhdr)
/* Unpack block descriptor */
static int
-unpackbd(int fd, struct bd *bd)
+unpackbd(unsigned char *buf, struct bd *bd)
{
- unsigned char buf[BDSIZE];
char fmt[BUFSIZ];
int n;
- if (xread(fd, buf, BDSIZE) != BDSIZE) {
- seterr("failed to read block descriptor: %s",
- strerror(errno));
- return -1;
- }
-
snprintf(fmt, sizeof(fmt), "s'6qqq'%d", MDSIZE);
n = unpack(buf, fmt,
&bd->type,
@@ -181,9 +174,8 @@ unpackbd(int fd, struct bd *bd)
/* Write block descriptor */
static int
-packbd(int fd, struct bd *bd)
+packbd(unsigned char *buf, struct bd *bd)
{
- unsigned char buf[BDSIZE];
char fmt[BUFSIZ];
int n;
@@ -197,11 +189,6 @@ packbd(int fd, struct bd *bd)
bd->md);
assert(n == BDSIZE);
- if (xwrite(fd, buf, n) != n) {
- seterr("failed to write block descriptor: %s",
- strerror(errno));
- return -1;
- }
return n;
}
@@ -209,6 +196,7 @@ packbd(int fd, struct bd *bd)
static int
loadbd(struct sctx *sctx)
{
+ unsigned char bdbuf[BDSIZE];
struct bd *bd;
bd = calloc(1, sizeof(*bd));
@@ -217,10 +205,13 @@ loadbd(struct sctx *sctx)
return -1;
}
- if (unpackbd(sctx->fd, bd) < 0) {
+ if (xread(sctx->fd, bdbuf, BDSIZE) != BDSIZE) {
free(bd);
+ seterr("failed to read block descriptor: %s",
+ strerror(errno));
return -1;
}
+ unpackbd(bdbuf, bd);
if (bd->type != BDTYPE) {
free(bd);
@@ -287,7 +278,7 @@ initbdcache(struct sctx *sctx)
static int
bscreat(struct bctx *bctx, char *path, int mode)
{
- unsigned char buf[BHDRSIZE];
+ unsigned char bhdrbuf[BHDRSIZE];
struct sctx *sctx;
struct bhdr *bhdr;
int fd;
@@ -320,8 +311,8 @@ bscreat(struct bctx *bctx, char *path, int mode)
bhdr->flags = (VMAJ << VMAJSHIFT) | VMIN;
bhdr->nbd = 0;
- packbhdr(buf, bhdr);
- if (xwrite(fd, buf, BHDRSIZE) != BHDRSIZE) {
+ packbhdr(bhdrbuf, bhdr);
+ if (xwrite(fd, bhdrbuf, BHDRSIZE) != BHDRSIZE) {
free(sctx);
close(fd);
seterr("failed to write block header: %s", strerror(errno));
@@ -334,7 +325,7 @@ bscreat(struct bctx *bctx, char *path, int mode)
static int
bsopen(struct bctx *bctx, char *path, int flags, int mode)
{
- unsigned char buf[BHDRSIZE];
+ unsigned char bhdrbuf[BHDRSIZE];
struct sctx *sctx;
struct bhdr *bhdr;
int fd, algo;
@@ -374,13 +365,13 @@ bsopen(struct bctx *bctx, char *path, int flags, int mode)
SLIST_INIT(&sctx->gchead);
bhdr = &sctx->bhdr;
- if (xread(fd, buf, BHDRSIZE) != BHDRSIZE) {
+ if (xread(fd, bhdrbuf, BHDRSIZE) != BHDRSIZE) {
free(sctx);
close(fd);
seterr("failed to read block header: %s", strerror(errno));
return -1;
}
- unpackbhdr(buf, bhdr);
+ unpackbhdr(bhdrbuf, bhdr);
if (memcmp(bhdr->magic, BHDRMAGIC, NBHDRMAGIC) != 0) {
free(sctx);
@@ -412,6 +403,7 @@ bsopen(struct bctx *bctx, char *path, int flags, int mode)
static int
bsput(struct bctx *bctx, void *buf, size_t n, unsigned char *md)
{
+ unsigned char bdbuf[BDSIZE];
struct sctx *sctx;
struct bhdr *bhdr;
struct bd key, *bd;
@@ -439,8 +431,11 @@ bsput(struct bctx *bctx, void *buf, size_t n, unsigned char *md)
}
bd->refcnt++;
- if (packbd(sctx->fd, bd) < 0) {
+ packbd(bdbuf, bd);
+ if (xwrite(sctx->fd, bdbuf, BDSIZE) != BDSIZE) {
bd->refcnt--;
+ seterr("failed to write block descriptor: %s",
+ strerror(errno));
return -1;
}
@@ -466,8 +461,13 @@ bsput(struct bctx *bctx, void *buf, size_t n, unsigned char *md)
bd->refcnt = 1;
memcpy(bd->md, key.md, MDSIZE);
- if (packbd(sctx->fd, bd) < 0) {
+ packbd(bdbuf, bd);
+ if (xwrite(sctx->fd, bdbuf, BDSIZE) != BDSIZE) {
+ /* Shouldn't fail but if it does rewind storage file state */
+ ftruncate(sctx->fd, offs);
free(bd);
+ seterr("failed to write block descriptor: %s",
+ strerror(errno));
return -1;
}
@@ -528,6 +528,7 @@ bsget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n)
static int
bsrm(struct bctx *bctx, unsigned char *md)
{
+ unsigned char bdbuf[BDSIZE];
struct sctx *sctx;
struct bd key, *bd;
off_t bdoffs;
@@ -547,8 +548,11 @@ bsrm(struct bctx *bctx, unsigned char *md)
}
bd->refcnt--;
- if (packbd(sctx->fd, bd) < 0) {
+ packbd(bdbuf, bd);
+ if (xwrite(sctx->fd, bdbuf, BDSIZE) != BDSIZE) {
bd->refcnt++;
+ seterr("failed to write block descriptor: %s",
+ strerror(errno));
return -1;
}
@@ -563,7 +567,8 @@ bsrm(struct bctx *bctx, unsigned char *md)
*/
lseek(sctx->fd, bdoffs, SEEK_SET);
bd->refcnt++;
- packbd(sctx->fd, bd);
+ packbd(bdbuf, bd);
+ xwrite(sctx->fd, bdbuf, BDSIZE);
seterr("operation not supported");
return -1;
}
@@ -657,7 +662,7 @@ bscheck(struct bctx *bctx, unsigned char *md)
static int
bssync(struct bctx *bctx)
{
- unsigned char buf[BHDRSIZE];
+ unsigned char bhdrbuf[BHDRSIZE];
struct sctx *sctx;
struct bhdr *bhdr;
@@ -671,8 +676,8 @@ bssync(struct bctx *bctx)
}
bhdr = &sctx->bhdr;
- packbhdr(buf, bhdr);
- if (xwrite(sctx->fd, buf, BHDRSIZE) != BHDRSIZE) {
+ packbhdr(bhdrbuf, bhdr);
+ if (xwrite(sctx->fd, bhdrbuf, BHDRSIZE) != BHDRSIZE) {
seterr("failed to write block header: %s", strerror(errno));
return -1;
}