Skip to content

Commit

Permalink
fix: 潜在的文件夹创建异常
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 6, 2024
1 parent 0f3d875 commit 9c92768
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
5 changes: 4 additions & 1 deletion source/MaaFramework/Controller/ControllerAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,10 @@ bool ControllerAgent::recording() const
void ControllerAgent::init_recording()
{
auto recording_dir = GlobalOptionMgr::get_instance().log_dir() / "recording";
std::filesystem::create_directories(recording_dir);
if (std::error_code ec; recording_dir.has_parent_path() && !std::filesystem::create_directories(recording_dir.parent_path(), ec)) {
LogError << "failed to create_directories" << VAR(recording_dir.parent_path()) << VAR(ec.message());
return;
}
recording_path_ = recording_dir / std::format("maa_recording_{}.txt", format_now_for_filename());
}

Expand Down
13 changes: 11 additions & 2 deletions source/MaaProjectInterface/Impl/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,18 @@ void Configurator::save(const std::filesystem::path& user_dir)
{
LogInfo << VAR(user_dir);

std::filesystem::create_directories((user_dir / kConfigFilename).parent_path());
const auto config_path = user_dir / kConfigFilename;
if (std::error_code ec; config_path.has_parent_path() && !std::filesystem::create_directories(config_path.parent_path(), ec)) {
LogError << "failed to create_directories" << VAR(config_path.parent_path()) << VAR(ec.message());
return;
}

std::ofstream ofs(config_path);
if (!ofs.is_open()) {
LogError << "failed to open" << VAR(config_path);
return;
}

std::ofstream ofs(user_dir / kConfigFilename);
ofs << config_.to_json();
}

Expand Down
6 changes: 5 additions & 1 deletion source/MaaToolkit/Config/GlobalOptionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ bool GlobalOptionConfig::save() const
{
LogInfo;

std::filesystem::create_directories(config_path_.parent_path());
if (std::error_code ec; config_path_.has_parent_path() && !std::filesystem::create_directories(config_path_.parent_path(), ec)) {
LogError << "failed to create_directories" << VAR(config_path_.parent_path()) << VAR(ec.message());
return false;
}

std::ofstream ofs(config_path_, std::ios::out);
if (!ofs.is_open()) {
LogError << "Failed to open config file" << config_path_;
Expand Down
6 changes: 4 additions & 2 deletions source/MaaUtils/Logger/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ void Logger::open()
if (log_path_.empty()) {
return;
}

std::filesystem::create_directories(log_dir_);

if (std::error_code ec; log_dir_.has_parent_path() && !std::filesystem::create_directories(log_dir_.parent_path(), ec)) {
return;
}

std::unique_lock trace_lock(trace_mutex_);
if (ofs_.is_open()) {
Expand Down
3 changes: 1 addition & 2 deletions source/include/Utils/ImageIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ inline cv::Mat imread(const std::string& utf8_path, int flags = cv::IMREAD_COLOR

inline bool imwrite(const std::filesystem::path& path, cv::InputArray img)
{
if (path.has_parent_path() && !std::filesystem::exists(path.parent_path())
&& !std::filesystem::create_directories(path.parent_path())) {
if (std::error_code ec; path.has_parent_path() && !std::filesystem::create_directories(path.parent_path(), ec)) {
return false;
}

Expand Down

0 comments on commit 9c92768

Please sign in to comment.