Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

fs: Remove redundant stat in RealFs metadata (#63190)

Hamish Clarkad51f6825c36
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index f128b7e5796..e775b85534c 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -27,7 +27,7 @@ use std::os::fd::{AsFd, AsRawFd};
use std::os::unix::ffi::OsStrExt;
#[cfg(unix)]
-use std::os::unix::fs::{FileTypeExt, MetadataExt};
+use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
use std::mem::MaybeUninit;
@@ -35,6 +35,7 @@ use std::mem::MaybeUninit;
use async_tar::Archive;
use futures::{AsyncRead, Stream, StreamExt, future::BoxFuture};
use git::repository::{GitRepository, RealGitRepository};
+#[cfg(windows)]
use is_executable::IsExecutable;
use rope::Rope;
use serde::{Deserialize, Serialize};
@@ -1101,7 +1102,12 @@ impl Fs for RealFs {
#[cfg(unix)]
let is_fifo = metadata.file_type().is_fifo();
+ #[cfg(unix)]
+ let is_executable = metadata.is_file() && metadata.permissions().mode() & 0o111 != 0;
+
+ #[cfg(windows)]
let path_buf = path.to_path_buf();
+ #[cfg(windows)]
let is_executable = self
.executor
.spawn(async move { path_buf.is_executable() })
diff --git a/crates/fs/tests/integration/fs_tests.rs b/crates/fs/tests/integration/fs_tests.rs
index 9b7fc979cdb..91dfce57bdb 100644
--- a/crates/fs/tests/integration/fs_tests.rs
+++ b/crates/fs/tests/integration/fs_tests.rs
@@ -666,6 +666,49 @@ async fn test_fake_fs_rename_onto_itself_keeps_the_file(executor: BackgroundExec
assert_eq!(fs.load(path).await.unwrap(), "content");
}
+#[gpui::test]
+#[cfg(unix)]
+async fn test_realfs_executable_metadata(executor: BackgroundExecutor) {
+ use std::os::unix::fs::PermissionsExt as _;
+
+ let tempdir = TempDir::new().unwrap();
+ let path = tempdir.path();
+ let non_executable_path = path.join("non-executable.sh");
+ let executable_path = path.join("executable.sh");
+ let symlink_path = path.join("executable-symlink.sh");
+
+ std::fs::write(&non_executable_path, "#!/bin/sh\n").unwrap();
+ std::fs::write(&executable_path, "#!/bin/sh\n").unwrap();
+ let mut permissions = std::fs::metadata(&executable_path).unwrap().permissions();
+ permissions.set_mode(0o755);
+ std::fs::set_permissions(&executable_path, permissions).unwrap();
+
+ let fs = RealFs::new(None, executor);
+ gpui::block_on(fs.create_symlink(&symlink_path, PathBuf::from("executable.sh"))).unwrap();
+
+ let non_executable_metadata = fs
+ .metadata(&non_executable_path)
+ .await
+ .expect("metadata call succeeds")
+ .expect("metadata returned");
+ assert!(!non_executable_metadata.is_executable);
+
+ let executable_metadata = fs
+ .metadata(&executable_path)
+ .await
+ .expect("metadata call succeeds")
+ .expect("metadata returned");
+ assert!(executable_metadata.is_executable);
+
+ let symlink_metadata = fs
+ .metadata(&symlink_path)
+ .await
+ .expect("metadata call succeeds")
+ .expect("metadata returned");
+ assert!(symlink_metadata.is_symlink);
+ assert!(symlink_metadata.is_executable);
+}
+
#[gpui::test]
#[cfg(unix)]
async fn test_realfs_broken_symlink_metadata(executor: BackgroundExecutor) {