fs.rs

  1use anyhow::{anyhow, Result};
  2use fsevent::EventStream;
  3use futures::{Stream, StreamExt};
  4use smol::io::{AsyncReadExt, AsyncWriteExt};
  5use std::{
  6    io,
  7    os::unix::fs::MetadataExt,
  8    path::{Path, PathBuf},
  9    pin::Pin,
 10    time::{Duration, SystemTime},
 11};
 12use text::Rope;
 13
 14#[async_trait::async_trait]
 15pub trait Fs: Send + Sync {
 16    async fn create_dir(&self, path: &Path) -> Result<()>;
 17    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()>;
 18    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()>;
 19    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()>;
 20    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;
 21    async fn load(&self, path: &Path) -> Result<String>;
 22    async fn save(&self, path: &Path, text: &Rope) -> Result<()>;
 23    async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
 24    async fn is_file(&self, path: &Path) -> bool;
 25    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;
 26    async fn read_dir(
 27        &self,
 28        path: &Path,
 29    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>>;
 30    async fn watch(
 31        &self,
 32        path: &Path,
 33        latency: Duration,
 34    ) -> Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>>;
 35    fn is_fake(&self) -> bool;
 36    #[cfg(any(test, feature = "test-support"))]
 37    fn as_fake(&self) -> &FakeFs;
 38}
 39
 40#[derive(Copy, Clone, Default)]
 41pub struct CreateOptions {
 42    pub overwrite: bool,
 43    pub ignore_if_exists: bool,
 44}
 45
 46#[derive(Copy, Clone, Default)]
 47pub struct RenameOptions {
 48    pub overwrite: bool,
 49    pub ignore_if_exists: bool,
 50}
 51
 52#[derive(Copy, Clone, Default)]
 53pub struct RemoveOptions {
 54    pub recursive: bool,
 55    pub ignore_if_not_exists: bool,
 56}
 57
 58#[derive(Clone, Debug)]
 59pub struct Metadata {
 60    pub inode: u64,
 61    pub mtime: SystemTime,
 62    pub is_symlink: bool,
 63    pub is_dir: bool,
 64}
 65
 66pub struct RealFs;
 67
 68#[async_trait::async_trait]
 69impl Fs for RealFs {
 70    async fn create_dir(&self, path: &Path) -> Result<()> {
 71        Ok(smol::fs::create_dir_all(path).await?)
 72    }
 73
 74    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
 75        let mut open_options = smol::fs::OpenOptions::new();
 76        open_options.write(true).create(true);
 77        if options.overwrite {
 78            open_options.truncate(true);
 79        } else if !options.ignore_if_exists {
 80            open_options.create_new(true);
 81        }
 82        open_options.open(path).await?;
 83        Ok(())
 84    }
 85
 86    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()> {
 87        if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
 88            if options.ignore_if_exists {
 89                return Ok(());
 90            } else {
 91                return Err(anyhow!("{target:?} already exists"));
 92            }
 93        }
 94
 95        smol::fs::rename(source, target).await?;
 96        Ok(())
 97    }
 98
 99    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
