diff --git a/android/src/com/mapswithme/maps/settings/StoragePathManager.java b/android/src/com/mapswithme/maps/settings/StoragePathManager.java index 0f8f96b6a6..ef05b9f2f0 100644 --- a/android/src/com/mapswithme/maps/settings/StoragePathManager.java +++ b/android/src/com/mapswithme/maps/settings/StoragePathManager.java @@ -382,19 +382,17 @@ public class StoragePathManager { for (int i = 0; i < oldFiles.length; ++i) { - if (MapManager.nativeMoveFile(oldFiles[i].getAbsolutePath(), newFiles[i].getAbsolutePath())) + File parent = newFiles[i].getParentFile(); + if (parent != null) + parent.mkdirs(); + if (!MapManager.nativeMoveFile(oldFiles[i].getAbsolutePath(), newFiles[i].getAbsolutePath())) { - // No need to delete oldFiles[i] because it was moved to newFiles[i]. - oldFiles[i] = null; - } else - { - File parent = newFiles[i].getParentFile(); - if (parent != null) - parent.mkdirs(); - StorageUtils.copyFile(oldFiles[i], newFiles[i]); + throw new IOException("Failed to move " + oldFiles[i].getAbsolutePath() + " to " + newFiles[i] + .getAbsolutePath()); } } - } catch (IOException e) + } + catch (IOException e) { e.printStackTrace(); // In the case of failure delete all new files. Old files will diff --git a/coding/internal/file_data.cpp b/coding/internal/file_data.cpp index 1900ba8465..28c5b10b5b 100644 --- a/coding/internal/file_data.cpp +++ b/coding/internal/file_data.cpp @@ -195,6 +195,23 @@ bool RenameFileX(string const & fOld, string const & fNew) return CheckFileOperationResult(res, fOld); } +bool MoveFileX(string const & fOld, string const & fNew) +{ + // Try to rename the file first. + int res = rename(fOld.c_str(), fNew.c_str()); + if (res == 0) + return true; + + // Otherwise perform the full move. + if (!CopyFileX(fOld, fNew)) + { + (void) DeleteFileX(fNew); + return false; + } + (void) DeleteFileX(fOld); + return true; +} + bool WriteToTempAndRenameToFile(string const & dest, function const & write, string const & tmp) { @@ -232,33 +249,22 @@ void AppendFileToFile(string const & fromFilename, string const & toFilename) bool CopyFileX(string const & fOld, string const & fNew) { + ifstream ifs; + ofstream ofs; + ifs.exceptions(ifstream::failbit | ifstream::badbit); + ofs.exceptions(ifstream::failbit | ifstream::badbit); + try { - ifstream ifs(fOld.c_str()); - ofstream ofs(fNew.c_str()); - - if (ifs.is_open() && ofs.is_open()) - { - if (ifs.peek() == ifstream::traits_type::eof()) - return true; - - ofs << ifs.rdbuf(); - ofs.flush(); - - if (ofs.fail()) - { - LOG(LWARNING, ("Bad or Fail bit is set while writing file:", fNew)); - return false; - } - - return true; - } - else - LOG(LERROR, ("Can't open files:", fOld, fNew)); + ifs.open(fOld.c_str()); + ofs.open(fNew.c_str()); + ofs << ifs.rdbuf(); + ofs.flush(); + return true; } catch (exception const & ex) { - LOG(LERROR, ("Copy file error:", ex.what())); + LOG(LERROR, ("Failed to copy file from", fOld, "to", fNew, ":", strerror(errno))); } return false; diff --git a/coding/internal/file_data.hpp b/coding/internal/file_data.hpp index 0bfe136daa..0d7c39e7ee 100644 --- a/coding/internal/file_data.hpp +++ b/coding/internal/file_data.hpp @@ -58,7 +58,9 @@ bool WriteToTempAndRenameToFile(std::string const & dest, void AppendFileToFile(std::string const & fromFilename, std::string const & toFilename); -/// @return false if copy fails. DO NOT THROWS exceptions +/// @return false if copy fails. DOES NOT THROWS exceptions bool CopyFileX(std::string const & fOld, std::string const & fNew); +/// @return false if moving fails. DOES NOT THROW exceptions +bool MoveFileX(std::string const & fOld, std::string const & fNew); bool IsEqualFiles(std::string const & firstFile, std::string const & secondFile); } // namespace base