1
1
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
use crate :: chmod:: { set_mode, set_mode_sync} ;
4
+
5
+ use llrt_path:: resolve_path;
4
6
use llrt_utils:: result:: ResultExt ;
5
7
use ring:: rand:: { SecureRandom , SystemRandom } ;
6
8
use rquickjs:: { function:: Opt , Ctx , Object , Result } ;
7
9
use tokio:: fs;
8
10
9
11
pub async fn mkdir < ' js > ( ctx : Ctx < ' js > , path : String , options : Opt < Object < ' js > > ) -> Result < String > {
10
- let ( recursive, mode) = get_params ( options) ;
12
+ let ( recursive, mode, path ) = get_params ( & path , options) ? ;
11
13
12
14
if recursive {
13
15
fs:: create_dir_all ( & path) . await
@@ -22,7 +24,7 @@ pub async fn mkdir<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>)
22
24
}
23
25
24
26
pub fn mkdir_sync < ' js > ( ctx : Ctx < ' js > , path : String , options : Opt < Object < ' js > > ) -> Result < String > {
25
- let ( recursive, mode) = get_params ( options) ;
27
+ let ( recursive, mode, path ) = get_params ( & path , options) ? ;
26
28
27
29
if recursive {
28
30
std:: fs:: create_dir_all ( & path)
@@ -36,15 +38,16 @@ pub fn mkdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -
36
38
Ok ( path)
37
39
}
38
40
39
- fn get_params ( options : Opt < Object > ) -> ( bool , u32 ) {
41
+ fn get_params ( path : & str , options : Opt < Object > ) -> Result < ( bool , u32 , String ) > {
40
42
let mut recursive = false ;
41
43
let mut mode = 0o777 ;
42
44
43
45
if let Some ( options) = options. 0 {
44
46
recursive = options. get ( "recursive" ) . unwrap_or_default ( ) ;
45
47
mode = options. get ( "mode" ) . unwrap_or ( 0o777 ) ;
46
48
}
47
- ( recursive, mode)
49
+ let path = resolve_path ( [ path] ) ?;
50
+ Ok ( ( recursive, mode, path) )
48
51
}
49
52
50
53
const CHARS : & [ u8 ] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
0 commit comments