Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/coding
diff options
context:
space:
mode:
authorSergey Magidovich <mgsergio@mapswithme.com>2016-03-11 17:34:10 +0300
committerSergey Yershov <yershov@corp.mail.ru>2016-03-23 16:56:28 +0300
commit9cec925752462dd806bce66c2c8cbff6e9144666 (patch)
tree6e779e374d4f782b7c616ae0ed6ae3f3aedd4764 /coding
parentf9e8c97c6fb55926ff906e0320cf68c618dc02a3 (diff)
Code review. Fix data loss.
Diffstat (limited to 'coding')
-rw-r--r--coding/internal/file_data.cpp21
-rw-r--r--coding/internal/file_data.hpp10
2 files changed, 29 insertions, 2 deletions
diff --git a/coding/internal/file_data.cpp b/coding/internal/file_data.cpp
index c02650f47e..316a399285 100644
--- a/coding/internal/file_data.cpp
+++ b/coding/internal/file_data.cpp
@@ -6,12 +6,14 @@
#include "base/exception.hpp"
#include "base/logging.hpp"
+#include "base/string_utils.hpp"
#include "std/cerrno.hpp"
#include "std/cstring.hpp"
#include "std/exception.hpp"
#include "std/fstream.hpp"
#include "std/target_os.hpp"
+#include "std/thread.hpp"
#ifdef OMIM_OS_WINDOWS
#include <io.h>
@@ -267,6 +269,25 @@ bool RenameFileX(string const & fOld, string const & fNew)
return CheckFileOperationResult(res, fOld);
}
+bool WriteToTempAndRenameToFile(string const & dest, function<bool(string const &)> const & write,
+ string const & tmp)
+{
+ string const tmpFileName =
+ tmp.empty() ? dest + ".tmp" + strings::to_string(this_thread::get_id()) : tmp;
+ if (!write(tmpFileName))
+ {
+ LOG(LERROR, ("Can't write to", tmpFileName));
+ return false;
+ }
+ if (!RenameFileX(tmpFileName, dest))
+ {
+ LOG(LERROR, ("Can't rename file", tmpFileName, "to", dest));
+ DeleteFileX(tmpFileName);
+ return false;
+ }
+ return true;
+}
+
bool CopyFileX(string const & fOld, string const & fNew)
{
try
diff --git a/coding/internal/file_data.hpp b/coding/internal/file_data.hpp
index ce118bb8c5..2bd4514d98 100644
--- a/coding/internal/file_data.hpp
+++ b/coding/internal/file_data.hpp
@@ -3,9 +3,10 @@
#include "base/base.hpp"
+#include "std/functional.hpp"
+#include "std/noncopyable.hpp"
#include "std/string.hpp"
#include "std/target_os.hpp"
-#include "std/noncopyable.hpp"
#ifdef OMIM_OS_TIZEN
namespace Tizen
@@ -58,8 +59,13 @@ private:
bool GetFileSize(string const & fName, uint64_t & sz);
bool DeleteFileX(string const & fName);
bool RenameFileX(string const & fOld, string const & fNew);
+
+/// Write to temp file and rename it to dest. Delete temp on failure.
+/// @param write function that writes to file with a given name, returns true on success.
+bool WriteToTempAndRenameToFile(string const & dest, function<bool(string const &)> const & write,
+ string const & tmp = "");
+
/// @return false if copy fails. DO NOT THROWS exceptions
bool CopyFileX(string const & fOld, string const & fNew);
bool IsEqualFiles(string const & firstFile, string const & secondFile);
-
}