From 01601728a492b537c49fae629b5688d74650c4fb Mon Sep 17 00:00:00 2001 From: ian Date: Sun, 15 Jan 2006 15:42:11 +0000 Subject: [PATCH] writeable wip --- cdb/cdb.tct | 3 +++ cdb/writeable.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/cdb/cdb.tct b/cdb/cdb.tct index e55e15c..fca6ecb 100644 --- a/cdb/cdb.tct +++ b/cdb/cdb.tct @@ -26,10 +26,13 @@ Table cdbwr Cdbwr_SubCommand pathb string # files: # .main + # .lock # .cdb # .log # .tmp (might be new .main or new .cdb) # invariants: + # .lock is an empty file + # which is locked with fcntl by open # .main is a cdb native text file # and always exists # if .tmp exists it is irrelevant diff --git a/cdb/writeable.c b/cdb/writeable.c index 140c1ff..15afdf5 100644 --- a/cdb/writeable.c +++ b/cdb/writeable.c @@ -2,8 +2,67 @@ #include "chiark_tcl_cdb.h" +/*---------- Pathbuf ----------*/ + +typedef struct { + char *buf, *sfx; +} Pathbuf; + +#define MAX_SUFFIX 4 + +static void pathbuf_init(Pathbuf *pb, const char *pathb) { + int l= strlen(pathb); + pb->buf= TALLOC(l + 4); + 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); + strcpy(pb->sfx, suffix); + return pb->buf; +} +static void pathbuf_free(Pathbuf *pb) { + TFREE(pb->buf); + pb->buf= 0; +} + +/*---------- Rw data structure ----------*/ + +typedef struct { + Pathbuf pbsome, pbtmp; +} Rw; + static void destroy_cdbrw_idtabcb(Tcl_Interp *ip, void *val) { abort(); } const IdDataSpec cdbtcl_rwdatabases= { "cdb-rwdb", "cdb-openrwdatabases-table", destroy_cdbrw_idtabcb }; + + + +/*---------- Misc functionality ----------*/ + +int cht_do_cdbwr_create_empty(ClientData cd, Tcl_Interp *ip, + const char *pathb) { + Pathbuf pb; + int lock_fd=-1, fd=-1; + + pathbuf_init(&pb, pathb); + rc= acquire_lock(ip, &pb, &lock_fd); if (rc) goto x_rc; + + fd= open(pathbuf_sfx(".lock"), O_RDONLY + + + +int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb, Tcl_Obj *on_info, void **result); + + +int cht_do_cdbwr_close(ClientData cd, Tcl_Interp *ip, void *db); +int cht_do_cdbwr_close_quick(ClientData cd, Tcl_Interp *ip, void *db); +int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *db, Tcl_Obj *key, Tcl_Obj **result); +int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *db, HBytes_Value key, HBytes_Value *result); +int cht_do_cdbwr_update(ClientData cd, Tcl_Interp *ip, void *db, Tcl_Obj *key, Tcl_Obj *value); +int cht_do_cdbwr_update_hb(ClientData cd, Tcl_Interp *ip, void *db, HBytes_Value key, HBytes_Value value); +int cht_do_cdbwr_update_quick(ClientData cd, Tcl_Interp *ip, void *db, Tcl_Obj *key, Tcl_Obj *value); +int cht_do_cdbwr_update_quick_hb(ClientData cd, Tcl_Interp *ip, void *db, HBytes_Value key, HBytes_Value value); -- 2.30.2