]> git.proxmox.com Git - rustc.git/blame - library/std/src/sys/hermit/fs.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / library / std / src / sys / hermit / fs.rs
CommitLineData
2b03887a 1use crate::convert::TryFrom;
60c5eb7d 2use crate::ffi::{CStr, CString, OsString};
e74abb32 3use crate::fmt;
e74abb32 4use crate::hash::{Hash, Hasher};
60c5eb7d 5use crate::io::{self, Error, ErrorKind};
f2b60f7d 6use crate::io::{BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
136023e0 7use crate::os::unix::ffi::OsStrExt;
e74abb32 8use crate::path::{Path, PathBuf};
2b03887a 9use crate::sys::common::small_c_string::run_path_with_cstr;
60c5eb7d 10use crate::sys::cvt;
e74abb32 11use crate::sys::hermit::abi;
ba9703b0 12use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
e74abb32 13use crate::sys::hermit::fd::FileDesc;
60c5eb7d 14use crate::sys::time::SystemTime;
cdc7bbd5 15use crate::sys::unsupported;
e74abb32 16
17df50a5 17pub use crate::sys_common::fs::{copy, try_exists};
e74abb32
XL
18//pub use crate::sys_common::fs::remove_dir_all;
19
e74abb32
XL
20#[derive(Debug)]
21pub struct File(FileDesc);
22
cdc7bbd5 23pub struct FileAttr(!);
e74abb32 24
cdc7bbd5 25pub struct ReadDir(!);
e74abb32 26
cdc7bbd5 27pub struct DirEntry(!);
e74abb32
XL
28
29#[derive(Clone, Debug)]
30pub 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
60c5eb7d 39 mode: i32,
e74abb32
XL
40}
41
f2b60f7d
FG
42#[derive(Copy, Clone, Debug, Default)]
43pub struct FileTimes {}
44
cdc7bbd5 45pub struct FilePermissions(!);
e74abb32 46
cdc7bbd5 47pub struct FileType(!);
e74abb32
XL
48
49#[derive(Debug)]
60c5eb7d 50pub struct DirBuilder {}
e74abb32
XL
51
52impl FileAttr {
53 pub fn size(&self) -> u64 {
cdc7bbd5 54 self.0
e74abb32
XL
55 }
56
57 pub fn perm(&self) -> FilePermissions {
cdc7bbd5 58 self.0
e74abb32
XL
59 }
60
61 pub fn file_type(&self) -> FileType {
cdc7bbd5 62 self.0
e74abb32
XL
63 }
64
65 pub fn modified(&self) -> io::Result<SystemTime> {
cdc7bbd5 66 self.0
e74abb32
XL
67 }
68
69 pub fn accessed(&self) -> io::Result<SystemTime> {
cdc7bbd5 70 self.0
e74abb32
XL
71 }
72
73 pub fn created(&self) -> io::Result<SystemTime> {
cdc7bbd5 74 self.0
e74abb32
XL
75 }
76}
77
78impl Clone for FileAttr {
79 fn clone(&self) -> FileAttr {
cdc7bbd5 80 self.0
e74abb32
XL
81 }
82}
83
84impl FilePermissions {
85 pub fn readonly(&self) -> bool {
cdc7bbd5 86 self.0
e74abb32
XL
87 }
88
89 pub fn set_readonly(&mut self, _readonly: bool) {
cdc7bbd5 90 self.0
e74abb32
XL
91 }
92}
93
94impl Clone for FilePermissions {
95 fn clone(&self) -> FilePermissions {
cdc7bbd5 96 self.0
e74abb32
XL
97 }
98}
99
100impl PartialEq for FilePermissions {
101 fn eq(&self, _other: &FilePermissions) -> bool {
cdc7bbd5 102 self.0
e74abb32
XL
103 }
104}
105
60c5eb7d 106impl Eq for FilePermissions {}
e74abb32
XL
107
108impl fmt::Debug for FilePermissions {
109 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
cdc7bbd5 110 self.0
e74abb32
XL
111 }
112}
113
f2b60f7d
FG
114impl FileTimes {
115 pub fn set_accessed(&mut self, _t: SystemTime) {}
116 pub fn set_modified(&mut self, _t: SystemTime) {}
117}
118
e74abb32
XL
119impl FileType {
120 pub fn is_dir(&self) -> bool {
cdc7bbd5 121 self.0
e74abb32
XL
122 }
123
124 pub fn is_file(&self) -> bool {
cdc7bbd5 125 self.0
e74abb32
XL
126 }
127
128 pub fn is_symlink(&self) -> bool {
cdc7bbd5 129 self.0
e74abb32
XL
130 }
131}
132
133impl Clone for FileType {
134 fn clone(&self) -> FileType {
cdc7bbd5 135 self.0
e74abb32
XL
136 }
137}
138
139impl Copy for FileType {}
140
141impl PartialEq for FileType {
142 fn eq(&self, _other: &FileType) -> bool {
cdc7bbd5 143 self.0
e74abb32
XL
144 }
145}
146
60c5eb7d 147impl Eq for FileType {}
e74abb32
XL
148
149impl Hash for FileType {
150 fn hash<H: Hasher>(&self, _h: &mut H) {
cdc7bbd5 151 self.0
e74abb32
XL
152 }
153}
154
155impl fmt::Debug for FileType {
156 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
cdc7bbd5 157 self.0
e74abb32
XL
158 }
159}
160
161impl fmt::Debug for ReadDir {
162 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
cdc7bbd5 163 self.0
e74abb32
XL
164 }
165}
166
167impl Iterator for ReadDir {
168 type Item = io::Result<DirEntry>;
169
170 fn next(&mut self) -> Option<io::Result<DirEntry>> {
cdc7bbd5 171 self.0
e74abb32
XL
172 }
173}
174
175impl DirEntry {
176 pub fn path(&self) -> PathBuf {
cdc7bbd5 177 self.0
e74abb32
XL
178 }
179
180 pub fn file_name(&self) -> OsString {
cdc7bbd5 181 self.0
e74abb32
XL
182 }
183
184 pub fn metadata(&self) -> io::Result<FileAttr> {
cdc7bbd5 185 self.0
e74abb32
XL
186 }
187
188 pub fn file_type(&self) -> io::Result<FileType> {
cdc7bbd5 189 self.0
e74abb32
XL
190 }
191}
192
193impl 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
60c5eb7d 204 mode: 0x777,
e74abb32
XL
205 }
206 }
207
60c5eb7d
XL
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 }
e74abb32
XL
226
227 fn get_access_mode(&self) -> io::Result<i32> {
228 match (self.read, self.write, self.append) {
60c5eb7d
XL
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),
e74abb32 234 (false, false, false) => {
5099ac24 235 Err(io::const_io_error!(ErrorKind::InvalidInput, "invalid access mode"))
60c5eb7d 236 }
e74abb32
XL
237 }
238 }
239
240 fn get_creation_mode(&self) -> io::Result<i32> {
241 match (self.write, self.append) {
242 (true, false) => {}
60c5eb7d 243 (false, false) => {
e74abb32 244 if self.truncate || self.create || self.create_new {
5099ac24 245 return Err(io::const_io_error!(
cdc7bbd5 246 ErrorKind::InvalidInput,
5099ac24 247 "invalid creation mode",
cdc7bbd5 248 ));
60c5eb7d
XL
249 }
250 }
251 (_, true) => {
e74abb32 252 if self.truncate && !self.create_new {
5099ac24 253 return Err(io::const_io_error!(
cdc7bbd5 254 ErrorKind::InvalidInput,
5099ac24 255 "invalid creation mode",
cdc7bbd5 256 ));
60c5eb7d
XL
257 }
258 }
e74abb32
XL
259 }
260
261 Ok(match (self.create, self.truncate, self.create_new) {
60c5eb7d
XL
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 })
e74abb32
XL
268 }
269}
270
271impl File {
272 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
2b03887a 273 run_path_with_cstr(path, |path| File::open_c(&path, opts))
e74abb32
XL
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
f9f354fc
XL
315 #[inline]
316 pub fn is_read_vectored(&self) -> bool {
317 false
318 }
319
f2b60f7d
FG
320 pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
321 crate::io::default_read_buf(|buf| self.read(buf), cursor)
a2a8927a
XL
322 }
323
e74abb32
XL
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
f9f354fc
XL
332 #[inline]
333 pub fn is_write_vectored(&self) -> bool {
334 false
335 }
336
e74abb32
XL
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 }
f2b60f7d
FG
352
353 pub fn set_times(&self, _times: FileTimes) -> io::Result<()> {
354 Err(Error::from_raw_os_error(22))
355 }
e74abb32
XL
356}
357
358impl DirBuilder {
359 pub fn new() -> DirBuilder {
60c5eb7d 360 DirBuilder {}
e74abb32
XL
361 }
362
363 pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
364 unsupported()
365 }
366}
367
368pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
369 unsupported()
370}
371
372pub fn unlink(path: &Path) -> io::Result<()> {
2b03887a 373 run_path_with_cstr(path, |path| cvt(unsafe { abi::unlink(path.as_ptr()) }).map(|_| ()))
e74abb32
XL
374}
375
376pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
377 unsupported()
378}
379
380pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> {
381 match perm.0 {}
382}
383
384pub fn rmdir(_p: &Path) -> io::Result<()> {
385 unsupported()
386}
387
388pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
389 //unsupported()
390 Ok(())
391}
392
393pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
394 unsupported()
395}
396
fc512014 397pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
e74abb32
XL
398 unsupported()
399}
400
fc512014 401pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
e74abb32
XL
402 unsupported()
403}
404
405pub fn stat(_p: &Path) -> io::Result<FileAttr> {
406 unsupported()
407}
408
409pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
410 unsupported()
411}
412
413pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
414 unsupported()
415}