diff options
Diffstat (limited to 'src/io/lock.cpp')
-rw-r--r-- | src/io/lock.cpp | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/io/lock.cpp b/src/io/lock.cpp index 96c3649..911b5db 100644 --- a/src/io/lock.cpp +++ b/src/io/lock.cpp @@ -1,5 +1,5 @@ #include "lock.hpp" -// io/lock.hpp - Output to files with atomic replacement and backups. +// io/lock.cpp - Output to files with atomic replacement and backups. // // Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com> // @@ -21,7 +21,13 @@ #include <fcntl.h> #include <unistd.h> +#include <cerrno> +#include <cstdio> +#include <cstdlib> + +#include "../strings/astring.hpp" #include "../strings/zstring.hpp" +#include "../strings/literal.hpp" #include "cxxstdio.hpp" #include "fd.hpp" @@ -29,9 +35,11 @@ #include "../poison.hpp" +namespace tmwa +{ /// number of backups to keep static -const int backup_count = 10; +const int backup_count = 9; /// Protected file writing /// (Until the file is closed, it keeps the old file) @@ -49,7 +57,7 @@ namespace io AString newfile; do { - newfile = STRPRINTF("%s_%d.tmp", filename, no++); + newfile = STRPRINTF("%s_%d.tmp"_fmt, filename, no++); fd = FD::open(newfile, O_WRONLY | O_CREAT | O_EXCL, 0666); } while (fd == FD() && errno == EEXIST); @@ -67,21 +75,22 @@ namespace io if (!WriteFile::close()) { // leave partial file - FPRINTF(stderr, "Warning: failed to write replacement for %s\n", filename); + FPRINTF(stderr, "Warning: failed to write replacement for %s\n"_fmt, filename); abort(); } int n = backup_count; - AString old_filename = STRPRINTF("%s.%d", filename, n); + AString old_filename = STRPRINTF("%s.%d"_fmt, filename, n); while (--n) { - AString newer_filename = STRPRINTF("%s.%d", filename, n); + AString newer_filename = STRPRINTF("%s.%d"_fmt, filename, n); rename(newer_filename.c_str(), old_filename.c_str()); old_filename = std::move(newer_filename); } rename(filename.c_str(), old_filename.c_str()); - AString tmpfile = STRPRINTF("%s_%d.tmp", filename, tmp_suffix); + AString tmpfile = STRPRINTF("%s_%d.tmp"_fmt, filename, tmp_suffix); rename(tmpfile.c_str(), filename.c_str()); } } // namespace io +} // namespace tmwa |