100        let result = if options.recursive {
101            smol::fs::remove_dir_all(path).await
102        } else {
103            smol::fs::remove_dir(path).await
104        };
105        match result {
106            Ok(()) => Ok(()),
107            Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
108                Ok(())
109            }
110            Err(err) => Err(err)?,
111        }
112    }
113
114    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
115        match smol::fs::remove_file(path).await {
116            Ok(()) => Ok(()),
117            Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
118                Ok(())
119            }
120            Err(err) => Err(err)?,
121        }
122    }
123
124    async fn load(&self, path: &Path) -> Result<String> {
125        let mut file = smol::fs::File::open(path).await?;
126        let mut text = String::new();
127        file.read_to_string(&mut text).await?;
128        Ok(text)
129    }
130
131    async fn save(&self, path: &Path, text: &Rope) -> Result<()> {
132        let buffer_size = text.summary().bytes.min(10 * 1024);
133        let file = smol::fs::File::create(path).await?;
134        let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
135        for chunk in text.chunks() {
136            writer.write_all(chunk.as_bytes()).await?;
137        }
138        writer.flush().await?;
139        Ok(())
140    }
141
142    async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
143        Ok(smol::fs::canonicalize(path).await?)
144    }
145
146    async fn is_file(&self, path: &Path) -> bool {
147        smol::fs::metadata(path)
148            .await
149            .map_or(false, |metadata| metadata.is_file())
150    }
151
152    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
153        let symlink_metadata = match smol::fs::symlink_metadata(path).await {
154            Ok(metadata) => metadata,
155            Err(err) => {
156                return match (err.kind(), err.raw_os_error()) {
157                    (io::ErrorKind::NotFound, _) => Ok(None),
158                    (io::ErrorKind::Other, Some(libc::ENOTDIR)) => Ok(None),
159                    _ => Err(anyhow::Error::new(err)),
160                }
161            }
162        };
163
164        let is_symlink = symlink_metadata.file_type().is_symlink();
165        let metadata = if is_symlink {
166            smol::fs::metadata(path).await?
167        } else {
168            symlink_metadata
169        };
170        Ok(Some(Metadata {
171            inode: metadata.ino(),
172            mtime: metadata.modified().unwrap(),
173            is_symlink,
174            is_dir: metadata.file_type().is_dir(),
175        }))
176    }
177
178    async fn read_dir(
179        &self,
180        path: &Path,
181    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
182        let result = smol::fs::read_dir(path).await?.map(|entry| match entry {
183            Ok(entry) => Ok(entry.path()),
184            Err(error) => Err(anyhow!("failed to read dir entry {:?}", error)),
185        });
186        Ok(Box::pin(result))
187    }
188
189    async fn watch(
190        &self,
191        path: &Path,
192        latency: Duration,
193    ) -> Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>> {
194        let (tx, rx) = smol::channel::unbounded();
195        let (stream, handle) = EventStream::new(&[path], latency);
196        std::mem::forget(handle);
197        std::thread::spawn(move || {
198            stream.run(move |events| smol::block_on(tx.send(events)).is_ok());
199        });
200        Box::pin(rx)
201    }
202
203    fn is_fake(&self) -> bool {
204        false
205    }
206
207    #[cfg(any(test, feature = "test-support"))]
208    fn as_fake(&self) -> &FakeFs {
209        panic!("called `RealFs::as_fake`")
210    }
211}
212
213#[cfg(any(test, feature = "test-support"))]
214#[derive(Clone, Debug)]
215struct FakeFsEntry {
216    metadata: Metadata,
217    content: Option<String>,
218}
219
220#[cfg(any(test, feature = "test-support"))]
221struct FakeFsState {
222    entries: std::collections::BTreeMap<PathBuf, FakeFsEntry>,
223    next_inode: u64,
224    events_tx: postage::broadcast::Sender<Vec<fsevent::Event>>,
225}
226
227#[cfg(any(test, feature = "test-support"))]
228impl FakeFsState {
229    fn validate_path(&self, path: &Path) -> Result<()> {
230        if path.is_absolute()
231            && path
232                .parent()
233                .and_then(|path| self.entries.get(path))
234                .map_or(false, |e| e.metadata.is_dir)
235        {
236            Ok(())
237        } else {
238            Err(anyhow!("invalid path {:?}", path))
239        }
240    }
241
242    async fn emit_event<I, T>(&mut self, paths: I)
243    where
244        I: IntoIterator<Item = T>,
245        T: Into<PathBuf>,
246    {
247        use postage::prelude::Sink as _;
248
249        let events = paths
250            .into_iter()
251            .map(|path| fsevent::Event {
252                event_id: 0,
253                flags: fsevent::StreamFlags::empty(),
254                path: path.into(),
255            })
256            .collect();
257
258        let _ = self.events_tx.send(events).await;
259    }
260}
261
262#[cfg(any(test, feature = "test-support"))]
263pub struct FakeFs {
264    // Use an unfair lock to ensure tests are deterministic.
265    state: futures::lock::Mutex<FakeFsState>,
266    executor: std::sync::Arc<gpui::executor::Background>,
267}
268
269#[cfg(any(test, feature = "test-support"))]
270impl FakeFs {
271    pub fn new(executor: std::sync::Arc<gpui::executor::Background>) -> std::sync::Arc<Self> {
272        let (events_tx, _) = postage::broadcast::channel(2048);
273        let mut entries = std::collections::BTreeMap::new();
274        entries.insert(
275            Path::new("/").to_path_buf(),
276            FakeFsEntry {
277                metadata: Metadata {
278                    inode: 0,
279                    mtime: SystemTime::now(),
280                    is_dir: true,
281                    is_symlink: false,
282                },
283                content: None,
284            },
285        );
286        std::sync::Arc::new(Self {
287            executor,
288            state: futures::lock::Mutex::new(FakeFsState {
289                entries,
290                next_inode: 1,
291                events_tx,
292            }),
293        })
294    }
295
296    pub async fn insert_dir(&self, path: impl AsRef<Path>) {
297        let mut state = self.state.lock().await;
298        let path = path.as_ref();
299        state.validate_path(path).unwrap();
300
301        let inode = state.next_inode;
302        state.next_inode += 1;
303        state.entries.insert(
304            path.to_path_buf(),
305            FakeFsEntry {
306                metadata: Metadata {
307                    inode,
308                    mtime: SystemTime::now(),
309                    is_dir: true,
310                    is_symlink: false,
311                },
312                content: None,
313            },
314        );
315        state.emit_event(&[path]).await;
316    }
317
318    pub async fn insert_file(&self, path: impl AsRef<Path>, content: String) {
319        let mut state = self.state.lock().await;
320        let path = path.as_ref();
321        state.validate_path(path).unwrap();
322
323        let inode = state.next_inode;
324        state.next_inode += 1;
325        state.entries.insert(
326            path.to_path_buf(),
327            FakeFsEntry {
328                metadata: Metadata {
329                    inode,
330                    mtime: SystemTime::now(),
331                    is_dir: false,
332                    is_symlink: false,
333                },
334                content: Some(content),
335            },
336        );
337        state.emit_event(&[path]).await;
338    }
339
340    #[must_use]
341    pub fn insert_tree<'a>(
342        &'a self,
343        path: impl 'a + AsRef<Path> + Send,
344        tree: serde_json::Value,
345    ) -> futures::future::BoxFuture<'a, ()> {
346        use futures::FutureExt as _;
347        use serde_json::Value::*;
348
349        async move {
350            let path = path.as_ref();
351
352            match tree {
353                Object(map) => {
354                    self.insert_dir(path).await;
355                    for (name, contents) in map {
356                        let mut path = PathBuf::from(path);
357                        path.push(name);
358                        self.insert_tree(&path, contents).await;
359                    }
360                }
361                Null => {
362                    self.insert_dir(&path).await;
363                }
364                String(contents) => {
365                    self.insert_file(&path, contents).await;
366                }
367                _ => {
368                    panic!("JSON object must contain only objects, strings, or null");
369                }
370            }
371        }
372        .boxed()
373    }
374}
375
376#[cfg(any(test, feature = "test-support"))]
377#[async_trait::async_trait]
378impl Fs for FakeFs {
379    async fn create_dir(&self, path: &Path) -> Result<()> {
380        self.executor.simulate_random_delay().await;
381        let state = &mut *self.state.lock().await;
382        let mut ancestor_path = PathBuf::new();
383        let mut created_dir_paths = Vec::new();
384        for component in path.components() {
385            ancestor_path.push(component);
386            let entry = state
387                .entries
388                .entry(ancestor_path.clone())
389                .or_insert_with(|| {
390                    let inode = state.next_inode;
391                    state.next_inode += 1;
392                    created_dir_paths.push(ancestor_path.clone());
393                    FakeFsEntry {
394                        metadata: Metadata {
395                            inode,
396                            mtime: SystemTime::now(),
397                            is_dir: true,
398                            is_symlink: false,
399                        },
400                        content: None,
401                    }
402                });
403            if !entry.metadata.is_dir {
404                return Err(anyhow!(
405                    "cannot create directory because {:?} is a file",
406                    ancestor_path
407                ));
408            }
409        }
410        state.emit_event(&created_dir_paths).await;
411
412        Ok(())
413    }
414
415    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
416        self.executor.simulate_random_delay().await;
417        let mut state = self.state.lock().await;
418        state.validate_path(path)?;
419        if let Some(entry) = state.entries.get_mut(path) {
420            if entry.metadata.is_dir || entry.metadata.is_symlink {
421                return Err(anyhow!(
422                    "cannot create file because {:?} is a dir or a symlink",
423                    path
424                ));
425            }
426
427            if options.overwrite {
428                entry.metadata.mtime = SystemTime::now();
429                entry.content = Some(Default::default());
430            } else if !options.ignore_if_exists {
431                return Err(anyhow!(
432                    "cannot create file because {:?} already exists",
433                    path
434                ));
435            }
436        } else {
437            let inode = state.next_inode;
438            state.next_inode += 1;
439            let entry = FakeFsEntry {
440                metadata: Metadata {
441                    inode,
442                    mtime: SystemTime::now(),
443                    is_dir: false,
444                    is_symlink: false,
445                },
446                content: Some(Default::default()),
447            };
448            state.entries.insert(path.to_path_buf(), entry);
449        }
450        state.emit_event(&[path]).await;
451
452        Ok(())
453    }
454
455    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()> {
456        let mut state = self.state.lock().await;
457        state.validate_path(source)?;
458        state.validate_path(target)?;
459
460        if !options.overwrite && state.entries.contains_key(target) {
461            if options.ignore_if_exists {
462                return Ok(());
463            } else {
464                return Err(anyhow!("{target:?} already exists"));
465            }
466        }
467
468        let mut removed = Vec::new();
469        state.entries.retain(|path, entry| {
470            if let Ok(relative_path) = path.strip_prefix(source) {
471                removed.push((relative_path.to_path_buf(), entry.clone()));
472                false
473            } else {
474                true
475            }
476        });
477
478        for (relative_path, entry) in removed {
479            let new_path = target.join(relative_path);
480            state.entries.insert(new_path, entry);
481        }
482
483        state.emit_event(&[source, target]).await;
484        Ok(())
485    }
486
487    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
488        let mut state = self.state.lock().await;
489        state.validate_path(path)?;
490        if let Some(entry) = state.entries.get(path) {
491            if !entry.metadata.is_dir {
492                return Err(anyhow!("cannot remove {path:?} because it is not a dir"));
493            }
494
495            if !options.recursive {
496                let descendants = state
497                    .entries
498                    .keys()
499                    .filter(|path| path.starts_with(path))
500                    .count();
501                if descendants > 1 {
502                    return Err(anyhow!("{path:?} is not empty"));
503                }
504            }
505
506            state.entries.retain(|path, _| !path.starts_with(path));
507            state.emit_event(&[path]).await;
508        } else if !options.ignore_if_not_exists {
509            return Err(anyhow!("{path:?} does not exist"));
510        }
511
512        Ok(())
513    }
514
515    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
516        let mut state = self.state.lock().await;
517        state.validate_path(path)?;
518        if let Some(entry) = state.entries.get(path) {
519            if entry.metadata.is_dir {
520                return Err(anyhow!("cannot remove {path:?} because it is not a file"));
521            }
522
523            state.entries.remove(path);
524            state.emit_event(&[path]).await;
525        } else if !options.ignore_if_not_exists {
526            return Err(anyhow!("{path:?} does not exist"));
527        }
528        Ok(())
529    }
530
531    async fn load(&self, path: &Path) -> Result<String> {
532        self.executor.simulate_random_delay().await;
533        let state = self.state.lock().await;
534        let text = state
535            .entries
536            .get(path)
537            .and_then(|e| e.content.as_ref())
538            .ok_or_else(|| anyhow!("file {:?} does not exist", path))?;
539        Ok(text.clone())
540    }
541
542    async fn save(&self, path: &Path, text: &Rope) -> Result<()> {
543        self.executor.simulate_random_delay().await;
544        let mut state = self.state.lock().await;
545        state.validate_path(path)?;
546        if let Some(entry) = state.entries.get_mut(path) {
547            if entry.metadata.is_dir {
548                Err(anyhow!("cannot overwrite a directory with a file"))
549            } else {
550                entry.content = Some(text.chunks().collect());
551                entry.metadata.mtime = SystemTime::now();
552                state.emit_event(&[path]).await;
553                Ok(())
554            }
555        } else {
556            let inode = state.next_inode;
557            state.next_inode += 1;
558            let entry = FakeFsEntry {
559                metadata: Metadata {
560                    inode,
561                    mtime: SystemTime::now(),
562                    is_dir: false,
563                    is_symlink: false,
564                },
565                content: Some(text.chunks().collect()),
566            };
567            state.entries.insert(path.to_path_buf(), entry);
568            state.emit_event(&[path]).await;
569            Ok(())
570        }
571    }
572
573    async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
574        self.executor.simulate_random_delay().await;
575        Ok(path.to_path_buf())
576    }
577
578    async fn is_file(&self, path: &Path) -> bool {
579        self.executor.simulate_random_delay().await;
580        let state = self.state.lock().await;
581        state
582            .entries
583            .get(path)
584            .map_or(false, |entry| !entry.metadata.is_dir)
585    }
586
587    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
588        self.executor.simulate_random_delay().await;
589        let state = self.state.lock().await;
590        Ok(state.entries.get(path).map(|entry| entry.metadata.clone()))
591    }
592
593    async fn read_dir(
594        &self,
595        abs_path: &Path,
596    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
597        use futures::{future, stream};
598        self.executor.simulate_random_delay().await;
599        let state = self.state.lock().await;
600        let abs_path = abs_path.to_path_buf();
601        Ok(Box::pin(stream::iter(state.entries.clone()).filter_map(
602            move |(child_path, _)| {
603                future::ready(if child_path.parent() == Some(&abs_path) {
604                    Some(Ok(child_path))
605                } else {
606                    None
607                })
608            },
609        )))
610    }
611
612    async fn watch(
613        &self,
614        path: &Path,
615        _: Duration,
616    ) -> Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>> {
617        let state = self.state.lock().await;
618        self.executor.simulate_random_delay().await;
619        let rx = state.events_tx.subscribe();
620        let path = path.to_path_buf();
621        Box::pin(futures::StreamExt::filter(rx, move |events| {
622            let result = events.iter().any(|event| event.path.starts_with(&path));
623            async move { result }
624        }))
625    }
626
627    fn is_fake(&self) -> bool {
628        true
629    }
630
631    #[cfg(any(test, feature = "test-support"))]
632    fn as_fake(&self) -> &FakeFs {
633        self
634    }
635}