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