fs.rs

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