@@ -5,9 +5,22 @@ use std::io::{Read, Write};
55use std:: path:: Path ;
66use std:: rc:: Rc ;
77use techscript_runtime:: {
8- context:: Capability , error:: RuntimeError , error:: RuntimeErrorKind , value:: RuntimeValue ,
8+ context:: { Capability , RuntimeContext } , error:: RuntimeError , error:: RuntimeErrorKind , value:: RuntimeValue ,
99} ;
1010
11+ fn check_fs_capability ( ctx : & RuntimeContext ) -> Result < ( ) , RuntimeError > {
12+ if !ctx. config . capabilities . contains ( & Capability :: FileSystem ) {
13+ return Err ( RuntimeError :: new (
14+ RuntimeErrorKind :: InvalidOperation (
15+ "Security policy violation: FileSystem capability is denied" . to_string ( ) ,
16+ ) ,
17+ None ,
18+ None ,
19+ ) ) ;
20+ }
21+ Ok ( ( ) )
22+ }
23+
1124impl StdlibRegistry {
1225 pub fn register_compress ( & mut self ) {
1326 let mut exports: HashMap < String , Rc < dyn techscript_runtime:: function:: Callable > > =
@@ -19,16 +32,7 @@ impl StdlibRegistry {
1932 name : "zip" . to_string ( ) ,
2033 arity : 2 ,
2134 callback : |ctx, args| {
22- if !ctx. config . capabilities . contains ( & Capability :: FileSystem ) {
23- return Err ( RuntimeError :: new (
24- RuntimeErrorKind :: InvalidOperation (
25- "Security policy violation: FileSystem capability is denied"
26- . to_string ( ) ,
27- ) ,
28- None ,
29- None ,
30- ) ) ;
31- }
35+ check_fs_capability ( ctx) ?;
3236 let src_dir = args[ 0 ] . try_into_string ( ) ?;
3337 let archive_path = args[ 1 ] . try_into_string ( ) ?;
3438 zip_dir ( & src_dir, & archive_path) . map_err ( |e| {
@@ -49,16 +53,7 @@ impl StdlibRegistry {
4953 name : "unzip" . to_string ( ) ,
5054 arity : 2 ,
5155 callback : |ctx, args| {
52- if !ctx. config . capabilities . contains ( & Capability :: FileSystem ) {
53- return Err ( RuntimeError :: new (
54- RuntimeErrorKind :: InvalidOperation (
55- "Security policy violation: FileSystem capability is denied"
56- . to_string ( ) ,
57- ) ,
58- None ,
59- None ,
60- ) ) ;
61- }
56+ check_fs_capability ( ctx) ?;
6257 let archive_path = args[ 0 ] . try_into_string ( ) ?;
6358 let dest_dir = args[ 1 ] . try_into_string ( ) ?;
6459 unzip_archive ( & archive_path, & dest_dir) . map_err ( |e| {
@@ -79,16 +74,7 @@ impl StdlibRegistry {
7974 name : "tar" . to_string ( ) ,
8075 arity : 2 ,
8176 callback : |ctx, args| {
82- if !ctx. config . capabilities . contains ( & Capability :: FileSystem ) {
83- return Err ( RuntimeError :: new (
84- RuntimeErrorKind :: InvalidOperation (
85- "Security policy violation: FileSystem capability is denied"
86- . to_string ( ) ,
87- ) ,
88- None ,
89- None ,
90- ) ) ;
91- }
77+ check_fs_capability ( ctx) ?;
9278 let src_dir = args[ 0 ] . try_into_string ( ) ?;
9379 let archive_path = args[ 1 ] . try_into_string ( ) ?;
9480 tar_dir ( & src_dir, & archive_path) . map_err ( |e| {
@@ -109,16 +95,7 @@ impl StdlibRegistry {
10995 name : "untar" . to_string ( ) ,
11096 arity : 2 ,
11197 callback : |ctx, args| {
112- if !ctx. config . capabilities . contains ( & Capability :: FileSystem ) {
113- return Err ( RuntimeError :: new (
114- RuntimeErrorKind :: InvalidOperation (
115- "Security policy violation: FileSystem capability is denied"
116- . to_string ( ) ,
117- ) ,
118- None ,
119- None ,
120- ) ) ;
121- }
98+ check_fs_capability ( ctx) ?;
12299 let archive_path = args[ 0 ] . try_into_string ( ) ?;
123100 let dest_dir = args[ 1 ] . try_into_string ( ) ?;
124101 untar_archive ( & archive_path, & dest_dir) . map_err ( |e| {
@@ -139,16 +116,7 @@ impl StdlibRegistry {
139116 name : "gzip" . to_string ( ) ,
140117 arity : 2 ,
141118 callback : |ctx, args| {
142- if !ctx. config . capabilities . contains ( & Capability :: FileSystem ) {
143- return Err ( RuntimeError :: new (
144- RuntimeErrorKind :: InvalidOperation (
145- "Security policy violation: FileSystem capability is denied"
146- . to_string ( ) ,
147- ) ,
148- None ,
149- None ,
150- ) ) ;
151- }
119+ check_fs_capability ( ctx) ?;
152120 let src_file = args[ 0 ] . try_into_string ( ) ?;
153121 let archive_path = args[ 1 ] . try_into_string ( ) ?;
154122 gzip_file ( & src_file, & archive_path) . map_err ( |e| {
@@ -169,16 +137,7 @@ impl StdlibRegistry {
169137 name : "gunzip" . to_string ( ) ,
170138 arity : 2 ,
171139 callback : |ctx, args| {
172- if !ctx. config . capabilities . contains ( & Capability :: FileSystem ) {
173- return Err ( RuntimeError :: new (
174- RuntimeErrorKind :: InvalidOperation (
175- "Security policy violation: FileSystem capability is denied"
176- . to_string ( ) ,
177- ) ,
178- None ,
179- None ,
180- ) ) ;
181- }
140+ check_fs_capability ( ctx) ?;
182141 let archive_path = args[ 0 ] . try_into_string ( ) ?;
183142 let dest_file = args[ 1 ] . try_into_string ( ) ?;
184143 gunzip_archive ( & archive_path, & dest_file) . map_err ( |e| {
0 commit comments