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