Skip to main content

grit_lib/worktree/
io_error.rs

1//! Map filesystem failures on repository-relative paths to typed [`Error::WorktreePathIo`].
2//!
3//! Callers pass the index-relative path and what they were doing so Windows sharing
4//! violations and Unix `EBUSY` produce a stable, actionable message without copying Git.
5
6use std::io;
7
8use crate::error::{Error, Result};
9
10/// Worktree filesystem operation that failed on a single path.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum WorktreePathOperation {
14    /// Reading file bytes or a symlink target.
15    Read,
16    /// Replacing an existing file (checkout, hard reset, apply).
17    Replace,
18    /// Removing a file or symlink before rewrite.
19    Remove,
20    /// Creating a new path (file or symlink).
21    Create,
22    /// `stat` / metadata probe without opening for read.
23    Metadata,
24}
25
26impl WorktreePathOperation {
27    fn verb(self) -> &'static str {
28        match self {
29            Self::Read => "read",
30            Self::Replace => "replace",
31            Self::Remove => "remove",
32            Self::Create => "create",
33            Self::Metadata => "inspect",
34        }
35    }
36}
37
38/// Whether `err` usually means another process holds the file open.
39#[must_use]
40pub fn is_file_in_use(err: &io::Error) -> bool {
41    if err.kind() == io::ErrorKind::WouldBlock {
42        return true;
43    }
44    if let Some(raw) = err.raw_os_error() {
45        #[cfg(windows)]
46        if raw == 32 {
47            return true;
48        }
49        if raw == libc::EBUSY {
50            return true;
51        }
52        #[cfg(windows)]
53        if raw == 33 {
54            // ERROR_LOCK_VIOLATION
55            return true;
56        }
57    }
58    false
59}
60
61/// Human-readable summary for [`Error::WorktreePathIo`].
62#[must_use]
63pub fn format_worktree_path_io_message(
64    path: &str,
65    operation: WorktreePathOperation,
66    source: &io::Error,
67) -> String {
68    if is_file_in_use(source) {
69        format!(
70            "cannot {} '{path}': file is in use by another process (close it and retry)",
71            operation.verb()
72        )
73    } else {
74        format!("cannot {} '{path}': {source}", operation.verb())
75    }
76}
77
78/// Wrap a raw I/O failure with path and operation context.
79#[must_use]
80pub fn map_worktree_io(
81    path: impl Into<String>,
82    operation: WorktreePathOperation,
83    source: io::Error,
84) -> Error {
85    crate::error::PathIoError::worktree(path, operation, source).into()
86}
87
88/// Attach path context to [`std::io::Result`].
89pub fn io_with_path<T>(
90    path: impl Into<String>,
91    operation: WorktreePathOperation,
92    result: io::Result<T>,
93) -> Result<T> {
94    result.map_err(|source| map_worktree_io(path, operation, source))
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    #[cfg(windows)]
103    fn windows_sharing_violation_maps_to_in_use_message() {
104        let err = io::Error::from_raw_os_error(32);
105        let msg = format_worktree_path_io_message("b.txt", WorktreePathOperation::Replace, &err);
106        assert!(msg.contains("b.txt"));
107        assert!(msg.contains("in use by another process"));
108        assert!(!msg.contains("os error 32"));
109    }
110
111    #[test]
112    fn ebusy_maps_to_in_use_message() {
113        let err = io::Error::from_raw_os_error(libc::EBUSY as i32);
114        let msg = format_worktree_path_io_message("a.txt", WorktreePathOperation::Read, &err);
115        assert!(msg.contains("in use by another process"));
116    }
117
118    #[test]
119    fn other_io_keeps_source_kind() {
120        let err = io::Error::new(io::ErrorKind::NotFound, "gone");
121        let msg = format_worktree_path_io_message("x", WorktreePathOperation::Remove, &err);
122        assert!(msg.contains("cannot remove 'x'"));
123        assert!(msg.contains("gone"));
124    }
125
126    #[test]
127    fn error_reports_file_in_use_only_for_worktree_lock_failures() {
128        let busy = map_worktree_io(
129            "a.txt",
130            WorktreePathOperation::Read,
131            io::Error::from_raw_os_error(libc::EBUSY),
132        );
133        assert!(busy.is_worktree_file_in_use());
134        let not_dir = map_worktree_io(
135            "d/x",
136            WorktreePathOperation::Metadata,
137            io::Error::from(io::ErrorKind::NotADirectory),
138        );
139        assert!(!not_dir.is_worktree_file_in_use());
140        assert!(!Error::Io(io::Error::from_raw_os_error(libc::EBUSY)).is_worktree_file_in_use());
141    }
142}