]>
Commit | Line | Data |
---|---|---|
1 | use crate::ffi::{CStr, CString, OsString}; | |
2 | use crate::fmt; | |
3 | use crate::hash::{Hash, Hasher}; | |
4 | use crate::io::{self, Error, ErrorKind}; | |
5 | use crate::io::{IoSlice, IoSliceMut, SeekFrom}; | |
6 | use crate::path::{Path, PathBuf}; | |
7 | use crate::sys::cvt; | |
8 | use crate::sys::hermit::abi; | |
9 | use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY}; | |
10 | use crate::sys::hermit::fd::FileDesc; | |
11 | use crate::sys::time::SystemTime; | |
12 | use crate::sys::{unsupported, Void}; | |
13 | use crate::sys_common::os_str_bytes::OsStrExt; | |
14 | ||
15 | pub use crate::sys_common::fs::copy; | |
16 | //pub use crate::sys_common::fs::remove_dir_all; | |
17 | ||
18 | fn cstr(path: &Path) -> io::Result<CString> { | |
19 | Ok(CString::new(path.as_os_str().as_bytes())?) | |
20 | } | |
21 | ||
22 | #[derive(Debug)] | |
23 | pub struct File(FileDesc); | |
24 | ||
25 | pub struct FileAttr(Void); | |
26 | ||
27 | pub struct ReadDir(Void); | |
28 | ||
29 | pub struct DirEntry(Void); | |
30 | ||
31 | #[derive(Clone, Debug)] | |
32 | pub struct OpenOptions { | |
33 | // generic | |
34 | read: bool, | |
35 | write: bool, | |
36 | append: bool, | |
37 | truncate: bool, | |
38 | create: bool, | |
39 | create_new: bool, | |
40 | // system-specific | |
41 | mode: i32, | |
42 | } | |
43 | ||
44 | pub struct FilePermissions(Void); | |
45 | ||
46 | pub struct FileType(Void); | |
47 | ||
48 | #[derive(Debug)] | |
49 | pub struct DirBuilder {} | |
50 | ||
51 | impl FileAttr { | |
52 | pub fn size(&self) -> u64 { | |
53 | match self.0 {} | |
54 | } | |
55 | ||
56 | pub fn perm(&self) -> FilePermissions { | |
57 | match self.0 {} | |
58 | } | |
59 | ||
60 | pub fn file_type(&self) -> FileType { | |
61 | match self.0 {} | |
62 | } | |
63 | ||
64 | pub fn modified(&self) -> io::Result<SystemTime> { | |
65 | match self.0 {} | |
66 | } | |
67 | ||
68 | pub fn accessed(&self) -> io::Result<SystemTime> { | |
69 | match self.0 {} | |
70 | } | |
71 | ||
72 | pub fn created(&self) -> io::Result<SystemTime> { | |
73 | match self.0 {} | |
74 | } | |
75 | } | |
76 | ||
77 | impl Clone for FileAttr { | |
78 | fn clone(&self) -> FileAttr { | |
79 | match self.0 {} | |
80 | } | |
81 | } | |
82 | ||
83 | impl FilePermissions { | |
84 | pub fn readonly(&self) -> bool { | |
85 | match self.0 {} | |
86 | } | |
87 | ||
88 | pub fn set_readonly(&mut self, _readonly: bool) { | |
89 | match self.0 {} | |
90 | } | |
91 | } | |
92 | ||
93 | impl Clone for FilePermissions { | |
94 | fn clone(&self) -> FilePermissions { | |
95 | match self.0 {} | |
96 | } | |
97 | } | |
98 | ||
99 | impl PartialEq for FilePermissions { | |
100 | fn eq(&self, _other: &FilePermissions) -> bool { | |
101 | match self.0 {} | |
102 | } | |
103 | } | |
104 | ||
105 | impl Eq for FilePermissions {} | |
106 | ||
107 | impl fmt::Debug for FilePermissions { | |
108 | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
109 | match self.0 {} | |
110 | } | |
111 | } | |
112 | ||
113 | impl FileType { | |
114 | pub fn is_dir(&self) -> bool { | |
115 | match self.0 {} | |
116 | } | |
117 | ||
118 | pub fn is_file(&self) -> bool { | |
119 | match self.0 {} | |
120 | } | |
121 | ||
122 | pub fn is_symlink(&self) -> bool { | |
123 | match self.0 {} | |
124 | } | |
125 | } | |
126 | ||
127 | impl Clone for FileType { | |
128 | fn clone(&self) -> FileType { | |
129 | match self.0 {} | |
130 | } | |
131 | } | |
132 | ||
133 | impl Copy for FileType {} | |
134 | ||
135 | impl PartialEq for FileType { | |
136 | fn eq(&self, _other: &FileType) -> bool { | |
137 | match self.0 {} | |
138 | } | |
139 | } | |
140 | ||
141 | impl Eq for FileType {} | |
142 | ||
143 | impl Hash for FileType { | |
144 | fn hash<H: Hasher>(&self, _h: &mut H) { | |
145 | match self.0 {} | |
146 | } | |
147 | } | |
148 | ||
149 | impl fmt::Debug for FileType { | |
150 | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
151 | match self.0 {} | |
152 | } | |
153 | } | |
154 | ||
155 | impl fmt::Debug for ReadDir { | |
156 | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
157 | match self.0 {} | |
158 | } | |
159 | } | |
160 | ||
161 | impl Iterator for ReadDir { | |
162 | type Item = io::Result<DirEntry>; | |
163 | ||
164 | fn next(&mut self) -> Option<io::Result<DirEntry>> { | |
165 | match self.0 {} | |
166 | } | |
167 | } | |
168 | ||
169 | impl DirEntry { | |
170 | pub fn path(&self) -> PathBuf { | |
171 | match self.0 {} | |
172 | } | |
173 | ||
174 | pub fn file_name(&self) -> OsString { | |
175 | match self.0 {} | |
176 | } | |
177 | ||
178 | pub fn metadata(&self) -> io::Result<FileAttr> { | |
179 | match self.0 {} | |
180 | } | |
181 | ||
182 | pub fn file_type(&self) -> io::Result<FileType> { | |
183 | match self.0 {} | |
184 | } | |
185 | } | |
186 | ||
187 | impl OpenOptions { | |
188 | pub fn new() -> OpenOptions { | |
189 | OpenOptions { | |
190 | // generic | |
191 | read: false, | |
192 | write: false, | |
193 | append: false, | |
194 | truncate: false, | |
195 | create: false, | |
196 | create_new: false, | |
197 | // system-specific | |
198 | mode: 0x777, | |
199 | } | |
200 | } | |
201 | ||
202 | pub fn read(&mut self, read: bool) { | |
203 | self.read = read; | |
204 | } | |
205 | pub fn write(&mut self, write: bool) { | |
206 | self.write = write; | |
207 | } | |
208 | pub fn append(&mut self, append: bool) { | |
209 | self.append = append; | |
210 | } | |
211 | pub fn truncate(&mut self, truncate: bool) { | |
212 | self.truncate = truncate; | |
213 | } | |
214 | pub fn create(&mut self, create: bool) { | |
215 | self.create = create; | |
216 | } | |
217 | pub fn create_new(&mut self, create_new: bool) { | |
218 | self.create_new = create_new; | |
219 | } | |
220 | ||
221 | fn get_access_mode(&self) -> io::Result<i32> { | |
222 | match (self.read, self.write, self.append) { | |
223 | (true, false, false) => Ok(O_RDONLY), | |
224 | (false, true, false) => Ok(O_WRONLY), | |
225 | (true, true, false) => Ok(O_RDWR), | |
226 | (false, _, true) => Ok(O_WRONLY | O_APPEND), | |
227 | (true, _, true) => Ok(O_RDWR | O_APPEND), | |
228 | (false, false, false) => { | |
229 | Err(io::Error::new(ErrorKind::InvalidInput, "invalid access mode")) | |
230 | } | |
231 | } | |
232 | } | |
233 | ||
234 | fn get_creation_mode(&self) -> io::Result<i32> { | |
235 | match (self.write, self.append) { | |
236 | (true, false) => {} | |
237 | (false, false) => { | |
238 | if self.truncate || self.create || self.create_new { | |
239 | return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode")); | |
240 | } | |
241 | } | |
242 | (_, true) => { | |
243 | if self.truncate && !self.create_new { | |
244 | return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode")); | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | Ok(match (self.create, self.truncate, self.create_new) { | |
250 | (false, false, false) => 0, | |
251 | (true, false, false) => O_CREAT, | |
252 | (false, true, false) => O_TRUNC, | |
253 | (true, true, false) => O_CREAT | O_TRUNC, | |
254 | (_, _, true) => O_CREAT | O_EXCL, | |
255 | }) | |
256 | } | |
257 | } | |
258 | ||
259 | impl File { | |
260 | pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> { | |
261 | let path = cstr(path)?; | |
262 | File::open_c(&path, opts) | |
263 | } | |
264 | ||
265 | pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> { | |
266 | let mut flags = opts.get_access_mode()?; | |
267 | flags = flags | opts.get_creation_mode()?; | |
268 | ||
269 | let mode; | |
270 | if flags & O_CREAT == O_CREAT { | |
271 | mode = opts.mode; | |
272 | } else { | |
273 | mode = 0; | |
274 | } | |
275 | ||
276 | let fd = unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? }; | |
277 | Ok(File(FileDesc::new(fd as i32))) | |
278 | } | |
279 | ||
280 | pub fn file_attr(&self) -> io::Result<FileAttr> { | |
281 | Err(Error::from_raw_os_error(22)) | |
282 | } | |
283 | ||
284 | pub fn fsync(&self) -> io::Result<()> { | |
285 | Err(Error::from_raw_os_error(22)) | |
286 | } | |
287 | ||
288 | pub fn datasync(&self) -> io::Result<()> { | |
289 | self.fsync() | |
290 | } | |
291 | ||
292 | pub fn truncate(&self, _size: u64) -> io::Result<()> { | |
293 | Err(Error::from_raw_os_error(22)) | |
294 | } | |
295 | ||
296 | pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { | |
297 | self.0.read(buf) | |
298 | } | |
299 | ||
300 | pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { | |
301 | crate::io::default_read_vectored(|buf| self.read(buf), bufs) | |
302 | } | |
303 | ||
304 | #[inline] | |
305 | pub fn is_read_vectored(&self) -> bool { | |
306 | false | |
307 | } | |
308 | ||
309 | pub fn write(&self, buf: &[u8]) -> io::Result<usize> { | |
310 | self.0.write(buf) | |
311 | } | |
312 | ||
313 | pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { | |
314 | crate::io::default_write_vectored(|buf| self.write(buf), bufs) | |
315 | } | |
316 | ||
317 | #[inline] | |
318 | pub fn is_write_vectored(&self) -> bool { | |
319 | false | |
320 | } | |
321 | ||
322 | pub fn flush(&self) -> io::Result<()> { | |
323 | Ok(()) | |
324 | } | |
325 | ||
326 | pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> { | |
327 | Err(Error::from_raw_os_error(22)) | |
328 | } | |
329 | ||
330 | pub fn duplicate(&self) -> io::Result<File> { | |
331 | Err(Error::from_raw_os_error(22)) | |
332 | } | |
333 | ||
334 | pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> { | |
335 | Err(Error::from_raw_os_error(22)) | |
336 | } | |
337 | ||
338 | pub fn diverge(&self) -> ! { | |
339 | loop {} | |
340 | } | |
341 | } | |
342 | ||
343 | impl DirBuilder { | |
344 | pub fn new() -> DirBuilder { | |
345 | DirBuilder {} | |
346 | } | |
347 | ||
348 | pub fn mkdir(&self, _p: &Path) -> io::Result<()> { | |
349 | unsupported() | |
350 | } | |
351 | } | |
352 | ||
353 | pub fn readdir(_p: &Path) -> io::Result<ReadDir> { | |
354 | unsupported() | |
355 | } | |
356 | ||
357 | pub fn unlink(path: &Path) -> io::Result<()> { | |
358 | let name = cstr(path)?; | |
359 | let _ = unsafe { cvt(abi::unlink(name.as_ptr()))? }; | |
360 | Ok(()) | |
361 | } | |
362 | ||
363 | pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> { | |
364 | unsupported() | |
365 | } | |
366 | ||
367 | pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> { | |
368 | match perm.0 {} | |
369 | } | |
370 | ||
371 | pub fn rmdir(_p: &Path) -> io::Result<()> { | |
372 | unsupported() | |
373 | } | |
374 | ||
375 | pub fn remove_dir_all(_path: &Path) -> io::Result<()> { | |
376 | //unsupported() | |
377 | Ok(()) | |
378 | } | |
379 | ||
380 | pub fn readlink(_p: &Path) -> io::Result<PathBuf> { | |
381 | unsupported() | |
382 | } | |
383 | ||
384 | pub fn symlink(_src: &Path, _dst: &Path) -> io::Result<()> { | |
385 | unsupported() | |
386 | } | |
387 | ||
388 | pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> { | |
389 | unsupported() | |
390 | } | |
391 | ||
392 | pub fn stat(_p: &Path) -> io::Result<FileAttr> { | |
393 | unsupported() | |
394 | } | |
395 | ||
396 | pub fn lstat(_p: &Path) -> io::Result<FileAttr> { | |
397 | unsupported() | |
398 | } | |
399 | ||
400 | pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> { | |
401 | unsupported() | |
402 | } |