]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/hermit/fs.rs
New upstream version 1.45.0+dfsg1
[rustc.git] / src / libstd / sys / hermit / fs.rs
CommitLineData
60c5eb7d 1use crate::ffi::{CStr, CString, OsString};
e74abb32 2use crate::fmt;
e74abb32 3use crate::hash::{Hash, Hasher};
60c5eb7d
XL
4use crate::io::{self, Error, ErrorKind};
5use crate::io::{IoSlice, IoSliceMut, SeekFrom};
e74abb32 6use crate::path::{Path, PathBuf};
60c5eb7d 7use crate::sys::cvt;
e74abb32 8use crate::sys::hermit::abi;
ba9703b0 9use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
e74abb32 10use crate::sys::hermit::fd::FileDesc;
60c5eb7d
XL
11use crate::sys::time::SystemTime;
12use crate::sys::{unsupported, Void};
e74abb32
XL
13use crate::sys_common::os_str_bytes::OsStrExt;
14
15pub use crate::sys_common::fs::copy;
16//pub use crate::sys_common::fs::remove_dir_all;
17
18fn cstr(path: &Path) -> io::Result<CString> {
19 Ok(CString::new(path.as_os_str().as_bytes())?)
20}
e74abb32
XL
21
22#[derive(Debug)]
23pub struct File(FileDesc);
24
25pub struct FileAttr(Void);
26
27pub struct ReadDir(Void);
28
29pub struct DirEntry(Void);
30
31#[derive(Clone, Debug)]
32pub 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
60c5eb7d 41 mode: i32,
e74abb32
XL
42}
43
44pub struct FilePermissions(Void);
45
46pub struct FileType(Void);
47
48#[derive(Debug)]
60c5eb7d 49pub struct DirBuilder {}
e74abb32
XL
50
51impl 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
77impl Clone for FileAttr {
78 fn clone(&self) -> FileAttr {
79 match self.0 {}
80 }
81}
82
83impl 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
93impl Clone for FilePermissions {
94 fn clone(&self) -> FilePermissions {
95 match self.0 {}
96 }
97}
98
99impl PartialEq for FilePermissions {
100 fn eq(&self, _other: &FilePermissions) -> bool {
101 match self.0 {}
102 }
103}
104
60c5eb7d 105impl Eq for FilePermissions {}
e74abb32
XL
106
107impl fmt::Debug for FilePermissions {
108 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 match self.0 {}
110 }
111}
112
113impl 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
127impl Clone for FileType {
128 fn clone(&self) -> FileType {
129 match self.0 {}
130 }
131}
132
133impl Copy for FileType {}
134
135impl PartialEq for FileType {
136 fn eq(&self, _other: &FileType) -> bool {
137 match self.0 {}
138 }
139}
140
60c5eb7d 141impl Eq for FileType {}
e74abb32
XL
142
143impl Hash for FileType {
144 fn hash<H: Hasher>(&self, _h: &mut H) {
145 match self.0 {}
146 }
147}
148
149impl fmt::Debug for FileType {
150 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 match self.0 {}
152 }
153}
154
155impl fmt::Debug for ReadDir {
156 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 match self.0 {}
158 }
159}
160
161impl 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
169impl 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
187impl 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
60c5eb7d 198 mode: 0x777,
e74abb32
XL
199 }
200 }
201
60c5eb7d
XL
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 }
e74abb32
XL
220
221 fn get_access_mode(&self) -> io::Result<i32> {
222 match (self.read, self.write, self.append) {
60c5eb7d
XL
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),
e74abb32
XL
228 (false, false, false) => {
229 Err(io::Error::new(ErrorKind::InvalidInput, "invalid access mode"))
60c5eb7d 230 }
e74abb32
XL
231 }
232 }
233
234 fn get_creation_mode(&self) -> io::Result<i32> {
235 match (self.write, self.append) {
236 (true, false) => {}
60c5eb7d 237 (false, false) => {
e74abb32
XL
238 if self.truncate || self.create || self.create_new {
239 return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
60c5eb7d
XL
240 }
241 }
242 (_, true) => {
e74abb32
XL
243 if self.truncate && !self.create_new {
244 return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
60c5eb7d
XL
245 }
246 }
e74abb32
XL
247 }
248
249 Ok(match (self.create, self.truncate, self.create_new) {
60c5eb7d
XL
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 })
e74abb32
XL
256 }
257}
258
259impl 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
f9f354fc
XL
304 #[inline]
305 pub fn is_read_vectored(&self) -> bool {
306 false
307 }
308
e74abb32
XL
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
f9f354fc
XL
317 #[inline]
318 pub fn is_write_vectored(&self) -> bool {
319 false
320 }
321
e74abb32
XL
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
343impl DirBuilder {
344 pub fn new() -> DirBuilder {
60c5eb7d 345 DirBuilder {}
e74abb32
XL
346 }
347
348 pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
349 unsupported()
350 }
351}
352
353pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
354 unsupported()
355}
356
357pub fn unlink(path: &Path) -> io::Result<()> {
358 let name = cstr(path)?;
359 let _ = unsafe { cvt(abi::unlink(name.as_ptr()))? };
360 Ok(())
361}
362
363pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
364 unsupported()
365}
366
367pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> {
368 match perm.0 {}
369}
370
371pub fn rmdir(_p: &Path) -> io::Result<()> {
372 unsupported()
373}
374
375pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
376 //unsupported()
377 Ok(())
378}
379
380pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
381 unsupported()
382}
383
384pub fn symlink(_src: &Path, _dst: &Path) -> io::Result<()> {
385 unsupported()
386}
387
388pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
389 unsupported()
390}
391
392pub fn stat(_p: &Path) -> io::Result<FileAttr> {
393 unsupported()
394}
395
396pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
397 unsupported()
398}
399
400pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
401 unsupported()
402}