@@ -3,13 +3,14 @@ use crate::*;
3
3
use serde:: { de, Deserialize , Serialize } ;
4
4
use std:: convert:: TryInto ;
5
5
use std:: fs;
6
+ use std:: io:: { self , Read , Seek , Write } ;
6
7
use std:: io:: { Read , Seek , Write } ;
7
8
#[ cfg( unix) ]
8
9
use std:: os:: unix:: io:: { AsRawFd , RawFd } ;
9
10
#[ cfg( windows) ]
10
11
use std:: os:: windows:: io:: { AsRawHandle , RawHandle } ;
11
12
use std:: path:: { Path , PathBuf } ;
12
- use std:: time:: SystemTime ;
13
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
13
14
14
15
impl FileDescriptor {
15
16
#[ cfg( unix) ]
@@ -69,17 +70,17 @@ pub struct HostFileSystem;
69
70
70
71
impl FileSystem for HostFileSystem {
71
72
fn read_dir ( & self , path : & Path ) -> Result < ReadDir , FsError > {
72
- let read_dir = std :: fs:: read_dir ( path) ?;
73
+ let read_dir = fs:: read_dir ( path) ?;
73
74
let data = read_dir
74
75
. map ( |entry| -> Result < DirEntry , _ > {
75
76
let entry = entry?;
76
77
let metadata = entry. metadata ( ) ?;
77
78
Ok ( DirEntry {
78
79
path : entry. path ( ) ,
79
- metadata : Ok ( fs_metadata_to_metadata ( metadata) ?) ,
80
+ metadata : Ok ( metadata. try_into ( ) ?) ,
80
81
} )
81
82
} )
82
- . collect :: < Result < Vec < DirEntry > , std :: io:: Error > > ( )
83
+ . collect :: < Result < Vec < DirEntry > , io:: Error > > ( )
83
84
. map_err :: < FsError , _ > ( Into :: into) ?;
84
85
Ok ( ReadDir :: new ( data) )
85
86
}
@@ -103,58 +104,61 @@ impl FileSystem for HostFileSystem {
103
104
104
105
fn metadata ( & self , path : & Path ) -> Result < Metadata , FsError > {
105
106
fs:: metadata ( path)
106
- . and_then ( fs_metadata_to_metadata )
107
+ . and_then ( TryInto :: try_into )
107
108
. map_err ( Into :: into)
108
109
}
109
110
}
110
111
111
- fn fs_metadata_to_metadata ( metadata : fs:: Metadata ) -> Result < Metadata , std:: io:: Error > {
112
- use std:: time:: UNIX_EPOCH ;
113
- let filetype = metadata. file_type ( ) ;
114
- let ( char_device, block_device, socket, fifo) = {
115
- #[ cfg( unix) ]
116
- {
117
- use std:: os:: unix:: fs:: FileTypeExt ;
118
- (
119
- filetype. is_char_device ( ) ,
120
- filetype. is_block_device ( ) ,
121
- filetype. is_socket ( ) ,
122
- filetype. is_fifo ( ) ,
123
- )
124
- }
125
- #[ cfg( not( unix) ) ]
126
- {
127
- ( false , false , false , false )
128
- }
129
- } ;
130
-
131
- Ok ( Metadata {
132
- ft : FileType {
133
- dir : filetype. is_dir ( ) ,
134
- file : filetype. is_file ( ) ,
135
- symlink : filetype. is_symlink ( ) ,
136
- char_device,
137
- block_device,
138
- socket,
139
- fifo,
140
- } ,
141
- accessed : metadata
142
- . accessed ( ) ?
143
- . duration_since ( UNIX_EPOCH )
144
- . unwrap ( )
145
- . as_nanos ( ) as u64 ,
146
- created : metadata
147
- . created ( ) ?
148
- . duration_since ( UNIX_EPOCH )
149
- . unwrap ( )
150
- . as_nanos ( ) as u64 ,
151
- modified : metadata
152
- . modified ( ) ?
153
- . duration_since ( UNIX_EPOCH )
154
- . unwrap ( )
155
- . as_nanos ( ) as u64 ,
156
- len : metadata. len ( ) ,
157
- } )
112
+ impl TryInto < Metadata > for fs:: Metadata {
113
+ type Error = io:: Error ;
114
+
115
+ fn try_into ( self ) -> Result < Metadata , Self :: Error > {
116
+ let filetype = self . file_type ( ) ;
117
+ let ( char_device, block_device, socket, fifo) = {
118
+ #[ cfg( unix) ]
119
+ {
120
+ use std:: os:: unix:: fs:: FileTypeExt ;
121
+ (
122
+ filetype. is_char_device ( ) ,
123
+ filetype. is_block_device ( ) ,
124
+ filetype. is_socket ( ) ,
125
+ filetype. is_fifo ( ) ,
126
+ )
127
+ }
128
+ #[ cfg( not( unix) ) ]
129
+ {
130
+ ( false , false , false , false )
131
+ }
132
+ } ;
133
+
134
+ Ok ( Metadata {
135
+ ft : FileType {
136
+ dir : filetype. is_dir ( ) ,
137
+ file : filetype. is_file ( ) ,
138
+ symlink : filetype. is_symlink ( ) ,
139
+ char_device,
140
+ block_device,
141
+ socket,
142
+ fifo,
143
+ } ,
144
+ accessed : self
145
+ . accessed ( ) ?
146
+ . duration_since ( UNIX_EPOCH )
147
+ . unwrap ( )
148
+ . as_nanos ( ) as u64 ,
149
+ created : self
150
+ . created ( ) ?
151
+ . duration_since ( UNIX_EPOCH )
152
+ . unwrap ( )
153
+ . as_nanos ( ) as u64 ,
154
+ modified : self
155
+ . modified ( ) ?
156
+ . duration_since ( UNIX_EPOCH )
157
+ . unwrap ( )
158
+ . as_nanos ( ) as u64 ,
159
+ len : self . len ( ) ,
160
+ } )
161
+ }
158
162
}
159
163
160
164
#[ derive( Debug , Clone ) ]
@@ -170,7 +174,7 @@ impl FileOpener for HostFileOpener {
170
174
let read = conf. read ( ) ;
171
175
let write = conf. write ( ) ;
172
176
let append = conf. append ( ) ;
173
- let mut oo = std :: fs:: OpenOptions :: new ( ) ;
177
+ let mut oo = fs:: OpenOptions :: new ( ) ;
174
178
oo. read ( conf. read ( ) )
175
179
. write ( conf. write ( ) )
176
180
. create_new ( conf. create_new ( ) )
@@ -228,7 +232,7 @@ impl<'de> Deserialize<'de> for HostFile {
228
232
let flags = seq
229
233
. next_element ( ) ?
230
234
. ok_or_else ( || de:: Error :: invalid_length ( 1 , & self ) ) ?;
231
- let inner = std :: fs:: OpenOptions :: new ( )
235
+ let inner = fs:: OpenOptions :: new ( )
232
236
. read ( flags & HostFile :: READ != 0 )
233
237
. write ( flags & HostFile :: WRITE != 0 )
234
238
. append ( flags & HostFile :: APPEND != 0 )
@@ -265,7 +269,7 @@ impl<'de> Deserialize<'de> for HostFile {
265
269
}
266
270
let host_path = host_path. ok_or_else ( || de:: Error :: missing_field ( "host_path" ) ) ?;
267
271
let flags = flags. ok_or_else ( || de:: Error :: missing_field ( "flags" ) ) ?;
268
- let inner = std :: fs:: OpenOptions :: new ( )
272
+ let inner = fs:: OpenOptions :: new ( )
269
273
. read ( flags & HostFile :: READ != 0 )
270
274
. write ( flags & HostFile :: WRITE != 0 )
271
275
. append ( flags & HostFile :: APPEND != 0 )
@@ -385,7 +389,7 @@ impl VirtualFile for HostFile {
385
389
}
386
390
387
391
fn unlink ( & mut self ) -> Result < ( ) , FsError > {
388
- std :: fs:: remove_file ( & self . host_path ) . map_err ( Into :: into)
392
+ fs:: remove_file ( & self . host_path ) . map_err ( Into :: into)
389
393
}
390
394
fn sync_to_disk ( & self ) -> Result < ( ) , FsError > {
391
395
self . inner . sync_all ( ) . map_err ( Into :: into)
0 commit comments