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