Skip to content

Commit

Permalink
Replace std::filesystem throwing functions with std::error_code match
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Feb 10, 2021
1 parent 3c625eb commit 965ca2f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/OrcCommand/Command/WolfLauncher/WolfLauncher_Run.cpp
Expand Up @@ -573,7 +573,8 @@ HRESULT Orc::Command::Wolf::Main::CreateAndUploadOutline()
outlineSize());
}

if (std::filesystem::exists(config.Outline.Path))
std::error_code ec;
if (std::filesystem::exists(config.Outline.Path, ec))
{
if (auto hr = UploadSingleFile(config.Outline.FileName, config.Outline.Path); FAILED(hr))
{
Expand Down
32 changes: 13 additions & 19 deletions src/OrcLib/ArchiveExtract.cpp
Expand Up @@ -180,14 +180,11 @@ STDMETHODIMP ArchiveExtract::Extract(
}
fs::path absolute(szDirectory);

try
std::error_code ec;
fs::create_directories(absolute, ec);
if (ec)
{
fs::create_directories(absolute);
}
catch (const fs::filesystem_error& error)
{
const auto [hr, msg] = AnsiToWide(error.what());
Log::Error(L"Failed to create directory '{}' while processing file '{}': {}", absolute, item.Path, msg);
Log::Error(L"Failed to create directory '{}' while processing file '{}' [{}]", absolute, item.Path, ec);
return nullptr;
}

Expand Down Expand Up @@ -297,13 +294,11 @@ STDMETHODIMP ArchiveExtract::Extract(
}
fs::path absolute(szDirectory);

try
{
fs::create_directories(absolute);
}
catch (const fs::filesystem_error& error)
std::error_code ec;
fs::create_directories(absolute, ec);
if (ec)
{
auto code = error.code();
Log::Error(L"Failed to create directory '{}' [{}]", absolute, ec);
return nullptr;
}

Expand Down Expand Up @@ -365,15 +360,14 @@ STDMETHODIMP ArchiveExtract::Extract(
}
fs::path absolute(szDirectory);

try
std::error_code ec;
fs::create_directories(absolute, ec);
if (ec)
{
fs::create_directories(absolute);
}
catch (const fs::filesystem_error& exception)
{
auto code = exception.code();
Log::Error(L"Failed to create directory '{}' [{}]", absolute, ec);
return nullptr;
}

auto filestream = make_shared<FileStream>();
if (FAILED(hr = filestream->OpenHandle(hFile)))
return nullptr;
Expand Down
8 changes: 7 additions & 1 deletion src/OrcLib/EmbeddedResource_Extract.cpp
Expand Up @@ -1110,7 +1110,13 @@ HRESULT EmbeddedResource::ExpandArchivesAndBinaries(const std::wstring& outDir,

wstring out = outDir + L"\\" + item.Name;

fs::create_directories(fs::path(out));
std::error_code ec;
fs::create_directories(fs::path(out), ec);
if (ec)
{
Log::Error(L"Failed to create directory '{}' [{}]", out, ec);
continue;
}

if (FAILED(hr = extractor->Extract(archStream, out.c_str(), nullptr)))
{
Expand Down
4 changes: 3 additions & 1 deletion src/OrcLib/ExtensionLibrary.cpp
Expand Up @@ -396,7 +396,9 @@ STDMETHODIMP ExtensionLibrary::Cleanup()
if (auto hr = UtilDeleteTemporaryFile(m_libFile); FAILED(hr))
return hr;
}
if (m_bDeleteTemp && !m_tempDir.empty() && std::filesystem::exists(m_tempDir))

std::error_code ec;
if (m_bDeleteTemp && !m_tempDir.empty() && std::filesystem::exists(m_tempDir, ec))
{
if (auto hr = UtilDeleteTemporaryDirectory(m_tempDir); FAILED(hr))
return hr;
Expand Down
7 changes: 6 additions & 1 deletion src/OrcLib/Log/Sink/FileSink.h
Expand Up @@ -53,7 +53,12 @@ class FileSink final : public spdlog::sinks::base_sink<Mutex>
return;
}

std::filesystem::remove(path);
std::filesystem::remove(path, ec);
if (ec)
{
Log::Debug(L"Failed to remove '{}' [{}]", path, ec);
ec.clear();
}

// Dump memorySink if any
if (m_memorySink)
Expand Down

0 comments on commit 965ca2f

Please sign in to comment.