Skip to content

fix: Write in a temp file before replacing the original file, fixes #903 #1370

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions crates/rnote-ui/src/canvas/imexport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,28 +232,39 @@ impl RnCanvas {
let file_write_operation = async move {
let bytes = rnote_bytes_receiver.await??;
self.set_output_file_expect_write(true);

let mut temp_path = file_path.to_path_buf();
temp_path.set_extension("tmp");

let mut write_file = async_fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&file_path)
.open(&temp_path)
.await
.context(format!(
"Failed to create/open/truncate file for path '{}'",
file_path.display()
temp_path.display()
))?;
if !skip_set_output_file {
// this installs the file watcher.
self.set_output_file(Some(file.to_owned()));
}
write_file.write_all(&bytes).await.context(format!(
"Failed to write bytes to file with path '{}'",
file_path.display()
temp_path.display()
))?;
write_file.sync_all().await.context(format!(
"Failed to sync file after writing with path '{}'",
file_path.display()
temp_path.display()
))?;
// If everything goes well, replace the original file
async_fs::rename(&temp_path, &file_path)
.await
.context(format!(
"Failed to rename temporary file to '{}'",
file_path.display()
))?;
Ok(())
};

Expand Down