2024-01-01 14:58:21 -05:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2023-05-10 20:06:59 -04:00
use std ::borrow ::Cow ;
2024-12-10 11:13:14 -05:00
use std ::cell ::RefCell ;
2023-05-10 20:06:59 -04:00
use std ::collections ::HashMap ;
use std ::collections ::HashSet ;
use std ::fs ::File ;
use std ::io ::Read ;
use std ::io ::Seek ;
use std ::io ::SeekFrom ;
2024-10-24 15:48:48 -04:00
use std ::ops ::Range ;
2023-05-10 20:06:59 -04:00
use std ::path ::Path ;
use std ::path ::PathBuf ;
use std ::rc ::Rc ;
use std ::sync ::Arc ;
2024-07-03 20:54:33 -04:00
use deno_core ::anyhow ::anyhow ;
2023-05-10 20:06:59 -04:00
use deno_core ::anyhow ::Context ;
use deno_core ::error ::AnyError ;
use deno_core ::parking_lot ::Mutex ;
use deno_core ::BufMutView ;
use deno_core ::BufView ;
2023-08-01 14:48:39 -04:00
use deno_core ::ResourceHandleFd ;
2023-05-10 20:06:59 -04:00
use deno_runtime ::deno_fs ::FsDirEntry ;
use deno_runtime ::deno_io ;
use deno_runtime ::deno_io ::fs ::FsError ;
use deno_runtime ::deno_io ::fs ::FsResult ;
use deno_runtime ::deno_io ::fs ::FsStat ;
use serde ::Deserialize ;
use serde ::Serialize ;
2023-05-27 10:33:15 -04:00
use thiserror ::Error ;
2023-05-10 20:06:59 -04:00
use crate ::util ;
2023-05-28 00:03:49 -04:00
use crate ::util ::fs ::canonicalize_path ;
2023-05-10 20:06:59 -04:00
2024-11-25 09:37:48 -05:00
#[ derive(Debug, Copy, Clone) ]
pub enum VfsFileSubDataKind {
/// Raw bytes of the file.
Raw ,
/// Bytes to use for module loading. For example, for TypeScript
/// files this will be the transpiled JavaScript source.
ModuleGraph ,
}
2023-05-27 10:33:15 -04:00
#[ derive(Error, Debug) ]
#[ error(
" Failed to strip prefix '{}' from '{}' " , root_path . display ( ) , target . display ( )
) ]
pub struct StripRootError {
root_path : PathBuf ,
target : PathBuf ,
}
2023-05-10 20:06:59 -04:00
pub struct VfsBuilder {
root_path : PathBuf ,
root_dir : VirtualDirectory ,
files : Vec < Vec < u8 > > ,
current_offset : u64 ,
file_offsets : HashMap < String , u64 > ,
}
impl VfsBuilder {
2023-05-28 00:03:49 -04:00
pub fn new ( root_path : PathBuf ) -> Result < Self , AnyError > {
2024-11-19 16:19:35 -05:00
let root_path = canonicalize_path ( & root_path )
. with_context ( | | format! ( " Canonicalizing {} " , root_path . display ( ) ) ) ? ;
2023-05-27 10:33:15 -04:00
log ::debug! ( " Building vfs with root '{}' " , root_path . display ( ) ) ;
2023-05-28 00:03:49 -04:00
Ok ( Self {
2023-05-10 20:06:59 -04:00
root_dir : VirtualDirectory {
name : root_path
. file_stem ( )
2024-07-03 20:54:33 -04:00
. map ( | s | s . to_string_lossy ( ) . into_owned ( ) )
. unwrap_or ( " root " . to_string ( ) ) ,
2023-05-10 20:06:59 -04:00
entries : Vec ::new ( ) ,
} ,
root_path ,
files : Vec ::new ( ) ,
current_offset : 0 ,
file_offsets : Default ::default ( ) ,
2023-05-28 00:03:49 -04:00
} )
2023-05-10 20:06:59 -04:00
}
2024-10-24 15:48:48 -04:00
pub fn set_new_root_path (
& mut self ,
root_path : PathBuf ,
) -> Result < ( ) , AnyError > {
let root_path = canonicalize_path ( & root_path ) ? ;
self . root_path = root_path ;
self . root_dir = VirtualDirectory {
name : self
. root_path
. file_stem ( )
. map ( | s | s . to_string_lossy ( ) . into_owned ( ) )
. unwrap_or ( " root " . to_string ( ) ) ,
entries : vec ! [ VfsEntry ::Dir ( VirtualDirectory {
name : std ::mem ::take ( & mut self . root_dir . name ) ,
entries : std ::mem ::take ( & mut self . root_dir . entries ) ,
} ) ] ,
} ;
Ok ( ( ) )
}
2024-07-03 20:54:33 -04:00
pub fn with_root_dir < R > (
& mut self ,
with_root : impl FnOnce ( & mut VirtualDirectory ) -> R ,
) -> R {
with_root ( & mut self . root_dir )
2023-05-10 20:06:59 -04:00
}
pub fn add_dir_recursive ( & mut self , path : & Path ) -> Result < ( ) , AnyError > {
2024-07-03 20:54:33 -04:00
let target_path = canonicalize_path ( path ) ? ;
if path ! = target_path {
self . add_symlink ( path , & target_path ) ? ;
}
self . add_dir_recursive_internal ( & target_path )
2023-05-28 00:03:49 -04:00
}
fn add_dir_recursive_internal (
& mut self ,
path : & Path ,
) -> Result < ( ) , AnyError > {
2023-05-27 10:33:15 -04:00
self . add_dir ( path ) ? ;
2023-05-10 20:06:59 -04:00
let read_dir = std ::fs ::read_dir ( path )
. with_context ( | | format! ( " Reading {} " , path . display ( ) ) ) ? ;
2024-08-19 12:41:11 -04:00
let mut dir_entries =
read_dir . into_iter ( ) . collect ::< Result < Vec < _ > , _ > > ( ) ? ;
dir_entries . sort_by_cached_key ( | entry | entry . file_name ( ) ) ; // determinism
for entry in dir_entries {
2023-05-10 20:06:59 -04:00
let file_type = entry . file_type ( ) ? ;
let path = entry . path ( ) ;
if file_type . is_dir ( ) {
2023-05-28 00:03:49 -04:00
self . add_dir_recursive_internal ( & path ) ? ;
2023-05-10 20:06:59 -04:00
} else if file_type . is_file ( ) {
2024-07-03 20:54:33 -04:00
self . add_file_at_path_not_symlink ( & path ) ? ;
2023-05-10 20:06:59 -04:00
} else if file_type . is_symlink ( ) {
2023-12-06 16:25:24 -05:00
match util ::fs ::canonicalize_path ( & path ) {
Ok ( target ) = > {
if let Err ( StripRootError { .. } ) = self . add_symlink ( & path , & target )
{
if target . is_file ( ) {
// this may change behavior, so warn the user about it
log ::warn! (
" {} Symlink target is outside '{}'. Inlining symlink at '{}' to '{}' as file. " ,
crate ::colors ::yellow ( " Warning " ) ,
self . root_path . display ( ) ,
path . display ( ) ,
target . display ( ) ,
) ;
// inline the symlink and make the target file
let file_bytes = std ::fs ::read ( & target )
. with_context ( | | format! ( " Reading {} " , path . display ( ) ) ) ? ;
2024-11-25 09:37:48 -05:00
self . add_file_with_data_inner (
& path ,
file_bytes ,
VfsFileSubDataKind ::Raw ,
) ? ;
2023-12-06 16:25:24 -05:00
} else {
log ::warn! (
" {} Symlink target is outside '{}'. Excluding symlink at '{}' with target '{}'. " ,
crate ::colors ::yellow ( " Warning " ) ,
self . root_path . display ( ) ,
path . display ( ) ,
target . display ( ) ,
) ;
}
}
}
Err ( err ) = > {
2023-05-27 10:33:15 -04:00
log ::warn! (
2023-12-06 16:25:24 -05:00
" {} Failed resolving symlink. Ignoring. \n Path: {} \n Message: {:#} " ,
crate ::colors ::yellow ( " Warning " ) ,
2023-05-27 10:33:15 -04:00
path . display ( ) ,
2023-12-06 16:25:24 -05:00
err
2023-05-27 10:33:15 -04:00
) ;
}
}
2023-05-10 20:06:59 -04:00
}
}
Ok ( ( ) )
}
2023-05-28 00:03:49 -04:00
fn add_dir (
2023-05-27 10:33:15 -04:00
& mut self ,
path : & Path ,
) -> Result < & mut VirtualDirectory , StripRootError > {
log ::debug! ( " Ensuring directory '{}' " , path . display ( ) ) ;
let path = self . path_relative_root ( path ) ? ;
2023-05-10 20:06:59 -04:00
let mut current_dir = & mut self . root_dir ;
for component in path . components ( ) {
let name = component . as_os_str ( ) . to_string_lossy ( ) ;
let index = match current_dir
. entries
. binary_search_by ( | e | e . name ( ) . cmp ( & name ) )
{
Ok ( index ) = > index ,
Err ( insert_index ) = > {
current_dir . entries . insert (
insert_index ,
VfsEntry ::Dir ( VirtualDirectory {
name : name . to_string ( ) ,
entries : Vec ::new ( ) ,
} ) ,
) ;
insert_index
}
} ;
match & mut current_dir . entries [ index ] {
VfsEntry ::Dir ( dir ) = > {
current_dir = dir ;
}
_ = > unreachable! ( ) ,
} ;
}
2023-05-27 10:33:15 -04:00
Ok ( current_dir )
2023-05-10 20:06:59 -04:00
}
2023-11-29 09:32:23 -05:00
pub fn add_file_at_path ( & mut self , path : & Path ) -> Result < ( ) , AnyError > {
2024-07-03 20:54:33 -04:00
let target_path = canonicalize_path ( path ) ? ;
if target_path ! = path {
self . add_symlink ( path , & target_path ) ? ;
}
self . add_file_at_path_not_symlink ( & target_path )
}
2024-10-24 15:48:48 -04:00
fn add_file_at_path_not_symlink (
2024-07-03 20:54:33 -04:00
& mut self ,
path : & Path ,
) -> Result < ( ) , AnyError > {
2023-11-29 09:32:23 -05:00
let file_bytes = std ::fs ::read ( path )
. with_context ( | | format! ( " Reading {} " , path . display ( ) ) ) ? ;
2024-11-25 09:37:48 -05:00
self . add_file_with_data_inner ( path , file_bytes , VfsFileSubDataKind ::Raw )
2023-11-29 09:32:23 -05:00
}
2024-10-24 15:48:48 -04:00
pub fn add_file_with_data (
& mut self ,
path : & Path ,
data : Vec < u8 > ,
2024-11-25 09:37:48 -05:00
sub_data_kind : VfsFileSubDataKind ,
2024-10-24 15:48:48 -04:00
) -> Result < ( ) , AnyError > {
let target_path = canonicalize_path ( path ) ? ;
if target_path ! = path {
self . add_symlink ( path , & target_path ) ? ;
}
2024-11-25 09:37:48 -05:00
self . add_file_with_data_inner ( & target_path , data , sub_data_kind )
2024-10-24 15:48:48 -04:00
}
fn add_file_with_data_inner (
& mut self ,
path : & Path ,
data : Vec < u8 > ,
2024-11-25 09:37:48 -05:00
sub_data_kind : VfsFileSubDataKind ,
2024-10-24 15:48:48 -04:00
) -> Result < ( ) , AnyError > {
2023-05-27 10:33:15 -04:00
log ::debug! ( " Adding file '{}' " , path . display ( ) ) ;
2023-05-10 20:06:59 -04:00
let checksum = util ::checksum ::gen ( & [ & data ] ) ;
let offset = if let Some ( offset ) = self . file_offsets . get ( & checksum ) {
// duplicate file, reuse an old offset
* offset
} else {
self . file_offsets . insert ( checksum , self . current_offset ) ;
self . current_offset
} ;
2023-05-27 10:33:15 -04:00
let dir = self . add_dir ( path . parent ( ) . unwrap ( ) ) ? ;
2023-05-10 20:06:59 -04:00
let name = path . file_name ( ) . unwrap ( ) . to_string_lossy ( ) ;
2024-12-09 20:11:52 -05:00
let offset_and_len = OffsetWithLength {
offset ,
len : data . len ( ) as u64 ,
} ;
2023-05-10 20:06:59 -04:00
match dir . entries . binary_search_by ( | e | e . name ( ) . cmp ( & name ) ) {
2024-11-25 09:37:48 -05:00
Ok ( index ) = > {
let entry = & mut dir . entries [ index ] ;
match entry {
VfsEntry ::File ( virtual_file ) = > match sub_data_kind {
VfsFileSubDataKind ::Raw = > {
2024-12-09 20:11:52 -05:00
virtual_file . offset = offset_and_len ;
2024-11-25 09:37:48 -05:00
}
VfsFileSubDataKind ::ModuleGraph = > {
2024-12-09 20:11:52 -05:00
virtual_file . module_graph_offset = offset_and_len ;
2024-11-25 09:37:48 -05:00
}
} ,
VfsEntry ::Dir ( _ ) | VfsEntry ::Symlink ( _ ) = > unreachable! ( ) ,
}
2024-07-03 20:54:33 -04:00
}
2023-05-10 20:06:59 -04:00
Err ( insert_index ) = > {
dir . entries . insert (
insert_index ,
VfsEntry ::File ( VirtualFile {
name : name . to_string ( ) ,
2024-12-09 20:11:52 -05:00
offset : offset_and_len ,
module_graph_offset : offset_and_len ,
2023-05-10 20:06:59 -04:00
} ) ,
) ;
}
}
// new file, update the list of files
if self . current_offset = = offset {
self . files . push ( data ) ;
2024-12-09 20:11:52 -05:00
self . current_offset + = offset_and_len . len ;
2023-05-10 20:06:59 -04:00
}
2023-05-27 10:33:15 -04:00
Ok ( ( ) )
2023-05-10 20:06:59 -04:00
}
2023-05-28 00:03:49 -04:00
fn add_symlink (
2023-05-27 10:33:15 -04:00
& mut self ,
path : & Path ,
target : & Path ,
) -> Result < ( ) , StripRootError > {
log ::debug! (
" Adding symlink '{}' to '{}' " ,
path . display ( ) ,
target . display ( )
) ;
2024-10-24 15:48:48 -04:00
let relative_target = self . path_relative_root ( target ) ? ;
let relative_path = match self . path_relative_root ( path ) {
Ok ( path ) = > path ,
Err ( StripRootError { .. } ) = > {
// ignore if the original path is outside the root directory
return Ok ( ( ) ) ;
}
} ;
if relative_target = = relative_path {
2024-07-03 20:54:33 -04:00
// it's the same, ignore
return Ok ( ( ) ) ;
}
2023-05-27 10:33:15 -04:00
let dir = self . add_dir ( path . parent ( ) . unwrap ( ) ) ? ;
2023-05-10 20:06:59 -04:00
let name = path . file_name ( ) . unwrap ( ) . to_string_lossy ( ) ;
match dir . entries . binary_search_by ( | e | e . name ( ) . cmp ( & name ) ) {
2024-11-25 17:08:06 -05:00
Ok ( _ ) = > Ok ( ( ) ) , // previously inserted
2023-05-10 20:06:59 -04:00
Err ( insert_index ) = > {
dir . entries . insert (
insert_index ,
VfsEntry ::Symlink ( VirtualSymlink {
name : name . to_string ( ) ,
2024-10-24 15:48:48 -04:00
dest_parts : relative_target
2023-05-10 20:06:59 -04:00
. components ( )
. map ( | c | c . as_os_str ( ) . to_string_lossy ( ) . to_string ( ) )
. collect ::< Vec < _ > > ( ) ,
} ) ,
) ;
2024-11-25 17:08:06 -05:00
Ok ( ( ) )
2023-05-10 20:06:59 -04:00
}
}
}
pub fn into_dir_and_files ( self ) -> ( VirtualDirectory , Vec < Vec < u8 > > ) {
( self . root_dir , self . files )
}
2023-05-25 15:29:58 -04:00
2023-05-27 10:33:15 -04:00
fn path_relative_root ( & self , path : & Path ) -> Result < PathBuf , StripRootError > {
2023-05-25 15:29:58 -04:00
match path . strip_prefix ( & self . root_path ) {
2023-05-27 10:33:15 -04:00
Ok ( p ) = > Ok ( p . to_path_buf ( ) ) ,
Err ( _ ) = > Err ( StripRootError {
root_path : self . root_path . clone ( ) ,
target : path . to_path_buf ( ) ,
} ) ,
2023-05-25 15:29:58 -04:00
}
}
2023-05-10 20:06:59 -04:00
}
#[ derive(Debug) ]
enum VfsEntryRef < ' a > {
Dir ( & ' a VirtualDirectory ) ,
File ( & ' a VirtualFile ) ,
Symlink ( & ' a VirtualSymlink ) ,
}
impl < ' a > VfsEntryRef < ' a > {
pub fn as_fs_stat ( & self ) -> FsStat {
match self {
VfsEntryRef ::Dir ( _ ) = > FsStat {
is_directory : true ,
is_file : false ,
is_symlink : false ,
atime : None ,
birthtime : None ,
mtime : None ,
2024-11-12 23:35:04 -05:00
ctime : None ,
2023-05-10 20:06:59 -04:00
blksize : 0 ,
size : 0 ,
dev : 0 ,
ino : 0 ,
mode : 0 ,
nlink : 0 ,
uid : 0 ,
gid : 0 ,
rdev : 0 ,
blocks : 0 ,
2023-05-24 15:18:13 -04:00
is_block_device : false ,
is_char_device : false ,
is_fifo : false ,
is_socket : false ,
2023-05-10 20:06:59 -04:00
} ,
VfsEntryRef ::File ( file ) = > FsStat {
is_directory : false ,
is_file : true ,
is_symlink : false ,
atime : None ,
birthtime : None ,
mtime : None ,
2024-11-12 23:35:04 -05:00
ctime : None ,
2023-05-10 20:06:59 -04:00
blksize : 0 ,
2024-12-09 20:11:52 -05:00
size : file . offset . len ,
2023-05-10 20:06:59 -04:00
dev : 0 ,
ino : 0 ,
mode : 0 ,
nlink : 0 ,
uid : 0 ,
gid : 0 ,
rdev : 0 ,
blocks : 0 ,
2023-05-24 15:18:13 -04:00
is_block_device : false ,
is_char_device : false ,
is_fifo : false ,
is_socket : false ,
2023-05-10 20:06:59 -04:00
} ,
VfsEntryRef ::Symlink ( _ ) = > FsStat {
is_directory : false ,
is_file : false ,
is_symlink : true ,
atime : None ,
birthtime : None ,
mtime : None ,
2024-11-12 23:35:04 -05:00
ctime : None ,
2023-05-10 20:06:59 -04:00
blksize : 0 ,
size : 0 ,
dev : 0 ,
ino : 0 ,
mode : 0 ,
nlink : 0 ,
uid : 0 ,
gid : 0 ,
rdev : 0 ,
blocks : 0 ,
2023-05-24 15:18:13 -04:00
is_block_device : false ,
is_char_device : false ,
is_fifo : false ,
is_socket : false ,
2023-05-10 20:06:59 -04:00
} ,
}
}
}
// todo(dsherret): we should store this more efficiently in the binary
#[ derive(Debug, Serialize, Deserialize) ]
pub enum VfsEntry {
Dir ( VirtualDirectory ) ,
File ( VirtualFile ) ,
Symlink ( VirtualSymlink ) ,
}
impl VfsEntry {
pub fn name ( & self ) -> & str {
match self {
VfsEntry ::Dir ( dir ) = > & dir . name ,
VfsEntry ::File ( file ) = > & file . name ,
VfsEntry ::Symlink ( symlink ) = > & symlink . name ,
}
}
fn as_ref ( & self ) -> VfsEntryRef {
match self {
VfsEntry ::Dir ( dir ) = > VfsEntryRef ::Dir ( dir ) ,
VfsEntry ::File ( file ) = > VfsEntryRef ::File ( file ) ,
VfsEntry ::Symlink ( symlink ) = > VfsEntryRef ::Symlink ( symlink ) ,
}
}
}
#[ derive(Debug, Serialize, Deserialize) ]
pub struct VirtualDirectory {
2024-12-09 20:11:52 -05:00
#[ serde(rename = " n " ) ]
2023-05-10 20:06:59 -04:00
pub name : String ,
// should be sorted by name
2024-12-09 20:11:52 -05:00
#[ serde(rename = " e " ) ]
2023-05-10 20:06:59 -04:00
pub entries : Vec < VfsEntry > ,
}
2024-12-09 20:11:52 -05:00
#[ derive(Debug, Clone, Copy, Serialize, Deserialize) ]
pub struct OffsetWithLength {
#[ serde(rename = " o " ) ]
pub offset : u64 ,
#[ serde(rename = " l " ) ]
pub len : u64 ,
}
2023-05-10 20:06:59 -04:00
#[ derive(Debug, Clone, Serialize, Deserialize) ]
pub struct VirtualFile {
2024-12-09 20:11:52 -05:00
#[ serde(rename = " n " ) ]
2023-05-10 20:06:59 -04:00
pub name : String ,
2024-12-09 20:11:52 -05:00
#[ serde(rename = " o " ) ]
pub offset : OffsetWithLength ,
2024-11-25 09:37:48 -05:00
/// Offset file to use for module loading when it differs from the
/// raw file. Often this will be the same offset as above for data
/// such as JavaScript files, but for TypeScript files the `offset`
/// will be the original raw bytes when included as an asset and this
/// offset will be to the transpiled JavaScript source.
2024-12-09 20:11:52 -05:00
#[ serde(rename = " m " ) ]
pub module_graph_offset : OffsetWithLength ,
2023-05-10 20:06:59 -04:00
}
#[ derive(Debug, Serialize, Deserialize) ]
pub struct VirtualSymlink {
2024-12-09 20:11:52 -05:00
#[ serde(rename = " n " ) ]
2023-05-10 20:06:59 -04:00
pub name : String ,
2024-12-09 20:11:52 -05:00
#[ serde(rename = " p " ) ]
2023-05-10 20:06:59 -04:00
pub dest_parts : Vec < String > ,
}
impl VirtualSymlink {
pub fn resolve_dest_from_root ( & self , root : & Path ) -> PathBuf {
let mut dest = root . to_path_buf ( ) ;
for part in & self . dest_parts {
dest . push ( part ) ;
}
dest
}
}
#[ derive(Debug) ]
pub struct VfsRoot {
pub dir : VirtualDirectory ,
pub root_path : PathBuf ,
pub start_file_offset : u64 ,
}
impl VfsRoot {
fn find_entry < ' a > (
& ' a self ,
path : & Path ,
) -> std ::io ::Result < ( PathBuf , VfsEntryRef < ' a > ) > {
self . find_entry_inner ( path , & mut HashSet ::new ( ) )
}
fn find_entry_inner < ' a > (
& ' a self ,
path : & Path ,
seen : & mut HashSet < PathBuf > ,
) -> std ::io ::Result < ( PathBuf , VfsEntryRef < ' a > ) > {
let mut path = Cow ::Borrowed ( path ) ;
loop {
let ( resolved_path , entry ) =
self . find_entry_no_follow_inner ( & path , seen ) ? ;
match entry {
VfsEntryRef ::Symlink ( symlink ) = > {
if ! seen . insert ( path . to_path_buf ( ) ) {
return Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::Other ,
" circular symlinks " ,
) ) ;
}
path = Cow ::Owned ( symlink . resolve_dest_from_root ( & self . root_path ) ) ;
}
_ = > {
return Ok ( ( resolved_path , entry ) ) ;
}
}
}
}
fn find_entry_no_follow (
& self ,
path : & Path ,
) -> std ::io ::Result < ( PathBuf , VfsEntryRef ) > {
self . find_entry_no_follow_inner ( path , & mut HashSet ::new ( ) )
}
fn find_entry_no_follow_inner < ' a > (
& ' a self ,
path : & Path ,
seen : & mut HashSet < PathBuf > ,
) -> std ::io ::Result < ( PathBuf , VfsEntryRef < ' a > ) > {
let relative_path = match path . strip_prefix ( & self . root_path ) {
Ok ( p ) = > p ,
Err ( _ ) = > {
return Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::NotFound ,
" path not found " ,
) ) ;
}
} ;
let mut final_path = self . root_path . clone ( ) ;
let mut current_entry = VfsEntryRef ::Dir ( & self . dir ) ;
for component in relative_path . components ( ) {
let component = component . as_os_str ( ) . to_string_lossy ( ) ;
let current_dir = match current_entry {
VfsEntryRef ::Dir ( dir ) = > {
final_path . push ( component . as_ref ( ) ) ;
dir
}
VfsEntryRef ::Symlink ( symlink ) = > {
let dest = symlink . resolve_dest_from_root ( & self . root_path ) ;
let ( resolved_path , entry ) = self . find_entry_inner ( & dest , seen ) ? ;
final_path = resolved_path ; // overwrite with the new resolved path
match entry {
VfsEntryRef ::Dir ( dir ) = > {
final_path . push ( component . as_ref ( ) ) ;
dir
}
_ = > {
return Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::NotFound ,
" path not found " ,
) ) ;
}
}
}
_ = > {
return Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::NotFound ,
" path not found " ,
) ) ;
}
} ;
match current_dir
. entries
. binary_search_by ( | e | e . name ( ) . cmp ( & component ) )
{
Ok ( index ) = > {
current_entry = current_dir . entries [ index ] . as_ref ( ) ;
}
Err ( _ ) = > {
return Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::NotFound ,
" path not found " ,
) ) ;
}
}
}
Ok ( ( final_path , current_entry ) )
}
}
struct FileBackedVfsFile {
file : VirtualFile ,
2024-12-10 11:13:14 -05:00
pos : RefCell < u64 > ,
2023-05-10 20:06:59 -04:00
vfs : Arc < FileBackedVfs > ,
}
impl FileBackedVfsFile {
fn seek ( & self , pos : SeekFrom ) -> FsResult < u64 > {
match pos {
SeekFrom ::Start ( pos ) = > {
2024-12-10 11:13:14 -05:00
* self . pos . borrow_mut ( ) = pos ;
2023-05-10 20:06:59 -04:00
Ok ( pos )
}
SeekFrom ::End ( offset ) = > {
2024-12-09 20:11:52 -05:00
if offset < 0 & & - offset as u64 > self . file . offset . len {
2023-05-27 10:33:15 -04:00
let msg = " An attempt was made to move the file pointer before the beginning of the file. " ;
Err (
std ::io ::Error ::new ( std ::io ::ErrorKind ::PermissionDenied , msg )
. into ( ) ,
)
2023-05-10 20:06:59 -04:00
} else {
2024-12-10 11:13:14 -05:00
let mut current_pos = self . pos . borrow_mut ( ) ;
2023-05-10 20:06:59 -04:00
* current_pos = if offset > = 0 {
2024-12-09 20:11:52 -05:00
self . file . offset . len - ( offset as u64 )
2023-05-10 20:06:59 -04:00
} else {
2024-12-09 20:11:52 -05:00
self . file . offset . len + ( - offset as u64 )
2023-05-10 20:06:59 -04:00
} ;
Ok ( * current_pos )
}
}
SeekFrom ::Current ( offset ) = > {
2024-12-10 11:13:14 -05:00
let mut current_pos = self . pos . borrow_mut ( ) ;
2023-05-10 20:06:59 -04:00
if offset > = 0 {
* current_pos + = offset as u64 ;
} else if - offset as u64 > * current_pos {
return Err ( std ::io ::Error ::new ( std ::io ::ErrorKind ::PermissionDenied , " An attempt was made to move the file pointer before the beginning of the file. " ) . into ( ) ) ;
} else {
* current_pos - = - offset as u64 ;
}
Ok ( * current_pos )
}
}
}
fn read_to_buf ( & self , buf : & mut [ u8 ] ) -> FsResult < usize > {
2024-11-22 14:26:38 -05:00
let read_pos = {
2024-12-10 11:13:14 -05:00
let mut pos = self . pos . borrow_mut ( ) ;
2023-05-10 20:06:59 -04:00
let read_pos = * pos ;
// advance the position due to the read
2024-12-09 20:11:52 -05:00
* pos = std ::cmp ::min ( self . file . offset . len , * pos + buf . len ( ) as u64 ) ;
2023-05-10 20:06:59 -04:00
read_pos
} ;
self
. vfs
2024-11-22 14:26:38 -05:00
. read_file ( & self . file , read_pos , buf )
2023-05-10 20:06:59 -04:00
. map_err ( | err | err . into ( ) )
}
2024-11-25 09:37:48 -05:00
fn read_to_end ( & self ) -> FsResult < Cow < 'static , [ u8 ] > > {
2024-11-22 14:26:38 -05:00
let read_pos = {
2024-12-10 11:13:14 -05:00
let mut pos = self . pos . borrow_mut ( ) ;
2023-05-10 20:06:59 -04:00
let read_pos = * pos ;
// todo(dsherret): should this always set it to the end of the file?
2024-12-09 20:11:52 -05:00
if * pos < self . file . offset . len {
2023-05-10 20:06:59 -04:00
// advance the position due to the read
2024-12-09 20:11:52 -05:00
* pos = self . file . offset . len ;
2023-05-10 20:06:59 -04:00
}
read_pos
} ;
2024-12-09 20:11:52 -05:00
if read_pos > self . file . offset . len {
2024-11-25 09:37:48 -05:00
return Ok ( Cow ::Borrowed ( & [ ] ) ) ;
}
if read_pos = = 0 {
Ok (
self
. vfs
. read_file_all ( & self . file , VfsFileSubDataKind ::Raw ) ? ,
)
} else {
2024-12-09 20:11:52 -05:00
let size = ( self . file . offset . len - read_pos ) as usize ;
2024-11-25 09:37:48 -05:00
let mut buf = vec! [ 0 ; size ] ;
self . vfs . read_file ( & self . file , read_pos , & mut buf ) ? ;
Ok ( Cow ::Owned ( buf ) )
2023-05-10 20:06:59 -04:00
}
}
}
#[ async_trait::async_trait(?Send) ]
impl deno_io ::fs ::File for FileBackedVfsFile {
fn read_sync ( self : Rc < Self > , buf : & mut [ u8 ] ) -> FsResult < usize > {
self . read_to_buf ( buf )
}
async fn read_byob (
self : Rc < Self > ,
mut buf : BufMutView ,
) -> FsResult < ( usize , BufMutView ) > {
2024-12-10 11:13:14 -05:00
// this is fast, no need to spawn a task
let nread = self . read_to_buf ( & mut buf ) ? ;
Ok ( ( nread , buf ) )
2023-05-10 20:06:59 -04:00
}
fn write_sync ( self : Rc < Self > , _buf : & [ u8 ] ) -> FsResult < usize > {
Err ( FsError ::NotSupported )
}
async fn write (
self : Rc < Self > ,
_buf : BufView ,
) -> FsResult < deno_core ::WriteOutcome > {
Err ( FsError ::NotSupported )
}
fn write_all_sync ( self : Rc < Self > , _buf : & [ u8 ] ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn write_all ( self : Rc < Self > , _buf : BufView ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
2024-11-27 21:28:41 -05:00
fn read_all_sync ( self : Rc < Self > ) -> FsResult < Cow < 'static , [ u8 ] > > {
self . read_to_end ( )
2023-05-10 20:06:59 -04:00
}
2024-11-27 21:28:41 -05:00
async fn read_all_async ( self : Rc < Self > ) -> FsResult < Cow < 'static , [ u8 ] > > {
2024-12-10 11:13:14 -05:00
// this is fast, no need to spawn a task
self . read_to_end ( )
2023-05-10 20:06:59 -04:00
}
fn chmod_sync ( self : Rc < Self > , _pathmode : u32 ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn chmod_async ( self : Rc < Self > , _mode : u32 ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
fn seek_sync ( self : Rc < Self > , pos : SeekFrom ) -> FsResult < u64 > {
self . seek ( pos )
}
async fn seek_async ( self : Rc < Self > , pos : SeekFrom ) -> FsResult < u64 > {
self . seek ( pos )
}
fn datasync_sync ( self : Rc < Self > ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn datasync_async ( self : Rc < Self > ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
fn sync_sync ( self : Rc < Self > ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn sync_async ( self : Rc < Self > ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
fn stat_sync ( self : Rc < Self > ) -> FsResult < FsStat > {
Err ( FsError ::NotSupported )
}
async fn stat_async ( self : Rc < Self > ) -> FsResult < FsStat > {
Err ( FsError ::NotSupported )
}
fn lock_sync ( self : Rc < Self > , _exclusive : bool ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn lock_async ( self : Rc < Self > , _exclusive : bool ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
fn unlock_sync ( self : Rc < Self > ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn unlock_async ( self : Rc < Self > ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
fn truncate_sync ( self : Rc < Self > , _len : u64 ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn truncate_async ( self : Rc < Self > , _len : u64 ) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
fn utime_sync (
self : Rc < Self > ,
_atime_secs : i64 ,
_atime_nanos : u32 ,
_mtime_secs : i64 ,
_mtime_nanos : u32 ,
) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
async fn utime_async (
self : Rc < Self > ,
_atime_secs : i64 ,
_atime_nanos : u32 ,
_mtime_secs : i64 ,
_mtime_nanos : u32 ,
) -> FsResult < ( ) > {
Err ( FsError ::NotSupported )
}
// lower level functionality
fn as_stdio ( self : Rc < Self > ) -> FsResult < std ::process ::Stdio > {
Err ( FsError ::NotSupported )
}
2023-08-01 14:48:39 -04:00
fn backing_fd ( self : Rc < Self > ) -> Option < ResourceHandleFd > {
2023-05-10 20:06:59 -04:00
None
}
fn try_clone_inner ( self : Rc < Self > ) -> FsResult < Rc < dyn deno_io ::fs ::File > > {
Ok ( self )
}
}
#[ derive(Debug) ]
pub struct FileBackedVfs {
2024-10-24 15:48:48 -04:00
vfs_data : Cow < 'static , [ u8 ] > ,
2023-05-10 20:06:59 -04:00
fs_root : VfsRoot ,
}
impl FileBackedVfs {
2024-10-24 15:48:48 -04:00
pub fn new ( data : Cow < 'static , [ u8 ] > , fs_root : VfsRoot ) -> Self {
2023-05-10 20:06:59 -04:00
Self {
2024-10-24 15:48:48 -04:00
vfs_data : data ,
2023-05-10 20:06:59 -04:00
fs_root ,
}
}
pub fn root ( & self ) -> & Path {
& self . fs_root . root_path
}
pub fn is_path_within ( & self , path : & Path ) -> bool {
path . starts_with ( & self . fs_root . root_path )
}
pub fn open_file (
self : & Arc < Self > ,
path : & Path ,
) -> std ::io ::Result < Rc < dyn deno_io ::fs ::File > > {
let file = self . file_entry ( path ) ? ;
Ok ( Rc ::new ( FileBackedVfsFile {
file : file . clone ( ) ,
vfs : self . clone ( ) ,
pos : Default ::default ( ) ,
} ) )
}
pub fn read_dir ( & self , path : & Path ) -> std ::io ::Result < Vec < FsDirEntry > > {
let dir = self . dir_entry ( path ) ? ;
Ok (
dir
. entries
. iter ( )
. map ( | entry | FsDirEntry {
name : entry . name ( ) . to_string ( ) ,
is_file : matches ! ( entry , VfsEntry ::File ( _ ) ) ,
is_directory : matches ! ( entry , VfsEntry ::Dir ( _ ) ) ,
is_symlink : matches ! ( entry , VfsEntry ::Symlink ( _ ) ) ,
} )
. collect ( ) ,
)
}
pub fn read_link ( & self , path : & Path ) -> std ::io ::Result < PathBuf > {
let ( _ , entry ) = self . fs_root . find_entry_no_follow ( path ) ? ;
match entry {
VfsEntryRef ::Symlink ( symlink ) = > {
Ok ( symlink . resolve_dest_from_root ( & self . fs_root . root_path ) )
}
VfsEntryRef ::Dir ( _ ) | VfsEntryRef ::File ( _ ) = > Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::Other ,
" not a symlink " ,
) ) ,
}
}
pub fn lstat ( & self , path : & Path ) -> std ::io ::Result < FsStat > {
let ( _ , entry ) = self . fs_root . find_entry_no_follow ( path ) ? ;
Ok ( entry . as_fs_stat ( ) )
}
pub fn stat ( & self , path : & Path ) -> std ::io ::Result < FsStat > {
let ( _ , entry ) = self . fs_root . find_entry ( path ) ? ;
Ok ( entry . as_fs_stat ( ) )
}
pub fn canonicalize ( & self , path : & Path ) -> std ::io ::Result < PathBuf > {
let ( path , _ ) = self . fs_root . find_entry ( path ) ? ;
Ok ( path )
}
2024-10-24 15:48:48 -04:00
pub fn read_file_all (
& self ,
file : & VirtualFile ,
2024-11-25 09:37:48 -05:00
sub_data_kind : VfsFileSubDataKind ,
2024-10-24 15:48:48 -04:00
) -> std ::io ::Result < Cow < 'static , [ u8 ] > > {
2024-12-09 20:11:52 -05:00
let read_len = match sub_data_kind {
VfsFileSubDataKind ::Raw = > file . offset . len ,
VfsFileSubDataKind ::ModuleGraph = > file . module_graph_offset . len ,
} ;
let read_range = self . get_read_range ( file , sub_data_kind , 0 , read_len ) ? ;
2024-10-24 15:48:48 -04:00
match & self . vfs_data {
Cow ::Borrowed ( data ) = > Ok ( Cow ::Borrowed ( & data [ read_range ] ) ) ,
Cow ::Owned ( data ) = > Ok ( Cow ::Owned ( data [ read_range ] . to_vec ( ) ) ) ,
}
2023-05-10 20:06:59 -04:00
}
pub fn read_file (
& self ,
file : & VirtualFile ,
pos : u64 ,
buf : & mut [ u8 ] ,
) -> std ::io ::Result < usize > {
2024-11-25 09:37:48 -05:00
let read_range = self . get_read_range (
file ,
VfsFileSubDataKind ::Raw ,
pos ,
buf . len ( ) as u64 ,
) ? ;
2024-11-22 14:26:38 -05:00
let read_len = read_range . len ( ) ;
buf [ .. read_len ] . copy_from_slice ( & self . vfs_data [ read_range ] ) ;
Ok ( read_len )
2024-10-24 15:48:48 -04:00
}
fn get_read_range (
& self ,
file : & VirtualFile ,
2024-11-25 09:37:48 -05:00
sub_data_kind : VfsFileSubDataKind ,
2024-10-24 15:48:48 -04:00
pos : u64 ,
len : u64 ,
) -> std ::io ::Result < Range < usize > > {
2024-12-09 20:11:52 -05:00
let file_offset_and_len = match sub_data_kind {
VfsFileSubDataKind ::Raw = > file . offset ,
VfsFileSubDataKind ::ModuleGraph = > file . module_graph_offset ,
} ;
if pos > file_offset_and_len . len {
2024-08-01 00:15:13 -04:00
return Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::UnexpectedEof ,
" unexpected EOF " ,
) ) ;
}
2024-12-09 20:11:52 -05:00
let file_offset =
self . fs_root . start_file_offset + file_offset_and_len . offset ;
2024-11-22 14:26:38 -05:00
let start = file_offset + pos ;
2024-12-09 20:11:52 -05:00
let end = file_offset + std ::cmp ::min ( pos + len , file_offset_and_len . len ) ;
2024-10-24 15:48:48 -04:00
Ok ( start as usize .. end as usize )
2023-05-10 20:06:59 -04:00
}
pub fn dir_entry ( & self , path : & Path ) -> std ::io ::Result < & VirtualDirectory > {
let ( _ , entry ) = self . fs_root . find_entry ( path ) ? ;
match entry {
VfsEntryRef ::Dir ( dir ) = > Ok ( dir ) ,
VfsEntryRef ::Symlink ( _ ) = > unreachable! ( ) ,
VfsEntryRef ::File ( _ ) = > Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::Other ,
" path is a file " ,
) ) ,
}
}
pub fn file_entry ( & self , path : & Path ) -> std ::io ::Result < & VirtualFile > {
let ( _ , entry ) = self . fs_root . find_entry ( path ) ? ;
match entry {
VfsEntryRef ::Dir ( _ ) = > Err ( std ::io ::Error ::new (
std ::io ::ErrorKind ::Other ,
" path is a directory " ,
) ) ,
VfsEntryRef ::Symlink ( _ ) = > unreachable! ( ) ,
VfsEntryRef ::File ( file ) = > Ok ( file ) ,
}
}
}
#[ cfg(test) ]
mod test {
use std ::io ::Write ;
use test_util ::TempDir ;
use super ::* ;
2023-05-28 00:03:49 -04:00
#[ track_caller ]
2023-05-10 20:06:59 -04:00
fn read_file ( vfs : & FileBackedVfs , path : & Path ) -> String {
let file = vfs . file_entry ( path ) . unwrap ( ) ;
2024-11-25 09:37:48 -05:00
String ::from_utf8 (
vfs
. read_file_all ( file , VfsFileSubDataKind ::Raw )
. unwrap ( )
. into_owned ( ) ,
)
. unwrap ( )
2023-05-10 20:06:59 -04:00
}
#[ test ]
fn builds_and_uses_virtual_fs ( ) {
let temp_dir = TempDir ::new ( ) ;
2023-05-28 09:55:30 -04:00
// we canonicalize the temp directory because the vfs builder
// will canonicalize the root path
2023-06-10 11:09:45 -04:00
let src_path = temp_dir . path ( ) . canonicalize ( ) . join ( " src " ) ;
src_path . create_dir_all ( ) ;
let src_path = src_path . to_path_buf ( ) ;
2023-05-28 00:03:49 -04:00
let mut builder = VfsBuilder ::new ( src_path . clone ( ) ) . unwrap ( ) ;
2023-05-27 10:33:15 -04:00
builder
2024-11-25 09:37:48 -05:00
. add_file_with_data_inner (
& src_path . join ( " a.txt " ) ,
" data " . into ( ) ,
VfsFileSubDataKind ::Raw ,
)
2023-05-27 10:33:15 -04:00
. unwrap ( ) ;
builder
2024-11-25 09:37:48 -05:00
. add_file_with_data_inner (
& src_path . join ( " b.txt " ) ,
" data " . into ( ) ,
VfsFileSubDataKind ::Raw ,
)
2023-05-27 10:33:15 -04:00
. unwrap ( ) ;
2023-05-10 20:06:59 -04:00
assert_eq! ( builder . files . len ( ) , 1 ) ; // because duplicate data
2023-05-27 10:33:15 -04:00
builder
2024-11-25 09:37:48 -05:00
. add_file_with_data_inner (
& src_path . join ( " c.txt " ) ,
" c " . into ( ) ,
VfsFileSubDataKind ::Raw ,
)
2023-05-27 10:33:15 -04:00
. unwrap ( ) ;
builder
2024-10-24 15:48:48 -04:00
. add_file_with_data_inner (
& src_path . join ( " sub_dir " ) . join ( " d.txt " ) ,
" d " . into ( ) ,
2024-11-25 09:37:48 -05:00
VfsFileSubDataKind ::Raw ,
2024-10-24 15:48:48 -04:00
)
2023-05-27 10:33:15 -04:00
. unwrap ( ) ;
builder
2024-11-25 09:37:48 -05:00
. add_file_with_data_inner (
& src_path . join ( " e.txt " ) ,
" e " . into ( ) ,
VfsFileSubDataKind ::Raw ,
)
2023-05-27 10:33:15 -04:00
. unwrap ( ) ;
builder
. add_symlink (
& src_path . join ( " sub_dir " ) . join ( " e.txt " ) ,
& src_path . join ( " e.txt " ) ,
)
. unwrap ( ) ;
2023-05-10 20:06:59 -04:00
// get the virtual fs
let ( dest_path , virtual_fs ) = into_virtual_fs ( builder , & temp_dir ) ;
assert_eq! ( read_file ( & virtual_fs , & dest_path . join ( " a.txt " ) ) , " data " ) ;
assert_eq! ( read_file ( & virtual_fs , & dest_path . join ( " b.txt " ) ) , " data " ) ;
// attempt reading a symlink
assert_eq! (
read_file ( & virtual_fs , & dest_path . join ( " sub_dir " ) . join ( " e.txt " ) ) ,
" e " ,
) ;
// canonicalize symlink
assert_eq! (
virtual_fs
. canonicalize ( & dest_path . join ( " sub_dir " ) . join ( " e.txt " ) )
. unwrap ( ) ,
dest_path . join ( " e.txt " ) ,
) ;
// metadata
assert! (
virtual_fs
. lstat ( & dest_path . join ( " sub_dir " ) . join ( " e.txt " ) )
. unwrap ( )
. is_symlink
) ;
assert! (
virtual_fs
. stat ( & dest_path . join ( " sub_dir " ) . join ( " e.txt " ) )
. unwrap ( )
. is_file
) ;
assert! (
virtual_fs
. stat ( & dest_path . join ( " sub_dir " ) )
. unwrap ( )
. is_directory ,
) ;
assert! ( virtual_fs . stat ( & dest_path . join ( " e.txt " ) ) . unwrap ( ) . is_file , ) ;
}
#[ test ]
fn test_include_dir_recursive ( ) {
let temp_dir = TempDir ::new ( ) ;
2023-06-10 11:09:45 -04:00
let temp_dir_path = temp_dir . path ( ) . canonicalize ( ) ;
2023-05-10 20:06:59 -04:00
temp_dir . create_dir_all ( " src/nested/sub_dir " ) ;
temp_dir . write ( " src/a.txt " , " data " ) ;
temp_dir . write ( " src/b.txt " , " data " ) ;
util ::fs ::symlink_dir (
2023-06-10 11:09:45 -04:00
temp_dir_path . join ( " src/nested/sub_dir " ) . as_path ( ) ,
temp_dir_path . join ( " src/sub_dir_link " ) . as_path ( ) ,
2023-05-10 20:06:59 -04:00
)
. unwrap ( ) ;
temp_dir . write ( " src/nested/sub_dir/c.txt " , " c " ) ;
// build and create the virtual fs
2023-06-10 11:09:45 -04:00
let src_path = temp_dir_path . join ( " src " ) . to_path_buf ( ) ;
2023-05-28 00:03:49 -04:00
let mut builder = VfsBuilder ::new ( src_path . clone ( ) ) . unwrap ( ) ;
2023-05-10 20:06:59 -04:00
builder . add_dir_recursive ( & src_path ) . unwrap ( ) ;
let ( dest_path , virtual_fs ) = into_virtual_fs ( builder , & temp_dir ) ;
assert_eq! ( read_file ( & virtual_fs , & dest_path . join ( " a.txt " ) ) , " data " , ) ;
assert_eq! ( read_file ( & virtual_fs , & dest_path . join ( " b.txt " ) ) , " data " , ) ;
assert_eq! (
read_file (
& virtual_fs ,
& dest_path . join ( " nested " ) . join ( " sub_dir " ) . join ( " c.txt " )
) ,
" c " ,
) ;
assert_eq! (
read_file ( & virtual_fs , & dest_path . join ( " sub_dir_link " ) . join ( " c.txt " ) ) ,
" c " ,
) ;
assert! (
virtual_fs
. lstat ( & dest_path . join ( " sub_dir_link " ) )
. unwrap ( )
. is_symlink
) ;
assert_eq! (
virtual_fs
. canonicalize ( & dest_path . join ( " sub_dir_link " ) . join ( " c.txt " ) )
. unwrap ( ) ,
dest_path . join ( " nested " ) . join ( " sub_dir " ) . join ( " c.txt " ) ,
) ;
}
fn into_virtual_fs (
builder : VfsBuilder ,
temp_dir : & TempDir ,
) -> ( PathBuf , FileBackedVfs ) {
let virtual_fs_file = temp_dir . path ( ) . join ( " virtual_fs " ) ;
let ( root_dir , files ) = builder . into_dir_and_files ( ) ;
{
let mut file = std ::fs ::File ::create ( & virtual_fs_file ) . unwrap ( ) ;
for file_data in & files {
file . write_all ( file_data ) . unwrap ( ) ;
}
}
let dest_path = temp_dir . path ( ) . join ( " dest " ) ;
2024-08-01 00:15:13 -04:00
let data = std ::fs ::read ( & virtual_fs_file ) . unwrap ( ) ;
2023-05-10 20:06:59 -04:00
(
2023-06-10 11:09:45 -04:00
dest_path . to_path_buf ( ) ,
2023-05-10 20:06:59 -04:00
FileBackedVfs ::new (
2024-10-24 15:48:48 -04:00
Cow ::Owned ( data ) ,
2023-05-10 20:06:59 -04:00
VfsRoot {
dir : root_dir ,
2023-06-10 11:09:45 -04:00
root_path : dest_path . to_path_buf ( ) ,
2023-05-10 20:06:59 -04:00
start_file_offset : 0 ,
} ,
) ,
)
}
#[ test ]
fn circular_symlink ( ) {
let temp_dir = TempDir ::new ( ) ;
2023-06-10 11:09:45 -04:00
let src_path = temp_dir . path ( ) . canonicalize ( ) . join ( " src " ) ;
src_path . create_dir_all ( ) ;
let src_path = src_path . to_path_buf ( ) ;
2023-05-28 00:03:49 -04:00
let mut builder = VfsBuilder ::new ( src_path . clone ( ) ) . unwrap ( ) ;
2023-05-27 10:33:15 -04:00
builder
. add_symlink ( & src_path . join ( " a.txt " ) , & src_path . join ( " b.txt " ) )
. unwrap ( ) ;
builder
. add_symlink ( & src_path . join ( " b.txt " ) , & src_path . join ( " c.txt " ) )
. unwrap ( ) ;
builder
. add_symlink ( & src_path . join ( " c.txt " ) , & src_path . join ( " a.txt " ) )
. unwrap ( ) ;
2023-05-10 20:06:59 -04:00
let ( dest_path , virtual_fs ) = into_virtual_fs ( builder , & temp_dir ) ;
assert_eq! (
virtual_fs
. file_entry ( & dest_path . join ( " a.txt " ) )
. err ( )
. unwrap ( )
. to_string ( ) ,
" circular symlinks " ,
) ;
assert_eq! (
virtual_fs . read_link ( & dest_path . join ( " a.txt " ) ) . unwrap ( ) ,
dest_path . join ( " b.txt " )
) ;
assert_eq! (
virtual_fs . read_link ( & dest_path . join ( " b.txt " ) ) . unwrap ( ) ,
dest_path . join ( " c.txt " )
) ;
}
#[ tokio::test ]
async fn test_open_file ( ) {
let temp_dir = TempDir ::new ( ) ;
2023-06-10 11:09:45 -04:00
let temp_path = temp_dir . path ( ) . canonicalize ( ) ;
2023-05-28 00:03:49 -04:00
let mut builder = VfsBuilder ::new ( temp_path . to_path_buf ( ) ) . unwrap ( ) ;
2023-05-27 10:33:15 -04:00
builder
2024-10-24 15:48:48 -04:00
. add_file_with_data_inner (
2023-06-10 11:09:45 -04:00
temp_path . join ( " a.txt " ) . as_path ( ) ,
2023-05-27 10:33:15 -04:00
" 0123456789 " . to_string ( ) . into_bytes ( ) ,
2024-11-25 09:37:48 -05:00
VfsFileSubDataKind ::Raw ,
2023-05-27 10:33:15 -04:00
)
. unwrap ( ) ;
2023-05-10 20:06:59 -04:00
let ( dest_path , virtual_fs ) = into_virtual_fs ( builder , & temp_dir ) ;
let virtual_fs = Arc ::new ( virtual_fs ) ;
let file = virtual_fs . open_file ( & dest_path . join ( " a.txt " ) ) . unwrap ( ) ;
file . clone ( ) . seek_sync ( SeekFrom ::Current ( 2 ) ) . unwrap ( ) ;
let mut buf = vec! [ 0 ; 2 ] ;
file . clone ( ) . read_sync ( & mut buf ) . unwrap ( ) ;
assert_eq! ( buf , b " 23 " ) ;
file . clone ( ) . read_sync ( & mut buf ) . unwrap ( ) ;
assert_eq! ( buf , b " 45 " ) ;
file . clone ( ) . seek_sync ( SeekFrom ::Current ( - 4 ) ) . unwrap ( ) ;
file . clone ( ) . read_sync ( & mut buf ) . unwrap ( ) ;
assert_eq! ( buf , b " 23 " ) ;
file . clone ( ) . seek_sync ( SeekFrom ::Start ( 2 ) ) . unwrap ( ) ;
file . clone ( ) . read_sync ( & mut buf ) . unwrap ( ) ;
assert_eq! ( buf , b " 23 " ) ;
file . clone ( ) . seek_sync ( SeekFrom ::End ( 2 ) ) . unwrap ( ) ;
file . clone ( ) . read_sync ( & mut buf ) . unwrap ( ) ;
assert_eq! ( buf , b " 89 " ) ;
file . clone ( ) . seek_sync ( SeekFrom ::Current ( - 8 ) ) . unwrap ( ) ;
file . clone ( ) . read_sync ( & mut buf ) . unwrap ( ) ;
assert_eq! ( buf , b " 23 " ) ;
assert_eq! (
file
. clone ( )
. seek_sync ( SeekFrom ::Current ( - 5 ) )
. err ( )
. unwrap ( )
. into_io_error ( )
. to_string ( ) ,
" An attempt was made to move the file pointer before the beginning of the file. "
) ;
// go beyond the file length, then back
file . clone ( ) . seek_sync ( SeekFrom ::Current ( 40 ) ) . unwrap ( ) ;
file . clone ( ) . seek_sync ( SeekFrom ::Current ( - 38 ) ) . unwrap ( ) ;
let read_buf = file . clone ( ) . read ( 2 ) . await . unwrap ( ) ;
assert_eq! ( read_buf . to_vec ( ) , b " 67 " ) ;
file . clone ( ) . seek_sync ( SeekFrom ::Current ( - 2 ) ) . unwrap ( ) ;
// read to the end of the file
let all_buf = file . clone ( ) . read_all_sync ( ) . unwrap ( ) ;
assert_eq! ( all_buf . to_vec ( ) , b " 6789 " ) ;
file . clone ( ) . seek_sync ( SeekFrom ::Current ( - 9 ) ) . unwrap ( ) ;
// try try_clone_inner and read_all_async
let all_buf = file
. try_clone_inner ( )
. unwrap ( )
. read_all_async ( )
. await
. unwrap ( ) ;
assert_eq! ( all_buf . to_vec ( ) , b " 123456789 " ) ;
}
}