]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/hermit/fs.rs
New upstream version 1.60.0+dfsg1
[rustc.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::{IoSlice, IoSliceMut, ReadBuf, 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 pub struct FilePermissions(!);
45
46 pub struct FileType(!);
47
48 #[derive(Debug)]
49 pub struct DirBuilder {}
50
51 impl FileAttr {
52 pub fn size(&self) -> u64 {
53 self.0
54 }
55
56 pub fn perm(&self) -> FilePermissions {
57 self.0
58 }
59
60 pub fn file_type(&self) -> FileType {
61 self.0
62 }
63
64 pub fn modified(&self) -> io::Result<SystemTime> {
65 self.0
66 }
67
68 pub fn accessed(&self) -> io::Result<SystemTime> {
69 self.0
70 }
71
72 pub fn created(&self) -> io::Result<SystemTime> {
73 self.0
74 }
75 }
76
77 impl Clone for FileAttr {
78 fn clone(&self) -> FileAttr {
79 self.0
80 }
81 }
82
83 impl FilePermissions {
84 pub fn readonly(&self) -> bool {
85 self.0
86 }
87
88 pub fn set_readonly(&mut self, _readonly: bool) {
89 self.0
90 }
91 }
92
93 impl Clone for FilePermissions {
94 fn clone(&self) -> FilePermissions {
95 self.0
96 }
97 }
98
99 impl PartialEq for FilePermissions {
100 fn eq(&self, _other: &FilePermissions) -> bool {
101 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 self.0
110 }
111 }
112
113 impl FileType {
114 pub fn is_dir(&self) -> bool {
115 self.0
116 }
117
118 pub fn is_file(&self) -> bool {
119 self.0
120 }
121
122 pub fn is_symlink(&self) -> bool {
123 self.0
124 }
125 }
126
127 impl Clone for FileType {
128 fn clone(&self) -> FileType {
129 self.0
130 }
131 }
132
133 impl Copy for FileType {}
134
135 impl PartialEq for FileType {
136 fn eq(&self, _other: &FileType) -> bool {
137 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 self.0
146 }
147 }
148
149 impl fmt::Debug for FileType {
150 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 self.0
152 }
153 }
154
155 impl fmt::Debug for ReadDir {
156 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 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 self.0
166 }
167 }
168
169 impl DirEntry {
170 pub fn path(&self) -> PathBuf {
171 self.0
172 }
173
174 pub fn file_name(&self) -> OsString {
175 self.0
176 }
177
178 pub fn metadata(&self) -> io::Result<FileAttr> {
179 self.0
180 }
181
182 pub fn file_type(&self) -> io::Result<FileType> {
183 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::const_io_error!(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::const_io_error!(
240 ErrorKind::InvalidInput,
241 "invalid creation mode",
242 ));
243 }
244 }
245 (_, true) => {
246 if self.truncate && !self.create_new {
247 return Err(io::const_io_error!(
248 ErrorKind::InvalidInput,
249 "invalid creation mode",
250 ));
251 }
252 }
253 }
254
255 Ok(match (self.create, self.truncate, self.create_new) {
256 (false, false, false) => 0,
257 (true, false, false) => O_CREAT,
258 (false, true, false) => O_TRUNC,
259 (true, true, false) => O_CREAT | O_TRUNC,
260 (_, _, true) => O_CREAT | O_EXCL,
261 })
262 }
263 }
264
265 impl File {
266 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
267 let path = cstr(path)?;
268 File::open_c(&path, opts)
269 }
270
271 pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
272 let mut flags = opts.get_access_mode()?;
273 flags = flags | opts.get_creation_mode()?;
274
275 let mode;
276 if flags & O_CREAT == O_CREAT {
277 mode = opts.mode;
278 } else {
279 mode = 0;
280 }
281
282 let fd = unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? };
283 Ok(File(FileDesc::new(fd as i32)))
284 }
285
286 pub fn file_attr(&self) -> io::Result<FileAttr> {
287 Err(Error::from_raw_os_error(22))
288 }
289
290 pub fn fsync(&self) -> io::Result<()> {
291 Err(Error::from_raw_os_error(22))
292 }
293
294 pub fn datasync(&self) -> io::Result<()> {
295 self.fsync()
296 }
297
298 pub fn truncate(&self, _size: u64) -> io::Result<()> {
299 Err(Error::from_raw_os_error(22))
300 }
301
302 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
303 self.0.read(buf)
304 }
305
306 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
307 crate::io::default_read_vectored(|buf| self.read(buf), bufs)
308 }
309
310 #[inline]
311 pub fn is_read_vectored(&self) -> bool {
312 false
313 }
314
315 pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
316 crate::io::default_read_buf(|buf| self.read(buf), buf)
317 }
318
319 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
320 self.0.write(buf)
321 }
322
323 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
324 crate::io::default_write_vectored(|buf| self.write(buf), bufs)
325 }
326
327 #[inline]
328 pub fn is_write_vectored(&self) -> bool {
329 false
330 }
331
332 pub fn flush(&self) -> io::Result<()> {
333 Ok(())
334 }
335
336 pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> {
337 Err(Error::from_raw_os_error(22))
338 }
339
340 pub fn duplicate(&self) -> io::Result<File> {
341 Err(Error::from_raw_os_error(22))
342 }
343
344 pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> {
345 Err(Error::from_raw_os_error(22))
346 }
347 }
348
349 impl DirBuilder {
350 pub fn new() -> DirBuilder {
351 DirBuilder {}
352 }
353
354 pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
355 unsupported()
356 }
357 }
358
359 pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
360 unsupported()
361 }
362
363 pub fn unlink(path: &Path) -> io::Result<()> {
364 let name = cstr(path)?;
365 let _ = unsafe { cvt(abi::unlink(name.as_ptr()))? };
366 Ok(())
367 }
368
369 pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
370 unsupported()
371 }
372
373 pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> {
374 match perm.0 {}
375 }
376
377 pub fn rmdir(_p: &Path) -> io::Result<()> {
378 unsupported()
379 }
380
381 pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
382 //unsupported()
383 Ok(())
384 }
385
386 pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
387 unsupported()
388 }
389
390 pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
391 unsupported()
392 }
393
394 pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
395 unsupported()
396 }
397
398 pub fn stat(_p: &Path) -> io::Result<FileAttr> {
399 unsupported()
400 }
401
402 pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
403 unsupported()
404 }
405
406 pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
407 unsupported()
408 }