diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 3d73d5560fcb4..bcf9ef421ef90 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -899,7 +899,10 @@ impl Document { } if readonly(&path) { - bail!("File is readonly"); + bail!(std::io::Error::new( + std::io::ErrorKind::PermissionDenied, + "Path is read only" + )); } let (mut tmp_file, tmp_path) = tokio::task::spawn_blocking( @@ -988,23 +991,6 @@ impl Document { }; } - #[cfg(not(unix))] - // Detect if the file is readonly and change the readonly field if necessary (non-unix os) - pub fn detect_readonly(&mut self) { - // TODO Use the Windows' function `CreateFileW` to check if a file is readonly - // Discussion: https://github.com/helix-editor/helix/pull/7740#issuecomment-1656806459 - // Vim implementation: https://github.com/vim/vim/blob/4c0089d696b8d1d5dc40568f25ea5738fa5bbffb/src/os_win32.c#L7665 - // Windows binding: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Storage/FileSystem/fn.CreateFileW.html - self.readonly = match &self.path { - None => false, - Some(p) => match std::fs::metadata(p) { - Err(err) if err.kind() == std::io::ErrorKind::NotFound => false, - Err(_) => false, - Ok(metadata) => metadata.permissions().readonly(), - }, - }; - } - /// Reload the document from its path. pub fn reload( &mut self, diff --git a/helix-view/src/faccess.rs b/helix-view/src/faccess.rs index c28a8368c76f1..71800b88130d6 100644 --- a/helix-view/src/faccess.rs +++ b/helix-view/src/faccess.rs @@ -1,4 +1,4 @@ -//! From https://github.com/Freaky/faccess +//! From use std::io; use std::path::Path; @@ -66,7 +66,7 @@ mod imp { chown(to, Some(uid), Some(gid))?; let mut perms = meta.permissions(); - let new_perms = (perms.mode() & 0x0707) | (perms.mode() & 0x07) << 3; + let new_perms = (perms.mode() & 0o0707) | (perms.mode() & 0o07) << 3; perms.set_mode(new_perms); std::fs::set_permissions(to, perms)?; @@ -436,7 +436,10 @@ mod imp { } pub fn copy_metadata(_from: &path, _to: &Path) -> std::io::Result<()> { - // Not possible + let meta = std::fs::File::open(from)?.metadata()?; + let perms = meta.permissions(); + std::fs::set_permissions(to, perms)?; + Ok(()) } }