fs.rs

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