Added delete to Path with error handling

presentation
Andreas Butti 7 years ago
parent 1e32e4fbf5
commit f92c0cd447
  1. 10
      src/control/Control.cpp
  2. 25
      src/util/Path.cpp
  3. 7
      src/util/Path.h

@ -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())

@ -1,6 +1,9 @@
#include "Path.h"
#include "StringUtils.h"
#include <glib/gstdio.h>
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
*/

@ -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
*/

Loading…
Cancel
Save