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