fs.rs

   1pub mod repository;
   2
   3use anyhow::{anyhow, Result};
   4#[cfg(target_os = "macos")]
   5pub use fsevent::Event;
   6#[cfg(target_os = "macos")]
   7use fsevent::EventStream;
   8
   9#[cfg(not(target_os = "macos"))]
  10pub use notify::Event;
  11#[cfg(not(target_os = "macos"))]
  12use notify::{Config, Watcher};
  13
  14use futures::{future::BoxFuture, Stream, StreamExt};
  15use git2::Repository as LibGitRepository;
  16use parking_lot::Mutex;
  17use repository::GitRepository;
  18use rope::Rope;
  19use smol::io::{AsyncReadExt, AsyncWriteExt};
  20use std::io::Write;
  21use std::sync::Arc;
  22use std::{
  23    io,
  24    os::unix::fs::MetadataExt,
  25    path::{Component, Path, PathBuf},
  26    pin::Pin,
  27    time::{Duration, SystemTime},
  28};
  29use tempfile::NamedTempFile;
  30use text::LineEnding;
  31use util::ResultExt;
  32
  33#[cfg(any(test, feature = "test-support"))]
  34use collections::{btree_map, BTreeMap};
  35#[cfg(any(test, feature = "test-support"))]
  36use repository::{FakeGitRepositoryState, GitFileStatus};
  37#[cfg(any(test, feature = "test-support"))]
  38use std::ffi::OsStr;
  39
  40#[async_trait::async_trait]
  41pub trait Fs: Send + Sync {
  42    async fn create_dir(&self, path: &Path) -> Result<()>;
  43    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()>;
  44    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()>;
  45    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()>;
  46    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()>;
  47    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;
  48    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read>>;
  49    async fn load(&self, path: &Path) -> Result<String>;
  50    async fn atomic_write(&self, path: PathBuf, text: String) -> Result<()>;
  51    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()>;
  52    async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
  53    async fn is_file(&self, path: &Path) -> bool;
  54    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;
  55    async fn read_link(&self, path: &Path) -> Result<PathBuf>;
  56    async fn read_dir(
  57        &self,
  58        path: &Path,
  59    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>>;
  60
  61    async fn watch(
  62        &self,
  63        path: &Path,
  64        latency: Duration,
  65    ) -> Pin<Box<dyn Send + Stream<Item = Vec<Event>>>>;
  66
  67    fn open_repo(&self, abs_dot_git: &Path) -> Option<Arc<Mutex<dyn GitRepository>>>;
  68    fn is_fake(&self) -> bool;
  69    #[cfg(any(test, feature = "test-support"))]
  70    fn as_fake(&self) -> &FakeFs;
  71}
  72
  73#[derive(Copy, Clone, Default)]
  74pub struct CreateOptions {
  75    pub overwrite: bool,
  76    pub ignore_if_exists: bool,
  77}
  78
  79#[derive(Copy, Clone, Default)]
  80pub struct CopyOptions {
  81    pub overwrite: bool,
  82    pub ignore_if_exists: bool,
  83}
  84
  85#[derive(Copy, Clone, Default)]
  86pub struct RenameOptions {
  87    pub overwrite: bool,
  88    pub ignore_if_exists: bool,
  89}
  90
  91#[derive(Copy, Clone, Default)]
  92pub struct RemoveOptions {
  93    pub recursive: bool,
  94    pub ignore_if_not_exists: bool,
  95}
  96
  97#[derive(Copy, Clone, Debug)]
  98pub struct Metadata {
  99    pub inode: u64,
 100    pub mtime: SystemTime,
 101    pub is_symlink: bool,
 102    pub is_dir: bool,
 103}
 104
 105pub struct RealFs;
 106
 107#[async_trait::async_trait]
 108impl Fs for RealFs {
 109    async fn create_dir(&self, path: &Path) -> Result<()> {
 110        Ok(smol::fs::create_dir_all(path).await?)
 111    }
 112
 113    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
 114        let mut open_options = smol::fs::OpenOptions::new();
 115        open_options.write(true).create(true);
 116        if options.overwrite {
 117            open_options.truncate(true);
 118        } else if !options.ignore_if_exists {
 119            open_options.create_new(true);
 120        }
 121        open_options.open(path).await?;
 122        Ok(())
 123    }
 124
 125    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
 126        if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
 127            if options.ignore_if_exists {
 128                return Ok(());
 129            } else {
 130                return Err(anyhow!("{target:?} already exists"));
 131            }
 132        }
 133
 134        smol::fs::copy(source, target).await?;
 135        Ok(())
 136    }
 137
 138    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()> {
 139        if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
 140            if options.ignore_if_exists {
 141                return Ok(());
 142            } else {
 143                return Err(anyhow!("{target:?} already exists"));
 144            }
 145        }
 146
 147        smol::fs::rename(source, target).await?;
 148        Ok(())
 149    }
 150
 151    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 152        let result = if options.recursive {
 153            smol::fs::remove_dir_all(path).await
 154        } else {
 155            smol::fs::remove_dir(path).await
 156        };
 157        match result {
 158            Ok(()) => Ok(()),
 159            Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
 160                Ok(())
 161            }
 162            Err(err) => Err(err)?,
 163        }
 164    }
 165
 166    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 167        match smol::fs::remove_file(path).await {
 168            Ok(()) => Ok(()),
 169            Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
 170                Ok(())
 171            }
 172            Err(err) => Err(err)?,
 173        }
 174    }
 175
 176    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read>> {
 177        Ok(Box::new(std::fs::File::open(path)?))
 178    }
 179
 180    async fn load(&self, path: &Path) -> Result<String> {
 181        let mut file = smol::fs::File::open(path).await?;
 182        let mut text = String::new();
 183        file.read_to_string(&mut text).await?;
 184        Ok(text)
 185    }
 186
 187    async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
 188        smol::unblock(move || {
 189            let mut tmp_file = NamedTempFile::new()?;
 190            tmp_file.write_all(data.as_bytes())?;
 191            tmp_file.persist(path)?;
 192            Ok::<(), anyhow::Error>(())
 193        })
 194        .await?;
 195
 196        Ok(())
 197    }
 198
 199    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
 200        let buffer_size = text.summary().len.min(10 * 1024);
 201        if let Some(path) = path.parent() {
 202            self.create_dir(path).await?;
 203        }
 204        let file = smol::fs::File::create(path).await?;
 205        let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
 206        for chunk in chunks(text, line_ending) {
 207            writer.write_all(chunk.as_bytes()).await?;
 208        }
 209        writer.flush().await?;
 210        Ok(())
 211    }
 212
 213    async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
 214        Ok(smol::fs::canonicalize(path).await?)
 215    }
 216
 217    async fn is_file(&self, path: &Path) -> bool {
 218        smol::fs::metadata(path)
 219            .await
 220            .map_or(false, |metadata| metadata.is_file())
 221    }
 222
 223    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
 224        let symlink_metadata = match smol::fs::symlink_metadata(path).await {
 225            Ok(metadata) => metadata,
 226            Err(err) => {
 227                return match (err.kind(), err.raw_os_error()) {
 228                    (io::ErrorKind::NotFound, _) => Ok(None),
 229                    (io::ErrorKind::Other, Some(libc::ENOTDIR)) => Ok(None),
 230                    _ => Err(anyhow::Error::new(err)),
 231                }
 232            }
 233        };
 234
 235        let is_symlink = symlink_metadata.file_type().is_symlink();
 236        let metadata = if is_symlink {
 237            smol::fs::metadata(path).await?
 238        } else {
 239            symlink_metadata
 240        };
 241        Ok(Some(Metadata {
 242            inode: metadata.ino(),
 243            mtime: metadata.modified().unwrap(),
 244            is_symlink,
 245            is_dir: metadata.file_type().is_dir(),
 246        }))
 247    }
 248
 249    async fn read_link(&self, path: &Path) -> Result<PathBuf> {
 250        let path = smol::fs::read_link(path).await?;
 251        Ok(path)
 252    }
 253
 254    async fn read_dir(
 255        &self,
 256        path: &Path,
 257    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
 258        let result = smol::fs::read_dir(path).await?.map(|entry| match entry {
 259            Ok(entry) => Ok(entry.path()),
 260            Err(error) => Err(anyhow!("failed to read dir entry {:?}", error)),
 261        });
 262        Ok(Box::pin(result))
 263    }
 264
 265    #[cfg(target_os = "macos")]
 266    async fn watch(
 267        &self,
 268        path: &Path,
 269        latency: Duration,
 270    ) -> Pin<Box<dyn Send + Stream<Item = Vec<Event>>>> {
 271        let (tx, rx) = smol::channel::unbounded();
 272        let (stream, handle) = EventStream::new(&[path], latency);
 273        std::thread::spawn(move || {
 274            stream.run(move |events| smol::block_on(tx.send(events)).is_ok());
 275        });
 276        Box::pin(rx.chain(futures::stream::once(async move {
 277            drop(handle);
 278            vec![]
 279        })))
 280    }
 281
 282    #[cfg(not(target_os = "macos"))]
 283    async fn watch(
 284        &self,
 285        path: &Path,
 286        latency: Duration,
 287    ) -> Pin<Box<dyn Send + Stream<Item = Vec<Event>>>> {
 288        let (tx, rx) = smol::channel::unbounded();
 289
 290        let mut watcher = notify::recommended_watcher(move |res| match res {
 291            Ok(event) => {
 292                let _ = tx.try_send(vec![event]);
 293            }
 294            Err(err) => {
 295                eprintln!("watch error: {:?}", err);
 296            }
 297        })
 298        .unwrap();
 299
 300        watcher
 301            .configure(Config::default().with_poll_interval(latency))
 302            .unwrap();
 303
 304        //TODO: unblock this
 305        #[cfg(not(target_os = "linux"))]
 306        watcher
 307            .watch(path, notify::RecursiveMode::Recursive)
 308            .unwrap();
 309
 310        Box::pin(rx)
 311    }
 312
 313    fn open_repo(&self, dotgit_path: &Path) -> Option<Arc<Mutex<dyn GitRepository>>> {
 314        LibGitRepository::open(&dotgit_path)
 315            .log_err()
 316            .and_then::<Arc<Mutex<dyn GitRepository>>, _>(|libgit_repository| {
 317                Some(Arc::new(Mutex::new(libgit_repository)))
 318            })
 319    }
 320
 321    fn is_fake(&self) -> bool {
 322        false
 323    }
 324    #[cfg(any(test, feature = "test-support"))]
 325    fn as_fake(&self) -> &FakeFs {
 326        panic!("called `RealFs::as_fake`")
 327    }
 328}
 329
 330#[cfg(target_os = "macos")]
 331pub fn fs_events_paths(events: Vec<Event>) -> Vec<PathBuf> {
 332    events.into_iter().map(|event| event.path).collect()
 333}
 334
 335#[cfg(not(target_os = "macos"))]
 336pub fn fs_events_paths(events: Vec<Event>) -> Vec<PathBuf> {
 337    events
 338        .into_iter()
 339        .map(|event| event.paths.into_iter())
 340        .flatten()
 341        .collect()
 342}
 343
 344#[cfg(any(test, feature = "test-support"))]
 345pub struct FakeFs {
 346    // Use an unfair lock to ensure tests are deterministic.
 347    state: Mutex<FakeFsState>,
 348    executor: gpui::BackgroundExecutor,
 349}
 350
 351#[cfg(any(test, feature = "test-support"))]
 352struct FakeFsState {
 353    root: Arc<Mutex<FakeFsEntry>>,
 354    next_inode: u64,
 355    next_mtime: SystemTime,
 356    event_txs: Vec<smol::channel::Sender<Vec<fsevent::Event>>>,
 357    events_paused: bool,
 358    buffered_events: Vec<fsevent::Event>,
 359    metadata_call_count: usize,
 360    read_dir_call_count: usize,
 361}
 362
 363#[cfg(any(test, feature = "test-support"))]
 364#[derive(Debug)]
 365enum FakeFsEntry {
 366    File {
 367        inode: u64,
 368        mtime: SystemTime,
 369        content: String,
 370    },
 371    Dir {
 372        inode: u64,
 373        mtime: SystemTime,
 374        entries: BTreeMap<String, Arc<Mutex<FakeFsEntry>>>,
 375        git_repo_state: Option<Arc<Mutex<repository::FakeGitRepositoryState>>>,
 376    },
 377    Symlink {
 378        target: PathBuf,
 379    },
 380}
 381
 382#[cfg(any(test, feature = "test-support"))]
 383impl FakeFsState {
 384    fn read_path<'a>(&'a self, target: &Path) -> Result<Arc<Mutex<FakeFsEntry>>> {
 385        Ok(self
 386            .try_read_path(target, true)
 387            .ok_or_else(|| anyhow!("path does not exist: {}", target.display()))?
 388            .0)
 389    }
 390
 391    fn try_read_path<'a>(
 392        &'a self,
 393        target: &Path,
 394        follow_symlink: bool,
 395    ) -> Option<(Arc<Mutex<FakeFsEntry>>, PathBuf)> {
 396        let mut path = target.to_path_buf();
 397        let mut canonical_path = PathBuf::new();
 398        let mut entry_stack = Vec::new();
 399        'outer: loop {
 400            let mut path_components = path.components().peekable();
 401            while let Some(component) = path_components.next() {
 402                match component {
 403                    Component::Prefix(_) => panic!("prefix paths aren't supported"),
 404                    Component::RootDir => {
 405                        entry_stack.clear();
 406                        entry_stack.push(self.root.clone());
 407                        canonical_path.clear();
 408                        canonical_path.push("/");
 409                    }
 410                    Component::CurDir => {}
 411                    Component::ParentDir => {
 412                        entry_stack.pop()?;
 413                        canonical_path.pop();
 414                    }
 415                    Component::Normal(name) => {
 416                        let current_entry = entry_stack.last().cloned()?;
 417                        let current_entry = current_entry.lock();
 418                        if let FakeFsEntry::Dir { entries, .. } = &*current_entry {
 419                            let entry = entries.get(name.to_str().unwrap()).cloned()?;
 420                            if path_components.peek().is_some() || follow_symlink {
 421                                let entry = entry.lock();
 422                                if let FakeFsEntry::Symlink { target, .. } = &*entry {
 423                                    let mut target = target.clone();
 424                                    target.extend(path_components);
 425                                    path = target;
 426                                    continue 'outer;
 427                                }
 428                            }
 429                            entry_stack.push(entry.clone());
 430                            canonical_path.push(name);
 431                        } else {
 432                            return None;
 433                        }
 434                    }
 435                }
 436            }
 437            break;
 438        }
 439        Some((entry_stack.pop()?, canonical_path))
 440    }
 441
 442    fn write_path<Fn, T>(&self, path: &Path, callback: Fn) -> Result<T>
 443    where
 444        Fn: FnOnce(btree_map::Entry<String, Arc<Mutex<FakeFsEntry>>>) -> Result<T>,
 445    {
 446        let path = normalize_path(path);
 447        let filename = path
 448            .file_name()
 449            .ok_or_else(|| anyhow!("cannot overwrite the root"))?;
 450        let parent_path = path.parent().unwrap();
 451
 452        let parent = self.read_path(parent_path)?;
 453        let mut parent = parent.lock();
 454        let new_entry = parent
 455            .dir_entries(parent_path)?
 456            .entry(filename.to_str().unwrap().into());
 457        callback(new_entry)
 458    }
 459
 460    fn emit_event<I, T>(&mut self, paths: I)
 461    where
 462        I: IntoIterator<Item = T>,
 463        T: Into<PathBuf>,
 464    {
 465        self.buffered_events
 466            .extend(paths.into_iter().map(|path| fsevent::Event {
 467                event_id: 0,
 468                flags: fsevent::StreamFlags::empty(),
 469                path: path.into(),
 470            }));
 471
 472        if !self.events_paused {
 473            self.flush_events(self.buffered_events.len());
 474        }
 475    }
 476
 477    fn flush_events(&mut self, mut count: usize) {
 478        count = count.min(self.buffered_events.len());
 479        let events = self.buffered_events.drain(0..count).collect::<Vec<_>>();
 480        self.event_txs.retain(|tx| {
 481            let _ = tx.try_send(events.clone());
 482            !tx.is_closed()
 483        });
 484    }
 485}
 486
 487#[cfg(any(test, feature = "test-support"))]
 488lazy_static::lazy_static! {
 489    pub static ref FS_DOT_GIT: &'static OsStr = OsStr::new(".git");
 490}
 491
 492#[cfg(any(test, feature = "test-support"))]
 493impl FakeFs {
 494    pub fn new(executor: gpui::BackgroundExecutor) -> Arc<Self> {
 495        Arc::new(Self {
 496            executor,
 497            state: Mutex::new(FakeFsState {
 498                root: Arc::new(Mutex::new(FakeFsEntry::Dir {
 499                    inode: 0,
 500                    mtime: SystemTime::UNIX_EPOCH,
 501                    entries: Default::default(),
 502                    git_repo_state: None,
 503                })),
 504                next_mtime: SystemTime::UNIX_EPOCH,
 505                next_inode: 1,
 506                event_txs: Default::default(),
 507                buffered_events: Vec::new(),
 508                events_paused: false,
 509                read_dir_call_count: 0,
 510                metadata_call_count: 0,
 511            }),
 512        })
 513    }
 514
 515    pub async fn insert_file(&self, path: impl AsRef<Path>, content: String) {
 516        self.write_file_internal(path, content).unwrap()
 517    }
 518
 519    pub async fn insert_symlink(&self, path: impl AsRef<Path>, target: PathBuf) {
 520        let mut state = self.state.lock();
 521        let path = path.as_ref();
 522        let file = Arc::new(Mutex::new(FakeFsEntry::Symlink { target }));
 523        state
 524            .write_path(path.as_ref(), move |e| match e {
 525                btree_map::Entry::Vacant(e) => {
 526                    e.insert(file);
 527                    Ok(())
 528                }
 529                btree_map::Entry::Occupied(mut e) => {
 530                    *e.get_mut() = file;
 531                    Ok(())
 532                }
 533            })
 534            .unwrap();
 535        state.emit_event(&[path]);
 536    }
 537
 538    pub fn write_file_internal(&self, path: impl AsRef<Path>, content: String) -> Result<()> {
 539        let mut state = self.state.lock();
 540        let path = path.as_ref();
 541        let inode = state.next_inode;
 542        let mtime = state.next_mtime;
 543        state.next_inode += 1;
 544        state.next_mtime += Duration::from_nanos(1);
 545        let file = Arc::new(Mutex::new(FakeFsEntry::File {
 546            inode,
 547            mtime,
 548            content,
 549        }));
 550        state.write_path(path, move |entry| {
 551            match entry {
 552                btree_map::Entry::Vacant(e) => {
 553                    e.insert(file);
 554                }
 555                btree_map::Entry::Occupied(mut e) => {
 556                    *e.get_mut() = file;
 557                }
 558            }
 559            Ok(())
 560        })?;
 561        state.emit_event(&[path]);
 562        Ok(())
 563    }
 564
 565    pub fn pause_events(&self) {
 566        self.state.lock().events_paused = true;
 567    }
 568
 569    pub fn buffered_event_count(&self) -> usize {
 570        self.state.lock().buffered_events.len()
 571    }
 572
 573    pub fn flush_events(&self, count: usize) {
 574        self.state.lock().flush_events(count);
 575    }
 576
 577    #[must_use]
 578    pub fn insert_tree<'a>(
 579        &'a self,
 580        path: impl 'a + AsRef<Path> + Send,
 581        tree: serde_json::Value,
 582    ) -> futures::future::BoxFuture<'a, ()> {
 583        use futures::FutureExt as _;
 584        use serde_json::Value::*;
 585
 586        async move {
 587            let path = path.as_ref();
 588
 589            match tree {
 590                Object(map) => {
 591                    self.create_dir(path).await.unwrap();
 592                    for (name, contents) in map {
 593                        let mut path = PathBuf::from(path);
 594                        path.push(name);
 595                        self.insert_tree(&path, contents).await;
 596                    }
 597                }
 598                Null => {
 599                    self.create_dir(path).await.unwrap();
 600                }
 601                String(contents) => {
 602                    self.insert_file(&path, contents).await;
 603                }
 604                _ => {
 605                    panic!("JSON object must contain only objects, strings, or null");
 606                }
 607            }
 608        }
 609        .boxed()
 610    }
 611
 612    pub fn with_git_state<F>(&self, dot_git: &Path, emit_git_event: bool, f: F)
 613    where
 614        F: FnOnce(&mut FakeGitRepositoryState),
 615    {
 616        let mut state = self.state.lock();
 617        let entry = state.read_path(dot_git).unwrap();
 618        let mut entry = entry.lock();
 619
 620        if let FakeFsEntry::Dir { git_repo_state, .. } = &mut *entry {
 621            let repo_state = git_repo_state.get_or_insert_with(Default::default);
 622            let mut repo_state = repo_state.lock();
 623
 624            f(&mut repo_state);
 625
 626            if emit_git_event {
 627                state.emit_event([dot_git]);
 628            }
 629        } else {
 630            panic!("not a directory");
 631        }
 632    }
 633
 634    pub fn set_branch_name(&self, dot_git: &Path, branch: Option<impl Into<String>>) {
 635        self.with_git_state(dot_git, true, |state| {
 636            state.branch_name = branch.map(Into::into)
 637        })
 638    }
 639
 640    pub fn set_index_for_repo(&self, dot_git: &Path, head_state: &[(&Path, String)]) {
 641        self.with_git_state(dot_git, true, |state| {
 642            state.index_contents.clear();
 643            state.index_contents.extend(
 644                head_state
 645                    .iter()
 646                    .map(|(path, content)| (path.to_path_buf(), content.clone())),
 647            );
 648        });
 649    }
 650
 651    pub fn set_status_for_repo_via_working_copy_change(
 652        &self,
 653        dot_git: &Path,
 654        statuses: &[(&Path, GitFileStatus)],
 655    ) {
 656        self.with_git_state(dot_git, false, |state| {
 657            state.worktree_statuses.clear();
 658            state.worktree_statuses.extend(
 659                statuses
 660                    .iter()
 661                    .map(|(path, content)| ((**path).into(), content.clone())),
 662            );
 663        });
 664        self.state.lock().emit_event(
 665            statuses
 666                .iter()
 667                .map(|(path, _)| dot_git.parent().unwrap().join(path)),
 668        );
 669    }
 670
 671    pub fn set_status_for_repo_via_git_operation(
 672        &self,
 673        dot_git: &Path,
 674        statuses: &[(&Path, GitFileStatus)],
 675    ) {
 676        self.with_git_state(dot_git, true, |state| {
 677            state.worktree_statuses.clear();
 678            state.worktree_statuses.extend(
 679                statuses
 680                    .iter()
 681                    .map(|(path, content)| ((**path).into(), content.clone())),
 682            );
 683        });
 684    }
 685
 686    pub fn paths(&self, include_dot_git: bool) -> Vec<PathBuf> {
 687        let mut result = Vec::new();
 688        let mut queue = collections::VecDeque::new();
 689        queue.push_back((PathBuf::from("/"), self.state.lock().root.clone()));
 690        while let Some((path, entry)) = queue.pop_front() {
 691            if let FakeFsEntry::Dir { entries, .. } = &*entry.lock() {
 692                for (name, entry) in entries {
 693                    queue.push_back((path.join(name), entry.clone()));
 694                }
 695            }
 696            if include_dot_git
 697                || !path
 698                    .components()
 699                    .any(|component| component.as_os_str() == *FS_DOT_GIT)
 700            {
 701                result.push(path);
 702            }
 703        }
 704        result
 705    }
 706
 707    pub fn directories(&self, include_dot_git: bool) -> Vec<PathBuf> {
 708        let mut result = Vec::new();
 709        let mut queue = collections::VecDeque::new();
 710        queue.push_back((PathBuf::from("/"), self.state.lock().root.clone()));
 711        while let Some((path, entry)) = queue.pop_front() {
 712            if let FakeFsEntry::Dir { entries, .. } = &*entry.lock() {
 713                for (name, entry) in entries {
 714                    queue.push_back((path.join(name), entry.clone()));
 715                }
 716                if include_dot_git
 717                    || !path
 718                        .components()
 719                        .any(|component| component.as_os_str() == *FS_DOT_GIT)
 720                {
 721                    result.push(path);
 722                }
 723            }
 724        }
 725        result
 726    }
 727
 728    pub fn files(&self) -> Vec<PathBuf> {
 729        let mut result = Vec::new();
 730        let mut queue = collections::VecDeque::new();
 731        queue.push_back((PathBuf::from("/"), self.state.lock().root.clone()));
 732        while let Some((path, entry)) = queue.pop_front() {
 733            let e = entry.lock();
 734            match &*e {
 735                FakeFsEntry::File { .. } => result.push(path),
 736                FakeFsEntry::Dir { entries, .. } => {
 737                    for (name, entry) in entries {
 738                        queue.push_back((path.join(name), entry.clone()));
 739                    }
 740                }
 741                FakeFsEntry::Symlink { .. } => {}
 742            }
 743        }
 744        result
 745    }
 746
 747    /// How many `read_dir` calls have been issued.
 748    pub fn read_dir_call_count(&self) -> usize {
 749        self.state.lock().read_dir_call_count
 750    }
 751
 752    /// How many `metadata` calls have been issued.
 753    pub fn metadata_call_count(&self) -> usize {
 754        self.state.lock().metadata_call_count
 755    }
 756
 757    fn simulate_random_delay(&self) -> impl futures::Future<Output = ()> {
 758        self.executor.simulate_random_delay()
 759    }
 760}
 761
 762#[cfg(any(test, feature = "test-support"))]
 763impl FakeFsEntry {
 764    fn is_file(&self) -> bool {
 765        matches!(self, Self::File { .. })
 766    }
 767
 768    fn is_symlink(&self) -> bool {
 769        matches!(self, Self::Symlink { .. })
 770    }
 771
 772    fn file_content(&self, path: &Path) -> Result<&String> {
 773        if let Self::File { content, .. } = self {
 774            Ok(content)
 775        } else {
 776            Err(anyhow!("not a file: {}", path.display()))
 777        }
 778    }
 779
 780    fn set_file_content(&mut self, path: &Path, new_content: String) -> Result<()> {
 781        if let Self::File { content, mtime, .. } = self {
 782            *mtime = SystemTime::now();
 783            *content = new_content;
 784            Ok(())
 785        } else {
 786            Err(anyhow!("not a file: {}", path.display()))
 787        }
 788    }
 789
 790    fn dir_entries(
 791        &mut self,
 792        path: &Path,
 793    ) -> Result<&mut BTreeMap<String, Arc<Mutex<FakeFsEntry>>>> {
 794        if let Self::Dir { entries, .. } = self {
 795            Ok(entries)
 796        } else {
 797            Err(anyhow!("not a directory: {}", path.display()))
 798        }
 799    }
 800}
 801
 802#[cfg(any(test, feature = "test-support"))]
 803#[async_trait::async_trait]
 804impl Fs for FakeFs {
 805    async fn create_dir(&self, path: &Path) -> Result<()> {
 806        self.simulate_random_delay().await;
 807
 808        let mut created_dirs = Vec::new();
 809        let mut cur_path = PathBuf::new();
 810        for component in path.components() {
 811            let mut state = self.state.lock();
 812            cur_path.push(component);
 813            if cur_path == Path::new("/") {
 814                continue;
 815            }
 816
 817            let inode = state.next_inode;
 818            let mtime = state.next_mtime;
 819            state.next_mtime += Duration::from_nanos(1);
 820            state.next_inode += 1;
 821            state.write_path(&cur_path, |entry| {
 822                entry.or_insert_with(|| {
 823                    created_dirs.push(cur_path.clone());
 824                    Arc::new(Mutex::new(FakeFsEntry::Dir {
 825                        inode,
 826                        mtime,
 827                        entries: Default::default(),
 828                        git_repo_state: None,
 829                    }))
 830                });
 831                Ok(())
 832            })?
 833        }
 834
 835        self.state.lock().emit_event(&created_dirs);
 836        Ok(())
 837    }
 838
 839    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
 840        self.simulate_random_delay().await;
 841        let mut state = self.state.lock();
 842        let inode = state.next_inode;
 843        let mtime = state.next_mtime;
 844        state.next_mtime += Duration::from_nanos(1);
 845        state.next_inode += 1;
 846        let file = Arc::new(Mutex::new(FakeFsEntry::File {
 847            inode,
 848            mtime,
 849            content: String::new(),
 850        }));
 851        state.write_path(path, |entry| {
 852            match entry {
 853                btree_map::Entry::Occupied(mut e) => {
 854                    if options.overwrite {
 855                        *e.get_mut() = file;
 856                    } else if !options.ignore_if_exists {
 857                        return Err(anyhow!("path already exists: {}", path.display()));
 858                    }
 859                }
 860                btree_map::Entry::Vacant(e) => {
 861                    e.insert(file);
 862                }
 863            }
 864            Ok(())
 865        })?;
 866        state.emit_event(&[path]);
 867        Ok(())
 868    }
 869
 870    async fn rename(&self, old_path: &Path, new_path: &Path, options: RenameOptions) -> Result<()> {
 871        self.simulate_random_delay().await;
 872
 873        let old_path = normalize_path(old_path);
 874        let new_path = normalize_path(new_path);
 875
 876        let mut state = self.state.lock();
 877        let moved_entry = state.write_path(&old_path, |e| {
 878            if let btree_map::Entry::Occupied(e) = e {
 879                Ok(e.get().clone())
 880            } else {
 881                Err(anyhow!("path does not exist: {}", &old_path.display()))
 882            }
 883        })?;
 884
 885        state.write_path(&new_path, |e| {
 886            match e {
 887                btree_map::Entry::Occupied(mut e) => {
 888                    if options.overwrite {
 889                        *e.get_mut() = moved_entry;
 890                    } else if !options.ignore_if_exists {
 891                        return Err(anyhow!("path already exists: {}", new_path.display()));
 892                    }
 893                }
 894                btree_map::Entry::Vacant(e) => {
 895                    e.insert(moved_entry);
 896                }
 897            }
 898            Ok(())
 899        })?;
 900
 901        state
 902            .write_path(&old_path, |e| {
 903                if let btree_map::Entry::Occupied(e) = e {
 904                    Ok(e.remove())
 905                } else {
 906                    unreachable!()
 907                }
 908            })
 909            .unwrap();
 910
 911        state.emit_event(&[old_path, new_path]);
 912        Ok(())
 913    }
 914
 915    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
 916        self.simulate_random_delay().await;
 917
 918        let source = normalize_path(source);
 919        let target = normalize_path(target);
 920        let mut state = self.state.lock();
 921        let mtime = state.next_mtime;
 922        let inode = util::post_inc(&mut state.next_inode);
 923        state.next_mtime += Duration::from_nanos(1);
 924        let source_entry = state.read_path(&source)?;
 925        let content = source_entry.lock().file_content(&source)?.clone();
 926        let entry = state.write_path(&target, |e| match e {
 927            btree_map::Entry::Occupied(e) => {
 928                if options.overwrite {
 929                    Ok(Some(e.get().clone()))
 930                } else if !options.ignore_if_exists {
 931                    return Err(anyhow!("{target:?} already exists"));
 932                } else {
 933                    Ok(None)
 934                }
 935            }
 936            btree_map::Entry::Vacant(e) => Ok(Some(
 937                e.insert(Arc::new(Mutex::new(FakeFsEntry::File {
 938                    inode,
 939                    mtime,
 940                    content: String::new(),
 941                })))
 942                .clone(),
 943            )),
 944        })?;
 945        if let Some(entry) = entry {
 946            entry.lock().set_file_content(&target, content)?;
 947        }
 948        state.emit_event(&[target]);
 949        Ok(())
 950    }
 951
 952    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 953        self.simulate_random_delay().await;
 954
 955        let path = normalize_path(path);
 956        let parent_path = path
 957            .parent()
 958            .ok_or_else(|| anyhow!("cannot remove the root"))?;
 959        let base_name = path.file_name().unwrap();
 960
 961        let mut state = self.state.lock();
 962        let parent_entry = state.read_path(parent_path)?;
 963        let mut parent_entry = parent_entry.lock();
 964        let entry = parent_entry
 965            .dir_entries(parent_path)?
 966            .entry(base_name.to_str().unwrap().into());
 967
 968        match entry {
 969            btree_map::Entry::Vacant(_) => {
 970                if !options.ignore_if_not_exists {
 971                    return Err(anyhow!("{path:?} does not exist"));
 972                }
 973            }
 974            btree_map::Entry::Occupied(e) => {
 975                {
 976                    let mut entry = e.get().lock();
 977                    let children = entry.dir_entries(&path)?;
 978                    if !options.recursive && !children.is_empty() {
 979                        return Err(anyhow!("{path:?} is not empty"));
 980                    }
 981                }
 982                e.remove();
 983            }
 984        }
 985        state.emit_event(&[path]);
 986        Ok(())
 987    }
 988
 989    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 990        self.simulate_random_delay().await;
 991
 992        let path = normalize_path(path);
 993        let parent_path = path
 994            .parent()
 995            .ok_or_else(|| anyhow!("cannot remove the root"))?;
 996        let base_name = path.file_name().unwrap();
 997        let mut state = self.state.lock();
 998        let parent_entry = state.read_path(parent_path)?;
 999        let mut parent_entry = parent_entry.lock();
1000        let entry = parent_entry
1001            .dir_entries(parent_path)?
1002            .entry(base_name.to_str().unwrap().into());
1003        match entry {
1004            btree_map::Entry::Vacant(_) => {
1005                if !options.ignore_if_not_exists {
1006                    return Err(anyhow!("{path:?} does not exist"));
1007                }
1008            }
1009            btree_map::Entry::Occupied(e) => {
1010                e.get().lock().file_content(&path)?;
1011                e.remove();
1012            }
1013        }
1014        state.emit_event(&[path]);
1015        Ok(())
1016    }
1017
1018    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read>> {
1019        let text = self.load(path).await?;
1020        Ok(Box::new(io::Cursor::new(text)))
1021    }
1022
1023    async fn load(&self, path: &Path) -> Result<String> {
1024        let path = normalize_path(path);
1025        self.simulate_random_delay().await;
1026        let state = self.state.lock();
1027        let entry = state.read_path(&path)?;
1028        let entry = entry.lock();
1029        entry.file_content(&path).cloned()
1030    }
1031
1032    async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
1033        self.simulate_random_delay().await;
1034        let path = normalize_path(path.as_path());
1035        self.write_file_internal(path, data.to_string())?;
1036
1037        Ok(())
1038    }
1039
1040    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
1041        self.simulate_random_delay().await;
1042        let path = normalize_path(path);
1043        let content = chunks(text, line_ending).collect();
1044        if let Some(path) = path.parent() {
1045            self.create_dir(path).await?;
1046        }
1047        self.write_file_internal(path, content)?;
1048        Ok(())
1049    }
1050
1051    async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
1052        let path = normalize_path(path);
1053        self.simulate_random_delay().await;
1054        let state = self.state.lock();
1055        if let Some((_, canonical_path)) = state.try_read_path(&path, true) {
1056            Ok(canonical_path)
1057        } else {
1058            Err(anyhow!("path does not exist: {}", path.display()))
1059        }
1060    }
1061
1062    async fn is_file(&self, path: &Path) -> bool {
1063        let path = normalize_path(path);
1064        self.simulate_random_delay().await;
1065        let state = self.state.lock();
1066        if let Some((entry, _)) = state.try_read_path(&path, true) {
1067            entry.lock().is_file()
1068        } else {
1069            false
1070        }
1071    }
1072
1073    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
1074        self.simulate_random_delay().await;
1075        let path = normalize_path(path);
1076        let mut state = self.state.lock();
1077        state.metadata_call_count += 1;
1078        if let Some((mut entry, _)) = state.try_read_path(&path, false) {
1079            let is_symlink = entry.lock().is_symlink();
1080            if is_symlink {
1081                if let Some(e) = state.try_read_path(&path, true).map(|e| e.0) {
1082                    entry = e;
1083                } else {
1084                    return Ok(None);
1085                }
1086            }
1087
1088            let entry = entry.lock();
1089            Ok(Some(match &*entry {
1090                FakeFsEntry::File { inode, mtime, .. } => Metadata {
1091                    inode: *inode,
1092                    mtime: *mtime,
1093                    is_dir: false,
1094                    is_symlink,
1095                },
1096                FakeFsEntry::Dir { inode, mtime, .. } => Metadata {
1097                    inode: *inode,
1098                    mtime: *mtime,
1099                    is_dir: true,
1100                    is_symlink,
1101                },
1102                FakeFsEntry::Symlink { .. } => unreachable!(),
1103            }))
1104        } else {
1105            Ok(None)
1106        }
1107    }
1108
1109    async fn read_link(&self, path: &Path) -> Result<PathBuf> {
1110        self.simulate_random_delay().await;
1111        let path = normalize_path(path);
1112        let state = self.state.lock();
1113        if let Some((entry, _)) = state.try_read_path(&path, false) {
1114            let entry = entry.lock();
1115            if let FakeFsEntry::Symlink { target } = &*entry {
1116                Ok(target.clone())
1117            } else {
1118                Err(anyhow!("not a symlink: {}", path.display()))
1119            }
1120        } else {
1121            Err(anyhow!("path does not exist: {}", path.display()))
1122        }
1123    }
1124
1125    async fn read_dir(
1126        &self,
1127        path: &Path,
1128    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
1129        self.simulate_random_delay().await;
1130        let path = normalize_path(path);
1131        let mut state = self.state.lock();
1132        state.read_dir_call_count += 1;
1133        let entry = state.read_path(&path)?;
1134        let mut entry = entry.lock();
1135        let children = entry.dir_entries(&path)?;
1136        let paths = children
1137            .keys()
1138            .map(|file_name| Ok(path.join(file_name)))
1139            .collect::<Vec<_>>();
1140        Ok(Box::pin(futures::stream::iter(paths)))
1141    }
1142
1143    async fn watch(
1144        &self,
1145        path: &Path,
1146        _: Duration,
1147    ) -> Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>> {
1148        self.simulate_random_delay().await;
1149        let (tx, rx) = smol::channel::unbounded();
1150        self.state.lock().event_txs.push(tx);
1151        let path = path.to_path_buf();
1152        let executor = self.executor.clone();
1153        Box::pin(futures::StreamExt::filter(rx, move |events| {
1154            let result = events.iter().any(|event| event.path.starts_with(&path));
1155            let executor = executor.clone();
1156            async move {
1157                executor.simulate_random_delay().await;
1158                result
1159            }
1160        }))
1161    }
1162
1163    fn open_repo(&self, abs_dot_git: &Path) -> Option<Arc<Mutex<dyn GitRepository>>> {
1164        let state = self.state.lock();
1165        let entry = state.read_path(abs_dot_git).unwrap();
1166        let mut entry = entry.lock();
1167        if let FakeFsEntry::Dir { git_repo_state, .. } = &mut *entry {
1168            let state = git_repo_state
1169                .get_or_insert_with(|| Arc::new(Mutex::new(FakeGitRepositoryState::default())))
1170                .clone();
1171            Some(repository::FakeGitRepository::open(state))
1172        } else {
1173            None
1174        }
1175    }
1176
1177    fn is_fake(&self) -> bool {
1178        true
1179    }
1180
1181    #[cfg(any(test, feature = "test-support"))]
1182    fn as_fake(&self) -> &FakeFs {
1183        self
1184    }
1185}
1186
1187fn chunks(rope: &Rope, line_ending: LineEnding) -> impl Iterator<Item = &str> {
1188    rope.chunks().flat_map(move |chunk| {
1189        let mut newline = false;
1190        chunk.split('\n').flat_map(move |line| {
1191            let ending = if newline {
1192                Some(line_ending.as_str())
1193            } else {
1194                None
1195            };
1196            newline = true;
1197            ending.into_iter().chain([line])
1198        })
1199    })
1200}
1201
1202pub fn normalize_path(path: &Path) -> PathBuf {
1203    let mut components = path.components().peekable();
1204    let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
1205        components.next();
1206        PathBuf::from(c.as_os_str())
1207    } else {
1208        PathBuf::new()
1209    };
1210
1211    for component in components {
1212        match component {
1213            Component::Prefix(..) => unreachable!(),
1214            Component::RootDir => {
1215                ret.push(component.as_os_str());
1216            }
1217            Component::CurDir => {}
1218            Component::ParentDir => {
1219                ret.pop();
1220            }
1221            Component::Normal(c) => {
1222                ret.push(c);
1223            }
1224        }
1225    }
1226    ret
1227}
1228
1229pub fn copy_recursive<'a>(
1230    fs: &'a dyn Fs,
1231    source: &'a Path,
1232    target: &'a Path,
1233    options: CopyOptions,
1234) -> BoxFuture<'a, Result<()>> {
1235    use futures::future::FutureExt;
1236
1237    async move {
1238        let metadata = fs
1239            .metadata(source)
1240            .await?
1241            .ok_or_else(|| anyhow!("path does not exist: {}", source.display()))?;
1242        if metadata.is_dir {
1243            if !options.overwrite && fs.metadata(target).await.is_ok_and(|m| m.is_some()) {
1244                if options.ignore_if_exists {
1245                    return Ok(());
1246                } else {
1247                    return Err(anyhow!("{target:?} already exists"));
1248                }
1249            }
1250
1251            let _ = fs
1252                .remove_dir(
1253                    target,
1254                    RemoveOptions {
1255                        recursive: true,
1256                        ignore_if_not_exists: true,
1257                    },
1258                )
1259                .await;
1260            fs.create_dir(target).await?;
1261            let mut children = fs.read_dir(source).await?;
1262            while let Some(child_path) = children.next().await {
1263                if let Ok(child_path) = child_path {
1264                    if let Some(file_name) = child_path.file_name() {
1265                        let child_target_path = target.join(file_name);
1266                        copy_recursive(fs, &child_path, &child_target_path, options).await?;
1267                    }
1268                }
1269            }
1270
1271            Ok(())
1272        } else {
1273            fs.copy_file(source, target, options).await
1274        }
1275    }
1276    .boxed()
1277}
1278
1279#[cfg(test)]
1280mod tests {
1281    use super::*;
1282    use gpui::BackgroundExecutor;
1283    use serde_json::json;
1284
1285    #[gpui::test]
1286    async fn test_fake_fs(executor: BackgroundExecutor) {
1287        let fs = FakeFs::new(executor.clone());
1288        fs.insert_tree(
1289            "/root",
1290            json!({
1291                "dir1": {
1292                    "a": "A",
1293                    "b": "B"
1294                },
1295                "dir2": {
1296                    "c": "C",
1297                    "dir3": {
1298                        "d": "D"
1299                    }
1300                }
1301            }),
1302        )
1303        .await;
1304
1305        assert_eq!(
1306            fs.files(),
1307            vec![
1308                PathBuf::from("/root/dir1/a"),
1309                PathBuf::from("/root/dir1/b"),
1310                PathBuf::from("/root/dir2/c"),
1311                PathBuf::from("/root/dir2/dir3/d"),
1312            ]
1313        );
1314
1315        fs.insert_symlink("/root/dir2/link-to-dir3", "./dir3".into())
1316            .await;
1317
1318        assert_eq!(
1319            fs.canonicalize("/root/dir2/link-to-dir3".as_ref())
1320                .await
1321                .unwrap(),
1322            PathBuf::from("/root/dir2/dir3"),
1323        );
1324        assert_eq!(
1325            fs.canonicalize("/root/dir2/link-to-dir3/d".as_ref())
1326                .await
1327                .unwrap(),
1328            PathBuf::from("/root/dir2/dir3/d"),
1329        );
1330        assert_eq!(
1331            fs.load("/root/dir2/link-to-dir3/d".as_ref()).await.unwrap(),
1332            "D",
1333        );
1334    }
1335}