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