diff options
| author | Quentin Carbonneaux | 2021-11-19 10:58:05 +0100 |
|---|---|---|
| committer | Quentin Carbonneaux | 2021-11-20 17:39:07 +0100 |
| commit | 72ed312051e68deacfa0cdf41692a3454bf74408 (patch) | |
| tree | 4d9b40e11e5027117eabca22ab6daa631c0ec28b | |
| parent | fe1aa0e32f9e6134dd0497e94aed79280a5850dc (diff) | |
style fixes
| -rw-r--r-- | sdar/all.h | 13 | ||||
| -rw-r--r-- | sdar/arch.c (renamed from sdar/archive.c) | 35 | ||||
| -rw-r--r-- | sdar/crypt.c | 4 | ||||
| -rw-r--r-- | sdar/key.c | 4 | ||||
| -rw-r--r-- | sdar/main.c | 87 | ||||
| -rw-r--r-- | sdar/mmh.c | 8 | ||||
| -rw-r--r-- | sdar/sha256.c | 12 | ||||
| -rw-r--r-- | sdar/stash.c | 66 | ||||
| -rw-r--r-- | sdar/util.c | 6 | ||||
| -rw-r--r-- | sdar/writer.c | 5 |
10 files changed, 129 insertions, 111 deletions
@@ -45,7 +45,7 @@ typedef struct Ilevel Ilevel; typedef struct Key Key; typedef struct Loghd Loghd; typedef struct Reader Reader; -typedef struct Segmap Segmap; +typedef struct Segfile Segfile; typedef struct Segmeta Segmeta; typedef struct Writer Writer; @@ -119,9 +119,9 @@ struct Segmeta { char msg[128]; }; -struct Segmap { - char in; +struct Segfile { uchar id[Segidsz]; + char here; }; struct Loghd { @@ -134,7 +134,7 @@ struct Loghd { struct Arch { int fd; Cache cache; - Segmap *seg; + Segfile *seg; int nseg; /* stash */ @@ -151,6 +151,7 @@ struct Addr { #define MAKESURE(what, x) _Static_assert(x, #what) #define min(a, b) ((a) > (b) ? (b) : (a)) +#define sizeof (vlong)sizeof extern uchar iobuf[Maxblk]; extern uchar inflate[Maxblk]; @@ -197,7 +198,7 @@ int cgetseg(Cache *, uchar[Segidsz], int *); void cdumphashes(Cache *, int); void cfree(Cache *); -/* archive.c */ +/* arch.c */ extern uchar segmagic[Magicsz]; int archinit(Arch *, char *); void archsync(Arch *); @@ -220,7 +221,7 @@ int stashsync(Arch *); int stashcommit(Arch *, char *, uchar[Segidsz]); /* writer.c */ -int writerinit(Writer *, flushcb *, void *); +void writerinit(Writer *, flushcb *, void *); int writer(Writer *, uchar *, vlong); int writerdone(Writer *, Addr *); int getlevel(Ilevel *, uchar *, long); diff --git a/sdar/archive.c b/sdar/arch.c index 5c88975..8b8ae71 100644 --- a/sdar/archive.c +++ b/sdar/arch.c @@ -63,7 +63,7 @@ encmeta(uchar *buf, Segmeta *meta) enc64be(buf, meta->nitem); enc64be(buf + 8, meta->dlen); enc64be(buf + 16, meta->date); - memcpy(buf + 24, meta->msg, sizeof(meta->msg)); + memcpy(buf + 24, meta->msg, sizeof meta->msg); } static void @@ -72,7 +72,7 @@ decmeta(uchar *buf, Segmeta *meta) meta->nitem = dec64be(buf); meta->dlen = dec64be(buf + 8); meta->date = dec64be(buf + 16); - memcpy(meta->msg, buf + 24, sizeof(meta->msg)); + memcpy(meta->msg, buf + 24, sizeof meta->msg); } void @@ -86,7 +86,7 @@ decitem(uchar *buf, Block *b) int walkblocks(FILE *f, blockcb *fn, void *arg) { - static uchar scratch[sizeof(plain)]; + static uchar scratch[sizeof plain]; uchar buf[Magicsz + Keysz]; uchar *it, *end, *segkey; vlong ni, sz, idx, nleft; @@ -94,7 +94,7 @@ walkblocks(FILE *f, blockcb *fn, void *arg) char *e; Block b = {.off = 0}; - if (fread(buf, sizeof(buf), 1, f) != 1 + if (fread(buf, sizeof buf, 1, f) != 1 || memcmp(segmagic, buf, Magicsz) != 0) { logs("E invalid header"); return 1; @@ -173,7 +173,7 @@ indexseg(Arch *a, uchar segid[Segidsz]) static int addseg(Arch *a, uchar segid[Segidsz], void *arg) { - Segmap *p, *m; + Segfile *p, *s; int n; if (cgetseg(&a->cache, segid, &n)) { @@ -183,21 +183,21 @@ addseg(Arch *a, uchar segid[Segidsz], void *arg) if (indexseg(a, segid)) return 1; n = a->nseg; - p = realloc(a->seg, a->nseg*sizeof(*p)); + p = realloc(a->seg, n*sizeof *p); if (!p) { a->nseg--; return 1; } a->seg = p; - a->seg[n-1] = (Segmap){0}; + a->seg[n-1] = (Segfile){0}; } - m = &a->seg[n-1]; - if (n > a->nseg || m->in) { + s = &a->seg[n-1]; + if (n > a->nseg || s->here) { logs("W broken cache invariant"); return 1; } - m->in = 1; - memcpy(m->id, segid, Segidsz); + s->here = 1; + memcpy(s->id, segid, Segidsz); ++*(int*)arg; return 0; } @@ -217,7 +217,7 @@ readcache(Arch *a) } a->nseg = a->cache.meta->nseg; - a->seg = calloc(a->nseg, sizeof(Segmap)); + a->seg = calloc(a->nseg, sizeof a->seg[0]); nold = a->nseg; nin = 0; switch (walkseg(a, addseg, &nin)) { @@ -242,7 +242,8 @@ archinit(Arch *a, char *path) { int fd; - if ((fd = open(path, O_RDONLY)) == -1) + fd = open(path, O_RDONLY); + if (fd == -1) return 1; if (faccessat(fd, "seg", F_OK, 0) != 0) { logs("E %s/seg does not exist", path); @@ -260,7 +261,7 @@ archinit(Arch *a, char *path) error: close(fd); - memset(a, 0, sizeof(*a)); + memset(a, 0, sizeof *a); return 1; } @@ -274,14 +275,14 @@ archsync(Arch *a) static uchar * getsegid(Arch *a, int n) { - Segmap *p; + Segfile *p; if (n > a->nseg) /* may happen when a concurrent sdar process * has added entries to the cache */ return 0; p = &a->seg[n-1]; - return p->in ? p->id : 0; + return p->here ? p->id : 0; } int @@ -321,7 +322,7 @@ readblock(Reader *r, hmac_t hmac, Block *b, uchar **data, long *blen) r->segno = b->seg; if (!f) goto error; - if (fread(buf, sizeof(buf), 1, f) != 1) + if (fread(buf, sizeof buf, 1, f) != 1) goto error; assert(key.mastersec); crypto_box_beforenm(r->segpre, buf + Magicsz, key.mastersec); diff --git a/sdar/crypt.c b/sdar/crypt.c index 58b9856..a7f8465 100644 --- a/sdar/crypt.c +++ b/sdar/crypt.c @@ -38,7 +38,7 @@ creadp(FILE *f, uchar *prep, vlong nonceidx, long sz) uchar nonce[Noncesz]; long cryptsz; - if (Plnpad + sz > (vlong)sizeof(cipher)) + if (Plnpad + sz > sizeof cipher) return "too big"; cryptsz = (Plnpad - Cippad) + sz; @@ -62,7 +62,7 @@ cwritep(FILE *f, uchar *prep, vlong nonceidx, uchar *buf, long sz) assert(key.masterpub); assert(sz >= Plnpad); - if (sz > (vlong)sizeof(cipher)) + if (sz > sizeof cipher) return "too big"; memset(buf, 0, Plnpad); @@ -90,7 +90,7 @@ keywrite(char *p, long np, FILE *f) memcpy(buf + Omasterpub, key.masterpub, Keysz); ret = crypto_scrypt((uint8_t*)p, np, buf + Osalt, Saltsz, - ScryptN, Scryptr, Scryptp, nk, sizeof(nk)); + ScryptN, Scryptr, Scryptp, nk, sizeof nk); if (ret != 0) return 1; @@ -139,7 +139,7 @@ keyreadsec(char *p, long np, FILE *f) memcpy(masterpub, buf + Omasterpub, Keysz); ret = crypto_scrypt((uint8_t*)p, np, buf + Osalt, Saltsz, - ScryptN, Scryptr, Scryptp, nk, sizeof(nk)); + ScryptN, Scryptr, Scryptp, nk, sizeof nk); if (ret != 0) return "out of memory"; diff --git a/sdar/main.c b/sdar/main.c index eced9a7..7d5226c 100644 --- a/sdar/main.c +++ b/sdar/main.c @@ -42,22 +42,18 @@ keypath() { static char buf[512]; char *p; - int n; if (flagk) return flagk; + p = getenv("SDAR_KEY"); if (p) return p; + p = getenv("HOME"); - if (p) { - n = snprintf(buf, sizeof(buf), "%s/.sdar.key", p); - if (n >= (int)sizeof(buf)) { - err("key path too long"); - exit(1); - } - } else - strcpy(buf, ".sdar.key"); + if (!p) + p = "."; + snprintf(buf, sizeof buf, "%s/.sdar.key", p); return buf; } @@ -95,7 +91,7 @@ readpass(char *prefix, int *interact) --*interact; write(fd, prefix, strlen(prefix)); - write(fd, "passphrase:", sizeof("passphrase:")-1); + write(fd, "passphrase:", 11); sigemptyset(&sa.sa_mask); sa.sa_flags = 0; @@ -104,10 +100,10 @@ readpass(char *prefix, int *interact) sigaction(SIGTERM, &sa, 0); tcgetattr(fd, &old); - memcpy(&new, &old, sizeof(new)); + memcpy(&new, &old, sizeof new); new.c_lflag &= ~ECHO; tcsetattr(fd, TCSANOW, &new); - for (i = 0; i < (int)sizeof(buf)-1; i++) { + for (i = 0; i < sizeof buf - 1; i++) { n = read(fd, buf+i, 1); if (n <= 0 || buf[i] == '\n') break; @@ -139,25 +135,29 @@ initkey(int sec) err("could not open key file %s", path); exit(1); } + if (!sec) { - if (!(e = keyreadpub(f))) { + e = keyreadpub(f); + if (!e) { fclose(f); return; } err("could not open key (%s)", e); exit(1); } + interact = 3; do { pass = readpass("", &interact); fseek(f, 0, SEEK_SET); e = keyreadsec(pass, strlen(pass), f); - if (e == 0) { + if (!e) { fclose(f); return; } err("could not open key (%s)", e); } while (interact); + exit(1); } @@ -235,7 +235,7 @@ readnum(vlong *pn, FILE *in) p = buf; for (;;) { - if (p == &buf[sizeof(buf)]) + if (p == &buf[sizeof buf]) return 1; c = getc(in); if (c == '\n' || c == ' ') { @@ -259,9 +259,9 @@ pkt(char *msg, ...) int n; va_start(vl, msg); - n = vsnprintf(buf, sizeof(buf), msg, vl); + n = vsnprintf(buf, sizeof buf, msg, vl); va_end(vl); - assert(n < (int)sizeof(buf)); + assert(n < sizeof buf); printf("%02x%s\n", n+1, buf); } @@ -290,36 +290,38 @@ addbatch(Arch *a) if (memcmp(buf+2, "blk ", 4) == 0) { lz4 = getchar() - '0'; - if (lz4 != 0 && lz4 != 1) + if (lz4 & -2) goto error; - (void)getchar(); /* skip ' ' */ - if (fread(buf, 64, 1, stdin) != 1) - goto error; - if (shex(buf, Hmacsz, hmac)) - goto error; - (void)getchar(); /* skip ' ' */ - if (readnum(&sz, stdin) || sz > Maxblk) + getchar(); + + if (fread(buf, 64, 1, stdin) != 1 + || shex(buf, Hmacsz, hmac)) goto error; - if (sz != 0 && fread(blk, sz, 1, stdin) != 1) + getchar(); + + if (readnum(&sz, stdin) + || sz > Maxblk + || (sz > 0 && fread(blk, sz, 1, stdin) != 1)) goto error; + if (stashblk(a, hmac, lz4, blk, sz)) { pkt("err"); rc = 1; } else pkt("ok"); + continue; } if (memcmp(buf+2, "raw ", 4) != 0) goto error; - /* raw data */ if (readnum(&sz, stdin)) goto error; werr = 0; while (sz > 0) { - rd = min(sz, (vlong)sizeof(iobuf)); + rd = min(sz, sizeof iobuf); rd = fread(iobuf, 1, rd, stdin); sz -= rd; if (rd == 0) @@ -327,6 +329,7 @@ addbatch(Arch *a) if (!werr && stashadd(a, iobuf, rd)) werr = 1; } + if (werr || stashdone(a, &addr)) { pkt("err"); rc = 1; @@ -376,7 +379,7 @@ cmd_add(int argc, char *argv[]) } tot = 0; - while ((sz = fread(iobuf, 1, sizeof(iobuf), stdin)) > 0) { + while ((sz = fread(iobuf, 1, sizeof iobuf, stdin)) > 0) { if (stashadd(&a, iobuf, sz)) goto writefail; tot += sz; @@ -473,7 +476,7 @@ idbatch(Writer *w) if (readnum(&sz, stdin)) goto error; while (sz > 0) { - rd = min(sz, (vlong)sizeof(iobuf)); + rd = min(sz, sizeof iobuf); rd = fread(iobuf, 1, rd, stdin); sz -= rd; if (rd == 0) @@ -507,13 +510,12 @@ cmd_id(int argc, char *argv[]) } ARGEND initkey(0); - if (writerinit(&w, 0, 0)) - return 1; + writerinit(&w, 0, 0); if (flagb) return idbatch(&w); - while ((sz = (long)fread(iobuf, 1, sizeof(iobuf), stdin)) > 0) + while ((sz = fread(iobuf, 1, sizeof iobuf, stdin)) > 0) if (writer(&w, iobuf, sz)) return 1; if (writerdone(&w, &a)) @@ -532,8 +534,8 @@ parseaddr(char *s, Addr *a) return shex(s + 1, Hmacsz, a->hmac); } -/* len < 0 will read everything - * len == 0 will read a natural chunk of data */ +/* len < 0 - reads everything + * == 0 - reads a natural chunk */ static int readdata(Reader *rd, Addr *addr, vlong off, vlong len, int batch) { @@ -566,7 +568,6 @@ readbatch(Reader *rd) vlong off, len; Addr addr; uchar n; - int nok; pkt("ready"); for (;;) { @@ -575,10 +576,10 @@ readbatch(Reader *rd) cur = buf; if (fread(buf, 2, 1, stdin) != 1) return 0; - nok = shex(buf, 1, &n); - nok = nok || n == 0 || n > (int)sizeof(buf); - nok = nok || fread(buf, n, 1, stdin) != 1; - if (nok) + if (shex(buf, 1, &n) + || n == 0 + || n > sizeof buf + || fread(buf, n, 1, stdin) != 1) goto error; buf[n-1] = 0; @@ -787,8 +788,8 @@ openlog() err("could find $HOME/.sdar.log"); exit(1); } - n = snprintf(buf, sizeof(buf), "%s/.sdar.log", p); - if (n >= (int)sizeof(buf)) { + n = snprintf(buf, sizeof buf, "%s/.sdar.log", p); + if (n >= sizeof buf) { err("log path too long"); exit(1); } @@ -901,7 +902,7 @@ main(int argc, char *argv[]) match = 0; cmd = *argv; p = ambig; - left = sizeof(ambig); + left = sizeof ambig; for (c = cmds; c->name; c++) if (fuzzymatch(cmd, c->name)) { sub = c, match++; @@ -54,7 +54,7 @@ hash(void *key) uint64_t h; assert(Keysz >= 8); - memcpy(&h, key, sizeof(h)); + memcpy(&h, key, sizeof h); return h; } #else @@ -137,7 +137,7 @@ err1: munmap(h->hdr, mapsz(Genmx, h->pagesz)); errno = err; err0: - memset(h, 0, sizeof(*h)); + memset(h, 0, sizeof *h); return -1; } @@ -166,7 +166,7 @@ mmhunmap(Mmh *h) if (munmap(h->hdr, mapsz(Genmx, h->pagesz)) == -1) err = errno; close(h->mapfd); - memset(h, 0, sizeof(*h)); + memset(h, 0, sizeof *h); if (err) { errno = err; return -1; @@ -392,7 +392,7 @@ Retry: /* the data needs to move to the * upper half of the table, or it * is in a wrapped chain */ - memcpy(&tmp, e, sizeof(tmp)); + memcpy(&tmp, e, sizeof tmp); del(h, pos); put(h, keyh, tmp.key, tmp.val, 0); } diff --git a/sdar/sha256.c b/sdar/sha256.c index e06d3a0..d0bd43b 100644 --- a/sdar/sha256.c +++ b/sdar/sha256.c @@ -157,7 +157,7 @@ sha256_update(Shactx *ctx, uint8_t *buf, size_t len) void sha256_init(Shactx *ctx) { - memcpy(ctx->val, sha256_IV, sizeof(ctx->val)); + memcpy(ctx->val, sha256_IV, sizeof ctx->val); ctx->count = 0; } @@ -171,7 +171,7 @@ sha256_finish(Shactx *ctx, uint8_t *dst) off = (size_t)(ctx->count & 63); memcpy(buf, ctx->buf, off); - memcpy(val, ctx->val, sizeof(val)); + memcpy(val, ctx->val, sizeof val); buf[off++] = 0x80; if (off > 56) { memset(&buf[off], 0, 64 - off); @@ -232,7 +232,7 @@ hmac_sha256_finish(Hmacctx *ctx, uint8_t *dst) buf[i] = ctx->key[i] ^ 0x5c; sha256_finish(&ctx->ctx, &buf[64]); sha256_init(&hctx); - sha256_update(&hctx, buf, sizeof(buf)); + sha256_update(&hctx, buf, sizeof buf); sha256_finish(&hctx, dst); } @@ -254,7 +254,7 @@ pbkdf2_sha256(uint8_t *p, size_t np, uint8_t *s, size_t ns, uint64_t c, assert(dkLen <= 32 * (size_t)UINT32_MAX); hmac_sha256_init(&pctx, p, np); - memcpy(&psctx, &pctx, sizeof(psctx)); + memcpy(&psctx, &pctx, sizeof psctx); hmac_sha256_update(&psctx, s, ns); for (i = 0; i * 32 < dkLen; i++) { @@ -262,7 +262,7 @@ pbkdf2_sha256(uint8_t *p, size_t np, uint8_t *s, size_t ns, uint64_t c, enc32be(ibuf, (uint32_t)(i + 1)); /* U_1 = PRF(P, S || INT(i)) */ - memcpy(&hctx, &psctx, sizeof(hctx)); + memcpy(&hctx, &psctx, sizeof hctx); hmac_sha256_update(&hctx, ibuf, 4); hmac_sha256_finish(&hctx, U); @@ -271,7 +271,7 @@ pbkdf2_sha256(uint8_t *p, size_t np, uint8_t *s, size_t ns, uint64_t c, for (j = 2; j <= c; j++) { /* U_j */ - memcpy(&hctx, &pctx, sizeof(hctx)); + memcpy(&hctx, &pctx, sizeof hctx); hmac_sha256_update(&hctx, U, 32); hmac_sha256_finish(&hctx, U); diff --git a/sdar/stash.c b/sdar/stash.c index 04eb7f9..bdd6354 100644 --- a/sdar/stash.c +++ b/sdar/stash.c @@ -19,39 +19,51 @@ stashinit(Arch *a) { uchar seckey[Keysz]; struct stat sb; - vlong sz, page; - int fd, flags; + vlong sz, pg; + int log, flags; Loghd *h; - if (writerinit(&a->writer, flushblock, a)) + assert(!a->log); + writerinit(&a->writer, flushblock, a); + + flags = O_CREAT|O_RDWR; + log = openat(a->fd, "stash/log", flags, Permf); + if (log == -1) return 1; - a->log = 0; - fd = openat(a->fd, "stash/log", O_CREAT|O_RDWR, Permf); - if (fd == -1) - goto error; - page = sysconf(_SC_PAGESIZE); - h = mmap(0, page, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + + flags = PROT_READ|PROT_WRITE; + pg = sysconf(_SC_PAGESIZE); + h = mmap(0, pg, flags, MAP_SHARED, log, 0); if (h == MAP_FAILED) goto error; - if (fstat(fd, &sb) == -1) + + if (fstat(log, &sb) == -1) goto error1; sz = sb.st_size; - if (sz < (vlong)sizeof(Loghd)) { - if (ftruncate(fd, sizeof(Loghd)) == -1) + + if (sz < sizeof *h) { + if (ftruncate(log, sizeof *h) == -1) goto error1; crypto_box_keypair(h->kpub, seckey); crypto_box_beforenm(h->kpre, key.masterpub, seckey); h->nitem = 0; h->dlen = 0; - msync(h, sizeof(Loghd), MS_SYNC); + msync(h, sizeof *h, MS_SYNC); } - if ((a->log = fdopen(fd, "w+")) == 0) + + a->log = fdopen(log, "w+"); + if (!a->log) goto error1; + log = -1; + if (fseek(a->log, 0, SEEK_END)) goto error2; + flags = O_CREAT|O_RDWR|O_APPEND; - if ((a->data = fopenat(a->fd, flags, "stash/data")) == 0) + a->data = fopenat(a->fd, flags, "stash/data"); + if (!a->data) goto error2; + a->logh = h; return 0; @@ -61,8 +73,8 @@ error2: error1: munmap(h, sizeof(Loghd)); error: - if (fd != -1) - close(fd); + if (log != -1) + close(log); return 1; } @@ -130,7 +142,6 @@ stashblk(Arch *a, hmac_t hmac, int lz4, uchar *buf, int sz) uchar *dbuf; int dsz; - assert(key.hmac); if (lz4) { src = (char*)buf; dst = (char*)inflate; @@ -144,6 +155,8 @@ stashblk(Arch *a, hmac_t hmac, int lz4, uchar *buf, int sz) } if (dsz > Maxblk) return 1; + + assert(key.hmac); hmac_blake3(key.hmac, Keysz, dbuf, dsz, check); if (memcmp(hmac, check, Hmacsz) != 0) { logs("E invalid block hmac %s", hmacs(hmac)); @@ -152,6 +165,7 @@ stashblk(Arch *a, hmac_t hmac, int lz4, uchar *buf, int sz) if (archhas(a, hmac)) return 0; + return flushraw(a, hmac, lz4, buf, sz); } @@ -170,7 +184,7 @@ flushblock(void *arg, hmac_t hmac, uchar *buf, int sz) /* we may also want to dedup within the stash */ dst = (char*)deflate + Plnpad; - dstsz = sizeof(deflate) - Plnpad; + dstsz = sizeof deflate - Plnpad; compsz = LZ4_compress_default((char*)buf, dst, sz, dstsz); if (compsz && compsz < sz) { buf = deflate + Plnpad; @@ -200,7 +214,7 @@ writeseg(Arch *a, char *msg, FILE *segf) meta.date = 0; if (gettimeofday(&tv, 0) == 0) meta.date = tv.tv_sec; - strncpy(meta.msg, msg, sizeof(meta.msg)-1); + strncpy(meta.msg, msg, sizeof meta.msg - 1); encmeta(plain + Plnpad, &meta); idx = -1; if (cwritep(segf, h->kpre, idx, plain, Plnpad + Segmetasz)) @@ -209,7 +223,7 @@ writeseg(Arch *a, char *msg, FILE *segf) if (fseek(a->data, 0, SEEK_SET)) return 1; total = 0; - while ((sz = fread(iobuf, 1, sizeof(iobuf), a->data))) { + while ((sz = fread(iobuf, 1, sizeof iobuf, a->data))) { if ((vlong)fwrite(iobuf, 1, sz, segf) != sz) return 1; total += sz; @@ -275,14 +289,15 @@ stashcommit(Arch *a, char *msg, uchar segid[Segidsz]) memcpy(segid, a->logh->kpub, Segidsz); flags = O_CREAT|O_EXCL|O_WRONLY; - if (!(segf = fopenat(a->fd, flags, segs(segid)))) { + segf = fopenat(a->fd, flags, segs(segid)); + if (!segf) { logs("W %s already exists", segs(segid)); return 1; } - if (writeseg(a, msg, segf)) - goto error; - if (fflush(segf) == EOF || fsync(fileno(segf))) + if (writeseg(a, msg, segf) + || fflush(segf) == EOF + || fsync(fileno(segf)) == -1) goto error; /* update the cache */ @@ -295,6 +310,7 @@ stashcommit(Arch *a, char *msg, uchar segid[Segidsz]) a->log = 0; a->logh = 0; a->data = 0; + if (unlinkat(a->fd, "stash/log", 0) || unlinkat(a->fd, "stash/data", 0)) logs("W could not unlink stash files"); diff --git a/sdar/util.c b/sdar/util.c index 1d4f153..84fa830 100644 --- a/sdar/util.c +++ b/sdar/util.c @@ -23,7 +23,7 @@ logs(char *fmt, ...) assert(flog); gettimeofday(&tv, 0); tm = localtime(&tv.tv_sec); - strftime(date, sizeof(date), "%F %H:%M:%S", tm); + strftime(date, sizeof date, "%F %H:%M:%S", tm); va_start(ap, fmt); vsprintf(msg, fmt, ap); va_end(ap); @@ -93,9 +93,9 @@ fopenat(int afd, int flags, char *path, ...) int n, fd; va_start(ap, path); - n = vsnprintf(buf, sizeof(buf), path, ap); + n = vsnprintf(buf, sizeof buf, path, ap); va_end(ap); - if (n >= (int)sizeof(buf)) + if (n >= sizeof buf) return 0; fd = openat(afd, buf, flags, Permf); if (fd == -1) diff --git a/sdar/writer.c b/sdar/writer.c index cd39ea8..2f45858 100644 --- a/sdar/writer.c +++ b/sdar/writer.c @@ -5,7 +5,7 @@ MAKESURE(vlong_is_enough_for_vlen, (uvlong)Indexsz * Maxblk <= LLONG_MAX); MAKESURE(Avglog_ok, 6 < Avglog && Avglog < 32); -int +void writerinit(Writer *w, flushcb *flush, void *arg) { w->flush = flush; @@ -17,7 +17,6 @@ writerinit(Writer *w, flushcb *flush, void *arg) w->vlen0 = 0; w->level[0].nent = 0; w->level[1].nent = 0; - return 0; } /* callers must arrange for Plnpad bytes to be available @@ -189,7 +188,7 @@ writer(Writer *w, uchar *buf, vlong sz) int n; while (sz > 0) { - n = sizeof(w->buf) - (Plnpad + w->bpos); + n = sizeof w->buf - (Plnpad + w->bpos); if (n > sz) n = sz; memcpy(w->buf + Plnpad + w->bpos, buf, n); |
