mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
fix: make sure creating directories in OutputPathFilesystem cannot lead to a crash
This commit is contained in:
parent
27c01e0a41
commit
b584cd7423
@ -1,6 +1,8 @@
|
||||
#include "OutputPathFilesystem.h"
|
||||
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@ -17,11 +19,21 @@ std::unique_ptr<std::ostream> OutputPathFilesystem::Open(const std::string& file
|
||||
return nullptr;
|
||||
|
||||
const auto containingDirectory = fullNewPath.parent_path();
|
||||
fs::create_directories(containingDirectory);
|
||||
|
||||
std::error_code ec;
|
||||
fs::create_directories(containingDirectory, ec);
|
||||
if (ec)
|
||||
{
|
||||
std::cerr << std::format("Failed to create folder '{}' when try to open file '{}'\n", containingDirectory, fileName);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::ofstream stream(fullNewPath, std::ios::binary | std::ios::out);
|
||||
if (!stream.is_open())
|
||||
{
|
||||
std::cerr << std::format("Failed to open file '{}'\n", fileName);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_unique<std::ofstream>(std::move(stream));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user