diff options
author | Alex Zolotarev <alex@mapswithme.com> | 2014-01-15 22:47:07 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:10:15 +0300 |
commit | 419c12b2b707ea88a4c3e8cc70a56c06e452ab2d (patch) | |
tree | 786bc3d38f5abd526ca5bd2e943e26a473b15db6 /map/bookmark.cpp | |
parent | 2bf457fcc492b1b06759de8467fa616f54f36086 (diff) |
[bookmarks] Safe bookmarks file overwrite when editing bookmarks
Diffstat (limited to 'map/bookmark.cpp')
-rw-r--r-- | map/bookmark.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/map/bookmark.cpp b/map/bookmark.cpp index ce990ca2c1..71fa1f5a7d 100644 --- a/map/bookmark.cpp +++ b/map/bookmark.cpp @@ -775,18 +775,24 @@ bool BookmarkCategory::SaveToKMLFile() else m_file = GenerateUniqueFileName(GetPlatform().WritableDir(), name); + string const fileTmp = m_file + ".tmp"; + try { + // First, we save to the temporary file /// @todo On Windows UTF-8 file names are not supported. - ofstream of(m_file.c_str()); + ofstream of(fileTmp.c_str(), std::ios_base::out | std::ios_base::trunc); SaveToKML(of); of.flush(); if (!of.fail()) { + // Only after successfull save we replace original file + my::DeleteFileX(m_file); + VERIFY(my::RenameFileX(fileTmp, m_file), (fileTmp, m_file)); // delete old file if (!oldFile.empty()) - VERIFY ( my::DeleteFileX(oldFile), (oldFile, m_file)); + VERIFY(my::DeleteFileX(oldFile), (oldFile, m_file)); return true; } @@ -798,6 +804,9 @@ bool BookmarkCategory::SaveToKMLFile() LOG(LWARNING, ("Can't save bookmarks category", m_name, "to file", m_file)); + // remove possibly left tmp file + my::DeleteFileX(fileTmp); + // return old file name in case of error if (!oldFile.empty()) m_file.swap(oldFile); |