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