3 #include "chiark_tcl_cdb.h"
8 /*---------- Forward declarations ----------*/
12 /*---------- Useful routines ----------*/
14 static void maybe_close(int fd) {
19 rc= cht_posixerr(ip, errno, "failed to " m); goto x_rc; \
22 /*==================== Subsystems and subtypes ====================*/
24 /*---------- Pathbuf ----------*/
26 typedef struct Pathbuf {
32 static void pathbuf_init(Pathbuf *pb, const char *pathb) {
34 pb->buf= TALLOC(l + 4);
35 memcpy(pb->buf, pathb, l);
39 static const char *pathbuf_sfx(Pathbuf *pb, const char *suffix) {
40 assert(strlen(suffix) <= MAX_SUFFIX);
41 strcpy(pb->sfx, suffix);
44 static void pathbuf_free(Pathbuf *pb) {
49 /*---------- Our hash table ----------*/
51 typedef struct HashTable {
53 Byte padding[128]; /* allow for expansion by Tcl, urgh */
57 typedef struct HashValue {
62 static HashValue *htv_prep(int len) {
64 hd= TALLOC((hd->data - (Byte*)hd) + len);
68 static Byte *htv_fillptr(HashValue *hd) {
72 static void ht_setup(HashTable *ht) {
73 Tcl_InitHashTable(&ht->t, TCL_STRING_KEYS);
75 static void ht_update(HashTable *ht, const char *key, HashValue *val_eat) {
79 he= Tcl_CreateHashEntry(&ht->t, (char*)key, &new);
80 if (!new) TFREE(Tcl_GetHashValue(he));
81 Tcl_SetHashValue(he, val_eat);
82 /* eats the value since the data structure owns the memory */
84 static void ht_maybeupdate(HashTable *ht, const char *key,
86 /* like ht_update except does not overwrite existing values */
90 he= Tcl_CreateHashEntry(&ht->t, (char*)key, &new);
91 if (!new) { TFREE(val_eat); return; }
92 Tcl_SetHashValue(he, val_eat);
95 static const HashValue *ht_lookup(HashTable *ht, const char *key) {
99 he= Tcl_FindHashEntry(ht, key);
102 return Tcl_GetHashValue(he);
105 static int ht_forall(HashTable *ht,
106 int (*fn)(const char *key, HashValue *val,
107 struct ht_forall_ctx *ctx),
108 struct ht_forall_ctx *ctx) {
109 /* Returns first positive value returned by any call to fn, or 0. */
116 for (he= Tcl_FirstHashEntry(&ht->t, &sp);
118 he= Tcl_NextHashEntry(&sp)) {
119 val= Tcl_GetHashValue(he);
120 if (!val->len) continue;
122 key= Tcl_GetHashKey(&ht->t, he);
124 r= fn(key, val, ctx);
130 static void ht_destroy(HashTable *ht) {
134 for (he= Tcl_FirstHashEntry(&ht->t, &sp);
136 he= Tcl_NextHashEntry(&sp)) {
137 /* ht_forall skips empty (deleted) entries so is no good for this */
138 TFREE(Tcl_GetHashValue(he));
140 Tcl_DeleteHashTable(&ht->t);
143 /*==================== Existential ====================*/
145 /*---------- Rw data structure ----------*/
150 struct cdb cdb; /* valid iff cdb_fd >= 0 */
153 Pathbuf pbsome, pbother;
155 ScriptToInvoke on_info, on_lexminval;
158 static int rw_close(Tcl_Interp *ip, Rw *rw) {
162 ht_destroy(&rw->logincore);
163 if (rw->cdb_fd >= 0) cdb_free(&rw->cdb);
164 maybe_close(rw->cdb_fd);
165 maybe_close(rw->lock_fd);
168 r= fclose(rw->logfile);
169 if (r && ip) { rc= cht_posixerr(ip, errno, "probable data loss! failed to"
170 " fclose logfile during untidy close"); }
173 pathbuf_free(&rw->pbsome); pathbuf_free(&rw->pbother);
178 static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *rw) { rw_close(0,rw); }
179 const IdDataSpec cdbtcl_rwdatabases= {
180 "cdb-rwdb", "cdb-openrwdatabases-table", destroy_cdbrw_idtabcb
183 /*---------- File handling ----------*/
185 static int acquire_lock(Tcl_Interp *ip, Pathbuf *pb, int *lockfd_r) {
186 /* *lockfd_r must be -1 on entry. If may be set to >=0 even
187 * on error, and must be closed by the caller. */
192 um= umask(~(mode_t)0);
195 lockmode= 0666 & ~((um & 0444)>>1);
196 /* Remove r where umask would remove w;
197 * eg umask intending 0664 here gives 0660 */
199 *lockfd_r= open(pathbuf_sfx(pb,".lock"), O_RDONLY|O_CREAT, lockmode);
201 return cht_posixerr(ip, errno, "could not open/create lockfile");
204 fl.l_whence= SEEK_SET;
209 r= fcntl(*lockfd_r, F_SETLK, &fl);
211 if (errno == EACCES || errno == EAGAIN)
212 return cht_staticerr(ip, "lock held by another process", "CDB LOCKED");
213 else return cht_posixerr(ip, errno, "unexpected error from fcntl while"
220 /*---------- Log reading and writing ----------*/
222 static int readlognum(FILE *f, int delim, int *num_r) {
224 char numbuf[20], *p, *ep;
229 c= getc(f); if (c==EOF) return -2;
230 if (c == delim) break;
231 if (!isdigit((unsigned char)c)) return -2;
233 if (p == numbuf+sizeof(numbuf)) return -2;
235 if (p == numbuf) return -2;
238 errno=0; ul= strtoul(numbuf, &ep, 10);
239 if (*ep || errno || ul >= INT_MAX/2) return -2;
244 static int readstorelogrecord(FILE *f, HashTable *ht,
245 int (*omitfn)(const HashValue*,
246 struct ht_forall_ctx *ctx),
247 struct ht_forall_ctx *ctx,
248 void (*updatefn)(HashTable*, const char*,
253 * -2 corrupt or error
254 * -3 got newline indicating end
255 * >0 value from omitfn
263 if (c==EOF) { if (feof(f)) return -1; return -2; }
264 if (c=='\n') return -3;
265 if (c!='+') return -2;
267 rc= readlognum(f, ',', &keylen); if (rc) return rc;
268 rc= readlognum(f, ':', &vallen); if (rc) return rc;
270 key= TALLOC(keylen+1);
271 val= htv_prep(vallen);
273 r= fread(key, 1,keylen, f);
274 if (r!=keylen) goto x2_free_keyval;
275 if (memchr(key,0,keylen)) goto x2_free_keyval;
278 c= getc(f); if (c!='-') goto x2_free_keyval;
279 c= getc(f); if (c!='>') goto x2_free_keyval;
281 r= fread(htv_fillptr(val), 1,vallen, f);
282 if (r!=vallen) goto x2_free_keyval;
284 rc= omitfn ? omitfn(val, ctx) : TCL_OK;
285 if (rc) { assert(rc>0); TFREE(val); }
286 else updatefn(ht, key, val);
297 static int writerecord(FILE *f, const char *key, const HashValue *val) {
300 r= fprintf(f, "+%d,%d:%s->", strlen(key), val->len, key);
303 r= fwrite(val->data, 1, val->len, f);
304 if (r != val->len) return -1;
309 /*---------- Creating ----------*/
311 int cht_do_cdbwr_create_empty(ClientData cd, Tcl_Interp *ip,
313 static const char *const toremoves[]= {
314 ".main", ".cdb", ".log", ".tmp", 0
318 int lock_fd=-1, fd=-1, rc, r;
319 const char *const *toremove;
321 pathbuf_init(&pb, pathb);
322 rc= acquire_lock(ip, &pb, &lock_fd); if (rc) goto x_rc;
324 fd= open(pathbuf_sfx(&pb, ".main"), O_RDWR|O_CREAT|O_EXCL, 0666);
325 if (fd <= 0) PE("create new database file");
327 for (toremove=toremoves; *toremove; toremove++) {
328 r= remove(*toremove);
329 if (r && errno != ENOENT)
330 PE("delete possible spurious file during creation");
337 maybe_close(lock_fd);
342 /*---------- Info callbacks ----------*/
344 static int infocbv3(Tcl_Interp *ip, Rw *rw, const char *arg1,
345 const char *arg2fmt, const char *arg3, va_list al) {
349 vsnprintf(buf, sizeof(buf), arg2fmt, al);
352 aa[na++]= cht_ret_string(ip, arg1);
353 aa[na++]= cht_ret_string(ip, buf);
354 if (arg3) aa[na++]= cht_ret_string(ip, arg3);
356 return cht_scriptinv_invoke_fg(&rw->on_info, na, aa);
359 static int infocb3(Tcl_Interp *ip, Rw *rw, const char *arg1,
360 const char *arg2fmt, const char *arg3, ...) {
364 rc= infocbv3(ip,rw,arg1,arg2fmt,arg3,al);
369 static int infocb(Tcl_Interp *ip, Rw *rw, const char *arg1,
370 const char *arg2fmt, ...) {
373 va_start(al, arg2fmt);
374 rc= infocbv3(ip,rw,arg1,arg2fmt,0,al);
379 /*---------- Opening ----------*/
381 static int cdbinit(Tcl_Interp *ip, Rw *rw) {
382 /* On entry, cdb_fd >=0 but cdb is _undefined_/
383 * On exit, either cdb_fd<0 or cdb is initialised */
386 r= cdb_init(&rw->cdb, rw->cdb_fd);
388 rc= cht_posixerr(ip, errno, "failed to initialise cdb reader");
389 close(rw->cdb_fd); rw->cdb_fd= -1; return rc;
394 int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb,
395 Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
397 const Cdbwr_SubCommand *subcmd= cd;
398 int r, rc, mainfd=-1;
401 off_t logrecstart, logjunkpos;
403 rw= TALLOC(sizeof(*rw));
404 ht_setup(&rw->logincore);
405 cht_scriptinv_init(&rw->on_info);
406 cht_scriptinv_init(&rw->on_lexminval);
407 rw->cdb_fd= rw->lock_fd= -1; rw->logfile= 0;
408 pathbuf_init(&rw->pbsome, pathb);
409 pathbuf_init(&rw->pbother, pathb);
413 rc= cht_scriptinv_set(&rw->on_lexminval, ip, on_lexminval, 0);
416 rw->on_lexminval.llength= 0;
419 mainfd= open(pathbuf_sfx(&rw->pbsome,".main"), O_RDONLY);
420 if (mainfd<0) PE("open exist3ing database file .main");
421 rc= acquire_lock(ip, &rw->pbsome, &rw->lock_fd); if (rc) goto x_rc;
423 r= fstat(mainfd, &stab); if (r) PE("fstat .main");
424 rw->mainsz= stab.st_size;
426 rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
427 if (rw->cdb_fd >=0) {
428 rc= cdbinit(ip, rw); if (rc) goto x_rc;
429 } else if (errno == ENOENT) {
431 rc= cht_staticerr(ip, ".cdb does not exist but .main is nonempty -"
432 " .cdb must have been accidentally deleted!",
441 rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".log"), "r+");
443 if (errno != ENOENT) PE("failed to open .log during open");
444 rw->logfile= fopen(rw->pbsome.buf, "w");
445 if (!rw->logfile) PE("create .log during (clean) open");
446 } else { /* rw->logfile */
447 r= fstat(fileno(rw->logfile), &stab);
448 if (r==-1) PE("fstat .log during open");
449 rc= infocb(ip, rw, "open-dirty-start", "log=%luby",
450 (unsigned long)stab.st_size);
454 logrecstart= ftello(rw->logfile);
455 if (logrecstart < 0) PE("ftello .log during (dirty) open");
456 r= readstorelogrecord(rw->logfile, &rw->logincore, 0,0, ht_update);
457 if (ferror(rw->logfile)) {
458 rc= cht_posixerr(ip, errno, "error reading .log during (dirty) open");
463 } else if (r==-2 || r==-3) {
465 logjunkpos= ftello(rw->logfile);
466 if(logjunkpos<0) PE("ftello .log during report of junk in dirty open");
468 snprintf(buf,sizeof(buf), "CDB SYNTAX LOG %lu %lu",
469 (unsigned long)logjunkpos, (unsigned long)logrecstart);
471 if (!(subcmd->flags & RWSCF_OKJUNK)) {
472 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1));
473 snprintf(buf,sizeof(buf),"%lu",(unsigned long)logjunkpos);
475 Tcl_AppendResult(ip, "syntax error (junk) in .log during"
476 " (dirty) open, at file position ", buf, (char*)0);
480 rc= infocb3(ip, rw, "open-dirty-junk", "errorfpos=%luby", buf,
481 (unsigned long)logjunkpos);
484 r= fseeko(rw->logfile, logrecstart, SEEK_SET);
485 if (r) PE("failed to fseeko .log before junk during dirty open");
487 r= ftruncate(fileno(rw->logfile), logrecstart);
488 if (r) PE("ftruncate .log to chop junk during dirty open");
494 /* now log is positioned for appending and everything is read */
506 int cht_do_cdbwr_open_okjunk(ClientData cd, Tcl_Interp *ip, const char *pathb,
507 Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
509 return cht_do_cdbwr_open(cd,ip,pathb,on_info,on_lexminval,result);
512 /*==================== COMPACTION ====================*/
514 struct ht_forall_ctx {
515 struct cdb_make cdbm;
519 const char *lexminval;
522 /*---------- helper functions ----------*/
524 static int expiredp(const HashValue *val, struct ht_forall_ctx *a) {
526 if (!val->len) return 0;
527 l= val->len < a->lexminvall ? val->len : a->lexminvall;
528 r= memcmp(val->data, a->lexminval, l);
531 return val->len < a->lexminvall;
534 static int delete_ifexpired(const char *key, HashValue *val,
535 struct ht_forall_ctx *a) {
536 if (!expiredp(val, a)) return 0;
538 /* we don't actually need to realloc it to free the memory because
539 * this will shortly all be deleted as part of the compaction */
543 static int addto_cdb(const char *key, HashValue *val,
544 struct ht_forall_ctx *a) {
545 return cdb_make_add(&a->cdbm, key, strlen(key), val->data, val->len);
548 static int addto_main(const char *key, HashValue *val,
549 struct ht_forall_ctx *a) {
551 return writerecord(a->mainfile, key, val);
554 /*---------- compact main entrypoint ----------*/
556 static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz,
558 /* creates new .cdb and .main
560 * leaves .log with old data
561 * leaves cdb fd open onto old db
562 * leaves logincore full of crap
565 int cdbfd, cdbmaking;
566 off_t errpos, newmainsz;
569 struct ht_forall_ctx a;
575 a.reccount= reccount_r;
577 r= fclose(rw->logfile);
578 if (r) { rc= cht_posixerr(ip, errno, "probable data loss! failed to fclose"
579 " logfile during compact"); goto x_rc; }
582 rc= infocb(ip, rw, "compact-start", "log=%luby main=%luby",
583 logsz, (unsigned long)rw->mainsz);
586 if (rw->on_lexminval.llength) {
587 rc= cht_scriptinv_invoke_fg(&rw->on_lexminval, 0,0);
590 res= Tcl_GetObjResult(ip); assert(res);
591 a.lexminval= Tcl_GetStringFromObj(res, &a.lexminvall);
594 /* we rely not calling Tcl_Eval during the actual compaction;
595 * if we did Tcl_Eval then the interp result would be trashed.
597 rc= ht_forall(&rw->logincore, delete_ifexpired, &a);
603 /* merge unsuperseded records from main into hash table */
605 a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".main"), "r");
606 if (!a.mainfile) PE("failed to open .main for reading during compact");
609 r= readstorelogrecord(a.mainfile, &rw->logincore,
612 if (ferror(a.mainfile)) { rc= cht_posixerr(ip, errno, "error reading"
613 " .main during compact"); goto x_rc;
617 } else if (r==-1 || r==-2) {
618 errpos= ftello(a.mainfile);
619 if (errpos<0) PE("ftello .main during report of syntax error");
620 snprintf(buf,sizeof(buf), "CDB SYNTAX MAIN %lu", (unsigned long)errpos);
621 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1));
622 snprintf(buf,sizeof(buf), "%lu", (unsigned long)errpos);
624 Tcl_AppendResult(ip, "syntax error in .main during"
625 " compact, at file position ", buf, (char*)0);
637 cdbfd= open(pathbuf_sfx(&rw->pbsome,".tmp"), O_WRONLY|O_CREAT|O_TRUNC, 0666);
638 if (cdbfd<0) PE("create .tmp for new cdb during compact");
640 r= cdb_make_start(&a.cdbm, cdbfd);
641 if (r) PE("cdb_make_start during compact");
644 r= ht_forall(&rw->logincore, addto_cdb, &a);
645 if (r) PE("cdb_make_add during compact");
647 r= cdb_make_finish(&a.cdbm);
648 if(r) PE("cdb_make_finish during compact");
651 r= fdatasync(cdbfd); if (r) PE("fdatasync new cdb during compact");
652 r= close(cdbfd); if (r) PE("close new cdb during compact");
655 r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".cdb"));
656 if (r) PE("install new .cdb during compact");
658 /* create new main */
660 a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".tmp"), "w");
661 if (!a.mainfile) PE("create .tmp for new main during compact");
663 r= ht_forall(&rw->logincore, addto_main, &a);
664 if (r) { rc= cht_posixerr(ip, r, "error writing to new .main"
665 " during compact"); goto x_rc; }
667 r= fflush(a.mainfile); if (r) PE("fflush new main during compact");
668 r= fdatasync(fileno(a.mainfile));
669 if (r) PE("fdatasync new main during compact");
671 newmainsz= ftello(a.mainfile);
672 if (newmainsz<0) PE("ftello new main during compact");
674 r= fclose(a.mainfile); if (r) PE("fclose new main during compact");
677 r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".main"));
678 if (r) PE("install new .main during compact");
680 rw->mainsz= newmainsz;
684 rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%l",
685 (unsigned long)rw->mainsz, *a.reccount);
691 if (a.mainfile) fclose(a.mainfile);
692 if (cdbmaking) cdb_make_finish(&a.cdbm);
694 remove(pathbuf_sfx(&rw->pbsome,".tmp")); /* for tidyness */
698 /*---------- Closing ----------*/
700 static int compact_forclose(Tcl_Interp *ip, Rw *rw, long *reccount_r) {
704 logsz= ftello(rw->logfile);
705 if (logsz < 0) PE("ftello logfile (during tidy close)");
707 rc= compact_core(ip, rw, logsz, reccount_r); if (rc) goto x_rc;
709 r= remove(pathbuf_sfx(&rw->pbsome,".log"));
710 if (r) PE("remove .log (during tidy close)");
717 int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) {
723 if (rw->autocompact) rc= compact_forclose(ip, rw, &reccount);
728 logsz= ftello(rw->logfile);
730 rc= cht_posixerr(ip, errno, "ftell logfile during close info");
732 rc= infocb(ip, rw, "close", "main=%luby log=%luby",
734 } else if (reccount>=0) {
735 rc= infocb(ip, rw, "close", "main=%luby nrecs=%l", rw->mainsz, reccount);
737 rc= infocb(ip, rw, "close", "main=%luby", rw->mainsz);
740 rc_close= rw_close(ip,rw);
741 if (rc_close) rc= rc_close;
743 cht_tabledataid_disposing(ip, rw_v, &cdbtcl_rwdatabases);
747 /*---------- Other compaction-related entrypoints ----------*/
749 static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) {
754 logsz= ftello(rw->logfile);
755 if (logsz < 0) return cht_posixerr(ip, errno, "ftell .log"
756 " during compact check or force");
758 if (!force && logsz < rw->mainsz / 10 + 1000) return TCL_OK;
760 rc= compact_core(ip, rw, logsz, &reccount); if (rc) goto x_rc;
762 maybe_close(rw->cdb_fd);
764 ht_destroy(&rw->logincore);
765 ht_setup(&rw->logincore);
767 rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
768 if (rw->cdb_fd < 0) PE("reopen .cdb after compact");
770 rc= cdbinit(ip, rw); if (rc) goto x_rc;
772 rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".log"), "w");
773 if (!rw->logfile) PE("reopen .log after compact");
775 r= fsync(fileno(rw->logfile)); if (r) PE("fsync .log after compact reopen");
780 /* doom! all updates fail after this (because rw->logfile is 0), and
781 * we may be using a lot more RAM than would be ideal. Program will
782 * have to reopen if it really wants sanity. */
786 int cht_do_cdbwr_compact_force(ClientData cd, Tcl_Interp *ip, void *rw_v) {
787 return compact_keepopen(ip, rw_v, 1);
789 int cht_do_cdbwr_compact_check(ClientData cd, Tcl_Interp *ip, void *rw_v) {
790 return compact_keepopen(ip, rw_v, 0);
793 int cht_do_cdbwr_compact_explicit(ClientData cd, Tcl_Interp *ip, void *rw_v) {
798 int cht_do_cdbwr_compact_auto(ClientData cd, Tcl_Interp *ip, void *rw_v) {
804 /*---------- Updateing ----------*/
806 static int update(Tcl_Interp *ip, Rw *rw, const char *key,
807 const Byte *data, int dlen) {
811 if (!rw->logfile) return cht_staticerr
812 (ip, "previous compact failed; cdbwr must be closed and reopened "
813 "before any further updates", "CDB BROKEN");
815 val= htv_prep(dlen); assert(val);
816 memcpy(htv_fillptr(val), data, dlen);
818 r= writerecord(rw->logfile, key, val);
819 if (!r) r= fflush(rw->logfile);
820 if (r) PE("write update to logfile");
822 ht_update(&rw->logincore, key, val);
823 return compact_keepopen(ip, rw, 0);
830 int cht_do_cdbwr_update(ClientData cd, Tcl_Interp *ip,
831 void *rw_v, const char *key, Tcl_Obj *value) {
834 data= Tcl_GetStringFromObj(value, &dlen); assert(data);
835 return update(ip, rw_v, key, data, dlen);
838 int cht_do_cdbwr_update_hb(ClientData cd, Tcl_Interp *ip,
839 void *rw_v, const char *key, HBytes_Value value) {
840 return update(ip, rw_v, key, cht_hb_data(&value), cht_hb_len(&value));
843 int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v,
845 return update(ip, rw_v, key, 0, 0);
848 /*---------- Lookups ----------*/
850 static int lookup(Tcl_Interp *ip, Rw *rw, const char *key, ) {
853 val= ht_lookup(&rw->logincore, key);
860 int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *db,
861 const char *key, Tcl_Obj *def, Tcl_Obj **result) {
865 int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *db, const char *key, HBytes_Value def, HBytes_Value *result);