Skip to main content

kaish_types/
dir_entry.rs

1//! Directory entry types for VFS and backend operations.
2
3use std::path::PathBuf;
4use std::time::SystemTime;
5
6/// Kind of directory entry.
7#[non_exhaustive]
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum DirEntryKind {
10    File,
11    Directory,
12    Symlink,
13}
14
15/// A directory entry — the unified file metadata type.
16///
17/// Used everywhere: VFS `list()`, `stat()`, `lstat()`, and `KernelBackend` methods.
18#[derive(Debug, Clone)]
19pub struct DirEntry {
20    /// Name of the entry (not full path).
21    pub name: String,
22    /// Kind of entry.
23    pub kind: DirEntryKind,
24    /// Size in bytes (0 for directories).
25    pub size: u64,
26    /// Last modification time, if available.
27    pub modified: Option<SystemTime>,
28    /// Unix permissions (e.g., 0o644), if available.
29    pub permissions: Option<u32>,
30    /// For symlinks, the target path.
31    pub symlink_target: Option<PathBuf>,
32}
33
34impl DirEntry {
35    /// Create a new directory entry.
36    pub fn directory(name: impl Into<String>) -> Self {
37        Self {
38            name: name.into(),
39            kind: DirEntryKind::Directory,
40            size: 0,
41            modified: None,
42            permissions: None,
43            symlink_target: None,
44        }
45    }
46
47    /// Create a new file entry.
48    pub fn file(name: impl Into<String>, size: u64) -> Self {
49        Self {
50            name: name.into(),
51            kind: DirEntryKind::File,
52            size,
53            modified: None,
54            permissions: None,
55            symlink_target: None,
56        }
57    }
58
59    /// Create a new symlink entry.
60    pub fn symlink(name: impl Into<String>, target: impl Into<PathBuf>) -> Self {
61        Self {
62            name: name.into(),
63            kind: DirEntryKind::Symlink,
64            size: 0,
65            modified: None,
66            permissions: None,
67            symlink_target: Some(target.into()),
68        }
69    }
70
71    /// Returns true if this entry is a directory.
72    pub fn is_dir(&self) -> bool {
73        self.kind == DirEntryKind::Directory
74    }
75
76    /// Returns true if this entry is a regular file.
77    pub fn is_file(&self) -> bool {
78        self.kind == DirEntryKind::File
79    }
80
81    /// Returns true if this entry is a symbolic link.
82    pub fn is_symlink(&self) -> bool {
83        self.kind == DirEntryKind::Symlink
84    }
85}