diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/lock.cpp | 18 |
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); } } |