commit 1c0530ef5bf190a62ca8ce7c9b3ae14702b44a7f
parent d13482ec75f44aea5e16f7942945a9263d4736f5
Author: sin <sin@2f30.org>
Date: Tue, 14 May 2019 13:27:44 +0300
Split ssync() into plaintext/encryption variants
Diffstat:
M | snap.c | | | 121 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
1 file changed, 68 insertions(+), 53 deletions(-)
diff --git a/snap.c b/snap.c
@@ -385,12 +385,74 @@ srewind(struct sctx *sctx)
return 0;
}
-int
-ssync(struct sctx *sctx)
+static int
+syncnone(struct sctx *sctx)
{
+ unsigned char hdr[SHDRSIZE];
+ struct mdnode *mdnode;
struct shdr *shdr;
+
+ shdr = &sctx->shdr;
+ packshdr(hdr, shdr);
+ if (xwrite(sctx->fd, hdr, SHDRSIZE) != SHDRSIZE) {
+ seterr("failed to write snapshot header: %s", strerror(errno));
+ return -1;
+ }
+
+ TAILQ_FOREACH(mdnode, &sctx->mdhead, e) {
+ if (xwrite(sctx->fd, mdnode->md, MDSIZE) != MDSIZE) {
+ seterr("failed to write block hash: %s",
+ strerror(errno));
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static int
+synccrypto(struct sctx *sctx)
+{
+ unsigned char hdr[SHDRSIZE];
+ crypto_secretstream_xchacha20poly1305_state state;
struct mdnode *mdnode;
+ struct shdr *shdr;
+
+ shdr = &sctx->shdr;
+ crypto_secretstream_xchacha20poly1305_init_push(&state,
+ shdr->header,
+ param.key);
+
+ packshdr(hdr, shdr);
+ if (xwrite(sctx->fd, hdr, SHDRSIZE) != SHDRSIZE) {
+ seterr("failed to write snapshot header: %s", strerror(errno));
+ return -1;
+ }
+
+ TAILQ_FOREACH(mdnode, &sctx->mdhead, e) {
+ unsigned char buf[MDSIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
+ unsigned char tag;
+
+ if (TAILQ_LAST(&sctx->mdhead, mdhead) == mdnode)
+ tag = crypto_secretstream_xchacha20poly1305_TAG_FINAL;
+ else
+ tag = 0;
+
+ crypto_secretstream_xchacha20poly1305_push(&state,
+ buf, NULL,
+ mdnode->md, MDSIZE,
+ hdr, SHDRSIZE, tag);
+ if (xwrite(sctx->fd, buf, sizeof(buf)) != sizeof(buf)) {
+ seterr("failed to write block hash: %s",
+ strerror(errno));
+ return -1;
+ }
+ }
+ return 0;
+}
+int
+ssync(struct sctx *sctx)
+{
if (sctx == NULL) {
seterr("invalid params");
return -1;
@@ -404,59 +466,12 @@ ssync(struct sctx *sctx)
return -1;
}
- shdr = &sctx->shdr;
- if (sctx->crypto) {
- unsigned char hdr[SHDRSIZE];
- crypto_secretstream_xchacha20poly1305_state state;
-
- crypto_secretstream_xchacha20poly1305_init_push(&state,
- shdr->header,
- param.key);
-
- packshdr(hdr, shdr);
- if (xwrite(sctx->fd, hdr, SHDRSIZE) != SHDRSIZE) {
- seterr("failed to write snapshot header: %s", strerror(errno));
- return -1;
- }
-
- TAILQ_FOREACH(mdnode, &sctx->mdhead, e) {
- unsigned char buf[MDSIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
- unsigned char tag;
-
- if (TAILQ_LAST(&sctx->mdhead, mdhead) == mdnode)
- tag = crypto_secretstream_xchacha20poly1305_TAG_FINAL;
- else
- tag = 0;
-
- crypto_secretstream_xchacha20poly1305_push(&state,
- buf, NULL,
- mdnode->md, MDSIZE,
- hdr, SHDRSIZE, tag);
- if (xwrite(sctx->fd, buf, sizeof(buf)) != sizeof(buf)) {
- seterr("failed to write block hash: %s",
- strerror(errno));
- return -1;
- }
- }
- } else {
- unsigned char hdr[SHDRSIZE];
-
- packshdr(hdr, shdr);
- if (xwrite(sctx->fd, hdr, SHDRSIZE) != SHDRSIZE) {
- seterr("failed to write snapshot header: %s", strerror(errno));
- return -1;
- }
+ if (!sctx->crypto)
+ syncnone(sctx);
+ else
+ synccrypto(sctx);
- TAILQ_FOREACH(mdnode, &sctx->mdhead, e) {
- if (xwrite(sctx->fd, mdnode->md, MDSIZE) != MDSIZE) {
- seterr("failed to write block hash: %s",
- strerror(errno));
- return -1;
- }
- }
- }
fsync(sctx->fd);
-
return 0;
}