From 2979ad0aa6f8de173254eb7ae867b46894b51105 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Wed, 27 Feb 2013 14:10:08 -0800 Subject: Keep a few backup copies of the DB --- src/common/lock.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/common/lock.cpp b/src/common/lock.cpp index a708c40..82856e1 100644 --- a/src/common/lock.cpp +++ b/src/common/lock.cpp @@ -9,6 +9,10 @@ #include "../poison.hpp" +/// number of backups to keep +static +const int backup_count = 10; + /// Protected file writing /// (Until the file is closed, it keeps the old file) @@ -36,7 +40,17 @@ void lock_fclose(FILE *fp, const char *filename, int *info) if (fp) { fclose_(fp); - std::string newfile = STRPRINTF("%s_%d.tmp", filename, *info); - rename(newfile.c_str(), filename); + int n = backup_count; + std::string old_filename = STRPRINTF("%s.%d", filename, n); + while (--n) + { + std::string newer_filename = STRPRINTF("%s.%d", filename, n); + rename(newer_filename.c_str(), old_filename.c_str()); + old_filename = std::move(newer_filename); + } + rename(filename, old_filename.c_str()); + + std::string tmpfile = STRPRINTF("%s_%d.tmp", filename, *info); + rename(tmpfile.c_str(), filename); } } -- cgit v1.2.3-60-g2f50