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
use std::{
fs, io,
path::{Path, PathBuf},
};
/// File System abstraction used for `ResolverGeneric`
pub trait FileSystem: Send + Sync {
/// See [std::fs::read_to_string]
///
/// # Errors
///
/// * See [std::fs::read_to_string]
/// ## Warning
/// Use `&Path` instead of a generic `P: AsRef<Path>` here,
/// because object safety requirements, it is especially useful, when
/// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
/// napi env.
fn read_to_string(&self, path: &Path) -> io::Result<String>;
/// See [std::fs::metadata]
///
/// # Errors
/// See [std::fs::metadata]
/// ## Warning
/// Use `&Path` instead of a generic `P: AsRef<Path>` here,
/// because object safety requirements, it is especially useful, when
/// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
/// napi env.
fn metadata(&self, path: &Path) -> io::Result<FileMetadata>;
/// See [std::fs::symlink_metadata]
///
/// # Errors
///
/// See [std::fs::symlink_metadata]
/// ## Warning
/// Use `&Path` instead of a generic `P: AsRef<Path>` here,
/// because object safety requirements, it is especially useful, when
/// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
/// napi env.
fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata>;
/// See [std::fs::canonicalize]
///
/// # Errors
///
/// See [std::fs::read_link]
/// ## Warning
/// Use `&Path` instead of a generic `P: AsRef<Path>` here,
/// because object safety requirements, it is especially useful, when
/// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
/// napi env.
fn canonicalize(&self, path: &Path) -> io::Result<PathBuf>;
}
/// Metadata information about a file
#[derive(Debug, Clone, Copy)]
pub struct FileMetadata {
pub(crate) is_file: bool,
pub(crate) is_dir: bool,
pub(crate) is_symlink: bool,
}
impl FileMetadata {
pub fn new(is_file: bool, is_dir: bool, is_symlink: bool) -> Self {
Self { is_file, is_dir, is_symlink }
}
}
impl From<fs::Metadata> for FileMetadata {
fn from(metadata: fs::Metadata) -> Self {
Self::new(metadata.is_file(), metadata.is_dir(), metadata.is_symlink())
}
}
/// Operating System
#[derive(Default)]
pub struct FileSystemOs;
impl FileSystem for FileSystemOs {
fn read_to_string(&self, path: &Path) -> io::Result<String> {
fs::read_to_string(path)
}
fn metadata(&self, path: &Path) -> io::Result<FileMetadata> {
fs::metadata(path).map(FileMetadata::from)
}
fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata> {
fs::symlink_metadata(path).map(FileMetadata::from)
}
fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
#[cfg(not(target_os = "wasi"))]
{
dunce::canonicalize(path)
}
#[cfg(target_os = "wasi")]
{
let meta = fs::symlink_metadata(path)?;
if meta.file_type().is_symlink() {
let link = fs::read_link(path)?;
let mut path_buf = path.to_path_buf();
path_buf.pop();
for segment in link.iter() {
match segment.to_str() {
Some("..") => {
path_buf.pop();
}
Some(".") | None => {}
Some(seg) => {
// Need to trim the extra \0 introduces by rust std rust-lang/rust#123727
path_buf.push(seg.trim_end_matches('\0'));
}
}
}
Ok(path_buf)
} else {
Ok(path.to_path_buf())
}
}
}
}
#[test]
fn metadata() {
let meta = FileMetadata { is_file: true, is_dir: true, is_symlink: true };
assert_eq!(
format!("{meta:?}"),
"FileMetadata { is_file: true, is_dir: true, is_symlink: true }"
);
let _ = meta;
}