Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
pub mod fs_watcher;
mod git_clone_progress;
use parking_lot::Mutex;
use slotmap::{KeyData, SlotMap};
use std::ffi::OsString;
use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
use std::time::Instant;
use util::maybe;
use anyhow::{Context as _, Result};
use futures::stream::iter;
use gpui::App;
use gpui::BackgroundExecutor;
use gpui::Global;
use gpui::ReadGlobal as _;
use gpui::SharedString;
#[cfg(unix)]
use std::ffi::CString;
use util::command::{Stdio, new_command};
#[cfg(unix)]
use std::os::fd::{AsFd, AsRawFd};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(unix)]
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
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};
use smol::io::AsyncWriteExt;
#[cfg(feature = "test-support")]
use std::path::Component;
use std::{
io::{self, Write},
path::{Path, PathBuf},
pin::Pin,
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use tempfile::TempDir;
use text::LineEnding;
#[cfg(feature = "test-support")]
mod fake_git_repo;
#[cfg(feature = "test-support")]
use collections::{BTreeMap, btree_map};
#[cfg(feature = "test-support")]
pub use fake_git_repo::FakeBlobReadGate;
#[cfg(feature = "test-support")]
use fake_git_repo::{FakeCommitDataEntry, FakeGitRepositoryState};
#[cfg(feature = "test-support")]
use git::{
repository::{CommitData, InitialGraphCommitData, RepoPath, Worktree, repo_path},
status::{FileStatus, StatusCode, TrackedStatus, UnmergedStatus},
};
#[cfg(feature = "test-support")]
use util::paths::normalize_path;
#[cfg(feature = "test-support")]
use smol::io::AsyncReadExt;
#[cfg(feature = "test-support")]
use std::ffi::OsStr;
pub trait Watcher: Send + Sync {
fn add(&self, path: &Path) -> Result<()>;
fn remove(&self, path: &Path) -> Result<()>;
}
pub trait ReadSeek: io::Read + io::Seek {}
impl<T: io::Read + io::Seek + ?Sized> ReadSeek for T {}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum PathEventKind {
Removed,
Created,
Changed,
Rescan,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct PathEvent {
pub path: PathBuf,
pub kind: Option<PathEventKind>,
}
impl From<PathEvent> for PathBuf {
fn from(event: PathEvent) -> Self {
event.path
}
}
#[async_trait::async_trait]
pub trait Fs: Send + Sync {
async fn create_dir(&self, path: &Path) -> Result<()>;
async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()>;
async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()>;
async fn create_file_with(
&self,
path: &Path,
content: Pin<&mut (dyn AsyncRead + Send)>,
) -> Result<()>;
async fn extract_tar_file(
&self,
path: &Path,
content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
) -> Result<()>;
async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()>;
async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()>;
/// Removes a directory from the filesystem.
/// There is no expectation that the directory will be preserved in the
/// system trash.
async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()>;
/// Moves a file or directory to the system trash.
/// Returns a [`TrashedEntry`] that can be used to keep track of the
/// location of the trashed item in the system's trash.
async fn trash(&self, path: &Path, options: RemoveOptions) -> Result<TrashId>;
/// Removes a file from the filesystem.
/// There is no expectation that the file will be preserved in the system
/// trash.
async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;
async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>>;
async fn open_sync(&self, path: &Path) -> Result<Box<dyn ReadSeek + Send + Sync>>;
async fn load(&self, path: &Path) -> Result<String> {
Ok(String::from_utf8(self.load_bytes(path).await?)?)
}
async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>>;
async fn atomic_write(&self, path: PathBuf, text: String) -> Result<()>;
async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()>;
async fn write(&self, path: &Path, content: &[u8]) -> Result<()>;
async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
async fn is_file(&self, path: &Path) -> bool;
async fn is_dir(&self, path: &Path) -> bool;
async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;
async fn read_link(&self, path: &Path) -> Result<PathBuf>;
async fn read_dir(
&self,
path: &Path,
) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>>;
/// Creates the native file watcher now rather than on the first `watch`, so a
/// failure to start it (e.g. inotify instance limits) can be reported at startup.
fn start_native_watcher(&self) -> Result<()> {
Ok(())
}
/// Records raw local watcher notifications until the returned recording is dropped.
fn record_watcher_diagnostics(&self) -> Option<fs_watcher::WatchRecording> {
None
}
/// Whether `path` exists, without following a final symlink. Synchronous
/// because watches are registered synchronously by the worktree scanner.
fn path_exists(&self, path: &Path) -> bool;
/// Whether the volume holding `path` compares file names case-sensitively.
fn is_path_case_sensitive(&self, path: &Path) -> bool;
/// Whether `path` sits on a filesystem where native file watching does not
/// deliver events (network mounts, some FUSE and WSL mounts), so it must be polled.
fn requires_poll_watcher(&self, path: &Path) -> bool;
async fn watch(
&self,
path: &Path,
latency: Duration,
) -> (
Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
Arc<dyn Watcher>,
);
fn open_repo(
&self,
abs_dot_git: &Path,
system_git_binary_path: Option<&Path>,
) -> Result<Arc<dyn GitRepository>>;
async fn git_init(&self, abs_work_directory: &Path, fallback_branch_name: String)
-> Result<()>;
async fn git_clone(&self, abs_work_directory: &Path, repo_url: &str) -> Result<()>;
async fn git_config(&self, abs_work_directory: &Path, args: Vec<String>) -> Result<String>;
fn is_fake(&self) -> bool;
async fn is_case_sensitive(&self) -> bool;
fn subscribe_to_jobs(&self) -> JobEventReceiver;
/// Returns the original absolute path of the item identified by `trash_id`.
fn original_path_for_trash_id(&self, trash_id: TrashId) -> Option<PathBuf>;
/// Restores the item identified by `trash_id`, moving it from the system's
/// trash back to its original path.
async fn restore(&self, trash_id: TrashId) -> std::result::Result<PathBuf, TrashRestoreError>;
#[cfg(feature = "test-support")]
fn as_fake(&self) -> Arc<FakeFs> {
panic!("called as_fake on a real fs");
}
}
// We use our own type rather than `trash::TrashItem` directly to avoid carrying
// over fields we don't need (e.g. `time_deleted`) and to insulate callers and
// tests from changes to that crate's API surface.
/// Represents a file or directory that has been moved to the system trash,
/// retaining enough information to restore it to its original location.
#[derive(Clone, PartialEq, Debug)]
struct TrashedEntry {
/// Platform-specific identifier for the file/directory in the trash.
///
/// * Freedesktop – Path to the `.trashinfo` file.
/// * macOS & Windows – Full path to the file/directory in the system's
/// trash.
pub id: OsString,
/// Name of the file/directory at the time of trashing, including extension.
pub name: OsString,
/// Absolute path to the parent directory at the time of trashing.
pub original_parent: PathBuf,
}
impl From<trash::TrashItem> for TrashedEntry {
fn from(item: trash::TrashItem) -> Self {
Self {
id: item.id,
name: item.name,
original_parent: item.original_parent,
}
}
}
impl TrashedEntry {
fn into_trash_item(self) -> trash::TrashItem {
trash::TrashItem {
id: self.id,
name: self.name,
original_parent: self.original_parent,
// `TrashedEntry` doesn't preserve `time_deleted` as we don't
// currently need it for restore, so we default it to 0 here.
time_deleted: 0,
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum TrashRestoreError {
#[error("The specified `path` ({}) was not found in the system's trash.", path.display())]
NotFound { path: PathBuf },
#[error("File or directory ({}) already exists at the restore destination.", path.display())]
Collision { path: PathBuf },
// This should never occur, the only way to get a TrashId is to undo
// consumes the Change::Trashed. We worry about remoting duplicate messages
// we do not want to crash the app then which is why this error is there.
#[error("The item was already restored")]
AlreadyRestored,
#[error("Unknown error ({description})")]
Unknown { description: String },
}
impl From<trash::Error> for TrashRestoreError {
fn from(err: trash::Error) -> Self {
match err {
trash::Error::RestoreCollision { path, .. } => Self::Collision { path },
trash::Error::Unknown { description } => Self::Unknown { description },
other => Self::Unknown {
description: other.to_string(),
},
}
}
}
struct GlobalFs(Arc<dyn Fs>);
impl Global for GlobalFs {}
impl dyn Fs {
/// Returns the global [`Fs`].
pub fn global(cx: &App) -> Arc<Self> {
GlobalFs::global(cx).0.clone()
}
/// Sets the global [`Fs`].
pub fn set_global(fs: Arc<Self>, cx: &mut App) {
cx.set_global(GlobalFs(fs));
}
}
#[derive(Copy, Clone, Default)]
pub struct CreateOptions {
pub overwrite: bool,
pub ignore_if_exists: bool,
}
#[derive(Copy, Clone, Default)]
pub struct CopyOptions {
pub overwrite: bool,
pub ignore_if_exists: bool,
}
#[derive(Copy, Clone, Default)]
pub struct RenameOptions {
pub overwrite: bool,
pub ignore_if_exists: bool,
/// Whether to create parent directories if they do not exist.
pub create_parents: bool,
}
#[derive(Copy, Clone, Default)]
pub struct RemoveOptions {
pub recursive: bool,
pub ignore_if_not_exists: bool,
}
#[derive(Copy, Clone, Debug)]
pub struct Metadata {
pub inode: u64,
pub mtime: MTime,
pub is_symlink: bool,
pub is_dir: bool,
pub len: u64,
pub is_fifo: bool,
pub is_executable: bool,
pub is_writable: bool,
}
/// Filesystem modification time. The purpose of this newtype is to discourage use of operations
/// that do not make sense for mtimes. In particular, it is not always valid to compare mtimes using
/// `<` or `>`, as there are many things that can cause the mtime of a file to be earlier than it
/// was. See ["mtime comparison considered harmful" - apenwarr](https://apenwarr.ca/log/20181113).
///
/// Do not derive Ord, PartialOrd, or arithmetic operation traits.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(transparent)]
pub struct MTime(SystemTime);
pub type JobId = usize;
#[derive(Clone, Debug)]
pub struct JobInfo {
pub start: Instant,
pub message: SharedString,
pub id: JobId,
}
#[derive(Debug, Clone)]
pub enum JobEvent {
Started { info: JobInfo },
Updated { id: JobId, message: SharedString },
Completed { id: JobId },
}
pub type JobEventSender = futures::channel::mpsc::UnboundedSender<JobEvent>;
pub type JobEventReceiver = futures::channel::mpsc::UnboundedReceiver<JobEvent>;
struct JobTracker {
id: JobId,
subscribers: Arc<Mutex<Vec<JobEventSender>>>,
}
impl JobTracker {
fn new(info: JobInfo, subscribers: Arc<Mutex<Vec<JobEventSender>>>) -> Self {
let id = info.id;
{
let mut subs = subscribers.lock();
subs.retain(|sender| {
sender
.unbounded_send(JobEvent::Started { info: info.clone() })
.is_ok()
});
}
Self { id, subscribers }
}
fn update(&self, message: SharedString) {
let mut subscribers = self.subscribers.lock();
subscribers.retain(|sender| {
sender
.unbounded_send(JobEvent::Updated {
id: self.id,
message: message.clone(),
})
.is_ok()
});
}
}
impl Drop for JobTracker {
fn drop(&mut self) {
let mut subs = self.subscribers.lock();
subs.retain(|sender| {
sender
.unbounded_send(JobEvent::Completed { id: self.id })
.is_ok()
});
}
}
impl MTime {
/// Conversion intended for persistence and testing.
pub fn from_seconds_and_nanos(secs: u64, nanos: u32) -> Self {
MTime(UNIX_EPOCH + Duration::new(secs, nanos))
}
/// Conversion intended for persistence.
pub fn to_seconds_and_nanos_for_persistence(self) -> Option<(u64, u32)> {
self.0
.duration_since(UNIX_EPOCH)
.ok()
.map(|duration| (duration.as_secs(), duration.subsec_nanos()))
}
/// Returns the value wrapped by this `MTime`, for presentation to the user. The name including
/// "_for_user" is to discourage misuse - this method should not be used when making decisions
/// about file dirtiness.
pub fn timestamp_for_user(self) -> SystemTime {
self.0
}
/// Temporary method to split out the behavior changes from introduction of this newtype.
pub fn bad_is_greater_than(self, other: MTime) -> bool {
self.0 > other.0
}
}
impl From<proto::Timestamp> for MTime {
fn from(timestamp: proto::Timestamp) -> Self {
MTime(timestamp.into())
}
}
impl From<MTime> for proto::Timestamp {
fn from(mtime: MTime) -> Self {
mtime.0.into()
}
}
slotmap::new_key_type! { pub struct TrashId; }
impl TrashId {
pub fn from_proto(value: u64) -> Self {
KeyData::from_ffi(value).into()
}
pub fn to_proto(self) -> u64 {
self.0.as_ffi()
}
}
pub struct RealFs {
this: std::sync::Weak<Self>,
bundled_git_binary_path: Option<PathBuf>,
executor: BackgroundExecutor,
native_watcher: Arc<fs_watcher::OsWatcher>,
poll_watcher: Arc<fs_watcher::OsWatcher>,
next_job_id: Arc<AtomicUsize>,
job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,
trash: Arc<Mutex<SlotMap<TrashId, TrashedEntry>>>,
is_case_sensitive: AtomicU8,
}
pub trait FileHandle: Send + Sync + std::fmt::Debug {
fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf>;
}
impl FileHandle for std::fs::File {
#[cfg(target_os = "macos")]
fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
use std::{
ffi::{CStr, OsStr},
os::unix::ffi::OsStrExt,
};
let fd = self.as_fd();
let mut path_buf = MaybeUninit::<[u8; libc::PATH_MAX as usize]>::uninit();
let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETPATH, path_buf.as_mut_ptr()) };
anyhow::ensure!(result != -1, "fcntl returned -1");
// SAFETY: `fcntl` will initialize the path buffer.
let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr().cast()) };
anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");
let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
Ok(path)
}
#[cfg(target_os = "linux")]
fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
let fd = self.as_fd();
let fd_path = format!("/proc/self/fd/{}", fd.as_raw_fd());
let new_path = std::fs::read_link(fd_path)?;
if new_path
.file_name()
.is_some_and(|f| f.to_string_lossy().ends_with(" (deleted)"))
{
anyhow::bail!("file was deleted")
};
Ok(new_path)
}
#[cfg(target_os = "freebsd")]
fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
use std::{
ffi::{CStr, OsStr},
os::unix::ffi::OsStrExt,
};
let fd = self.as_fd();
let mut kif = MaybeUninit::<libc::kinfo_file>::uninit();
kif.kf_structsize = libc::KINFO_FILE_SIZE;
let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_KINFO, kif.as_mut_ptr()) };
anyhow::ensure!(result != -1, "fcntl returned -1");
// SAFETY: `fcntl` will initialize the kif.
let c_str = unsafe { CStr::from_ptr(kif.assume_init().kf_path.as_ptr()) };
anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");
let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
Ok(path)
}
#[cfg(target_os = "windows")]
fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::os::windows::io::AsRawHandle;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::Storage::FileSystem::{
FILE_NAME_NORMALIZED, GetFinalPathNameByHandleW,
};
let handle = HANDLE(self.as_raw_handle() as _);
// Query required buffer size (in wide chars)
let required_len =
unsafe { GetFinalPathNameByHandleW(handle, &mut [], FILE_NAME_NORMALIZED) };
anyhow::ensure!(
required_len != 0,
"GetFinalPathNameByHandleW returned 0 length"
);
// Allocate buffer and retrieve the path
let mut buf: Vec<u16> = vec![0u16; required_len as usize + 1];
let written = unsafe { GetFinalPathNameByHandleW(handle, &mut buf, FILE_NAME_NORMALIZED) };
anyhow::ensure!(
written != 0,
"GetFinalPathNameByHandleW failed to write path"
);
let os_str: OsString = OsString::from_wide(&buf[..written as usize]);
anyhow::ensure!(!os_str.is_empty(), "Could find a path for the file handle");
Ok(PathBuf::from(os_str))
}
}
pub struct RealWatcher {}
impl RealFs {
pub fn new(git_binary_path: Option<PathBuf>, executor: BackgroundExecutor) -> Arc<Self> {
Arc::new_cyclic(|this| Self {
this: this.clone(),
bundled_git_binary_path: git_binary_path,
native_watcher: fs_watcher::OsWatcher::new(
fs_watcher::OsWatcherKind::Native,
executor.clone(),
),
poll_watcher: fs_watcher::OsWatcher::new(
fs_watcher::OsWatcherKind::Poll,
executor.clone(),
),
executor,
next_job_id: Arc::new(AtomicUsize::new(0)),
job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
trash: Arc::new(Mutex::new(SlotMap::with_key())),
is_case_sensitive: Default::default(),
})
}
#[cfg(target_os = "windows")]
fn canonicalize(path: &Path) -> Result<PathBuf> {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use windows::Win32::Storage::FileSystem::GetVolumePathNameW;
use windows::core::HSTRING;
// std::fs::canonicalize resolves mapped network paths to UNC paths, which can
// confuse some software. To mitigate this, we canonicalize the input, then rebase
// the result onto the input's original volume root if both paths are on the same
// volume. This keeps the same drive letter or mount point the caller used.
let abs_path = if path.is_relative() {
std::env::current_dir()?.join(path)
} else {
path.to_path_buf()
};
let path_hstring = HSTRING::from(abs_path.as_os_str());
let mut vol_buf = vec![0u16; abs_path.as_os_str().len() + 2];
unsafe { GetVolumePathNameW(&path_hstring, &mut vol_buf)? };
let volume_root = {
let len = vol_buf
.iter()
.position(|&c| c == 0)
.unwrap_or(vol_buf.len());
PathBuf::from(OsString::from_wide(&vol_buf[..len]))
};
let resolved_path = dunce::canonicalize(&abs_path)?;
let resolved_root = dunce::canonicalize(&volume_root)?;
if let Ok(relative) = resolved_path.strip_prefix(&resolved_root) {
let mut result = volume_root;
result.push(relative);
Ok(result)
} else {
Ok(resolved_path)
}
}
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn rename_without_replace(source: &Path, target: &Path) -> io::Result<()> {
let source = path_to_c_string(source)?;
let target = path_to_c_string(target)?;
#[cfg(target_os = "macos")]
let result = unsafe { libc::renamex_np(source.as_ptr(), target.as_ptr(), libc::RENAME_EXCL) };
#[cfg(target_os = "linux")]
let result = unsafe {
libc::syscall(
libc::SYS_renameat2,
libc::AT_FDCWD,
source.as_ptr(),
libc::AT_FDCWD,
target.as_ptr(),
libc::RENAME_NOREPLACE,
)
};
if result == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}
#[cfg(target_os = "windows")]
fn rename_without_replace(source: &Path, target: &Path) -> io::Result<()> {
use std::os::windows::ffi::OsStrExt;
use windows::Win32::Storage::FileSystem::{MOVE_FILE_FLAGS, MoveFileExW};
use windows::core::PCWSTR;
let source: Vec<u16> = source.as_os_str().encode_wide().chain(Some(0)).collect();
let target: Vec<u16> = target.as_os_str().encode_wide().chain(Some(0)).collect();
unsafe {
MoveFileExW(
PCWSTR(source.as_ptr()),
PCWSTR(target.as_ptr()),
MOVE_FILE_FLAGS::default(),
)
}
.map_err(|_| io::Error::last_os_error())
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn path_to_c_string(path: &Path) -> io::Result<CString> {
CString::new(path.as_os_str().as_bytes()).map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!("path contains interior NUL: {}", path.display()),
)
})
}
// On Unix targets, std::fs::ReadDir panics in its Drop implementation
// when an unexpected error is returned from closedir(2). We hit this
// condition in production; one cause seems to be macOS's FSEventStream
// incorrectly closing fds it doesn't own, resulting in closedir returning
// EBADF, see https://github.com/zed-industries/zed/issues/59952#issuecomment-5080178879.
//
// We also see occasional errors like ENXIO and ETIMEDOUT that seem to
// come from network or other exotic filesystems.
//
// To avoid crashing the app in this situation, we use the rustix analogue of
// ReadDir, which doesn't have this panic in drop.
#[cfg(unix)]
fn read_dir_entries(path: PathBuf) -> Result<impl Send + Iterator<Item = Result<PathBuf>>> {
use rustix::fs::{Dir, Mode, OFlags};
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
let directory_fd = rustix::fs::open(
&path,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)
.with_context(|| format!("failed to open directory {path:?}"))?;
let directory =
Dir::new(directory_fd).with_context(|| format!("failed to read directory {path:?}"))?;
Ok(directory.filter_map(move |entry| {
let entry = match entry {
Ok(entry) => entry,
Err(error) => {
return Some(Err(anyhow::Error::new(error)
.context(format!("failed to read directory entry in {path:?}"))));
}
};
let name = entry.file_name().to_bytes();
if name == b"." || name == b".." {
return None;
}
Some(Ok(path.join(OsStr::from_bytes(name))))
}))
}
#[cfg(not(unix))]
fn read_dir_entries(path: PathBuf) -> Result<impl Send + Iterator<Item = Result<PathBuf>>> {
let entries =
std::fs::read_dir(&path).with_context(|| format!("failed to open directory {path:?}"))?;
Ok(entries.map(move |entry| {
entry
.map(|entry| entry.path())
.with_context(|| format!("failed to read directory entry in {path:?}"))
}))
}
#[async_trait::async_trait]
impl Fs for RealFs {
async fn create_dir(&self, path: &Path) -> Result<()> {
Ok(smol::fs::create_dir_all(path).await?)
}
async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()> {
#[cfg(unix)]
smol::fs::unix::symlink(target, path).await?;
#[cfg(windows)]
if smol::fs::metadata(&target).await?.is_dir() {
let status = new_command("cmd")
.args(["/C", "mklink", "/J"])
.args([path, target.as_path()])
.status()
.await?;
if !status.success() {
return Err(anyhow::anyhow!(
"Failed to create junction from {:?} to {:?}",
path,
target
));
}
} else {
smol::fs::windows::symlink_file(target, path).await?
}
Ok(())
}
async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
let mut open_options = smol::fs::OpenOptions::new();
open_options.write(true).create(true);
if options.overwrite {
open_options.truncate(true);
} else if !options.ignore_if_exists {
open_options.create_new(true);
}
open_options
.open(path)
.await
.with_context(|| format!("Failed to create file at {:?}", path))?;
Ok(())
}
async fn create_file_with(
&self,
path: &Path,
content: Pin<&mut (dyn AsyncRead + Send)>,
) -> Result<()> {
let mut file = smol::fs::File::create(&path)
.await
.with_context(|| format!("Failed to create file at {:?}", path))?;
futures::io::copy(content, &mut file).await?;
Ok(())
}
async fn extract_tar_file(
&self,
path: &Path,
content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
) -> Result<()> {
content.unpack(path).await?;
Ok(())
}
async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
if options.ignore_if_exists {
return Ok(());
} else {
anyhow::bail!("{target:?} already exists");
}
}
smol::fs::copy(source, target).await?;
Ok(())
}
async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()> {
if options.create_parents {
if let Some(parent) = target.parent() {
self.create_dir(parent).await?;
}
}
if options.overwrite {
smol::fs::rename(source, target).await?;
return Ok(());
}
let use_metadata_fallback = {
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
{
let source = source.to_path_buf();
let target = target.to_path_buf();
match self
.executor
.spawn(async move { rename_without_replace(&source, &target) })
.await
{
Ok(()) => return Ok(()),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
if options.ignore_if_exists {
return Ok(());
}
return Err(error.into());
}
Err(error)
if error.raw_os_error().is_some_and(|code| {
code == libc::ENOSYS
|| code == libc::ENOTSUP
|| code == libc::EOPNOTSUPP
|| code == libc::EINVAL
}) =>
{
// For case when filesystem or kernel does not support atomic no-overwrite rename.
// EINVAL is returned by FUSE-based filesystems (e.g. NTFS via ntfs-3g)
// that don't support RENAME_NOREPLACE.
true
}
Err(error) => return Err(error.into()),
}
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
// For platforms which do not have an atomic no-overwrite rename yet.
true
}
};
if use_metadata_fallback && smol::fs::metadata(target).await.is_ok() {
if options.ignore_if_exists {
return Ok(());
} else {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("{target:?} already exists"),
)
.into());
}
}
smol::fs::rename(source, target).await?;
Ok(())
}
async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
let result = if options.recursive {
smol::fs::remove_dir_all(path).await
} else {
smol::fs::remove_dir(path).await
};
match result {
Ok(()) => Ok(()),
Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
Ok(())
}
Err(err) => Err(err)?,
}
}
async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
#[cfg(windows)]
if let Ok(Some(metadata)) = self.metadata(path).await
&& metadata.is_symlink
&& metadata.is_dir
{
self.remove_dir(
path,
RemoveOptions {
recursive: false,
ignore_if_not_exists: true,
},
)
.await?;
return Ok(());
}
match smol::fs::remove_file(path).await {
Ok(()) => Ok(()),
Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
Ok(())
}
Err(err) => Err(err)?,
}
}
async fn trash(&self, path: &Path, _options: RemoveOptions) -> Result<TrashId> {
// We must make the path absolute or trash will make a weird abomination
// of the zed working directory (not usually the worktree) and whatever
// the path variable holds.
// We deliberately use `std::path::absolute` instead of `canonicalize`
// to avoid resolving symlinks. Otherwise trashing a symlink would trash
// its target and leave the link behind.
let path = std::path::absolute(path).context("Could not make the path absolute")?;
let entry = smol::unblock(move || trash::delete_with_info(path))
.await
.context("Could not trash file or dir")?
.into();
Ok(self.trash.lock().insert(entry))
}
async fn open_sync(&self, path: &Path) -> Result<Box<dyn ReadSeek + Send + Sync>> {
Ok(Box::new(std::fs::File::open(path)?))
}
async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>> {
let mut options = std::fs::OpenOptions::new();
options.read(true);
#[cfg(windows)]
{
use std::os::windows::fs::OpenOptionsExt;
options.custom_flags(windows::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS.0);
}
Ok(Arc::new(options.open(path)?))
}
async fn load(&self, path: &Path) -> Result<String> {
let path = path.to_path_buf();
self.executor
.spawn(async move {
std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read file {}", path.display()))
})
.await
}
async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>> {
let path = path.to_path_buf();
let bytes = self
.executor
.spawn(async move { std::fs::read(path) })
.await?;
Ok(bytes)
}
#[cfg(not(target_os = "windows"))]
async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
smol::unblock(move || {
// Use the directory of the destination as temp dir to avoid
// invalid cross-device link error, and XDG_CACHE_DIR for fallback.
// See https://github.com/zed-industries/zed/pull/8437 for more details.
let mut tmp_file =
tempfile::NamedTempFile::new_in(path.parent().unwrap_or(paths::temp_dir()))?;
tmp_file.write_all(data.as_bytes())?;
tmp_file.persist(path)?;
anyhow::Ok(())
})
.await?;
Ok(())
}
#[cfg(target_os = "windows")]
async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
smol::unblock(move || {
// If temp dir is set to a different drive than the destination,
// we receive error:
//
// failed to persist temporary file:
// The system cannot move the file to a different disk drive. (os error 17)
//
// This is because `ReplaceFileW` does not support cross volume moves.
// See the remark section: "The backup file, replaced file, and replacement file must all reside on the same volume."
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-replacefilew#remarks
//
// So we use the directory of the destination as a temp dir to avoid it.
// https://github.com/zed-industries/zed/issues/16571
let temp_dir = TempDir::new_in(path.parent().unwrap_or(paths::temp_dir()))?;
let temp_file = {
let temp_file_path = temp_dir.path().join("temp_file");
let mut file = std::fs::File::create_new(&temp_file_path)?;
file.write_all(data.as_bytes())?;
temp_file_path
};
atomic_replace(path.as_path(), temp_file.as_path())?;
anyhow::Ok(())
})
.await?;
Ok(())
}
async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
let buffer_size = text.summary().len.min(10 * 1024);
if let Some(path) = path.parent() {
self.create_dir(path)
.await
.with_context(|| format!("Failed to create directory at {:?}", path))?;
}
let file = smol::fs::File::create(path)
.await
.with_context(|| format!("Failed to create file at {:?}", path))?;
let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
for chunk in text::chunks_with_line_ending(text, line_ending) {
writer.write_all(chunk.as_bytes()).await?;
}
writer.flush().await?;
Ok(())
}
async fn write(&self, path: &Path, content: &[u8]) -> Result<()> {
if let Some(path) = path.parent() {
self.create_dir(path)
.await
.with_context(|| format!("Failed to create directory at {:?}", path))?;
}
let path = path.to_owned();
let contents = content.to_owned();
self.executor
.spawn(async move {
std::fs::write(path, contents)?;
Ok(())
})
.await
}
async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
let path = path.to_owned();
self.executor
.spawn(async move {
#[cfg(target_os = "windows")]
let result = Self::canonicalize(&path);
#[cfg(not(target_os = "windows"))]
let result = std::fs::canonicalize(&path);
result.with_context(|| format!("canonicalizing {path:?}"))
})
.await
}
async fn is_file(&self, path: &Path) -> bool {
let path = path.to_owned();
self.executor
.spawn(async move { std::fs::metadata(path).is_ok_and(|metadata| metadata.is_file()) })
.await
}
async fn is_dir(&self, path: &Path) -> bool {
let path = path.to_owned();
self.executor
.spawn(async move { std::fs::metadata(path).is_ok_and(|metadata| metadata.is_dir()) })
.await
}
async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
let path_buf = path.to_owned();
let symlink_metadata = match self
.executor
.spawn(async move { std::fs::symlink_metadata(&path_buf) })
.await
{
Ok(metadata) => metadata,
Err(err) => {
return match err.kind() {
io::ErrorKind::NotFound | io::ErrorKind::NotADirectory => Ok(None),
_ => Err(anyhow::Error::new(err)),
};
}
};
let is_symlink = symlink_metadata.file_type().is_symlink();
let metadata = if is_symlink {
let path_buf = path.to_path_buf();
// Read target metadata, if the target exists
match self
.executor
.spawn(async move { std::fs::metadata(path_buf) })
.await
{
Ok(target_metadata) => target_metadata,
Err(err) => {
if err.kind() != io::ErrorKind::NotFound {
// TODO: Also FilesystemLoop when that's stable
log::warn!(
"Failed to read symlink target metadata for path {path:?}: {err}"
);
}
// For a broken or recursive symlink, return the symlink metadata. (Or
// as edge cases, a symlink into a directory we can't read, which is hard
// to distinguish from just being broken.)
symlink_metadata
}
}
} else {
symlink_metadata
};
#[cfg(unix)]
let inode = metadata.ino();
#[cfg(windows)]
let inode = file_id(path).await?;
#[cfg(windows)]
let is_fifo = false;
#[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() })
.await;
Ok(Some(Metadata {
inode,
mtime: MTime(metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH)),
len: metadata.len(),
is_symlink,
is_dir: metadata.file_type().is_dir(),
is_fifo,
is_executable,
is_writable: !metadata.permissions().readonly(),
}))
}
async fn read_link(&self, path: &Path) -> Result<PathBuf> {
let path = path.to_owned();
let path = self
.executor
.spawn(async move { std::fs::read_link(&path) })
.await?;
Ok(path)
}
async fn read_dir(
&self,
path: &Path,
) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
let path = path.to_owned();
let entries = self
.executor
.spawn(async move { read_dir_entries(path) })
.await?;
Ok(Box::pin(iter(entries)))
}
fn start_native_watcher(&self) -> Result<()> {
self.native_watcher.ensure_backend()
}
fn record_watcher_diagnostics(&self) -> Option<fs_watcher::WatchRecording> {
Some(fs_watcher::WatchRecording::new([
self.native_watcher.clone(),
self.poll_watcher.clone(),
]))
}
fn path_exists(&self, path: &Path) -> bool {
std::fs::symlink_metadata(path).is_ok()
}
fn is_path_case_sensitive(&self, path: &Path) -> bool {
!fs_watcher::case_insensitive_path(path)
}
fn requires_poll_watcher(&self, path: &Path) -> bool {
fs_watcher::requires_poll_watcher(path)
}
async fn watch(
&self,
path: &Path,
latency: Duration,
) -> (
Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
Arc<dyn Watcher>,
) {
let this = self
.this
.upgrade()
.expect("RealFs is only constructed inside an Arc");
fs_watcher::watch(
this,
self.native_watcher.clone(),
self.poll_watcher.clone(),
self.executor.clone(),
path,
latency,
)
.await
}
fn open_repo(
&self,
dotgit_path: &Path,
system_git_binary_path: Option<&Path>,
) -> Result<Arc<dyn GitRepository>> {
Ok(Arc::new(RealGitRepository::new(
dotgit_path,
self.bundled_git_binary_path.clone(),
system_git_binary_path.map(|path| path.to_path_buf()),
self.executor.clone(),
)?))
}
async fn git_init(
&self,
abs_work_directory_path: &Path,
fallback_branch_name: String,
) -> Result<()> {
let result = new_command("git")
.current_dir(abs_work_directory_path)
.args(&["config", "--global", "--get", "init.defaultBranch"])
.output()
.await;
// In case the `git config` command fails, which would be the case if
// the user doesn't have an `init.defaultBranch` value set, we'll just
// default to the provided `fallback_branch_name`.
let branch_name = match result {
Ok(output) if !output.stdout.is_empty() => String::from_utf8(output.stdout)?,
_ => fallback_branch_name,
};
new_command("git")
.current_dir(abs_work_directory_path)
.args(&["init", "-b"])
.arg(branch_name.trim())
.output()
.await?;
Ok(())
}
async fn git_clone(&self, abs_work_directory: &Path, repo_url: &str) -> Result<()> {
let job_id = self.next_job_id.fetch_add(1, Ordering::SeqCst);
let job_info = JobInfo {
id: job_id,
start: Instant::now(),
message: SharedString::from(format!("Cloning {}", repo_url)),
};
let job_tracker = JobTracker::new(job_info, self.job_event_subscribers.clone());
let mut child = new_command("git")
.current_dir(abs_work_directory)
.args(["clone", "--progress", repo_url])
.stdout(Stdio::null())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()?;
let stderr = child
.stderr
.take()
.context("failed to read git clone progress")?;
let stderr_output = git_clone_progress::read(stderr, |message| {
job_tracker.update(message.into());
})
.await?;
let status = child.status().await?;
if !status.success() {
anyhow::bail!(
"git clone failed: {}",
git_clone_progress::failure_message(&stderr_output)
);
}
Ok(())
}
/// Runs `git config` with the given arguments.
/// Will return `Ok` if the commands exit status is `0`, with the stdout
/// contents. Otherwise returns `Err` with the stderr contents.
async fn git_config(&self, abs_work_directory: &Path, args: Vec<String>) -> Result<String> {
let output = new_command("git")
.current_dir(abs_work_directory)
.args([String::from("config")].into_iter().chain(args))
.output()
.await?;
if !output.status.success() {
let err = String::from_utf8(output.stderr)?;
anyhow::bail!(err);
}
String::from_utf8(output.stdout).map_err(Into::into)
}
fn is_fake(&self) -> bool {
false
}
fn subscribe_to_jobs(&self) -> JobEventReceiver {
let (sender, receiver) = futures::channel::mpsc::unbounded();
self.job_event_subscribers.lock().push(sender);
receiver
}
/// Checks whether the file system is case sensitive by attempting to create two files
/// that have the same name except for the casing.
///
/// It creates both files in a temporary directory it removes at the end.
async fn is_case_sensitive(&self) -> bool {
const UNINITIALIZED: u8 = 0;
const CASE_SENSITIVE: u8 = 1;
const NOT_CASE_SENSITIVE: u8 = 2;
// Note we could CAS here, but really, if we race we do this work twice at worst which isn't a big deal.
let load = self.is_case_sensitive.load(Ordering::Acquire);
if load != UNINITIALIZED {
return load == CASE_SENSITIVE;
}
let temp_dir = self.executor.spawn(async { TempDir::new() });
let res = maybe!(async {
let temp_dir = temp_dir.await?;
let test_file_1 = temp_dir.path().join("case_sensitivity_test.tmp");
let test_file_2 = temp_dir.path().join("CASE_SENSITIVITY_TEST.TMP");
let create_opts = CreateOptions {
overwrite: false,
ignore_if_exists: false,
};
// Create file1
self.create_file(&test_file_1, create_opts).await?;
// Now check whether it's possible to create file2
let case_sensitive = match self.create_file(&test_file_2, create_opts).await {
Ok(_) => Ok(true),
Err(e) => {
if let Some(io_error) = e.downcast_ref::<io::Error>() {
if io_error.kind() == io::ErrorKind::AlreadyExists {
Ok(false)
} else {
Err(e)
}
} else {
Err(e)
}
}
};
temp_dir.close()?;
case_sensitive
}).await.unwrap_or_else(|e| {
log::error!(
"Failed to determine whether filesystem is case sensitive (falling back to true) due to error: {e:#}"
);
true
});
self.is_case_sensitive.store(
if res {
CASE_SENSITIVE
} else {
NOT_CASE_SENSITIVE
},
Ordering::Release,
);
res
}
fn original_path_for_trash_id(&self, trash_id: TrashId) -> Option<PathBuf> {
self.trash
.lock()
.get(trash_id)
.map(|entry| entry.original_parent.join(&entry.name))
}
async fn restore(&self, trash_id: TrashId) -> std::result::Result<PathBuf, TrashRestoreError> {
let trashed_entry = self
.trash
.lock()
.get(trash_id)
.cloned()
.ok_or(TrashRestoreError::AlreadyRestored)?;
let restored_item_path = trashed_entry.original_parent.join(&trashed_entry.name);
let (tx, rx) = futures::channel::oneshot::channel();
std::thread::Builder::new()
.name("restore trashed item".to_string())
.spawn(move || {
let res = trash::restore_all([trashed_entry.into_trash_item()]);
tx.send(res)
})
.expect("The OS can spawn a threads");
rx.await.expect("Restore all never panics")?;
self.trash.lock().remove(trash_id);
Ok(restored_item_path)
}
}
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
impl Watcher for RealWatcher {
fn add(&self, _: &Path) -> Result<()> {
Ok(())
}
fn remove(&self, _: &Path) -> Result<()> {
Ok(())
}
}
#[cfg(feature = "test-support")]
pub struct FakeFs {
this: std::sync::Weak<Self>,
// Use an unfair lock to ensure tests are deterministic.
state: Arc<Mutex<FakeFsState>>,
executor: gpui::BackgroundExecutor,
native_watcher: Arc<fs_watcher::OsWatcher>,
poll_watcher: Arc<fs_watcher::OsWatcher>,
}
#[cfg(feature = "test-support")]
struct FakeFsState {
root: FakeFsEntry,
next_inode: u64,
next_mtime: SystemTime,
git_event_tx: async_channel::Sender<PathBuf>,
watch_roots: Vec<(PathBuf, std::sync::Weak<dyn Watcher>)>,
watches: FakeWatches,
events_paused: bool,
buffered_events: Vec<PathEvent>,
metadata_call_count: usize,
read_dir_call_count: usize,
read_dir_hooks: BTreeMap<PathBuf, BoxFuture<'static, ()>>,
path_write_counts: std::collections::HashMap<PathBuf, usize>,
job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,
trash: Mutex<SlotMap<TrashId, (TrashedEntry, FakeFsEntry)>>,
remove_dir_errors: std::collections::HashMap<PathBuf, String>,
case_sensitive: bool,
}
/// The kernel's side of file watching, as far as the real watcher code above
/// notify can tell: the paths the backend has registered and the callback that
/// delivers events for them into the native `OsWatcher`.
#[cfg(feature = "test-support")]
#[derive(Default)]
struct FakeWatches {
registered_paths: Vec<PathBuf>,
watch_calls: Vec<PathBuf>,
event_sink: Option<Box<dyn Fn(notify::Result<notify::Event>) + Send + Sync>>,
/// A file to create, without an event, when the given path is next watched.
file_to_create_on_watch: Option<(PathBuf, PathBuf)>,
}
#[cfg(feature = "test-support")]
#[derive(Clone, Debug)]
enum FakeFsEntry {
File {
inode: u64,
mtime: MTime,
len: u64,
content: Vec<u8>,
// The path to the repository state directory, if this is a gitfile.
git_dir_path: Option<PathBuf>,
},
Dir {
inode: u64,
mtime: MTime,
len: u64,
entries: BTreeMap<String, FakeFsEntry>,
git_repo_state: Option<Arc<Mutex<FakeGitRepositoryState>>>,
},
Symlink {
target: PathBuf,
},
}
#[cfg(feature = "test-support")]
impl PartialEq for FakeFsEntry {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Self::File {
inode: l_inode,
mtime: l_mtime,
len: l_len,
content: l_content,
git_dir_path: l_git_dir_path,
},
Self::File {
inode: r_inode,
mtime: r_mtime,
len: r_len,
content: r_content,
git_dir_path: r_git_dir_path,
},
) => {
l_inode == r_inode
&& l_mtime == r_mtime
&& l_len == r_len
&& l_content == r_content
&& l_git_dir_path == r_git_dir_path
}
(
Self::Dir {
inode: l_inode,
mtime: l_mtime,
len: l_len,
entries: l_entries,
git_repo_state: l_git_repo_state,
},
Self::Dir {
inode: r_inode,
mtime: r_mtime,
len: r_len,
entries: r_entries,
git_repo_state: r_git_repo_state,
},
) => {
let same_repo_state = match (l_git_repo_state.as_ref(), r_git_repo_state.as_ref()) {
(Some(l), Some(r)) => Arc::ptr_eq(l, r),
(None, None) => true,
_ => false,
};
l_inode == r_inode
&& l_mtime == r_mtime
&& l_len == r_len
&& l_entries == r_entries
&& same_repo_state
}
(Self::Symlink { target: l_target }, Self::Symlink { target: r_target }) => {
l_target == r_target
}
_ => false,
}
}
}
#[cfg(feature = "test-support")]
impl FakeFsState {
fn get_and_increment_mtime(&mut self) -> MTime {
let mtime = self.next_mtime;
self.next_mtime += FakeFs::SYSTEMTIME_INTERVAL;
MTime(mtime)
}
fn get_and_increment_inode(&mut self) -> u64 {
let inode = self.next_inode;
self.next_inode += 1;
inode
}
fn canonicalize(&self, target: &Path, follow_symlink: bool) -> Option<PathBuf> {
let mut canonical_path = PathBuf::new();
let mut path = target.to_path_buf();
let mut entry_stack = Vec::new();
'outer: loop {
let mut path_components = path.components().peekable();
let mut prefix = None;
while let Some(component) = path_components.next() {
match component {
Component::Prefix(prefix_component) => prefix = Some(prefix_component),
Component::RootDir => {
entry_stack.clear();
entry_stack.push(&self.root);
canonical_path.clear();
match prefix {
Some(prefix_component) => {
canonical_path = PathBuf::from(prefix_component.as_os_str());
// Prefixes like `C:\\` are represented without their trailing slash, so we have to re-add it.
canonical_path.push(std::path::MAIN_SEPARATOR_STR);
}
None => canonical_path = PathBuf::from(std::path::MAIN_SEPARATOR_STR),
}
}
Component::CurDir => {}
Component::ParentDir => {
entry_stack.pop()?;
canonical_path.pop();
}
Component::Normal(name) => {
let current_entry = *entry_stack.last()?;
if let FakeFsEntry::Dir { entries, .. } = current_entry {
let name_str = name.to_str().unwrap();
let (canonical_name, entry) = match entries.get(name_str) {
Some(entry) => (name_str, entry),
None => {
if !self.case_sensitive {
entries
.iter()
.find(|(key, _)| key.eq_ignore_ascii_case(name_str))
.map(|(key, entry)| (key.as_str(), entry))?
} else {
return None;
}
}
};
if (path_components.peek().is_some() || follow_symlink)
&& let FakeFsEntry::Symlink { target, .. } = entry
{
let mut target = target.clone();
target.extend(path_components);
path = target;
continue 'outer;
}
entry_stack.push(entry);
canonical_path = canonical_path.join(canonical_name);
} else {
return None;
}
}
}
}
break;
}
if entry_stack.is_empty() {
None
} else {
Some(canonical_path)
}
}
fn try_entry(
&mut self,
target: &Path,
follow_symlink: bool,
) -> Option<(&mut FakeFsEntry, PathBuf)> {
let canonical_path = self.canonicalize(target, follow_symlink)?;
let mut components = canonical_path
.components()
.skip_while(|component| matches!(component, Component::Prefix(_)));
let Some(Component::RootDir) = components.next() else {
panic!(
"the path {:?} was not canonicalized properly {:?}",
target, canonical_path
)
};
let mut entry = &mut self.root;
for component in components {
match component {
Component::Normal(name) => {
if let FakeFsEntry::Dir { entries, .. } = entry {
entry = entries.get_mut(name.to_str().unwrap())?;
} else {
return None;
}
}
_ => {
panic!(
"the path {:?} was not canonicalized properly {:?}",
target, canonical_path
)
}
}
}
Some((entry, canonical_path))
}
fn entry(&mut self, target: &Path) -> Result<&mut FakeFsEntry> {
Ok(self
.try_entry(target, true)
.ok_or_else(|| {
anyhow::anyhow!(io::Error::new(
io::ErrorKind::NotFound,
format!("not found: {target:?}")
))
})?
.0)
}
fn write_path<Fn, T>(&mut self, path: &Path, callback: Fn) -> Result<T>
where
Fn: FnOnce(btree_map::Entry<String, FakeFsEntry>) -> Result<T>,
{
let path = normalize_path(path);
let filename = path.file_name().context("cannot overwrite the root")?;
let parent_path = path.parent().unwrap();
let parent = self.entry(parent_path)?;
let new_entry = parent
.dir_entries(parent_path)?
.entry(filename.to_str().unwrap().into());
callback(new_entry)
}
fn emit_event<I, T>(&mut self, paths: I)
where
I: IntoIterator<Item = (T, Option<PathEventKind>)>,
T: Into<PathBuf>,
{
self.buffered_events
.extend(paths.into_iter().map(|(path, kind)| PathEvent {
path: path.into(),
kind,
}));
if !self.events_paused {
self.flush_events(self.buffered_events.len());
}
}
fn flush_events(&mut self, mut count: usize) {
use notify::event::{CreateKind, Flag, ModifyKind, RemoveKind};
count = count.min(self.buffered_events.len());
let events = self.buffered_events.drain(0..count).collect::<Vec<_>>();
let Some(event_sink) = &self.watches.event_sink else {
return;
};
for event in events {
let is_registered = self.watches.registered_paths.iter().any(|registered_path| {
if self.case_sensitive {
event.path.starts_with(registered_path)
} else {
let event_path = event.path.to_string_lossy().to_lowercase();
let registered_path = registered_path.to_string_lossy().to_lowercase();
Path::new(&event_path).starts_with(Path::new(®istered_path))
}
});
if !is_registered {
continue;
}
let notify_event = match event.kind {
Some(PathEventKind::Created) => {
notify::Event::new(notify::EventKind::Create(CreateKind::Any))
}
Some(PathEventKind::Changed) => {
notify::Event::new(notify::EventKind::Modify(ModifyKind::Any))
}
Some(PathEventKind::Removed) => {
notify::Event::new(notify::EventKind::Remove(RemoveKind::Any))
}
Some(PathEventKind::Rescan) => {
notify::Event::new(notify::EventKind::Other).set_flag(Flag::Rescan)
}
None => notify::Event::new(notify::EventKind::Any),
};
event_sink(Ok(notify_event.add_path(event.path)));
}
}
}
/// Stands in for notify at the boundary the real watcher code talks to: the
/// fake filesystem's own mutations are the "kernel" events.
#[cfg(feature = "test-support")]
struct FakeWatchBackend {
state: Arc<Mutex<FakeFsState>>,
}
#[cfg(feature = "test-support")]
impl fs_watcher::WatchBackend for FakeWatchBackend {
fn watch(&mut self, path: &Path, _mode: notify::RecursiveMode) -> notify::Result<()> {
let path = normalize_path(path);
let mut state = self.state.try_lock().expect(
"fake filesystem state is locked; this execution would have caused a test hang",
);
state.watches.watch_calls.push(path.clone());
if let Some((_, file_path)) = state
.watches
.file_to_create_on_watch
.take_if(|(watch_path, _)| *watch_path == path)
{
let inode = state.get_and_increment_inode();
let mtime = state.get_and_increment_mtime();
state
.write_path(&file_path, |entry| {
let btree_map::Entry::Vacant(entry) = entry else {
anyhow::bail!("file already exists: {}", file_path.display());
};
entry.insert(FakeFsEntry::File {
inode,
mtime,
len: 0,
content: Vec::new(),
git_dir_path: None,
});
Ok(())
})
.map_err(|error| notify::Error::generic(&error.to_string()))?;
}
state.watches.registered_paths.push(path);
Ok(())
}
fn unwatch(&mut self, path: &Path) -> notify::Result<()> {
let path = normalize_path(path);
self.state
.lock()
.watches
.registered_paths
.retain(|registered_path| *registered_path != path);
Ok(())
}
}
#[cfg(feature = "test-support")]
pub static FS_DOT_GIT: std::sync::LazyLock<&'static OsStr> =
std::sync::LazyLock::new(|| OsStr::new(".git"));
#[cfg(feature = "test-support")]
impl FakeFs {
/// We need to use something large enough for Windows and Unix to consider this a new file.
/// https://doc.rust-lang.org/nightly/std/time/struct.SystemTime.html#platform-specific-behavior
const SYSTEMTIME_INTERVAL: Duration = Duration::from_nanos(100);
pub fn new(executor: gpui::BackgroundExecutor) -> Arc<Self> {
let (tx, rx) = async_channel::bounded::<PathBuf>(10);
let state = Arc::new(Mutex::new(FakeFsState {
root: FakeFsEntry::Dir {
inode: 0,
mtime: MTime(UNIX_EPOCH),
len: 0,
entries: Default::default(),
git_repo_state: None,
},
git_event_tx: tx,
next_mtime: UNIX_EPOCH + Self::SYSTEMTIME_INTERVAL,
next_inode: 1,
watch_roots: Vec::new(),
watches: FakeWatches::default(),
buffered_events: Vec::new(),
events_paused: false,
read_dir_call_count: 0,
metadata_call_count: 0,
read_dir_hooks: Default::default(),
path_write_counts: Default::default(),
job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
trash: Mutex::new(SlotMap::with_key()),
remove_dir_errors: Default::default(),
case_sensitive: true,
}));
let native_watcher = fs_watcher::OsWatcher::with_backend(
fs_watcher::OsWatcherKind::Native,
executor.clone(),
Some(Box::new(FakeWatchBackend {
state: state.clone(),
})),
);
state.lock().watches.event_sink = Some(Box::new(native_watcher.event_sink()));
let poll_watcher =
fs_watcher::OsWatcher::new(fs_watcher::OsWatcherKind::Poll, executor.clone());
let this = Arc::new_cyclic(|this| Self {
this: this.clone(),
executor: executor.clone(),
state,
native_watcher,
poll_watcher,
});
executor.spawn({
let this = this.clone();
async move {
while let Ok(git_event) = rx.recv().await {
if let Some(mut state) = this.state.try_lock() {
state.emit_event([(git_event, Some(PathEventKind::Changed))]);
} else {
panic!("Failed to lock file system state, this execution would have caused a test hang");
}
}
}
}).detach();
this
}
/// Configures whether the fake filesystem reports as case-sensitive.
pub fn set_case_sensitive(&self, case_sensitive: bool) {
self.state.lock().case_sensitive = case_sensitive;
}
pub fn set_next_mtime(&self, next_mtime: SystemTime) {
let mut state = self.state.lock();
state.next_mtime = next_mtime;
}
pub fn get_and_increment_mtime(&self) -> MTime {
let mut state = self.state.lock();
state.get_and_increment_mtime()
}
pub async fn touch_path(&self, path: impl AsRef<Path>) {
let mut state = self.state.lock();
let path = path.as_ref();
let new_mtime = state.get_and_increment_mtime();
let new_inode = state.get_and_increment_inode();
state
.write_path(path, move |entry| {
match entry {
btree_map::Entry::Vacant(e) => {
e.insert(FakeFsEntry::File {
inode: new_inode,
mtime: new_mtime,
content: Vec::new(),
len: 0,
git_dir_path: None,
});
}
btree_map::Entry::Occupied(mut e) => match &mut *e.get_mut() {
FakeFsEntry::File { mtime, .. } => *mtime = new_mtime,
FakeFsEntry::Dir { mtime, .. } => *mtime = new_mtime,
FakeFsEntry::Symlink { .. } => {}
},
}
Ok(())
})
.unwrap();
state.emit_event([(path.to_path_buf(), Some(PathEventKind::Changed))]);
}
pub async fn insert_file(&self, path: impl AsRef<Path>, content: Vec<u8>) {
self.write_file_internal(path, content, true).unwrap()
}
pub async fn insert_symlink(&self, path: impl AsRef<Path>, target: PathBuf) {
let mut state = self.state.lock();
let path = path.as_ref();
let file = FakeFsEntry::Symlink { target };
state
.write_path(path.as_ref(), move |e| match e {
btree_map::Entry::Vacant(e) => {
e.insert(file);
Ok(())
}
btree_map::Entry::Occupied(mut e) => {
*e.get_mut() = file;
Ok(())
}
})
.unwrap();
state.emit_event([(path, Some(PathEventKind::Created))]);
}
fn write_file_internal(
&self,
path: impl AsRef<Path>,
new_content: Vec<u8>,
recreate_inode: bool,
) -> Result<()> {
fn inner(
this: &FakeFs,
path: &Path,
new_content: Vec<u8>,
recreate_inode: bool,
) -> Result<()> {
let mut state = this.state.lock();
let path_buf = path.to_path_buf();
*state.path_write_counts.entry(path_buf).or_insert(0) += 1;
let new_inode = state.get_and_increment_inode();
let new_mtime = state.get_and_increment_mtime();
let new_len = new_content.len() as u64;
let mut kind = None;
state.write_path(path, |entry| {
match entry {
btree_map::Entry::Vacant(e) => {
kind = Some(PathEventKind::Created);
e.insert(FakeFsEntry::File {
inode: new_inode,
mtime: new_mtime,
len: new_len,
content: new_content,
git_dir_path: None,
});
}
btree_map::Entry::Occupied(mut e) => {
kind = Some(PathEventKind::Changed);
if let FakeFsEntry::File {
inode,
mtime,
len,
content,
..
} = e.get_mut()
{
*mtime = new_mtime;
*content = new_content;
*len = new_len;
if recreate_inode {
*inode = new_inode;
}
} else {
anyhow::bail!("not a file")
}
}
}
Ok(())
})?;
state.emit_event([(path, kind)]);
Ok(())
}
inner(self, path.as_ref(), new_content, recreate_inode)
}
pub fn read_file_sync(&self, path: impl AsRef<Path>) -> Result<Vec<u8>> {
let path = path.as_ref();
let path = normalize_path(path);
let mut state = self.state.lock();
let entry = state.entry(&path)?;
entry.file_content(&path).cloned()
}
async fn load_internal(&self, path: impl AsRef<Path>) -> Result<Vec<u8>> {
let path = path.as_ref();
let path = normalize_path(path);
self.simulate_random_delay().await;
let mut state = self.state.lock();
let entry = state.entry(&path)?;
entry.file_content(&path).cloned()
}
pub fn pause_events(&self) {
self.state.lock().events_paused = true;
}
pub fn unpause_events_and_flush(&self) {
self.state.lock().events_paused = false;
self.flush_events(usize::MAX);
}
pub fn buffered_event_count(&self) -> usize {
self.state.lock().buffered_events.len()
}
pub fn clear_buffered_events(&self) {
self.state.lock().buffered_events.clear();
}
/// Simulates the kernel's watch queue overflowing: all buffered
/// (undelivered) events are lost, and the watcher reports only a single
/// `Rescan` event for `root`, mirroring how the native backends report
/// lost sync (FSEvents `kFSEventStreamEventFlagMustScanSubDirs`, inotify
/// `IN_Q_OVERFLOW`, Windows `ERROR_NOTIFY_ENUM_DIR`).
///
/// Note that the fake file system's state is unaffected; like a real
/// overflow, only the notifications are lost, not the changes themselves.
pub fn simulate_watcher_overflow(&self, root: impl Into<PathBuf>) {
let mut state = self.state.lock();
state.buffered_events.clear();
state.emit_event([(root, Some(PathEventKind::Rescan))]);
}
/// Every path the watcher backend has been asked to watch, in order,
/// including paths that were later unwatched.
pub fn watch_calls(&self) -> Vec<PathBuf> {
self.state.lock().watches.watch_calls.clone()
}
/// The paths currently registered with the watcher backend.
pub fn registered_watch_paths(&self) -> Vec<PathBuf> {
self.state.lock().watches.registered_paths.clone()
}
/// Runs once, immediately before the next `read_dir` resolves `path`.
pub fn before_read_dir(&self, path: impl AsRef<Path>, hook: BoxFuture<'static, ()>) {
self.state
.lock()
.read_dir_hooks
.insert(normalize_path(path.as_ref()), hook);
}
/// Creates `path` without emitting an event when `watch_path` is next
/// watched, simulating a change that lands while the watch is installed.
pub fn create_file_before_next_watch_add(
&self,
watch_path: impl AsRef<Path>,
path: impl AsRef<Path>,
) {
self.state.lock().watches.file_to_create_on_watch = Some((
normalize_path(watch_path.as_ref()),
normalize_path(path.as_ref()),
));
}
pub fn flush_events(&self, count: usize) {
self.state.lock().flush_events(count);
}
pub(crate) fn entry(&self, target: &Path) -> Result<FakeFsEntry> {
self.state.lock().entry(target).cloned()
}
pub(crate) fn insert_entry(&self, target: &Path, new_entry: FakeFsEntry) -> Result<()> {
let mut state = self.state.lock();
state.write_path(target, |entry| {
match entry {
btree_map::Entry::Vacant(vacant_entry) => {
vacant_entry.insert(new_entry);
}
btree_map::Entry::Occupied(mut occupied_entry) => {
occupied_entry.insert(new_entry);
}
}
Ok(())
})
}
#[must_use]
pub fn insert_tree<'a>(
&'a self,
path: impl 'a + AsRef<Path> + Send,
tree: serde_json::Value,
) -> futures::future::BoxFuture<'a, ()> {
use futures::FutureExt as _;
use serde_json::Value::*;
fn inner<'a>(
this: &'a FakeFs,
path: Arc<Path>,
tree: serde_json::Value,
) -> futures::future::BoxFuture<'a, ()> {
async move {
match tree {
Object(map) => {
this.create_dir(&path).await.unwrap();
for (name, contents) in map {
let mut path = PathBuf::from(path.as_ref());
path.push(name);
this.insert_tree(&path, contents).await;
}
}
Null => {
this.create_dir(&path).await.unwrap();
}
String(contents) => {
this.insert_file(&path, contents.into_bytes()).await;
}
_ => {
panic!("JSON object must contain only objects, strings, or null");
}
}
}
.boxed()
}
inner(self, Arc::from(path.as_ref()), tree)
}
pub fn insert_tree_from_real_fs<'a>(
&'a self,
path: impl 'a + AsRef<Path> + Send,
src_path: impl 'a + AsRef<Path> + Send,
) -> futures::future::BoxFuture<'a, ()> {
use futures::FutureExt as _;
async move {
let path = path.as_ref();
if std::fs::metadata(&src_path).unwrap().is_file() {
let contents = std::fs::read(src_path).unwrap();
self.insert_file(path, contents).await;
} else {
self.create_dir(path).await.unwrap();
for entry in std::fs::read_dir(&src_path).unwrap() {
let entry = entry.unwrap();
self.insert_tree_from_real_fs(path.join(entry.file_name()), entry.path())
.await;
}
}
}
.boxed()
}
pub fn with_git_state_and_paths<T, F>(
&self,
dot_git: &Path,
emit_git_event: bool,
f: F,
) -> Result<T>
where
F: FnOnce(&mut FakeGitRepositoryState, &Path, &Path) -> T,
{
let mut state = self.state.lock();
let git_event_tx = state.git_event_tx.clone();
let entry = state.entry(dot_git).context("open .git")?;
if let FakeFsEntry::Dir { git_repo_state, .. } = entry {
let repo_state = git_repo_state.get_or_insert_with(|| {
log::debug!("insert git state for {dot_git:?}");
Arc::new(Mutex::new(FakeGitRepositoryState::new(git_event_tx)))
});
let mut repo_state = repo_state.lock();
let result = f(&mut repo_state, dot_git, dot_git);
drop(repo_state);
if emit_git_event {
state.emit_event([(
dot_git.join("fake_git_repo_event"),
Some(PathEventKind::Changed),
)]);
}
Ok(result)
} else if let FakeFsEntry::File {
content,
git_dir_path,
..
} = &mut *entry
{
let path = match git_dir_path {
Some(path) => path,
None => {
let path = std::str::from_utf8(content)
.ok()
.and_then(|content| content.strip_prefix("gitdir:"))
.context("not a valid gitfile")?
.trim();
git_dir_path.insert(normalize_path(&dot_git.parent().unwrap().join(path)))
}
}
.clone();
let Some((git_dir_entry, canonical_path)) = state.try_entry(&path, true) else {
anyhow::bail!("pointed-to git dir {path:?} not found")
};
let FakeFsEntry::Dir {
git_repo_state,
entries,
..
} = git_dir_entry
else {
anyhow::bail!("gitfile points to a non-directory")
};
let common_dir = if let Some(child) = entries.get("commondir") {
let raw = std::str::from_utf8(child.file_content("commondir".as_ref())?)
.context("commondir content")?
.trim();
let raw_path = Path::new(raw);
if raw_path.is_relative() {
normalize_path(&canonical_path.join(raw_path))
} else {
raw_path.to_owned()
}
} else {
canonical_path.clone()
};
let repo_state = git_repo_state.get_or_insert_with(|| {
Arc::new(Mutex::new(FakeGitRepositoryState::new(git_event_tx)))
});
let mut repo_state = repo_state.lock();
let result = f(&mut repo_state, &canonical_path, &common_dir);
if emit_git_event {
drop(repo_state);
state.emit_event([(
canonical_path.join("fake_git_repo_event"),
Some(PathEventKind::Changed),
)]);
}
Ok(result)
} else {
anyhow::bail!("not a valid git repository");
}
}
pub fn with_git_state<T, F>(&self, dot_git: &Path, emit_git_event: bool, f: F) -> Result<T>
where
F: FnOnce(&mut FakeGitRepositoryState) -> T,
{
self.with_git_state_and_paths(dot_git, emit_git_event, |state, _, _| f(state))
}
pub fn set_branch_name(&self, dot_git: &Path, branch: Option<impl Into<String>>) {
self.with_git_state(dot_git, true, |state| {
let branch = branch.map(Into::into);
state.branches.extend(branch.clone());
state.current_branch_name = branch
})
.unwrap();
}
pub fn set_remote_for_repo(
&self,
dot_git: &Path,
name: impl Into<String>,
url: impl Into<String>,
) {
self.with_git_state(dot_git, true, |state| {
state.remotes.insert(name.into(), url.into());
})
.unwrap();
}
pub fn insert_branches(&self, dot_git: &Path, branches: &[&str]) {
self.with_git_state(dot_git, true, |state| {
if let Some(first) = branches.first()
&& state.current_branch_name.is_none()
{
state.current_branch_name = Some(first.to_string())
}
state
.branches
.extend(branches.iter().map(ToString::to_string));
})
.unwrap();
}
pub async fn add_linked_worktree_for_repo(
&self,
dot_git: &Path,
emit_git_event: bool,
worktree: Worktree,
) {
let ref_name = worktree
.ref_name
.as_ref()
.expect("linked worktree must have a ref_name");
let branch_name = ref_name
.strip_prefix("refs/heads/")
.unwrap_or(ref_name.as_ref());
// Create ref in git state.
self.with_git_state(dot_git, false, |state| {
state
.refs
.insert(ref_name.to_string(), worktree.sha.to_string());
})
.unwrap();
// Create .git/worktrees/<name>/ directory with HEAD, commondir, and gitdir.
let worktrees_entry_dir = dot_git.join("worktrees").join(branch_name);
self.create_dir(&worktrees_entry_dir).await.unwrap();
self.write_file_internal(
worktrees_entry_dir.join("HEAD"),
format!("ref: {ref_name}").into_bytes(),
false,
)
.unwrap();
self.write_file_internal(
worktrees_entry_dir.join("commondir"),
dot_git.to_string_lossy().into_owned().into_bytes(),
false,
)
.unwrap();
let worktree_dot_git = worktree.path.join(".git");
self.write_file_internal(
worktrees_entry_dir.join("gitdir"),
worktree_dot_git.to_string_lossy().into_owned().into_bytes(),
false,
)
.unwrap();
// Create the worktree checkout directory with a .git file pointing back.
self.create_dir(&worktree.path).await.unwrap();
self.write_file_internal(
&worktree_dot_git,
format!("gitdir: {}", worktrees_entry_dir.display()).into_bytes(),
false,
)
.unwrap();
if emit_git_event {
self.with_git_state(dot_git, true, |_| {}).unwrap();
}
}
pub async fn remove_worktree_for_repo(
&self,
dot_git: &Path,
emit_git_event: bool,
ref_name: &str,
) {
let branch_name = ref_name.strip_prefix("refs/heads/").unwrap_or(ref_name);
let worktrees_entry_dir = dot_git.join("worktrees").join(branch_name);
// Read gitdir to find the worktree checkout path.
let gitdir_content = self
.load_internal(worktrees_entry_dir.join("gitdir"))
.await
.unwrap();
let gitdir_str = String::from_utf8(gitdir_content).unwrap();
let worktree_path = PathBuf::from(gitdir_str.trim())
.parent()
.map(PathBuf::from)
.unwrap_or_default();
// Remove the worktree checkout directory.
self.remove_dir(
&worktree_path,
RemoveOptions {
recursive: true,
ignore_if_not_exists: true,
},
)
.await
.unwrap();
// Remove the .git/worktrees/<name>/ directory.
self.remove_dir(
&worktrees_entry_dir,
RemoveOptions {
recursive: true,
ignore_if_not_exists: false,
},
)
.await
.unwrap();
if emit_git_event {
self.with_git_state(dot_git, true, |_| {}).unwrap();
}
}
pub fn set_unmerged_paths_for_repo(
&self,
dot_git: &Path,
unmerged_state: &[(RepoPath, UnmergedStatus)],
) {
self.with_git_state(dot_git, true, |state| {
state.unmerged_paths.clear();
state.unmerged_paths.extend(
unmerged_state
.iter()
.map(|(path, content)| (path.clone(), *content)),
);
})
.unwrap();
}
pub fn set_index_for_repo(&self, dot_git: &Path, index_state: &[(&str, String)]) {
self.with_git_state(dot_git, true, |state| {
state.index_contents.clear();
state.index_contents.extend(
index_state
.iter()
.map(|(path, content)| (repo_path(path), content.as_bytes().to_vec())),
);
})
.unwrap();
}
pub fn set_head_for_repo(
&self,
dot_git: &Path,
head_state: &[(&str, String)],
sha: impl Into<String>,
) {
self.with_git_state(dot_git, true, |state| {
state.head_contents.clear();
state.head_contents.extend(
head_state
.iter()
.map(|(path, content)| (repo_path(path), content.as_bytes().to_vec())),
);
state.refs.insert("HEAD".into(), sha.into());
})
.unwrap();
}
pub fn set_head_and_index_for_repo(&self, dot_git: &Path, contents_by_path: &[(&str, String)]) {
self.with_git_state(dot_git, true, |state| {
state.head_contents.clear();
state.head_contents.extend(
contents_by_path
.iter()
.map(|(path, contents)| (repo_path(path), contents.as_bytes().to_vec())),
);
state.index_contents = state.head_contents.clone();
})
.unwrap();
}
pub fn set_merge_base_content_for_repo(
&self,
dot_git: &Path,
contents_by_path: &[(&str, String)],
) -> Vec<git::Oid> {
self.with_git_state(dot_git, true, |state| {
use git::Oid;
state.merge_base_contents.clear();
let mut assigned = Vec::with_capacity(contents_by_path.len());
for (index, (path, content)) in contents_by_path.iter().enumerate() {
let mut bytes = [0u8; 20];
bytes[..4].copy_from_slice(&((index as u32) + 1).to_be_bytes());
let oid = Oid::from_bytes(&bytes).unwrap();
state.merge_base_contents.insert(repo_path(path), oid);
state.oids.insert(oid, content.as_bytes().to_vec());
assigned.push(oid);
}
assigned
})
.unwrap()
}
pub fn install_blob_read_gate_for_repo(&self, dot_git: &Path) -> FakeBlobReadGate {
let gate = FakeBlobReadGate::default();
self.with_git_state(dot_git, false, |state| {
state.blob_read_gate = Some(gate.clone());
})
.unwrap();
gate
}
pub fn set_blame_for_repo(&self, dot_git: &Path, blames: Vec<(RepoPath, git::blame::Blame)>) {
self.with_git_state(dot_git, true, |state| {
state.blames.clear();
state.blames.extend(blames);
})
.unwrap();
}
pub fn set_graph_commits(&self, dot_git: &Path, commits: Vec<Arc<InitialGraphCommitData>>) {
self.with_git_state(dot_git, true, |state| {
state.graph_commits = commits;
})
.unwrap();
}
pub fn set_graph_error(&self, dot_git: &Path, error: Option<String>) {
self.with_git_state(dot_git, true, |state| {
state.simulated_graph_error = error;
})
.unwrap();
}
pub fn set_commit_data(
&self,
dot_git: &Path,
commit_data: impl IntoIterator<Item = (CommitData, bool)>,
) {
self.with_git_state(dot_git, true, |state| {
state.commit_data = commit_data
.into_iter()
.map(|(data, should_fail)| {
(
data.sha,
if should_fail {
FakeCommitDataEntry::Fail(data)
} else {
FakeCommitDataEntry::Success(data)
},
)
})
.collect();
})
.unwrap();
}
/// Put the given git repository into a state with the given status,
/// by mutating the head, index, and unmerged state.
pub fn set_status_for_repo(&self, dot_git: &Path, statuses: &[(&str, FileStatus)]) {
let workdir_path = dot_git.parent().unwrap();
let workdir_contents = self.files_with_contents(workdir_path);
self.with_git_state(dot_git, true, |state| {
state.index_contents.clear();
state.head_contents.clear();
state.unmerged_paths.clear();
for (path, content) in workdir_contents {
use util::{paths::PathStyle, rel_path::RelPath};
let repo_path = RelPath::new(path.strip_prefix(&workdir_path).unwrap(), PathStyle::local()).unwrap();
let repo_path = RepoPath::from_rel_path(&repo_path);
let status = statuses
.iter()
.find_map(|(p, status)| (*p == repo_path.as_unix_str()).then_some(status));
let mut content = String::from_utf8_lossy(&content).to_string();
let mut index_content = None;
let mut head_content = None;
match status {
None => {
index_content = Some(content.clone());
head_content = Some(content);
}
Some(FileStatus::Untracked | FileStatus::Ignored) => {}
Some(FileStatus::Unmerged(unmerged_status)) => {
state
.unmerged_paths
.insert(repo_path.clone(), *unmerged_status);
content.push_str(" (unmerged)");
index_content = Some(content.clone());
head_content = Some(content);
}
Some(FileStatus::Tracked(TrackedStatus {
index_status,
worktree_status,
})) => {
match worktree_status {
StatusCode::Modified => {
let mut content = content.clone();
content.push_str(" (modified in working copy)");
index_content = Some(content);
}
StatusCode::TypeChanged | StatusCode::Unmodified => {
index_content = Some(content.clone());
}
StatusCode::Added => {}
StatusCode::Deleted | StatusCode::Renamed | StatusCode::Copied => {
panic!("cannot create these statuses for an existing file");
}
};
match index_status {
StatusCode::Modified => {
let mut content = index_content.clone().expect(
"file cannot be both modified in index and created in working copy",
);
content.push_str(" (modified in index)");
head_content = Some(content);
}
StatusCode::TypeChanged | StatusCode::Unmodified => {
head_content = Some(index_content.clone().expect("file cannot be both unmodified in index and created in working copy"));
}
StatusCode::Added => {}
StatusCode::Deleted => {
head_content = Some("".into());
}
StatusCode::Renamed | StatusCode::Copied => {
panic!("cannot create these statuses for an existing file");
}
};
}
};
if let Some(content) = index_content {
state
.index_contents
.insert(repo_path.clone(), content.into_bytes());
}
if let Some(content) = head_content {
state
.head_contents
.insert(repo_path.clone(), content.into_bytes());
}
}
}).unwrap();
}
pub fn set_error_message_for_index_write(&self, dot_git: &Path, message: Option<String>) {
self.with_git_state(dot_git, true, |state| {
state.simulated_index_write_error_message = message;
})
.unwrap();
}
pub fn set_create_worktree_error(&self, dot_git: &Path, message: Option<String>) {
self.with_git_state(dot_git, true, |state| {
state.simulated_create_worktree_error = message;
})
.unwrap();
}
/// Makes subsequent `remove_dir` calls for `path` fail with `message`.
pub fn set_remove_dir_error(&self, path: impl AsRef<Path>, message: String) {
self.state
.lock()
.remove_dir_errors
.insert(Self::remove_dir_error_key(path.as_ref()), message);
}
pub fn clear_remove_dir_error(&self, path: impl AsRef<Path>) {
self.state
.lock()
.remove_dir_errors
.remove(&Self::remove_dir_error_key(path.as_ref()));
}
/// Entry resolution in `try_entry` ignores drive prefixes, so the error
/// injection map must too.
/// Otherwise, on Windows, a key like `C:\workspace\dir` would never match a
/// lookup for `\workspace\dir`.
fn remove_dir_error_key(path: &Path) -> PathBuf {
normalize_path(path)
.components()
.skip_while(|component| matches!(component, Component::Prefix(_)))
.collect()
}
pub fn paths(&self, include_dot_git: bool) -> Vec<PathBuf> {
let mut result = Vec::new();
let mut queue = collections::VecDeque::new();
let state = &*self.state.lock();
queue.push_back((PathBuf::from(util::path!("/")), &state.root));
while let Some((path, entry)) = queue.pop_front() {
if let FakeFsEntry::Dir { entries, .. } = entry {
for (name, entry) in entries {
queue.push_back((path.join(name), entry));
}
}
if include_dot_git
|| !path
.components()
.any(|component| component.as_os_str() == *FS_DOT_GIT)
{
result.push(path);
}
}
result
}
pub fn directories(&self, include_dot_git: bool) -> Vec<PathBuf> {
let mut result = Vec::new();
let mut queue = collections::VecDeque::new();
let state = &*self.state.lock();
queue.push_back((PathBuf::from(util::path!("/")), &state.root));
while let Some((path, entry)) = queue.pop_front() {
if let FakeFsEntry::Dir { entries, .. } = entry {
for (name, entry) in entries {
queue.push_back((path.join(name), entry));
}
if include_dot_git
|| !path
.components()
.any(|component| component.as_os_str() == *FS_DOT_GIT)
{
result.push(path);
}
}
}
result
}
pub fn files(&self) -> Vec<PathBuf> {
let mut result = Vec::new();
let mut queue = collections::VecDeque::new();
let state = &*self.state.lock();
queue.push_back((PathBuf::from(util::path!("/")), &state.root));
while let Some((path, entry)) = queue.pop_front() {
match entry {
FakeFsEntry::File { .. } => result.push(path),
FakeFsEntry::Dir { entries, .. } => {
for (name, entry) in entries {
queue.push_back((path.join(name), entry));
}
}
FakeFsEntry::Symlink { .. } => {}
}
}
result
}
pub fn files_with_contents(&self, prefix: &Path) -> Vec<(PathBuf, Vec<u8>)> {
let mut result = Vec::new();
let mut queue = collections::VecDeque::new();
let state = &*self.state.lock();
queue.push_back((PathBuf::from(util::path!("/")), &state.root));
while let Some((path, entry)) = queue.pop_front() {
match entry {
FakeFsEntry::File { content, .. } => {
if path.starts_with(prefix) {
result.push((path, content.clone()));
}
}
FakeFsEntry::Dir { entries, .. } => {
for (name, entry) in entries {
queue.push_back((path.join(name), entry));
}
}
FakeFsEntry::Symlink { .. } => {}
}
}
result
}
/// How many `read_dir` calls have been issued.
pub fn read_dir_call_count(&self) -> usize {
self.state.lock().read_dir_call_count
}
/// The roots passed to `Fs::watch` whose watchers are still alive.
pub fn watched_paths(&self) -> Vec<PathBuf> {
let state = self.state.lock();
state
.watch_roots
.iter()
.filter_map(|(path, watcher)| (watcher.strong_count() > 0).then_some(path.clone()))
.collect()
}
/// How many `metadata` calls have been issued.
pub fn metadata_call_count(&self) -> usize {
self.state.lock().metadata_call_count
}
/// How many write operations have been issued for a specific path.
pub fn write_count_for_path(&self, path: impl AsRef<Path>) -> usize {
let path = path.as_ref().to_path_buf();
self.state
.lock()
.path_write_counts
.get(&path)
.copied()
.unwrap_or(0)
}
pub fn emit_fs_event(&self, path: impl Into<PathBuf>, event: Option<PathEventKind>) {
self.state.lock().emit_event(std::iter::once((path, event)));
}
fn simulate_random_delay(&self) -> impl futures::Future<Output = ()> {
self.executor.simulate_random_delay()
}
async fn remove_dir_inner(
&self,
path: &Path,
options: RemoveOptions,
) -> Result<Option<FakeFsEntry>> {
self.simulate_random_delay().await;
let path = normalize_path(path);
if let Some(message) = self
.state
.lock()
.remove_dir_errors
.get(&Self::remove_dir_error_key(&path))
{
anyhow::bail!("{message}");
}
let parent_path = path.parent().context("cannot remove the root")?;
let base_name = path.file_name().context("cannot remove the root")?;
let mut state = self.state.lock();
let parent_entry = state.entry(parent_path)?;
let entry = parent_entry
.dir_entries(parent_path)?
.entry(base_name.to_str().unwrap().into());
let removed = match entry {
btree_map::Entry::Vacant(_) => {
if !options.ignore_if_not_exists {
anyhow::bail!("{path:?} does not exist");
}
None
}
btree_map::Entry::Occupied(mut entry) => {
{
let children = entry.get_mut().dir_entries(&path)?;
if !options.recursive && !children.is_empty() {
anyhow::bail!("{path:?} is not empty");
}
}
Some(entry.remove())
}
};
state.emit_event([(path, Some(PathEventKind::Removed))]);
Ok(removed)
}
async fn remove_file_inner(
&self,
path: &Path,
options: RemoveOptions,
) -> Result<Option<FakeFsEntry>> {
self.simulate_random_delay().await;
let path = normalize_path(path);
let parent_path = path.parent().context("cannot remove the root")?;
let base_name = path.file_name().unwrap();
let mut state = self.state.lock();
let parent_entry = state.entry(parent_path)?;
let entry = parent_entry
.dir_entries(parent_path)?
.entry(base_name.to_str().unwrap().into());
let removed = match entry {
btree_map::Entry::Vacant(_) => {
if !options.ignore_if_not_exists {
anyhow::bail!("{path:?} does not exist");
}
None
}
btree_map::Entry::Occupied(entry) => {
// Like `unlink`, removing a symlink removes the link itself.
if let entry = entry.get()
&& !entry.is_symlink()
{
entry.file_content(&path)?;
}
Some(entry.remove())
}
};
state.emit_event([(path, Some(PathEventKind::Removed))]);
Ok(removed)
}
pub fn trashed_paths(&self) -> Vec<PathBuf> {
self.state
.lock()
.trash
.lock()
.values()
.map(|(trashed_entry, _fake_entry)| {
PathBuf::new()
.join(trashed_entry.original_parent.clone())
.join(trashed_entry.name.clone())
})
.collect::<Vec<PathBuf>>()
}
}
#[cfg(feature = "test-support")]
impl FakeFsEntry {
fn is_file(&self) -> bool {
matches!(self, Self::File { .. })
}
fn is_symlink(&self) -> bool {
matches!(self, Self::Symlink { .. })
}
fn file_content(&self, path: &Path) -> Result<&Vec<u8>> {
if let Self::File { content, .. } = self {
Ok(content)
} else {
anyhow::bail!("not a file: {path:?}");
}
}
fn dir_entries(&mut self, path: &Path) -> Result<&mut BTreeMap<String, FakeFsEntry>> {
if let Self::Dir { entries, .. } = self {
Ok(entries)
} else {
anyhow::bail!("not a directory: {path:?}");
}
}
}
#[cfg(feature = "test-support")]
#[derive(Debug)]
struct FakeHandle {
inode: u64,
}
#[cfg(feature = "test-support")]
impl FileHandle for FakeHandle {
fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf> {
let fs = fs.as_fake();
let state = fs.state.lock();
let mut queue = collections::VecDeque::new();
queue.push_back((PathBuf::from(util::path!("/")), &state.root));
while let Some((path, entry)) = queue.pop_front() {
match entry {
FakeFsEntry::File { inode, .. } | FakeFsEntry::Dir { inode, .. }
if *inode == self.inode =>
{
return Ok(path);
}
FakeFsEntry::Dir { entries, .. } => {
for (name, entry) in entries {
queue.push_back((path.join(name), entry));
}
}
_ => {}
}
}
anyhow::bail!("fake fd target not found")
}
}
#[cfg(feature = "test-support")]
#[async_trait::async_trait]
impl Fs for FakeFs {
async fn create_dir(&self, path: &Path) -> Result<()> {
self.simulate_random_delay().await;
let mut created_dirs = Vec::new();
let mut cur_path = PathBuf::new();
for component in path.components() {
let should_skip = matches!(component, Component::Prefix(..) | Component::RootDir);
cur_path.push(component);
if should_skip {
continue;
}
let mut state = self.state.lock();
let inode = state.get_and_increment_inode();
let mtime = state.get_and_increment_mtime();
state.write_path(&cur_path, |entry| {
entry.or_insert_with(|| {
created_dirs.push((cur_path.clone(), Some(PathEventKind::Created)));
FakeFsEntry::Dir {
inode,
mtime,
len: 0,
entries: Default::default(),
git_repo_state: None,
}
});
Ok(())
})?
}
self.state.lock().emit_event(created_dirs);
Ok(())
}
async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
self.simulate_random_delay().await;
let mut state = self.state.lock();
let inode = state.get_and_increment_inode();
let mtime = state.get_and_increment_mtime();
let file = FakeFsEntry::File {
inode,
mtime,
len: 0,
content: Vec::new(),
git_dir_path: None,
};
let mut kind = Some(PathEventKind::Created);
state.write_path(path, |entry| {
match entry {
btree_map::Entry::Occupied(mut e) => {
if options.overwrite {
kind = Some(PathEventKind::Changed);
*e.get_mut() = file;
} else if !options.ignore_if_exists {
anyhow::bail!("path already exists: {path:?}");
}
}
btree_map::Entry::Vacant(e) => {
e.insert(file);
}
}
Ok(())
})?;
state.emit_event([(path, kind)]);
Ok(())
}
async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()> {
let mut state = self.state.lock();
let file = FakeFsEntry::Symlink { target };
state
.write_path(path.as_ref(), move |e| match e {
btree_map::Entry::Vacant(e) => {
e.insert(file);
Ok(())
}
btree_map::Entry::Occupied(mut e) => {
*e.get_mut() = file;
Ok(())
}
})
.unwrap();
state.emit_event([(path, Some(PathEventKind::Created))]);
Ok(())
}
async fn create_file_with(
&self,
path: &Path,
mut content: Pin<&mut (dyn AsyncRead + Send)>,
) -> Result<()> {
let mut bytes = Vec::new();
content.read_to_end(&mut bytes).await?;
self.write_file_internal(path, bytes, true)?;
Ok(())
}
async fn extract_tar_file(
&self,
path: &Path,
content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
) -> Result<()> {
let mut entries = content.entries()?;
while let Some(entry) = entries.next().await {
let mut entry = entry?;
if entry.header().entry_type().is_file() {
let path = path.join(entry.path()?.as_ref());
let mut bytes = Vec::new();
entry.read_to_end(&mut bytes).await?;
self.create_dir(path.parent().unwrap()).await?;
self.write_file_internal(&path, bytes, true)?;
}
}
Ok(())
}
async fn rename(&self, old_path: &Path, new_path: &Path, options: RenameOptions) -> Result<()> {
self.simulate_random_delay().await;
let old_path = normalize_path(old_path);
let new_path = normalize_path(new_path);
if options.create_parents {
if let Some(parent) = new_path.parent() {
self.create_dir(parent).await?;
}
}
let mut state = self.state.lock();
let moved_entry = state.write_path(&old_path, |e| {
if let btree_map::Entry::Occupied(e) = e {
Ok(e.get().clone())
} else {
anyhow::bail!("path does not exist: {old_path:?}")
}
})?;
// POSIX `rename` succeeds without doing anything when both names resolve
// to the same file. Falling through would assign the entry onto itself
// and then remove it, destroying the file. The lookup above has already
// reported a missing source, so only an existing one reaches here.
if old_path == new_path {
return Ok(());
}
let mut moved = true;
state.write_path(&new_path, |e| {
match e {
btree_map::Entry::Occupied(mut e) => {
if options.overwrite {
*e.get_mut() = moved_entry;
} else if options.ignore_if_exists {
// `RealFs` reports success without moving anything here,
// leaving the source in place. Removing it instead would
// destroy a file the caller still expects to find.
moved = false;
} else {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("path already exists: {new_path:?}"),
)
.into());
}
}
btree_map::Entry::Vacant(e) => {
e.insert(moved_entry);
}
}
Ok(())
})?;
if !moved {
return Ok(());
}
state
.write_path(&old_path, |e| {
if let btree_map::Entry::Occupied(e) = e {
Ok(e.remove())
} else {
unreachable!()
}
})
.unwrap();
state.emit_event([
(old_path, Some(PathEventKind::Removed)),
(new_path, Some(PathEventKind::Created)),
]);
Ok(())
}
async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
self.simulate_random_delay().await;
let source = normalize_path(source);
let target = normalize_path(target);
let mut state = self.state.lock();
let mtime = state.get_and_increment_mtime();
let new_inode = state.get_and_increment_inode();
let source_entry = state.entry(&source)?;
let content = source_entry.file_content(&source)?.clone();
let new_entry = move |inode| FakeFsEntry::File {
inode,
mtime,
len: content.len() as u64,
content,
git_dir_path: None,
};
let kind = state.write_path(&target, |e| match e {
btree_map::Entry::Occupied(mut e) => {
if !options.overwrite {
if options.ignore_if_exists {
return Ok(None);
}
anyhow::bail!("{target:?} already exists");
}
let inode = match e.get() {
FakeFsEntry::File { inode, .. } => *inode,
FakeFsEntry::Dir { .. } => anyhow::bail!("{target:?} is a directory"),
FakeFsEntry::Symlink { .. } => new_inode,
};
e.insert(new_entry(inode));
Ok(Some(PathEventKind::Changed))
}
btree_map::Entry::Vacant(e) => {
e.insert(new_entry(new_inode));
Ok(Some(PathEventKind::Created))
}
})?;
if let Some(kind) = kind {
state.emit_event([(target, Some(kind))]);
}
Ok(())
}
async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
self.remove_dir_inner(path, options).await.map(|_| ())
}
async fn trash(&self, path: &Path, options: RemoveOptions) -> Result<TrashId> {
let normalized_path = normalize_path(path);
let parent_path = normalized_path.parent().context("cannot remove the root")?;
let base_name = normalized_path.file_name().unwrap();
let result = if self.is_dir(path).await {
self.remove_dir_inner(path, options).await?
} else {
self.remove_file_inner(path, options).await?
};
match result {
Some(fake_entry) => {
let trashed_entry = TrashedEntry {
id: base_name.to_str().unwrap().into(),
name: base_name.to_str().unwrap().into(),
original_parent: parent_path.to_path_buf(),
};
let trash_id = self
.state
.lock()
.trash
.lock()
.insert((trashed_entry, fake_entry));
Ok(trash_id)
}
None => anyhow::bail!("{normalized_path:?} does not exist"),
}
}
async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
self.remove_file_inner(path, options).await.map(|_| ())
}
async fn open_sync(&self, path: &Path) -> Result<Box<dyn ReadSeek + Send + Sync>> {
let bytes = self.load_internal(path).await?;
Ok(Box::new(io::Cursor::new(bytes)))
}
async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>> {
self.simulate_random_delay().await;
let mut state = self.state.lock();
let inode = match state.entry(path)? {
FakeFsEntry::File { inode, .. } => *inode,
FakeFsEntry::Dir { inode, .. } => *inode,
_ => unreachable!(),
};
Ok(Arc::new(FakeHandle { inode }))
}
async fn load(&self, path: &Path) -> Result<String> {
let content = self.load_internal(path).await?;
Ok(String::from_utf8(content)?)
}
async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>> {
self.load_internal(path).await
}
async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
self.simulate_random_delay().await;
let path = normalize_path(path.as_path());
if let Some(path) = path.parent() {
self.create_dir(path).await?;
}
self.write_file_internal(path, data.into_bytes(), true)?;
Ok(())
}
async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
self.simulate_random_delay().await;
let path = normalize_path(path);
let content = text::chunks_with_line_ending(text, line_ending).collect::<String>();
if let Some(path) = path.parent() {
self.create_dir(path).await?;
}
self.write_file_internal(path, content.into_bytes(), false)?;
Ok(())
}
async fn write(&self, path: &Path, content: &[u8]) -> Result<()> {
self.simulate_random_delay().await;
let path = normalize_path(path);
if let Some(path) = path.parent() {
self.create_dir(path).await?;
}
self.write_file_internal(path, content.to_vec(), false)?;
Ok(())
}
async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
let path = normalize_path(path);
self.simulate_random_delay().await;
let state = self.state.lock();
let canonical_path = state
.canonicalize(&path, true)
.with_context(|| format!("path does not exist: {path:?}"))?;
Ok(canonical_path)
}
async fn is_file(&self, path: &Path) -> bool {
let path = normalize_path(path);
self.simulate_random_delay().await;
let mut state = self.state.lock();
if let Some((entry, _)) = state.try_entry(&path, true) {
entry.is_file()
} else {
false
}
}
async fn is_dir(&self, path: &Path) -> bool {
self.metadata(path)
.await
.is_ok_and(|metadata| metadata.is_some_and(|metadata| metadata.is_dir))
}
async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
self.simulate_random_delay().await;
let path = normalize_path(path);
let mut state = self.state.lock();
state.metadata_call_count += 1;
if let Some((mut entry, _)) = state.try_entry(&path, false) {
let is_symlink = entry.is_symlink();
if is_symlink {
if let Some(e) = state.try_entry(&path, true).map(|e| e.0) {
entry = e;
} else {
return Ok(None);
}
}
Ok(Some(match &*entry {
FakeFsEntry::File {
inode, mtime, len, ..
} => Metadata {
inode: *inode,
mtime: *mtime,
len: *len,
is_dir: false,
is_symlink,
is_fifo: false,
is_executable: false,
is_writable: true,
},
FakeFsEntry::Dir {
inode, mtime, len, ..
} => Metadata {
inode: *inode,
mtime: *mtime,
len: *len,
is_dir: true,
is_symlink,
is_fifo: false,
is_executable: false,
is_writable: true,
},
FakeFsEntry::Symlink { .. } => unreachable!(),
}))
} else {
Ok(None)
}
}
async fn read_link(&self, path: &Path) -> Result<PathBuf> {
self.simulate_random_delay().await;
let path = normalize_path(path);
let mut state = self.state.lock();
let (entry, _) = state
.try_entry(&path, false)
.with_context(|| format!("path does not exist: {path:?}"))?;
if let FakeFsEntry::Symlink { target } = entry {
Ok(target.clone())
} else {
anyhow::bail!("not a symlink: {path:?}")
}
}
async fn read_dir(
&self,
path: &Path,
) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
self.simulate_random_delay().await;
let path = normalize_path(path);
let hook = self.state.lock().read_dir_hooks.remove(&path);
if let Some(hook) = hook {
hook.await;
}
let mut state = self.state.lock();
state.read_dir_call_count += 1;
let entry = state.entry(&path)?;
let children = entry.dir_entries(&path)?;
let paths = children
.keys()
.map(|file_name| Ok(path.join(file_name)))
.collect::<Vec<_>>();
Ok(Box::pin(futures::stream::iter(paths)))
}
async fn watch(
&self,
path: &Path,
_: Duration,
) -> (
Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
Arc<dyn Watcher>,
) {
self.simulate_random_delay().await;
// Zero latency: the deterministic executor doesn't advance time on its
// own, so a real debounce would stall every test until `advance_clock`.
let (events, watcher) = fs_watcher::watch(
self.this.upgrade().unwrap(),
self.native_watcher.clone(),
self.poll_watcher.clone(),
self.executor.clone(),
path,
Duration::ZERO,
)
.await;
self.state
.lock()
.watch_roots
.push((normalize_path(path), Arc::downgrade(&watcher)));
(events, watcher)
}
fn open_repo(
&self,
abs_dot_git: &Path,
_system_git_binary: Option<&Path>,
) -> Result<Arc<dyn GitRepository>> {
self.with_git_state_and_paths(
abs_dot_git,
false,
|_, repository_dir_path, common_dir_path| {
Arc::new(fake_git_repo::FakeGitRepository {
fs: self.this.upgrade().unwrap(),
executor: self.executor.clone(),
dot_git_path: abs_dot_git.to_path_buf(),
repository_dir_path: repository_dir_path.to_owned(),
common_dir_path: common_dir_path.to_owned(),
checkpoints: Arc::default(),
is_trusted: Arc::default(),
}) as _
},
)
}
async fn git_init(
&self,
abs_work_directory_path: &Path,
_fallback_branch_name: String,
) -> Result<()> {
self.create_dir(&abs_work_directory_path.join(".git")).await
}
async fn git_clone(&self, _abs_work_directory: &Path, _repo_url: &str) -> Result<()> {
anyhow::bail!("Git clone is not supported in fake Fs")
}
async fn git_config(&self, _abs_work_directory: &Path, _args: Vec<String>) -> Result<String> {
anyhow::bail!("Git config is not supported in fake Fs")
}
fn record_watcher_diagnostics(&self) -> Option<fs_watcher::WatchRecording> {
Some(fs_watcher::WatchRecording::new([
self.native_watcher.clone(),
self.poll_watcher.clone(),
]))
}
fn path_exists(&self, path: &Path) -> bool {
self.state
.lock()
.try_entry(&normalize_path(path), false)
.is_some()
}
fn is_path_case_sensitive(&self, _path: &Path) -> bool {
self.state.lock().case_sensitive
}
fn requires_poll_watcher(&self, _path: &Path) -> bool {
false
}
fn is_fake(&self) -> bool {
true
}
async fn is_case_sensitive(&self) -> bool {
self.state.lock().case_sensitive
}
fn subscribe_to_jobs(&self) -> JobEventReceiver {
let (sender, receiver) = futures::channel::mpsc::unbounded();
self.state.lock().job_event_subscribers.lock().push(sender);
receiver
}
fn original_path_for_trash_id(&self, trash_id: TrashId) -> Option<PathBuf> {
self.state
.lock()
.trash
.lock()
.get(trash_id)
.map(|(entry, _)| entry.original_parent.join(&entry.name))
}
async fn restore(&self, trash_id: TrashId) -> Result<PathBuf, TrashRestoreError> {
let mut state = self.state.lock();
let Some((trashed_entry, fake_entry)) = state.trash.lock().get(trash_id).cloned() else {
return Err(TrashRestoreError::AlreadyRestored);
};
let path = trashed_entry
.original_parent
.join(trashed_entry.name.clone());
let result = state.write_path(&path, |entry| match entry {
btree_map::Entry::Vacant(entry) => {
entry.insert(fake_entry);
Ok(())
}
btree_map::Entry::Occupied(_) => {
anyhow::bail!("Failed to restore {:?}", path);
}
});
match result {
Ok(_) => {
state.trash.lock().remove(trash_id);
state.emit_event([(path.clone(), Some(PathEventKind::Created))]);
Ok(path)
}
Err(_) => {
// For now we'll just assume that this failed because it was a
// collision error, which I think that, for the time being, is
// the only case where this could fail?
Err(TrashRestoreError::Collision { path })
}
}
}
#[cfg(feature = "test-support")]
fn as_fake(&self) -> Arc<FakeFs> {
self.this.upgrade().unwrap()
}
}
pub async fn copy_recursive<'a>(
fs: &'a dyn Fs,
source: &'a Path,
target: &'a Path,
options: CopyOptions,
) -> Result<()> {
for (item, is_dir) in read_dir_items(fs, source).await? {
let Ok(item_relative_path) = item.strip_prefix(source) else {
continue;
};
let target_item = if item_relative_path == Path::new("") {
target.to_path_buf()
} else {
target.join(item_relative_path)
};
if is_dir {
if !options.overwrite && fs.metadata(&target_item).await.is_ok_and(|m| m.is_some()) {
if options.ignore_if_exists {
continue;
} else {
anyhow::bail!("{target_item:?} already exists");
}
}
let _ = fs
.remove_dir(
&target_item,
RemoveOptions {
recursive: true,
ignore_if_not_exists: true,
},
)
.await;
fs.create_dir(&target_item).await?;
} else {
fs.copy_file(&item, &target_item, options).await?;
}
}
Ok(())
}
/// Recursively reads all of the paths in the given directory.
///
/// Returns a vector of tuples of (path, is_dir).
pub async fn read_dir_items<'a>(fs: &'a dyn Fs, source: &'a Path) -> Result<Vec<(PathBuf, bool)>> {
let mut items = Vec::new();
read_recursive(fs, source, &mut items).await?;
Ok(items)
}
fn read_recursive<'a>(
fs: &'a dyn Fs,
source: &'a Path,
output: &'a mut Vec<(PathBuf, bool)>,
) -> BoxFuture<'a, Result<()>> {
use futures::future::FutureExt;
async move {
let metadata = fs
.metadata(source)
.await?
.with_context(|| format!("path does not exist: {source:?}"))?;
if metadata.is_dir {
output.push((source.to_path_buf(), true));
let mut children = fs.read_dir(source).await?;
while let Some(child_path) = children.next().await {
if let Ok(child_path) = child_path {
read_recursive(fs, &child_path, output).await?;
}
}
} else {
output.push((source.to_path_buf(), false));
}
Ok(())
}
.boxed()
}
// todo(windows)
// can we get file id not open the file twice?
// https://github.com/rust-lang/rust/issues/63010
#[cfg(target_os = "windows")]
async fn file_id(path: impl AsRef<Path>) -> Result<u64> {
use std::os::windows::io::AsRawHandle;
use smol::fs::windows::OpenOptionsExt;
use windows::Win32::{
Foundation::HANDLE,
Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, FILE_FLAG_BACKUP_SEMANTICS, GetFileInformationByHandle,
},
};
let file = smol::fs::OpenOptions::new()
.read(true)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS.0)
.open(path)
.await?;
let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() };
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle
// This function supports Windows XP+
smol::unblock(move || {
unsafe { GetFileInformationByHandle(HANDLE(file.as_raw_handle() as _), &mut info)? };
Ok(((info.nFileIndexHigh as u64) << 32) | (info.nFileIndexLow as u64))
})
.await
}
#[cfg(target_os = "windows")]
fn atomic_replace<P: AsRef<Path>>(
replaced_file: P,
replacement_file: P,
) -> windows::core::Result<()> {
use windows::{
Win32::Storage::FileSystem::{REPLACE_FILE_FLAGS, ReplaceFileW},
core::HSTRING,
};
// If the file does not exist, create it.
let _ = std::fs::File::create_new(replaced_file.as_ref());
unsafe {
ReplaceFileW(
&HSTRING::from(replaced_file.as_ref().to_string_lossy().into_owned()),
&HSTRING::from(replacement_file.as_ref().to_string_lossy().into_owned()),
None,
REPLACE_FILE_FLAGS::default(),
None,
None,
)
}
}