fs.rs

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