1- use  std:: fs; 
21use  std:: io; 
32use  std:: path:: PathBuf ; 
3+ use  thiserror:: Error ; 
44
55use  wasmer:: { DeserializeError ,  Module ,  Store } ; 
66
77use  crate :: checksum:: Checksum ; 
88use  crate :: errors:: { VmError ,  VmResult } ; 
99
10+ use  crate :: filesystem:: mkdir_p; 
1011use  crate :: modules:: current_wasmer_module_version; 
1112
1213/// Bump this version whenever the module system changes in a way 
@@ -42,6 +43,20 @@ pub struct FileSystemCache {
4243    wasmer_module_version :  u32 , 
4344} 
4445
46+ /// An error type that hides system specific error information 
47+ /// to ensure deterministic errors across operating systems. 
48+ #[ derive( Error ,  Debug ) ]  
49+ pub  enum  NewFileSystemCacheError  { 
50+     #[ error( "Could not get metadata of cache path" ) ]  
51+     CouldntGetMetadata , 
52+     #[ error( "The supplied path is readonly" ) ]  
53+     ReadonlyPath , 
54+     #[ error( "The supplied path already exists but is no directory" ) ]  
55+     ExistsButNoDirectory , 
56+     #[ error( "Could not create cache path" ) ]  
57+     CouldntCreatePath , 
58+ } 
59+ 
4560impl  FileSystemCache  { 
4661    /// Construct a new `FileSystemCache` around the specified directory. 
4762/// The contents of the cache are stored in sub-versioned directories. 
@@ -50,38 +65,29 @@ impl FileSystemCache {
5065/// 
5166/// This method is unsafe because there's no way to ensure the artifacts 
5267/// stored in this cache haven't been corrupted or tampered with. 
53- pub  unsafe  fn  new ( path :  impl  Into < PathBuf > )  -> io :: Result < Self >  { 
68+ pub  unsafe  fn  new ( path :  impl  Into < PathBuf > )  -> Result < Self ,   NewFileSystemCacheError >  { 
5469        let  wasmer_module_version = current_wasmer_module_version ( ) ; 
5570
5671        let  path:  PathBuf  = path. into ( ) ; 
5772        if  path. exists ( )  { 
58-             let  metadata = path. metadata ( ) ?; 
73+             let  metadata = path
74+                 . metadata ( ) 
75+                 . map_err ( |_e| NewFileSystemCacheError :: CouldntGetMetadata ) ?; 
5976            if  metadata. is_dir ( )  { 
6077                if  !metadata. permissions ( ) . readonly ( )  { 
6178                    Ok ( Self  { 
6279                        base_path :  path, 
6380                        wasmer_module_version, 
6481                    } ) 
6582                }  else  { 
66-                     // This directory is readonly. 
67-                     Err ( io:: Error :: new ( 
68-                         io:: ErrorKind :: PermissionDenied , 
69-                         format ! ( "the supplied path is readonly: {}" ,  path. display( ) ) , 
70-                     ) ) 
83+                     Err ( NewFileSystemCacheError :: ReadonlyPath ) 
7184                } 
7285            }  else  { 
73-                 // This path points to a file. 
74-                 Err ( io:: Error :: new ( 
75-                     io:: ErrorKind :: PermissionDenied , 
76-                     format ! ( 
77-                         "the supplied path already points to a file: {}" , 
78-                         path. display( ) 
79-                     ) , 
80-                 ) ) 
86+                 Err ( NewFileSystemCacheError :: ExistsButNoDirectory ) 
8187            } 
8288        }  else  { 
8389            // Create the directory and any parent directories if they don't yet exist. 
84-             fs :: create_dir_all ( & path) ?; 
90+             mkdir_p ( & path) . map_err ( |_e|  NewFileSystemCacheError :: CouldntCreatePath ) ?; 
8591            Ok ( Self  { 
8692                base_path :  path, 
8793                wasmer_module_version, 
@@ -115,8 +121,9 @@ impl FileSystemCache {
115121    /// Stores a serialized module to the file system. Returns the size of the serialized module. 
116122pub  fn  store ( & mut  self ,  checksum :  & Checksum ,  module :  & Module )  -> VmResult < ( ) >  { 
117123        let  modules_dir = self . latest_modules_path ( ) ; 
118-         fs:: create_dir_all ( & modules_dir) 
119-             . map_err ( |e| VmError :: cache_err ( format ! ( "Error creating directory: {}" ,  e) ) ) ?; 
124+         mkdir_p ( & modules_dir) 
125+             . map_err ( |_e| VmError :: cache_err ( "Error creating modules directory" ) ) ?; 
126+ 
120127        let  filename = checksum. to_hex ( ) ; 
121128        let  path = modules_dir. join ( filename) ; 
122129        module
@@ -137,6 +144,8 @@ impl FileSystemCache {
137144
138145#[ cfg( test) ]  
139146mod  tests { 
147+     use  std:: fs; 
148+ 
140149    use  super :: * ; 
141150    use  crate :: size:: Size ; 
142151    use  crate :: wasm_backend:: { compile,  make_runtime_store} ; 
0 commit comments