54 lines
1.1 KiB
C++
54 lines
1.1 KiB
C++
#include "SafeFileUtils.h"
|
|
|
|
#ifdef _WIN32
|
|
#include <winerror.h>
|
|
#endif
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
void safeRemoveFile(const fs::path& p, const fs::path& tempFolder)
|
|
{
|
|
try
|
|
{
|
|
bool result = fs::remove(p);
|
|
}
|
|
catch (fs::filesystem_error& e)
|
|
{
|
|
if (e.code().category() == boost::system::system_category()
|
|
#ifdef _WIN32
|
|
&& e.code().value() == ERROR_ACCESS_DENIED)
|
|
#else
|
|
&& e.code().value() == EACCES)
|
|
#endif
|
|
{
|
|
auto cleanupFolder = tempFolder;
|
|
|
|
if (!fs::exists(cleanupFolder))
|
|
{
|
|
fs::create_directories(cleanupFolder);
|
|
}
|
|
|
|
if (!fs::is_directory(cleanupFolder))
|
|
{
|
|
throw std::runtime_error(".upd/.remove folder is not directory");
|
|
}
|
|
|
|
auto fileName = fs::unique_path();
|
|
fs::rename(p, cleanupFolder / fileName);
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
SafeFile safeCreateFile(const fs::path & p, const fs::path& tempFolder)
|
|
{
|
|
if (fs::exists(p))
|
|
{
|
|
safeRemoveFile(p, tempFolder);
|
|
}
|
|
return SafeFile(p);
|
|
}
|