diff options
| author | Quentin Carbonneaux | 2022-08-03 14:53:36 +0200 |
|---|---|---|
| committer | Quentin Carbonneaux | 2022-08-03 15:10:08 +0200 |
| commit | 50908608d263758af835e07f19283938a834dd07 (patch) | |
| tree | b09c03ea912cc7d3dbfd737c285136fce8195943 | |
| parent | 7dc2a20b54dbc22536b89020e835583c6f55b4bb (diff) | |
shrink sdar segment headers
| -rw-r--r-- | memex/main.go | 3 | ||||
| -rw-r--r-- | memex/sdar.go | 4 | ||||
| -rw-r--r-- | sdar/all.h | 21 | ||||
| -rw-r--r-- | sdar/arch.c | 55 | ||||
| -rw-r--r-- | sdar/main.c | 19 | ||||
| -rw-r--r-- | sdar/stash.c | 14 | ||||
| -rw-r--r-- | sdar/util.c | 6 |
7 files changed, 63 insertions, 59 deletions
diff --git a/memex/main.go b/memex/main.go index d12a3ad..bda5599 100644 --- a/memex/main.go +++ b/memex/main.go @@ -287,8 +287,7 @@ func commitCmd(args commitArgs) { if err != nil { faile(err) } - msg := fmt.Sprintf("%d %s", args.now.Unix(), args.msg) - if err := SdarCommit(config.Arch, msg); err != nil { + if err := SdarCommit(config.Arch); err != nil { faile(err) } diff --git a/memex/sdar.go b/memex/sdar.go index 730a88e..42a7a24 100644 --- a/memex/sdar.go +++ b/memex/sdar.go @@ -165,10 +165,10 @@ func startWriteOp() { writeOps++ } -func SdarCommit(archive_path, msg string) error { +func SdarCommit(archive_path string) error { startWriteOp() defer func() { writeOps-- }() - c := exec.Command(SdarPath, "commit", "-a", archive_path, "-m", msg) + c := exec.Command(SdarPath, "commit", "-a", archive_path) c.Stdin = nil c.Stdout = os.Stdout c.Stderr = os.Stderr @@ -20,8 +20,9 @@ enum { Entrysz = Hmacsz + 8, Lvlcap = Maxblk / Entrysz, Magicsz = 8, - Segmetasz = 8 + 8 + 8 + 128, - Seghdsz = Magicsz + Keysz + Cipovh + Segmetasz, + Segmetasz1 = 8 + 8 + 8 + 128, + Segmetasz2 = 8 + 8, + Seghdsz = Magicsz + Keysz + Cipovh, Segitemsz = Hmacsz + 4, Permf = 0666, @@ -45,6 +46,7 @@ typedef struct Level Level; typedef struct Loghd Loghd; typedef struct Reader Reader; typedef struct Segfile Segfile; +typedef struct Segctx Segctx; typedef struct Segmeta Segmeta; typedef struct Slice Slice; typedef struct Writer Writer; @@ -52,7 +54,7 @@ typedef struct Writer Writer; typedef uchar hmac_t[Hmacsz]; typedef int flushcb(void *, hmac_t, Slice); -typedef int blockcb(Block *, hmac_t, uchar[Prepsz], void *); +typedef int blockcb(Block *, hmac_t, Segctx *, void *); struct Slice { @@ -94,6 +96,11 @@ struct Level { Entry ent[Lvlcap]; }; +struct Segctx { + uchar prep[Prepsz]; + int metasz; +}; + struct Reader { Arch *arch; vlong pos; @@ -102,9 +109,9 @@ struct Reader { Level level[2]; /* open segment */ + Segctx segc; FILE *segf; int segno; - uchar segp[Prepsz]; }; struct Writer { @@ -121,8 +128,6 @@ struct Writer { struct Segmeta { vlong nitem; vlong dlen; - vlong date; - char msg[128]; }; struct Segfile { @@ -214,7 +219,7 @@ void cdumphashes(Cache *, int); void cfree(Cache *); /* arch.c */ -extern uchar segmagic[Magicsz]; +extern uchar segmagic2[Magicsz]; int archinit(Arch *, char *); void archsync(Arch *); int archhas(Arch *, hmac_t); @@ -231,7 +236,7 @@ int stashadd(Arch *, uchar *, vlong); int stashblk(Arch *, hmac_t, int, Slice); int stashdone(Arch *, Addr *); int stashsync(Arch *); -int stashcommit(Arch *, char *, uchar[Segidsz]); +int stashcommit(Arch *, uchar[Segidsz]); /* write.c */ void writerinit(Writer *, flushcb *, void *); diff --git a/sdar/arch.c b/sdar/arch.c index 67a1a26..2494603 100644 --- a/sdar/arch.c +++ b/sdar/arch.c @@ -19,11 +19,14 @@ struct Cacheblk { int segno; }; -uchar segmagic[Magicsz] = { +uchar segmagic1[Magicsz] = { 0x4c, 0x00, 0x7b, 0xf8, 0x62, 0xaa, 0x9a, 0x4e }; -uchar inflate[Maxblk]; +uchar segmagic2[Magicsz] = { + 0xb3, 0x8f, 0x9e, 0x05, + 0x00, 0x22, 0x57, 0x24 +}; static int walkseg(Arch *a, walkcb fn, void *arg) @@ -65,37 +68,45 @@ walkseg(Arch *a, walkcb fn, void *arg) } static int -prepseg(FILE *f, uchar prep[Prepsz]) +prepseg(FILE *f, Segctx *ctx) { uchar seghd[Magicsz + Keysz]; - if (fread(seghd, sizeof seghd, 1, f) != 1 - || memcmp(segmagic, seghd, Magicsz) != 0) { - logs("E invalid semment header"); - return 1; + if (fread(seghd, sizeof seghd, 1, f) != 1) + goto error; + crypto_box_beforenm(ctx->prep, seghd + Magicsz, key.sec); + if (memcmp(segmagic1, seghd, Magicsz) == 0) { + ctx->metasz = Segmetasz1; + return 0; } - crypto_box_beforenm(prep, seghd + Magicsz, key.sec); - return 0; + if (memcmp(segmagic2, seghd, Magicsz) == 0) { + ctx->metasz = Segmetasz2; + return 0; + } + +error: + logs("E invalid semment header"); + return 1; } int walkblocks(FILE *f, blockcb fn, void *arg) { - uchar segp[Prepsz]; + Segctx c; + Segmeta m; Slice s; + Block b; uchar *p, *pp; vlong n, len, idx; - Segmeta m; - Block b; int err; - if (prepseg(f, segp)) + if (prepseg(f, &c)) return 1; err = 1; s = newsl(); - if (decrypt(&s, f, Segmetasz, segp, -1)) { + if (decrypt(&s, f, c.metasz, c.prep, -1)) { logs("E cannot read metadata"); goto out; } @@ -111,7 +122,7 @@ walkblocks(FILE *f, blockcb fn, void *arg) n = (Maxblk/Segitemsz) * Segitemsz; n = min(len, n); len -= n; - if (decrypt(&s, f, n, segp, --idx)) { + if (decrypt(&s, f, n, c.prep, --idx)) { logs("E cannot read items"); goto out; } @@ -119,7 +130,7 @@ walkblocks(FILE *f, blockcb fn, void *arg) pp = p + n; while (p < pp) { decitem(p, &b); - if (fn(&b, p, segp, arg)) + if (fn(&b, p, &c, arg)) goto out; b.off += b.len + Cipovh; p += Segitemsz; @@ -133,11 +144,11 @@ out: } static int -cacheblk(Block *b, hmac_t hmac, uchar segp[Prepsz], void *arg) +cacheblk(Block *b, hmac_t hmac, Segctx *ctx, void *arg) { Cacheblk *cb; - (void)segp; + (void)ctx; cb = arg; b->seg = cb->segno; if (cput(cb->cache, hmac, b)) { @@ -307,7 +318,7 @@ openseg(Reader *r, uchar *segid, int segno) f = fopenat(r->arch->fd, O_RDONLY, segs(segid)); if (f == 0) return 1; - if (prepseg(f, r->segp)) { + if (prepseg(f, &r->segc)) { fclose(f); return 1; } @@ -325,6 +336,7 @@ readblock(Reader *r, hmac_t hmac, Block *b, Slice *ps) Slice s; uchar *segid, *segp; FILE *segf; + int seghdsz; vlong off; int len, err; @@ -342,9 +354,10 @@ readblock(Reader *r, hmac_t hmac, Block *b, Slice *ps) goto error; } segf = r->segf; - segp = r->segp; + segp = r->segc.prep; + seghdsz = Seghdsz + r->segc.metasz; - if (fseek(segf, Seghdsz + off, SEEK_SET) == -1) + if (fseek(segf, seghdsz + off, SEEK_SET) == -1) goto error; if (b->lz4) { s = newsl(); diff --git a/sdar/main.c b/sdar/main.c index 3086f4a..c5123c2 100644 --- a/sdar/main.c +++ b/sdar/main.c @@ -405,15 +405,10 @@ error: static int cmd_commit(int argc, char *argv[]) { - char *adir, *msg; + char *adir; uchar segid[Segidsz]; - msg = ""; - ARGBEGIN_("usage: commit [-h] [-a PATH] [-m MESSAGE]") { - case 'm': - if (!(msg = ARGF)) - goto usage; - break; + ARGBEGIN_("usage: commit [-h] [-a PATH]") { } ARGEND_ initkey(0); @@ -425,7 +420,7 @@ cmd_commit(int argc, char *argv[]) err("could not open stash"); goto error; } - if (stashcommit(&a, msg, segid)) { + if (stashcommit(&a, segid)) { err("commit failed"); goto error; } @@ -696,16 +691,18 @@ cmd_update(int argc, char *argv[]) } static int -emitblock(Block *b, hmac_t hmac, uchar segp[Prepsz], void *arg) +emitblock(Block *b, hmac_t hmac, Segctx *ctx, void *arg) { Slice s; FILE *segf; + int seghdsz; segf = arg; - if (fseek(segf, Seghdsz + b->off, SEEK_SET) == 1) + seghdsz = Seghdsz + ctx->metasz; + if (fseek(segf, seghdsz + b->off, SEEK_SET) == -1) return 1; s = newsl(); - if (decrypt(&s, segf, b->len, segp, b->off)) { + if (decrypt(&s, segf, b->len, ctx->prep, b->off)) { logs("E could not read block"); err("could not read block"); freesl(s); diff --git a/sdar/stash.c b/sdar/stash.c index cd0f829..6216566 100644 --- a/sdar/stash.c +++ b/sdar/stash.c @@ -4,7 +4,6 @@ #include <fcntl.h> #include <sys/mman.h> #include <sys/stat.h> -#include <sys/time.h> #include <unistd.h> #include <crypto_box.h> @@ -184,9 +183,8 @@ flushblk(void *arg, hmac_t hmac, Slice sb) } static int -writeseg(Arch *a, char *msg, FILE *f) +writeseg(Arch *a, FILE *f) { - struct timeval tv; Slice sb, sc; Segmeta m; vlong len, idx; @@ -198,17 +196,13 @@ writeseg(Arch *a, char *msg, FILE *f) sb = newsl(); sc = newsl(); - if (fwrite(segmagic, Magicsz, 1, f) != 1) + if (fwrite(segmagic2, Magicsz, 1, f) != 1) goto out; if (fwrite(h->segk, Keysz, 1, f) != 1) goto out; - memset(&m, 0, sizeof(Segmeta)); m.nitem = h->nitem; m.dlen = h->dlen; - if (gettimeofday(&tv, 0) == 0) - m.date = tv.tv_sec; - strncpy(m.msg, msg, sizeof m.msg - 1); encmeta(&sb, &m); slencrypt(&sc, sb, h->segp, -1); @@ -283,7 +277,7 @@ cacheupdate(Arch *a, uchar segid[Segidsz]) } int -stashcommit(Arch *a, char *msg, uchar segid[Segidsz]) +stashcommit(Arch *a, uchar segid[Segidsz]) { FILE *f; @@ -295,7 +289,7 @@ stashcommit(Arch *a, char *msg, uchar segid[Segidsz]) logs("W cannot create %s", segs(segid)); return 1; } - if (writeseg(a, msg, f)) + if (writeseg(a, f)) goto error; if (cacheupdate(a, segid)) goto error; diff --git a/sdar/util.c b/sdar/util.c index 5fc96da..7d9eab1 100644 --- a/sdar/util.c +++ b/sdar/util.c @@ -190,9 +190,7 @@ encmeta(Slice *s, Segmeta *meta) { enc64be(s->buf, meta->nitem); enc64be(s->buf + 8, meta->dlen); - enc64be(s->buf + 16, meta->date); - memcpy(s->buf + 24, meta->msg, sizeof meta->msg); - s->len = Segmetasz; + s->len = Segmetasz2; } void @@ -200,8 +198,6 @@ decmeta(Slice s, Segmeta *meta) { meta->nitem = dec64be(s.buf); meta->dlen = dec64be(s.buf + 8); - meta->date = dec64be(s.buf + 16); - memcpy(meta->msg, s.buf + 24, sizeof meta->msg); } void |
