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