summaryrefslogtreecommitdiff
path: root/src/common/lock.cpp
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2013-02-27 14:10:08 -0800
committerBen Longbons <b.r.longbons@gmail.com>2013-03-01 14:20:46 -0800
commit2979ad0aa6f8de173254eb7ae867b46894b51105 (patch)
treea1777372d24cdc71bd0dd39121e3ab7223daaf97 /src/common/lock.cpp
parent30335063bff54c2b4782689eebdb3b2717e878fa (diff)
downloadtmwa-2979ad0aa6f8de173254eb7ae867b46894b51105.tar.gz
tmwa-2979ad0aa6f8de173254eb7ae867b46894b51105.tar.bz2
tmwa-2979ad0aa6f8de173254eb7ae867b46894b51105.tar.xz
tmwa-2979ad0aa6f8de173254eb7ae867b46894b51105.zip
Keep a few backup copies of the DB
Diffstat (limited to 'src/common/lock.cpp')
-rw-r--r--src/common/lock.cpp18
1 files 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);
}
}