forked from organicmaps/organicmaps
[bookmarks] Safe bookmarks file overwrite when editing bookmarks
This commit is contained in:
parent
2bf457fcc4
commit
419c12b2b7
1 changed files with 11 additions and 2 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue