diff --git a/src/control/Control.cpp b/src/control/Control.cpp index 7b13f6e8..a74631d8 100644 --- a/src/control/Control.cpp +++ b/src/control/Control.cpp @@ -213,13 +213,12 @@ void Control::renameLastAutosaveFile() string error; if (filename.exists()) { - int result = g_unlink(renamed.c_str()); - if (result != 0) + if (!renamed.deleteFile()) { error += FS(_F("Could not delete old autosave file \"{1}\"") % renamed.str()); } - result = g_rename(filename.c_str(), renamed.c_str()); + int result = g_rename(filename.c_str(), renamed.c_str()); if (result != 0) { if (!error.empty()) @@ -231,15 +230,14 @@ void Control::renameLastAutosaveFile() } else { - int result = g_unlink(renamed.c_str()); - if (result != 0) + if (!renamed.deleteFile()) { error += FS(_F("Could not delete old autosave file \"{1}\"") % renamed.str()); } this->save(false); - result = g_rename(filename.c_str(), renamed.c_str()); + int result = g_rename(filename.c_str(), renamed.c_str()); if (result != 0) { if (!error.empty()) diff --git a/src/util/Path.cpp b/src/util/Path.cpp index 2a74a56d..090b8403 100644 --- a/src/util/Path.cpp +++ b/src/util/Path.cpp @@ -1,6 +1,9 @@ #include "Path.h" #include "StringUtils.h" +#include + + Path::Path() { } @@ -40,6 +43,28 @@ bool Path::exists() return g_file_test(path.c_str(), G_FILE_TEST_EXISTS); } +/** + * Delete the file + * + * @return true if the file is deleted or does not exists + */ +bool Path::deleteFile() +{ + if (!exists()) + { + // Does not exists, therefore cannot be deleted + return true; + } + + int result = g_unlink(c_str()); + if (result == 0) + { + return true; + } + + return false; +} + /** * Compare the path with another one */ diff --git a/src/util/Path.h b/src/util/Path.h index 32118730..94db6e6b 100644 --- a/src/util/Path.h +++ b/src/util/Path.h @@ -37,6 +37,13 @@ public: */ bool exists(); + /** + * Delete the file + * + * @return true if the file is deleted or does not exists + */ + bool deleteFile(); + /** * Compare the path with another one */