Skip to content

Commit

Permalink
vendor faccess
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Jan 8, 2024
1 parent 64e92ca commit 070d44f
Show file tree
Hide file tree
Showing 6 changed files with 479 additions and 9 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ anyhow = "1"
crossterm = { version = "0.27", optional = true }

tempfile = "3.9"
filetime = "0.2"

# Conversion traits
once_cell = "1.19"
Expand All @@ -52,6 +53,7 @@ parking_lot = "0.12.1"


[target.'cfg(windows)'.dependencies]
windows = "0.52"
clipboard-win = { version = "5.0", features = ["std"] }

[target.'cfg(unix)'.dependencies]
Expand Down
21 changes: 12 additions & 9 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use helix_core::{
};

use crate::editor::Config;
use crate::faccess::readonly;
use crate::{DocumentId, Editor, Theme, View, ViewId};

/// 8kB of buffer space for encoding and decoding `Rope`s.
Expand Down Expand Up @@ -914,11 +915,9 @@ impl Document {
}
}

match rustix::fs::access(&path, rustix::fs::Access::WRITE_OK) {
Ok(_) => {}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(_) => bail!("file is readonly"),
};
if readonly(&path) {
bail!("File is readonly");
}

let (mut tmp_file, tmp_path) = tokio::task::spawn_blocking(
move || -> anyhow::Result<(File, tempfile::TempPath)> {
Expand All @@ -929,10 +928,14 @@ impl Document {
.await??;
to_writer(&mut tmp_file, encoding_with_bom_info, &text).await?;

let perms = tokio::fs::metadata(&path).await?.permissions();
tmp_file.set_permissions(perms).await?;
chown(&path, &tmp_path).await?;
tokio::fs::rename(tmp_path, &path).await?;
{
let path = path.clone();
tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
tmp_path.persist(path)?;
Ok(())
})
}
.await??;

let event = DocumentSavedEvent {
revision: current_rev,
Expand Down
Loading

0 comments on commit 070d44f

Please sign in to comment.