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