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