1#[cfg(target_os = "macos")]
2mod mac_watcher;
3
4#[cfg(not(target_os = "macos"))]
5pub mod fs_watcher;
6
7use parking_lot::Mutex;
8use std::sync::atomic::{AtomicUsize, Ordering};
9use std::time::Instant;
10
11use anyhow::{Context as _, Result, anyhow};
12#[cfg(any(target_os = "linux", target_os = "freebsd"))]
13use ashpd::desktop::trash;
14use futures::stream::iter;
15use gpui::App;
16use gpui::BackgroundExecutor;
17use gpui::Global;
18use gpui::ReadGlobal as _;
19use gpui::SharedString;
20use std::borrow::Cow;
21use util::command::new_smol_command;
22
23#[cfg(unix)]
24use std::os::fd::{AsFd, AsRawFd};
25
26#[cfg(unix)]
27use std::os::unix::fs::{FileTypeExt, MetadataExt};
28
29#[cfg(any(target_os = "macos", target_os = "freebsd"))]
30use std::mem::MaybeUninit;
31
32use async_tar::Archive;
33use futures::{AsyncRead, Stream, StreamExt, future::BoxFuture};
34use git::repository::{GitRepository, RealGitRepository};
35use is_executable::IsExecutable;
36use rope::Rope;
37use serde::{Deserialize, Serialize};
38use smol::io::AsyncWriteExt;
39use std::{
40 io::{self, Write},
41 path::{Component, Path, PathBuf},
42 pin::Pin,
43 sync::Arc,
44 time::{Duration, SystemTime, UNIX_EPOCH},
45};
46use tempfile::TempDir;
47use text::LineEnding;
48
49#[cfg(any(test, feature = "test-support"))]
50mod fake_git_repo;
51#[cfg(any(test, feature = "test-support"))]
52use collections::{BTreeMap, btree_map};
53#[cfg(any(test, feature = "test-support"))]
54use fake_git_repo::FakeGitRepositoryState;
55#[cfg(any(test, feature = "test-support"))]
56use git::{
57 repository::{RepoPath, repo_path},
58 status::{FileStatus, StatusCode, TrackedStatus, UnmergedStatus},
59};
60
61#[cfg(any(test, feature = "test-support"))]
62use smol::io::AsyncReadExt;
63#[cfg(any(test, feature = "test-support"))]
64use std::ffi::OsStr;
65
66#[cfg(any(test, feature = "test-support"))]
67pub use fake_git_repo::{LOAD_HEAD_TEXT_TASK, LOAD_INDEX_TEXT_TASK};
68
69pub type AsyncReadBox = Pin<Box<dyn AsyncRead + Send>>;
70
71pub trait Watcher: Send + Sync {
72 fn add(&self, path: &Path) -> Result<()>;
73 fn remove(&self, path: &Path) -> Result<()>;
74}
75
76#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
77pub enum PathEventKind {
78 Removed,
79 Created,
80 Changed,
81}
82
83#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
84pub struct PathEvent {
85 pub path: PathBuf,
86 pub kind: Option<PathEventKind>,
87}
88
89impl From<PathEvent> for PathBuf {
90 fn from(event: PathEvent) -> Self {
91 event.path
92 }
93}
94
95#[async_trait::async_trait]
96pub trait Fs: Send + Sync {
97 async fn create_dir(&self, path: &Path) -> Result<()>;
98 async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()>;
99 async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()>;
100 async fn create_file_with(
101 &self,
102 path: &Path,
103 content: Pin<&mut (dyn AsyncRead + Send)>,
104 ) -> Result<()>;
105 async fn extract_tar_file(
106 &self,
107 path: &Path,
108 content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
109 ) -> Result<()>;
110 async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()>;
111 async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()>;
112 async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()>;
113 async fn trash_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
114 self.remove_dir(path, options).await
115 }
116 async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;
117 async fn trash_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
118 self.remove_file(path, options).await
119 }
120 async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>>;
121 async fn open_read(&self, path: &Path) -> Result<AsyncReadBox>;
122 async fn load(&self, path: &Path) -> Result<String> {
123 Ok(String::from_utf8(self.load_bytes(path).await?)?)
124 }
125 async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>>;
126 async fn atomic_write(&self, path: PathBuf, text: String) -> Result<()>;
127 async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()>;
128 async fn write(&self, path: &Path, content: &[u8]) -> Result<()>;
129 async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
130 async fn is_file(&self, path: &Path) -> bool;
131 async fn is_dir(&self, path: &Path) -> bool;
132 async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;
133 async fn read_link(&self, path: &Path) -> Result<PathBuf>;
134 async fn read_dir(
135 &self,
136 path: &Path,
137 ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>>;
138
139 async fn watch(
140 &self,
141 path: &Path,
142 latency: Duration,
143 ) -> (
144 Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
145 Arc<dyn Watcher>,
146 );
147
148 fn open_repo(
149 &self,
150 abs_dot_git: &Path,
151 system_git_binary_path: Option<&Path>,
152 ) -> Option<Arc<dyn GitRepository>>;
153 async fn git_init(&self, abs_work_directory: &Path, fallback_branch_name: String)
154 -> Result<()>;
155 async fn git_clone(&self, repo_url: &str, abs_work_directory: &Path) -> Result<()>;
156 fn is_fake(&self) -> bool;
157 async fn is_case_sensitive(&self) -> Result<bool>;
158 fn subscribe_to_jobs(&self) -> JobEventReceiver;
159
160 #[cfg(any(test, feature = "test-support"))]
161 fn as_fake(&self) -> Arc<FakeFs> {
162 panic!("called as_fake on a real fs");
163 }
164}
165
166struct GlobalFs(Arc<dyn Fs>);
167
168impl Global for GlobalFs {}
169
170impl dyn Fs {
171 /// Returns the global [`Fs`].
172 pub fn global(cx: &App) -> Arc<Self> {
173 GlobalFs::global(cx).0.clone()
174 }
175
176 /// Sets the global [`Fs`].
177 pub fn set_global(fs: Arc<Self>, cx: &mut App) {
178 cx.set_global(GlobalFs(fs));
179 }
180}
181
182#[derive(Copy, Clone, Default)]
183pub struct CreateOptions {
184 pub overwrite: bool,
185 pub ignore_if_exists: bool,
186}
187
188#[derive(Copy, Clone, Default)]
189pub struct CopyOptions {
190 pub overwrite: bool,
191 pub ignore_if_exists: bool,
192}
193
194#[derive(Copy, Clone, Default)]
195pub struct RenameOptions {
196 pub overwrite: bool,
197 pub ignore_if_exists: bool,
198 /// Whether to create parent directories if they do not exist.
199 pub create_parents: bool,
200}
201
202#[derive(Copy, Clone, Default)]
203pub struct RemoveOptions {
204 pub recursive: bool,
205 pub ignore_if_not_exists: bool,
206}
207
208#[derive(Copy, Clone, Debug)]
209pub struct Metadata {
210 pub inode: u64,
211 pub mtime: MTime,
212 pub is_symlink: bool,
213 pub is_dir: bool,
214 pub len: u64,
215 pub is_fifo: bool,
216 pub is_executable: bool,
217}
218
219/// Filesystem modification time. The purpose of this newtype is to discourage use of operations
220/// that do not make sense for mtimes. In particular, it is not always valid to compare mtimes using
221/// `<` or `>`, as there are many things that can cause the mtime of a file to be earlier than it
222/// was. See ["mtime comparison considered harmful" - apenwarr](https://apenwarr.ca/log/20181113).
223///
224/// Do not derive Ord, PartialOrd, or arithmetic operation traits.
225#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
226#[serde(transparent)]
227pub struct MTime(SystemTime);
228
229pub type JobId = usize;
230
231#[derive(Clone, Debug)]
232pub struct JobInfo {
233 pub start: Instant,
234 pub message: SharedString,
235 pub id: JobId,
236}
237
238#[derive(Debug, Clone)]
239pub enum JobEvent {
240 Started { info: JobInfo },
241 Completed { id: JobId },
242}
243
244pub type JobEventSender = futures::channel::mpsc::UnboundedSender<JobEvent>;
245pub type JobEventReceiver = futures::channel::mpsc::UnboundedReceiver<JobEvent>;
246
247struct JobTracker {
248 id: JobId,
249 subscribers: Arc<Mutex<Vec<JobEventSender>>>,
250}
251
252impl JobTracker {
253 fn new(info: JobInfo, subscribers: Arc<Mutex<Vec<JobEventSender>>>) -> Self {
254 let id = info.id;
255 {
256 let mut subs = subscribers.lock();
257 subs.retain(|sender| {
258 sender
259 .unbounded_send(JobEvent::Started { info: info.clone() })
260 .is_ok()
261 });
262 }
263 Self { id, subscribers }
264 }
265}
266
267impl Drop for JobTracker {
268 fn drop(&mut self) {
269 let mut subs = self.subscribers.lock();
270 subs.retain(|sender| {
271 sender
272 .unbounded_send(JobEvent::Completed { id: self.id })
273 .is_ok()
274 });
275 }
276}
277
278impl MTime {
279 /// Conversion intended for persistence and testing.
280 pub fn from_seconds_and_nanos(secs: u64, nanos: u32) -> Self {
281 MTime(UNIX_EPOCH + Duration::new(secs, nanos))
282 }
283
284 /// Conversion intended for persistence.
285 pub fn to_seconds_and_nanos_for_persistence(self) -> Option<(u64, u32)> {
286 self.0
287 .duration_since(UNIX_EPOCH)
288 .ok()
289 .map(|duration| (duration.as_secs(), duration.subsec_nanos()))
290 }
291
292 /// Returns the value wrapped by this `MTime`, for presentation to the user. The name including
293 /// "_for_user" is to discourage misuse - this method should not be used when making decisions
294 /// about file dirtiness.
295 pub fn timestamp_for_user(self) -> SystemTime {
296 self.0
297 }
298
299 /// Temporary method to split out the behavior changes from introduction of this newtype.
300 pub fn bad_is_greater_than(self, other: MTime) -> bool {
301 self.0 > other.0
302 }
303}
304
305impl From<proto::Timestamp> for MTime {
306 fn from(timestamp: proto::Timestamp) -> Self {
307 MTime(timestamp.into())
308 }
309}
310
311impl From<MTime> for proto::Timestamp {
312 fn from(mtime: MTime) -> Self {
313 mtime.0.into()
314 }
315}
316
317pub struct RealFs {
318 bundled_git_binary_path: Option<PathBuf>,
319 executor: BackgroundExecutor,
320 next_job_id: Arc<AtomicUsize>,
321 job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,
322}
323
324pub trait FileHandle: Send + Sync + std::fmt::Debug {
325 fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf>;
326}
327
328impl FileHandle for std::fs::File {
329 #[cfg(target_os = "macos")]
330 fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
331 use std::{
332 ffi::{CStr, OsStr},
333 os::unix::ffi::OsStrExt,
334 };
335
336 let fd = self.as_fd();
337 let mut path_buf = MaybeUninit::<[u8; libc::PATH_MAX as usize]>::uninit();
338
339 let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETPATH, path_buf.as_mut_ptr()) };
340 if result == -1 {
341 anyhow::bail!("fcntl returned -1".to_string());
342 }
343
344 // SAFETY: `fcntl` will initialize the path buffer.
345 let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr().cast()) };
346 let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
347 Ok(path)
348 }
349
350 #[cfg(target_os = "linux")]
351 fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
352 let fd = self.as_fd();
353 let fd_path = format!("/proc/self/fd/{}", fd.as_raw_fd());
354 let new_path = std::fs::read_link(fd_path)?;
355 if new_path
356 .file_name()
357 .is_some_and(|f| f.to_string_lossy().ends_with(" (deleted)"))
358 {
359 anyhow::bail!("file was deleted")
360 };
361
362 Ok(new_path)
363 }
364
365 #[cfg(target_os = "freebsd")]
366 fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
367 use std::{
368 ffi::{CStr, OsStr},
369 os::unix::ffi::OsStrExt,
370 };
371
372 let fd = self.as_fd();
373 let mut kif = MaybeUninit::<libc::kinfo_file>::uninit();
374 kif.kf_structsize = libc::KINFO_FILE_SIZE;
375
376 let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_KINFO, kif.as_mut_ptr()) };
377 if result == -1 {
378 anyhow::bail!("fcntl returned -1".to_string());
379 }
380
381 // SAFETY: `fcntl` will initialize the kif.
382 let c_str = unsafe { CStr::from_ptr(kif.assume_init().kf_path.as_ptr()) };
383 let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
384 Ok(path)
385 }
386
387 #[cfg(target_os = "windows")]
388 fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
389 use std::ffi::OsString;
390 use std::os::windows::ffi::OsStringExt;
391 use std::os::windows::io::AsRawHandle;
392
393 use windows::Win32::Foundation::HANDLE;
394 use windows::Win32::Storage::FileSystem::{
395 FILE_NAME_NORMALIZED, GetFinalPathNameByHandleW,
396 };
397
398 let handle = HANDLE(self.as_raw_handle() as _);
399
400 // Query required buffer size (in wide chars)
401 let required_len =
402 unsafe { GetFinalPathNameByHandleW(handle, &mut [], FILE_NAME_NORMALIZED) };
403 if required_len == 0 {
404 anyhow::bail!("GetFinalPathNameByHandleW returned 0 length");
405 }
406
407 // Allocate buffer and retrieve the path
408 let mut buf: Vec<u16> = vec![0u16; required_len as usize + 1];
409 let written = unsafe { GetFinalPathNameByHandleW(handle, &mut buf, FILE_NAME_NORMALIZED) };
410 if written == 0 {
411 anyhow::bail!("GetFinalPathNameByHandleW failed to write path");
412 }
413
414 let os_str: OsString = OsString::from_wide(&buf[..written as usize]);
415 Ok(PathBuf::from(os_str))
416 }
417}
418
419pub struct RealWatcher {}
420
421impl RealFs {
422 pub fn new(git_binary_path: Option<PathBuf>, executor: BackgroundExecutor) -> Self {
423 Self {
424 bundled_git_binary_path: git_binary_path,
425 executor,
426 next_job_id: Arc::new(AtomicUsize::new(0)),
427 job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
428 }
429 }
430
431 #[cfg(target_os = "windows")]
432 fn canonicalize(path: &Path) -> Result<PathBuf> {
433 let mut strip_prefix = None;
434
435 let mut new_path = PathBuf::new();
436 for component in path.components() {
437 match component {
438 std::path::Component::Prefix(_) => {
439 let component = component.as_os_str();
440 let canonicalized = if component
441 .to_str()
442 .map(|e| e.ends_with("\\"))
443 .unwrap_or(false)
444 {
445 std::fs::canonicalize(component)
446 } else {
447 let mut component = component.to_os_string();
448 component.push("\\");
449 std::fs::canonicalize(component)
450 }?;
451
452 let mut strip = PathBuf::new();
453 for component in canonicalized.components() {
454 match component {
455 Component::Prefix(prefix_component) => {
456 match prefix_component.kind() {
457 std::path::Prefix::Verbatim(os_str) => {
458 strip.push(os_str);
459 }
460 std::path::Prefix::VerbatimUNC(host, share) => {
461 strip.push("\\\\");
462 strip.push(host);
463 strip.push(share);
464 }
465 std::path::Prefix::VerbatimDisk(disk) => {
466 strip.push(format!("{}:", disk as char));
467 }
468 _ => strip.push(component),
469 };
470 }
471 _ => strip.push(component),
472 }
473 }
474 strip_prefix = Some(strip);
475 new_path.push(component);
476 }
477 std::path::Component::RootDir => {
478 new_path.push(component);
479 }
480 std::path::Component::CurDir => {
481 if strip_prefix.is_none() {
482 // unrooted path
483 new_path.push(component);
484 }
485 }
486 std::path::Component::ParentDir => {
487 if strip_prefix.is_some() {
488 // rooted path
489 new_path.pop();
490 } else {
491 new_path.push(component);
492 }
493 }
494 std::path::Component::Normal(_) => {
495 if let Ok(link) = std::fs::read_link(new_path.join(component)) {
496 let link = match &strip_prefix {
497 Some(e) => link.strip_prefix(e).unwrap_or(&link),
498 None => &link,
499 };
500 new_path.extend(link);
501 } else {
502 new_path.push(component);
503 }
504 }
505 }
506 }
507
508 Ok(new_path)
509 }
510}
511
512#[async_trait::async_trait]
513impl Fs for RealFs {
514 async fn create_dir(&self, path: &Path) -> Result<()> {
515 Ok(smol::fs::create_dir_all(path).await?)
516 }
517
518 async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()> {
519 #[cfg(unix)]
520 smol::fs::unix::symlink(target, path).await?;
521
522 #[cfg(windows)]
523 if smol::fs::metadata(&target).await?.is_dir() {
524 let status = new_smol_command("cmd")
525 .args(["/C", "mklink", "/J"])
526 .args([path, target.as_path()])
527 .status()
528 .await?;
529
530 if !status.success() {
531 return Err(anyhow::anyhow!(
532 "Failed to create junction from {:?} to {:?}",
533 path,
534 target
535 ));
536 }
537 } else {
538 smol::fs::windows::symlink_file(target, path).await?
539 }
540
541 Ok(())
542 }
543
544 async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
545 let mut open_options = smol::fs::OpenOptions::new();
546 open_options.write(true).create(true);
547 if options.overwrite {
548 open_options.truncate(true);
549 } else if !options.ignore_if_exists {
550 open_options.create_new(true);
551 }
552 open_options.open(path).await?;
553 Ok(())
554 }
555
556 async fn create_file_with(
557 &self,
558 path: &Path,
559 content: Pin<&mut (dyn AsyncRead + Send)>,
560 ) -> Result<()> {
561 let mut file = smol::fs::File::create(&path).await?;
562 futures::io::copy(content, &mut file).await?;
563 Ok(())
564 }
565
566 async fn extract_tar_file(
567 &self,
568 path: &Path,
569 content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
570 ) -> Result<()> {
571 content.unpack(path).await?;
572 Ok(())
573 }
574
575 async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
576 if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
577 if options.ignore_if_exists {
578 return Ok(());
579 } else {
580 anyhow::bail!("{target:?} already exists");
581 }
582 }
583
584 smol::fs::copy(source, target).await?;
585 Ok(())
586 }
587
588 async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()> {
589 if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
590 if options.ignore_if_exists {
591 return Ok(());
592 } else {
593 anyhow::bail!("{target:?} already exists");
594 }
595 }
596
597 if options.create_parents {
598 if let Some(parent) = target.parent() {
599 self.create_dir(parent).await?;
600 }
601 }
602
603 smol::fs::rename(source, target).await?;
604 Ok(())
605 }
606
607 async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
608 let result = if options.recursive {
609 smol::fs::remove_dir_all(path).await
610 } else {
611 smol::fs::remove_dir(path).await
612 };
613 match result {
614 Ok(()) => Ok(()),
615 Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
616 Ok(())
617 }
618 Err(err) => Err(err)?,
619 }
620 }
621
622 async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
623 #[cfg(windows)]
624 if let Ok(Some(metadata)) = self.metadata(path).await
625 && metadata.is_symlink
626 && metadata.is_dir
627 {
628 self.remove_dir(
629 path,
630 RemoveOptions {
631 recursive: false,
632 ignore_if_not_exists: true,
633 },
634 )
635 .await?;
636 return Ok(());
637 }
638
639 match smol::fs::remove_file(path).await {
640 Ok(()) => Ok(()),
641 Err(err) if err.kind() == io::ErrorKind::NotFound && options.ignore_if_not_exists => {
642 Ok(())
643 }
644 Err(err) => Err(err)?,
645 }
646 }
647
648 #[cfg(target_os = "macos")]
649 async fn trash_file(&self, path: &Path, _options: RemoveOptions) -> Result<()> {
650 use cocoa::{
651 base::{id, nil},
652 foundation::{NSAutoreleasePool, NSString},
653 };
654 use objc::{class, msg_send, sel, sel_impl};
655
656 unsafe {
657 /// Allow NSString::alloc use here because it sets autorelease
658 #[allow(clippy::disallowed_methods)]
659 unsafe fn ns_string(string: &str) -> id {
660 unsafe { NSString::alloc(nil).init_str(string).autorelease() }
661 }
662
663 let url: id = msg_send![class!(NSURL), fileURLWithPath: ns_string(path.to_string_lossy().as_ref())];
664 let array: id = msg_send![class!(NSArray), arrayWithObject: url];
665 let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
666
667 let _: id = msg_send![workspace, recycleURLs: array completionHandler: nil];
668 }
669 Ok(())
670 }
671
672 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
673 async fn trash_file(&self, path: &Path, _options: RemoveOptions) -> Result<()> {
674 if let Ok(Some(metadata)) = self.metadata(path).await
675 && metadata.is_symlink
676 {
677 // TODO: trash_file does not support trashing symlinks yet - https://github.com/bilelmoussaoui/ashpd/issues/255
678 return self.remove_file(path, RemoveOptions::default()).await;
679 }
680 let file = smol::fs::File::open(path).await?;
681 match trash::trash_file(&file.as_fd()).await {
682 Ok(_) => Ok(()),
683 Err(err) => {
684 log::error!("Failed to trash file: {}", err);
685 // Trashing files can fail if you don't have a trashing dbus service configured.
686 // In that case, delete the file directly instead.
687 return self.remove_file(path, RemoveOptions::default()).await;
688 }
689 }
690 }
691
692 #[cfg(target_os = "windows")]
693 async fn trash_file(&self, path: &Path, _options: RemoveOptions) -> Result<()> {
694 use util::paths::SanitizedPath;
695 use windows::{
696 Storage::{StorageDeleteOption, StorageFile},
697 core::HSTRING,
698 };
699 // todo(windows)
700 // When new version of `windows-rs` release, make this operation `async`
701 let path = path.canonicalize()?;
702 let path = SanitizedPath::new(&path);
703 let path_string = path.to_string();
704 let file = StorageFile::GetFileFromPathAsync(&HSTRING::from(path_string))?.get()?;
705 file.DeleteAsync(StorageDeleteOption::Default)?.get()?;
706 Ok(())
707 }
708
709 #[cfg(target_os = "macos")]
710 async fn trash_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
711 self.trash_file(path, options).await
712 }
713
714 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
715 async fn trash_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
716 self.trash_file(path, options).await
717 }
718
719 #[cfg(target_os = "windows")]
720 async fn trash_dir(&self, path: &Path, _options: RemoveOptions) -> Result<()> {
721 use util::paths::SanitizedPath;
722 use windows::{
723 Storage::{StorageDeleteOption, StorageFolder},
724 core::HSTRING,
725 };
726
727 // todo(windows)
728 // When new version of `windows-rs` release, make this operation `async`
729 let path = path.canonicalize()?;
730 let path = SanitizedPath::new(&path);
731 let path_string = path.to_string();
732 let folder = StorageFolder::GetFolderFromPathAsync(&HSTRING::from(path_string))?.get()?;
733 folder.DeleteAsync(StorageDeleteOption::Default)?.get()?;
734 Ok(())
735 }
736
737 async fn open_read(&self, path: &Path) -> Result<AsyncReadBox> {
738 Ok(Box::pin(smol::fs::File::open(path).await?))
739 }
740
741 async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>> {
742 let mut options = std::fs::OpenOptions::new();
743 options.read(true);
744 #[cfg(windows)]
745 {
746 use std::os::windows::fs::OpenOptionsExt;
747 options.custom_flags(windows::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS.0);
748 }
749 Ok(Arc::new(options.open(path)?))
750 }
751
752 async fn load(&self, path: &Path) -> Result<String> {
753 let path = path.to_path_buf();
754 self.executor
755 .spawn(async move { Ok(std::fs::read_to_string(path)?) })
756 .await
757 }
758
759 async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>> {
760 let path = path.to_path_buf();
761 let bytes = self
762 .executor
763 .spawn(async move { std::fs::read(path) })
764 .await?;
765 Ok(bytes)
766 }
767
768 #[cfg(not(target_os = "windows"))]
769 async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
770 smol::unblock(move || {
771 // Use the directory of the destination as temp dir to avoid
772 // invalid cross-device link error, and XDG_CACHE_DIR for fallback.
773 // See https://github.com/zed-industries/zed/pull/8437 for more details.
774 let mut tmp_file =
775 tempfile::NamedTempFile::new_in(path.parent().unwrap_or(paths::temp_dir()))?;
776 tmp_file.write_all(data.as_bytes())?;
777 tmp_file.persist(path)?;
778 anyhow::Ok(())
779 })
780 .await?;
781
782 Ok(())
783 }
784
785 #[cfg(target_os = "windows")]
786 async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
787 smol::unblock(move || {
788 // If temp dir is set to a different drive than the destination,
789 // we receive error:
790 //
791 // failed to persist temporary file:
792 // The system cannot move the file to a different disk drive. (os error 17)
793 //
794 // This is because `ReplaceFileW` does not support cross volume moves.
795 // See the remark section: "The backup file, replaced file, and replacement file must all reside on the same volume."
796 // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-replacefilew#remarks
797 //
798 // So we use the directory of the destination as a temp dir to avoid it.
799 // https://github.com/zed-industries/zed/issues/16571
800 let temp_dir = TempDir::new_in(path.parent().unwrap_or(paths::temp_dir()))?;
801 let temp_file = {
802 let temp_file_path = temp_dir.path().join("temp_file");
803 let mut file = std::fs::File::create_new(&temp_file_path)?;
804 file.write_all(data.as_bytes())?;
805 temp_file_path
806 };
807 atomic_replace(path.as_path(), temp_file.as_path())?;
808 anyhow::Ok(())
809 })
810 .await?;
811 Ok(())
812 }
813
814 async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
815 let buffer_size = text.summary().len.min(10 * 1024);
816 if let Some(path) = path.parent() {
817 self.create_dir(path).await?;
818 }
819 let file = smol::fs::File::create(path).await?;
820 let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
821 for chunk in text::chunks_with_line_ending(text, line_ending) {
822 writer.write_all(chunk.as_bytes()).await?;
823 }
824 writer.flush().await?;
825 Ok(())
826 }
827
828 async fn write(&self, path: &Path, content: &[u8]) -> Result<()> {
829 if let Some(path) = path.parent() {
830 self.create_dir(path).await?;
831 }
832 let path = path.to_owned();
833 let contents = content.to_owned();
834 self.executor
835 .spawn(async move {
836 std::fs::write(path, contents)?;
837 Ok(())
838 })
839 .await
840 }
841
842 async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
843 let path = path.to_owned();
844 self.executor
845 .spawn(async move {
846 #[cfg(target_os = "windows")]
847 let result = Self::canonicalize(&path);
848
849 #[cfg(not(target_os = "windows"))]
850 let result = std::fs::canonicalize(&path);
851
852 result.with_context(|| format!("canonicalizing {path:?}"))
853 })
854 .await
855 }
856
857 async fn is_file(&self, path: &Path) -> bool {
858 let path = path.to_owned();
859 self.executor
860 .spawn(async move { std::fs::metadata(path).is_ok_and(|metadata| metadata.is_file()) })
861 .await
862 }
863
864 async fn is_dir(&self, path: &Path) -> bool {
865 let path = path.to_owned();
866 self.executor
867 .spawn(async move { std::fs::metadata(path).is_ok_and(|metadata| metadata.is_dir()) })
868 .await
869 }
870
871 async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
872 let path_buf = path.to_owned();
873 let symlink_metadata = match self
874 .executor
875 .spawn(async move { std::fs::symlink_metadata(&path_buf) })
876 .await
877 {
878 Ok(metadata) => metadata,
879 Err(err) => {
880 return match err.kind() {
881 io::ErrorKind::NotFound | io::ErrorKind::NotADirectory => Ok(None),
882 _ => Err(anyhow::Error::new(err)),
883 };
884 }
885 };
886
887 let is_symlink = symlink_metadata.file_type().is_symlink();
888 let metadata = if is_symlink {
889 let path_buf = path.to_path_buf();
890 let path_exists = self
891 .executor
892 .spawn(async move {
893 path_buf
894 .try_exists()
895 .with_context(|| format!("checking existence for path {path_buf:?}"))
896 })
897 .await?;
898 if path_exists {
899 let path_buf = path.to_path_buf();
900 self.executor
901 .spawn(async move { std::fs::metadata(path_buf) })
902 .await
903 .with_context(|| "accessing symlink for path {path}")?
904 } else {
905 symlink_metadata
906 }
907 } else {
908 symlink_metadata
909 };
910
911 #[cfg(unix)]
912 let inode = metadata.ino();
913
914 #[cfg(windows)]
915 let inode = file_id(path).await?;
916
917 #[cfg(windows)]
918 let is_fifo = false;
919
920 #[cfg(unix)]
921 let is_fifo = metadata.file_type().is_fifo();
922
923 let path_buf = path.to_path_buf();
924 let is_executable = self
925 .executor
926 .spawn(async move { path_buf.is_executable() })
927 .await;
928
929 Ok(Some(Metadata {
930 inode,
931 mtime: MTime(metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH)),
932 len: metadata.len(),
933 is_symlink,
934 is_dir: metadata.file_type().is_dir(),
935 is_fifo,
936 is_executable,
937 }))
938 }
939
940 async fn read_link(&self, path: &Path) -> Result<PathBuf> {
941 let path = path.to_owned();
942 let path = self
943 .executor
944 .spawn(async move { std::fs::read_link(&path) })
945 .await?;
946 Ok(path)
947 }
948
949 async fn read_dir(
950 &self,
951 path: &Path,
952 ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
953 let path = path.to_owned();
954 let result = iter(
955 self.executor
956 .spawn(async move { std::fs::read_dir(path) })
957 .await?,
958 )
959 .map(|entry| match entry {
960 Ok(entry) => Ok(entry.path()),
961 Err(error) => Err(anyhow!("failed to read dir entry {error:?}")),
962 });
963 Ok(Box::pin(result))
964 }
965
966 #[cfg(target_os = "macos")]
967 async fn watch(
968 &self,
969 path: &Path,
970 latency: Duration,
971 ) -> (
972 Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
973 Arc<dyn Watcher>,
974 ) {
975 use fsevent::StreamFlags;
976
977 let (events_tx, events_rx) = smol::channel::unbounded();
978 let handles = Arc::new(parking_lot::Mutex::new(collections::BTreeMap::default()));
979 let watcher = Arc::new(mac_watcher::MacWatcher::new(
980 events_tx,
981 Arc::downgrade(&handles),
982 latency,
983 ));
984 watcher.add(path).expect("handles can't be dropped");
985
986 (
987 Box::pin(
988 events_rx
989 .map(|events| {
990 events
991 .into_iter()
992 .map(|event| {
993 log::trace!("fs path event: {event:?}");
994 let kind = if event.flags.contains(StreamFlags::ITEM_REMOVED) {
995 Some(PathEventKind::Removed)
996 } else if event.flags.contains(StreamFlags::ITEM_CREATED) {
997 Some(PathEventKind::Created)
998 } else if event.flags.contains(StreamFlags::ITEM_MODIFIED)
999 | event.flags.contains(StreamFlags::ITEM_RENAMED)
1000 {
1001 Some(PathEventKind::Changed)
1002 } else {
1003 None
1004 };
1005 PathEvent {
1006 path: event.path,
1007 kind,
1008 }
1009 })
1010 .collect()
1011 })
1012 .chain(futures::stream::once(async move {
1013 drop(handles);
1014 vec![]
1015 })),
1016 ),
1017 watcher,
1018 )
1019 }
1020
1021 #[cfg(not(target_os = "macos"))]
1022 async fn watch(
1023 &self,
1024 path: &Path,
1025 latency: Duration,
1026 ) -> (
1027 Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
1028 Arc<dyn Watcher>,
1029 ) {
1030 use util::{ResultExt as _, paths::SanitizedPath};
1031
1032 let (tx, rx) = smol::channel::unbounded();
1033 let pending_paths: Arc<Mutex<Vec<PathEvent>>> = Default::default();
1034 let watcher = Arc::new(fs_watcher::FsWatcher::new(tx, pending_paths.clone()));
1035
1036 // If the path doesn't exist yet (e.g. settings.json), watch the parent dir to learn when it's created.
1037 if let Err(e) = watcher.add(path)
1038 && let Some(parent) = path.parent()
1039 && let Err(parent_e) = watcher.add(parent)
1040 {
1041 log::warn!(
1042 "Failed to watch {} and its parent directory {}:\n{e}\n{parent_e}",
1043 path.display(),
1044 parent.display()
1045 );
1046 }
1047
1048 // Check if path is a symlink and follow the target parent
1049 if let Some(mut target) = self.read_link(path).await.ok() {
1050 log::trace!("watch symlink {path:?} -> {target:?}");
1051 // Check if symlink target is relative path, if so make it absolute
1052 if target.is_relative()
1053 && let Some(parent) = path.parent()
1054 {
1055 target = parent.join(target);
1056 if let Ok(canonical) = self.canonicalize(&target).await {
1057 target = SanitizedPath::new(&canonical).as_path().to_path_buf();
1058 }
1059 }
1060 watcher.add(&target).ok();
1061 if let Some(parent) = target.parent() {
1062 watcher.add(parent).log_err();
1063 }
1064 }
1065
1066 (
1067 Box::pin(rx.filter_map({
1068 let watcher = watcher.clone();
1069 move |_| {
1070 let _ = watcher.clone();
1071 let pending_paths = pending_paths.clone();
1072 async move {
1073 smol::Timer::after(latency).await;
1074 let paths = std::mem::take(&mut *pending_paths.lock());
1075 (!paths.is_empty()).then_some(paths)
1076 }
1077 }
1078 })),
1079 watcher,
1080 )
1081 }
1082
1083 fn open_repo(
1084 &self,
1085 dotgit_path: &Path,
1086 system_git_binary_path: Option<&Path>,
1087 ) -> Option<Arc<dyn GitRepository>> {
1088 Some(Arc::new(RealGitRepository::new(
1089 dotgit_path,
1090 self.bundled_git_binary_path.clone(),
1091 system_git_binary_path.map(|path| path.to_path_buf()),
1092 self.executor.clone(),
1093 )?))
1094 }
1095
1096 async fn git_init(
1097 &self,
1098 abs_work_directory_path: &Path,
1099 fallback_branch_name: String,
1100 ) -> Result<()> {
1101 let config = new_smol_command("git")
1102 .current_dir(abs_work_directory_path)
1103 .args(&["config", "--global", "--get", "init.defaultBranch"])
1104 .output()
1105 .await?;
1106
1107 let branch_name;
1108
1109 if config.status.success() && !config.stdout.is_empty() {
1110 branch_name = String::from_utf8_lossy(&config.stdout);
1111 } else {
1112 branch_name = Cow::Borrowed(fallback_branch_name.as_str());
1113 }
1114
1115 new_smol_command("git")
1116 .current_dir(abs_work_directory_path)
1117 .args(&["init", "-b"])
1118 .arg(branch_name.trim())
1119 .output()
1120 .await?;
1121
1122 Ok(())
1123 }
1124
1125 async fn git_clone(&self, repo_url: &str, abs_work_directory: &Path) -> Result<()> {
1126 let job_id = self.next_job_id.fetch_add(1, Ordering::SeqCst);
1127 let job_info = JobInfo {
1128 id: job_id,
1129 start: Instant::now(),
1130 message: SharedString::from(format!("Cloning {}", repo_url)),
1131 };
1132
1133 let _job_tracker = JobTracker::new(job_info, self.job_event_subscribers.clone());
1134
1135 let output = new_smol_command("git")
1136 .current_dir(abs_work_directory)
1137 .args(&["clone", repo_url])
1138 .output()
1139 .await?;
1140
1141 if !output.status.success() {
1142 anyhow::bail!(
1143 "git clone failed: {}",
1144 String::from_utf8_lossy(&output.stderr)
1145 );
1146 }
1147
1148 Ok(())
1149 }
1150
1151 fn is_fake(&self) -> bool {
1152 false
1153 }
1154
1155 fn subscribe_to_jobs(&self) -> JobEventReceiver {
1156 let (sender, receiver) = futures::channel::mpsc::unbounded();
1157 self.job_event_subscribers.lock().push(sender);
1158 receiver
1159 }
1160
1161 /// Checks whether the file system is case sensitive by attempting to create two files
1162 /// that have the same name except for the casing.
1163 ///
1164 /// It creates both files in a temporary directory it removes at the end.
1165 async fn is_case_sensitive(&self) -> Result<bool> {
1166 let temp_dir = TempDir::new()?;
1167 let test_file_1 = temp_dir.path().join("case_sensitivity_test.tmp");
1168 let test_file_2 = temp_dir.path().join("CASE_SENSITIVITY_TEST.TMP");
1169
1170 let create_opts = CreateOptions {
1171 overwrite: false,
1172 ignore_if_exists: false,
1173 };
1174
1175 // Create file1
1176 self.create_file(&test_file_1, create_opts).await?;
1177
1178 // Now check whether it's possible to create file2
1179 let case_sensitive = match self.create_file(&test_file_2, create_opts).await {
1180 Ok(_) => Ok(true),
1181 Err(e) => {
1182 if let Some(io_error) = e.downcast_ref::<io::Error>() {
1183 if io_error.kind() == io::ErrorKind::AlreadyExists {
1184 Ok(false)
1185 } else {
1186 Err(e)
1187 }
1188 } else {
1189 Err(e)
1190 }
1191 }
1192 };
1193
1194 temp_dir.close()?;
1195 case_sensitive
1196 }
1197}
1198
1199#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
1200impl Watcher for RealWatcher {
1201 fn add(&self, _: &Path) -> Result<()> {
1202 Ok(())
1203 }
1204
1205 fn remove(&self, _: &Path) -> Result<()> {
1206 Ok(())
1207 }
1208}
1209
1210#[cfg(any(test, feature = "test-support"))]
1211pub struct FakeFs {
1212 this: std::sync::Weak<Self>,
1213 // Use an unfair lock to ensure tests are deterministic.
1214 state: Arc<Mutex<FakeFsState>>,
1215 executor: gpui::BackgroundExecutor,
1216}
1217
1218#[cfg(any(test, feature = "test-support"))]
1219struct FakeFsState {
1220 root: FakeFsEntry,
1221 next_inode: u64,
1222 next_mtime: SystemTime,
1223 git_event_tx: smol::channel::Sender<PathBuf>,
1224 event_txs: Vec<(PathBuf, smol::channel::Sender<Vec<PathEvent>>)>,
1225 events_paused: bool,
1226 buffered_events: Vec<PathEvent>,
1227 metadata_call_count: usize,
1228 read_dir_call_count: usize,
1229 path_write_counts: std::collections::HashMap<PathBuf, usize>,
1230 moves: std::collections::HashMap<u64, PathBuf>,
1231 job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,
1232}
1233
1234#[cfg(any(test, feature = "test-support"))]
1235#[derive(Clone, Debug)]
1236enum FakeFsEntry {
1237 File {
1238 inode: u64,
1239 mtime: MTime,
1240 len: u64,
1241 content: Vec<u8>,
1242 // The path to the repository state directory, if this is a gitfile.
1243 git_dir_path: Option<PathBuf>,
1244 },
1245 Dir {
1246 inode: u64,
1247 mtime: MTime,
1248 len: u64,
1249 entries: BTreeMap<String, FakeFsEntry>,
1250 git_repo_state: Option<Arc<Mutex<FakeGitRepositoryState>>>,
1251 },
1252 Symlink {
1253 target: PathBuf,
1254 },
1255}
1256
1257#[cfg(any(test, feature = "test-support"))]
1258impl PartialEq for FakeFsEntry {
1259 fn eq(&self, other: &Self) -> bool {
1260 match (self, other) {
1261 (
1262 Self::File {
1263 inode: l_inode,
1264 mtime: l_mtime,
1265 len: l_len,
1266 content: l_content,
1267 git_dir_path: l_git_dir_path,
1268 },
1269 Self::File {
1270 inode: r_inode,
1271 mtime: r_mtime,
1272 len: r_len,
1273 content: r_content,
1274 git_dir_path: r_git_dir_path,
1275 },
1276 ) => {
1277 l_inode == r_inode
1278 && l_mtime == r_mtime
1279 && l_len == r_len
1280 && l_content == r_content
1281 && l_git_dir_path == r_git_dir_path
1282 }
1283 (
1284 Self::Dir {
1285 inode: l_inode,
1286 mtime: l_mtime,
1287 len: l_len,
1288 entries: l_entries,
1289 git_repo_state: l_git_repo_state,
1290 },
1291 Self::Dir {
1292 inode: r_inode,
1293 mtime: r_mtime,
1294 len: r_len,
1295 entries: r_entries,
1296 git_repo_state: r_git_repo_state,
1297 },
1298 ) => {
1299 let same_repo_state = match (l_git_repo_state.as_ref(), r_git_repo_state.as_ref()) {
1300 (Some(l), Some(r)) => Arc::ptr_eq(l, r),
1301 (None, None) => true,
1302 _ => false,
1303 };
1304 l_inode == r_inode
1305 && l_mtime == r_mtime
1306 && l_len == r_len
1307 && l_entries == r_entries
1308 && same_repo_state
1309 }
1310 (Self::Symlink { target: l_target }, Self::Symlink { target: r_target }) => {
1311 l_target == r_target
1312 }
1313 _ => false,
1314 }
1315 }
1316}
1317
1318#[cfg(any(test, feature = "test-support"))]
1319impl FakeFsState {
1320 fn get_and_increment_mtime(&mut self) -> MTime {
1321 let mtime = self.next_mtime;
1322 self.next_mtime += FakeFs::SYSTEMTIME_INTERVAL;
1323 MTime(mtime)
1324 }
1325
1326 fn get_and_increment_inode(&mut self) -> u64 {
1327 let inode = self.next_inode;
1328 self.next_inode += 1;
1329 inode
1330 }
1331
1332 fn canonicalize(&self, target: &Path, follow_symlink: bool) -> Option<PathBuf> {
1333 let mut canonical_path = PathBuf::new();
1334 let mut path = target.to_path_buf();
1335 let mut entry_stack = Vec::new();
1336 'outer: loop {
1337 let mut path_components = path.components().peekable();
1338 let mut prefix = None;
1339 while let Some(component) = path_components.next() {
1340 match component {
1341 Component::Prefix(prefix_component) => prefix = Some(prefix_component),
1342 Component::RootDir => {
1343 entry_stack.clear();
1344 entry_stack.push(&self.root);
1345 canonical_path.clear();
1346 match prefix {
1347 Some(prefix_component) => {
1348 canonical_path = PathBuf::from(prefix_component.as_os_str());
1349 // Prefixes like `C:\\` are represented without their trailing slash, so we have to re-add it.
1350 canonical_path.push(std::path::MAIN_SEPARATOR_STR);
1351 }
1352 None => canonical_path = PathBuf::from(std::path::MAIN_SEPARATOR_STR),
1353 }
1354 }
1355 Component::CurDir => {}
1356 Component::ParentDir => {
1357 entry_stack.pop()?;
1358 canonical_path.pop();
1359 }
1360 Component::Normal(name) => {
1361 let current_entry = *entry_stack.last()?;
1362 if let FakeFsEntry::Dir { entries, .. } = current_entry {
1363 let entry = entries.get(name.to_str().unwrap())?;
1364 if (path_components.peek().is_some() || follow_symlink)
1365 && let FakeFsEntry::Symlink { target, .. } = entry
1366 {
1367 let mut target = target.clone();
1368 target.extend(path_components);
1369 path = target;
1370 continue 'outer;
1371 }
1372 entry_stack.push(entry);
1373 canonical_path = canonical_path.join(name);
1374 } else {
1375 return None;
1376 }
1377 }
1378 }
1379 }
1380 break;
1381 }
1382
1383 if entry_stack.is_empty() {
1384 None
1385 } else {
1386 Some(canonical_path)
1387 }
1388 }
1389
1390 fn try_entry(
1391 &mut self,
1392 target: &Path,
1393 follow_symlink: bool,
1394 ) -> Option<(&mut FakeFsEntry, PathBuf)> {
1395 let canonical_path = self.canonicalize(target, follow_symlink)?;
1396
1397 let mut components = canonical_path
1398 .components()
1399 .skip_while(|component| matches!(component, Component::Prefix(_)));
1400 let Some(Component::RootDir) = components.next() else {
1401 panic!(
1402 "the path {:?} was not canonicalized properly {:?}",
1403 target, canonical_path
1404 )
1405 };
1406
1407 let mut entry = &mut self.root;
1408 for component in components {
1409 match component {
1410 Component::Normal(name) => {
1411 if let FakeFsEntry::Dir { entries, .. } = entry {
1412 entry = entries.get_mut(name.to_str().unwrap())?;
1413 } else {
1414 return None;
1415 }
1416 }
1417 _ => {
1418 panic!(
1419 "the path {:?} was not canonicalized properly {:?}",
1420 target, canonical_path
1421 )
1422 }
1423 }
1424 }
1425
1426 Some((entry, canonical_path))
1427 }
1428
1429 fn entry(&mut self, target: &Path) -> Result<&mut FakeFsEntry> {
1430 Ok(self
1431 .try_entry(target, true)
1432 .ok_or_else(|| {
1433 anyhow!(io::Error::new(
1434 io::ErrorKind::NotFound,
1435 format!("not found: {target:?}")
1436 ))
1437 })?
1438 .0)
1439 }
1440
1441 fn write_path<Fn, T>(&mut self, path: &Path, callback: Fn) -> Result<T>
1442 where
1443 Fn: FnOnce(btree_map::Entry<String, FakeFsEntry>) -> Result<T>,
1444 {
1445 let path = normalize_path(path);
1446 let filename = path.file_name().context("cannot overwrite the root")?;
1447 let parent_path = path.parent().unwrap();
1448
1449 let parent = self.entry(parent_path)?;
1450 let new_entry = parent
1451 .dir_entries(parent_path)?
1452 .entry(filename.to_str().unwrap().into());
1453 callback(new_entry)
1454 }
1455
1456 fn emit_event<I, T>(&mut self, paths: I)
1457 where
1458 I: IntoIterator<Item = (T, Option<PathEventKind>)>,
1459 T: Into<PathBuf>,
1460 {
1461 self.buffered_events
1462 .extend(paths.into_iter().map(|(path, kind)| PathEvent {
1463 path: path.into(),
1464 kind,
1465 }));
1466
1467 if !self.events_paused {
1468 self.flush_events(self.buffered_events.len());
1469 }
1470 }
1471
1472 fn flush_events(&mut self, mut count: usize) {
1473 count = count.min(self.buffered_events.len());
1474 let events = self.buffered_events.drain(0..count).collect::<Vec<_>>();
1475 self.event_txs.retain(|(_, tx)| {
1476 let _ = tx.try_send(events.clone());
1477 !tx.is_closed()
1478 });
1479 }
1480}
1481
1482#[cfg(any(test, feature = "test-support"))]
1483pub static FS_DOT_GIT: std::sync::LazyLock<&'static OsStr> =
1484 std::sync::LazyLock::new(|| OsStr::new(".git"));
1485
1486#[cfg(any(test, feature = "test-support"))]
1487impl FakeFs {
1488 /// We need to use something large enough for Windows and Unix to consider this a new file.
1489 /// https://doc.rust-lang.org/nightly/std/time/struct.SystemTime.html#platform-specific-behavior
1490 const SYSTEMTIME_INTERVAL: Duration = Duration::from_nanos(100);
1491
1492 pub fn new(executor: gpui::BackgroundExecutor) -> Arc<Self> {
1493 let (tx, rx) = smol::channel::bounded::<PathBuf>(10);
1494
1495 let this = Arc::new_cyclic(|this| Self {
1496 this: this.clone(),
1497 executor: executor.clone(),
1498 state: Arc::new(Mutex::new(FakeFsState {
1499 root: FakeFsEntry::Dir {
1500 inode: 0,
1501 mtime: MTime(UNIX_EPOCH),
1502 len: 0,
1503 entries: Default::default(),
1504 git_repo_state: None,
1505 },
1506 git_event_tx: tx,
1507 next_mtime: UNIX_EPOCH + Self::SYSTEMTIME_INTERVAL,
1508 next_inode: 1,
1509 event_txs: Default::default(),
1510 buffered_events: Vec::new(),
1511 events_paused: false,
1512 read_dir_call_count: 0,
1513 metadata_call_count: 0,
1514 path_write_counts: Default::default(),
1515 moves: Default::default(),
1516 job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
1517 })),
1518 });
1519
1520 executor.spawn({
1521 let this = this.clone();
1522 async move {
1523 while let Ok(git_event) = rx.recv().await {
1524 if let Some(mut state) = this.state.try_lock() {
1525 state.emit_event([(git_event, Some(PathEventKind::Changed))]);
1526 } else {
1527 panic!("Failed to lock file system state, this execution would have caused a test hang");
1528 }
1529 }
1530 }
1531 }).detach();
1532
1533 this
1534 }
1535
1536 pub fn set_next_mtime(&self, next_mtime: SystemTime) {
1537 let mut state = self.state.lock();
1538 state.next_mtime = next_mtime;
1539 }
1540
1541 pub fn get_and_increment_mtime(&self) -> MTime {
1542 let mut state = self.state.lock();
1543 state.get_and_increment_mtime()
1544 }
1545
1546 pub async fn touch_path(&self, path: impl AsRef<Path>) {
1547 let mut state = self.state.lock();
1548 let path = path.as_ref();
1549 let new_mtime = state.get_and_increment_mtime();
1550 let new_inode = state.get_and_increment_inode();
1551 state
1552 .write_path(path, move |entry| {
1553 match entry {
1554 btree_map::Entry::Vacant(e) => {
1555 e.insert(FakeFsEntry::File {
1556 inode: new_inode,
1557 mtime: new_mtime,
1558 content: Vec::new(),
1559 len: 0,
1560 git_dir_path: None,
1561 });
1562 }
1563 btree_map::Entry::Occupied(mut e) => match &mut *e.get_mut() {
1564 FakeFsEntry::File { mtime, .. } => *mtime = new_mtime,
1565 FakeFsEntry::Dir { mtime, .. } => *mtime = new_mtime,
1566 FakeFsEntry::Symlink { .. } => {}
1567 },
1568 }
1569 Ok(())
1570 })
1571 .unwrap();
1572 state.emit_event([(path.to_path_buf(), Some(PathEventKind::Changed))]);
1573 }
1574
1575 pub async fn insert_file(&self, path: impl AsRef<Path>, content: Vec<u8>) {
1576 self.write_file_internal(path, content, true).unwrap()
1577 }
1578
1579 pub async fn insert_symlink(&self, path: impl AsRef<Path>, target: PathBuf) {
1580 let mut state = self.state.lock();
1581 let path = path.as_ref();
1582 let file = FakeFsEntry::Symlink { target };
1583 state
1584 .write_path(path.as_ref(), move |e| match e {
1585 btree_map::Entry::Vacant(e) => {
1586 e.insert(file);
1587 Ok(())
1588 }
1589 btree_map::Entry::Occupied(mut e) => {
1590 *e.get_mut() = file;
1591 Ok(())
1592 }
1593 })
1594 .unwrap();
1595 state.emit_event([(path, Some(PathEventKind::Created))]);
1596 }
1597
1598 fn write_file_internal(
1599 &self,
1600 path: impl AsRef<Path>,
1601 new_content: Vec<u8>,
1602 recreate_inode: bool,
1603 ) -> Result<()> {
1604 let mut state = self.state.lock();
1605 let path_buf = path.as_ref().to_path_buf();
1606 *state.path_write_counts.entry(path_buf).or_insert(0) += 1;
1607 let new_inode = state.get_and_increment_inode();
1608 let new_mtime = state.get_and_increment_mtime();
1609 let new_len = new_content.len() as u64;
1610 let mut kind = None;
1611 state.write_path(path.as_ref(), |entry| {
1612 match entry {
1613 btree_map::Entry::Vacant(e) => {
1614 kind = Some(PathEventKind::Created);
1615 e.insert(FakeFsEntry::File {
1616 inode: new_inode,
1617 mtime: new_mtime,
1618 len: new_len,
1619 content: new_content,
1620 git_dir_path: None,
1621 });
1622 }
1623 btree_map::Entry::Occupied(mut e) => {
1624 kind = Some(PathEventKind::Changed);
1625 if let FakeFsEntry::File {
1626 inode,
1627 mtime,
1628 len,
1629 content,
1630 ..
1631 } = e.get_mut()
1632 {
1633 *mtime = new_mtime;
1634 *content = new_content;
1635 *len = new_len;
1636 if recreate_inode {
1637 *inode = new_inode;
1638 }
1639 } else {
1640 anyhow::bail!("not a file")
1641 }
1642 }
1643 }
1644 Ok(())
1645 })?;
1646 state.emit_event([(path.as_ref(), kind)]);
1647 Ok(())
1648 }
1649
1650 pub fn read_file_sync(&self, path: impl AsRef<Path>) -> Result<Vec<u8>> {
1651 let path = path.as_ref();
1652 let path = normalize_path(path);
1653 let mut state = self.state.lock();
1654 let entry = state.entry(&path)?;
1655 entry.file_content(&path).cloned()
1656 }
1657
1658 async fn load_internal(&self, path: impl AsRef<Path>) -> Result<Vec<u8>> {
1659 let path = path.as_ref();
1660 let path = normalize_path(path);
1661 self.simulate_random_delay().await;
1662 let mut state = self.state.lock();
1663 let entry = state.entry(&path)?;
1664 entry.file_content(&path).cloned()
1665 }
1666
1667 pub fn pause_events(&self) {
1668 self.state.lock().events_paused = true;
1669 }
1670
1671 pub fn unpause_events_and_flush(&self) {
1672 self.state.lock().events_paused = false;
1673 self.flush_events(usize::MAX);
1674 }
1675
1676 pub fn buffered_event_count(&self) -> usize {
1677 self.state.lock().buffered_events.len()
1678 }
1679
1680 pub fn flush_events(&self, count: usize) {
1681 self.state.lock().flush_events(count);
1682 }
1683
1684 pub(crate) fn entry(&self, target: &Path) -> Result<FakeFsEntry> {
1685 self.state.lock().entry(target).cloned()
1686 }
1687
1688 pub(crate) fn insert_entry(&self, target: &Path, new_entry: FakeFsEntry) -> Result<()> {
1689 let mut state = self.state.lock();
1690 state.write_path(target, |entry| {
1691 match entry {
1692 btree_map::Entry::Vacant(vacant_entry) => {
1693 vacant_entry.insert(new_entry);
1694 }
1695 btree_map::Entry::Occupied(mut occupied_entry) => {
1696 occupied_entry.insert(new_entry);
1697 }
1698 }
1699 Ok(())
1700 })
1701 }
1702
1703 #[must_use]
1704 pub fn insert_tree<'a>(
1705 &'a self,
1706 path: impl 'a + AsRef<Path> + Send,
1707 tree: serde_json::Value,
1708 ) -> futures::future::BoxFuture<'a, ()> {
1709 use futures::FutureExt as _;
1710 use serde_json::Value::*;
1711
1712 async move {
1713 let path = path.as_ref();
1714
1715 match tree {
1716 Object(map) => {
1717 self.create_dir(path).await.unwrap();
1718 for (name, contents) in map {
1719 let mut path = PathBuf::from(path);
1720 path.push(name);
1721 self.insert_tree(&path, contents).await;
1722 }
1723 }
1724 Null => {
1725 self.create_dir(path).await.unwrap();
1726 }
1727 String(contents) => {
1728 self.insert_file(&path, contents.into_bytes()).await;
1729 }
1730 _ => {
1731 panic!("JSON object must contain only objects, strings, or null");
1732 }
1733 }
1734 }
1735 .boxed()
1736 }
1737
1738 pub fn insert_tree_from_real_fs<'a>(
1739 &'a self,
1740 path: impl 'a + AsRef<Path> + Send,
1741 src_path: impl 'a + AsRef<Path> + Send,
1742 ) -> futures::future::BoxFuture<'a, ()> {
1743 use futures::FutureExt as _;
1744
1745 async move {
1746 let path = path.as_ref();
1747 if std::fs::metadata(&src_path).unwrap().is_file() {
1748 let contents = std::fs::read(src_path).unwrap();
1749 self.insert_file(path, contents).await;
1750 } else {
1751 self.create_dir(path).await.unwrap();
1752 for entry in std::fs::read_dir(&src_path).unwrap() {
1753 let entry = entry.unwrap();
1754 self.insert_tree_from_real_fs(path.join(entry.file_name()), entry.path())
1755 .await;
1756 }
1757 }
1758 }
1759 .boxed()
1760 }
1761
1762 pub fn with_git_state_and_paths<T, F>(
1763 &self,
1764 dot_git: &Path,
1765 emit_git_event: bool,
1766 f: F,
1767 ) -> Result<T>
1768 where
1769 F: FnOnce(&mut FakeGitRepositoryState, &Path, &Path) -> T,
1770 {
1771 let mut state = self.state.lock();
1772 let git_event_tx = state.git_event_tx.clone();
1773 let entry = state.entry(dot_git).context("open .git")?;
1774
1775 if let FakeFsEntry::Dir { git_repo_state, .. } = entry {
1776 let repo_state = git_repo_state.get_or_insert_with(|| {
1777 log::debug!("insert git state for {dot_git:?}");
1778 Arc::new(Mutex::new(FakeGitRepositoryState::new(git_event_tx)))
1779 });
1780 let mut repo_state = repo_state.lock();
1781
1782 let result = f(&mut repo_state, dot_git, dot_git);
1783
1784 drop(repo_state);
1785 if emit_git_event {
1786 state.emit_event([(dot_git, Some(PathEventKind::Changed))]);
1787 }
1788
1789 Ok(result)
1790 } else if let FakeFsEntry::File {
1791 content,
1792 git_dir_path,
1793 ..
1794 } = &mut *entry
1795 {
1796 let path = match git_dir_path {
1797 Some(path) => path,
1798 None => {
1799 let path = std::str::from_utf8(content)
1800 .ok()
1801 .and_then(|content| content.strip_prefix("gitdir:"))
1802 .context("not a valid gitfile")?
1803 .trim();
1804 git_dir_path.insert(normalize_path(&dot_git.parent().unwrap().join(path)))
1805 }
1806 }
1807 .clone();
1808 let Some((git_dir_entry, canonical_path)) = state.try_entry(&path, true) else {
1809 anyhow::bail!("pointed-to git dir {path:?} not found")
1810 };
1811 let FakeFsEntry::Dir {
1812 git_repo_state,
1813 entries,
1814 ..
1815 } = git_dir_entry
1816 else {
1817 anyhow::bail!("gitfile points to a non-directory")
1818 };
1819 let common_dir = if let Some(child) = entries.get("commondir") {
1820 Path::new(
1821 std::str::from_utf8(child.file_content("commondir".as_ref())?)
1822 .context("commondir content")?,
1823 )
1824 .to_owned()
1825 } else {
1826 canonical_path.clone()
1827 };
1828 let repo_state = git_repo_state.get_or_insert_with(|| {
1829 Arc::new(Mutex::new(FakeGitRepositoryState::new(git_event_tx)))
1830 });
1831 let mut repo_state = repo_state.lock();
1832
1833 let result = f(&mut repo_state, &canonical_path, &common_dir);
1834
1835 if emit_git_event {
1836 drop(repo_state);
1837 state.emit_event([(canonical_path, Some(PathEventKind::Changed))]);
1838 }
1839
1840 Ok(result)
1841 } else {
1842 anyhow::bail!("not a valid git repository");
1843 }
1844 }
1845
1846 pub fn with_git_state<T, F>(&self, dot_git: &Path, emit_git_event: bool, f: F) -> Result<T>
1847 where
1848 F: FnOnce(&mut FakeGitRepositoryState) -> T,
1849 {
1850 self.with_git_state_and_paths(dot_git, emit_git_event, |state, _, _| f(state))
1851 }
1852
1853 pub fn set_branch_name(&self, dot_git: &Path, branch: Option<impl Into<String>>) {
1854 self.with_git_state(dot_git, true, |state| {
1855 let branch = branch.map(Into::into);
1856 state.branches.extend(branch.clone());
1857 state.current_branch_name = branch
1858 })
1859 .unwrap();
1860 }
1861
1862 pub fn insert_branches(&self, dot_git: &Path, branches: &[&str]) {
1863 self.with_git_state(dot_git, true, |state| {
1864 if let Some(first) = branches.first()
1865 && state.current_branch_name.is_none()
1866 {
1867 state.current_branch_name = Some(first.to_string())
1868 }
1869 state
1870 .branches
1871 .extend(branches.iter().map(ToString::to_string));
1872 })
1873 .unwrap();
1874 }
1875
1876 pub fn set_unmerged_paths_for_repo(
1877 &self,
1878 dot_git: &Path,
1879 unmerged_state: &[(RepoPath, UnmergedStatus)],
1880 ) {
1881 self.with_git_state(dot_git, true, |state| {
1882 state.unmerged_paths.clear();
1883 state.unmerged_paths.extend(
1884 unmerged_state
1885 .iter()
1886 .map(|(path, content)| (path.clone(), *content)),
1887 );
1888 })
1889 .unwrap();
1890 }
1891
1892 pub fn set_index_for_repo(&self, dot_git: &Path, index_state: &[(&str, String)]) {
1893 self.with_git_state(dot_git, true, |state| {
1894 state.index_contents.clear();
1895 state.index_contents.extend(
1896 index_state
1897 .iter()
1898 .map(|(path, content)| (repo_path(path), content.clone())),
1899 );
1900 })
1901 .unwrap();
1902 }
1903
1904 pub fn set_head_for_repo(
1905 &self,
1906 dot_git: &Path,
1907 head_state: &[(&str, String)],
1908 sha: impl Into<String>,
1909 ) {
1910 self.with_git_state(dot_git, true, |state| {
1911 state.head_contents.clear();
1912 state.head_contents.extend(
1913 head_state
1914 .iter()
1915 .map(|(path, content)| (repo_path(path), content.clone())),
1916 );
1917 state.refs.insert("HEAD".into(), sha.into());
1918 })
1919 .unwrap();
1920 }
1921
1922 pub fn set_head_and_index_for_repo(&self, dot_git: &Path, contents_by_path: &[(&str, String)]) {
1923 self.with_git_state(dot_git, true, |state| {
1924 state.head_contents.clear();
1925 state.head_contents.extend(
1926 contents_by_path
1927 .iter()
1928 .map(|(path, contents)| (repo_path(path), contents.clone())),
1929 );
1930 state.index_contents = state.head_contents.clone();
1931 })
1932 .unwrap();
1933 }
1934
1935 pub fn set_merge_base_content_for_repo(
1936 &self,
1937 dot_git: &Path,
1938 contents_by_path: &[(&str, String)],
1939 ) {
1940 self.with_git_state(dot_git, true, |state| {
1941 use git::Oid;
1942
1943 state.merge_base_contents.clear();
1944 let oids = (1..)
1945 .map(|n| n.to_string())
1946 .map(|n| Oid::from_bytes(n.repeat(20).as_bytes()).unwrap());
1947 for ((path, content), oid) in contents_by_path.iter().zip(oids) {
1948 state.merge_base_contents.insert(repo_path(path), oid);
1949 state.oids.insert(oid, content.clone());
1950 }
1951 })
1952 .unwrap();
1953 }
1954
1955 pub fn set_blame_for_repo(&self, dot_git: &Path, blames: Vec<(RepoPath, git::blame::Blame)>) {
1956 self.with_git_state(dot_git, true, |state| {
1957 state.blames.clear();
1958 state.blames.extend(blames);
1959 })
1960 .unwrap();
1961 }
1962
1963 /// Put the given git repository into a state with the given status,
1964 /// by mutating the head, index, and unmerged state.
1965 pub fn set_status_for_repo(&self, dot_git: &Path, statuses: &[(&str, FileStatus)]) {
1966 let workdir_path = dot_git.parent().unwrap();
1967 let workdir_contents = self.files_with_contents(workdir_path);
1968 self.with_git_state(dot_git, true, |state| {
1969 state.index_contents.clear();
1970 state.head_contents.clear();
1971 state.unmerged_paths.clear();
1972 for (path, content) in workdir_contents {
1973 use util::{paths::PathStyle, rel_path::RelPath};
1974
1975 let repo_path = RelPath::new(path.strip_prefix(&workdir_path).unwrap(), PathStyle::local()).unwrap();
1976 let repo_path = RepoPath::from_rel_path(&repo_path);
1977 let status = statuses
1978 .iter()
1979 .find_map(|(p, status)| (*p == repo_path.as_unix_str()).then_some(status));
1980 let mut content = String::from_utf8_lossy(&content).to_string();
1981
1982 let mut index_content = None;
1983 let mut head_content = None;
1984 match status {
1985 None => {
1986 index_content = Some(content.clone());
1987 head_content = Some(content);
1988 }
1989 Some(FileStatus::Untracked | FileStatus::Ignored) => {}
1990 Some(FileStatus::Unmerged(unmerged_status)) => {
1991 state
1992 .unmerged_paths
1993 .insert(repo_path.clone(), *unmerged_status);
1994 content.push_str(" (unmerged)");
1995 index_content = Some(content.clone());
1996 head_content = Some(content);
1997 }
1998 Some(FileStatus::Tracked(TrackedStatus {
1999 index_status,
2000 worktree_status,
2001 })) => {
2002 match worktree_status {
2003 StatusCode::Modified => {
2004 let mut content = content.clone();
2005 content.push_str(" (modified in working copy)");
2006 index_content = Some(content);
2007 }
2008 StatusCode::TypeChanged | StatusCode::Unmodified => {
2009 index_content = Some(content.clone());
2010 }
2011 StatusCode::Added => {}
2012 StatusCode::Deleted | StatusCode::Renamed | StatusCode::Copied => {
2013 panic!("cannot create these statuses for an existing file");
2014 }
2015 };
2016 match index_status {
2017 StatusCode::Modified => {
2018 let mut content = index_content.clone().expect(
2019 "file cannot be both modified in index and created in working copy",
2020 );
2021 content.push_str(" (modified in index)");
2022 head_content = Some(content);
2023 }
2024 StatusCode::TypeChanged | StatusCode::Unmodified => {
2025 head_content = Some(index_content.clone().expect("file cannot be both unmodified in index and created in working copy"));
2026 }
2027 StatusCode::Added => {}
2028 StatusCode::Deleted => {
2029 head_content = Some("".into());
2030 }
2031 StatusCode::Renamed | StatusCode::Copied => {
2032 panic!("cannot create these statuses for an existing file");
2033 }
2034 };
2035 }
2036 };
2037
2038 if let Some(content) = index_content {
2039 state.index_contents.insert(repo_path.clone(), content);
2040 }
2041 if let Some(content) = head_content {
2042 state.head_contents.insert(repo_path.clone(), content);
2043 }
2044 }
2045 }).unwrap();
2046 }
2047
2048 pub fn set_error_message_for_index_write(&self, dot_git: &Path, message: Option<String>) {
2049 self.with_git_state(dot_git, true, |state| {
2050 state.simulated_index_write_error_message = message;
2051 })
2052 .unwrap();
2053 }
2054
2055 pub fn paths(&self, include_dot_git: bool) -> Vec<PathBuf> {
2056 let mut result = Vec::new();
2057 let mut queue = collections::VecDeque::new();
2058 let state = &*self.state.lock();
2059 queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2060 while let Some((path, entry)) = queue.pop_front() {
2061 if let FakeFsEntry::Dir { entries, .. } = entry {
2062 for (name, entry) in entries {
2063 queue.push_back((path.join(name), entry));
2064 }
2065 }
2066 if include_dot_git
2067 || !path
2068 .components()
2069 .any(|component| component.as_os_str() == *FS_DOT_GIT)
2070 {
2071 result.push(path);
2072 }
2073 }
2074 result
2075 }
2076
2077 pub fn directories(&self, include_dot_git: bool) -> Vec<PathBuf> {
2078 let mut result = Vec::new();
2079 let mut queue = collections::VecDeque::new();
2080 let state = &*self.state.lock();
2081 queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2082 while let Some((path, entry)) = queue.pop_front() {
2083 if let FakeFsEntry::Dir { entries, .. } = entry {
2084 for (name, entry) in entries {
2085 queue.push_back((path.join(name), entry));
2086 }
2087 if include_dot_git
2088 || !path
2089 .components()
2090 .any(|component| component.as_os_str() == *FS_DOT_GIT)
2091 {
2092 result.push(path);
2093 }
2094 }
2095 }
2096 result
2097 }
2098
2099 pub fn files(&self) -> Vec<PathBuf> {
2100 let mut result = Vec::new();
2101 let mut queue = collections::VecDeque::new();
2102 let state = &*self.state.lock();
2103 queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2104 while let Some((path, entry)) = queue.pop_front() {
2105 match entry {
2106 FakeFsEntry::File { .. } => result.push(path),
2107 FakeFsEntry::Dir { entries, .. } => {
2108 for (name, entry) in entries {
2109 queue.push_back((path.join(name), entry));
2110 }
2111 }
2112 FakeFsEntry::Symlink { .. } => {}
2113 }
2114 }
2115 result
2116 }
2117
2118 pub fn files_with_contents(&self, prefix: &Path) -> Vec<(PathBuf, Vec<u8>)> {
2119 let mut result = Vec::new();
2120 let mut queue = collections::VecDeque::new();
2121 let state = &*self.state.lock();
2122 queue.push_back((PathBuf::from(util::path!("/")), &state.root));
2123 while let Some((path, entry)) = queue.pop_front() {
2124 match entry {
2125 FakeFsEntry::File { content, .. } => {
2126 if path.starts_with(prefix) {
2127 result.push((path, content.clone()));
2128 }
2129 }
2130 FakeFsEntry::Dir { entries, .. } => {
2131 for (name, entry) in entries {
2132 queue.push_back((path.join(name), entry));
2133 }
2134 }
2135 FakeFsEntry::Symlink { .. } => {}
2136 }
2137 }
2138 result
2139 }
2140
2141 /// How many `read_dir` calls have been issued.
2142 pub fn read_dir_call_count(&self) -> usize {
2143 self.state.lock().read_dir_call_count
2144 }
2145
2146 pub fn watched_paths(&self) -> Vec<PathBuf> {
2147 let state = self.state.lock();
2148 state
2149 .event_txs
2150 .iter()
2151 .filter_map(|(path, tx)| Some(path.clone()).filter(|_| !tx.is_closed()))
2152 .collect()
2153 }
2154
2155 /// How many `metadata` calls have been issued.
2156 pub fn metadata_call_count(&self) -> usize {
2157 self.state.lock().metadata_call_count
2158 }
2159
2160 /// How many write operations have been issued for a specific path.
2161 pub fn write_count_for_path(&self, path: impl AsRef<Path>) -> usize {
2162 let path = path.as_ref().to_path_buf();
2163 self.state
2164 .lock()
2165 .path_write_counts
2166 .get(&path)
2167 .copied()
2168 .unwrap_or(0)
2169 }
2170
2171 pub fn emit_fs_event(&self, path: impl Into<PathBuf>, event: Option<PathEventKind>) {
2172 self.state.lock().emit_event(std::iter::once((path, event)));
2173 }
2174
2175 fn simulate_random_delay(&self) -> impl futures::Future<Output = ()> {
2176 self.executor.simulate_random_delay()
2177 }
2178}
2179
2180#[cfg(any(test, feature = "test-support"))]
2181impl FakeFsEntry {
2182 fn is_file(&self) -> bool {
2183 matches!(self, Self::File { .. })
2184 }
2185
2186 fn is_symlink(&self) -> bool {
2187 matches!(self, Self::Symlink { .. })
2188 }
2189
2190 fn file_content(&self, path: &Path) -> Result<&Vec<u8>> {
2191 if let Self::File { content, .. } = self {
2192 Ok(content)
2193 } else {
2194 anyhow::bail!("not a file: {path:?}");
2195 }
2196 }
2197
2198 fn dir_entries(&mut self, path: &Path) -> Result<&mut BTreeMap<String, FakeFsEntry>> {
2199 if let Self::Dir { entries, .. } = self {
2200 Ok(entries)
2201 } else {
2202 anyhow::bail!("not a directory: {path:?}");
2203 }
2204 }
2205}
2206
2207#[cfg(any(test, feature = "test-support"))]
2208struct FakeWatcher {
2209 tx: smol::channel::Sender<Vec<PathEvent>>,
2210 original_path: PathBuf,
2211 fs_state: Arc<Mutex<FakeFsState>>,
2212 prefixes: Mutex<Vec<PathBuf>>,
2213}
2214
2215#[cfg(any(test, feature = "test-support"))]
2216impl Watcher for FakeWatcher {
2217 fn add(&self, path: &Path) -> Result<()> {
2218 if path.starts_with(&self.original_path) {
2219 return Ok(());
2220 }
2221 self.fs_state
2222 .try_lock()
2223 .unwrap()
2224 .event_txs
2225 .push((path.to_owned(), self.tx.clone()));
2226 self.prefixes.lock().push(path.to_owned());
2227 Ok(())
2228 }
2229
2230 fn remove(&self, _: &Path) -> Result<()> {
2231 Ok(())
2232 }
2233}
2234
2235#[cfg(any(test, feature = "test-support"))]
2236#[derive(Debug)]
2237struct FakeHandle {
2238 inode: u64,
2239}
2240
2241#[cfg(any(test, feature = "test-support"))]
2242impl FileHandle for FakeHandle {
2243 fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf> {
2244 let fs = fs.as_fake();
2245 let mut state = fs.state.lock();
2246 let Some(target) = state.moves.get(&self.inode).cloned() else {
2247 anyhow::bail!("fake fd not moved")
2248 };
2249
2250 if state.try_entry(&target, false).is_some() {
2251 return Ok(target);
2252 }
2253 anyhow::bail!("fake fd target not found")
2254 }
2255}
2256
2257#[cfg(any(test, feature = "test-support"))]
2258#[async_trait::async_trait]
2259impl Fs for FakeFs {
2260 async fn create_dir(&self, path: &Path) -> Result<()> {
2261 self.simulate_random_delay().await;
2262
2263 let mut created_dirs = Vec::new();
2264 let mut cur_path = PathBuf::new();
2265 for component in path.components() {
2266 let should_skip = matches!(component, Component::Prefix(..) | Component::RootDir);
2267 cur_path.push(component);
2268 if should_skip {
2269 continue;
2270 }
2271 let mut state = self.state.lock();
2272
2273 let inode = state.get_and_increment_inode();
2274 let mtime = state.get_and_increment_mtime();
2275 state.write_path(&cur_path, |entry| {
2276 entry.or_insert_with(|| {
2277 created_dirs.push((cur_path.clone(), Some(PathEventKind::Created)));
2278 FakeFsEntry::Dir {
2279 inode,
2280 mtime,
2281 len: 0,
2282 entries: Default::default(),
2283 git_repo_state: None,
2284 }
2285 });
2286 Ok(())
2287 })?
2288 }
2289
2290 self.state.lock().emit_event(created_dirs);
2291 Ok(())
2292 }
2293
2294 async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()> {
2295 self.simulate_random_delay().await;
2296 let mut state = self.state.lock();
2297 let inode = state.get_and_increment_inode();
2298 let mtime = state.get_and_increment_mtime();
2299 let file = FakeFsEntry::File {
2300 inode,
2301 mtime,
2302 len: 0,
2303 content: Vec::new(),
2304 git_dir_path: None,
2305 };
2306 let mut kind = Some(PathEventKind::Created);
2307 state.write_path(path, |entry| {
2308 match entry {
2309 btree_map::Entry::Occupied(mut e) => {
2310 if options.overwrite {
2311 kind = Some(PathEventKind::Changed);
2312 *e.get_mut() = file;
2313 } else if !options.ignore_if_exists {
2314 anyhow::bail!("path already exists: {path:?}");
2315 }
2316 }
2317 btree_map::Entry::Vacant(e) => {
2318 e.insert(file);
2319 }
2320 }
2321 Ok(())
2322 })?;
2323 state.emit_event([(path, kind)]);
2324 Ok(())
2325 }
2326
2327 async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()> {
2328 let mut state = self.state.lock();
2329 let file = FakeFsEntry::Symlink { target };
2330 state
2331 .write_path(path.as_ref(), move |e| match e {
2332 btree_map::Entry::Vacant(e) => {
2333 e.insert(file);
2334 Ok(())
2335 }
2336 btree_map::Entry::Occupied(mut e) => {
2337 *e.get_mut() = file;
2338 Ok(())
2339 }
2340 })
2341 .unwrap();
2342 state.emit_event([(path, Some(PathEventKind::Created))]);
2343
2344 Ok(())
2345 }
2346
2347 async fn create_file_with(
2348 &self,
2349 path: &Path,
2350 mut content: Pin<&mut (dyn AsyncRead + Send)>,
2351 ) -> Result<()> {
2352 let mut bytes = Vec::new();
2353 content.read_to_end(&mut bytes).await?;
2354 self.write_file_internal(path, bytes, true)?;
2355 Ok(())
2356 }
2357
2358 async fn extract_tar_file(
2359 &self,
2360 path: &Path,
2361 content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
2362 ) -> Result<()> {
2363 let mut entries = content.entries()?;
2364 while let Some(entry) = entries.next().await {
2365 let mut entry = entry?;
2366 if entry.header().entry_type().is_file() {
2367 let path = path.join(entry.path()?.as_ref());
2368 let mut bytes = Vec::new();
2369 entry.read_to_end(&mut bytes).await?;
2370 self.create_dir(path.parent().unwrap()).await?;
2371 self.write_file_internal(&path, bytes, true)?;
2372 }
2373 }
2374 Ok(())
2375 }
2376
2377 async fn rename(&self, old_path: &Path, new_path: &Path, options: RenameOptions) -> Result<()> {
2378 self.simulate_random_delay().await;
2379
2380 let old_path = normalize_path(old_path);
2381 let new_path = normalize_path(new_path);
2382
2383 if options.create_parents {
2384 if let Some(parent) = new_path.parent() {
2385 self.create_dir(parent).await?;
2386 }
2387 }
2388
2389 let mut state = self.state.lock();
2390 let moved_entry = state.write_path(&old_path, |e| {
2391 if let btree_map::Entry::Occupied(e) = e {
2392 Ok(e.get().clone())
2393 } else {
2394 anyhow::bail!("path does not exist: {old_path:?}")
2395 }
2396 })?;
2397
2398 let inode = match moved_entry {
2399 FakeFsEntry::File { inode, .. } => inode,
2400 FakeFsEntry::Dir { inode, .. } => inode,
2401 _ => 0,
2402 };
2403
2404 state.moves.insert(inode, new_path.clone());
2405
2406 state.write_path(&new_path, |e| {
2407 match e {
2408 btree_map::Entry::Occupied(mut e) => {
2409 if options.overwrite {
2410 *e.get_mut() = moved_entry;
2411 } else if !options.ignore_if_exists {
2412 anyhow::bail!("path already exists: {new_path:?}");
2413 }
2414 }
2415 btree_map::Entry::Vacant(e) => {
2416 e.insert(moved_entry);
2417 }
2418 }
2419 Ok(())
2420 })?;
2421
2422 state
2423 .write_path(&old_path, |e| {
2424 if let btree_map::Entry::Occupied(e) = e {
2425 Ok(e.remove())
2426 } else {
2427 unreachable!()
2428 }
2429 })
2430 .unwrap();
2431
2432 state.emit_event([
2433 (old_path, Some(PathEventKind::Removed)),
2434 (new_path, Some(PathEventKind::Created)),
2435 ]);
2436 Ok(())
2437 }
2438
2439 async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
2440 self.simulate_random_delay().await;
2441
2442 let source = normalize_path(source);
2443 let target = normalize_path(target);
2444 let mut state = self.state.lock();
2445 let mtime = state.get_and_increment_mtime();
2446 let inode = state.get_and_increment_inode();
2447 let source_entry = state.entry(&source)?;
2448 let content = source_entry.file_content(&source)?.clone();
2449 let mut kind = Some(PathEventKind::Created);
2450 state.write_path(&target, |e| match e {
2451 btree_map::Entry::Occupied(e) => {
2452 if options.overwrite {
2453 kind = Some(PathEventKind::Changed);
2454 Ok(Some(e.get().clone()))
2455 } else if !options.ignore_if_exists {
2456 anyhow::bail!("{target:?} already exists");
2457 } else {
2458 Ok(None)
2459 }
2460 }
2461 btree_map::Entry::Vacant(e) => Ok(Some(
2462 e.insert(FakeFsEntry::File {
2463 inode,
2464 mtime,
2465 len: content.len() as u64,
2466 content,
2467 git_dir_path: None,
2468 })
2469 .clone(),
2470 )),
2471 })?;
2472 state.emit_event([(target, kind)]);
2473 Ok(())
2474 }
2475
2476 async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> {
2477 self.simulate_random_delay().await;
2478
2479 let path = normalize_path(path);
2480 let parent_path = path.parent().context("cannot remove the root")?;
2481 let base_name = path.file_name().context("cannot remove the root")?;
2482
2483 let mut state = self.state.lock();
2484 let parent_entry = state.entry(parent_path)?;
2485 let entry = parent_entry
2486 .dir_entries(parent_path)?
2487 .entry(base_name.to_str().unwrap().into());
2488
2489 match entry {
2490 btree_map::Entry::Vacant(_) => {
2491 if !options.ignore_if_not_exists {
2492 anyhow::bail!("{path:?} does not exist");
2493 }
2494 }
2495 btree_map::Entry::Occupied(mut entry) => {
2496 {
2497 let children = entry.get_mut().dir_entries(&path)?;
2498 if !options.recursive && !children.is_empty() {
2499 anyhow::bail!("{path:?} is not empty");
2500 }
2501 }
2502 entry.remove();
2503 }
2504 }
2505 state.emit_event([(path, Some(PathEventKind::Removed))]);
2506 Ok(())
2507 }
2508
2509 async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()> {
2510 self.simulate_random_delay().await;
2511
2512 let path = normalize_path(path);
2513 let parent_path = path.parent().context("cannot remove the root")?;
2514 let base_name = path.file_name().unwrap();
2515 let mut state = self.state.lock();
2516 let parent_entry = state.entry(parent_path)?;
2517 let entry = parent_entry
2518 .dir_entries(parent_path)?
2519 .entry(base_name.to_str().unwrap().into());
2520 match entry {
2521 btree_map::Entry::Vacant(_) => {
2522 if !options.ignore_if_not_exists {
2523 anyhow::bail!("{path:?} does not exist");
2524 }
2525 }
2526 btree_map::Entry::Occupied(mut entry) => {
2527 entry.get_mut().file_content(&path)?;
2528 entry.remove();
2529 }
2530 }
2531 state.emit_event([(path, Some(PathEventKind::Removed))]);
2532 Ok(())
2533 }
2534
2535 async fn open_read(&self, path: &Path) -> Result<AsyncReadBox> {
2536 let bytes = self.load_internal(path).await?;
2537 Ok(Box::pin(futures::io::Cursor::new(bytes)))
2538 }
2539
2540 async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>> {
2541 self.simulate_random_delay().await;
2542 let mut state = self.state.lock();
2543 let inode = match state.entry(path)? {
2544 FakeFsEntry::File { inode, .. } => *inode,
2545 FakeFsEntry::Dir { inode, .. } => *inode,
2546 _ => unreachable!(),
2547 };
2548 Ok(Arc::new(FakeHandle { inode }))
2549 }
2550
2551 async fn load(&self, path: &Path) -> Result<String> {
2552 let content = self.load_internal(path).await?;
2553 Ok(String::from_utf8(content)?)
2554 }
2555
2556 async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>> {
2557 self.load_internal(path).await
2558 }
2559
2560 async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
2561 self.simulate_random_delay().await;
2562 let path = normalize_path(path.as_path());
2563 if let Some(path) = path.parent() {
2564 self.create_dir(path).await?;
2565 }
2566 self.write_file_internal(path, data.into_bytes(), true)?;
2567 Ok(())
2568 }
2569
2570 async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> {
2571 self.simulate_random_delay().await;
2572 let path = normalize_path(path);
2573 let content = text::chunks_with_line_ending(text, line_ending).collect::<String>();
2574 if let Some(path) = path.parent() {
2575 self.create_dir(path).await?;
2576 }
2577 self.write_file_internal(path, content.into_bytes(), false)?;
2578 Ok(())
2579 }
2580
2581 async fn write(&self, path: &Path, content: &[u8]) -> Result<()> {
2582 self.simulate_random_delay().await;
2583 let path = normalize_path(path);
2584 if let Some(path) = path.parent() {
2585 self.create_dir(path).await?;
2586 }
2587 self.write_file_internal(path, content.to_vec(), false)?;
2588 Ok(())
2589 }
2590
2591 async fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
2592 let path = normalize_path(path);
2593 self.simulate_random_delay().await;
2594 let state = self.state.lock();
2595 let canonical_path = state
2596 .canonicalize(&path, true)
2597 .with_context(|| format!("path does not exist: {path:?}"))?;
2598 Ok(canonical_path)
2599 }
2600
2601 async fn is_file(&self, path: &Path) -> bool {
2602 let path = normalize_path(path);
2603 self.simulate_random_delay().await;
2604 let mut state = self.state.lock();
2605 if let Some((entry, _)) = state.try_entry(&path, true) {
2606 entry.is_file()
2607 } else {
2608 false
2609 }
2610 }
2611
2612 async fn is_dir(&self, path: &Path) -> bool {
2613 self.metadata(path)
2614 .await
2615 .is_ok_and(|metadata| metadata.is_some_and(|metadata| metadata.is_dir))
2616 }
2617
2618 async fn metadata(&self, path: &Path) -> Result<Option<Metadata>> {
2619 self.simulate_random_delay().await;
2620 let path = normalize_path(path);
2621 let mut state = self.state.lock();
2622 state.metadata_call_count += 1;
2623 if let Some((mut entry, _)) = state.try_entry(&path, false) {
2624 let is_symlink = entry.is_symlink();
2625 if is_symlink {
2626 if let Some(e) = state.try_entry(&path, true).map(|e| e.0) {
2627 entry = e;
2628 } else {
2629 return Ok(None);
2630 }
2631 }
2632
2633 Ok(Some(match &*entry {
2634 FakeFsEntry::File {
2635 inode, mtime, len, ..
2636 } => Metadata {
2637 inode: *inode,
2638 mtime: *mtime,
2639 len: *len,
2640 is_dir: false,
2641 is_symlink,
2642 is_fifo: false,
2643 is_executable: false,
2644 },
2645 FakeFsEntry::Dir {
2646 inode, mtime, len, ..
2647 } => Metadata {
2648 inode: *inode,
2649 mtime: *mtime,
2650 len: *len,
2651 is_dir: true,
2652 is_symlink,
2653 is_fifo: false,
2654 is_executable: false,
2655 },
2656 FakeFsEntry::Symlink { .. } => unreachable!(),
2657 }))
2658 } else {
2659 Ok(None)
2660 }
2661 }
2662
2663 async fn read_link(&self, path: &Path) -> Result<PathBuf> {
2664 self.simulate_random_delay().await;
2665 let path = normalize_path(path);
2666 let mut state = self.state.lock();
2667 let (entry, _) = state
2668 .try_entry(&path, false)
2669 .with_context(|| format!("path does not exist: {path:?}"))?;
2670 if let FakeFsEntry::Symlink { target } = entry {
2671 Ok(target.clone())
2672 } else {
2673 anyhow::bail!("not a symlink: {path:?}")
2674 }
2675 }
2676
2677 async fn read_dir(
2678 &self,
2679 path: &Path,
2680 ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
2681 self.simulate_random_delay().await;
2682 let path = normalize_path(path);
2683 let mut state = self.state.lock();
2684 state.read_dir_call_count += 1;
2685 let entry = state.entry(&path)?;
2686 let children = entry.dir_entries(&path)?;
2687 let paths = children
2688 .keys()
2689 .map(|file_name| Ok(path.join(file_name)))
2690 .collect::<Vec<_>>();
2691 Ok(Box::pin(futures::stream::iter(paths)))
2692 }
2693
2694 async fn watch(
2695 &self,
2696 path: &Path,
2697 _: Duration,
2698 ) -> (
2699 Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,
2700 Arc<dyn Watcher>,
2701 ) {
2702 self.simulate_random_delay().await;
2703 let (tx, rx) = smol::channel::unbounded();
2704 let path = path.to_path_buf();
2705 self.state.lock().event_txs.push((path.clone(), tx.clone()));
2706 let executor = self.executor.clone();
2707 let watcher = Arc::new(FakeWatcher {
2708 tx,
2709 original_path: path.to_owned(),
2710 fs_state: self.state.clone(),
2711 prefixes: Mutex::new(vec![path]),
2712 });
2713 (
2714 Box::pin(futures::StreamExt::filter(rx, {
2715 let watcher = watcher.clone();
2716 move |events| {
2717 let result = events.iter().any(|evt_path| {
2718 watcher
2719 .prefixes
2720 .lock()
2721 .iter()
2722 .any(|prefix| evt_path.path.starts_with(prefix))
2723 });
2724 let executor = executor.clone();
2725 async move {
2726 executor.simulate_random_delay().await;
2727 result
2728 }
2729 }
2730 })),
2731 watcher,
2732 )
2733 }
2734
2735 fn open_repo(
2736 &self,
2737 abs_dot_git: &Path,
2738 _system_git_binary: Option<&Path>,
2739 ) -> Option<Arc<dyn GitRepository>> {
2740 use util::ResultExt as _;
2741
2742 self.with_git_state_and_paths(
2743 abs_dot_git,
2744 false,
2745 |_, repository_dir_path, common_dir_path| {
2746 Arc::new(fake_git_repo::FakeGitRepository {
2747 fs: self.this.upgrade().unwrap(),
2748 executor: self.executor.clone(),
2749 dot_git_path: abs_dot_git.to_path_buf(),
2750 repository_dir_path: repository_dir_path.to_owned(),
2751 common_dir_path: common_dir_path.to_owned(),
2752 checkpoints: Arc::default(),
2753 }) as _
2754 },
2755 )
2756 .log_err()
2757 }
2758
2759 async fn git_init(
2760 &self,
2761 abs_work_directory_path: &Path,
2762 _fallback_branch_name: String,
2763 ) -> Result<()> {
2764 self.create_dir(&abs_work_directory_path.join(".git")).await
2765 }
2766
2767 async fn git_clone(&self, _repo_url: &str, _abs_work_directory: &Path) -> Result<()> {
2768 anyhow::bail!("Git clone is not supported in fake Fs")
2769 }
2770
2771 fn is_fake(&self) -> bool {
2772 true
2773 }
2774
2775 async fn is_case_sensitive(&self) -> Result<bool> {
2776 Ok(true)
2777 }
2778
2779 fn subscribe_to_jobs(&self) -> JobEventReceiver {
2780 let (sender, receiver) = futures::channel::mpsc::unbounded();
2781 self.state.lock().job_event_subscribers.lock().push(sender);
2782 receiver
2783 }
2784
2785 #[cfg(any(test, feature = "test-support"))]
2786 fn as_fake(&self) -> Arc<FakeFs> {
2787 self.this.upgrade().unwrap()
2788 }
2789}
2790
2791pub fn normalize_path(path: &Path) -> PathBuf {
2792 let mut components = path.components().peekable();
2793 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
2794 components.next();
2795 PathBuf::from(c.as_os_str())
2796 } else {
2797 PathBuf::new()
2798 };
2799
2800 for component in components {
2801 match component {
2802 Component::Prefix(..) => unreachable!(),
2803 Component::RootDir => {
2804 ret.push(component.as_os_str());
2805 }
2806 Component::CurDir => {}
2807 Component::ParentDir => {
2808 ret.pop();
2809 }
2810 Component::Normal(c) => {
2811 ret.push(c);
2812 }
2813 }
2814 }
2815 ret
2816}
2817
2818pub async fn copy_recursive<'a>(
2819 fs: &'a dyn Fs,
2820 source: &'a Path,
2821 target: &'a Path,
2822 options: CopyOptions,
2823) -> Result<()> {
2824 for (item, is_dir) in read_dir_items(fs, source).await? {
2825 let Ok(item_relative_path) = item.strip_prefix(source) else {
2826 continue;
2827 };
2828 let target_item = if item_relative_path == Path::new("") {
2829 target.to_path_buf()
2830 } else {
2831 target.join(item_relative_path)
2832 };
2833 if is_dir {
2834 if !options.overwrite && fs.metadata(&target_item).await.is_ok_and(|m| m.is_some()) {
2835 if options.ignore_if_exists {
2836 continue;
2837 } else {
2838 anyhow::bail!("{target_item:?} already exists");
2839 }
2840 }
2841 let _ = fs
2842 .remove_dir(
2843 &target_item,
2844 RemoveOptions {
2845 recursive: true,
2846 ignore_if_not_exists: true,
2847 },
2848 )
2849 .await;
2850 fs.create_dir(&target_item).await?;
2851 } else {
2852 fs.copy_file(&item, &target_item, options).await?;
2853 }
2854 }
2855 Ok(())
2856}
2857
2858/// Recursively reads all of the paths in the given directory.
2859///
2860/// Returns a vector of tuples of (path, is_dir).
2861pub async fn read_dir_items<'a>(fs: &'a dyn Fs, source: &'a Path) -> Result<Vec<(PathBuf, bool)>> {
2862 let mut items = Vec::new();
2863 read_recursive(fs, source, &mut items).await?;
2864 Ok(items)
2865}
2866
2867fn read_recursive<'a>(
2868 fs: &'a dyn Fs,
2869 source: &'a Path,
2870 output: &'a mut Vec<(PathBuf, bool)>,
2871) -> BoxFuture<'a, Result<()>> {
2872 use futures::future::FutureExt;
2873
2874 async move {
2875 let metadata = fs
2876 .metadata(source)
2877 .await?
2878 .with_context(|| format!("path does not exist: {source:?}"))?;
2879
2880 if metadata.is_dir {
2881 output.push((source.to_path_buf(), true));
2882 let mut children = fs.read_dir(source).await?;
2883 while let Some(child_path) = children.next().await {
2884 if let Ok(child_path) = child_path {
2885 read_recursive(fs, &child_path, output).await?;
2886 }
2887 }
2888 } else {
2889 output.push((source.to_path_buf(), false));
2890 }
2891 Ok(())
2892 }
2893 .boxed()
2894}
2895
2896// todo(windows)
2897// can we get file id not open the file twice?
2898// https://github.com/rust-lang/rust/issues/63010
2899#[cfg(target_os = "windows")]
2900async fn file_id(path: impl AsRef<Path>) -> Result<u64> {
2901 use std::os::windows::io::AsRawHandle;
2902
2903 use smol::fs::windows::OpenOptionsExt;
2904 use windows::Win32::{
2905 Foundation::HANDLE,
2906 Storage::FileSystem::{
2907 BY_HANDLE_FILE_INFORMATION, FILE_FLAG_BACKUP_SEMANTICS, GetFileInformationByHandle,
2908 },
2909 };
2910
2911 let file = smol::fs::OpenOptions::new()
2912 .read(true)
2913 .custom_flags(FILE_FLAG_BACKUP_SEMANTICS.0)
2914 .open(path)
2915 .await?;
2916
2917 let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() };
2918 // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle
2919 // This function supports Windows XP+
2920 smol::unblock(move || {
2921 unsafe { GetFileInformationByHandle(HANDLE(file.as_raw_handle() as _), &mut info)? };
2922
2923 Ok(((info.nFileIndexHigh as u64) << 32) | (info.nFileIndexLow as u64))
2924 })
2925 .await
2926}
2927
2928#[cfg(target_os = "windows")]
2929fn atomic_replace<P: AsRef<Path>>(
2930 replaced_file: P,
2931 replacement_file: P,
2932) -> windows::core::Result<()> {
2933 use windows::{
2934 Win32::Storage::FileSystem::{REPLACE_FILE_FLAGS, ReplaceFileW},
2935 core::HSTRING,
2936 };
2937
2938 // If the file does not exist, create it.
2939 let _ = std::fs::File::create_new(replaced_file.as_ref());
2940
2941 unsafe {
2942 ReplaceFileW(
2943 &HSTRING::from(replaced_file.as_ref().to_string_lossy().into_owned()),
2944 &HSTRING::from(replacement_file.as_ref().to_string_lossy().into_owned()),
2945 None,
2946 REPLACE_FILE_FLAGS::default(),
2947 None,
2948 None,
2949 )
2950 }
2951}
2952
2953#[cfg(test)]
2954mod tests {
2955 use super::*;
2956 use gpui::BackgroundExecutor;
2957 use serde_json::json;
2958 use util::path;
2959
2960 #[gpui::test]
2961 async fn test_fake_fs(executor: BackgroundExecutor) {
2962 let fs = FakeFs::new(executor.clone());
2963 fs.insert_tree(
2964 path!("/root"),
2965 json!({
2966 "dir1": {
2967 "a": "A",
2968 "b": "B"
2969 },
2970 "dir2": {
2971 "c": "C",
2972 "dir3": {
2973 "d": "D"
2974 }
2975 }
2976 }),
2977 )
2978 .await;
2979
2980 assert_eq!(
2981 fs.files(),
2982 vec![
2983 PathBuf::from(path!("/root/dir1/a")),
2984 PathBuf::from(path!("/root/dir1/b")),
2985 PathBuf::from(path!("/root/dir2/c")),
2986 PathBuf::from(path!("/root/dir2/dir3/d")),
2987 ]
2988 );
2989
2990 fs.create_symlink(path!("/root/dir2/link-to-dir3").as_ref(), "./dir3".into())
2991 .await
2992 .unwrap();
2993
2994 assert_eq!(
2995 fs.canonicalize(path!("/root/dir2/link-to-dir3").as_ref())
2996 .await
2997 .unwrap(),
2998 PathBuf::from(path!("/root/dir2/dir3")),
2999 );
3000 assert_eq!(
3001 fs.canonicalize(path!("/root/dir2/link-to-dir3/d").as_ref())
3002 .await
3003 .unwrap(),
3004 PathBuf::from(path!("/root/dir2/dir3/d")),
3005 );
3006 assert_eq!(
3007 fs.load(path!("/root/dir2/link-to-dir3/d").as_ref())
3008 .await
3009 .unwrap(),
3010 "D",
3011 );
3012 }
3013
3014 #[gpui::test]
3015 async fn test_copy_recursive_with_single_file(executor: BackgroundExecutor) {
3016 let fs = FakeFs::new(executor.clone());
3017 fs.insert_tree(
3018 path!("/outer"),
3019 json!({
3020 "a": "A",
3021 "b": "B",
3022 "inner": {}
3023 }),
3024 )
3025 .await;
3026
3027 assert_eq!(
3028 fs.files(),
3029 vec![
3030 PathBuf::from(path!("/outer/a")),
3031 PathBuf::from(path!("/outer/b")),
3032 ]
3033 );
3034
3035 let source = Path::new(path!("/outer/a"));
3036 let target = Path::new(path!("/outer/a copy"));
3037 copy_recursive(fs.as_ref(), source, target, Default::default())
3038 .await
3039 .unwrap();
3040
3041 assert_eq!(
3042 fs.files(),
3043 vec![
3044 PathBuf::from(path!("/outer/a")),
3045 PathBuf::from(path!("/outer/a copy")),
3046 PathBuf::from(path!("/outer/b")),
3047 ]
3048 );
3049
3050 let source = Path::new(path!("/outer/a"));
3051 let target = Path::new(path!("/outer/inner/a copy"));
3052 copy_recursive(fs.as_ref(), source, target, Default::default())
3053 .await
3054 .unwrap();
3055
3056 assert_eq!(
3057 fs.files(),
3058 vec![
3059 PathBuf::from(path!("/outer/a")),
3060 PathBuf::from(path!("/outer/a copy")),
3061 PathBuf::from(path!("/outer/b")),
3062 PathBuf::from(path!("/outer/inner/a copy")),
3063 ]
3064 );
3065 }
3066
3067 #[gpui::test]
3068 async fn test_copy_recursive_with_single_dir(executor: BackgroundExecutor) {
3069 let fs = FakeFs::new(executor.clone());
3070 fs.insert_tree(
3071 path!("/outer"),
3072 json!({
3073 "a": "A",
3074 "empty": {},
3075 "non-empty": {
3076 "b": "B",
3077 }
3078 }),
3079 )
3080 .await;
3081
3082 assert_eq!(
3083 fs.files(),
3084 vec![
3085 PathBuf::from(path!("/outer/a")),
3086 PathBuf::from(path!("/outer/non-empty/b")),
3087 ]
3088 );
3089 assert_eq!(
3090 fs.directories(false),
3091 vec![
3092 PathBuf::from(path!("/")),
3093 PathBuf::from(path!("/outer")),
3094 PathBuf::from(path!("/outer/empty")),
3095 PathBuf::from(path!("/outer/non-empty")),
3096 ]
3097 );
3098
3099 let source = Path::new(path!("/outer/empty"));
3100 let target = Path::new(path!("/outer/empty copy"));
3101 copy_recursive(fs.as_ref(), source, target, Default::default())
3102 .await
3103 .unwrap();
3104
3105 assert_eq!(
3106 fs.files(),
3107 vec![
3108 PathBuf::from(path!("/outer/a")),
3109 PathBuf::from(path!("/outer/non-empty/b")),
3110 ]
3111 );
3112 assert_eq!(
3113 fs.directories(false),
3114 vec![
3115 PathBuf::from(path!("/")),
3116 PathBuf::from(path!("/outer")),
3117 PathBuf::from(path!("/outer/empty")),
3118 PathBuf::from(path!("/outer/empty copy")),
3119 PathBuf::from(path!("/outer/non-empty")),
3120 ]
3121 );
3122
3123 let source = Path::new(path!("/outer/non-empty"));
3124 let target = Path::new(path!("/outer/non-empty copy"));
3125 copy_recursive(fs.as_ref(), source, target, Default::default())
3126 .await
3127 .unwrap();
3128
3129 assert_eq!(
3130 fs.files(),
3131 vec![
3132 PathBuf::from(path!("/outer/a")),
3133 PathBuf::from(path!("/outer/non-empty/b")),
3134 PathBuf::from(path!("/outer/non-empty copy/b")),
3135 ]
3136 );
3137 assert_eq!(
3138 fs.directories(false),
3139 vec![
3140 PathBuf::from(path!("/")),
3141 PathBuf::from(path!("/outer")),
3142 PathBuf::from(path!("/outer/empty")),
3143 PathBuf::from(path!("/outer/empty copy")),
3144 PathBuf::from(path!("/outer/non-empty")),
3145 PathBuf::from(path!("/outer/non-empty copy")),
3146 ]
3147 );
3148 }
3149
3150 #[gpui::test]
3151 async fn test_copy_recursive(executor: BackgroundExecutor) {
3152 let fs = FakeFs::new(executor.clone());
3153 fs.insert_tree(
3154 path!("/outer"),
3155 json!({
3156 "inner1": {
3157 "a": "A",
3158 "b": "B",
3159 "inner3": {
3160 "d": "D",
3161 },
3162 "inner4": {}
3163 },
3164 "inner2": {
3165 "c": "C",
3166 }
3167 }),
3168 )
3169 .await;
3170
3171 assert_eq!(
3172 fs.files(),
3173 vec![
3174 PathBuf::from(path!("/outer/inner1/a")),
3175 PathBuf::from(path!("/outer/inner1/b")),
3176 PathBuf::from(path!("/outer/inner2/c")),
3177 PathBuf::from(path!("/outer/inner1/inner3/d")),
3178 ]
3179 );
3180 assert_eq!(
3181 fs.directories(false),
3182 vec![
3183 PathBuf::from(path!("/")),
3184 PathBuf::from(path!("/outer")),
3185 PathBuf::from(path!("/outer/inner1")),
3186 PathBuf::from(path!("/outer/inner2")),
3187 PathBuf::from(path!("/outer/inner1/inner3")),
3188 PathBuf::from(path!("/outer/inner1/inner4")),
3189 ]
3190 );
3191
3192 let source = Path::new(path!("/outer"));
3193 let target = Path::new(path!("/outer/inner1/outer"));
3194 copy_recursive(fs.as_ref(), source, target, Default::default())
3195 .await
3196 .unwrap();
3197
3198 assert_eq!(
3199 fs.files(),
3200 vec![
3201 PathBuf::from(path!("/outer/inner1/a")),
3202 PathBuf::from(path!("/outer/inner1/b")),
3203 PathBuf::from(path!("/outer/inner2/c")),
3204 PathBuf::from(path!("/outer/inner1/inner3/d")),
3205 PathBuf::from(path!("/outer/inner1/outer/inner1/a")),
3206 PathBuf::from(path!("/outer/inner1/outer/inner1/b")),
3207 PathBuf::from(path!("/outer/inner1/outer/inner2/c")),
3208 PathBuf::from(path!("/outer/inner1/outer/inner1/inner3/d")),
3209 ]
3210 );
3211 assert_eq!(
3212 fs.directories(false),
3213 vec![
3214 PathBuf::from(path!("/")),
3215 PathBuf::from(path!("/outer")),
3216 PathBuf::from(path!("/outer/inner1")),
3217 PathBuf::from(path!("/outer/inner2")),
3218 PathBuf::from(path!("/outer/inner1/inner3")),
3219 PathBuf::from(path!("/outer/inner1/inner4")),
3220 PathBuf::from(path!("/outer/inner1/outer")),
3221 PathBuf::from(path!("/outer/inner1/outer/inner1")),
3222 PathBuf::from(path!("/outer/inner1/outer/inner2")),
3223 PathBuf::from(path!("/outer/inner1/outer/inner1/inner3")),
3224 PathBuf::from(path!("/outer/inner1/outer/inner1/inner4")),
3225 ]
3226 );
3227 }
3228
3229 #[gpui::test]
3230 async fn test_copy_recursive_with_overwriting(executor: BackgroundExecutor) {
3231 let fs = FakeFs::new(executor.clone());
3232 fs.insert_tree(
3233 path!("/outer"),
3234 json!({
3235 "inner1": {
3236 "a": "A",
3237 "b": "B",
3238 "outer": {
3239 "inner1": {
3240 "a": "B"
3241 }
3242 }
3243 },
3244 "inner2": {
3245 "c": "C",
3246 }
3247 }),
3248 )
3249 .await;
3250
3251 assert_eq!(
3252 fs.files(),
3253 vec![
3254 PathBuf::from(path!("/outer/inner1/a")),
3255 PathBuf::from(path!("/outer/inner1/b")),
3256 PathBuf::from(path!("/outer/inner2/c")),
3257 PathBuf::from(path!("/outer/inner1/outer/inner1/a")),
3258 ]
3259 );
3260 assert_eq!(
3261 fs.load(path!("/outer/inner1/outer/inner1/a").as_ref())
3262 .await
3263 .unwrap(),
3264 "B",
3265 );
3266
3267 let source = Path::new(path!("/outer"));
3268 let target = Path::new(path!("/outer/inner1/outer"));
3269 copy_recursive(
3270 fs.as_ref(),
3271 source,
3272 target,
3273 CopyOptions {
3274 overwrite: true,
3275 ..Default::default()
3276 },
3277 )
3278 .await
3279 .unwrap();
3280
3281 assert_eq!(
3282 fs.files(),
3283 vec![
3284 PathBuf::from(path!("/outer/inner1/a")),
3285 PathBuf::from(path!("/outer/inner1/b")),
3286 PathBuf::from(path!("/outer/inner2/c")),
3287 PathBuf::from(path!("/outer/inner1/outer/inner1/a")),
3288 PathBuf::from(path!("/outer/inner1/outer/inner1/b")),
3289 PathBuf::from(path!("/outer/inner1/outer/inner2/c")),
3290 PathBuf::from(path!("/outer/inner1/outer/inner1/outer/inner1/a")),
3291 ]
3292 );
3293 assert_eq!(
3294 fs.load(path!("/outer/inner1/outer/inner1/a").as_ref())
3295 .await
3296 .unwrap(),
3297 "A"
3298 );
3299 }
3300
3301 #[gpui::test]
3302 async fn test_copy_recursive_with_ignoring(executor: BackgroundExecutor) {
3303 let fs = FakeFs::new(executor.clone());
3304 fs.insert_tree(
3305 path!("/outer"),
3306 json!({
3307 "inner1": {
3308 "a": "A",
3309 "b": "B",
3310 "outer": {
3311 "inner1": {
3312 "a": "B"
3313 }
3314 }
3315 },
3316 "inner2": {
3317 "c": "C",
3318 }
3319 }),
3320 )
3321 .await;
3322
3323 assert_eq!(
3324 fs.files(),
3325 vec![
3326 PathBuf::from(path!("/outer/inner1/a")),
3327 PathBuf::from(path!("/outer/inner1/b")),
3328 PathBuf::from(path!("/outer/inner2/c")),
3329 PathBuf::from(path!("/outer/inner1/outer/inner1/a")),
3330 ]
3331 );
3332 assert_eq!(
3333 fs.load(path!("/outer/inner1/outer/inner1/a").as_ref())
3334 .await
3335 .unwrap(),
3336 "B",
3337 );
3338
3339 let source = Path::new(path!("/outer"));
3340 let target = Path::new(path!("/outer/inner1/outer"));
3341 copy_recursive(
3342 fs.as_ref(),
3343 source,
3344 target,
3345 CopyOptions {
3346 ignore_if_exists: true,
3347 ..Default::default()
3348 },
3349 )
3350 .await
3351 .unwrap();
3352
3353 assert_eq!(
3354 fs.files(),
3355 vec![
3356 PathBuf::from(path!("/outer/inner1/a")),
3357 PathBuf::from(path!("/outer/inner1/b")),
3358 PathBuf::from(path!("/outer/inner2/c")),
3359 PathBuf::from(path!("/outer/inner1/outer/inner1/a")),
3360 PathBuf::from(path!("/outer/inner1/outer/inner1/b")),
3361 PathBuf::from(path!("/outer/inner1/outer/inner2/c")),
3362 PathBuf::from(path!("/outer/inner1/outer/inner1/outer/inner1/a")),
3363 ]
3364 );
3365 assert_eq!(
3366 fs.load(path!("/outer/inner1/outer/inner1/a").as_ref())
3367 .await
3368 .unwrap(),
3369 "B"
3370 );
3371 }
3372
3373 #[gpui::test]
3374 async fn test_realfs_atomic_write(executor: BackgroundExecutor) {
3375 // With the file handle still open, the file should be replaced
3376 // https://github.com/zed-industries/zed/issues/30054
3377 let fs = RealFs {
3378 bundled_git_binary_path: None,
3379 executor,
3380 next_job_id: Arc::new(AtomicUsize::new(0)),
3381 job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
3382 };
3383 let temp_dir = TempDir::new().unwrap();
3384 let file_to_be_replaced = temp_dir.path().join("file.txt");
3385 let mut file = std::fs::File::create_new(&file_to_be_replaced).unwrap();
3386 file.write_all(b"Hello").unwrap();
3387 // drop(file); // We still hold the file handle here
3388 let content = std::fs::read_to_string(&file_to_be_replaced).unwrap();
3389 assert_eq!(content, "Hello");
3390 smol::block_on(fs.atomic_write(file_to_be_replaced.clone(), "World".into())).unwrap();
3391 let content = std::fs::read_to_string(&file_to_be_replaced).unwrap();
3392 assert_eq!(content, "World");
3393 }
3394
3395 #[gpui::test]
3396 async fn test_realfs_atomic_write_non_existing_file(executor: BackgroundExecutor) {
3397 let fs = RealFs {
3398 bundled_git_binary_path: None,
3399 executor,
3400 next_job_id: Arc::new(AtomicUsize::new(0)),
3401 job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
3402 };
3403 let temp_dir = TempDir::new().unwrap();
3404 let file_to_be_replaced = temp_dir.path().join("file.txt");
3405 smol::block_on(fs.atomic_write(file_to_be_replaced.clone(), "Hello".into())).unwrap();
3406 let content = std::fs::read_to_string(&file_to_be_replaced).unwrap();
3407 assert_eq!(content, "Hello");
3408 }
3409
3410 #[gpui::test]
3411 #[cfg(target_os = "windows")]
3412 async fn test_realfs_canonicalize(executor: BackgroundExecutor) {
3413 use util::paths::SanitizedPath;
3414
3415 let fs = RealFs {
3416 bundled_git_binary_path: None,
3417 executor,
3418 next_job_id: Arc::new(AtomicUsize::new(0)),
3419 job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
3420 };
3421 let temp_dir = TempDir::new().unwrap();
3422 let file = temp_dir.path().join("test (1).txt");
3423 let file = SanitizedPath::new(&file);
3424 std::fs::write(&file, "test").unwrap();
3425
3426 let canonicalized = fs.canonicalize(file.as_path()).await;
3427 assert!(canonicalized.is_ok());
3428 }
3429
3430 #[gpui::test]
3431 async fn test_rename(executor: BackgroundExecutor) {
3432 let fs = FakeFs::new(executor.clone());
3433 fs.insert_tree(
3434 path!("/root"),
3435 json!({
3436 "src": {
3437 "file_a.txt": "content a",
3438 "file_b.txt": "content b"
3439 }
3440 }),
3441 )
3442 .await;
3443
3444 fs.rename(
3445 Path::new(path!("/root/src/file_a.txt")),
3446 Path::new(path!("/root/src/new/renamed_a.txt")),
3447 RenameOptions {
3448 create_parents: true,
3449 ..Default::default()
3450 },
3451 )
3452 .await
3453 .unwrap();
3454
3455 // Assert that the `file_a.txt` file was being renamed and moved to a
3456 // different directory that did not exist before.
3457 assert_eq!(
3458 fs.files(),
3459 vec![
3460 PathBuf::from(path!("/root/src/file_b.txt")),
3461 PathBuf::from(path!("/root/src/new/renamed_a.txt")),
3462 ]
3463 );
3464
3465 let result = fs
3466 .rename(
3467 Path::new(path!("/root/src/file_b.txt")),
3468 Path::new(path!("/root/src/old/renamed_b.txt")),
3469 RenameOptions {
3470 create_parents: false,
3471 ..Default::default()
3472 },
3473 )
3474 .await;
3475
3476 // Assert that the `file_b.txt` file was not renamed nor moved, as
3477 // `create_parents` was set to `false`.
3478 // different directory that did not exist before.
3479 assert!(result.is_err());
3480 assert_eq!(
3481 fs.files(),
3482 vec![
3483 PathBuf::from(path!("/root/src/file_b.txt")),
3484 PathBuf::from(path!("/root/src/new/renamed_a.txt")),
3485 ]
3486 );
3487 }
3488}