X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;ds=sidebyside;f=cdb%2Fwriteable.c;h=a73be821a001a894b0114920a06e4b045f1ab985;hb=f16e3e0cf0a2b965643df01e24ef59e1393da78d;hp=3c718d83fd813f63e26645839cfba0f02e0bf1a5;hpb=2aaa6d9084b8a950151d64b4fe7a483be59b5722;p=chiark-tcl.git diff --git a/cdb/writeable.c b/cdb/writeable.c index 3c718d8..a73be82 100644 --- a/cdb/writeable.c +++ b/cdb/writeable.c @@ -15,10 +15,6 @@ static void maybe_close(int fd) { if (fd>=0) close(fd); } -#define PE(m) do{ \ - rc= cht_posixerr(ip, errno, "failed to " m); goto x_rc; \ - }while(0) - /*==================== Subsystems and subtypes ====================*/ /*---------- Pathbuf ----------*/ @@ -27,14 +23,13 @@ typedef struct Pathbuf { char *buf, *sfx; } Pathbuf; -#define MAX_SUFFIX 4 +#define MAX_SUFFIX 5 static void pathbuf_init(Pathbuf *pb, const char *pathb) { int l= strlen(pathb); - pb->buf= TALLOC(l + 4); + pb->buf= TALLOC(l + MAX_SUFFIX + 1); memcpy(pb->buf, pathb, l); pb->sfx= pb->buf + l; - *pb->sfx++= '.'; } static const char *pathbuf_sfx(Pathbuf *pb, const char *suffix) { assert(strlen(suffix) <= MAX_SUFFIX); @@ -68,11 +63,6 @@ static HashValue *htv_prep(int len) { static Byte *htv_fillptr(HashValue *hd) { return hd->data; } -#if 0 -static void htv_fill(HashValue *hd, const Byte *data) { - memcpy(hd->data, data, hd->len); -} -#endif static void ht_setup(HashTable *ht) { Tcl_InitHashTable(&ht->t, TCL_STRING_KEYS); @@ -97,6 +87,15 @@ static void ht_maybeupdate(HashTable *ht, const char *key, Tcl_SetHashValue(he, val_eat); } +static const HashValue *ht_lookup(HashTable *ht, const char *key) { + Tcl_HashEntry *he; + + he= Tcl_FindHashEntry(&ht->t, key); + if (!he) return 0; + + return Tcl_GetHashValue(he); +} + static int ht_forall(HashTable *ht, int (*fn)(const char *key, HashValue *val, struct ht_forall_ctx *ctx), @@ -166,11 +165,13 @@ static int rw_close(Tcl_Interp *ip, Rw *rw) { } pathbuf_free(&rw->pbsome); pathbuf_free(&rw->pbother); - TFREE(rw); return rc; } -static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *rw) { rw_close(0,rw); } +static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *rw) { + rw_close(0,rw); + TFREE(rw); +} const IdDataSpec cdbtcl_rwdatabases= { "cdb-rwdb", "cdb-openrwdatabases-table", destroy_cdbrw_idtabcb }; @@ -191,7 +192,7 @@ static int acquire_lock(Tcl_Interp *ip, Pathbuf *pb, int *lockfd_r) { /* Remove r where umask would remove w; * eg umask intending 0664 here gives 0660 */ - *lockfd_r= open(pathbuf_sfx(pb,".lock"), O_RDONLY|O_CREAT, lockmode); + *lockfd_r= open(pathbuf_sfx(pb,".lock"), O_RDWR|O_CREAT, lockmode); if (*lockfd_r < 0) return cht_posixerr(ip, errno, "could not open/create lockfile"); @@ -212,7 +213,7 @@ static int acquire_lock(Tcl_Interp *ip, Pathbuf *pb, int *lockfd_r) { return TCL_OK; } -/*---------- Log reading ----------*/ +/*---------- Log reading and writing ----------*/ static int readlognum(FILE *f, int delim, int *num_r) { int c; @@ -255,7 +256,7 @@ static int readstorelogrecord(FILE *f, HashTable *ht, int c, rc, r; c= getc(f); - if (c==EOF) { if (feof(f)) return -1; return -2; } + if (c==EOF) { return feof(f) ? -1 : -2; } if (c=='\n') return -3; if (c!='+') return -2; @@ -276,6 +277,8 @@ static int readstorelogrecord(FILE *f, HashTable *ht, r= fread(htv_fillptr(val), 1,vallen, f); if (r!=vallen) goto x2_free_keyval; + c= getc(f); if (c!='\n') goto x2_free_keyval; + rc= omitfn ? omitfn(val, ctx) : TCL_OK; if (rc) { assert(rc>0); TFREE(val); } else updatefn(ht, key, val); @@ -289,36 +292,64 @@ static int readstorelogrecord(FILE *f, HashTable *ht, return -2; } +static int writerecord(FILE *f, const char *key, const HashValue *val) { + int r; + + r= fprintf(f, "+%d,%d:%s->", strlen(key), val->len, key); + if (r<0) return -1; + + r= fwrite(val->data, 1, val->len, f); + if (r != val->len) return -1; + + r= putc('\n', f); + if (r==EOF) return -1; + + return 0; +} + /*---------- Creating ----------*/ int cht_do_cdbwr_create_empty(ClientData cd, Tcl_Interp *ip, const char *pathb) { - static const char *const toremoves[]= { - ".main", ".cdb", ".log", ".tmp", 0 - }; + static const char *const toremoves[]= { ".cdb", ".log", ".tmp", 0 }; - Pathbuf pb; - int lock_fd=-1, fd=-1, rc, r; + Pathbuf pb, pbmain; + int lock_fd=-1, rc, r; + FILE *f= 0; const char *const *toremove; + struct stat stab; pathbuf_init(&pb, pathb); + pathbuf_init(&pbmain, pathb); + rc= acquire_lock(ip, &pb, &lock_fd); if (rc) goto x_rc; - - fd= open(pathbuf_sfx(&pb, ".main"), O_RDWR|O_CREAT|O_EXCL, 0666); - if (fd <= 0) PE("create new database file"); + + r= lstat(pathbuf_sfx(&pbmain, ".main"), &stab); + if (!r) { rc= cht_staticerr(ip, "database already exists during creation", + "CDB ALREADY-EXISTS"); goto x_rc; } + if (errno != ENOENT) PE("check for existing database .main during creation"); for (toremove=toremoves; *toremove; toremove++) { - r= remove(*toremove); + r= remove(pathbuf_sfx(&pb, *toremove)); if (r && errno != ENOENT) PE("delete possible spurious file during creation"); } + f= fopen(pathbuf_sfx(&pb, ".tmp"), "w"); + if (!f) PE("create new database .tmp"); + r= putc('\n', f); if (r==EOF) PE("write sentinel to new database .tmp"); + r= fclose(f); f=0; if (r) PE("close new database .tmp during creation"); + + r= rename(pb.buf, pbmain.buf); + if (r) PE("install new database .tmp as .main (finalising creation)"); + rc= TCL_OK; x_rc: - maybe_close(fd); + if (f) fclose(f); maybe_close(lock_fd); pathbuf_free(&pb); + pathbuf_free(&pbmain); return rc; } @@ -384,6 +415,7 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, off_t logrecstart, logjunkpos; rw= TALLOC(sizeof(*rw)); + rw->ix= -1; ht_setup(&rw->logincore); cht_scriptinv_init(&rw->on_info); cht_scriptinv_init(&rw->on_lexminval); @@ -392,15 +424,14 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, pathbuf_init(&rw->pbother, pathb); rw->autocompact= 1; - if (on_lexminval) { - rc= cht_scriptinv_set(&rw->on_lexminval, ip, on_lexminval, 0); - if (rc) goto x_rc; - } else { - rw->on_lexminval.llength= 0; - } + rc= cht_scriptinv_set(&rw->on_info, ip, on_info, 0); + if (rc) goto x_rc; + + rc= cht_scriptinv_set(&rw->on_lexminval, ip, on_lexminval, 0); + if (rc) goto x_rc; mainfd= open(pathbuf_sfx(&rw->pbsome,".main"), O_RDONLY); - if (mainfd<0) PE("open exist3ing database file .main"); + if (mainfd<0) PE("open existing database file .main"); rc= acquire_lock(ip, &rw->pbsome, &rw->lock_fd); if (rc) goto x_rc; r= fstat(mainfd, &stab); if (r) PE("fstat .main"); @@ -410,8 +441,8 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, if (rw->cdb_fd >=0) { rc= cdbinit(ip, rw); if (rc) goto x_rc; } else if (errno == ENOENT) { - if (rw->mainsz) { - rc= cht_staticerr(ip, ".cdb does not exist but .main is nonempty -" + if (rw->mainsz > 1) { + rc= cht_staticerr(ip, ".cdb does not exist but .main is >1byte -" " .cdb must have been accidentally deleted!", "CDB CDBMISSING"); goto x_rc; @@ -482,25 +513,32 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, x_rc: rw_close(0,rw); + TFREE(rw); maybe_close(mainfd); return rc; } +int cht_do_cdbwr_open_okjunk(ClientData cd, Tcl_Interp *ip, const char *pathb, + Tcl_Obj *on_info, Tcl_Obj *on_lexminval, + void **result) { + return cht_do_cdbwr_open(cd,ip,pathb,on_info,on_lexminval,result); +} + /*==================== COMPACTION ====================*/ struct ht_forall_ctx { struct cdb_make cdbm; FILE *mainfile; - int lexminvall; long *reccount; - const char *lexminval; + int lexminvall; + const char *lexminval; /* may be invalid if lexminvall <= 0 */ }; /*---------- helper functions ----------*/ static int expiredp(const HashValue *val, struct ht_forall_ctx *a) { int r, l; - if (!val->len) return 0; + if (!val->len || a->lexminvall<=0) return 0; l= val->len < a->lexminvall ? val->len : a->lexminvall; r= memcmp(val->data, a->lexminval, l); if (r>0) return 0; @@ -524,17 +562,8 @@ static int addto_cdb(const char *key, HashValue *val, static int addto_main(const char *key, HashValue *val, struct ht_forall_ctx *a) { - int r; - (*a->reccount)++; - - r= fprintf(a->mainfile, "+%d,%d:%s->", strlen(key), val->len, key); - if (r<0) return -1; - - r= fwrite(val->data, 1, val->len, a->mainfile); - if (r != val->len) return -1; - - return 0; + return writerecord(a->mainfile, key, val); } /*---------- compact main entrypoint ----------*/ @@ -569,7 +598,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, logsz, (unsigned long)rw->mainsz); if (rc) goto x_rc; - if (rw->on_lexminval.llength) { + if (cht_scriptinv_interp(&rw->on_lexminval)) { rc= cht_scriptinv_invoke_fg(&rw->on_lexminval, 0,0); if (rc) goto x_rc; @@ -583,7 +612,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, rc= ht_forall(&rw->logincore, delete_ifexpired, &a); } else { - a.lexminval= ""; + a.lexminvall= 0; } /* merge unsuperseded records from main into hash table */ @@ -596,19 +625,23 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, expiredp, &a, ht_maybeupdate); if (ferror(a.mainfile)) { rc= cht_posixerr(ip, errno, "error reading" - " .main during compact"); goto x_rc; - } + " .main during compact"); goto x_rc; } if (r==-3) { break; } else if (r==-1 || r==-2) { errpos= ftello(a.mainfile); if (errpos<0) PE("ftello .main during report of syntax error"); - snprintf(buf,sizeof(buf), "CDB SYNTAX MAIN %lu", (unsigned long)errpos); + snprintf(buf,sizeof(buf), "CDB %s MAIN %lu", + r==-1 ? "TRUNCATED" : "SYNTAX", (unsigned long)errpos); Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1)); snprintf(buf,sizeof(buf), "%lu", (unsigned long)errpos); Tcl_ResetResult(ip); - Tcl_AppendResult(ip, "syntax error in .main during" - " compact, at file position ", buf, (char*)0); + Tcl_AppendResult(ip, + r==-1 ? "unexpected eof (truncated file)" + " in .main during compact, at file position " + : "syntax error" + " in .main during compact, at file position ", + buf, (char*)0); rc= TCL_ERROR; goto x_rc; } else { @@ -649,6 +682,9 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, r= ht_forall(&rw->logincore, addto_main, &a); if (r) { rc= cht_posixerr(ip, r, "error writing to new .main" " during compact"); goto x_rc; } + + r= putc('\n', a.mainfile); + if (r==EOF) PE("write trailing \n to main during compact"); r= fflush(a.mainfile); if (r) PE("fflush new main during compact"); r= fdatasync(fileno(a.mainfile)); @@ -667,7 +703,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz, /* done! */ - rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%l", + rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%ld", (unsigned long)rw->mainsz, *a.reccount); if (rc) goto x_rc; @@ -727,12 +763,13 @@ int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) { if (rc_close) rc= rc_close; cht_tabledataid_disposing(ip, rw_v, &cdbtcl_rwdatabases); + TFREE(rw); return rc; } /*---------- Other compaction-related entrypoints ----------*/ -static int compact_keeopen(Tcl_Interp *ip, Rw *rw, int force) { +static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) { off_t logsz; long reccount; int rc, r; @@ -741,7 +778,7 @@ static int compact_keeopen(Tcl_Interp *ip, Rw *rw, int force) { if (logsz < 0) return cht_posixerr(ip, errno, "ftell .log" " during compact check or force"); - if (!force && logsz < rw->mainsz / 10 + 1000) return TCL_OK; + if (!force && logsz < rw->mainsz / 2 + 1000) return TCL_OK; rc= compact_core(ip, rw, logsz, &reccount); if (rc) goto x_rc; @@ -764,16 +801,16 @@ static int compact_keeopen(Tcl_Interp *ip, Rw *rw, int force) { x_rc: /* doom! all updates fail after this (because rw->logfile is 0), and - * we may be usin a lot more RAM than would be ideal. Program will + * we may be using a lot more RAM than would be ideal. Program will * have to reopen if it really wants sanity. */ return rc; } int cht_do_cdbwr_compact_force(ClientData cd, Tcl_Interp *ip, void *rw_v) { - return compact_keeopen(ip, rw_v, 1); + return compact_keepopen(ip, rw_v, 1); } int cht_do_cdbwr_compact_check(ClientData cd, Tcl_Interp *ip, void *rw_v) { - return compact_keeopen(ip, rw_v, 0); + return compact_keepopen(ip, rw_v, 0); } int cht_do_cdbwr_compact_explicit(ClientData cd, Tcl_Interp *ip, void *rw_v) { @@ -786,3 +823,93 @@ int cht_do_cdbwr_compact_auto(ClientData cd, Tcl_Interp *ip, void *rw_v) { rw->autocompact= 1; return TCL_OK; } + +/*---------- Updateing ----------*/ + +static int update(Tcl_Interp *ip, Rw *rw, const char *key, + const Byte *data, int dlen) { + HashValue *val; + int rc, r; + + if (!rw->logfile) return cht_staticerr + (ip, "previous compact failed; cdbwr must be closed and reopened " + "before any further updates", "CDB BROKEN"); + + val= htv_prep(dlen); assert(val); + memcpy(htv_fillptr(val), data, dlen); + + r= writerecord(rw->logfile, key, val); + if (!r) r= fflush(rw->logfile); + if (r) PE("write update to logfile"); + + ht_update(&rw->logincore, key, val); + + if (!rw->autocompact) return TCL_OK; + return compact_keepopen(ip, rw, 0); + + x_rc: + TFREE(val); + return rc; +} + +int cht_do_cdbwr_update(ClientData cd, Tcl_Interp *ip, + void *rw_v, const char *key, Tcl_Obj *value) { + int dlen; + const char *data; + data= Tcl_GetStringFromObj(value, &dlen); assert(data); + return update(ip, rw_v, key, data, dlen); +} + +int cht_do_cdbwr_update_hb(ClientData cd, Tcl_Interp *ip, + void *rw_v, const char *key, HBytes_Value value) { + return update(ip, rw_v, key, cht_hb_data(&value), cht_hb_len(&value)); +} + +int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v, + const char *key) { + return update(ip, rw_v, key, 0, 0); +} + +/*---------- Lookups ----------*/ + +static int lookup_rw(Tcl_Interp *ip, void *rw_v, const char *key, + const Byte **data_r, int *len_r /* -1 => notfound */) { + Rw *rw= rw_v; + const HashValue *val; + + val= ht_lookup(&rw->logincore, key); + if (val) { + if (val->len) { *data_r= val->data; *len_r= val->len; return TCL_OK; } + else { *data_r= 0; *len_r= -1; return TCL_OK; } + } + + return cht_cdb_lookup_cdb(ip, &rw->cdb, key, strlen(key), data_r, len_r); +} + +int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v, + const char *key, Tcl_Obj *def, + Tcl_Obj **result) { + const Byte *data; + int dlen, r; + + r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r; + return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen, + cht_cdb_storeanswer_string); +} + +int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *rw_v, + const char *key, Tcl_Obj *def, + Tcl_Obj **result) { + const Byte *data; + int dlen, r; + + r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r; + return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen, + cht_cdb_storeanswer_hb); +} + +int cht_do_cdbtoplevel_cdb_wr(ClientData cd, Tcl_Interp *ip, + const Cdbwr_SubCommand* subcmd, + int objc, Tcl_Obj *const *objv) { + return subcmd->func((void*)subcmd,ip,objc,objv); +}