3524 lines · 118.1 KB · Rust
RawDownload
pub mod fs_watcher; pub use fs_watcher::requires_poll_watcher; use parking_lot::Mutex;use slotmap::{KeyData, SlotMap};use std::ffi::OsString;use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};use std::time::Instant;use util::maybe; use anyhow::{Context as _, Result};use futures::stream::iter;use gpui::App;use gpui::BackgroundExecutor;use gpui::Global;use gpui::ReadGlobal as _;use gpui::SharedString;#[cfg(unix)]use std::ffi::CString;use util::command::new_command; #[cfg(unix)]use std::os::fd::{AsFd, AsRawFd};#[cfg(unix)]use std::os::unix::ffi::OsStrExt; #[cfg(unix)]use std::os::unix::fs::{FileTypeExt, MetadataExt}; #[cfg(any(target_os = "macos", target_os = "freebsd"))]use std::mem::MaybeUninit; use async_tar::Archive;use futures::{AsyncRead, Stream, StreamExt, future::BoxFuture};use git::repository::{GitRepository, RealGitRepository};use is_executable::IsExecutable;use rope::Rope;use serde::{Deserialize, Serialize};use smol::io::AsyncWriteExt;#[cfg(feature = "test-support")]use std::path::Component;use std::{    io::{self, Write},    path::{Path, PathBuf},    pin::Pin,    sync::Arc,    time::{Duration, SystemTime, UNIX_EPOCH},};use tempfile::TempDir;use text::LineEnding; #[cfg(feature = "test-support")]mod fake_git_repo;#[cfg(feature = "test-support")]use collections::{BTreeMap, btree_map};#[cfg(feature = "test-support")]use fake_git_repo::{FakeCommitDataEntry, FakeGitRepositoryState};#[cfg(feature = "test-support")]use git::{    repository::{CommitData, InitialGraphCommitData, RepoPath, Worktree, repo_path},    status::{FileStatus, StatusCode, TrackedStatus, UnmergedStatus},};#[cfg(feature = "test-support")]use path::normalize_path; #[cfg(feature = "test-support")]use smol::io::AsyncReadExt;#[cfg(feature = "test-support")]use std::ffi::OsStr; pub trait Watcher: Send + Sync {    fn add(&self, path: &Path) -> Result<()>;    fn remove(&self, path: &Path) -> Result<()>;} #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]pub enum PathEventKind {    Removed,    Created,    Changed,    Rescan,} #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]pub struct PathEvent {    pub path: PathBuf,    pub kind: Option<PathEventKind>,} impl From<PathEvent> for PathBuf {    fn from(event: PathEvent) -> Self {        event.path    }} #[async_trait::async_trait]pub trait Fs: Send + Sync {    async fn create_dir(&self, path: &Path) -> Result<()>;    async fn create_symlink(&self, path: &Path, target: PathBuf) -> Result<()>;    async fn create_file(&self, path: &Path, options: CreateOptions) -> Result<()>;    async fn create_file_with(        &self,        path: &Path,        content: Pin<&mut (dyn AsyncRead + Send)>,    ) -> Result<()>;    async fn extract_tar_file(        &self,        path: &Path,        content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,    ) -> Result<()>;    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()>;    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()>;     /// Removes a directory from the filesystem.    /// There is no expectation that the directory will be preserved in the    /// system trash.    async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()>;     /// Moves a file or directory to the system trash.    /// Returns a [`TrashedEntry`] that can be used to keep track of the    /// location of the trashed item in the system's trash.    async fn trash(&self, path: &Path, options: RemoveOptions) -> Result<TrashId>;     /// Removes a file from the filesystem.    /// There is no expectation that the file will be preserved in the system    /// trash.    async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;     async fn open_handle(&self, path: &Path) -> Result<Arc<dyn FileHandle>>;    async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read + Send + Sync>>;    async fn load(&self, path: &Path) -> Result<String> {        Ok(String::from_utf8(self.load_bytes(path).await?)?)    }    async fn load_bytes(&self, path: &Path) -> Result<Vec<u8>>;    async fn atomic_write(&self, path: PathBuf, text: String) -> Result<()>;    async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()>;    async fn write(&self, path: &Path, content: &[u8]) -> Result<()>;    async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;    async fn is_file(&self, path: &Path) -> bool;    async fn is_dir(&self, path: &Path) -> bool;    async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;    async fn read_link(&self, path: &Path) -> Result<PathBuf>;    async fn read_dir(        &self,        path: &Path,    ) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>>;     async fn watch(        &self,        path: &Path,        latency: Duration,    ) -> (        Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>,        Arc<dyn Watcher>,    );     fn open_repo(        &self,        abs_dot_git: &Path,        system_git_binary_path: Option<&Path>,    ) -> Result<Arc<dyn GitRepository>>;    async fn git_init(&self, abs_work_directory: &Path, fallback_branch_name: String)    -> Result<()>;    async fn git_clone(&self, abs_work_directory: &Path, repo_url: &str) -> Result<()>;    async fn git_config(&self, abs_work_directory: &Path, args: Vec<String>) -> Result<String>;    fn is_fake(&self) -> bool;    async fn is_case_sensitive(&self) -> bool;    fn subscribe_to_jobs(&self) -> JobEventReceiver;     /// Returns the original absolute path of the item identified by `trash_id`.    fn original_path_for_trash_id(&self, trash_id: TrashId) -> Option<PathBuf>;     /// Restores the item identified by `trash_id`, moving it from the system's    /// trash back to its original path.    async fn restore(&self, trash_id: TrashId) -> std::result::Result<PathBuf, TrashRestoreError>;     #[cfg(feature = "test-support")]    fn as_fake(&self) -> Arc<FakeFs> {        panic!("called as_fake on a real fs");    }} // We use our own type rather than `trash::TrashItem` directly to avoid carrying// over fields we don't need (e.g. `time_deleted`) and to insulate callers and// tests from changes to that crate's API surface./// Represents a file or directory that has been moved to the system trash,/// retaining enough information to restore it to its original location.#[derive(Clone, PartialEq, Debug)]struct TrashedEntry {    /// Platform-specific identifier for the file/directory in the trash.    ///    /// * Freedesktop – Path to the `.trashinfo` file.    /// * macOS & Windows – Full path to the file/directory in the system's    /// trash.    pub id: OsString,    /// Name of the file/directory at the time of trashing, including extension.    pub name: OsString,    /// Absolute path to the parent directory at the time of trashing.    pub original_parent: PathBuf,} impl From<trash::TrashItem> for TrashedEntry {    fn from(item: trash::TrashItem) -> Self {        Self {            id: item.id,            name: item.name,            original_parent: item.original_parent,        }    }} impl TrashedEntry {    fn into_trash_item(self) -> trash::TrashItem {        trash::TrashItem {            id: self.id,            name: self.name,            original_parent: self.original_parent,            // `TrashedEntry` doesn't preserve `time_deleted` as we don't            // currently need it for restore, so we default it to 0 here.            time_deleted: 0,        }    }} #[derive(Debug, thiserror::Error)]pub enum TrashRestoreError {    #[error("The specified `path` ({}) was not found in the system's trash.", path.display())]    NotFound { path: PathBuf },    #[error("File or directory ({}) already exists at the restore destination.", path.display())]    Collision { path: PathBuf },    // This should never occur, the only way to get a TrashId is to undo    // consumes the Change::Trashed. We worry about remoting duplicate messages    // we do not want to crash the app then which is why this error is there.    #[error("The item was already restored")]    AlreadyRestored,    #[error("Unknown error ({description})")]    Unknown { description: String },} impl From<trash::Error> for TrashRestoreError {    fn from(err: trash::Error) -> Self {        match err {            trash::Error::RestoreCollision { path, .. } => Self::Collision { path },            trash::Error::Unknown { description } => Self::Unknown { description },            other => Self::Unknown {                description: other.to_string(),            },        }    }} struct GlobalFs(Arc<dyn Fs>); impl Global for GlobalFs {} impl dyn Fs {    /// Returns the global [`Fs`].    pub fn global(cx: &App) -> Arc<Self> {        GlobalFs::global(cx).0.clone()    }     /// Sets the global [`Fs`].    pub fn set_global(fs: Arc<Self>, cx: &mut App) {        cx.set_global(GlobalFs(fs));    }} #[derive(Copy, Clone, Default)]pub struct CreateOptions {    pub overwrite: bool,    pub ignore_if_exists: bool,} #[derive(Copy, Clone, Default)]pub struct CopyOptions {    pub overwrite: bool,    pub ignore_if_exists: bool,} #[derive(Copy, Clone, Default)]pub struct RenameOptions {    pub overwrite: bool,    pub ignore_if_exists: bool,    /// Whether to create parent directories if they do not exist.    pub create_parents: bool,} #[derive(Copy, Clone, Default)]pub struct RemoveOptions {    pub recursive: bool,    pub ignore_if_not_exists: bool,} #[derive(Copy, Clone, Debug)]pub struct Metadata {    pub inode: u64,    pub mtime: MTime,    pub is_symlink: bool,    pub is_dir: bool,    pub len: u64,    pub is_fifo: bool,    pub is_executable: bool,    pub is_writable: bool,} /// Filesystem modification time. The purpose of this newtype is to discourage use of operations/// that do not make sense for mtimes. In particular, it is not always valid to compare mtimes using/// `<` or `>`, as there are many things that can cause the mtime of a file to be earlier than it/// was. See ["mtime comparison considered harmful" - apenwarr](https://apenwarr.ca/log/20181113).////// Do not derive Ord, PartialOrd, or arithmetic operation traits.#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]#[serde(transparent)]pub struct MTime(SystemTime); pub type JobId = usize; #[derive(Clone, Debug)]pub struct JobInfo {    pub start: Instant,    pub message: SharedString,    pub id: JobId,} #[derive(Debug, Clone)]pub enum JobEvent {    Started { info: JobInfo },    Completed { id: JobId },} pub type JobEventSender = futures::channel::mpsc::UnboundedSender<JobEvent>;pub type JobEventReceiver = futures::channel::mpsc::UnboundedReceiver<JobEvent>; struct JobTracker {    id: JobId,    subscribers: Arc<Mutex<Vec<JobEventSender>>>,} impl JobTracker {    fn new(info: JobInfo, subscribers: Arc<Mutex<Vec<JobEventSender>>>) -> Self {        let id = info.id;        {            let mut subs = subscribers.lock();            subs.retain(|sender| {                sender                    .unbounded_send(JobEvent::Started { info: info.clone() })                    .is_ok()            });        }        Self { id, subscribers }    }} impl Drop for JobTracker {    fn drop(&mut self) {        let mut subs = self.subscribers.lock();        subs.retain(|sender| {            sender                .unbounded_send(JobEvent::Completed { id: self.id })                .is_ok()        });    }} impl MTime {    /// Conversion intended for persistence and testing.    pub fn from_seconds_and_nanos(secs: u64, nanos: u32) -> Self {        MTime(UNIX_EPOCH + Duration::new(secs, nanos))    }     /// Conversion intended for persistence.    pub fn to_seconds_and_nanos_for_persistence(self) -> Option<(u64, u32)> {        self.0            .duration_since(UNIX_EPOCH)            .ok()            .map(|duration| (duration.as_secs(), duration.subsec_nanos()))    }     /// Returns the value wrapped by this `MTime`, for presentation to the user. The name including    /// "_for_user" is to discourage misuse - this method should not be used when making decisions    /// about file dirtiness.    pub fn timestamp_for_user(self) -> SystemTime {        self.0    }     /// Temporary method to split out the behavior changes from introduction of this newtype.    pub fn bad_is_greater_than(self, other: MTime) -> bool {        self.0 > other.0    }} impl From<proto::Timestamp> for MTime {    fn from(timestamp: proto::Timestamp) -> Self {        MTime(timestamp.into())    }} impl From<MTime> for proto::Timestamp {    fn from(mtime: MTime) -> Self {        mtime.0.into()    }} slotmap::new_key_type! { pub struct TrashId; } impl TrashId {    pub fn from_proto(value: u64) -> Self {        KeyData::from_ffi(value).into()    }     pub fn to_proto(self) -> u64 {        self.0.as_ffi()    }} pub struct RealFs {    bundled_git_binary_path: Option<PathBuf>,    executor: BackgroundExecutor,    next_job_id: Arc<AtomicUsize>,    job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,    trash: Arc<Mutex<SlotMap<TrashId, TrashedEntry>>>,    is_case_sensitive: AtomicU8,} pub trait FileHandle: Send + Sync + std::fmt::Debug {    fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf>;} impl FileHandle for std::fs::File {    #[cfg(target_os = "macos")]    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {        use std::{            ffi::{CStr, OsStr},            os::unix::ffi::OsStrExt,        };         let fd = self.as_fd();        let mut path_buf = MaybeUninit::<[u8; libc::PATH_MAX as usize]>::uninit();         let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETPATH, path_buf.as_mut_ptr()) };        anyhow::ensure!(result != -1, "fcntl returned -1");         // SAFETY: `fcntl` will initialize the path buffer.        let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr().cast()) };        anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");        let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));        Ok(path)    }     #[cfg(target_os = "linux")]    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {        let fd = self.as_fd();        let fd_path = format!("/proc/self/fd/{}", fd.as_raw_fd());        let new_path = std::fs::read_link(fd_path)?;        if new_path            .file_name()            .is_some_and(|f| f.to_string_lossy().ends_with(" (deleted)"))        {            anyhow::bail!("file was deleted")        };         Ok(new_path)    }     #[cfg(target_os = "freebsd")]    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {        use std::{            ffi::{CStr, OsStr},            os::unix::ffi::OsStrExt,        };         let fd = self.as_fd();        let mut kif = MaybeUninit::<libc::kinfo_file>::uninit();        kif.kf_structsize = libc::KINFO_FILE_SIZE;         let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_KINFO, kif.as_mut_ptr()) };        anyhow::ensure!(result != -1, "fcntl returned -1");         // SAFETY: `fcntl` will initialize the kif.        let c_str = unsafe { CStr::from_ptr(kif.assume_init().kf_path.as_ptr()) };        anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");        let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));        Ok(path)    }     #[cfg(target_os = "windows")]    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {        use std::ffi::OsString;        use std::os::windows::ffi::OsStringExt;        use std::os::windows::io::AsRawHandle;         use windows::Win32::Foundation::HANDLE;        use windows::Win32::Storage::FileSystem::{            FILE_NAME_NORMALIZED, GetFinalPathNameByHandleW,        };         let handle = HANDLE(self.as_raw_handle() as _);         // Query required buffer size (in wide chars)

Lines 1–500 of 3524.