1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use serde::{Deserialize, Serialize};
use std::ffi::OsString;
use std::fs::read_link;
use std::fs::{FileType, Metadata};
use std::os::linux::fs::MetadataExt;
use std::os::unix::ffi::OsStringExt;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilesystemEntry {
kind: FilesystemKind,
path: Vec<u8>,
len: u64,
mode: u32,
mtime: i64,
mtime_ns: i64,
atime: i64,
atime_ns: i64,
symlink_target: Option<PathBuf>,
}
#[allow(clippy::len_without_is_empty)]
impl FilesystemEntry {
pub fn from_metadata(path: &Path, meta: &Metadata) -> anyhow::Result<Self> {
let kind = FilesystemKind::from_file_type(meta.file_type());
Ok(Self {
path: path.to_path_buf().into_os_string().into_vec(),
kind: FilesystemKind::from_file_type(meta.file_type()),
len: meta.len(),
mode: meta.st_mode(),
mtime: meta.st_mtime(),
mtime_ns: meta.st_mtime_nsec(),
atime: meta.st_atime(),
atime_ns: meta.st_atime_nsec(),
symlink_target: if kind == FilesystemKind::Symlink {
Some(read_link(path)?)
} else {
None
},
})
}
pub fn kind(&self) -> FilesystemKind {
self.kind
}
pub fn pathbuf(&self) -> PathBuf {
let path = self.path.clone();
PathBuf::from(OsString::from_vec(path))
}
pub fn len(&self) -> u64 {
self.len
}
pub fn mode(&self) -> u32 {
self.mode
}
pub fn atime(&self) -> i64 {
self.atime
}
pub fn atime_ns(&self) -> i64 {
self.atime_ns
}
pub fn mtime(&self) -> i64 {
self.mtime
}
pub fn mtime_ns(&self) -> i64 {
self.mtime_ns
}
pub fn is_dir(&self) -> bool {
self.kind() == FilesystemKind::Directory
}
pub fn symlink_target(&self) -> Option<PathBuf> {
self.symlink_target.clone()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub enum FilesystemKind {
Regular,
Directory,
Symlink,
}
impl FilesystemKind {
pub fn from_file_type(file_type: FileType) -> Self {
if file_type.is_file() {
FilesystemKind::Regular
} else if file_type.is_dir() {
FilesystemKind::Directory
} else if file_type.is_symlink() {
FilesystemKind::Symlink
} else {
panic!("unknown file type {:?}", file_type);
}
}
pub fn as_code(&self) -> u8 {
match self {
FilesystemKind::Regular => 0,
FilesystemKind::Directory => 1,
FilesystemKind::Symlink => 2,
}
}
pub fn from_code(code: u8) -> anyhow::Result<Self> {
match code {
0 => Ok(FilesystemKind::Regular),
1 => Ok(FilesystemKind::Directory),
2 => Ok(FilesystemKind::Symlink),
_ => Err(Error::UnknownFileKindCode(code).into()),
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("unknown file kind code {0}")]
UnknownFileKindCode(u8),
}
#[cfg(test)]
mod test {
use super::FilesystemKind;
#[test]
fn file_kind_regular_round_trips() {
one_file_kind_round_trip(FilesystemKind::Regular);
one_file_kind_round_trip(FilesystemKind::Directory);
}
fn one_file_kind_round_trip(kind: FilesystemKind) {
assert_eq!(kind, FilesystemKind::from_code(kind.as_code()).unwrap());
}
}