Skip to content

Commit 225c3de

Browse files
authored
refactor: update some fs_util functions to use sys_traits (denoland#27515)
This is in preparation for extracting out these functions from the CLI crate. A side benefit is these functions will now work in Wasm.
1 parent 9215aa6 commit 225c3de

File tree

7 files changed

+205
-148
lines changed

7 files changed

+205
-148
lines changed

Cargo.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ slab = "0.4"
193193
smallvec = "1.8"
194194
socket2 = { version = "0.5.3", features = ["all"] }
195195
spki = "0.7.2"
196-
sys_traits = "=0.1.4"
196+
sys_traits = "=0.1.6"
197197
tar = "=0.4.40"
198198
tempfile = "3.4.0"
199199
termcolor = "1.1.3"
@@ -240,7 +240,7 @@ syn = { version = "2", features = ["full", "extra-traits"] }
240240
nix = "=0.27.1"
241241

242242
# windows deps
243-
junction = "=0.2.0"
243+
junction = "=1.2.0"
244244
winapi = "=0.3.9"
245245
windows-sys = { version = "0.59.0", features = ["Win32_Foundation", "Win32_Media", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_WindowsProgramming", "Wdk", "Wdk_System", "Wdk_System_SystemInformation", "Win32_Security", "Win32_System_Pipes", "Wdk_Storage_FileSystem", "Win32_System_Registry", "Win32_System_Kernel", "Win32_System_Threading", "Win32_UI", "Win32_UI_Shell"] }
246246
winres = "=0.1.12"

cli/npm/managed/resolvers/local.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,11 @@ async fn sync_resolution_with_fs(
433433
deno_core::unsync::spawn_blocking({
434434
let package_path = package_path.clone();
435435
move || {
436-
clone_dir_recursive(&cache_folder, &package_path)?;
436+
clone_dir_recursive(
437+
&crate::sys::CliSys::default(),
438+
&cache_folder,
439+
&package_path,
440+
)?;
437441
// write out a file that indicates this folder has been initialized
438442
fs::write(initialized_file, tags)?;
439443

@@ -490,7 +494,11 @@ async fn sync_resolution_with_fs(
490494
&package.id.nv.name,
491495
);
492496

493-
clone_dir_recursive(&source_path, &package_path)?;
497+
clone_dir_recursive(
498+
&crate::sys::CliSys::default(),
499+
&source_path,
500+
&package_path,
501+
)?;
494502
// write out a file that indicates this folder has been initialized
495503
fs::write(initialized_file, "")?;
496504
}
@@ -1057,7 +1065,8 @@ fn symlink_package_dir(
10571065
}
10581066
#[cfg(not(windows))]
10591067
{
1060-
symlink_dir(&old_path_relative, new_path).map_err(Into::into)
1068+
symlink_dir(&crate::sys::CliSys::default(), &old_path_relative, new_path)
1069+
.map_err(Into::into)
10611070
}
10621071
}
10631072

@@ -1079,7 +1088,8 @@ fn junction_or_symlink_dir(
10791088
.context("Failed creating junction in node_modules folder");
10801089
}
10811090

1082-
match symlink_dir(old_path_relative, new_path) {
1091+
match symlink_dir(&crate::sys::CliSys::default(), old_path_relative, new_path)
1092+
{
10831093
Ok(()) => Ok(()),
10841094
Err(symlink_err)
10851095
if symlink_err.kind() == std::io::ErrorKind::PermissionDenied =>

cli/standalone/file_system.rs

+64-16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use sys_traits::boxed::BoxedFsDirEntry;
2323
use sys_traits::boxed::BoxedFsMetadataValue;
2424
use sys_traits::boxed::FsMetadataBoxed;
2525
use sys_traits::boxed::FsReadDirBoxed;
26+
use sys_traits::FsCopy;
2627
use sys_traits::FsMetadata;
2728

2829
use super::virtual_fs::FileBackedVfs;
@@ -47,24 +48,32 @@ impl DenoCompileFileSystem {
4748
}
4849
}
4950

50-
fn copy_to_real_path(&self, oldpath: &Path, newpath: &Path) -> FsResult<()> {
51+
fn copy_to_real_path(
52+
&self,
53+
oldpath: &Path,
54+
newpath: &Path,
55+
) -> std::io::Result<u64> {
5156
let old_file = self.0.file_entry(oldpath)?;
5257
let old_file_bytes =
5358
self.0.read_file_all(old_file, VfsFileSubDataKind::Raw)?;
54-
RealFs.write_file_sync(
55-
newpath,
56-
OpenOptions {
57-
read: false,
58-
write: true,
59-
create: true,
60-
truncate: true,
61-
append: false,
62-
create_new: false,
63-
mode: None,
64-
},
65-
None,
66-
&old_file_bytes,
67-
)
59+
let len = old_file_bytes.len() as u64;
60+
RealFs
61+
.write_file_sync(
62+
newpath,
63+
OpenOptions {
64+
read: false,
65+
write: true,
66+
create: true,
67+
truncate: true,
68+
append: false,
69+
create_new: false,
70+
mode: None,
71+
},
72+
None,
73+
&old_file_bytes,
74+
)
75+
.map_err(|err| err.into_io_error())?;
76+
Ok(len)
6877
}
6978
}
7079

@@ -191,7 +200,10 @@ impl FileSystem for DenoCompileFileSystem {
191200
fn copy_file_sync(&self, oldpath: &Path, newpath: &Path) -> FsResult<()> {
192201
self.error_if_in_vfs(newpath)?;
193202
if self.0.is_path_within(oldpath) {
194-
self.copy_to_real_path(oldpath, newpath)
203+
self
204+
.copy_to_real_path(oldpath, newpath)
205+
.map(|_| ())
206+
.map_err(FsError::Io)
195207
} else {
196208
RealFs.copy_file_sync(oldpath, newpath)
197209
}
@@ -206,6 +218,8 @@ impl FileSystem for DenoCompileFileSystem {
206218
let fs = self.clone();
207219
tokio::task::spawn_blocking(move || {
208220
fs.copy_to_real_path(&oldpath, &newpath)
221+
.map(|_| ())
222+
.map_err(FsError::Io)
209223
})
210224
.await?
211225
} else {
@@ -593,6 +607,32 @@ impl sys_traits::BaseFsMetadata for DenoCompileFileSystem {
593607
}
594608
}
595609

610+
impl sys_traits::BaseFsCopy for DenoCompileFileSystem {
611+
#[inline]
612+
fn base_fs_copy(&self, from: &Path, to: &Path) -> std::io::Result<u64> {
613+
self
614+
.error_if_in_vfs(to)
615+
.map_err(|err| err.into_io_error())?;
616+
if self.0.is_path_within(from) {
617+
self.copy_to_real_path(from, to)
618+
} else {
619+
#[allow(clippy::disallowed_types)] // ok because we're implementing the fs
620+
sys_traits::impls::RealSys.fs_copy(from, to)
621+
}
622+
}
623+
}
624+
625+
impl sys_traits::BaseFsCloneFile for DenoCompileFileSystem {
626+
fn base_fs_clone_file(
627+
&self,
628+
_from: &Path,
629+
_to: &Path,
630+
) -> std::io::Result<()> {
631+
// will cause a fallback in the code that uses this
632+
Err(not_supported("cloning files"))
633+
}
634+
}
635+
596636
impl sys_traits::BaseFsCreateDir for DenoCompileFileSystem {
597637
#[inline]
598638
fn base_fs_create_dir(
@@ -794,6 +834,14 @@ impl sys_traits::BaseFsOpen for DenoCompileFileSystem {
794834
}
795835
}
796836

837+
impl sys_traits::BaseFsSymlinkDir for DenoCompileFileSystem {
838+
fn base_fs_symlink_dir(&self, src: &Path, dst: &Path) -> std::io::Result<()> {
839+
self
840+
.symlink_sync(src, dst, Some(FsFileType::Directory))
841+
.map_err(|err| err.into_io_error())
842+
}
843+
}
844+
797845
impl sys_traits::SystemRandom for DenoCompileFileSystem {
798846
#[inline]
799847
fn sys_random(&self, buf: &mut [u8]) -> std::io::Result<()> {

cli/standalone/virtual_fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ mod test {
16851685
temp_dir.write("src/a.txt", "data");
16861686
temp_dir.write("src/b.txt", "data");
16871687
util::fs::symlink_dir(
1688+
&crate::sys::CliSys::default(),
16881689
temp_dir_path.join("src/nested/sub_dir").as_path(),
16891690
temp_dir_path.join("src/sub_dir_link").as_path(),
16901691
)

cli/sys.rs

+41-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// denort or the deno binary. We should extract out denort to a separate binary.
88

99
use std::borrow::Cow;
10+
use std::path::Path;
11+
use std::path::PathBuf;
1012

1113
use sys_traits::boxed::BoxedFsDirEntry;
1214
use sys_traits::boxed::BoxedFsFile;
@@ -35,12 +37,35 @@ impl Default for CliSys {
3537

3638
impl deno_runtime::deno_node::ExtNodeSys for CliSys {}
3739

40+
impl sys_traits::BaseFsCloneFile for CliSys {
41+
fn base_fs_clone_file(&self, src: &Path, dst: &Path) -> std::io::Result<()> {
42+
match self {
43+
Self::Real(sys) => sys.base_fs_clone_file(src, dst),
44+
Self::DenoCompile(sys) => sys.base_fs_clone_file(src, dst),
45+
}
46+
}
47+
}
48+
49+
impl sys_traits::BaseFsSymlinkDir for CliSys {
50+
fn base_fs_symlink_dir(&self, src: &Path, dst: &Path) -> std::io::Result<()> {
51+
match self {
52+
Self::Real(sys) => sys.base_fs_symlink_dir(src, dst),
53+
Self::DenoCompile(sys) => sys.base_fs_symlink_dir(src, dst),
54+
}
55+
}
56+
}
57+
58+
impl sys_traits::BaseFsCopy for CliSys {
59+
fn base_fs_copy(&self, src: &Path, dst: &Path) -> std::io::Result<u64> {
60+
match self {
61+
Self::Real(sys) => sys.base_fs_copy(src, dst),
62+
Self::DenoCompile(sys) => sys.base_fs_copy(src, dst),
63+
}
64+
}
65+
}
66+
3867
impl sys_traits::BaseFsHardLink for CliSys {
39-
fn base_fs_hard_link(
40-
&self,
41-
src: &std::path::Path,
42-
dst: &std::path::Path,
43-
) -> std::io::Result<()> {
68+
fn base_fs_hard_link(&self, src: &Path, dst: &Path) -> std::io::Result<()> {
4469
match self {
4570
Self::Real(sys) => sys.base_fs_hard_link(src, dst),
4671
Self::DenoCompile(sys) => sys.base_fs_hard_link(src, dst),
@@ -49,10 +74,7 @@ impl sys_traits::BaseFsHardLink for CliSys {
4974
}
5075

5176
impl sys_traits::BaseFsRead for CliSys {
52-
fn base_fs_read(
53-
&self,
54-
p: &std::path::Path,
55-
) -> std::io::Result<Cow<'static, [u8]>> {
77+
fn base_fs_read(&self, p: &Path) -> std::io::Result<Cow<'static, [u8]>> {
5678
match self {
5779
Self::Real(sys) => sys.base_fs_read(p),
5880
Self::DenoCompile(sys) => sys.base_fs_read(p),
@@ -65,7 +87,7 @@ impl sys_traits::BaseFsReadDir for CliSys {
6587

6688
fn base_fs_read_dir(
6789
&self,
68-
p: &std::path::Path,
90+
p: &Path,
6991
) -> std::io::Result<
7092
Box<dyn Iterator<Item = std::io::Result<Self::ReadDirEntry>> + '_>,
7193
> {
@@ -77,10 +99,7 @@ impl sys_traits::BaseFsReadDir for CliSys {
7799
}
78100

79101
impl sys_traits::BaseFsCanonicalize for CliSys {
80-
fn base_fs_canonicalize(
81-
&self,
82-
p: &std::path::Path,
83-
) -> std::io::Result<std::path::PathBuf> {
102+
fn base_fs_canonicalize(&self, p: &Path) -> std::io::Result<PathBuf> {
84103
match self {
85104
Self::Real(sys) => sys.base_fs_canonicalize(p),
86105
Self::DenoCompile(sys) => sys.base_fs_canonicalize(p),
@@ -91,10 +110,7 @@ impl sys_traits::BaseFsCanonicalize for CliSys {
91110
impl sys_traits::BaseFsMetadata for CliSys {
92111
type Metadata = BoxedFsMetadataValue;
93112

94-
fn base_fs_metadata(
95-
&self,
96-
path: &std::path::Path,
97-
) -> std::io::Result<Self::Metadata> {
113+
fn base_fs_metadata(&self, path: &Path) -> std::io::Result<Self::Metadata> {
98114
match self {
99115
Self::Real(sys) => sys.fs_metadata_boxed(path),
100116
Self::DenoCompile(sys) => sys.fs_metadata_boxed(path),
@@ -103,7 +119,7 @@ impl sys_traits::BaseFsMetadata for CliSys {
103119

104120
fn base_fs_symlink_metadata(
105121
&self,
106-
path: &std::path::Path,
122+
path: &Path,
107123
) -> std::io::Result<Self::Metadata> {
108124
match self {
109125
Self::Real(sys) => sys.fs_symlink_metadata_boxed(path),
@@ -115,7 +131,7 @@ impl sys_traits::BaseFsMetadata for CliSys {
115131
impl sys_traits::BaseFsCreateDir for CliSys {
116132
fn base_fs_create_dir(
117133
&self,
118-
p: &std::path::Path,
134+
p: &Path,
119135
options: &CreateDirOptions,
120136
) -> std::io::Result<()> {
121137
match self {
@@ -130,7 +146,7 @@ impl sys_traits::BaseFsOpen for CliSys {
130146

131147
fn base_fs_open(
132148
&self,
133-
path: &std::path::Path,
149+
path: &Path,
134150
options: &sys_traits::OpenOptions,
135151
) -> std::io::Result<Self::File> {
136152
match self {
@@ -141,7 +157,7 @@ impl sys_traits::BaseFsOpen for CliSys {
141157
}
142158

143159
impl sys_traits::BaseFsRemoveFile for CliSys {
144-
fn base_fs_remove_file(&self, p: &std::path::Path) -> std::io::Result<()> {
160+
fn base_fs_remove_file(&self, p: &Path) -> std::io::Result<()> {
145161
match self {
146162
Self::Real(sys) => sys.base_fs_remove_file(p),
147163
Self::DenoCompile(sys) => sys.base_fs_remove_file(p),
@@ -150,11 +166,7 @@ impl sys_traits::BaseFsRemoveFile for CliSys {
150166
}
151167

152168
impl sys_traits::BaseFsRename for CliSys {
153-
fn base_fs_rename(
154-
&self,
155-
old: &std::path::Path,
156-
new: &std::path::Path,
157-
) -> std::io::Result<()> {
169+
fn base_fs_rename(&self, old: &Path, new: &Path) -> std::io::Result<()> {
158170
match self {
159171
Self::Real(sys) => sys.base_fs_rename(old, new),
160172
Self::DenoCompile(sys) => sys.base_fs_rename(old, new),
@@ -190,7 +202,7 @@ impl sys_traits::ThreadSleep for CliSys {
190202
}
191203

192204
impl sys_traits::EnvCurrentDir for CliSys {
193-
fn env_current_dir(&self) -> std::io::Result<std::path::PathBuf> {
205+
fn env_current_dir(&self) -> std::io::Result<PathBuf> {
194206
match self {
195207
Self::Real(sys) => sys.env_current_dir(),
196208
Self::DenoCompile(sys) => sys.env_current_dir(),
@@ -211,7 +223,7 @@ impl sys_traits::BaseEnvVar for CliSys {
211223
}
212224

213225
impl sys_traits::EnvHomeDir for CliSys {
214-
fn env_home_dir(&self) -> Option<std::path::PathBuf> {
226+
fn env_home_dir(&self) -> Option<PathBuf> {
215227
#[allow(clippy::disallowed_types)] // ok because sys impl
216228
sys_traits::impls::RealSys.env_home_dir()
217229
}

0 commit comments

Comments
 (0)