3 #include "chiark_tcl_cdb.h"
8 /*---------- Forward declarations ----------*/
12 /*---------- Useful routines ----------*/
14 static void maybe_close(int fd) {
18 /*==================== Subsystems and subtypes ====================*/
20 /*---------- Pathbuf ----------*/
22 typedef struct Pathbuf {
28 static void pathbuf_init(Pathbuf *pb, const char *pathb) {
30 pb->buf= TALLOC(l + MAX_SUFFIX + 1);
31 memcpy(pb->buf, pathb, l);
34 static const char *pathbuf_sfx(Pathbuf *pb, const char *suffix) {
35 assert(strlen(suffix) <= MAX_SUFFIX);
36 strcpy(pb->sfx, suffix);
39 static void pathbuf_free(Pathbuf *pb) {
44 /*---------- Our hash table ----------*/
46 typedef struct HashTable {
48 Byte padding[128]; /* allow for expansion by Tcl, urgh */
52 typedef struct HashValue {
57 static HashValue *htv_prep(int len) {
59 hd= TALLOC((hd->data - (Byte*)hd) + len);
63 static Byte *htv_fillptr(HashValue *hd) {
67 static void ht_setup(HashTable *ht) {
68 Tcl_InitHashTable(&ht->t, TCL_STRING_KEYS);
70 static void ht_update(HashTable *ht, const char *key, HashValue *val_eat) {
74 he= Tcl_CreateHashEntry(&ht->t, (char*)key, &new);
75 if (!new) TFREE(Tcl_GetHashValue(he));
76 Tcl_SetHashValue(he, val_eat);
77 /* eats the value since the data structure owns the memory */
79 static void ht_maybeupdate(HashTable *ht, const char *key,
81 /* like ht_update except does not overwrite existing values */
85 he= Tcl_CreateHashEntry(&ht->t, (char*)key, &new);
86 if (!new) { TFREE(val_eat); return; }
87 Tcl_SetHashValue(he, val_eat);
90 static const HashValue *ht_lookup(HashTable *ht, const char *key) {
93 he= Tcl_FindHashEntry(&ht->t, key);
96 return Tcl_GetHashValue(he);
99 static int ht_forall(HashTable *ht,
100 int (*fn)(const char *key, HashValue *val,
101 struct ht_forall_ctx *ctx),
102 struct ht_forall_ctx *ctx) {
103 /* Returns first positive value returned by any call to fn, or 0. */
110 for (he= Tcl_FirstHashEntry(&ht->t, &sp);
112 he= Tcl_NextHashEntry(&sp)) {
113 val= Tcl_GetHashValue(he);
114 if (!val->len) continue;
116 key= Tcl_GetHashKey(&ht->t, he);
118 r= fn(key, val, ctx);
124 static void ht_destroy(HashTable *ht) {
128 for (he= Tcl_FirstHashEntry(&ht->t, &sp);
130 he= Tcl_NextHashEntry(&sp)) {
131 /* ht_forall skips empty (deleted) entries so is no good for this */
132 TFREE(Tcl_GetHashValue(he));
134 Tcl_DeleteHashTable(&ht->t);
137 /*==================== Existential ====================*/
139 /*---------- Rw data structure ----------*/
144 struct cdb cdb; /* valid iff cdb_fd >= 0 */
147 Pathbuf pbsome, pbother;
149 ScriptToInvoke on_info, on_lexminval;
152 static int rw_close(Tcl_Interp *ip, Rw *rw) {
156 ht_destroy(&rw->logincore);
157 if (rw->cdb_fd >= 0) cdb_free(&rw->cdb);
158 maybe_close(rw->cdb_fd);
159 maybe_close(rw->lock_fd);
162 r= fclose(rw->logfile);
163 if (r && ip) { rc= cht_posixerr(ip, errno, "probable data loss! failed to"
164 " fclose logfile during untidy close"); }
167 pathbuf_free(&rw->pbsome); pathbuf_free(&rw->pbother);
171 static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *rw) {
175 const IdDataSpec cdbtcl_rwdatabases= {
176 "cdb-rwdb", "cdb-openrwdatabases-table", destroy_cdbrw_idtabcb
179 /*---------- File handling ----------*/
181 static int acquire_lock(Tcl_Interp *ip, Pathbuf *pb, int *lockfd_r) {
182 /* *lockfd_r must be -1 on entry. If may be set to >=0 even
183 * on error, and must be closed by the caller. */
188 um= umask(~(mode_t)0);
191 lockmode= 0666 & ~((um & 0444)>>1);
192 /* Remove r where umask would remove w;
193 * eg umask intending 0664 here gives 0660 */
195 *lockfd_r= open(pathbuf_sfx(pb,".lock"), O_RDWR|O_CREAT, lockmode);
197 return cht_posixerr(ip, errno, "could not open/create lockfile");
200 fl.l_whence= SEEK_SET;
205 r= fcntl(*lockfd_r, F_SETLK, &fl);
207 if (errno == EACCES || errno == EAGAIN)
208 return cht_staticerr(ip, "lock held by another process", "CDB LOCKED");
209 else return cht_posixerr(ip, errno, "unexpected error from fcntl while"
216 /*---------- Log reading and writing ----------*/
218 static int readlognum(FILE *f, int delim, int *num_r) {
220 char numbuf[20], *p, *ep;
225 c= getc(f); if (c==EOF) return -2;
226 if (c == delim) break;
227 if (!isdigit((unsigned char)c)) return -2;
229 if (p == numbuf+sizeof(numbuf)) return -2;
231 if (p == numbuf) return -2;
234 errno=0; ul= strtoul(numbuf, &ep, 10);
235 if (*ep || errno || ul >= INT_MAX/2) return -2;
240 static int readstorelogrecord(FILE *f, HashTable *ht,
241 int (*omitfn)(const HashValue*,
242 struct ht_forall_ctx *ctx),
243 struct ht_forall_ctx *ctx,
244 void (*updatefn)(HashTable*, const char*,
249 * -2 corrupt or error
250 * -3 got newline indicating end
251 * >0 value from omitfn
259 if (c==EOF) { return feof(f) ? -1 : -2; }
260 if (c=='\n') return -3;
261 if (c!='+') return -2;
263 rc= readlognum(f, ',', &keylen); if (rc) return rc;
264 rc= readlognum(f, ':', &vallen); if (rc) return rc;
266 key= TALLOC(keylen+1);
267 val= htv_prep(vallen);
269 r= fread(key, 1,keylen, f);
270 if (r!=keylen) goto x2_free_keyval;
271 if (memchr(key,0,keylen)) goto x2_free_keyval;
274 c= getc(f); if (c!='-') goto x2_free_keyval;
275 c= getc(f); if (c!='>') goto x2_free_keyval;
277 r= fread(htv_fillptr(val), 1,vallen, f);
278 if (r!=vallen) goto x2_free_keyval;
280 c= getc(f); if (c!='\n') goto x2_free_keyval;
282 rc= omitfn ? omitfn(val, ctx) : TCL_OK;
283 if (rc) { assert(rc>0); TFREE(val); }
284 else updatefn(ht, key, val);
295 static int writerecord(FILE *f, const char *key, const HashValue *val) {
298 r= fprintf(f, "+%d,%d:%s->", strlen(key), val->len, key);
301 r= fwrite(val->data, 1, val->len, f);
302 if (r != val->len) return -1;
305 if (r==EOF) return -1;
310 /*---------- Creating ----------*/
312 int cht_do_cdbwr_create_empty(ClientData cd, Tcl_Interp *ip,
314 static const char *const toremoves[]= { ".cdb", ".log", ".tmp", 0 };
317 int lock_fd=-1, rc, r;
319 const char *const *toremove;
322 pathbuf_init(&pb, pathb);
323 pathbuf_init(&pbmain, pathb);
325 rc= acquire_lock(ip, &pb, &lock_fd); if (rc) goto x_rc;
327 r= lstat(pathbuf_sfx(&pbmain, ".main"), &stab);
328 if (!r) { rc= cht_staticerr(ip, "database already exists during creation",
329 "CDB ALREADY-EXISTS"); goto x_rc; }
330 if (errno != ENOENT) PE("check for existing database .main during creation");
332 for (toremove=toremoves; *toremove; toremove++) {
333 r= remove(pathbuf_sfx(&pb, *toremove));
334 if (r && errno != ENOENT)
335 PE("delete possible spurious file during creation");
338 f= fopen(pathbuf_sfx(&pb, ".tmp"), "w");
339 if (!f) PE("create new database .tmp");
340 r= putc('\n', f); if (r==EOF) PE("write sentinel to new database .tmp");
341 r= fclose(f); f=0; if (r) PE("close new database .tmp during creation");
343 r= rename(pb.buf, pbmain.buf);
344 if (r) PE("install new database .tmp as .main (finalising creation)");
350 maybe_close(lock_fd);
352 pathbuf_free(&pbmain);
356 /*---------- Info callbacks ----------*/
358 static int infocbv3(Tcl_Interp *ip, Rw *rw, const char *arg1,
359 const char *arg2fmt, const char *arg3, va_list al) {
363 vsnprintf(buf, sizeof(buf), arg2fmt, al);
366 aa[na++]= cht_ret_string(ip, arg1);
367 aa[na++]= cht_ret_string(ip, buf);
368 if (arg3) aa[na++]= cht_ret_string(ip, arg3);
370 return cht_scriptinv_invoke_fg(&rw->on_info, na, aa);
373 static int infocb3(Tcl_Interp *ip, Rw *rw, const char *arg1,
374 const char *arg2fmt, const char *arg3, ...) {
378 rc= infocbv3(ip,rw,arg1,arg2fmt,arg3,al);
383 static int infocb(Tcl_Interp *ip, Rw *rw, const char *arg1,
384 const char *arg2fmt, ...) {
387 va_start(al, arg2fmt);
388 rc= infocbv3(ip,rw,arg1,arg2fmt,0,al);
393 /*---------- Opening ----------*/
395 static int cdbinit(Tcl_Interp *ip, Rw *rw) {
396 /* On entry, cdb_fd >=0 but cdb is _undefined_/
397 * On exit, either cdb_fd<0 or cdb is initialised */
400 r= cdb_init(&rw->cdb, rw->cdb_fd);
402 rc= cht_posixerr(ip, errno, "failed to initialise cdb reader");
403 close(rw->cdb_fd); rw->cdb_fd= -1; return rc;
408 int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb,
409 Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
411 const Cdbwr_SubCommand *subcmd= cd;
412 int r, rc, mainfd=-1;
415 off_t logrecstart, logjunkpos;
417 rw= TALLOC(sizeof(*rw));
419 ht_setup(&rw->logincore);
420 cht_scriptinv_init(&rw->on_info);
421 cht_scriptinv_init(&rw->on_lexminval);
422 rw->cdb_fd= rw->lock_fd= -1; rw->logfile= 0;
423 pathbuf_init(&rw->pbsome, pathb);
424 pathbuf_init(&rw->pbother, pathb);
427 rc= cht_scriptinv_set(&rw->on_info, ip, on_info, 0);
430 rc= cht_scriptinv_set(&rw->on_lexminval, ip, on_lexminval, 0);
433 mainfd= open(pathbuf_sfx(&rw->pbsome,".main"), O_RDONLY);
434 if (mainfd<0) PE("open existing database file .main");
435 rc= acquire_lock(ip, &rw->pbsome, &rw->lock_fd); if (rc) goto x_rc;
437 r= fstat(mainfd, &stab); if (r) PE("fstat .main");
438 rw->mainsz= stab.st_size;
440 rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
441 if (rw->cdb_fd >=0) {
442 rc= cdbinit(ip, rw); if (rc) goto x_rc;
443 } else if (errno == ENOENT) {
444 if (rw->mainsz > 1) {
445 rc= cht_staticerr(ip, ".cdb does not exist but .main is >1byte -"
446 " .cdb must have been accidentally deleted!",
455 rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".log"), "r+");
457 if (errno != ENOENT) PE("failed to open .log during open");
458 rw->logfile= fopen(rw->pbsome.buf, "w");
459 if (!rw->logfile) PE("create .log during (clean) open");
460 } else { /* rw->logfile */
461 r= fstat(fileno(rw->logfile), &stab);
462 if (r==-1) PE("fstat .log during open");
463 rc= infocb(ip, rw, "open-dirty-start", "log=%luby",
464 (unsigned long)stab.st_size);
468 logrecstart= ftello(rw->logfile);
469 if (logrecstart < 0) PE("ftello .log during (dirty) open");
470 r= readstorelogrecord(rw->logfile, &rw->logincore, 0,0, ht_update);
471 if (ferror(rw->logfile)) {
472 rc= cht_posixerr(ip, errno, "error reading .log during (dirty) open");
477 } else if (r==-2 || r==-3) {
479 logjunkpos= ftello(rw->logfile);
480 if(logjunkpos<0) PE("ftello .log during report of junk in dirty open");
482 snprintf(buf,sizeof(buf), "CDB SYNTAX LOG %lu %lu",
483 (unsigned long)logjunkpos, (unsigned long)logrecstart);
485 if (!(subcmd->flags & RWSCF_OKJUNK)) {
486 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1));
487 snprintf(buf,sizeof(buf),"%lu",(unsigned long)logjunkpos);
489 Tcl_AppendResult(ip, "syntax error (junk) in .log during"
490 " (dirty) open, at file position ", buf, (char*)0);
494 rc= infocb3(ip, rw, "open-dirty-junk", "errorfpos=%luby", buf,
495 (unsigned long)logjunkpos);
498 r= fseeko(rw->logfile, logrecstart, SEEK_SET);
499 if (r) PE("failed to fseeko .log before junk during dirty open");
501 r= ftruncate(fileno(rw->logfile), logrecstart);
502 if (r) PE("ftruncate .log to chop junk during dirty open");
508 /* now log is positioned for appending and everything is read */
521 int cht_do_cdbwr_open_okjunk(ClientData cd, Tcl_Interp *ip, const char *pathb,
522 Tcl_Obj *on_info, Tcl_Obj *on_lexminval,
524 return cht_do_cdbwr_open(cd,ip,pathb,on_info,on_lexminval,result);
527 /*==================== COMPACTION ====================*/
529 struct ht_forall_ctx {
530 struct cdb_make cdbm;
534 const char *lexminval; /* may be invalid if lexminvall <= 0 */
537 /*---------- helper functions ----------*/
539 static int expiredp(const HashValue *val, struct ht_forall_ctx *a) {
541 if (!val->len || a->lexminvall<=0) return 0;
542 l= val->len < a->lexminvall ? val->len : a->lexminvall;
543 r= memcmp(val->data, a->lexminval, l);
546 return val->len < a->lexminvall;
549 static int delete_ifexpired(const char *key, HashValue *val,
550 struct ht_forall_ctx *a) {
551 if (!expiredp(val, a)) return 0;
553 /* we don't actually need to realloc it to free the memory because
554 * this will shortly all be deleted as part of the compaction */
558 static int addto_cdb(const char *key, HashValue *val,
559 struct ht_forall_ctx *a) {
560 return cdb_make_add(&a->cdbm, key, strlen(key), val->data, val->len);
563 static int addto_main(const char *key, HashValue *val,
564 struct ht_forall_ctx *a) {
566 return writerecord(a->mainfile, key, val);
569 /*---------- compact main entrypoint ----------*/
571 static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz,
573 /* creates new .cdb and .main
575 * leaves .log with old data
576 * leaves cdb fd open onto old db
577 * leaves logincore full of crap
580 int cdbfd, cdbmaking;
581 off_t errpos, newmainsz;
584 struct ht_forall_ctx a;
590 a.reccount= reccount_r;
592 r= fclose(rw->logfile);
593 if (r) { rc= cht_posixerr(ip, errno, "probable data loss! failed to fclose"
594 " logfile during compact"); goto x_rc; }
597 rc= infocb(ip, rw, "compact-start", "log=%luby main=%luby",
598 logsz, (unsigned long)rw->mainsz);
601 if (cht_scriptinv_interp(&rw->on_lexminval)) {
602 rc= cht_scriptinv_invoke_fg(&rw->on_lexminval, 0,0);
605 res= Tcl_GetObjResult(ip); assert(res);
606 a.lexminval= Tcl_GetStringFromObj(res, &a.lexminvall);
609 /* we rely not calling Tcl_Eval during the actual compaction;
610 * if we did Tcl_Eval then the interp result would be trashed.
612 rc= ht_forall(&rw->logincore, delete_ifexpired, &a);
618 /* merge unsuperseded records from main into hash table */
620 a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".main"), "r");
621 if (!a.mainfile) PE("failed to open .main for reading during compact");
624 r= readstorelogrecord(a.mainfile, &rw->logincore,
627 if (ferror(a.mainfile)) { rc= cht_posixerr(ip, errno, "error reading"
628 " .main during compact"); goto x_rc; }
631 } else if (r==-1 || r==-2) {
632 errpos= ftello(a.mainfile);
633 if (errpos<0) PE("ftello .main during report of syntax error");
634 snprintf(buf,sizeof(buf), "CDB %s MAIN %lu",
635 r==-1 ? "TRUNCATED" : "SYNTAX", (unsigned long)errpos);
636 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(buf,-1));
637 snprintf(buf,sizeof(buf), "%lu", (unsigned long)errpos);
640 r==-1 ? "unexpected eof (truncated file)"
641 " in .main during compact, at file position "
643 " in .main during compact, at file position ",
656 cdbfd= open(pathbuf_sfx(&rw->pbsome,".tmp"), O_WRONLY|O_CREAT|O_TRUNC, 0666);
657 if (cdbfd<0) PE("create .tmp for new cdb during compact");
659 r= cdb_make_start(&a.cdbm, cdbfd);
660 if (r) PE("cdb_make_start during compact");
663 r= ht_forall(&rw->logincore, addto_cdb, &a);
664 if (r) PE("cdb_make_add during compact");
666 r= cdb_make_finish(&a.cdbm);
667 if(r) PE("cdb_make_finish during compact");
670 r= fdatasync(cdbfd); if (r) PE("fdatasync new cdb during compact");
671 r= close(cdbfd); if (r) PE("close new cdb during compact");
674 r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".cdb"));
675 if (r) PE("install new .cdb during compact");
677 /* create new main */
679 a.mainfile= fopen(pathbuf_sfx(&rw->pbsome,".tmp"), "w");
680 if (!a.mainfile) PE("create .tmp for new main during compact");
682 r= ht_forall(&rw->logincore, addto_main, &a);
683 if (r) { rc= cht_posixerr(ip, r, "error writing to new .main"
684 " during compact"); goto x_rc; }
686 r= putc('\n', a.mainfile);
687 if (r==EOF) PE("write trailing \n to main during compact");
689 r= fflush(a.mainfile); if (r) PE("fflush new main during compact");
690 r= fdatasync(fileno(a.mainfile));
691 if (r) PE("fdatasync new main during compact");
693 newmainsz= ftello(a.mainfile);
694 if (newmainsz<0) PE("ftello new main during compact");
696 r= fclose(a.mainfile); if (r) PE("fclose new main during compact");
699 r= rename(rw->pbsome.buf, pathbuf_sfx(&rw->pbother,".main"));
700 if (r) PE("install new .main during compact");
702 rw->mainsz= newmainsz;
706 rc= infocb(ip, rw, "compact-end", "main=%luby nrecs=%ld",
707 (unsigned long)rw->mainsz, *a.reccount);
713 if (a.mainfile) fclose(a.mainfile);
714 if (cdbmaking) cdb_make_finish(&a.cdbm);
716 remove(pathbuf_sfx(&rw->pbsome,".tmp")); /* for tidyness */
720 /*---------- Closing ----------*/
722 static int compact_forclose(Tcl_Interp *ip, Rw *rw, long *reccount_r) {
726 logsz= ftello(rw->logfile);
727 if (logsz < 0) PE("ftello logfile (during tidy close)");
729 rc= compact_core(ip, rw, logsz, reccount_r); if (rc) goto x_rc;
731 r= remove(pathbuf_sfx(&rw->pbsome,".log"));
732 if (r) PE("remove .log (during tidy close)");
739 int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *rw_v) {
745 if (rw->autocompact) rc= compact_forclose(ip, rw, &reccount);
750 logsz= ftello(rw->logfile);
752 rc= cht_posixerr(ip, errno, "ftell logfile during close info");
754 rc= infocb(ip, rw, "close", "main=%luby log=%luby",
756 } else if (reccount>=0) {
757 rc= infocb(ip, rw, "close", "main=%luby nrecs=%l", rw->mainsz, reccount);
759 rc= infocb(ip, rw, "close", "main=%luby", rw->mainsz);
762 rc_close= rw_close(ip,rw);
763 if (rc_close) rc= rc_close;
765 cht_tabledataid_disposing(ip, rw_v, &cdbtcl_rwdatabases);
770 /*---------- Other compaction-related entrypoints ----------*/
772 static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) {
777 logsz= ftello(rw->logfile);
778 if (logsz < 0) return cht_posixerr(ip, errno, "ftell .log"
779 " during compact check or force");
781 if (!force && logsz < rw->mainsz / 2 + 1000) return TCL_OK;
783 rc= compact_core(ip, rw, logsz, &reccount); if (rc) goto x_rc;
785 maybe_close(rw->cdb_fd);
787 ht_destroy(&rw->logincore);
788 ht_setup(&rw->logincore);
790 rw->cdb_fd= open(pathbuf_sfx(&rw->pbsome,".cdb"), O_RDONLY);
791 if (rw->cdb_fd < 0) PE("reopen .cdb after compact");
793 rc= cdbinit(ip, rw); if (rc) goto x_rc;
795 rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".log"), "w");
796 if (!rw->logfile) PE("reopen .log after compact");
798 r= fsync(fileno(rw->logfile)); if (r) PE("fsync .log after compact reopen");
803 /* doom! all updates fail after this (because rw->logfile is 0), and
804 * we may be using a lot more RAM than would be ideal. Program will
805 * have to reopen if it really wants sanity. */
809 int cht_do_cdbwr_compact_force(ClientData cd, Tcl_Interp *ip, void *rw_v) {
810 return compact_keepopen(ip, rw_v, 1);
812 int cht_do_cdbwr_compact_check(ClientData cd, Tcl_Interp *ip, void *rw_v) {
813 return compact_keepopen(ip, rw_v, 0);
816 int cht_do_cdbwr_compact_explicit(ClientData cd, Tcl_Interp *ip, void *rw_v) {
821 int cht_do_cdbwr_compact_auto(ClientData cd, Tcl_Interp *ip, void *rw_v) {
827 /*---------- Updateing ----------*/
829 static int update(Tcl_Interp *ip, Rw *rw, const char *key,
830 const Byte *data, int dlen) {
834 if (!rw->logfile) return cht_staticerr
835 (ip, "previous compact failed; cdbwr must be closed and reopened "
836 "before any further updates", "CDB BROKEN");
838 val= htv_prep(dlen); assert(val);
839 memcpy(htv_fillptr(val), data, dlen);
841 r= writerecord(rw->logfile, key, val);
842 if (!r) r= fflush(rw->logfile);
843 if (r) PE("write update to logfile");
845 ht_update(&rw->logincore, key, val);
847 if (!rw->autocompact) return TCL_OK;
848 return compact_keepopen(ip, rw, 0);
855 int cht_do_cdbwr_update(ClientData cd, Tcl_Interp *ip,
856 void *rw_v, const char *key, Tcl_Obj *value) {
859 data= Tcl_GetStringFromObj(value, &dlen); assert(data);
860 return update(ip, rw_v, key, data, dlen);
863 int cht_do_cdbwr_update_hb(ClientData cd, Tcl_Interp *ip,
864 void *rw_v, const char *key, HBytes_Value value) {
865 return update(ip, rw_v, key, cht_hb_data(&value), cht_hb_len(&value));
868 int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v,
870 return update(ip, rw_v, key, 0, 0);
873 /*---------- Lookups ----------*/
875 static int lookup_rw(Tcl_Interp *ip, void *rw_v, const char *key,
876 const Byte **data_r, int *len_r /* -1 => notfound */) {
878 const HashValue *val;
880 val= ht_lookup(&rw->logincore, key);
882 if (val->len) { *data_r= val->data; *len_r= val->len; return TCL_OK; }
883 else { *data_r= 0; *len_r= -1; return TCL_OK; }
886 return cht_cdb_lookup_cdb(ip, &rw->cdb, key, strlen(key), data_r, len_r);
889 int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v,
890 const char *key, Tcl_Obj *def,
895 r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r;
896 return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
897 cht_cdb_storeanswer_string);
900 int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *rw_v,
901 const char *key, Tcl_Obj *def,
906 r= lookup_rw(ip, rw_v, key, &data, &dlen); if (r) return r;
907 return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
908 cht_cdb_storeanswer_hb);
911 int cht_do_cdbtoplevel_cdb_wr(ClientData cd, Tcl_Interp *ip,
912 const Cdbwr_SubCommand* subcmd,
913 int objc, Tcl_Obj *const *objv) {
914 return subcmd->func((void*)subcmd,ip,objc,objv);