Move project save commit planning to app core

This commit is contained in:
2026-06-06 12:09:36 +02:00
parent a03db82307
commit f3834827b1
8 changed files with 214 additions and 28 deletions

View File

@@ -118,6 +118,23 @@ struct DocumentCanvasProjectSaveWritePlan {
bool falls_back_to_direct_on_temporary_open_failure = false;
};
struct DocumentCanvasProjectSaveCommitInput {
bool used_temporary = false;
bool target_remove_attempted = false;
bool target_remove_succeeded = false;
bool temporary_rename_attempted = false;
bool temporary_rename_succeeded = false;
};
struct DocumentCanvasProjectSaveCommitPlan {
bool saved = false;
bool used_temporary = false;
bool target_removed = false;
bool temporary_renamed = false;
bool target_may_be_missing = false;
std::string_view log_message;
};
class DocumentCanvasClearServices {
public:
virtual ~DocumentCanvasClearServices() = default;
@@ -397,6 +414,36 @@ plan_document_canvas_project_save_write(
return pp::foundation::Result<DocumentCanvasProjectSaveWritePlan>::success(std::move(plan));
}
[[nodiscard]] constexpr DocumentCanvasProjectSaveCommitPlan plan_document_canvas_project_save_commit(
DocumentCanvasProjectSaveCommitInput input) noexcept
{
DocumentCanvasProjectSaveCommitPlan plan;
plan.used_temporary = input.used_temporary;
if (!input.used_temporary) {
plan.saved = true;
plan.log_message = "project saved to target";
return plan;
}
if (!input.target_remove_attempted || !input.target_remove_succeeded) {
plan.log_message = "could not remove target project before temporary swap";
return plan;
}
plan.target_removed = true;
if (!input.temporary_rename_attempted || !input.temporary_rename_succeeded) {
plan.target_may_be_missing = true;
plan.log_message = "temporary project not swapped after original removal";
return plan;
}
plan.saved = true;
plan.temporary_renamed = true;
plan.log_message = "temporary project swapped successfully";
return plan;
}
[[nodiscard]] inline pp::foundation::Result<DocumentCanvasClearPlan> plan_document_canvas_clear(
bool has_canvas,
float r = 0.0F,

View File

@@ -2541,33 +2541,39 @@ bool Canvas::project_save_thread(std::string file_path, bool show_progress)
fclose(fp);
bool success = false;
bool target_remove_attempted = false;
bool target_remove_succeeded = false;
bool temporary_rename_attempted = false;
bool temporary_rename_succeeded = false;
if (use_tmp)
{
LOG("project saved tmp to %s", tmp_path.c_str());
LOG("swapping to %s", file_path.c_str());
if (std::remove(file_path.c_str()) == 0)
target_remove_attempted = true;
target_remove_succeeded = std::remove(file_path.c_str()) == 0;
if (target_remove_succeeded)
{
if (std::rename(tmp_path.c_str(), file_path.c_str()) == 0)
{
success = true;
LOG("tmp file swapped succesfully");
}
else
{
success = false;
LOG("tmp file NOT swapped, original removed");
}
}
else
{
success = false;
LOG("could not remove %s", file_path.c_str());
temporary_rename_attempted = true;
temporary_rename_succeeded = std::rename(tmp_path.c_str(), file_path.c_str()) == 0;
}
}
else
{
success = true;
const auto commit_plan = pp::app::plan_document_canvas_project_save_commit(
pp::app::DocumentCanvasProjectSaveCommitInput {
.used_temporary = use_tmp,
.target_remove_attempted = target_remove_attempted,
.target_remove_succeeded = target_remove_succeeded,
.temporary_rename_attempted = temporary_rename_attempted,
.temporary_rename_succeeded = temporary_rename_succeeded,
});
const bool success = commit_plan.saved;
if (commit_plan.saved && commit_plan.temporary_renamed) {
LOG("tmp file swapped succesfully");
} else if (!commit_plan.saved && commit_plan.target_may_be_missing) {
LOG("tmp file NOT swapped, original removed");
} else if (!commit_plan.saved && commit_plan.used_temporary) {
LOG("could not remove %s", file_path.c_str());
} else if (commit_plan.saved) {
LOG("project saved to %s", file_path.c_str());
}