chiark / gitweb /
Multiarch: Move .so's to triplet paths, and declare M-A: same.
[chiark-tcl.git] / cdb / writeable.c
index 0df1b2799ae55a59e60672c23e99fe56f97304aa..3ad0c1e5499ab01cc994e7d54dbf352299823595 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * cdb, cdb-wr - Tcl bindings for tinycdb and a journalling write extension
- * Copyright 2006 Ian Jackson
+ * Copyright 2006-2012 Ian Jackson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -13,9 +13,7 @@
  * General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "chiark_tcl_cdb.h"
@@ -77,7 +75,7 @@ typedef struct HashValue {
 
 static HashValue *htv_prep(int len) {
   HashValue *hd;
-  hd= TALLOC((hd->data - (Byte*)hd) + len);
+  hd= TALLOC(offsetof(typeof(*hd), data) + len);
   hd->len= len;
   return hd;
 }  
@@ -163,20 +161,24 @@ typedef struct Rw {
   int ix, autocompact;
   int cdb_fd, lock_fd;
   struct cdb cdb; /* valid iff cdb_fd >= 0 */
-  FILE *logfile;
+  FILE *logfile; /* may be 0; if so, is broken */
   HashTable logincore;
   Pathbuf pbsome, pbother;
   off_t mainsz;
   ScriptToInvoke on_info, on_lexminval;
 } Rw;
 
+static void rw_cdb_close(Tcl_Interp *ip, Rw *rw) {
+  if (rw->cdb_fd >= 0) cdb_free(&rw->cdb);
+  maybe_close(rw->cdb_fd);
+}
+
 static int rw_close(Tcl_Interp *ip, Rw *rw) {
   int rc, r;
 
   rc= TCL_OK;
   ht_destroy(&rw->logincore);
-  if (rw->cdb_fd >= 0) cdb_free(&rw->cdb);
-  maybe_close(rw->cdb_fd);
+  rw_cdb_close(ip,rw);
   maybe_close(rw->lock_fd);
 
   if (rw->logfile) {
@@ -414,7 +416,7 @@ static int infocb(Tcl_Interp *ip, Rw *rw, const char *arg1,
 /*---------- Opening ----------*/
 
 static int cdbinit(Tcl_Interp *ip, Rw *rw) {
-  /* On entry, cdb_fd >=0 but cdb is _undefined_/
+  /* On entry, cdb_fd >=0 but cdb is _undefined_
    * On exit, either cdb_fd<0 or cdb is initialised */
   int r, rc;
   
@@ -611,9 +613,9 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz,
   a.reccount= reccount_r;
 
   r= fclose(rw->logfile);
+  rw->logfile= 0;
   if (r) { rc= cht_posixerr(ip, errno, "probable data loss!  failed to fclose"
                            " logfile during compact");  goto x_rc; }
-  rw->logfile= 0;
   
   rc= infocb(ip, rw, "compact-start", "log=%luby main=%luby",
             logsz, (unsigned long)rw->mainsz);
@@ -701,7 +703,7 @@ static int compact_core(Tcl_Interp *ip, Rw *rw, unsigned long logsz,
   if (!a.mainfile) PE("create .tmp for new main during compact");
 
   r= ht_forall(&rw->logincore, addto_main, &a);
-  if (r) { rc= cht_posixerr(ip, r, "error writing to new .main"
+  if (r) { rc= cht_posixerr(ip, errno, "error writing to new .main"
                            " during compact");  goto x_rc; }
 
   r= putc('\n', a.mainfile);
@@ -813,8 +815,7 @@ static int compact_keepopen(Tcl_Interp *ip, Rw *rw, int force) {
 
   rc= compact_core(ip, rw, logsz, &reccount);  if (rc) goto x_rc;
 
-  maybe_close(rw->cdb_fd);
-  rw->cdb_fd= -1;
+  rw_cdb_close(ip,rw);
   ht_destroy(&rw->logincore);
   ht_setup(&rw->logincore);
 
@@ -860,15 +861,22 @@ int cht_do_cdbwr_compact_auto(ClientData cd, Tcl_Interp *ip, void *rw_v) {
 static int update(Tcl_Interp *ip, Rw *rw, const char *key,
                  const Byte *data, int dlen) {
   HashValue *val;
+  const char *failed;
   int rc, r;
+  off_t recstart;
 
   if (strlen(key) >= KEYLEN_MAX)
     return cht_staticerr(ip, "key too long", "CDB KEYOVERFLOW");
 
   if (!rw->logfile) return cht_staticerr
-    (ip, "previous compact failed; cdbwr must be closed and reopened "
-     "before any further updates", "CDB BROKEN");
+    (ip, "failure during previous compact or error recovery;"
+     " cdbwr must be closed and reopened before any further updates",
+     "CDB BROKEN");
   
+  recstart= ftello(rw->logfile);
+  if (recstart < 0)
+    return cht_posixerr(ip, errno, "failed to ftello .jrn during update");
+
   val= htv_prep(dlen);  assert(val);
   memcpy(htv_fillptr(val), data, dlen);
 
@@ -883,6 +891,33 @@ static int update(Tcl_Interp *ip, Rw *rw, const char *key,
 
  x_rc:
   TFREE(val);
+  assert(rc);
+
+  /* Now, we have to try to sort out the journal so that it's
+   * truncated and positioned to where this abortively-written record
+   * started, with no buffered output and the error indicator clear.
+   *
+   * There seems to be no portable way to ensure the buffered unwritten
+   * output is discarded, so we close and reopen the stream.
+   */
+  fclose(rw->logfile);
+
+  rw->logfile= fopen(pathbuf_sfx(&rw->pbsome,".jrn"), "r+");
+  if (!rw->logfile) { failed= "fopen"; goto reset_fail; }
+
+  r= ftruncate(fileno(rw->logfile), recstart);
+  if (r) { failed= "ftruncate"; goto reset_fail; }
+
+  r= fseeko(rw->logfile, recstart, SEEK_SET);
+  if (r) { failed= "fseeko"; goto reset_fail; }
+
+  return rc;
+
+ reset_fail:
+  Tcl_AppendResult(ip, " (additionally, ", failed, " failed"
+                  " in error recovery: ", strerror(errno), ")", (char*)0);
+  if (rw->logfile) { fclose(rw->logfile); rw->logfile= 0; }
+
   return rc;
 }  
 
@@ -914,11 +949,18 @@ static int lookup_rw(Tcl_Interp *ip, void *rw_v, const char *key,
   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; }
+    else goto not_found;
   }
 
+  if (rw->cdb_fd<0) goto not_found;
+
   return cht_cdb_lookup_cdb(ip, &rw->cdb, key, strlen(key), data_r, len_r);
-} 
+
+ not_found:
+  *data_r= 0;
+  *len_r= -1;
+  return TCL_OK;
+}
 
 int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v,
                        const char *key, Tcl_Obj *def,