Skip to main content

strate_fs_abstraction/
capabilities.rs

1//! Filesystem capability flags.
2//!
3//! This module defines the capabilities that a filesystem implementation
4//! can advertise, allowing the VFS layer to adapt its behavior accordingly.
5
6/// Filesystem capabilities.
7///
8/// These flags describe what features a filesystem supports.
9/// The VFS layer uses this information to:
10/// - Reject unsupported operations early
11/// - Adapt behavior (e.g., case sensitivity)
12/// - Provide accurate information to applications
13#[derive(Debug, Clone)]
14pub struct FsCapabilities {
15    // === Access Mode ===
16    /// Filesystem is read-only (no write operations allowed)
17    pub read_only: bool,
18
19    // === Naming ===
20    /// Filesystem is case-sensitive (Linux default)
21    pub case_sensitive: bool,
22
23    /// Filesystem preserves case even if not case-sensitive
24    pub case_preserving: bool,
25
26    /// Maximum filename length in bytes
27    pub max_filename_len: usize,
28
29    /// Maximum full path length in bytes
30    pub max_path_len: usize,
31
32    // === Links ===
33    /// Supports symbolic links
34    pub supports_symlinks: bool,
35
36    /// Supports hard links
37    pub supports_hardlinks: bool,
38
39    // === File Features ===
40    /// Supports sparse files (holes in files)
41    pub supports_sparse_files: bool,
42
43    /// Maximum file size in bytes
44    pub max_file_size: u64,
45
46    // === Extended Attributes ===
47    /// Supports extended attributes (xattr)
48    pub supports_xattr: bool,
49
50    /// Supports POSIX ACLs
51    pub supports_acl: bool,
52
53    // === Timestamps ===
54    /// Supports sub-second timestamp precision
55    pub supports_nanoseconds: bool,
56
57    /// Supports creation/birth time (crtime)
58    pub supports_crtime: bool,
59}
60
61impl FsCapabilities {
62    /// Create capabilities for a typical read-only Linux filesystem.
63    pub const fn read_only_linux() -> Self {
64        Self {
65            read_only: true,
66            case_sensitive: true,
67            case_preserving: true,
68            max_filename_len: 255,
69            max_path_len: 4096,
70            supports_symlinks: true,
71            supports_hardlinks: true,
72            supports_sparse_files: true,
73            max_file_size: i64::MAX as u64,
74            supports_xattr: false,
75            supports_acl: false,
76            supports_nanoseconds: true,
77            supports_crtime: false,
78        }
79    }
80
81    /// Create capabilities for a writable Linux filesystem.
82    pub const fn writable_linux() -> Self {
83        Self {
84            read_only: false,
85            ..Self::read_only_linux()
86        }
87    }
88
89    /// Create default XFS capabilities.
90    pub const fn xfs() -> Self {
91        Self {
92            read_only: false,
93            case_sensitive: true,
94            case_preserving: true,
95            max_filename_len: 255,
96            max_path_len: 4096,
97            supports_symlinks: true,
98            supports_hardlinks: true,
99            supports_sparse_files: true,
100            max_file_size: 8 * 1024 * 1024 * 1024 * 1024 * 1024, // 8 EiB
101            supports_xattr: true,
102            supports_acl: true,
103            supports_nanoseconds: true,
104            supports_crtime: true, // XFS v5 has crtime
105        }
106    }
107
108    /// Create default ext4 capabilities.
109    pub const fn ext4() -> Self {
110        Self {
111            read_only: false,
112            case_sensitive: true,
113            case_preserving: true,
114            max_filename_len: 255,
115            max_path_len: 4096,
116            supports_symlinks: true,
117            supports_hardlinks: true,
118            supports_sparse_files: true,
119            max_file_size: 16 * 1024 * 1024 * 1024 * 1024, // 16 TiB
120            supports_xattr: true,
121            supports_acl: true,
122            supports_nanoseconds: true,
123            supports_crtime: true,
124        }
125    }
126
127    /// Create default btrfs capabilities.
128    pub const fn btrfs() -> Self {
129        Self {
130            read_only: false,
131            case_sensitive: true,
132            case_preserving: true,
133            max_filename_len: 255,
134            max_path_len: 4096,
135            supports_symlinks: true,
136            supports_hardlinks: true,
137            supports_sparse_files: true,
138            max_file_size: 16 * 1024 * 1024 * 1024 * 1024 * 1024, // 16 EiB
139            supports_xattr: true,
140            supports_acl: true,
141            supports_nanoseconds: true,
142            supports_crtime: true,
143        }
144    }
145
146    /// Check if write operations are allowed.
147    pub const fn can_write(&self) -> bool {
148        !self.read_only
149    }
150}
151
152impl Default for FsCapabilities {
153    /// Implements default.
154    fn default() -> Self {
155        Self::read_only_linux()
156    }
157}