commit 42901e4da4f38681c9cb7defdcce92d5f63b38a6
parent 1e5d78e32e12ac882d187912f60107d025a4dc77
Author: sin <sin@2f30.org>
Date: Fri, 26 Apr 2019 09:00:44 +0100
Rework bparam handling in the storage layer
Diffstat:
M | bstorage.c | | | 67 | +++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 27 insertions(+), 40 deletions(-)
diff --git a/bstorage.c b/bstorage.c
@@ -44,6 +44,10 @@
#define BHDRSIZE (NBHDRMAGIC + 8 + 8)
#define BDSIZE (8 + 8 + 8 + 8 + (MDSIZE))
+#define CNONETYPE 0
+#define CSNAPPYTYPE 1
+#define HBLAKE2BTYPE 0
+
extern int pack(unsigned char *dst, char *fmt, ...);
extern int unpack(unsigned char *src, char *fmt, ...);
@@ -93,19 +97,6 @@ struct sctx {
int rdonly; /* when set to 1, the bssync() operation is a no-op */
};
-#define NCALGOS 2
-static char *ctbl[] = {
- "none",
- "snappy",
- NULL,
-};
-
-#define NHALGOS 1
-static char *htbl[] = {
- "blake2b",
- NULL,
-};
-
static int
bd_cmp(struct bd *b1, struct bd *b2)
{
@@ -318,7 +309,6 @@ bscreat(struct bctx *bctx, char *path, int mode, struct bparam *bpar)
{
struct sctx *sctx;
struct bhdr *bhdr;
- char **algo;
int fd;
fd = open(path, O_RDWR | O_CREAT | O_EXCL, mode);
@@ -337,33 +327,21 @@ bscreat(struct bctx *bctx, char *path, int mode, struct bparam *bpar)
memcpy(bhdr->magic, BHDRMAGIC, NBHDRMAGIC);
bhdr->flags = (VMAJ << VMAJSHIFT) | VMIN;
- /* Set compression algorithm */
- for (algo = ctbl; *algo; algo++) {
- if (strcmp(*algo, bpar->calgo) == 0) {
- uint64_t v;
-
- v = algo - ctbl;
- bhdr->flags |= v << CALGOSHIFT;
- break;
- }
- }
- if (*algo == NULL) {
+ /* Set compression type */
+ if (strcmp(bpar->calgo, "none") == 0) {
+ bhdr->flags |= CNONETYPE << CALGOSHIFT;
+ } else if (strcmp(bpar->calgo, "snappy") == 0) {
+ bhdr->flags |= CSNAPPYTYPE << CALGOSHIFT;
+ } else {
free(sctx);
close(fd);
return -1;
}
- /* Set hash algorithm */
- for (algo = htbl; *algo; algo++) {
- if (strcmp(*algo, bpar->halgo) == 0) {
- uint64_t v;
-
- v = algo - htbl;
- bhdr->flags |= v << HALGOSHIFT;
- break;
- }
- }
- if (*algo == NULL) {
+ /* Set hash type */
+ if (strcmp(bpar->halgo, "blake2b") == 0) {
+ bhdr->flags |= HBLAKE2BTYPE << HALGOSHIFT;
+ } else {
free(sctx);
close(fd);
return -1;
@@ -423,20 +401,29 @@ bsopen(struct bctx *bctx, char *path, int flags, int mode, struct bparam *bpar)
/* Populate bparam */
calgo = (bhdr->flags >> CALGOSHIFT) & CALGOMASK;
- if (calgo < 0 || calgo >= NCALGOS) {
+ switch (calgo) {
+ case CNONETYPE:
+ bpar->calgo = "none";
+ break;
+ case CSNAPPYTYPE:
+ bpar->calgo = "snappy";
+ break;
+ default:
free(sctx);
close(fd);
return -1;
}
- bpar->calgo = ctbl[calgo];
halgo = (bhdr->flags >> HALGOSHIFT) & HALGOMASK;
- if (halgo < 0 || halgo >= NHALGOS) {
+ switch (halgo) {
+ case HBLAKE2BTYPE:
+ bpar->halgo = "blake2b";
+ break;
+ default:
free(sctx);
close(fd);
return -1;
}
- bpar->halgo = htbl[halgo];
sctx->fd = fd;
sctx->rdonly = flags == O_RDONLY;