diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 6d79f543eeaf0..3d73d5560fcb4 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -34,7 +34,7 @@ use helix_core::{ }; use crate::editor::Config; -use crate::faccess::readonly; +use crate::faccess::{copy_metadata, readonly}; use crate::{DocumentId, Editor, Theme, View, ViewId}; /// 8kB of buffer space for encoding and decoding `Rope`s. @@ -844,23 +844,6 @@ impl Document { impl Future> + 'static + Send, anyhow::Error, > { - #[cfg(unix)] - async fn chown(path: &Path, other: &Path) -> anyhow::Result<()> { - use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::MetadataExt; - let meta = tokio::fs::metadata(path).await?; - - // From https://github.com/uutils/coreutils/blob/2c73e978ba882c9cd56f0f16fae6f8dcce1c9850/src/uucore/src/lib/features/perms.rs#L46-L61 - let uid = meta.uid(); - let gid = meta.gid(); - let s = std::ffi::CString::new(other.as_os_str().as_bytes())?; - let ret = unsafe { libc::chown(s.as_ptr(), uid, gid) }; - if ret != 0 { - return Err(anyhow::anyhow!(tokio::io::Error::last_os_error())); - } - Ok(()) - } - log::debug!( "submitting save of doc '{:?}'", self.path().map(|path| path.to_string_lossy()) @@ -931,6 +914,7 @@ impl Document { { let path = path.clone(); tokio::task::spawn_blocking(move || -> anyhow::Result<()> { + copy_metadata(&path, &tmp_path)?; tmp_path.persist(path)?; Ok(()) }) @@ -995,18 +979,12 @@ impl Document { } } - #[cfg(unix)] // Detect if the file is readonly and change the readonly field if necessary (unix only) pub fn detect_readonly(&mut self) { - use rustix::fs::{access, Access}; // Allows setting the flag for files the user cannot modify, like root files self.readonly = match &self.path { None => false, - Some(p) => match access(p, Access::WRITE_OK) { - Ok(_) => false, - Err(err) if err.kind() == std::io::ErrorKind::NotFound => false, - Err(_) => true, - }, + Some(p) => readonly(p), }; } diff --git a/helix-view/src/faccess.rs b/helix-view/src/faccess.rs index 2b87e594a21f4..c28a8368c76f1 100644 --- a/helix-view/src/faccess.rs +++ b/helix-view/src/faccess.rs @@ -1,5 +1,4 @@ -//! Forked from https://github.com/Freaky/faccess -//! Licensed under MIT +//! From https://github.com/Freaky/faccess use std::io; use std::path::Path; @@ -8,6 +7,7 @@ use filetime::FileTime; use bitflags::bitflags; +// Licensed under MIT from faccess bitflags! { /// Access mode flags for `access` function to test for. pub struct AccessMode: u8 { @@ -80,6 +80,7 @@ mod imp { } } +// Licensed under MIT from faccess except for `chown` and `copy_metadata` #[cfg(windows)] mod imp { use windows::core::PCWSTR; @@ -410,6 +411,7 @@ mod imp { } } +// Licensed under MIT from faccess #[cfg(not(any(unix, windows)))] mod imp { use super::*; @@ -440,7 +442,11 @@ mod imp { } pub fn readonly(p: &Path) -> bool { - imp::access(p, AccessMode::READ).is_ok() && imp::access(p, AccessMode::WRITE).is_err() + match imp::access(p, AccessMode::WRITE) { + Ok(_) => false, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => false, + Err(_) => true, + } } pub fn copy_metadata(from: &Path, to: &Path) -> std::io::Result<()> { diff --git a/helix-view/src/file.rs b/helix-view/src/file.rs deleted file mode 100644 index 8b137891791fe..0000000000000 --- a/helix-view/src/file.rs +++ /dev/null @@ -1 +0,0 @@ -