tkrzw: "too small file" error when database was not closed

I’m using the C bridge, in combination with HashDBM, version 0.9.38.

If tkrzw_dbm_close was not called for some reason, and program is terminated, next time the database is opened with tkrzw_dbm_open, the result is NULL, and tkrzw_last_status_message returns “too small file”.

The only way I found to restore the database is with

$ tkrzw_dbm_util rebuild <file>

($ tkrzw_dbm_util restore <file> does not work for that case)

I know that tkrzw_dbm_close always should be called. But in practice, it can always happen that it was not called. For example when an unexpected system reboot or program fault occurs.

Is there any way to prevent issues while opening a previously unclosed database? For example an auto restore in tkrzw_dbm_open?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 27 (12 by maintainers)

Most upvoted comments

I released 0.9.41 where this issue is HIDDEN (not fixed) by another logic to omit the restore operation. To reproduce this issue, the following patch is useful.

diff --git a/example/langc_ex1.c b/example/langc_ex1.c
index b9ceb6d..0b01544 100644
--- a/example/langc_ex1.c
+++ b/example/langc_ex1.c
@@ -18,7 +18,7 @@
 int main(int argc, char** argv) {
   // Opens the database file.
   TkrzwDBM* dbm = tkrzw_dbm_open(
-      "casket.tkh", true, "truncate=true,num_buckets=100");
+      "casket.tkh", true, "truncate=false,num_buckets=100");
   
   // Stores records.
   tkrzw_dbm_set(dbm, "foo", -1, "hop", -1, true);
@@ -48,7 +48,7 @@ int main(int argc, char** argv) {
   tkrzw_dbm_iter_free(iter);
   
   // Closes the database file.
-  tkrzw_dbm_close(dbm);
+  // tkrzw_dbm_close(dbm);
 
   return 0;
 }
diff --git a/tkrzw_dbm_hash.cc b/tkrzw_dbm_hash.cc
index 64ddef5..46671c0 100644
--- a/tkrzw_dbm_hash.cc
+++ b/tkrzw_dbm_hash.cc
@@ -276,7 +276,7 @@ Status HashDBMImpl::Open(const std::string& path, bool writable,
                file_size_ == act_file_size) {
       healthy_ = true;
       closure_flags_ |= CLOSURE_FLAG_CLOSE;
-    } else if (tuning_params.restore_mode == HashDBM::RESTORE_DEFAULT &&
+    } else if (false && tuning_params.restore_mode == HashDBM::RESTORE_DEFAULT &&
                (static_flags_ & STATIC_FLAG_UPDATE_IN_PLACE) &&
                file_size_ <= act_file_size &&
                ValidateRecordsImpl(record_base_, act_file_size,

On my Intel Macbook, this issue is not reproduced even if I applied the patch. I’ll investigate this issue further when I obtained M1 Macbook.