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

Read symlink when writing files #10339

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,9 @@ impl Document {
"Path is read only"
));
}
let write_path = std::fs::read_link(&path).unwrap_or_else(|_| path.clone());
kirawi marked this conversation as resolved.
Show resolved Hide resolved
let backup = if path.exists() {
let path_ = path.clone();
let path_ = write_path.clone();
// hacks: we use tempfile to handle the complex task of creating
// non clobbered temporary path for us we don't want
// the whole automatically delete path on drop thing
Expand All @@ -925,7 +926,7 @@ impl Document {
};

let write_result: anyhow::Result<_> = async {
let mut dst = tokio::fs::File::create(&path).await?;
let mut dst = tokio::fs::File::create(&write_path).await?;
to_writer(&mut dst, encoding_with_bom_info, &text).await?;
Ok(())
}
Expand All @@ -934,14 +935,13 @@ impl Document {
if let Some(backup) = backup {
if write_result.is_err() {
// restore backup
let _ = tokio::fs::rename(&backup, &path)
let _ = tokio::fs::rename(&backup, &write_path)
.await
.map_err(|e| log::error!("Failed to restore backup on write failure: {e}"));
} else {
// copy metadata and delete backup
let path_ = path.clone();
let _ = tokio::task::spawn_blocking(move || {
let _ = copy_metadata(&backup, &path_)
let _ = copy_metadata(&backup, &write_path)
.map_err(|e| log::error!("Failed to copy metadata on write: {e}"));
let _ = std::fs::remove_file(backup)
.map_err(|e| log::error!("Failed to remove backup file on write: {e}"));
Expand Down
Loading