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