chiark / gitweb /
@@ -2,9 +2,13 @@
[chiark-tcl.git] / cdb / writeable.c
index 6dc412b21ded57aca8e516ee88ce0f99b1252b4c..02c98cb8ad773230984f454f02f8b3205d826213 100644 (file)
@@ -1,7 +1,27 @@
-/**/
+/*
+ * cdb, cdb-wr - Tcl bindings for tinycdb and a journalling write extension
+ * Copyright 2006 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
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * 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.
+ */
 
 #include "chiark_tcl_cdb.h"
 
+#define KEYLEN_MAX (INT_MAX/2)
+
 #define ftello ftell
 #define fseeko fseek
 
@@ -26,7 +46,8 @@ typedef struct Pathbuf {
 #define MAX_SUFFIX 5
 
 static void pathbuf_init(Pathbuf *pb, const char *pathb) {
-  int l= strlen(pathb);
+  size_t l= strlen(pathb);
+  assert(l < INT_MAX);
   pb->buf= TALLOC(l + MAX_SUFFIX + 1);
   memcpy(pb->buf, pathb, l);
   pb->sfx= pb->buf + l;
@@ -142,7 +163,7 @@ 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;
@@ -232,7 +253,7 @@ static int readlognum(FILE *f, int delim, int *num_r) {
   *p= 0;
 
   errno=0; ul= strtoul(numbuf, &ep, 10);
-  if (*ep || errno || ul >= INT_MAX/2) return -2;
+  if (*ep || errno || ul >= KEYLEN_MAX) return -2;
   *num_r= ul;
   return 0;
 }
@@ -295,7 +316,7 @@ static int readstorelogrecord(FILE *f, HashTable *ht,
 static int writerecord(FILE *f, const char *key, const HashValue *val) {
   int r;
 
-  r= fprintf(f, "+%d,%d:%s->", strlen(key), val->len, key);
+  r= fprintf(f, "+%d,%d:%s->", (int)strlen(key), val->len, key);
   if (r<0) return -1;
   
   r= fwrite(val->data, 1, val->len, f);
@@ -590,9 +611,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);
@@ -680,7 +701,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);
@@ -839,12 +860,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);
 
@@ -859,6 +890,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;
 }  
 
@@ -917,9 +975,3 @@ int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *rw_v,
   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);
-}