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