1 use crate::ffi::{CStr, CString, OsString};
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};
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;
15 pub use crate::sys_common::fs::{copy, try_exists};
16 //pub use crate::sys_common::fs::remove_dir_all;
18 fn cstr(path: &Path) -> io::Result<CString> {
19 Ok(CString::new(path.as_os_str().as_bytes())?)
23 pub struct File(FileDesc);
25 pub struct FileAttr(!);
27 pub struct ReadDir(!);
29 pub struct DirEntry(!);
31 #[derive(Clone, Debug)]
32 pub struct OpenOptions {
44 #[derive(Copy, Clone, Debug, Default)]
45 pub struct FileTimes {}
47 pub struct FilePermissions(!);
49 pub struct FileType(!);
52 pub struct DirBuilder {}
55 pub fn size(&self) -> u64 {
59 pub fn perm(&self) -> FilePermissions {
63 pub fn file_type(&self) -> FileType {
67 pub fn modified(&self) -> io::Result<SystemTime> {
71 pub fn accessed(&self) -> io::Result<SystemTime> {
75 pub fn created(&self) -> io::Result<SystemTime> {
80 impl Clone for FileAttr {
81 fn clone(&self) -> FileAttr {
86 impl FilePermissions {
87 pub fn readonly(&self) -> bool {
91 pub fn set_readonly(&mut self, _readonly: bool) {
96 impl Clone for FilePermissions {
97 fn clone(&self) -> FilePermissions {
102 impl PartialEq for FilePermissions {
103 fn eq(&self, _other: &FilePermissions) -> bool {
108 impl Eq for FilePermissions {}
110 impl fmt::Debug for FilePermissions {
111 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 pub fn set_accessed(&mut self, _t: SystemTime) {}
118 pub fn set_modified(&mut self, _t: SystemTime) {}
122 pub fn is_dir(&self) -> bool {
126 pub fn is_file(&self) -> bool {
130 pub fn is_symlink(&self) -> bool {
135 impl Clone for FileType {
136 fn clone(&self) -> FileType {
141 impl Copy for FileType {}
143 impl PartialEq for FileType {
144 fn eq(&self, _other: &FileType) -> bool {
149 impl Eq for FileType {}
151 impl Hash for FileType {
152 fn hash<H: Hasher>(&self, _h: &mut H) {
157 impl fmt::Debug for FileType {
158 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 impl fmt::Debug for ReadDir {
164 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 impl Iterator for ReadDir {
170 type Item = io::Result<DirEntry>;
172 fn next(&mut self) -> Option<io::Result<DirEntry>> {
178 pub fn path(&self) -> PathBuf {
182 pub fn file_name(&self) -> OsString {
186 pub fn metadata(&self) -> io::Result<FileAttr> {
190 pub fn file_type(&self) -> io::Result<FileType> {
196 pub fn new() -> OpenOptions {
210 pub fn read(&mut self, read: bool) {
213 pub fn write(&mut self, write: bool) {
216 pub fn append(&mut self, append: bool) {
217 self.append = append;
219 pub fn truncate(&mut self, truncate: bool) {
220 self.truncate = truncate;
222 pub fn create(&mut self, create: bool) {
223 self.create = create;
225 pub fn create_new(&mut self, create_new: bool) {
226 self.create_new = create_new;
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"))
242 fn get_creation_mode(&self) -> io::Result<i32> {
243 match (self.write, self.append) {
246 if self.truncate || self.create || self.create_new {
247 return Err(io::const_io_error!(
248 ErrorKind::InvalidInput,
249 "invalid creation mode",
254 if self.truncate && !self.create_new {
255 return Err(io::const_io_error!(
256 ErrorKind::InvalidInput,
257 "invalid creation mode",
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,
274 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
275 let path = cstr(path)?;
276 File::open_c(&path, opts)
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()?;
284 if flags & O_CREAT == O_CREAT {
290 let fd = unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? };
291 Ok(File(FileDesc::new(fd as i32)))
294 pub fn file_attr(&self) -> io::Result<FileAttr> {
295 Err(Error::from_raw_os_error(22))
298 pub fn fsync(&self) -> io::Result<()> {
299 Err(Error::from_raw_os_error(22))
302 pub fn datasync(&self) -> io::Result<()> {
306 pub fn truncate(&self, _size: u64) -> io::Result<()> {
307 Err(Error::from_raw_os_error(22))
310 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
314 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
315 crate::io::default_read_vectored(|buf| self.read(buf), bufs)
319 pub fn is_read_vectored(&self) -> bool {
323 pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
324 crate::io::default_read_buf(|buf| self.read(buf), cursor)
327 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
331 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
332 crate::io::default_write_vectored(|buf| self.write(buf), bufs)
336 pub fn is_write_vectored(&self) -> bool {
340 pub fn flush(&self) -> io::Result<()> {
344 pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> {
345 Err(Error::from_raw_os_error(22))
348 pub fn duplicate(&self) -> io::Result<File> {
349 Err(Error::from_raw_os_error(22))
352 pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> {
353 Err(Error::from_raw_os_error(22))
356 pub fn set_times(&self, _times: FileTimes) -> io::Result<()> {
357 Err(Error::from_raw_os_error(22))
362 pub fn new() -> DirBuilder {
366 pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
371 pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
375 pub fn unlink(path: &Path) -> io::Result<()> {
376 let name = cstr(path)?;
377 let _ = unsafe { cvt(abi::unlink(name.as_ptr()))? };
381 pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
385 pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> {
389 pub fn rmdir(_p: &Path) -> io::Result<()> {
393 pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
398 pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
402 pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
406 pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
410 pub fn stat(_p: &Path) -> io::Result<FileAttr> {
414 pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
418 pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {