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        watcher
 305            .watch(path, notify::RecursiveMode::Recursive)
 306            .unwrap();
 307
 308        Box::pin(rx)
 309    }
 310
 311    fn open_repo(&self, dotgit_path: &Path) -> Option<Arc<Mutex<dyn GitRepository>>> {
 312        LibGitRepository::open(&dotgit_path)
 313            .log_err()
 314            .and_then::<Arc<Mutex<dyn GitRepository>>, _>(|libgit_repository| {
 315                Some(Arc::new(Mutex::new(libgit_repository)))
 316            })
 317    }
 318
 319    fn is_fake(&self) -> bool {
 320        false
 321    }
 322    #[cfg(any(test, feature = "test-support"))]
 323    fn as_fake(&self) -> &FakeFs {
 324        panic!("called `RealFs::as_fake`")
 325    }
 326}
 327
 328#[cfg(target_os = "macos")]
 329pub fn fs_events_paths(events: Vec<Event>) -> Vec<PathBuf> {
 330    events.into_iter().map(|event| event.path).collect()
 331}
 332
 333#[cfg(not(target_os = "macos"))]
 334pub fn fs_events_paths(events: Vec<Event>) -> Vec<PathBuf> {
 335    events
 336        .into_iter()
 337        .map(|event| event.paths.into_iter())
 338        .flatten()
 339        .collect()
 340}
 341
 342#[cfg(any(test, feature = "test-support"))]
 343pub struct FakeFs {
 344    // Use an unfair lock to ensure tests are deterministic.
 345    state: Mutex<FakeFsState>,
 346    executor: gpui::BackgroundExecutor,
 347}
 348
 349#[cfg(any(test, feature = "test-support"))]
 350struct FakeFsState {
 351    root: Arc<Mutex<FakeFsEntry>>,
 352    next_inode: u64,
 353    next_mtime: SystemTime,
 354    event_txs: Vec<smol::channel::Sender<Vec<fsevent::Event>>>,
 355    events_paused: bool,
 356    buffered_events: Vec<fsevent::Event>,
 357    metadata_call_count: usize,
 358    read_dir_call_count: usize,
 359}
 360
 361#[cfg(any(test, feature = "test-support"))]
 362#[derive(Debug)]
 363enum FakeFsEntry {
 364    File {
 365        inode: u64,
 366        mtime: SystemTime,
 367        content: String,
 368    },
 369    Dir {
 370        inode: u64,
 371        mtime: SystemTime,
 372        entries: BTreeMap<String, Arc<Mutex<FakeFsEntry>>>,
 373        git_repo_state: Option<Arc<Mutex<repository::FakeGitRepositoryState>>>,
 374    },
 375    Symlink {
 376        target: PathBuf,
 377    },
 378}
 379
 380#[cfg(any(test, feature = "test-support"))]
 381impl FakeFsState {
 382    fn read_path<'a>(&'a self, target: &Path) -> Result<Arc<Mutex<FakeFsEntry>>> {
 383        Ok(self
 384            .try_read_path(target, true)
 385            .ok_or_else(|| anyhow!("path does not exist: {}", target.display()))?
 386            .0)
 387    }
 388
 389    fn try_read_path<'a>(
 390        &'a self,
 391        target: &Path,
 392        follow_symlink: bool,
 393    ) -> Option<(Arc<Mutex<FakeFsEntry>>, PathBuf)> {
 394        let mut path = target.to_path_buf();
 395        let mut canonical_path = PathBuf::new();
 396        let mut entry_stack = Vec::new();
 397        'outer: loop {
 398            let mut path_components = path.components().peekable();
 399            while let Some(component) = path_components.next() {
 400                match component {
 401                    Component::Prefix(_) => panic!("prefix paths aren't supported"),
 402                    Component::RootDir => {
 403                        entry_stack.clear();
 404                        entry_stack.push(self.root.clone());
 405                        canonical_path.clear();
 406                        canonical_path.push("/");
 407                    }
 408                    Component::CurDir => {}
 409                    Component::ParentDir => {
 410                        entry_stack.pop()?;
 411                        canonical_path.pop();
 412                    }
 413                    Component::Normal(name) => {
 414                        let current_entry = entry_stack.last().cloned()?;
 415                        let current_entry = current_entry.lock();
 416                        if let FakeFsEntry::Dir { entries, .. } = &*current_entry {
 417                            let entry = entries.get(name.to_str().unwrap()).cloned()?;
 418                            if path_components.peek().is_some() || follow_symlink {
 419                                let entry = entry.lock();
 420                                if let FakeFsEntry::Symlink { target, .. } = &*entry {
 421                                    let mut target = target.clone();
 422                                    target.extend(path_components);
 423                                    path = target;
 424                                    continue 'outer;
 425                                }
 426                            }
 427                            entry_stack.push(entry.clone());
 428                            canonical_path.push(name);
 429                        } else {
 430                            return None;
 431                        }
 432                    }
 433                }
 434            }
 435            break;
 436        }
 437        Some((entry_stack.pop()?, canonical_path))
 438    }
 439
 440    fn write_path<Fn, T>(&self, path: &Path, callback: Fn) -> Result<T>
 441    where
 442        Fn: FnOnce(btree_map::Entry<String, Arc<Mutex<FakeFsEntry>>>) -> Result<T>,
 443    {
 444        let path = normalize_path(path);
 445        let filename = path
 446            .file_name()
 447            .ok_or_else(|| anyhow!("cannot overwrite the root"))?;
 448        let parent_path = path.parent().unwrap();
 449
 450        let parent = self.read_path(parent_path)?;
 451        let mut parent = parent.lock();
 452        let new_entry = parent
 453            .dir_entries(parent_path)?
 454            .entry(filename.to_str().unwrap().into());
 455        callback(new_entry)
 456    }
 457
 458    fn emit_event<I, T>(&mut self, paths: I)
 459    where
 460        I: IntoIterator<Item = T>,
 461        T: Into<PathBuf>,
 462    {
 463        self.buffered_events
 464            .extend(paths.into_iter().map(|path| fsevent::Event {
 465                event_id: 0,
 466                flags: fsevent::StreamFlags::empty(),
 467                path: path.into(),
 468            }));
 469
 470        if !self.events_paused {
 471            self.flush_events(self.buffered_events.len());
 472        }
 473    }
 474
 475    fn flush_events(&mut self, mut count: usize) {
 476        count = count.min(self.buffered_events.len());
 477        let events = self.buffered_events.drain(0..count).collect::<Vec<_>>();
 478        self.event_txs.retain(|tx| {
 479            let _ = tx.try_send(events.clone());
 480            !tx.is_closed()
 481        });
 482    }
 483}
 484
 485#[cfg(any(test, feature = "test-support"))]
 486lazy_static::lazy_static! {
 487    pub static ref FS_DOT_GIT: &'static OsStr = OsStr::new(".git");
 488}
 489
 490#[cfg(any(test, feature = "test-support"))]
 491impl FakeFs {
 492    pub fn new(executor: gpui::BackgroundExecutor) -> Arc<Self> {
 493        Arc::new(Self {
 494            executor,
 495            state: Mutex::new(FakeFsState {
 496                root: Arc::new(Mutex::new(FakeFsEntry::Dir {
 497                    inode: 0,
 498                    mtime: SystemTime::UNIX_EPOCH,
 499                    entries: Default::default(),
 500                    git_repo_state: None,
 501                })),
 502                next_mtime: SystemTime::UNIX_EPOCH,
 503                next_inode: 1,
 504                event_txs: Default::default(),
 505                buffered_events: Vec::new(),
 506                events_paused: false,
 507                read_dir_call_count: 0,
 508                metadata_call_count: 0,
 509            }),
 510        })
 511    }
 512
 513    pub async fn insert_file(&self, path: impl AsRef<Path>, content: String) {
 514        self.write_file_internal(path, content).unwrap()
 515    }
 516
 517    pub async fn insert_symlink(&self, path: impl AsRef<Path>, target: PathBuf) {
 518        let mut state = self.state.lock();
 519        let path = path.as_ref();
 520        let file = Arc::new(Mutex::new(FakeFsEntry::Symlink { target }));
 521        state
 522            .write_path(path.as_ref(), move |e| match e {
 523                btree_map::Entry::Vacant(e) => {
 524                    e.insert(file);
 525                    Ok(())
 526                }
 527                btree_map::Entry::Occupied(mut e) => {
 528                    *e.get_mut() = file;
 529                    Ok(())
 530                }
 531            })
 532            .unwrap();
 533        state.emit_event(&[path]);
 534    }
 535
 536    pub fn write_file_internal(&self, path: impl AsRef<Path>, content: String) -> Result<()> {
 537        let mut state = self.state.lock();
 538        let path = path.as_ref();
 539        let inode = state.next_inode;
 540        let mtime = state.next_mtime;
 541        state.next_inode += 1;
 542        state.next_mtime += Duration::from_nanos(1);
 543        let file = Arc::new(Mutex::new(FakeFsEntry::File {
 544            inode,
 545            mtime,
 546            content,
 547        }));
 548        state.write_path(path, move |entry| {
 549            match entry {
 550                btree_map::Entry::Vacant(e) => {
 551                    e.insert(file);
 552                }
 553                btree_map::Entry::Occupied(mut e) => {
 554                    *e.get_mut() = file;
 555                }
 556            }
 557            Ok(())
 558        })?;
 559        state.emit_event(&[path]);
 560        Ok(())
 561    }
 562
 563    pub fn pause_events(&self) {
 564        self.state.lock().events_paused = true;
 565    }
 566
 567    pub fn buffered_event_count(&self) -> usize {
 568        self.state.lock().buffered_events.len()
 569    }
 570
 571    pub fn flush_events(&self, count: usize) {
 572        self.state.lock().flush_events(count);
 573    }
 574
 575    #[must_use]
 576    pub fn insert_tree<'a>(
 577        &'a self,
 578        path: impl 'a + AsRef<Path> + Send,
 579        tree: serde_json::Value,
 580    ) -> futures::future::BoxFuture<'a, ()> {
 581        use futures::FutureExt as _;
 582        use serde_json::Value::*;
 583
 584        async move {
 585            let path = path.as_ref();
 586
 587            match tree {
 588                Object(map) => {
 589                    self.create_dir(path).await.unwrap();
 590                    for (name, contents) in map {
 591                        let mut path = PathBuf::from(path);
 592                        path.push(name);
 593                        self.insert_tree(&path, contents).await;
 594                    }
 595                }
 596                Null => {
 597                    self.create_dir(path).await.unwrap();
 598                }
 599                String(contents) => {
 600                    self.insert_file(&path, contents).await;
 601                }
 602                _ => {
 603                    panic!("JSON object must contain only objects, strings, or null");
 604                }
 605            }
 606        }
 607        .boxed()
 608    }
 609
 610    pub fn with_git_state<F>(&self, dot_git: &Path, emit_git_event: bool, f: F)
 611    where
 612        F: FnOnce(&mut FakeGitRepositoryState),
 613    {
 614        let mut state = self.state.lock();
 615        let entry = state.read_path(dot_git).unwrap();
 616        let mut entry = entry.lock();
 617
 618        if let FakeFsEntry::Dir { git_repo_state, .. } = &mut *entry {
 619            let repo_state = git_repo_state.get_or_insert_with(Default::default);
 620            let mut repo_state = repo_state.lock();
 621
 622            f(&mut repo_state);
 623
 624            if emit_git_event {
 625                state.emit_event([dot_git]);
 626            }
 627        } else {
 628            panic!("not a directory");
 629        }
 630    }
 631
 632    pub fn set_branch_name(&self, dot_git: &Path, branch: Option<impl Into<String>>) {
 633        self.with_git_state(dot_git, true, |state| {
 634            state.branch_name = branch.map(Into::into)
 635        })
 636    }
 637
 638    pub fn set_index_for_repo(&self, dot_git: &Path, head_state: &[(&Path, String)]) {
 639        self.with_git_state(dot_git, true, |state| {
 640            state.index_contents.clear();
 641            state.index_contents.extend(
 642                head_state
 643                    .iter()
 644                    .map(|(path, content)| (path.to_path_buf(), content.clone())),
 645            );
 646        });
 647    }
 648
 649    pub fn set_status_for_repo_via_working_copy_change(
 650        &self,
 651        dot_git: &Path,
 652        statuses: &[(&Path, GitFileStatus)],
 653    ) {
 654        self.with_git_state(dot_git, false, |state| {
 655            state.worktree_statuses.clear();
 656            state.worktree_statuses.extend(
 657                statuses
 658                    .iter()
 659                    .map(|(path, content)| ((**path).into(), content.clone())),
 660            );
 661        });
 662        self.state.lock().emit_event(
 663            statuses
 664                .iter()
 665                .map(|(path, _)| dot_git.parent().unwrap().join(path)),
 666        );
 667    }
 668
 669    pub fn set_status_for_repo_via_git_operation(
 670        &self,
 671        dot_git: &Path,
 672        statuses: &[(&Path, GitFileStatus)],
 673    ) {
 674        self.with_git_state(dot_git, true, |state| {
 675            state.worktree_statuses.clear();
 676            state.worktree_statuses.extend(
 677                statuses
 678                    .iter()
 679                    .map(|(path, content)| ((**path).into(), content.clone())),
 680            );
 681        });
 682    }
 683
 684    pub fn paths(&self, include_dot_git: bool) -> Vec<PathBuf> {
 685        let mut result = Vec::new();
 686        let mut queue = collections::VecDeque::new();
 687        queue.push_back((PathBuf::from("/"), self.state.lock().root.clone()));
 688        while let Some((path, entry)) = queue.pop_front() {
 689            if let FakeFsEntry::Dir { entries, .. } = &*entry.lock() {
 690                for (name, entry) in entries {
 691                    queue.push_back((path.join(name), entry.clone()));
 692                }
 693            }
 694            if include_dot_git
 695                || !path
 696                    .components()
 697                    .any(|component| component.as_os_str() == *FS_DOT_GIT)
 698            {
 699                result.push(path);
 700            }
 701        }
 702        result
 703    }
 704
 705    pub fn directories(&self, include_dot_git: bool) -> Vec<PathBuf> {
 706        let mut result = Vec::new();
 707        let mut queue = collections::VecDeque::new();
 708        queue.push_back((PathBuf::from("/"), self.state.lock().root.clone()));
 709        while let Some((path, entry)) = queue.pop_front() {
 710            if let FakeFsEntry::Dir { entries, .. } = &*entry.lock() {
 711                for (name, entry) in entries {
 712                    queue.push_back((path.join(name), entry.clone()));
 713                }
 714                if include_dot_git
 715                    || !path
 716                        .components()
 717                        .any(|component| component.as_os_str() == *FS_DOT_GIT)
 718                {
 719                    result.push(path);
 720                }
 721            }
 722        }
 723        result
 724    }
 725
 726    pub fn files(&self) -> Vec<PathBuf> {
 727        let mut result = Vec::new();
 728        let mut queue = collections::VecDeque::new();
 729        queue.push_back((PathBuf::from("/"), self.state.lock().root.clone()));
 730        while let Some((path, entry)) = queue.pop_front() {
 731            let e = entry.lock();
 732            match &*e {
 733                FakeFsEntry::File { .. } => result.push(path),
 734                FakeFsEntry::Dir { entries, .. } => {
 735                    for (name, entry) in entries {
 736                        queue.push_back((path.join(name), entry.clone()));
 737                    }
 738                }
 739                FakeFsEntry::Symlink { .. } => {}
 740            }
 741        }
 742        result
 743    }
 744
 745    /// How many `read_dir` calls have been issued.
 746    pub fn read_dir_call_count(&self) -> usize {
 747        self.state.lock().read_dir_call_count
 748    }
 749
 750    /// How many `metadata` calls have been issued.
 751    pub fn metadata_call_count(&self) -> usize {
 752        self.state.lock().metadata_call_count
 753    }
 754
 755    fn simulate_random_delay(&self) -> impl futures::Future<Output = ()> {
 756        self.executor.simulate_random_delay()
 757    }
 758}
 759
 760#[cfg(any(test, feature = "test-support"))]
 761impl FakeFsEntry {
 762    fn is_file(&self) -> bool {
 763        matches!(self, Self::File { .. })
 764    }
 765
 766    fn is_symlink(&self) -> bool {
 767        matches!(self, Self::Symlink { .. })
 768    }
 769
 770    fn file_content(&self, path: &Path) -> Result<&String> {
 771        if let Self::File { content, .. } = self {
 772            Ok(content)
 773        } else {
 774            Err(anyhow!("not a file: {}", path.display()))
 775        }
 776    }
 777
 778    fn set_file_content(&mut self, path: &Path, new_content: String) -> Result<()> {
 779        if let Self::File { content, mtime, .. } = self {
 780            *mtime = SystemTime::now();
 781            *content = new_content;
 782            Ok(())
 783        } else {
 784            Err(anyhow!("not a file: {}", path.display()))
 785        }
 786    }
 787
 788    fn dir_entries(
 789        &mut self,
 790        path: &Path,
 791    ) -> Result<&mut BTreeMap<String, Arc<Mutex<FakeFsEntry>>>> {
 792        if let Self::Dir { entries, .. } = self {
 793            Ok(entries)
 794        } else {
 795            Err(anyhow!("not a directory: {}", path.display()))
 796        }
 797    }
 798}
 799
 800#[cfg(any(test, feature = "test-support"))]
 801#[async_trait::async_trait]
 802impl Fs for FakeFs {
 803    async fn create_dir(&self, path: &Path) -> Result<()> {
 804        self.simulate_random_delay().await;
 805
 806        let mut created_dirs = Vec::new();
 807        let mut cur_path = PathBuf::new();
 808        for component in path.components() {
 809            let mut state = self.state.lock();
 810            cur_path.push(component);
 811            if cur_path == Path::new("/") {
 812                continue;
 813            }
 814
 815            let inode = state.next_inode;
 816            let mtime = state.next_mtime;
 817            state.next_mtime += Duration::from_nanos(1);
 818            state.next_inode += 1;
 819            state.write_path(&cur_path, |entry| {
 820                entry.or_insert_with(|| {
 821                    created_dirs.push(cur_path.clone());
 822                    Arc::new(Mutex::new(FakeFsEntry::Dir {
 823                        inode,
 824                        mtime,
 825                        entries: Default::default(),
 826                        git_repo_state: None,
 827                    }))
 828                });
 829                Ok(())
 830            })?
 831        }
 832
 833        self.state.lock().emit_event(&created_dirs);
 834        Ok(())
 835    }
 836
 837    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
 838        self.simulate_random_delay().await;
 839        let mut state = self.state.lock();
 840        let inode = state.next_inode;
 841        let mtime = state.next_mtime;
 842        state.next_mtime += Duration::from_nanos(1);
 843        state.next_inode += 1;
 844        let file = Arc::new(Mutex::new(FakeFsEntry::File {
 845            inode,
 846            mtime,
 847            content: String::new(),
 848        }));
 849        state.write_path(path, |entry| {
 850            match entry {
 851                btree_map::Entry::Occupied(mut e) => {
 852                    if options.overwrite {
 853                        *e.get_mut() = file;
 854                    } else if !options.ignore_if_exists {
 855                        return Err(anyhow!("path already exists: {}", path.display()));
 856                    }
 857                }
 858                btree_map::Entry::Vacant(e) => {
 859                    e.insert(file);
 860                }
 861            }
 862            Ok(())
 863        })?;
 864        state.emit_event(&[path]);
 865        Ok(())
 866    }
 867
 868    async fn rename(&self, old_path: &Path, new_path: &Path, options: RenameOptions) -> Result<()> {
 869        self.simulate_random_delay().await;
 870
 871        let old_path = normalize_path(old_path);
 872        let new_path = normalize_path(new_path);
 873
 874        let mut state = self.state.lock();
 875        let moved_entry = state.write_path(&old_path, |e| {
 876            if let btree_map::Entry::Occupied(e) = e {
 877                Ok(e.get().clone())
 878            } else {
 879                Err(anyhow!("path does not exist: {}", &old_path.display()))
 880            }
 881        })?;
 882
 883        state.write_path(&new_path, |e| {
 884            match e {
 885                btree_map::Entry::Occupied(mut e) => {
 886                    if options.overwrite {
 887                        *e.get_mut() = moved_entry;
 888                    } else if !options.ignore_if_exists {
 889                        return Err(anyhow!("path already exists: {}", new_path.display()));
 890                    }
 891                }
 892                btree_map::Entry::Vacant(e) => {
 893                    e.insert(moved_entry);
 894                }
 895            }
 896            Ok(())
 897        })?;
 898
 899        state
 900            .write_path(&old_path, |e| {
 901                if let btree_map::Entry::Occupied(e) = e {
 902                    Ok(e.remove())
 903                } else {
 904                    unreachable!()
 905                }
 906            })
 907            .unwrap();
 908
 909        state.emit_event(&[old_path, new_path]);
 910        Ok(())
 911    }
 912
 913    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
 914        self.simulate_random_delay().await;
 915
 916        let source = normalize_path(source);
 917        let target = normalize_path(target);
 918        let mut state = self.state.lock();
 919        let mtime = state.next_mtime;
 920        let inode = util::post_inc(&mut state.next_inode);
 921        state.next_mtime += Duration::from_nanos(1);
 922        let source_entry = state.read_path(&source)?;
 923        let content = source_entry.lock().file_content(&source)?.clone();
 924        let entry = state.write_path(&target, |e| match e {
 925            btree_map::Entry::Occupied(e) => {
 926                if options.overwrite {
 927                    Ok(Some(e.get().clone()))
 928                } else if !options.ignore_if_exists {
 929                    return Err(anyhow!("{target:?} already exists"));
 930                } else {
 931                    Ok(None)
 932                }
 933            }
 934            btree_map::Entry::Vacant(e) => Ok(Some(
 935                e.insert(Arc::new(Mutex::new(FakeFsEntry::File {
 936                    inode,
 937                    mtime,
 938                    content: String::new(),
 939                })))
 940                .clone(),
 941            )),
 942        })?;
 943        if let Some(entry) = entry {
 944            entry.lock().set_file_content(&target, content)?;
 945        }
 946        state.emit_event(&[target]);
 947        Ok(())
 948    }
 949
 950    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 951        self.simulate_random_delay().await;
 952
 953        let path = normalize_path(path);
 954        let parent_path = path
 955            .parent()
 956            .ok_or_else(|| anyhow!("cannot remove the root"))?;
 957        let base_name = path.file_name().unwrap();
 958
 959        let mut state = self.state.lock();
 960        let parent_entry = state.read_path(parent_path)?;
 961        let mut parent_entry = parent_entry.lock();
 962        let entry = parent_entry
 963            .dir_entries(parent_path)?
 964            .entry(base_name.to_str().unwrap().into());
 965
 966        match entry {
 967            btree_map::Entry::Vacant(_) => {
 968                if !options.ignore_if_not_exists {
 969                    return Err(anyhow!("{path:?} does not exist"));
 970                }
 971            }
 972            btree_map::Entry::Occupied(e) => {
 973                {
 974                    let mut entry = e.get().lock();
 975                    let children = entry.dir_entries(&path)?;
 976                    if !options.recursive && !children.is_empty() {
 977                        return Err(anyhow!("{path:?} is not empty"));
 978                    }
 979                }
 980                e.remove();
 981            }
 982        }
 983        state.emit_event(&[path]);
 984        Ok(())
 985    }
 986
 987    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
 988        self.simulate_random_delay().await;
 989
 990        let path = normalize_path(path);
 991        let parent_path = path
 992            .parent()
 993            .ok_or_else(|| anyhow!("cannot remove the root"))?;
 994        let base_name = path.file_name().unwrap();
 995        let mut state = self.state.lock();
 996        let parent_entry = state.read_path(parent_path)?;
 997        let mut parent_entry = parent_entry.lock();
 998        let entry = parent_entry
 999            .dir_entries(parent_path)?
1000            .entry(base_name.to_str().unwrap().into());
1001        match entry {
1002            btree_map::Entry::Vacant(_) => {
1003                if !options.ignore_if_not_exists {
1004                    return Err(anyhow!("{path:?} does not exist"));
1005                }
1006            }
1007            btree_map::Entry::Occupied(e) => {
1008                e.get().lock().file_content(&path)?;
1009                e.remove();
1010            }
1011        }
1012        state.emit_event(&[path]);
1013        Ok(())
1014    }
1015
1016    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read>> {
1017        let text = self.load(path).await?;
1018        Ok(Box::new(io::Cursor::new(text)))
1019    }
1020
1021    async fn load(&self, path: &Path) -> Result<String> {
1022        let path = normalize_path(path);
1023        self.simulate_random_delay().await;
1024        let state = self.state.lock();
1025        let entry = state.read_path(&path)?;
1026        let entry = entry.lock();
1027        entry.file_content(&path).cloned()
1028    }
1029
1030    async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
1031        self.simulate_random_delay().await;
1032        let path = normalize_path(path.as_path());
1033        self.write_file_internal(path, data.to_string())?;
1034
1035        Ok(())
1036    }
1037
1038    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
1039        self.simulate_random_delay().await;
1040        let path = normalize_path(path);
1041        let content = chunks(text, line_ending).collect();
1042        if let Some(path) = path.parent() {
1043            self.create_dir(path).await?;
1044        }
1045        self.write_file_internal(path, content)?;
1046        Ok(())
1047    }
1048
1049    async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
1050        let path = normalize_path(path);
1051        self.simulate_random_delay().await;
1052        let state = self.state.lock();
1053        if let Some((_, canonical_path)) = state.try_read_path(&path, true) {
1054            Ok(canonical_path)
1055        } else {
1056            Err(anyhow!("path does not exist: {}", path.display()))
1057        }
1058    }
1059
1060    async fn is_file(&self, path: &Path) -> bool {
1061        let path = normalize_path(path);
1062        self.simulate_random_delay().await;
1063        let state = self.state.lock();
1064        if let Some((entry, _)) = state.try_read_path(&path, true) {
1065            entry.lock().is_file()
1066        } else {
1067            false
1068        }
1069    }
1070
1071    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
1072        self.simulate_random_delay().await;
1073        let path = normalize_path(path);
1074        let mut state = self.state.lock();
1075        state.metadata_call_count += 1;
1076        if let Some((mut entry, _)) = state.try_read_path(&path, false) {
1077            let is_symlink = entry.lock().is_symlink();
1078            if is_symlink {
1079                if let Some(e) = state.try_read_path(&path, true).map(|e| e.0) {
1080                    entry = e;
1081                } else {
1082                    return Ok(None);
1083                }
1084            }
1085
1086            let entry = entry.lock();
1087            Ok(Some(match &*entry {
1088                FakeFsEntry::File { inode, mtime, .. } => Metadata {
1089                    inode: *inode,
1090                    mtime: *mtime,
1091                    is_dir: false,
1092                    is_symlink,
1093                },
1094                FakeFsEntry::Dir { inode, mtime, .. } => Metadata {
1095                    inode: *inode,
1096                    mtime: *mtime,
1097                    is_dir: true,
1098                    is_symlink,
1099                },
1100                FakeFsEntry::Symlink { .. } => unreachable!(),
1101            }))
1102        } else {
1103            Ok(None)
1104        }
1105    }
1106
1107    async fn read_link(&self, path: &Path) -> Result<PathBuf> {
1108        self.simulate_random_delay().await;
1109        let path = normalize_path(path);
1110        let state = self.state.lock();
1111        if let Some((entry, _)) = state.try_read_path(&path, false) {
1112            let entry = entry.lock();
1113            if let FakeFsEntry::Symlink { target } = &*entry {
1114                Ok(target.clone())
1115            } else {
1116                Err(anyhow!("not a symlink: {}", path.display()))
1117            }
1118        } else {
1119            Err(anyhow!("path does not exist: {}", path.display()))
1120        }
1121    }
1122
1123    async fn read_dir(
1124        &self,
1125        path: &Path,
1126    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
1127        self.simulate_random_delay().await;
1128        let path = normalize_path(path);
1129        let mut state = self.state.lock();
1130        state.read_dir_call_count += 1;
1131        let entry = state.read_path(&path)?;
1132        let mut entry = entry.lock();
1133        let children = entry.dir_entries(&path)?;
1134        let paths = children
1135            .keys()
1136            .map(|file_name| Ok(path.join(file_name)))
1137            .collect::<Vec<_>>();
1138        Ok(Box::pin(futures::stream::iter(paths)))
1139    }
1140
1141    async fn watch(
1142        &self,
1143        path: &Path,
1144        _: Duration,
1145    ) -> Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>> {
1146        self.simulate_random_delay().await;
1147        let (tx, rx) = smol::channel::unbounded();
1148        self.state.lock().event_txs.push(tx);
1149        let path = path.to_path_buf();
1150        let executor = self.executor.clone();
1151        Box::pin(futures::StreamExt::filter(rx, move |events| {
1152            let result = events.iter().any(|event| event.path.starts_with(&path));
1153            let executor = executor.clone();
1154            async move {
1155                executor.simulate_random_delay().await;
1156                result
1157            }
1158        }))
1159    }
1160
1161    fn open_repo(&self, abs_dot_git: &Path) -> Option<Arc<Mutex<dyn GitRepository>>> {
1162        let state = self.state.lock();
1163        let entry = state.read_path(abs_dot_git).unwrap();
1164        let mut entry = entry.lock();
1165        if let FakeFsEntry::Dir { git_repo_state, .. } = &mut *entry {
1166            let state = git_repo_state
1167                .get_or_insert_with(|| Arc::new(Mutex::new(FakeGitRepositoryState::default())))
1168                .clone();
1169            Some(repository::FakeGitRepository::open(state))
1170        } else {
1171            None
1172        }
1173    }
1174
1175    fn is_fake(&self) -> bool {
1176        true
1177    }
1178
1179    #[cfg(any(test, feature = "test-support"))]
1180    fn as_fake(&self) -> &FakeFs {
1181        self
1182    }
1183}
1184
1185fn chunks(rope: &Rope, line_ending: LineEnding) -> impl Iterator<Item = &str> {
1186    rope.chunks().flat_map(move |chunk| {
1187        let mut newline = false;
1188        chunk.split('\n').flat_map(move |line| {
1189            let ending = if newline {
1190                Some(line_ending.as_str())
1191            } else {
1192                None
1193            };
1194            newline = true;
1195            ending.into_iter().chain([line])
1196        })
1197    })
1198}
1199
1200pub fn normalize_path(path: &Path) -> PathBuf {
1201    let mut components = path.components().peekable();
1202    let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
1203        components.next();
1204        PathBuf::from(c.as_os_str())
1205    } else {
1206        PathBuf::new()
1207    };
1208
1209    for component in components {
1210        match component {
1211            Component::Prefix(..) => unreachable!(),
1212            Component::RootDir => {
1213                ret.push(component.as_os_str());
1214            }
1215            Component::CurDir => {}
1216            Component::ParentDir => {
1217                ret.pop();
1218            }
1219            Component::Normal(c) => {
1220                ret.push(c);
1221            }
1222        }
1223    }
1224    ret
1225}
1226
1227pub fn copy_recursive<'a>(
1228    fs: &'a dyn Fs,
1229    source: &'a Path,
1230    target: &'a Path,
1231    options: CopyOptions,
1232) -> BoxFuture<'a, Result<()>> {
1233    use futures::future::FutureExt;
1234
1235    async move {
1236        let metadata = fs
1237            .metadata(source)
1238            .await?
1239            .ok_or_else(|| anyhow!("path does not exist: {}", source.display()))?;
1240        if metadata.is_dir {
1241            if !options.overwrite && fs.metadata(target).await.is_ok_and(|m| m.is_some()) {
1242                if options.ignore_if_exists {
1243                    return Ok(());
1244                } else {
1245                    return Err(anyhow!("{target:?} already exists"));
1246                }
1247            }
1248
1249            let _ = fs
1250                .remove_dir(
1251                    target,
1252                    RemoveOptions {
1253                        recursive: true,
1254                        ignore_if_not_exists: true,
1255                    },
1256                )
1257                .await;
1258            fs.create_dir(target).await?;
1259            let mut children = fs.read_dir(source).await?;
1260            while let Some(child_path) = children.next().await {
1261                if let Ok(child_path) = child_path {
1262                    if let Some(file_name) = child_path.file_name() {
1263                        let child_target_path = target.join(file_name);
1264                        copy_recursive(fs, &child_path, &child_target_path, options).await?;
1265                    }
1266                }
1267            }
1268
1269            Ok(())
1270        } else {
1271            fs.copy_file(source, target, options).await
1272        }
1273    }
1274    .boxed()
1275}
1276
1277#[cfg(test)]
1278mod tests {
1279    use super::*;
1280    use gpui::BackgroundExecutor;
1281    use serde_json::json;
1282
1283    #[gpui::test]
1284    async fn test_fake_fs(executor: BackgroundExecutor) {
1285        let fs = FakeFs::new(executor.clone());
1286        fs.insert_tree(
1287            "/root",
1288            json!({
1289                "dir1": {
1290                    "a": "A",
1291                    "b": "B"
1292                },
1293                "dir2": {
1294                    "c": "C",
1295                    "dir3": {
1296                        "d": "D"
1297                    }
1298                }
1299            }),
1300        )
1301        .await;
1302
1303        assert_eq!(
1304            fs.files(),
1305            vec![
1306                PathBuf::from("/root/dir1/a"),
1307                PathBuf::from("/root/dir1/b"),
1308                PathBuf::from("/root/dir2/c"),
1309                PathBuf::from("/root/dir2/dir3/d"),
1310            ]
1311        );
1312
1313        fs.insert_symlink("/root/dir2/link-to-dir3", "./dir3".into())
1314            .await;
1315
1316        assert_eq!(
1317            fs.canonicalize("/root/dir2/link-to-dir3".as_ref())
1318                .await
1319                .unwrap(),
1320            PathBuf::from("/root/dir2/dir3"),
1321        );
1322        assert_eq!(
1323            fs.canonicalize("/root/dir2/link-to-dir3/d".as_ref())
1324                .await
1325                .unwrap(),
1326            PathBuf::from("/root/dir2/dir3/d"),
1327        );
1328        assert_eq!(
1329            fs.load("/root/dir2/link-to-dir3/d".as_ref()).await.unwrap(),
1330            "D",
1331        );
1332    }
1333}