@@ -142,13 +142,35 @@ fn os_write(fd: PyIntRef, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
142142 Ok ( vm. ctx . new_int ( written) )
143143}
144144
145- fn os_remove ( path : PyStringRef , vm : & VirtualMachine ) -> PyResult {
146- match fs:: remove_file ( & path. value ) {
147- Ok ( _) => ( ) ,
148- Err ( s) => return Err ( vm. new_os_error ( s. to_string ( ) ) ) ,
149- }
145+ fn os_remove ( path : PyStringRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
146+ fs:: remove_file ( & path. value ) . map_err ( |s| vm. new_os_error ( s. to_string ( ) ) )
147+ }
150148
151- Ok ( vm. get_none ( ) )
149+ fn os_mkdir ( path : PyStringRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
150+ fs:: create_dir ( & path. value ) . map_err ( |s| vm. new_os_error ( s. to_string ( ) ) )
151+ }
152+
153+ fn os_mkdirs ( path : PyStringRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
154+ fs:: create_dir_all ( & path. value ) . map_err ( |s| vm. new_os_error ( s. to_string ( ) ) )
155+ }
156+
157+ fn os_rmdir ( path : PyStringRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
158+ fs:: remove_dir ( & path. value ) . map_err ( |s| vm. new_os_error ( s. to_string ( ) ) )
159+ }
160+
161+ fn os_listdir ( path : PyStringRef , vm : & VirtualMachine ) -> PyResult {
162+ match fs:: read_dir ( & path. value ) {
163+ Ok ( iter) => {
164+ let res: PyResult < Vec < PyObjectRef > > = iter
165+ . map ( |entry| match entry {
166+ Ok ( path) => Ok ( vm. ctx . new_str ( path. file_name ( ) . into_string ( ) . unwrap ( ) ) ) ,
167+ Err ( s) => Err ( vm. new_os_error ( s. to_string ( ) ) ) ,
168+ } )
169+ . collect ( ) ;
170+ Ok ( vm. ctx . new_list ( res?) )
171+ }
172+ Err ( s) => Err ( vm. new_os_error ( s. to_string ( ) ) ) ,
173+ }
152174}
153175
154176pub fn make_module ( vm : & VirtualMachine ) -> PyObjectRef {
@@ -168,6 +190,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
168190 "write" => ctx. new_rustfunc( os_write) ,
169191 "remove" => ctx. new_rustfunc( os_remove) ,
170192 "unlink" => ctx. new_rustfunc( os_remove) ,
193+ "mkdir" => ctx. new_rustfunc( os_mkdir) ,
194+ "mkdirs" => ctx. new_rustfunc( os_mkdirs) ,
195+ "rmdir" => ctx. new_rustfunc( os_rmdir) ,
196+ "listdir" => ctx. new_rustfunc( os_listdir) ,
171197 "name" => ctx. new_str( os_name) ,
172198 "O_RDONLY" => ctx. new_int( 0 ) ,
173199 "O_WRONLY" => ctx. new_int( 1 ) ,
0 commit comments