Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pad and patches config saving atomic #10974

Merged
merged 2 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Utilities/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ bool cfg::node::from_string(const std::string& value, bool dynamic)
return true;
}

cfg_log.fatal("Failed to load node: %s", error);
cfg_log.error("Failed to load node: %s", error);
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion Utilities/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace fs
constexpr auto lock = +open_mode::lock; // Prevent opening the file more than once
constexpr auto unread = +open_mode::unread; // Aggressively prevent reading the opened file (do not use)

constexpr auto rewrite = open_mode::write + open_mode::create + open_mode::trunc;
constexpr auto write_new = write + create + excl;
constexpr auto rewrite = write + create + trunc;

// File seek mode
enum class seek_mode : u32
Expand Down
14 changes: 6 additions & 8 deletions Utilities/bin_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,13 +1064,6 @@ void patch_engine::save_config(const patch_map& patches_map)
const std::string path = get_patch_config_path();
patch_log.notice("Saving patch config file %s", path);

fs::file file(path, fs::rewrite);
if (!file)
{
patch_log.fatal("Failed to open patch config file %s (%s)", path, fs::g_tls_error);
return;
}

YAML::Emitter out;
out << YAML::BeginMap;

Expand Down Expand Up @@ -1134,7 +1127,12 @@ void patch_engine::save_config(const patch_map& patches_map)

out << YAML::EndMap;

file.write(out.c_str(), out.size());
fs::pending_file file(path);

if (!file.file || (file.file.write(out.c_str(), out.size()), !file.commit()))
{
patch_log.error("Failed to create patch config file %s (%s)", path, fs::g_tls_error);
}
}

static void append_patches(patch_engine::patch_map& existing_patches, const patch_engine::patch_map& new_patches, usz& count, usz& total, std::stringstream* log_messages)
Expand Down
8 changes: 3 additions & 5 deletions rpcs3/Emu/Io/pad_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ void cfg_input::save(const std::string& title_id, const std::string& profile) co
input_log.fatal("Failed to create path: %s (%s)", cfg_name, fs::g_tls_error);
}

if (auto cfg_file = fs::file(cfg_name, fs::rewrite))
{
cfg_file.write(to_string());
}
else
fs::pending_file cfg_file(cfg_name);

if (!cfg_file.file || (cfg_file.file.write(to_string()), !cfg_file.commit()))
{
input_log.error("Failed to save pad config to '%s'", cfg_name);
}
Expand Down
8 changes: 3 additions & 5 deletions rpcs3/rpcs3qt/patch_manager_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,11 +982,9 @@ bool patch_manager_dialog::handle_json(const QByteArray& data)
}

// Overwrite current patch file
if (fs::file patch_file = fs::file(path, fs::rewrite))
{
patch_file.write(content);
}
else
fs::pending_file patch_file(path);

if (!patch_file.file || (patch_file.file.write(content), !patch_file.commit()))
{
patch_log.error("Could not save new patches to %s (%s)", path, fs::g_tls_error);
return false;
Expand Down