fs.rs

   1pub mod fs_watcher;
   2
   3use parking_lot::Mutex;
   4use std::ffi::OsString;
   5use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
   6use std::time::Instant;
   7use util::maybe;
   8
   9use anyhow::{Context as _, Result, anyhow};
  10use futures::stream::iter;
  11use gpui::App;
  12use gpui::BackgroundExecutor;
  13use gpui::Global;
  14use gpui::ReadGlobal as _;
  15use gpui::SharedString;
  16use std::borrow::Cow;
  17#[cfg(unix)]
  18use std::ffi::CString;
  19use util::command::new_command;
  20
  21#[cfg(unix)]
  22use std::os::fd::{AsFd, AsRawFd};
  23#[cfg(unix)]
  24use std::os::unix::ffi::OsStrExt;
  25
  26#[cfg(unix)]
  27use std::os::unix::fs::{FileTypeExt, MetadataExt};
  28
  29#[cfg(any(target_os = "macos", target_os = "freebsd"))]
  30use std::mem::MaybeUninit;
  31
  32use async_tar::Archive;
  33use futures::{AsyncRead, Stream, StreamExt, future::BoxFuture};
  34use git::repository::{GitRepository, RealGitRepository};
  35use is_executable::IsExecutable;
  36use rope::Rope;
  37use serde::{Deserialize, Serialize};
  38use smol::io::AsyncWriteExt;
  39#[cfg(feature = "test-support")]
  40use std::path::Component;
  41use std::{
  42    io::{self, Write},
  43    path::{Path, PathBuf},
  44    pin::Pin,
  45    sync::Arc,
  46    time::{Duration, SystemTime, UNIX_EPOCH},
  47};
  48use tempfile::TempDir;
  49use text::LineEnding;
  50
  51#[cfg(feature = "test-support")]
  52mod fake_git_repo;
  53#[cfg(feature = "test-support")]
  54use collections::{BTreeMap, btree_map};
  55#[cfg(feature = "test-support")]
  56use fake_git_repo::FakeGitRepositoryState;
  57#[cfg(feature = "test-support")]
  58use git::{
  59    repository::{InitialGraphCommitData, RepoPath, Worktree, repo_path},
  60    status::{FileStatus, StatusCode, TrackedStatus, UnmergedStatus},
  61};
  62#[cfg(feature = "test-support")]
  63use util::normalize_path;
  64
  65#[cfg(feature = "test-support")]
  66use smol::io::AsyncReadExt;
  67#[cfg(feature = "test-support")]
  68use std::ffi::OsStr;
  69
  70pub trait Watcher: Send + Sync {
  71    fn add(&self, path: &Path) -> Result<()>;
  72    fn remove(&self, path: &Path) -> Result<()>;
  73}
  74
  75#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
  76pub enum PathEventKind {
  77    Removed,
  78    Created,
  79    Changed,
  80    Rescan,
  81}
  82
  83#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
  84pub struct PathEvent {
  85    pub path: PathBuf,
  86    pub kind: Option<PathEventKind>,
  87}
  88
  89impl From<PathEvent> for PathBuf {
  90    fn from(event: PathEvent) -> Self {
  91        event.path
  92    }
  93}
  94
  95#[async_trait::async_trait]
  96pub trait Fs: Send + Sync {
  97    async fn create_dir(&self, path: &Path) -> Result<()>;
  98    async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()>;
  99    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()>;
 100    async fn create_file_with(
 101        &self,
 102        path: &Path,
 103        content: Pin<&mut (dyn AsyncRead + Send)>,
 104    ) -> Result<()>;
 105    async fn extract_tar_file(
 106        &self,
 107        path: &Path,
 108        content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
 109    ) -> Result<()>;
 110    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()>;
 111    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()>;
 112
 113    /// Removes a directory from the filesystem.
 114    /// There is no expectation that the directory will be preserved in the
 115    /// system trash.
 116    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()>;
 117
 118    /// Moves a file or directory to the system trash.
 119    /// Returns a [`TrashedEntry`] that can be used to keep track of the
 120    /// location of the trashed item in the system's trash.
 121    async fn trash(&self, path: &Path, options: RemoveOptions) -> Result<TrashedEntry>;
 122
 123    /// Removes a file from the filesystem.
 124    /// There is no expectation that the file will be preserved in the system
 125    /// trash.
 126    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;
 127
 128    async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>>;
 129    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read + Send + Sync>>;
 130    async fn load(&self, path: &Path) -> Result<String> {
 131        Ok(String::from_utf8(self.load_bytes(path).await?)?)
 132    }
 133    async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>>;
 134    async fn atomic_write(&self, path: PathBuf, text: String) -> Result<()>;
 135    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()>;
 136    async fn write(&self, path: &Path, content: &[u8]) -> Result<()>;
 137    async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
 138    async fn is_file(&self, path: &Path) -> bool;
 139    async fn is_dir(&self, path: &Path) -> bool;
 140    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;
 141    async fn read_link(&self, path: &Path) -> Result<PathBuf>;
 142    async fn read_dir(
 143        &self,
 144        path: &Path,
 145    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>>;
 146
 147    async fn watch(
 148        &self,
 149        path: &Path,
 150        latency: Duration,
 151    ) -> (
 152        Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
 153        Arc<dyn Watcher>,
 154    );
 155
 156    fn open_repo(
 157        &self,
 158        abs_dot_git: &Path,
 159        system_git_binary_path: Option<&Path>,
 160    ) -> Result<Arc<dyn GitRepository>>;
 161    async fn git_init(&self, abs_work_directory: &Path, fallback_branch_name: String)
 162    -> Result<()>;
 163    async fn git_clone(&self, repo_url: &str, abs_work_directory: &Path) -> Result<()>;
 164    fn is_fake(&self) -> bool;
 165    async fn is_case_sensitive(&self) -> bool;
 166    fn subscribe_to_jobs(&self) -> JobEventReceiver;
 167
 168    /// Restores a given `TrashedEntry`, moving it from the system's trash back
 169    /// to the original path.
 170    async fn restore(
 171        &self,
 172        trashed_entry: TrashedEntry,
 173    ) -> std::result::Result<PathBuf, TrashRestoreError>;
 174
 175    #[cfg(feature = "test-support")]
 176    fn as_fake(&self) -> Arc<FakeFs> {
 177        panic!("called as_fake on a real fs");
 178    }
 179}
 180
 181// We use our own type rather than `trash::TrashItem` directly to avoid carrying
 182// over fields we don't need (e.g. `time_deleted`) and to insulate callers and
 183// tests from changes to that crate's API surface.
 184/// Represents a file or directory that has been moved to the system trash,
 185/// retaining enough information to restore it to its original location.
 186#[derive(Clone, PartialEq, Debug)]
 187pub struct TrashedEntry {
 188    /// Platform-specific identifier for the file/directory in the trash.
 189    ///
 190    /// * Freedesktop – Path to the `.trashinfo` file.
 191    /// * macOS & Windows – Full path to the file/directory in the system's
 192    /// trash.
 193    pub id: OsString,
 194    /// Name of the file/directory at the time of trashing, including extension.
 195    pub name: OsString,
 196    /// Absolute path to the parent directory at the time of trashing.
 197    pub original_parent: PathBuf,
 198}
 199
 200impl From<trash::TrashItem> for TrashedEntry {
 201    fn from(item: trash::TrashItem) -> Self {
 202        Self {
 203            id: item.id,
 204            name: item.name,
 205            original_parent: item.original_parent,
 206        }
 207    }
 208}
 209
 210impl TrashedEntry {
 211    fn into_trash_item(self) -> trash::TrashItem {
 212        trash::TrashItem {
 213            id: self.id,
 214            name: self.name,
 215            original_parent: self.original_parent,
 216            // `TrashedEntry` doesn't preserve `time_deleted` as we don't
 217            // currently need it for restore, so we default it to 0 here.
 218            time_deleted: 0,
 219        }
 220    }
 221}
 222
 223#[derive(Debug, thiserror::Error)]
 224pub enum TrashRestoreError {
 225    #[error("The specified `path` ({}) was not found in the system's trash.", path.display())]
 226    NotFound { path: PathBuf },
 227    #[error("File or directory ({}) already exists at the restore destination.", path.display())]
 228    Collision { path: PathBuf },
 229    #[error("Unknown error ({description})")]
 230    Unknown { description: String },
 231}
 232
 233impl From<trash::Error> for TrashRestoreError {
 234    fn from(err: trash::Error) -> Self {
 235        match err {
 236            trash::Error::RestoreCollision { path, .. } => Self::Collision { path },
 237            trash::Error::Unknown { description } => Self::Unknown { description },
 238            other => Self::Unknown {
 239                description: other.to_string(),
 240            },
 241        }
 242    }
 243}
 244
 245struct GlobalFs(Arc<dyn Fs>);
 246
 247impl Global for GlobalFs {}
 248
 249impl dyn Fs {
 250    /// Returns the global [`Fs`].
 251    pub fn global(cx: &App) -> Arc<Self> {
 252        GlobalFs::global(cx).0.clone()
 253    }
 254
 255    /// Sets the global [`Fs`].
 256    pub fn set_global(fs: Arc<Self>, cx: &mut App) {
 257        cx.set_global(GlobalFs(fs));
 258    }
 259}
 260
 261#[derive(Copy, Clone, Default)]
 262pub struct CreateOptions {
 263    pub overwrite: bool,
 264    pub ignore_if_exists: bool,
 265}
 266
 267#[derive(Copy, Clone, Default)]
 268pub struct CopyOptions {
 269    pub overwrite: bool,
 270    pub ignore_if_exists: bool,
 271}
 272
 273#[derive(Copy, Clone, Default)]
 274pub struct RenameOptions {
 275    pub overwrite: bool,
 276    pub ignore_if_exists: bool,
 277    /// Whether to create parent directories if they do not exist.
 278    pub create_parents: bool,
 279}
 280
 281#[derive(Copy, Clone, Default)]
 282pub struct RemoveOptions {
 283    pub recursive: bool,
 284    pub ignore_if_not_exists: bool,
 285}
 286
 287#[derive(Copy, Clone, Debug)]
 288pub struct Metadata {
 289    pub inode: u64,
 290    pub mtime: MTime,
 291    pub is_symlink: bool,
 292    pub is_dir: bool,
 293    pub len: u64,
 294    pub is_fifo: bool,
 295    pub is_executable: bool,
 296}
 297
 298/// Filesystem modification time. The purpose of this newtype is to discourage use of operations
 299/// that do not make sense for mtimes. In particular, it is not always valid to compare mtimes using
 300/// `<` or `>`, as there are many things that can cause the mtime of a file to be earlier than it
 301/// was. See ["mtime comparison considered harmful" - apenwarr](https://apenwarr.ca/log/20181113).
 302///
 303/// Do not derive Ord, PartialOrd, or arithmetic operation traits.
 304#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
 305#[serde(transparent)]
 306pub struct MTime(SystemTime);
 307
 308pub type JobId = usize;
 309
 310#[derive(Clone, Debug)]
 311pub struct JobInfo {
 312    pub start: Instant,
 313    pub message: SharedString,
 314    pub id: JobId,
 315}
 316
 317#[derive(Debug, Clone)]
 318pub enum JobEvent {
 319    Started { info: JobInfo },
 320    Completed { id: JobId },
 321}
 322
 323pub type JobEventSender = futures::channel::mpsc::UnboundedSender<JobEvent>;
 324pub type JobEventReceiver = futures::channel::mpsc::UnboundedReceiver<JobEvent>;
 325
 326struct JobTracker {
 327    id: JobId,
 328    subscribers: Arc<Mutex<Vec<JobEventSender>>>,
 329}
 330
 331impl JobTracker {
 332    fn new(info: JobInfo, subscribers: Arc<Mutex<Vec<JobEventSender>>>) -> Self {
 333        let id = info.id;
 334        {
 335            let mut subs = subscribers.lock();
 336            subs.retain(|sender| {
 337                sender
 338                    .unbounded_send(JobEvent::Started { info: info.clone() })
 339                    .is_ok()
 340            });
 341        }
 342        Self { id, subscribers }
 343    }
 344}
 345
 346impl Drop for JobTracker {
 347    fn drop(&mut self) {
 348        let mut subs = self.subscribers.lock();
 349        subs.retain(|sender| {
 350            sender
 351                .unbounded_send(JobEvent::Completed { id: self.id })
 352                .is_ok()
 353        });
 354    }
 355}
 356
 357impl MTime {
 358    /// Conversion intended for persistence and testing.
 359    pub fn from_seconds_and_nanos(secs: u64, nanos: u32) -> Self {
 360        MTime(UNIX_EPOCH + Duration::new(secs, nanos))
 361    }
 362
 363    /// Conversion intended for persistence.
 364    pub fn to_seconds_and_nanos_for_persistence(self) -> Option<(u64, u32)> {
 365        self.0
 366            .duration_since(UNIX_EPOCH)
 367            .ok()
 368            .map(|duration| (duration.as_secs(), duration.subsec_nanos()))
 369    }
 370
 371    /// Returns the value wrapped by this `MTime`, for presentation to the user. The name including
 372    /// "_for_user" is to discourage misuse - this method should not be used when making decisions
 373    /// about file dirtiness.
 374    pub fn timestamp_for_user(self) -> SystemTime {
 375        self.0
 376    }
 377
 378    /// Temporary method to split out the behavior changes from introduction of this newtype.
 379    pub fn bad_is_greater_than(self, other: MTime) -> bool {
 380        self.0 > other.0
 381    }
 382}
 383
 384impl From<proto::Timestamp> for MTime {
 385    fn from(timestamp: proto::Timestamp) -> Self {
 386        MTime(timestamp.into())
 387    }
 388}
 389
 390impl From<MTime> for proto::Timestamp {
 391    fn from(mtime: MTime) -> Self {
 392        mtime.0.into()
 393    }
 394}
 395
 396pub struct RealFs {
 397    bundled_git_binary_path: Option<PathBuf>,
 398    executor: BackgroundExecutor,
 399    next_job_id: Arc<AtomicUsize>,
 400    job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,
 401    is_case_sensitive: AtomicU8,
 402}
 403
 404pub trait FileHandle: Send + Sync + std::fmt::Debug {
 405    fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf>;
 406}
 407
 408impl FileHandle for std::fs::File {
 409    #[cfg(target_os = "macos")]
 410    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
 411        use std::{
 412            ffi::{CStr, OsStr},
 413            os::unix::ffi::OsStrExt,
 414        };
 415
 416        let fd = self.as_fd();
 417        let mut path_buf = MaybeUninit::<[u8; libc::PATH_MAX as usize]>::uninit();
 418
 419        let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETPATH, path_buf.as_mut_ptr()) };
 420        anyhow::ensure!(result != -1, "fcntl returned -1");
 421
 422        // SAFETY: `fcntl` will initialize the path buffer.
 423        let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr().cast()) };
 424        anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");
 425        let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
 426        Ok(path)
 427    }
 428
 429    #[cfg(target_os = "linux")]
 430    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
 431        let fd = self.as_fd();
 432        let fd_path = format!("/proc/self/fd/{}", fd.as_raw_fd());
 433        let new_path = std::fs::read_link(fd_path)?;
 434        if new_path
 435            .file_name()
 436            .is_some_and(|f| f.to_string_lossy().ends_with(" (deleted)"))
 437        {
 438            anyhow::bail!("file was deleted")
 439        };
 440
 441        Ok(new_path)
 442    }
 443
 444    #[cfg(target_os = "freebsd")]
 445    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
 446        use std::{
 447            ffi::{CStr, OsStr},
 448            os::unix::ffi::OsStrExt,
 449        };
 450
 451        let fd = self.as_fd();
 452        let mut kif = MaybeUninit::<libc::kinfo_file>::uninit();
 453        kif.kf_structsize = libc::KINFO_FILE_SIZE;
 454
 455        let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_KINFO, kif.as_mut_ptr()) };
 456        anyhow::ensure!(result != -1, "fcntl returned -1");
 457
 458        // SAFETY: `fcntl` will initialize the kif.
 459        let c_str = unsafe { CStr::from_ptr(kif.assume_init().kf_path.as_ptr()) };
 460        anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");
 461        let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
 462        Ok(path)
 463    }
 464
 465    #[cfg(target_os = "windows")]
 466    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
 467        use std::ffi::OsString;
 468        use std::os::windows::ffi::OsStringExt;
 469        use std::os::windows::io::AsRawHandle;
 470
 471        use windows::Win32::Foundation::HANDLE;
 472        use windows::Win32::Storage::FileSystem::{
 473            FILE_NAME_NORMALIZED, GetFinalPathNameByHandleW,
 474        };
 475
 476        let handle = HANDLE(self.as_raw_handle() as _);
 477
 478        // Query required buffer size (in wide chars)
 479        let required_len =
 480            unsafe { GetFinalPathNameByHandleW(handle, &mut [], FILE_NAME_NORMALIZED) };
 481        anyhow::ensure!(
 482            required_len != 0,
 483            "GetFinalPathNameByHandleW returned 0 length"
 484        );
 485
 486        // Allocate buffer and retrieve the path
 487        let mut buf: Vec<u16> = vec![0u16; required_len as usize + 1];
 488        let written = unsafe { GetFinalPathNameByHandleW(handle, &mut buf, FILE_NAME_NORMALIZED) };
 489        anyhow::ensure!(
 490            written != 0,
 491            "GetFinalPathNameByHandleW failed to write path"
 492        );
 493
 494        let os_str: OsString = OsString::from_wide(&buf[..written as usize]);
 495        anyhow::ensure!(!os_str.is_empty(), "Could find a path for the file handle");
 496        Ok(PathBuf::from(os_str))
 497    }
 498}
 499
 500pub struct RealWatcher {}
 501
 502impl RealFs {
 503    pub fn new(git_binary_path: Option<PathBuf>, executor: BackgroundExecutor) -> Self {
 504        Self {
 505            bundled_git_binary_path: git_binary_path,
 506            executor,
 507            next_job_id: Arc::new(AtomicUsize::new(0)),
 508            job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
 509            is_case_sensitive: Default::default(),
 510        }
 511    }
 512
 513    #[cfg(target_os = "windows")]
 514    fn canonicalize(path: &Path) -> Result<PathBuf> {
 515        use std::ffi::OsString;
 516        use std::os::windows::ffi::OsStringExt;
 517        use windows::Win32::Storage::FileSystem::GetVolumePathNameW;
 518        use windows::core::HSTRING;
 519
 520        // std::fs::canonicalize resolves mapped network paths to UNC paths, which can
 521        // confuse some software. To mitigate this, we canonicalize the input, then rebase
 522        // the result onto the input's original volume root if both paths are on the same
 523        // volume. This keeps the same drive letter or mount point the caller used.
 524
 525        let abs_path = if path.is_relative() {
 526            std::env::current_dir()?.join(path)
 527        } else {
 528            path.to_path_buf()
 529        };
 530
 531        let path_hstring = HSTRING::from(abs_path.as_os_str());
 532        let mut vol_buf = vec![0u16; abs_path.as_os_str().len() + 2];
 533        unsafe { GetVolumePathNameW(&path_hstring, &mut vol_buf)? };
 534        let volume_root = {
 535            let len = vol_buf
 536                .iter()
 537                .position(|&c| c == 0)
 538                .unwrap_or(vol_buf.len());
 539            PathBuf::from(OsString::from_wide(&vol_buf[..len]))
 540        };
 541
 542        let resolved_path = dunce::canonicalize(&abs_path)?;
 543        let resolved_root = dunce::canonicalize(&volume_root)?;
 544
 545        if let Ok(relative) = resolved_path.strip_prefix(&resolved_root) {
 546            let mut result = volume_root;
 547            result.push(relative);
 548            Ok(result)
 549        } else {
 550            Ok(resolved_path)
 551        }
 552    }
 553}
 554
 555#[cfg(any(target_os = "macos", target_os = "linux"))]
 556fn rename_without_replace(source: &Path, target: &Path) -> io::Result<()> {
 557    let source = path_to_c_string(source)?;
 558    let target = path_to_c_string(target)?;
 559
 560    #[cfg(target_os = "macos")]
 561    let result = unsafe { libc::renamex_np(source.as_ptr(), target.as_ptr(), libc::RENAME_EXCL) };
 562
 563    #[cfg(target_os = "linux")]
 564    let result = unsafe {
 565        libc::syscall(
 566            libc::SYS_renameat2,
 567            libc::AT_FDCWD,
 568            source.as_ptr(),
 569            libc::AT_FDCWD,
 570            target.as_ptr(),
 571            libc::RENAME_NOREPLACE,
 572        )
 573    };
 574
 575    if result == 0 {
 576        Ok(())
 577    } else {
 578        Err(io::Error::last_os_error())
 579    }
 580}
 581
 582#[cfg(target_os = "windows")]
 583fn rename_without_replace(source: &Path, target: &Path) -> io::Result<()> {
 584    use std::os::windows::ffi::OsStrExt;
 585
 586    use windows::Win32::Storage::FileSystem::{MOVE_FILE_FLAGS, MoveFileExW};
 587    use windows::core::PCWSTR;
 588
 589    let source: Vec<u16> = source.as_os_str().encode_wide().chain(Some(0)).collect();
 590    let target: Vec<u16> = target.as_os_str().encode_wide().chain(Some(0)).collect();
 591
 592    unsafe {
 593        MoveFileExW(
 594            PCWSTR(source.as_ptr()),
 595            PCWSTR(target.as_ptr()),
 596            MOVE_FILE_FLAGS::default(),
 597        )
 598    }
 599    .map_err(|_| io::Error::last_os_error())
 600}
 601
 602#[cfg(any(target_os = "macos", target_os = "linux"))]
 603fn path_to_c_string(path: &Path) -> io::Result<CString> {
 604    CString::new(path.as_os_str().as_bytes()).map_err(|_| {
 605        io::Error::new(
 606            io::ErrorKind::InvalidInput,
 607            format!("path contains interior NUL: {}", path.display()),
 608        )
 609    })
 610}
 611
 612#[async_trait::async_trait]
 613impl Fs for RealFs {
 614    async fn create_dir(&self, path: &Path) -> Result<()> {
 615        Ok(smol::fs::create_dir_all(path).await?)
 616    }
 617
 618    async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()> {
 619        #[cfg(unix)]
 620        smol::fs::unix::symlink(target, path).await?;
 621
 622        #[cfg(windows)]
 623        if smol::fs::metadata(&target).await?.is_dir() {
 624            let status = new_command("cmd")
 625                .args(["/C", "mklink", "/J"])
 626                .args([path, target.as_path()])
 627                .status()
 628                .await?;
 629
 630            if !status.success() {
 631                return Err(anyhow::anyhow!(
 632                    "Failed to create junction from {:?} to {:?}",
 633                    path,
 634                    target
 635                ));
 636            }
 637        } else {
 638            smol::fs::windows::symlink_file(target, path).await?
 639        }
 640
 641        Ok(())
 642    }
 643
 644    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
 645        let mut open_options = smol::fs::OpenOptions::new();
 646        open_options.write(true).create(true);
 647        if options.overwrite {
 648            open_options.truncate(true);
 649        } else if !options.ignore_if_exists {
 650            open_options.create_new(true);
 651        }
 652        open_options
 653            .open(path)
 654            .await
 655            .with_context(|| format!("Failed to create file at {:?}", path))?;
 656        Ok(())
 657    }
 658
 659    async fn create_file_with(
 660        &self,
 661        path: &Path,
 662        content: Pin<&mut (dyn AsyncRead + Send)>,
 663    ) -> Result<()> {
 664        let mut file = smol::fs::File::create(&path)
 665            .await
 666            .with_context(|| format!("Failed to create file at {:?}", path))?;
 667        futures::io::copy(content, &mut file).await?;
 668        Ok(())
 669    }
 670
 671    async fn extract_tar_file(
 672        &self,
 673        path: &Path,
 674        content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
 675    ) -> Result<()> {
 676        content.unpack(path).await?;
 677        Ok(())
 678    }
 679
 680    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
 681        if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
 682            if options.ignore_if_exists {
 683                return Ok(());
 684            } else {
 685                anyhow::bail!("{target:?} already exists");
 686            }
 687        }
 688
 689        smol::fs::copy(source, target).await?;
 690        Ok(())
 691    }
 692
 693    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()> {
 694        if options.create_parents {
 695            if let Some(parent) = target.parent() {
 696                self.create_dir(parent).await?;
 697            }
 698        }
 699
 700        if options.overwrite {
 701            smol::fs::rename(source, target).await?;
 702            return Ok(());
 703        }
 704
 705        let use_metadata_fallback = {
 706            #[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
 707            {
 708                let source = source.to_path_buf();
 709                let target = target.to_path_buf();
 710                match self
 711                    .executor
 712                    .spawn(async move { rename_without_replace(&source, &target) })
 713                    .await
 714                {
 715                    Ok(()) => return Ok(()),
 716                    Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
 717                        if options.ignore_if_exists {
 718                            return Ok(());
 719                        }
 720                        return Err(error.into());
 721                    }
 722                    Err(error)
 723                        if error.raw_os_error().is_some_and(|code| {
 724                            code == libc::ENOSYS
 725                                || code == libc::ENOTSUP
 726                                || code == libc::EOPNOTSUPP
 727                                || code == libc::EINVAL
 728                        }) =>
 729                    {
 730                        // For case when filesystem or kernel does not support atomic no-overwrite rename.
 731                        // EINVAL is returned by FUSE-based filesystems (e.g. NTFS via ntfs-3g)
 732                        // that don't support RENAME_NOREPLACE.
 733                        true
 734                    }
 735                    Err(error) => return Err(error.into()),
 736                }
 737            }
 738
 739            #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
 740            {
 741                // For platforms which do not have an atomic no-overwrite rename yet.
 742                true
 743            }
 744        };
 745
 746        if use_metadata_fallback && smol::fs::metadata(target).await.is_ok() {
 747            if options.ignore_if_exists {
 748                return Ok(());
 749            } else {
 750                anyhow::bail!("{target:?} already exists");
 751            }
 752        }
 753
 754        smol::fs::rename(source, target).await?;
 755        Ok(())
 756    }
 757
 758    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 759        let result = if options.recursive {
 760            smol::fs::remove_dir_all(path).await
 761        } else {
 762            smol::fs::remove_dir(path).await
 763        };
 764        match result {
 765            Ok(()) => Ok(()),
 766            Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
 767                Ok(())
 768            }
 769            Err(err) => Err(err)?,
 770        }
 771    }
 772
 773    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 774        #[cfg(windows)]
 775        if let Ok(Some(metadata)) = self.metadata(path).await
 776            && metadata.is_symlink
 777            && metadata.is_dir
 778        {
 779            self.remove_dir(
 780                path,
 781                RemoveOptions {
 782                    recursive: false,
 783                    ignore_if_not_exists: true,
 784                },
 785            )
 786            .await?;
 787            return Ok(());
 788        }
 789
 790        match smol::fs::remove_file(path).await {
 791            Ok(()) => Ok(()),
 792            Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
 793                Ok(())
 794            }
 795            Err(err) => Err(err)?,
 796        }
 797    }
 798
 799    async fn trash(&self, path: &Path, _options: RemoveOptions) -> Result<TrashedEntry> {
 800        // We must make the path absolute or trash will make a weird abomination
 801        // of the zed working directory (not usually the worktree) and whatever
 802        // the path variable holds.
 803        let path = self
 804            .canonicalize(path)
 805            .await
 806            .context("Could not canonicalize the path of the file")?;
 807
 808        let (tx, rx) = futures::channel::oneshot::channel();
 809        std::thread::Builder::new()
 810            .name("trash file or dir".to_string())
 811            .spawn(|| tx.send(trash::delete_with_info(path)))
 812            .expect("The os can spawn threads");
 813
 814        Ok(rx
 815            .await
 816            .context("Tx dropped or fs.restore panicked")?
 817            .context("Could not trash file or dir")?
 818            .into())
 819    }
 820
 821    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read + Send + Sync>> {
 822        Ok(Box::new(std::fs::File::open(path)?))
 823    }
 824
 825    async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>> {
 826        let mut options = std::fs::OpenOptions::new();
 827        options.read(true);
 828        #[cfg(windows)]
 829        {
 830            use std::os::windows::fs::OpenOptionsExt;
 831            options.custom_flags(windows::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS.0);
 832        }
 833        Ok(Arc::new(options.open(path)?))
 834    }
 835
 836    async fn load(&self, path: &Path) -> Result<String> {
 837        let path = path.to_path_buf();
 838        self.executor
 839            .spawn(async move {
 840                std::fs::read_to_string(&path)
 841                    .with_context(|| format!("Failed to read file {}", path.display()))
 842            })
 843            .await
 844    }
 845
 846    async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>> {
 847        let path = path.to_path_buf();
 848        let bytes = self
 849            .executor
 850            .spawn(async move { std::fs::read(path) })
 851            .await?;
 852        Ok(bytes)
 853    }
 854
 855    #[cfg(not(target_os = "windows"))]
 856    async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
 857        smol::unblock(move || {
 858            // Use the directory of the destination as temp dir to avoid
 859            // invalid cross-device link error, and XDG_CACHE_DIR for fallback.
 860            // See https://github.com/zed-industries/zed/pull/8437 for more details.
 861            let mut tmp_file =
 862                tempfile::NamedTempFile::new_in(path.parent().unwrap_or(paths::temp_dir()))?;
 863            tmp_file.write_all(data.as_bytes())?;
 864            tmp_file.persist(path)?;
 865            anyhow::Ok(())
 866        })
 867        .await?;
 868
 869        Ok(())
 870    }
 871
 872    #[cfg(target_os = "windows")]
 873    async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
 874        smol::unblock(move || {
 875            // If temp dir is set to a different drive than the destination,
 876            // we receive error:
 877            //
 878            // failed to persist temporary file:
 879            // The system cannot move the file to a different disk drive. (os error 17)
 880            //
 881            // This is because `ReplaceFileW` does not support cross volume moves.
 882            // See the remark section: "The backup file, replaced file, and replacement file must all reside on the same volume."
 883            // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-replacefilew#remarks
 884            //
 885            // So we use the directory of the destination as a temp dir to avoid it.
 886            // https://github.com/zed-industries/zed/issues/16571
 887            let temp_dir = TempDir::new_in(path.parent().unwrap_or(paths::temp_dir()))?;
 888            let temp_file = {
 889                let temp_file_path = temp_dir.path().join("temp_file");
 890                let mut file = std::fs::File::create_new(&temp_file_path)?;
 891                file.write_all(data.as_bytes())?;
 892                temp_file_path
 893            };
 894            atomic_replace(path.as_path(), temp_file.as_path())?;
 895            anyhow::Ok(())
 896        })
 897        .await?;
 898        Ok(())
 899    }
 900
 901    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
 902        let buffer_size = text.summary().len.min(10 * 1024);
 903        if let Some(path) = path.parent() {
 904            self.create_dir(path)
 905                .await
 906                .with_context(|| format!("Failed to create directory at {:?}", path))?;
 907        }
 908        let file = smol::fs::File::create(path)
 909            .await
 910            .with_context(|| format!("Failed to create file at {:?}", path))?;
 911        let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
 912        for chunk in text::chunks_with_line_ending(text, line_ending) {
 913            writer.write_all(chunk.as_bytes()).await?;
 914        }
 915        writer.flush().await?;
 916        Ok(())
 917    }
 918
 919    async fn write(&self, path: &Path, content: &[u8]) -> Result<()> {
 920        if let Some(path) = path.parent() {
 921            self.create_dir(path)
 922                .await
 923                .with_context(|| format!("Failed to create directory at {:?}", path))?;
 924        }
 925        let path = path.to_owned();
 926        let contents = content.to_owned();
 927        self.executor
 928            .spawn(async move {
 929                std::fs::write(path, contents)?;
 930                Ok(())
 931            })
 932            .await
 933    }
 934
 935    async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
 936        let path = path.to_owned();
 937        self.executor
 938            .spawn(async move {
 939                #[cfg(target_os = "windows")]
 940                let result = Self::canonicalize(&path);
 941
 942                #[cfg(not(target_os = "windows"))]
 943                let result = std::fs::canonicalize(&path);
 944
 945                result.with_context(|| format!("canonicalizing {path:?}"))
 946            })
 947            .await
 948    }
 949
 950    async fn is_file(&self, path: &Path) -> bool {
 951        let path = path.to_owned();
 952        self.executor
 953            .spawn(async move { std::fs::metadata(path).is_ok_and(|metadata| metadata.is_file()) })
 954            .await
 955    }
 956
 957    async fn is_dir(&self, path: &Path) -> bool {
 958        let path = path.to_owned();
 959        self.executor
 960            .spawn(async move { std::fs::metadata(path).is_ok_and(|metadata| metadata.is_dir()) })
 961            .await
 962    }
 963
 964    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
 965        let path_buf = path.to_owned();
 966        let symlink_metadata = match self
 967            .executor
 968            .spawn(async move { std::fs::symlink_metadata(&path_buf) })
 969            .await
 970        {
 971            Ok(metadata) => metadata,
 972            Err(err) => {
 973                return match err.kind() {
 974                    io::ErrorKind::NotFound | io::ErrorKind::NotADirectory => Ok(None),
 975                    _ => Err(anyhow::Error::new(err)),
 976                };
 977            }
 978        };
 979
 980        let is_symlink = symlink_metadata.file_type().is_symlink();
 981        let metadata = if is_symlink {
 982            let path_buf = path.to_path_buf();
 983            // Read target metadata, if the target exists
 984            match self
 985                .executor
 986                .spawn(async move { std::fs::metadata(path_buf) })
 987                .await
 988            {
 989                Ok(target_metadata) => target_metadata,
 990                Err(err) => {
 991                    if err.kind() != io::ErrorKind::NotFound {
 992                        // TODO: Also FilesystemLoop when that's stable
 993                        log::warn!(
 994                            "Failed to read symlink target metadata for path {path:?}: {err}"
 995                        );
 996                    }
 997                    // For a broken or recursive symlink, return the symlink metadata. (Or
 998                    // as edge cases, a symlink into a directory we can't read, which is hard
 999                    // to distinguish from just being broken.)
1000                    symlink_metadata
1001                }
1002            }
1003        } else {
1004            symlink_metadata
1005        };
1006
1007        #[cfg(unix)]
1008        let inode = metadata.ino();
1009
1010        #[cfg(windows)]
1011        let inode = file_id(path).await?;
1012
1013        #[cfg(windows)]
1014        let is_fifo = false;
1015
1016        #[cfg(unix)]
1017        let is_fifo = metadata.file_type().is_fifo();
1018
1019        let path_buf = path.to_path_buf();
1020        let is_executable = self
1021            .executor
1022            .spawn(async move { path_buf.is_executable() })
1023            .await;
1024
1025        Ok(Some(Metadata {
1026            inode,
1027            mtime: MTime(metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH)),
1028            len: metadata.len(),
1029            is_symlink,
1030            is_dir: metadata.file_type().is_dir(),
1031            is_fifo,
1032            is_executable,
1033        }))
1034    }
1035
1036    async fn read_link(&self, path: &Path) -> Result<PathBuf> {
1037        let path = path.to_owned();
1038        let path = self
1039            .executor
1040            .spawn(async move { std::fs::read_link(&path) })
1041            .await?;
1042        Ok(path)
1043    }
1044
1045    async fn read_dir(
1046        &self,
1047        path: &Path,
1048    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
1049        let path = path.to_owned();
1050        let result = iter(
1051            self.executor
1052                .spawn(async move { std::fs::read_dir(path) })
1053                .await?,
1054        )
1055        .map(|entry| match entry {
1056            Ok(entry) => Ok(entry.path()),
1057            Err(error) => Err(anyhow!("failed to read dir entry {error:?}")),
1058        });
1059        Ok(Box::pin(result))
1060    }
1061
1062    async fn watch(
1063        &self,
1064        path: &Path,
1065        latency: Duration,
1066    ) -> (
1067        Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
1068        Arc<dyn Watcher>,
1069    ) {
1070        use util::{ResultExt as _, paths::SanitizedPath};
1071        let executor = self.executor.clone();
1072
1073        let (tx, rx) = smol::channel::unbounded();
1074        let pending_paths: Arc<Mutex<Vec<PathEvent>>> = Default::default();
1075        let watcher = Arc::new(fs_watcher::FsWatcher::new(tx, pending_paths.clone()));
1076
1077        // If the path doesn't exist yet (e.g. settings.json), watch the parent dir to learn when it's created.
1078        if let Err(e) = watcher.add(path)
1079            && let Some(parent) = path.parent()
1080            && let Err(parent_e) = watcher.add(parent)
1081        {
1082            log::warn!(
1083                "Failed to watch {} and its parent directory {}:\n{e}\n{parent_e}",
1084                path.display(),
1085                parent.display()
1086            );
1087        }
1088
1089        // Check if path is a symlink and follow the target parent
1090        if let Some(mut target) = self.read_link(path).await.ok() {
1091            log::trace!("watch symlink {path:?} -> {target:?}");
1092            // Check if symlink target is relative path, if so make it absolute
1093            if target.is_relative()
1094                && let Some(parent) = path.parent()
1095            {
1096                target = parent.join(target);
1097                if let Ok(canonical) = self.canonicalize(&target).await {
1098                    target = SanitizedPath::new(&canonical).as_path().to_path_buf();
1099                }
1100            }
1101            watcher.add(&target).ok();
1102            if let Some(parent) = target.parent() {
1103                watcher.add(parent).log_err();
1104            }
1105        }
1106
1107        (
1108            Box::pin(rx.filter_map({
1109                let watcher = watcher.clone();
1110                let executor = executor.clone();
1111                move |_| {
1112                    let _ = watcher.clone();
1113                    let pending_paths = pending_paths.clone();
1114                    let executor = executor.clone();
1115                    async move {
1116                        executor.timer(latency).await;
1117                        let paths = std::mem::take(&mut *pending_paths.lock());
1118                        (!paths.is_empty()).then_some(paths)
1119                    }
1120                }
1121            })),
1122            watcher,
1123        )
1124    }
1125
1126    fn open_repo(
1127        &self,
1128        dotgit_path: &Path,
1129        system_git_binary_path: Option<&Path>,
1130    ) -> Result<Arc<dyn GitRepository>> {
1131        Ok(Arc::new(RealGitRepository::new(
1132            dotgit_path,
1133            self.bundled_git_binary_path.clone(),
1134            system_git_binary_path.map(|path| path.to_path_buf()),
1135            self.executor.clone(),
1136        )?))
1137    }
1138
1139    async fn git_init(
1140        &self,
1141        abs_work_directory_path: &Path,
1142        fallback_branch_name: String,
1143    ) -> Result<()> {
1144        let config = new_command("git")
1145            .current_dir(abs_work_directory_path)
1146            .args(&["config", "--global", "--get", "init.defaultBranch"])
1147            .output()
1148            .await?;
1149
1150        let branch_name;
1151
1152        if config.status.success() && !config.stdout.is_empty() {
1153            branch_name = String::from_utf8_lossy(&config.stdout);
1154        } else {
1155            branch_name = Cow::Borrowed(fallback_branch_name.as_str());
1156        }
1157
1158        new_command("git")
1159            .current_dir(abs_work_directory_path)
1160            .args(&["init", "-b"])
1161            .arg(branch_name.trim())
1162            .output()
1163            .await?;
1164
1165        Ok(())
1166    }
1167
1168    async fn git_clone(&self, repo_url: &str, abs_work_directory: &Path) -> Result<()> {
1169        let job_id = self.next_job_id.fetch_add(1, Ordering::SeqCst);
1170        let job_info = JobInfo {
1171            id: job_id,
1172            start: Instant::now(),
1173            message: SharedString::from(format!("Cloning {}", repo_url)),
1174        };
1175
1176        let _job_tracker = JobTracker::new(job_info, self.job_event_subscribers.clone());
1177
1178        let output = new_command("git")
1179            .current_dir(abs_work_directory)
1180            .args(&["clone", repo_url])
1181            .output()
1182            .await?;
1183
1184        if !output.status.success() {
1185            anyhow::bail!(
1186                "git clone failed: {}",
1187                String::from_utf8_lossy(&output.stderr)
1188            );
1189        }
1190
1191        Ok(())
1192    }
1193
1194    fn is_fake(&self) -> bool {
1195        false
1196    }
1197
1198    fn subscribe_to_jobs(&self) -> JobEventReceiver {
1199        let (sender, receiver) = futures::channel::mpsc::unbounded();
1200        self.job_event_subscribers.lock().push(sender);
1201        receiver
1202    }
1203
1204    /// Checks whether the file system is case sensitive by attempting to create two files
1205    /// that have the same name except for the casing.
1206    ///
1207    /// It creates both files in a temporary directory it removes at the end.
1208    async fn is_case_sensitive(&self) -> bool {
1209        const UNINITIALIZED: u8 = 0;
1210        const CASE_SENSITIVE: u8 = 1;
1211        const NOT_CASE_SENSITIVE: u8 = 2;
1212
1213        // Note we could CAS here, but really, if we race we do this work twice at worst which isn't a big deal.
1214        let load = self.is_case_sensitive.load(Ordering::Acquire);
1215        if load != UNINITIALIZED {
1216            return load == CASE_SENSITIVE;
1217        }
1218        let temp_dir = self.executor.spawn(async { TempDir::new() });
1219        let res = maybe!(async {
1220            let temp_dir = temp_dir.await?;
1221            let test_file_1 = temp_dir.path().join("case_sensitivity_test.tmp");
1222            let test_file_2 = temp_dir.path().join("CASE_SENSITIVITY_TEST.TMP");
1223
1224            let create_opts = CreateOptions {
1225                overwrite: false,
1226                ignore_if_exists: false,
1227            };
1228
1229            // Create file1
1230            self.create_file(&test_file_1, create_opts).await?;
1231
1232            // Now check whether it's possible to create file2
1233            let case_sensitive = match self.create_file(&test_file_2, create_opts).await {
1234                Ok(_) => Ok(true),
1235                Err(e) => {
1236                    if let Some(io_error) = e.downcast_ref::<io::Error>() {
1237                        if io_error.kind() == io::ErrorKind::AlreadyExists {
1238                            Ok(false)
1239                        } else {
1240                            Err(e)
1241                        }
1242                    } else {
1243                        Err(e)
1244                    }
1245                }
1246            };
1247
1248            temp_dir.close()?;
1249            case_sensitive
1250        }).await.unwrap_or_else(|e| {
1251            log::error!(
1252                "Failed to determine whether filesystem is case sensitive (falling back to true) due to error: {e:#}"
1253            );
1254            true
1255        });
1256        self.is_case_sensitive.store(
1257            if res {
1258                CASE_SENSITIVE
1259            } else {
1260                NOT_CASE_SENSITIVE
1261            },
1262            Ordering::Release,
1263        );
1264        res
1265    }
1266
1267    async fn restore(
1268        &self,
1269        trashed_entry: TrashedEntry,
1270    ) -> std::result::Result<PathBuf, TrashRestoreError> {
1271        let restored_item_path = trashed_entry.original_parent.join(&trashed_entry.name);
1272
1273        let (tx, rx) = futures::channel::oneshot::channel();
1274        std::thread::Builder::new()
1275            .name("restore trashed item".to_string())
1276            .spawn(move || {
1277                let res = trash::restore_all([trashed_entry.into_trash_item()]);
1278                tx.send(res)
1279            })
1280            .expect("The OS can spawn a threads");
1281        rx.await.expect("Restore all never panics")?;
1282        Ok(restored_item_path)
1283    }
1284}
1285
1286#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
1287impl Watcher for RealWatcher {
1288    fn add(&self, _: &Path) -> Result<()> {
1289        Ok(())
1290    }
1291
1292    fn remove(&self, _: &Path) -> Result<()> {
1293        Ok(())
1294    }
1295}
1296
1297#[cfg(feature = "test-support")]
1298pub struct FakeFs {
1299    this: std::sync::Weak<Self>,
1300    // Use an unfair lock to ensure tests are deterministic.
1301    state: Arc<Mutex<FakeFsState>>,
1302    executor: gpui::BackgroundExecutor,
1303}
1304
1305#[cfg(feature = "test-support")]
1306struct FakeFsState {
1307    root: FakeFsEntry,
1308    next_inode: u64,
1309    next_mtime: SystemTime,
1310    git_event_tx: smol::channel::Sender<PathBuf>,
1311    event_txs: Vec<(PathBuf, smol::channel::Sender<Vec<PathEvent>>)>,
1312    events_paused: bool,
1313    buffered_events: Vec<PathEvent>,
1314    metadata_call_count: usize,
1315    read_dir_call_count: usize,
1316    path_write_counts: std::collections::HashMap<PathBuf, usize>,
1317    moves: std::collections::HashMap<u64, PathBuf>,
1318    job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,
1319    trash: Vec<(TrashedEntry, FakeFsEntry)>,
1320}
1321
1322#[cfg(feature = "test-support")]
1323#[derive(Clone, Debug)]
1324enum FakeFsEntry {
1325    File {
1326        inode: u64,
1327        mtime: MTime,
1328        len: u64,
1329        content: Vec<u8>,
1330        // The path to the repository state directory, if this is a gitfile.
1331        git_dir_path: Option<PathBuf>,
1332    },
1333    Dir {
1334        inode: u64,
1335        mtime: MTime,
1336        len: u64,
1337        entries: BTreeMap<String, FakeFsEntry>,
1338        git_repo_state: Option<Arc<Mutex<FakeGitRepositoryState>>>,
1339    },
1340    Symlink {
1341        target: PathBuf,
1342    },
1343}
1344
1345#[cfg(feature = "test-support")]
1346impl PartialEq for FakeFsEntry {
1347    fn eq(&self, other: &Self) -> bool {
1348        match (self, other) {
1349            (
1350                Self::File {
1351                    inode: l_inode,
1352                    mtime: l_mtime,
1353                    len: l_len,
1354                    content: l_content,
1355                    git_dir_path: l_git_dir_path,
1356                },
1357                Self::File {
1358                    inode: r_inode,
1359                    mtime: r_mtime,
1360                    len: r_len,
1361                    content: r_content,
1362                    git_dir_path: r_git_dir_path,
1363                },
1364            ) => {
1365                l_inode == r_inode
1366                    && l_mtime == r_mtime
1367                    && l_len == r_len
1368                    && l_content == r_content
1369                    && l_git_dir_path == r_git_dir_path
1370            }
1371            (
1372                Self::Dir {
1373                    inode: l_inode,
1374                    mtime: l_mtime,
1375                    len: l_len,
1376                    entries: l_entries,
1377                    git_repo_state: l_git_repo_state,
1378                },
1379                Self::Dir {
1380                    inode: r_inode,
1381                    mtime: r_mtime,
1382                    len: r_len,
1383                    entries: r_entries,
1384                    git_repo_state: r_git_repo_state,
1385                },
1386            ) => {
1387                let same_repo_state = match (l_git_repo_state.as_ref(), r_git_repo_state.as_ref()) {
1388                    (Some(l), Some(r)) => Arc::ptr_eq(l, r),
1389                    (None, None) => true,
1390                    _ => false,
1391                };
1392                l_inode == r_inode
1393                    && l_mtime == r_mtime
1394                    && l_len == r_len
1395                    && l_entries == r_entries
1396                    && same_repo_state
1397            }
1398            (Self::Symlink { target: l_target }, Self::Symlink { target: r_target }) => {
1399                l_target == r_target
1400            }
1401            _ => false,
1402        }
1403    }
1404}
1405
1406#[cfg(feature = "test-support")]
1407impl FakeFsState {
1408    fn get_and_increment_mtime(&mut self) -> MTime {
1409        let mtime = self.next_mtime;
1410        self.next_mtime += FakeFs::SYSTEMTIME_INTERVAL;
1411        MTime(mtime)
1412    }
1413
1414    fn get_and_increment_inode(&mut self) -> u64 {
1415        let inode = self.next_inode;
1416        self.next_inode += 1;
1417        inode
1418    }
1419
1420    fn canonicalize(&self, target: &Path, follow_symlink: bool) -> Option<PathBuf> {
1421        let mut canonical_path = PathBuf::new();
1422        let mut path = target.to_path_buf();
1423        let mut entry_stack = Vec::new();
1424        'outer: loop {
1425            let mut path_components = path.components().peekable();
1426            let mut prefix = None;
1427            while let Some(component) = path_components.next() {
1428                match component {
1429                    Component::Prefix(prefix_component) => prefix = Some(prefix_component),
1430                    Component::RootDir => {
1431                        entry_stack.clear();
1432                        entry_stack.push(&self.root);
1433                        canonical_path.clear();
1434                        match prefix {
1435                            Some(prefix_component) => {
1436                                canonical_path = PathBuf::from(prefix_component.as_os_str());
1437                                // Prefixes like `C:\\` are represented without their trailing slash, so we have to re-add it.
1438                                canonical_path.push(std::path::MAIN_SEPARATOR_STR);
1439                            }
1440                            None => canonical_path = PathBuf::from(std::path::MAIN_SEPARATOR_STR),
1441                        }
1442                    }
1443                    Component::CurDir => {}
1444                    Component::ParentDir => {
1445                        entry_stack.pop()?;
1446                        canonical_path.pop();
1447                    }
1448                    Component::Normal(name) => {
1449                        let current_entry = *entry_stack.last()?;
1450                        if let FakeFsEntry::Dir { entries, .. } = current_entry {
1451                            let entry = entries.get(name.to_str().unwrap())?;
1452                            if (path_components.peek().is_some() || follow_symlink)
1453                                && let FakeFsEntry::Symlink { target, .. } = entry
1454                            {
1455                                let mut target = target.clone();
1456                                target.extend(path_components);
1457                                path = target;
1458                                continue 'outer;
1459                            }
1460                            entry_stack.push(entry);
1461                            canonical_path = canonical_path.join(name);
1462                        } else {
1463                            return None;
1464                        }
1465                    }
1466                }
1467            }
1468            break;
1469        }
1470
1471        if entry_stack.is_empty() {
1472            None
1473        } else {
1474            Some(canonical_path)
1475        }
1476    }
1477
1478    fn try_entry(
1479        &mut self,
1480        target: &Path,
1481        follow_symlink: bool,
1482    ) -> Option<(&mut FakeFsEntry, PathBuf)> {
1483        let canonical_path = self.canonicalize(target, follow_symlink)?;
1484
1485        let mut components = canonical_path
1486            .components()
1487            .skip_while(|component| matches!(component, Component::Prefix(_)));
1488        let Some(Component::RootDir) = components.next() else {
1489            panic!(
1490                "the path {:?} was not canonicalized properly {:?}",
1491                target, canonical_path
1492            )
1493        };
1494
1495        let mut entry = &mut self.root;
1496        for component in components {
1497            match component {
1498                Component::Normal(name) => {
1499                    if let FakeFsEntry::Dir { entries, .. } = entry {
1500                        entry = entries.get_mut(name.to_str().unwrap())?;
1501                    } else {
1502                        return None;
1503                    }
1504                }
1505                _ => {
1506                    panic!(
1507                        "the path {:?} was not canonicalized properly {:?}",
1508                        target, canonical_path
1509                    )
1510                }
1511            }
1512        }
1513
1514        Some((entry, canonical_path))
1515    }
1516
1517    fn entry(&mut self, target: &Path) -> Result<&mut FakeFsEntry> {
1518        Ok(self
1519            .try_entry(target, true)
1520            .ok_or_else(|| {
1521                anyhow!(io::Error::new(
1522                    io::ErrorKind::NotFound,
1523                    format!("not found: {target:?}")
1524                ))
1525            })?
1526            .0)
1527    }
1528
1529    fn write_path<Fn, T>(&mut self, path: &Path, callback: Fn) -> Result<T>
1530    where
1531        Fn: FnOnce(btree_map::Entry<String, FakeFsEntry>) -> Result<T>,
1532    {
1533        let path = normalize_path(path);
1534        let filename = path.file_name().context("cannot overwrite the root")?;
1535        let parent_path = path.parent().unwrap();
1536
1537        let parent = self.entry(parent_path)?;
1538        let new_entry = parent
1539            .dir_entries(parent_path)?
1540            .entry(filename.to_str().unwrap().into());
1541        callback(new_entry)
1542    }
1543
1544    fn emit_event<I, T>(&mut self, paths: I)
1545    where
1546        I: IntoIterator<Item = (T, Option<PathEventKind>)>,
1547        T: Into<PathBuf>,
1548    {
1549        self.buffered_events
1550            .extend(paths.into_iter().map(|(path, kind)| PathEvent {
1551                path: path.into(),
1552                kind,
1553            }));
1554
1555        if !self.events_paused {
1556            self.flush_events(self.buffered_events.len());
1557        }
1558    }
1559
1560    fn flush_events(&mut self, mut count: usize) {
1561        count = count.min(self.buffered_events.len());
1562        let events = self.buffered_events.drain(0..count).collect::<Vec<_>>();
1563        self.event_txs.retain(|(_, tx)| {
1564            let _ = tx.try_send(events.clone());
1565            !tx.is_closed()
1566        });
1567    }
1568}
1569
1570#[cfg(feature = "test-support")]
1571pub static FS_DOT_GIT: std::sync::LazyLock<&'static OsStr> =
1572    std::sync::LazyLock::new(|| OsStr::new(".git"));
1573
1574#[cfg(feature = "test-support")]
1575impl FakeFs {
1576    /// We need to use something large enough for Windows and Unix to consider this a new file.
1577    /// https://doc.rust-lang.org/nightly/std/time/struct.SystemTime.html#platform-specific-behavior
1578    const SYSTEMTIME_INTERVAL: Duration = Duration::from_nanos(100);
1579
1580    pub fn new(executor: gpui::BackgroundExecutor) -> Arc<Self> {
1581        let (tx, rx) = smol::channel::bounded::<PathBuf>(10);
1582
1583        let this = Arc::new_cyclic(|this| Self {
1584            this: this.clone(),
1585            executor: executor.clone(),
1586            state: Arc::new(Mutex::new(FakeFsState {
1587                root: FakeFsEntry::Dir {
1588                    inode: 0,
1589                    mtime: MTime(UNIX_EPOCH),
1590                    len: 0,
1591                    entries: Default::default(),
1592                    git_repo_state: None,
1593                },
1594                git_event_tx: tx,
1595                next_mtime: UNIX_EPOCH + Self::SYSTEMTIME_INTERVAL,
1596                next_inode: 1,
1597                event_txs: Default::default(),
1598                buffered_events: Vec::new(),
1599                events_paused: false,
1600                read_dir_call_count: 0,
1601                metadata_call_count: 0,
1602                path_write_counts: Default::default(),
1603                moves: Default::default(),
1604                job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
1605                trash: Vec::new(),
1606            })),
1607        });
1608
1609        executor.spawn({
1610            let this = this.clone();
1611            async move {
1612                while let Ok(git_event) = rx.recv().await {
1613                    if let Some(mut state) = this.state.try_lock() {
1614                        state.emit_event([(git_event, Some(PathEventKind::Changed))]);
1615                    } else {
1616                        panic!("Failed to lock file system state, this execution would have caused a test hang");
1617                    }
1618                }
1619            }
1620        }).detach();
1621
1622        this
1623    }
1624
1625    pub fn set_next_mtime(&self, next_mtime: SystemTime) {
1626        let mut state = self.state.lock();
1627        state.next_mtime = next_mtime;
1628    }
1629
1630    pub fn get_and_increment_mtime(&self) -> MTime {
1631        let mut state = self.state.lock();
1632        state.get_and_increment_mtime()
1633    }
1634
1635    pub async fn touch_path(&self, path: impl AsRef<Path>) {
1636        let mut state = self.state.lock();
1637        let path = path.as_ref();
1638        let new_mtime = state.get_and_increment_mtime();
1639        let new_inode = state.get_and_increment_inode();
1640        state
1641            .write_path(path, move |entry| {
1642                match entry {
1643                    btree_map::Entry::Vacant(e) => {
1644                        e.insert(FakeFsEntry::File {
1645                            inode: new_inode,
1646                            mtime: new_mtime,
1647                            content: Vec::new(),
1648                            len: 0,
1649                            git_dir_path: None,
1650                        });
1651                    }
1652                    btree_map::Entry::Occupied(mut e) => match &mut *e.get_mut() {
1653                        FakeFsEntry::File { mtime, .. } => *mtime = new_mtime,
1654                        FakeFsEntry::Dir { mtime, .. } => *mtime = new_mtime,
1655                        FakeFsEntry::Symlink { .. } => {}
1656                    },
1657                }
1658                Ok(())
1659            })
1660            .unwrap();
1661        state.emit_event([(path.to_path_buf(), Some(PathEventKind::Changed))]);
1662    }
1663
1664    pub async fn insert_file(&self, path: impl AsRef<Path>, content: Vec<u8>) {
1665        self.write_file_internal(path, content, true).unwrap()
1666    }
1667
1668    pub async fn insert_symlink(&self, path: impl AsRef<Path>, target: PathBuf) {
1669        let mut state = self.state.lock();
1670        let path = path.as_ref();
1671        let file = FakeFsEntry::Symlink { target };
1672        state
1673            .write_path(path.as_ref(), move |e| match e {
1674                btree_map::Entry::Vacant(e) => {
1675                    e.insert(file);
1676                    Ok(())
1677                }
1678                btree_map::Entry::Occupied(mut e) => {
1679                    *e.get_mut() = file;
1680                    Ok(())
1681                }
1682            })
1683            .unwrap();
1684        state.emit_event([(path, Some(PathEventKind::Created))]);
1685    }
1686
1687    fn write_file_internal(
1688        &self,
1689        path: impl AsRef<Path>,
1690        new_content: Vec<u8>,
1691        recreate_inode: bool,
1692    ) -> Result<()> {
1693        fn inner(
1694            this: &FakeFs,
1695            path: &Path,
1696            new_content: Vec<u8>,
1697            recreate_inode: bool,
1698        ) -> Result<()> {
1699            let mut state = this.state.lock();
1700            let path_buf = path.to_path_buf();
1701            *state.path_write_counts.entry(path_buf).or_insert(0) += 1;
1702            let new_inode = state.get_and_increment_inode();
1703            let new_mtime = state.get_and_increment_mtime();
1704            let new_len = new_content.len() as u64;
1705            let mut kind = None;
1706            state.write_path(path, |entry| {
1707                match entry {
1708                    btree_map::Entry::Vacant(e) => {
1709                        kind = Some(PathEventKind::Created);
1710                        e.insert(FakeFsEntry::File {
1711                            inode: new_inode,
1712                            mtime: new_mtime,
1713                            len: new_len,
1714                            content: new_content,
1715                            git_dir_path: None,
1716                        });
1717                    }
1718                    btree_map::Entry::Occupied(mut e) => {
1719                        kind = Some(PathEventKind::Changed);
1720                        if let FakeFsEntry::File {
1721                            inode,
1722                            mtime,
1723                            len,
1724                            content,
1725                            ..
1726                        } = e.get_mut()
1727                        {
1728                            *mtime = new_mtime;
1729                            *content = new_content;
1730                            *len = new_len;
1731                            if recreate_inode {
1732                                *inode = new_inode;
1733                            }
1734                        } else {
1735                            anyhow::bail!("not a file")
1736                        }
1737                    }
1738                }
1739                Ok(())
1740            })?;
1741            state.emit_event([(path, kind)]);
1742            Ok(())
1743        }
1744        inner(self, path.as_ref(), new_content, recreate_inode)
1745    }
1746
1747    pub fn read_file_sync(&self, path: impl AsRef<Path>) -> Result<Vec<u8>> {
1748        let path = path.as_ref();
1749        let path = normalize_path(path);
1750        let mut state = self.state.lock();
1751        let entry = state.entry(&path)?;
1752        entry.file_content(&path).cloned()
1753    }
1754
1755    async fn load_internal(&self, path: impl AsRef<Path>) -> Result<Vec<u8>> {
1756        let path = path.as_ref();
1757        let path = normalize_path(path);
1758        self.simulate_random_delay().await;
1759        let mut state = self.state.lock();
1760        let entry = state.entry(&path)?;
1761        entry.file_content(&path).cloned()
1762    }
1763
1764    pub fn pause_events(&self) {
1765        self.state.lock().events_paused = true;
1766    }
1767
1768    pub fn unpause_events_and_flush(&self) {
1769        self.state.lock().events_paused = false;
1770        self.flush_events(usize::MAX);
1771    }
1772
1773    pub fn buffered_event_count(&self) -> usize {
1774        self.state.lock().buffered_events.len()
1775    }
1776
1777    pub fn clear_buffered_events(&self) {
1778        self.state.lock().buffered_events.clear();
1779    }
1780
1781    pub fn flush_events(&self, count: usize) {
1782        self.state.lock().flush_events(count);
1783    }
1784
1785    pub(crate) fn entry(&self, target: &Path) -> Result<FakeFsEntry> {
1786        self.state.lock().entry(target).cloned()
1787    }
1788
1789    pub(crate) fn insert_entry(&self, target: &Path, new_entry: FakeFsEntry) -> Result<()> {
1790        let mut state = self.state.lock();
1791        state.write_path(target, |entry| {
1792            match entry {
1793                btree_map::Entry::Vacant(vacant_entry) => {
1794                    vacant_entry.insert(new_entry);
1795                }
1796                btree_map::Entry::Occupied(mut occupied_entry) => {
1797                    occupied_entry.insert(new_entry);
1798                }
1799            }
1800            Ok(())
1801        })
1802    }
1803
1804    #[must_use]
1805    pub fn insert_tree<'a>(
1806        &'a self,
1807        path: impl 'a + AsRef<Path> + Send,
1808        tree: serde_json::Value,
1809    ) -> futures::future::BoxFuture<'a, ()> {
1810        use futures::FutureExt as _;
1811        use serde_json::Value::*;
1812
1813        fn inner<'a>(
1814            this: &'a FakeFs,
1815            path: Arc<Path>,
1816            tree: serde_json::Value,
1817        ) -> futures::future::BoxFuture<'a, ()> {
1818            async move {
1819                match tree {
1820                    Object(map) => {
1821                        this.create_dir(&path).await.unwrap();
1822                        for (name, contents) in map {
1823                            let mut path = PathBuf::from(path.as_ref());
1824                            path.push(name);
1825                            this.insert_tree(&path, contents).await;
1826                        }
1827                    }
1828                    Null => {
1829                        this.create_dir(&path).await.unwrap();
1830                    }
1831                    String(contents) => {
1832                        this.insert_file(&path, contents.into_bytes()).await;
1833                    }
1834                    _ => {
1835                        panic!("JSON object must contain only objects, strings, or null");
1836                    }
1837                }
1838            }
1839            .boxed()
1840        }
1841        inner(self, Arc::from(path.as_ref()), tree)
1842    }
1843
1844    pub fn insert_tree_from_real_fs<'a>(
1845        &'a self,
1846        path: impl 'a + AsRef<Path> + Send,
1847        src_path: impl 'a + AsRef<Path> + Send,
1848    ) -> futures::future::BoxFuture<'a, ()> {
1849        use futures::FutureExt as _;
1850
1851        async move {
1852            let path = path.as_ref();
1853            if std::fs::metadata(&src_path).unwrap().is_file() {
1854                let contents = std::fs::read(src_path).unwrap();
1855                self.insert_file(path, contents).await;
1856            } else {
1857                self.create_dir(path).await.unwrap();
1858                for entry in std::fs::read_dir(&src_path).unwrap() {
1859                    let entry = entry.unwrap();
1860                    self.insert_tree_from_real_fs(path.join(entry.file_name()), entry.path())
1861                        .await;
1862                }
1863            }
1864        }
1865        .boxed()
1866    }
1867
1868    pub fn with_git_state_and_paths<T, F>(
1869        &self,
1870        dot_git: &Path,
1871        emit_git_event: bool,
1872        f: F,
1873    ) -> Result<T>
1874    where
1875        F: FnOnce(&mut FakeGitRepositoryState, &Path, &Path) -> T,
1876    {
1877        let mut state = self.state.lock();
1878        let git_event_tx = state.git_event_tx.clone();
1879        let entry = state.entry(dot_git).context("open .git")?;
1880
1881        if let FakeFsEntry::Dir { git_repo_state, .. } = entry {
1882            let repo_state = git_repo_state.get_or_insert_with(|| {
1883                log::debug!("insert git state for {dot_git:?}");
1884                Arc::new(Mutex::new(FakeGitRepositoryState::new(git_event_tx)))
1885            });
1886            let mut repo_state = repo_state.lock();
1887
1888            let result = f(&mut repo_state, dot_git, dot_git);
1889
1890            drop(repo_state);
1891            if emit_git_event {
1892                state.emit_event([(
1893                    dot_git.join("fake_git_repo_event"),
1894                    Some(PathEventKind::Changed),
1895                )]);
1896            }
1897
1898            Ok(result)
1899        } else if let FakeFsEntry::File {
1900            content,
1901            git_dir_path,
1902            ..
1903        } = &mut *entry
1904        {
1905            let path = match git_dir_path {
1906                Some(path) => path,
1907                None => {
1908                    let path = std::str::from_utf8(content)
1909                        .ok()
1910                        .and_then(|content| content.strip_prefix("gitdir:"))
1911                        .context("not a valid gitfile")?
1912                        .trim();
1913                    git_dir_path.insert(normalize_path(&dot_git.parent().unwrap().join(path)))
1914                }
1915            }
1916            .clone();
1917            let Some((git_dir_entry, canonical_path)) = state.try_entry(&path, true) else {
1918                anyhow::bail!("pointed-to git dir {path:?} not found")
1919            };
1920            let FakeFsEntry::Dir {
1921                git_repo_state,
1922                entries,
1923                ..
1924            } = git_dir_entry
1925            else {
1926                anyhow::bail!("gitfile points to a non-directory")
1927            };
1928            let common_dir = if let Some(child) = entries.get("commondir") {
1929                let raw = std::str::from_utf8(child.file_content("commondir".as_ref())?)
1930                    .context("commondir content")?
1931                    .trim();
1932                let raw_path = Path::new(raw);
1933                if raw_path.is_relative() {
1934                    normalize_path(&canonical_path.join(raw_path))
1935                } else {
1936                    raw_path.to_owned()
1937                }
1938            } else {
1939                canonical_path.clone()
1940            };
1941            let repo_state = git_repo_state.get_or_insert_with(|| {
1942                Arc::new(Mutex::new(FakeGitRepositoryState::new(git_event_tx)))
1943            });
1944            let mut repo_state = repo_state.lock();
1945
1946            let result = f(&mut repo_state, &canonical_path, &common_dir);
1947
1948            if emit_git_event {
1949                drop(repo_state);
1950                state.emit_event([(
1951                    canonical_path.join("fake_git_repo_event"),
1952                    Some(PathEventKind::Changed),
1953                )]);
1954            }
1955
1956            Ok(result)
1957        } else {
1958            anyhow::bail!("not a valid git repository");
1959        }
1960    }
1961
1962    pub fn with_git_state<T, F>(&self, dot_git: &Path, emit_git_event: bool, f: F) -> Result<T>
1963    where
1964        F: FnOnce(&mut FakeGitRepositoryState) -> T,
1965    {
1966        self.with_git_state_and_paths(dot_git, emit_git_event, |state, _, _| f(state))
1967    }
1968
1969    pub fn set_branch_name(&self, dot_git: &Path, branch: Option<impl Into<String>>) {
1970        self.with_git_state(dot_git, true, |state| {
1971            let branch = branch.map(Into::into);
1972            state.branches.extend(branch.clone());
1973            state.current_branch_name = branch
1974        })
1975        .unwrap();
1976    }
1977
1978    pub fn set_remote_for_repo(
1979        &self,
1980        dot_git: &Path,
1981        name: impl Into<String>,
1982        url: impl Into<String>,
1983    ) {
1984        self.with_git_state(dot_git, true, |state| {
1985            state.remotes.insert(name.into(), url.into());
1986        })
1987        .unwrap();
1988    }
1989
1990    pub fn insert_branches(&self, dot_git: &Path, branches: &[&str]) {
1991        self.with_git_state(dot_git, true, |state| {
1992            if let Some(first) = branches.first()
1993                && state.current_branch_name.is_none()
1994            {
1995                state.current_branch_name = Some(first.to_string())
1996            }
1997            state
1998                .branches
1999                .extend(branches.iter().map(ToString::to_string));
2000        })
2001        .unwrap();
2002    }
2003
2004    pub async fn add_linked_worktree_for_repo(
2005        &self,
2006        dot_git: &Path,
2007        emit_git_event: bool,
2008        worktree: Worktree,
2009    ) {
2010        let ref_name = worktree
2011            .ref_name
2012            .as_ref()
2013            .expect("linked worktree must have a ref_name");
2014        let branch_name = ref_name
2015            .strip_prefix("refs/heads/")
2016            .unwrap_or(ref_name.as_ref());
2017
2018        // Create ref in git state.
2019        self.with_git_state(dot_git, false, |state| {
2020            state
2021                .refs
2022                .insert(ref_name.to_string(), worktree.sha.to_string());
2023        })
2024        .unwrap();
2025
2026        // Create .git/worktrees/<name>/ directory with HEAD, commondir, and gitdir.
2027        let worktrees_entry_dir = dot_git.join("worktrees").join(branch_name);
2028        self.create_dir(&worktrees_entry_dir).await.unwrap();
2029
2030        self.write_file_internal(
2031            worktrees_entry_dir.join("HEAD"),
2032            format!("ref: {ref_name}").into_bytes(),
2033            false,
2034        )
2035        .unwrap();
2036
2037        self.write_file_internal(
2038            worktrees_entry_dir.join("commondir"),
2039            dot_git.to_string_lossy().into_owned().into_bytes(),
2040            false,
2041        )
2042        .unwrap();
2043
2044        let worktree_dot_git = worktree.path.join(".git");
2045        self.write_file_internal(
2046            worktrees_entry_dir.join("gitdir"),
2047            worktree_dot_git.to_string_lossy().into_owned().into_bytes(),
2048            false,
2049        )
2050        .unwrap();
2051
2052        // Create the worktree checkout directory with a .git file pointing back.
2053        self.create_dir(&worktree.path).await.unwrap();
2054
2055        self.write_file_internal(
2056            &worktree_dot_git,
2057            format!("gitdir: {}", worktrees_entry_dir.display()).into_bytes(),
2058            false,
2059        )
2060        .unwrap();
2061
2062        if emit_git_event {
2063            self.with_git_state(dot_git, true, |_| {}).unwrap();
2064        }
2065    }
2066
2067    pub async fn remove_worktree_for_repo(
2068        &self,
2069        dot_git: &Path,
2070        emit_git_event: bool,
2071        ref_name: &str,
2072    ) {
2073        let branch_name = ref_name.strip_prefix("refs/heads/").unwrap_or(ref_name);
2074        let worktrees_entry_dir = dot_git.join("worktrees").join(branch_name);
2075
2076        // Read gitdir to find the worktree checkout path.
2077        let gitdir_content = self
2078            .load_internal(worktrees_entry_dir.join("gitdir"))
2079            .await
2080            .unwrap();
2081        let gitdir_str = String::from_utf8(gitdir_content).unwrap();
2082        let worktree_path = PathBuf::from(gitdir_str.trim())
2083            .parent()
2084            .map(PathBuf::from)
2085            .unwrap_or_default();
2086
2087        // Remove the worktree checkout directory.
2088        self.remove_dir(
2089            &worktree_path,
2090            RemoveOptions {
2091                recursive: true,
2092                ignore_if_not_exists: true,
2093            },
2094        )
2095        .await
2096        .unwrap();
2097
2098        // Remove the .git/worktrees/<name>/ directory.
2099        self.remove_dir(
2100            &worktrees_entry_dir,
2101            RemoveOptions {
2102                recursive: true,
2103                ignore_if_not_exists: false,
2104            },
2105        )
2106        .await
2107        .unwrap();
2108
2109        if emit_git_event {
2110            self.with_git_state(dot_git, true, |_| {}).unwrap();
2111        }
2112    }
2113
2114    pub fn set_unmerged_paths_for_repo(
2115        &self,
2116        dot_git: &Path,
2117        unmerged_state: &[(RepoPath, UnmergedStatus)],
2118    ) {
2119        self.with_git_state(dot_git, true, |state| {
2120            state.unmerged_paths.clear();
2121            state.unmerged_paths.extend(
2122                unmerged_state
2123                    .iter()
2124                    .map(|(path, content)| (path.clone(), *content)),
2125            );
2126        })
2127        .unwrap();
2128    }
2129
2130    pub fn set_index_for_repo(&self, dot_git: &Path, index_state: &[(&str, String)]) {
2131        self.with_git_state(dot_git, true, |state| {
2132            state.index_contents.clear();
2133            state.index_contents.extend(
2134                index_state
2135                    .iter()
2136                    .map(|(path, content)| (repo_path(path), content.clone())),
2137            );
2138        })
2139        .unwrap();
2140    }
2141
2142    pub fn set_head_for_repo(
2143        &self,
2144        dot_git: &Path,
2145        head_state: &[(&str, String)],
2146        sha: impl Into<String>,
2147    ) {
2148        self.with_git_state(dot_git, true, |state| {
2149            state.head_contents.clear();
2150            state.head_contents.extend(
2151                head_state
2152                    .iter()
2153                    .map(|(path, content)| (repo_path(path), content.clone())),
2154            );
2155            state.refs.insert("HEAD".into(), sha.into());
2156        })
2157        .unwrap();
2158    }
2159
2160    pub fn set_head_and_index_for_repo(&self, dot_git: &Path, contents_by_path: &[(&str, String)]) {
2161        self.with_git_state(dot_git, true, |state| {
2162            state.head_contents.clear();
2163            state.head_contents.extend(
2164                contents_by_path
2165                    .iter()
2166                    .map(|(path, contents)| (repo_path(path), contents.clone())),
2167            );
2168            state.index_contents = state.head_contents.clone();
2169        })
2170        .unwrap();
2171    }
2172
2173    pub fn set_merge_base_content_for_repo(
2174        &self,
2175        dot_git: &Path,
2176        contents_by_path: &[(&str, String)],
2177    ) {
2178        self.with_git_state(dot_git, true, |state| {
2179            use git::Oid;
2180
2181            state.merge_base_contents.clear();
2182            let oids = (1..)
2183                .map(|n| n.to_string())
2184                .map(|n| Oid::from_bytes(n.repeat(20).as_bytes()).unwrap());
2185            for ((path, content), oid) in contents_by_path.iter().zip(oids) {
2186                state.merge_base_contents.insert(repo_path(path), oid);
2187                state.oids.insert(oid, content.clone());
2188            }
2189        })
2190        .unwrap();
2191    }
2192
2193    pub fn set_blame_for_repo(&self, dot_git: &Path, blames: Vec<(RepoPath, git::blame::Blame)>) {
2194        self.with_git_state(dot_git, true, |state| {
2195            state.blames.clear();
2196            state.blames.extend(blames);
2197        })
2198        .unwrap();
2199    }
2200
2201    pub fn set_graph_commits(&self, dot_git: &Path, commits: Vec<Arc<InitialGraphCommitData>>) {
2202        self.with_git_state(dot_git, true, |state| {
2203            state.graph_commits = commits;
2204        })
2205        .unwrap();
2206    }
2207
2208    pub fn set_graph_error(&self, dot_git: &Path, error: Option<String>) {
2209        self.with_git_state(dot_git, true, |state| {
2210            state.simulated_graph_error = error;
2211        })
2212        .unwrap();
2213    }
2214
2215    /// Put the given git repository into a state with the given status,
2216    /// by mutating the head, index, and unmerged state.
2217    pub fn set_status_for_repo(&self, dot_git: &Path, statuses: &[(&str, FileStatus)]) {
2218        let workdir_path = dot_git.parent().unwrap();
2219        let workdir_contents = self.files_with_contents(workdir_path);
2220        self.with_git_state(dot_git, true, |state| {
2221            state.index_contents.clear();
2222            state.head_contents.clear();
2223            state.unmerged_paths.clear();
2224            for (path, content) in workdir_contents {
2225                use util::{paths::PathStyle, rel_path::RelPath};
2226
2227                let repo_path = RelPath::new(path.strip_prefix(&workdir_path).unwrap(), PathStyle::local()).unwrap();
2228                let repo_path = RepoPath::from_rel_path(&repo_path);
2229                let status = statuses
2230                    .iter()
2231                    .find_map(|(p, status)| (*p == repo_path.as_unix_str()).then_some(status));
2232                let mut content = String::from_utf8_lossy(&content).to_string();
2233
2234                let mut index_content = None;
2235                let mut head_content = None;
2236                match status {
2237                    None => {
2238                        index_content = Some(content.clone());
2239                        head_content = Some(content);
2240                    }
2241                    Some(FileStatus::Untracked | FileStatus::Ignored) => {}
2242                    Some(FileStatus::Unmerged(unmerged_status)) => {
2243                        state
2244                            .unmerged_paths
2245                            .insert(repo_path.clone(), *unmerged_status);
2246                        content.push_str(" (unmerged)");
2247                        index_content = Some(content.clone());
2248                        head_content = Some(content);
2249                    }
2250                    Some(FileStatus::Tracked(TrackedStatus {
2251                        index_status,
2252                        worktree_status,
2253                    })) => {
2254                        match worktree_status {
2255                            StatusCode::Modified => {
2256                                let mut content = content.clone();
2257                                content.push_str(" (modified in working copy)");
2258                                index_content = Some(content);
2259                            }
2260                            StatusCode::TypeChanged | StatusCode::Unmodified => {
2261                                index_content = Some(content.clone());
2262                            }
2263                            StatusCode::Added => {}
2264                            StatusCode::Deleted | StatusCode::Renamed | StatusCode::Copied => {
2265                                panic!("cannot create these statuses for an existing file");
2266                            }
2267                        };
2268                        match index_status {
2269                            StatusCode::Modified => {
2270                                let mut content = index_content.clone().expect(
2271                                    "file cannot be both modified in index and created in working copy",
2272                                );
2273                                content.push_str(" (modified in index)");
2274                                head_content = Some(content);
2275                            }
2276                            StatusCode::TypeChanged | StatusCode::Unmodified => {
2277                                head_content = Some(index_content.clone().expect("file cannot be both unmodified in index and created in working copy"));
2278                            }
2279                            StatusCode::Added => {}
2280                            StatusCode::Deleted  => {
2281                                head_content = Some("".into());
2282                            }
2283                            StatusCode::Renamed | StatusCode::Copied => {
2284                                panic!("cannot create these statuses for an existing file");
2285                            }
2286                        };
2287                    }
2288                };
2289
2290                if let Some(content) = index_content {
2291                    state.index_contents.insert(repo_path.clone(), content);
2292                }
2293                if let Some(content) = head_content {
2294                    state.head_contents.insert(repo_path.clone(), content);
2295                }
2296            }
2297        }).unwrap();
2298    }
2299
2300    pub fn set_error_message_for_index_write(&self, dot_git: &Path, message: Option<String>) {
2301        self.with_git_state(dot_git, true, |state| {
2302            state.simulated_index_write_error_message = message;
2303        })
2304        .unwrap();
2305    }
2306
2307    pub fn set_create_worktree_error(&self, dot_git: &Path, message: Option<String>) {
2308        self.with_git_state(dot_git, true, |state| {
2309            state.simulated_create_worktree_error = message;
2310        })
2311        .unwrap();
2312    }
2313
2314    pub fn paths(&self, include_dot_git: bool) -> Vec<PathBuf> {
2315        let mut result = Vec::new();
2316        let mut queue = collections::VecDeque::new();
2317        let state = &*self.state.lock();
2318        queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2319        while let Some((path, entry)) = queue.pop_front() {
2320            if let FakeFsEntry::Dir { entries, .. } = entry {
2321                for (name, entry) in entries {
2322                    queue.push_back((path.join(name), entry));
2323                }
2324            }
2325            if include_dot_git
2326                || !path
2327                    .components()
2328                    .any(|component| component.as_os_str() == *FS_DOT_GIT)
2329            {
2330                result.push(path);
2331            }
2332        }
2333        result
2334    }
2335
2336    pub fn directories(&self, include_dot_git: bool) -> Vec<PathBuf> {
2337        let mut result = Vec::new();
2338        let mut queue = collections::VecDeque::new();
2339        let state = &*self.state.lock();
2340        queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2341        while let Some((path, entry)) = queue.pop_front() {
2342            if let FakeFsEntry::Dir { entries, .. } = entry {
2343                for (name, entry) in entries {
2344                    queue.push_back((path.join(name), entry));
2345                }
2346                if include_dot_git
2347                    || !path
2348                        .components()
2349                        .any(|component| component.as_os_str() == *FS_DOT_GIT)
2350                {
2351                    result.push(path);
2352                }
2353            }
2354        }
2355        result
2356    }
2357
2358    pub fn files(&self) -> Vec<PathBuf> {
2359        let mut result = Vec::new();
2360        let mut queue = collections::VecDeque::new();
2361        let state = &*self.state.lock();
2362        queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2363        while let Some((path, entry)) = queue.pop_front() {
2364            match entry {
2365                FakeFsEntry::File { .. } => result.push(path),
2366                FakeFsEntry::Dir { entries, .. } => {
2367                    for (name, entry) in entries {
2368                        queue.push_back((path.join(name), entry));
2369                    }
2370                }
2371                FakeFsEntry::Symlink { .. } => {}
2372            }
2373        }
2374        result
2375    }
2376
2377    pub fn files_with_contents(&self, prefix: &Path) -> Vec<(PathBuf, Vec<u8>)> {
2378        let mut result = Vec::new();
2379        let mut queue = collections::VecDeque::new();
2380        let state = &*self.state.lock();
2381        queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2382        while let Some((path, entry)) = queue.pop_front() {
2383            match entry {
2384                FakeFsEntry::File { content, .. } => {
2385                    if path.starts_with(prefix) {
2386                        result.push((path, content.clone()));
2387                    }
2388                }
2389                FakeFsEntry::Dir { entries, .. } => {
2390                    for (name, entry) in entries {
2391                        queue.push_back((path.join(name), entry));
2392                    }
2393                }
2394                FakeFsEntry::Symlink { .. } => {}
2395            }
2396        }
2397        result
2398    }
2399
2400    /// How many `read_dir` calls have been issued.
2401    pub fn read_dir_call_count(&self) -> usize {
2402        self.state.lock().read_dir_call_count
2403    }
2404
2405    pub fn watched_paths(&self) -> Vec<PathBuf> {
2406        let state = self.state.lock();
2407        state
2408            .event_txs
2409            .iter()
2410            .filter_map(|(path, tx)| Some(path.clone()).filter(|_| !tx.is_closed()))
2411            .collect()
2412    }
2413
2414    /// How many `metadata` calls have been issued.
2415    pub fn metadata_call_count(&self) -> usize {
2416        self.state.lock().metadata_call_count
2417    }
2418
2419    /// How many write operations have been issued for a specific path.
2420    pub fn write_count_for_path(&self, path: impl AsRef<Path>) -> usize {
2421        let path = path.as_ref().to_path_buf();
2422        self.state
2423            .lock()
2424            .path_write_counts
2425            .get(&path)
2426            .copied()
2427            .unwrap_or(0)
2428    }
2429
2430    pub fn emit_fs_event(&self, path: impl Into<PathBuf>, event: Option<PathEventKind>) {
2431        self.state.lock().emit_event(std::iter::once((path, event)));
2432    }
2433
2434    fn simulate_random_delay(&self) -> impl futures::Future<Output = ()> {
2435        self.executor.simulate_random_delay()
2436    }
2437
2438    /// Returns list of all tracked trash entries.
2439    pub fn trash_entries(&self) -> Vec<TrashedEntry> {
2440        self.state
2441            .lock()
2442            .trash
2443            .iter()
2444            .map(|(entry, _)| entry.clone())
2445            .collect()
2446    }
2447
2448    async fn remove_dir_inner(
2449        &self,
2450        path: &Path,
2451        options: RemoveOptions,
2452    ) -> Result<Option<FakeFsEntry>> {
2453        self.simulate_random_delay().await;
2454
2455        let path = normalize_path(path);
2456        let parent_path = path.parent().context("cannot remove the root")?;
2457        let base_name = path.file_name().context("cannot remove the root")?;
2458
2459        let mut state = self.state.lock();
2460        let parent_entry = state.entry(parent_path)?;
2461        let entry = parent_entry
2462            .dir_entries(parent_path)?
2463            .entry(base_name.to_str().unwrap().into());
2464
2465        let removed = match entry {
2466            btree_map::Entry::Vacant(_) => {
2467                if !options.ignore_if_not_exists {
2468                    anyhow::bail!("{path:?} does not exist");
2469                }
2470
2471                None
2472            }
2473            btree_map::Entry::Occupied(mut entry) => {
2474                {
2475                    let children = entry.get_mut().dir_entries(&path)?;
2476                    if !options.recursive && !children.is_empty() {
2477                        anyhow::bail!("{path:?} is not empty");
2478                    }
2479                }
2480
2481                Some(entry.remove())
2482            }
2483        };
2484
2485        state.emit_event([(path, Some(PathEventKind::Removed))]);
2486        Ok(removed)
2487    }
2488
2489    async fn remove_file_inner(
2490        &self,
2491        path: &Path,
2492        options: RemoveOptions,
2493    ) -> Result<Option<FakeFsEntry>> {
2494        self.simulate_random_delay().await;
2495
2496        let path = normalize_path(path);
2497        let parent_path = path.parent().context("cannot remove the root")?;
2498        let base_name = path.file_name().unwrap();
2499        let mut state = self.state.lock();
2500        let parent_entry = state.entry(parent_path)?;
2501        let entry = parent_entry
2502            .dir_entries(parent_path)?
2503            .entry(base_name.to_str().unwrap().into());
2504        let removed = match entry {
2505            btree_map::Entry::Vacant(_) => {
2506                if !options.ignore_if_not_exists {
2507                    anyhow::bail!("{path:?} does not exist");
2508                }
2509
2510                None
2511            }
2512            btree_map::Entry::Occupied(mut entry) => {
2513                entry.get_mut().file_content(&path)?;
2514                Some(entry.remove())
2515            }
2516        };
2517
2518        state.emit_event([(path, Some(PathEventKind::Removed))]);
2519        Ok(removed)
2520    }
2521}
2522
2523#[cfg(feature = "test-support")]
2524impl FakeFsEntry {
2525    fn is_file(&self) -> bool {
2526        matches!(self, Self::File { .. })
2527    }
2528
2529    fn is_symlink(&self) -> bool {
2530        matches!(self, Self::Symlink { .. })
2531    }
2532
2533    fn file_content(&self, path: &Path) -> Result<&Vec<u8>> {
2534        if let Self::File { content, .. } = self {
2535            Ok(content)
2536        } else {
2537            anyhow::bail!("not a file: {path:?}");
2538        }
2539    }
2540
2541    fn dir_entries(&mut self, path: &Path) -> Result<&mut BTreeMap<String, FakeFsEntry>> {
2542        if let Self::Dir { entries, .. } = self {
2543            Ok(entries)
2544        } else {
2545            anyhow::bail!("not a directory: {path:?}");
2546        }
2547    }
2548}
2549
2550#[cfg(feature = "test-support")]
2551struct FakeWatcher {
2552    tx: smol::channel::Sender<Vec<PathEvent>>,
2553    original_path: PathBuf,
2554    fs_state: Arc<Mutex<FakeFsState>>,
2555    prefixes: Mutex<Vec<PathBuf>>,
2556}
2557
2558#[cfg(feature = "test-support")]
2559impl Watcher for FakeWatcher {
2560    fn add(&self, path: &Path) -> Result<()> {
2561        if path.starts_with(&self.original_path) {
2562            return Ok(());
2563        }
2564        self.fs_state
2565            .try_lock()
2566            .unwrap()
2567            .event_txs
2568            .push((path.to_owned(), self.tx.clone()));
2569        self.prefixes.lock().push(path.to_owned());
2570        Ok(())
2571    }
2572
2573    fn remove(&self, _: &Path) -> Result<()> {
2574        Ok(())
2575    }
2576}
2577
2578#[cfg(feature = "test-support")]
2579#[derive(Debug)]
2580struct FakeHandle {
2581    inode: u64,
2582}
2583
2584#[cfg(feature = "test-support")]
2585impl FileHandle for FakeHandle {
2586    fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf> {
2587        let fs = fs.as_fake();
2588        let mut state = fs.state.lock();
2589        let Some(target) = state.moves.get(&self.inode).cloned() else {
2590            anyhow::bail!("fake fd not moved")
2591        };
2592
2593        if state.try_entry(&target, false).is_some() {
2594            return Ok(target);
2595        }
2596        anyhow::bail!("fake fd target not found")
2597    }
2598}
2599
2600#[cfg(feature = "test-support")]
2601#[async_trait::async_trait]
2602impl Fs for FakeFs {
2603    async fn create_dir(&self, path: &Path) -> Result<()> {
2604        self.simulate_random_delay().await;
2605
2606        let mut created_dirs = Vec::new();
2607        let mut cur_path = PathBuf::new();
2608        for component in path.components() {
2609            let should_skip = matches!(component, Component::Prefix(..) | Component::RootDir);
2610            cur_path.push(component);
2611            if should_skip {
2612                continue;
2613            }
2614            let mut state = self.state.lock();
2615
2616            let inode = state.get_and_increment_inode();
2617            let mtime = state.get_and_increment_mtime();
2618            state.write_path(&cur_path, |entry| {
2619                entry.or_insert_with(|| {
2620                    created_dirs.push((cur_path.clone(), Some(PathEventKind::Created)));
2621                    FakeFsEntry::Dir {
2622                        inode,
2623                        mtime,
2624                        len: 0,
2625                        entries: Default::default(),
2626                        git_repo_state: None,
2627                    }
2628                });
2629                Ok(())
2630            })?
2631        }
2632
2633        self.state.lock().emit_event(created_dirs);
2634        Ok(())
2635    }
2636
2637    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
2638        self.simulate_random_delay().await;
2639        let mut state = self.state.lock();
2640        let inode = state.get_and_increment_inode();
2641        let mtime = state.get_and_increment_mtime();
2642        let file = FakeFsEntry::File {
2643            inode,
2644            mtime,
2645            len: 0,
2646            content: Vec::new(),
2647            git_dir_path: None,
2648        };
2649        let mut kind = Some(PathEventKind::Created);
2650        state.write_path(path, |entry| {
2651            match entry {
2652                btree_map::Entry::Occupied(mut e) => {
2653                    if options.overwrite {
2654                        kind = Some(PathEventKind::Changed);
2655                        *e.get_mut() = file;
2656                    } else if !options.ignore_if_exists {
2657                        anyhow::bail!("path already exists: {path:?}");
2658                    }
2659                }
2660                btree_map::Entry::Vacant(e) => {
2661                    e.insert(file);
2662                }
2663            }
2664            Ok(())
2665        })?;
2666        state.emit_event([(path, kind)]);
2667        Ok(())
2668    }
2669
2670    async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()> {
2671        let mut state = self.state.lock();
2672        let file = FakeFsEntry::Symlink { target };
2673        state
2674            .write_path(path.as_ref(), move |e| match e {
2675                btree_map::Entry::Vacant(e) => {
2676                    e.insert(file);
2677                    Ok(())
2678                }
2679                btree_map::Entry::Occupied(mut e) => {
2680                    *e.get_mut() = file;
2681                    Ok(())
2682                }
2683            })
2684            .unwrap();
2685        state.emit_event([(path, Some(PathEventKind::Created))]);
2686
2687        Ok(())
2688    }
2689
2690    async fn create_file_with(
2691        &self,
2692        path: &Path,
2693        mut content: Pin<&mut (dyn AsyncRead + Send)>,
2694    ) -> Result<()> {
2695        let mut bytes = Vec::new();
2696        content.read_to_end(&mut bytes).await?;
2697        self.write_file_internal(path, bytes, true)?;
2698        Ok(())
2699    }
2700
2701    async fn extract_tar_file(
2702        &self,
2703        path: &Path,
2704        content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
2705    ) -> Result<()> {
2706        let mut entries = content.entries()?;
2707        while let Some(entry) = entries.next().await {
2708            let mut entry = entry?;
2709            if entry.header().entry_type().is_file() {
2710                let path = path.join(entry.path()?.as_ref());
2711                let mut bytes = Vec::new();
2712                entry.read_to_end(&mut bytes).await?;
2713                self.create_dir(path.parent().unwrap()).await?;
2714                self.write_file_internal(&path, bytes, true)?;
2715            }
2716        }
2717        Ok(())
2718    }
2719
2720    async fn rename(&self, old_path: &Path, new_path: &Path, options: RenameOptions) -> Result<()> {
2721        self.simulate_random_delay().await;
2722
2723        let old_path = normalize_path(old_path);
2724        let new_path = normalize_path(new_path);
2725
2726        if options.create_parents {
2727            if let Some(parent) = new_path.parent() {
2728                self.create_dir(parent).await?;
2729            }
2730        }
2731
2732        let mut state = self.state.lock();
2733        let moved_entry = state.write_path(&old_path, |e| {
2734            if let btree_map::Entry::Occupied(e) = e {
2735                Ok(e.get().clone())
2736            } else {
2737                anyhow::bail!("path does not exist: {old_path:?}")
2738            }
2739        })?;
2740
2741        let inode = match moved_entry {
2742            FakeFsEntry::File { inode, .. } => inode,
2743            FakeFsEntry::Dir { inode, .. } => inode,
2744            _ => 0,
2745        };
2746
2747        state.moves.insert(inode, new_path.clone());
2748
2749        state.write_path(&new_path, |e| {
2750            match e {
2751                btree_map::Entry::Occupied(mut e) => {
2752                    if options.overwrite {
2753                        *e.get_mut() = moved_entry;
2754                    } else if !options.ignore_if_exists {
2755                        anyhow::bail!("path already exists: {new_path:?}");
2756                    }
2757                }
2758                btree_map::Entry::Vacant(e) => {
2759                    e.insert(moved_entry);
2760                }
2761            }
2762            Ok(())
2763        })?;
2764
2765        state
2766            .write_path(&old_path, |e| {
2767                if let btree_map::Entry::Occupied(e) = e {
2768                    Ok(e.remove())
2769                } else {
2770                    unreachable!()
2771                }
2772            })
2773            .unwrap();
2774
2775        state.emit_event([
2776            (old_path, Some(PathEventKind::Removed)),
2777            (new_path, Some(PathEventKind::Created)),
2778        ]);
2779        Ok(())
2780    }
2781
2782    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
2783        self.simulate_random_delay().await;
2784
2785        let source = normalize_path(source);
2786        let target = normalize_path(target);
2787        let mut state = self.state.lock();
2788        let mtime = state.get_and_increment_mtime();
2789        let inode = state.get_and_increment_inode();
2790        let source_entry = state.entry(&source)?;
2791        let content = source_entry.file_content(&source)?.clone();
2792        let mut kind = Some(PathEventKind::Created);
2793        state.write_path(&target, |e| match e {
2794            btree_map::Entry::Occupied(e) => {
2795                if options.overwrite {
2796                    kind = Some(PathEventKind::Changed);
2797                    Ok(Some(e.get().clone()))
2798                } else if !options.ignore_if_exists {
2799                    anyhow::bail!("{target:?} already exists");
2800                } else {
2801                    Ok(None)
2802                }
2803            }
2804            btree_map::Entry::Vacant(e) => Ok(Some(
2805                e.insert(FakeFsEntry::File {
2806                    inode,
2807                    mtime,
2808                    len: content.len() as u64,
2809                    content,
2810                    git_dir_path: None,
2811                })
2812                .clone(),
2813            )),
2814        })?;
2815        state.emit_event([(target, kind)]);
2816        Ok(())
2817    }
2818
2819    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
2820        self.remove_dir_inner(path, options).await.map(|_| ())
2821    }
2822
2823    async fn trash(&self, path: &Path, options: RemoveOptions) -> Result<TrashedEntry> {
2824        let normalized_path = normalize_path(path);
2825        let parent_path = normalized_path.parent().context("cannot remove the root")?;
2826        let base_name = normalized_path.file_name().unwrap();
2827        let result = if self.is_dir(path).await {
2828            self.remove_dir_inner(path, options).await?
2829        } else {
2830            self.remove_file_inner(path, options).await?
2831        };
2832
2833        match result {
2834            Some(fake_entry) => {
2835                let trashed_entry = TrashedEntry {
2836                    id: base_name.to_str().unwrap().into(),
2837                    name: base_name.to_str().unwrap().into(),
2838                    original_parent: parent_path.to_path_buf(),
2839                };
2840
2841                let mut state = self.state.lock();
2842                state.trash.push((trashed_entry.clone(), fake_entry));
2843                Ok(trashed_entry)
2844            }
2845            None => anyhow::bail!("{normalized_path:?} does not exist"),
2846        }
2847    }
2848
2849    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
2850        self.remove_file_inner(path, options).await.map(|_| ())
2851    }
2852
2853    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read + Send + Sync>> {
2854        let bytes = self.load_internal(path).await?;
2855        Ok(Box::new(io::Cursor::new(bytes)))
2856    }
2857
2858    async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>> {
2859        self.simulate_random_delay().await;
2860        let mut state = self.state.lock();
2861        let inode = match state.entry(path)? {
2862            FakeFsEntry::File { inode, .. } => *inode,
2863            FakeFsEntry::Dir { inode, .. } => *inode,
2864            _ => unreachable!(),
2865        };
2866        Ok(Arc::new(FakeHandle { inode }))
2867    }
2868
2869    async fn load(&self, path: &Path) -> Result<String> {
2870        let content = self.load_internal(path).await?;
2871        Ok(String::from_utf8(content)?)
2872    }
2873
2874    async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>> {
2875        self.load_internal(path).await
2876    }
2877
2878    async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
2879        self.simulate_random_delay().await;
2880        let path = normalize_path(path.as_path());
2881        if let Some(path) = path.parent() {
2882            self.create_dir(path).await?;
2883        }
2884        self.write_file_internal(path, data.into_bytes(), true)?;
2885        Ok(())
2886    }
2887
2888    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
2889        self.simulate_random_delay().await;
2890        let path = normalize_path(path);
2891        let content = text::chunks_with_line_ending(text, line_ending).collect::<String>();
2892        if let Some(path) = path.parent() {
2893            self.create_dir(path).await?;
2894        }
2895        self.write_file_internal(path, content.into_bytes(), false)?;
2896        Ok(())
2897    }
2898
2899    async fn write(&self, path: &Path, content: &[u8]) -> Result<()> {
2900        self.simulate_random_delay().await;
2901        let path = normalize_path(path);
2902        if let Some(path) = path.parent() {
2903            self.create_dir(path).await?;
2904        }
2905        self.write_file_internal(path, content.to_vec(), false)?;
2906        Ok(())
2907    }
2908
2909    async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
2910        let path = normalize_path(path);
2911        self.simulate_random_delay().await;
2912        let state = self.state.lock();
2913        let canonical_path = state
2914            .canonicalize(&path, true)
2915            .with_context(|| format!("path does not exist: {path:?}"))?;
2916        Ok(canonical_path)
2917    }
2918
2919    async fn is_file(&self, path: &Path) -> bool {
2920        let path = normalize_path(path);
2921        self.simulate_random_delay().await;
2922        let mut state = self.state.lock();
2923        if let Some((entry, _)) = state.try_entry(&path, true) {
2924            entry.is_file()
2925        } else {
2926            false
2927        }
2928    }
2929
2930    async fn is_dir(&self, path: &Path) -> bool {
2931        self.metadata(path)
2932            .await
2933            .is_ok_and(|metadata| metadata.is_some_and(|metadata| metadata.is_dir))
2934    }
2935
2936    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
2937        self.simulate_random_delay().await;
2938        let path = normalize_path(path);
2939        let mut state = self.state.lock();
2940        state.metadata_call_count += 1;
2941        if let Some((mut entry, _)) = state.try_entry(&path, false) {
2942            let is_symlink = entry.is_symlink();
2943            if is_symlink {
2944                if let Some(e) = state.try_entry(&path, true).map(|e| e.0) {
2945                    entry = e;
2946                } else {
2947                    return Ok(None);
2948                }
2949            }
2950
2951            Ok(Some(match &*entry {
2952                FakeFsEntry::File {
2953                    inode, mtime, len, ..
2954                } => Metadata {
2955                    inode: *inode,
2956                    mtime: *mtime,
2957                    len: *len,
2958                    is_dir: false,
2959                    is_symlink,
2960                    is_fifo: false,
2961                    is_executable: false,
2962                },
2963                FakeFsEntry::Dir {
2964                    inode, mtime, len, ..
2965                } => Metadata {
2966                    inode: *inode,
2967                    mtime: *mtime,
2968                    len: *len,
2969                    is_dir: true,
2970                    is_symlink,
2971                    is_fifo: false,
2972                    is_executable: false,
2973                },
2974                FakeFsEntry::Symlink { .. } => unreachable!(),
2975            }))
2976        } else {
2977            Ok(None)
2978        }
2979    }
2980
2981    async fn read_link(&self, path: &Path) -> Result<PathBuf> {
2982        self.simulate_random_delay().await;
2983        let path = normalize_path(path);
2984        let mut state = self.state.lock();
2985        let (entry, _) = state
2986            .try_entry(&path, false)
2987            .with_context(|| format!("path does not exist: {path:?}"))?;
2988        if let FakeFsEntry::Symlink { target } = entry {
2989            Ok(target.clone())
2990        } else {
2991            anyhow::bail!("not a symlink: {path:?}")
2992        }
2993    }
2994
2995    async fn read_dir(
2996        &self,
2997        path: &Path,
2998    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
2999        self.simulate_random_delay().await;
3000        let path = normalize_path(path);
3001        let mut state = self.state.lock();
3002        state.read_dir_call_count += 1;
3003        let entry = state.entry(&path)?;
3004        let children = entry.dir_entries(&path)?;
3005        let paths = children
3006            .keys()
3007            .map(|file_name| Ok(path.join(file_name)))
3008            .collect::<Vec<_>>();
3009        Ok(Box::pin(futures::stream::iter(paths)))
3010    }
3011
3012    async fn watch(
3013        &self,
3014        path: &Path,
3015        _: Duration,
3016    ) -> (
3017        Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
3018        Arc<dyn Watcher>,
3019    ) {
3020        self.simulate_random_delay().await;
3021        let (tx, rx) = smol::channel::unbounded();
3022        let path = path.to_path_buf();
3023        self.state.lock().event_txs.push((path.clone(), tx.clone()));
3024        let executor = self.executor.clone();
3025        let watcher = Arc::new(FakeWatcher {
3026            tx,
3027            original_path: path.to_owned(),
3028            fs_state: self.state.clone(),
3029            prefixes: Mutex::new(vec![path]),
3030        });
3031        (
3032            Box::pin(futures::StreamExt::filter(rx, {
3033                let watcher = watcher.clone();
3034                move |events| {
3035                    let result = events.iter().any(|evt_path| {
3036                        watcher
3037                            .prefixes
3038                            .lock()
3039                            .iter()
3040                            .any(|prefix| evt_path.path.starts_with(prefix))
3041                    });
3042                    let executor = executor.clone();
3043                    async move {
3044                        executor.simulate_random_delay().await;
3045                        result
3046                    }
3047                }
3048            })),
3049            watcher,
3050        )
3051    }
3052
3053    fn open_repo(
3054        &self,
3055        abs_dot_git: &Path,
3056        _system_git_binary: Option<&Path>,
3057    ) -> Result<Arc<dyn GitRepository>> {
3058        self.with_git_state_and_paths(
3059            abs_dot_git,
3060            false,
3061            |_, repository_dir_path, common_dir_path| {
3062                Arc::new(fake_git_repo::FakeGitRepository {
3063                    fs: self.this.upgrade().unwrap(),
3064                    executor: self.executor.clone(),
3065                    dot_git_path: abs_dot_git.to_path_buf(),
3066                    repository_dir_path: repository_dir_path.to_owned(),
3067                    common_dir_path: common_dir_path.to_owned(),
3068                    checkpoints: Arc::default(),
3069                    is_trusted: Arc::default(),
3070                }) as _
3071            },
3072        )
3073    }
3074
3075    async fn git_init(
3076        &self,
3077        abs_work_directory_path: &Path,
3078        _fallback_branch_name: String,
3079    ) -> Result<()> {
3080        self.create_dir(&abs_work_directory_path.join(".git")).await
3081    }
3082
3083    async fn git_clone(&self, _repo_url: &str, _abs_work_directory: &Path) -> Result<()> {
3084        anyhow::bail!("Git clone is not supported in fake Fs")
3085    }
3086
3087    fn is_fake(&self) -> bool {
3088        true
3089    }
3090
3091    async fn is_case_sensitive(&self) -> bool {
3092        true
3093    }
3094
3095    fn subscribe_to_jobs(&self) -> JobEventReceiver {
3096        let (sender, receiver) = futures::channel::mpsc::unbounded();
3097        self.state.lock().job_event_subscribers.lock().push(sender);
3098        receiver
3099    }
3100
3101    async fn restore(&self, trashed_entry: TrashedEntry) -> Result<PathBuf, TrashRestoreError> {
3102        let mut state = self.state.lock();
3103
3104        let Some((trashed_entry, fake_entry)) = state
3105            .trash
3106            .iter()
3107            .find(|(entry, _)| *entry == trashed_entry)
3108            .cloned()
3109        else {
3110            return Err(TrashRestoreError::NotFound {
3111                path: PathBuf::from(trashed_entry.id),
3112            });
3113        };
3114
3115        let path = trashed_entry
3116            .original_parent
3117            .join(trashed_entry.name.clone());
3118
3119        let result = state.write_path(&path, |entry| match entry {
3120            btree_map::Entry::Vacant(entry) => {
3121                entry.insert(fake_entry);
3122                Ok(())
3123            }
3124            btree_map::Entry::Occupied(_) => {
3125                anyhow::bail!("Failed to restore {:?}", path);
3126            }
3127        });
3128
3129        match result {
3130            Ok(_) => {
3131                state.trash.retain(|(entry, _)| *entry != trashed_entry);
3132                state.emit_event([(path.clone(), Some(PathEventKind::Created))]);
3133                Ok(path)
3134            }
3135            Err(_) => {
3136                // For now we'll just assume that this failed because it was a
3137                // collision error, which I think that, for the time being, is
3138                // the only case where this could fail?
3139                Err(TrashRestoreError::Collision { path })
3140            }
3141        }
3142    }
3143
3144    #[cfg(feature = "test-support")]
3145    fn as_fake(&self) -> Arc<FakeFs> {
3146        self.this.upgrade().unwrap()
3147    }
3148}
3149
3150pub async fn copy_recursive<'a>(
3151    fs: &'a dyn Fs,
3152    source: &'a Path,
3153    target: &'a Path,
3154    options: CopyOptions,
3155) -> Result<()> {
3156    for (item, is_dir) in read_dir_items(fs, source).await? {
3157        let Ok(item_relative_path) = item.strip_prefix(source) else {
3158            continue;
3159        };
3160        let target_item = if item_relative_path == Path::new("") {
3161            target.to_path_buf()
3162        } else {
3163            target.join(item_relative_path)
3164        };
3165        if is_dir {
3166            if !options.overwrite && fs.metadata(&target_item).await.is_ok_and(|m| m.is_some()) {
3167                if options.ignore_if_exists {
3168                    continue;
3169                } else {
3170                    anyhow::bail!("{target_item:?} already exists");
3171                }
3172            }
3173            let _ = fs
3174                .remove_dir(
3175                    &target_item,
3176                    RemoveOptions {
3177                        recursive: true,
3178                        ignore_if_not_exists: true,
3179                    },
3180                )
3181                .await;
3182            fs.create_dir(&target_item).await?;
3183        } else {
3184            fs.copy_file(&item, &target_item, options).await?;
3185        }
3186    }
3187    Ok(())
3188}
3189
3190/// Recursively reads all of the paths in the given directory.
3191///
3192/// Returns a vector of tuples of (path, is_dir).
3193pub async fn read_dir_items<'a>(fs: &'a dyn Fs, source: &'a Path) -> Result<Vec<(PathBuf, bool)>> {
3194    let mut items = Vec::new();
3195    read_recursive(fs, source, &mut items).await?;
3196    Ok(items)
3197}
3198
3199fn read_recursive<'a>(
3200    fs: &'a dyn Fs,
3201    source: &'a Path,
3202    output: &'a mut Vec<(PathBuf, bool)>,
3203) -> BoxFuture<'a, Result<()>> {
3204    use futures::future::FutureExt;
3205
3206    async move {
3207        let metadata = fs
3208            .metadata(source)
3209            .await?
3210            .with_context(|| format!("path does not exist: {source:?}"))?;
3211
3212        if metadata.is_dir {
3213            output.push((source.to_path_buf(), true));
3214            let mut children = fs.read_dir(source).await?;
3215            while let Some(child_path) = children.next().await {
3216                if let Ok(child_path) = child_path {
3217                    read_recursive(fs, &child_path, output).await?;
3218                }
3219            }
3220        } else {
3221            output.push((source.to_path_buf(), false));
3222        }
3223        Ok(())
3224    }
3225    .boxed()
3226}
3227
3228// todo(windows)
3229// can we get file id not open the file twice?
3230// https://github.com/rust-lang/rust/issues/63010
3231#[cfg(target_os = "windows")]
3232async fn file_id(path: impl AsRef<Path>) -> Result<u64> {
3233    use std::os::windows::io::AsRawHandle;
3234
3235    use smol::fs::windows::OpenOptionsExt;
3236    use windows::Win32::{
3237        Foundation::HANDLE,
3238        Storage::FileSystem::{
3239            BY_HANDLE_FILE_INFORMATION, FILE_FLAG_BACKUP_SEMANTICS, GetFileInformationByHandle,
3240        },
3241    };
3242
3243    let file = smol::fs::OpenOptions::new()
3244        .read(true)
3245        .custom_flags(FILE_FLAG_BACKUP_SEMANTICS.0)
3246        .open(path)
3247        .await?;
3248
3249    let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() };
3250    // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle
3251    // This function supports Windows XP+
3252    smol::unblock(move || {
3253        unsafe { GetFileInformationByHandle(HANDLE(file.as_raw_handle() as _), &mut info)? };
3254
3255        Ok(((info.nFileIndexHigh as u64) << 32) | (info.nFileIndexLow as u64))
3256    })
3257    .await
3258}
3259
3260#[cfg(target_os = "windows")]
3261fn atomic_replace<P: AsRef<Path>>(
3262    replaced_file: P,
3263    replacement_file: P,
3264) -> windows::core::Result<()> {
3265    use windows::{
3266        Win32::Storage::FileSystem::{REPLACE_FILE_FLAGS, ReplaceFileW},
3267        core::HSTRING,
3268    };
3269
3270    // If the file does not exist, create it.
3271    let _ = std::fs::File::create_new(replaced_file.as_ref());
3272
3273    unsafe {
3274        ReplaceFileW(
3275            &HSTRING::from(replaced_file.as_ref().to_string_lossy().into_owned()),
3276            &HSTRING::from(replacement_file.as_ref().to_string_lossy().into_owned()),
3277            None,
3278            REPLACE_FILE_FLAGS::default(),
3279            None,
3280            None,
3281        )
3282    }
3283}