1 use crate::ffi::{CStr, CString, OsString};
3 use crate::hash::{Hash, Hasher};
4 use crate::io::{self, Error, ErrorKind};
5 use crate::io::{IoSlice, IoSliceMut, SeekFrom};
6 use crate::path::{Path, PathBuf};
8 use crate::sys::hermit::abi;
9 use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
10 use crate::sys::hermit::fd::FileDesc;
11 use crate::sys::time::SystemTime;
12 use crate::sys::{unsupported, Void};
13 use crate::sys_common::os_str_bytes::OsStrExt;
15 pub use crate::sys_common::fs::copy;
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(Void);
27 pub struct ReadDir(Void);
29 pub struct DirEntry(Void);
31 #[derive(Clone, Debug)]
32 pub struct OpenOptions {
44 pub struct FilePermissions(Void);
46 pub struct FileType(Void);
49 pub struct DirBuilder {}
52 pub fn size(&self) -> u64 {
56 pub fn perm(&self) -> FilePermissions {
60 pub fn file_type(&self) -> FileType {
64 pub fn modified(&self) -> io::Result<SystemTime> {
68 pub fn accessed(&self) -> io::Result<SystemTime> {
72 pub fn created(&self) -> io::Result<SystemTime> {
77 impl Clone for FileAttr {
78 fn clone(&self) -> FileAttr {
83 impl FilePermissions {
84 pub fn readonly(&self) -> bool {
88 pub fn set_readonly(&mut self, _readonly: bool) {
93 impl Clone for FilePermissions {
94 fn clone(&self) -> FilePermissions {
99 impl PartialEq for FilePermissions {
100 fn eq(&self, _other: &FilePermissions) -> bool {
105 impl Eq for FilePermissions {}
107 impl fmt::Debug for FilePermissions {
108 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 pub fn is_dir(&self) -> bool {
118 pub fn is_file(&self) -> bool {
122 pub fn is_symlink(&self) -> bool {
127 impl Clone for FileType {
128 fn clone(&self) -> FileType {
133 impl Copy for FileType {}
135 impl PartialEq for FileType {
136 fn eq(&self, _other: &FileType) -> bool {
141 impl Eq for FileType {}
143 impl Hash for FileType {
144 fn hash<H: Hasher>(&self, _h: &mut H) {
149 impl fmt::Debug for FileType {
150 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 impl fmt::Debug for ReadDir {
156 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 impl Iterator for ReadDir {
162 type Item = io::Result<DirEntry>;
164 fn next(&mut self) -> Option<io::Result<DirEntry>> {
170 pub fn path(&self) -> PathBuf {
174 pub fn file_name(&self) -> OsString {
178 pub fn metadata(&self) -> io::Result<FileAttr> {
182 pub fn file_type(&self) -> io::Result<FileType> {
188 pub fn new() -> OpenOptions {
202 pub fn read(&mut self, read: bool) {
205 pub fn write(&mut self, write: bool) {
208 pub fn append(&mut self, append: bool) {
209 self.append = append;
211 pub fn truncate(&mut self, truncate: bool) {
212 self.truncate = truncate;
214 pub fn create(&mut self, create: bool) {
215 self.create = create;
217 pub fn create_new(&mut self, create_new: bool) {
218 self.create_new = create_new;
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::Error::new(ErrorKind::InvalidInput, "invalid access mode"))
234 fn get_creation_mode(&self) -> io::Result<i32> {
235 match (self.write, self.append) {
238 if self.truncate || self.create || self.create_new {
239 return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
243 if self.truncate && !self.create_new {
244 return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
249 Ok(match (self.create, self.truncate, self.create_new) {
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,
260 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
261 let path = cstr(path)?;
262 File::open_c(&path, opts)
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()?;
270 if flags & O_CREAT == O_CREAT {
276 let fd = unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? };
277 Ok(File(FileDesc::new(fd as i32)))
280 pub fn file_attr(&self) -> io::Result<FileAttr> {
281 Err(Error::from_raw_os_error(22))
284 pub fn fsync(&self) -> io::Result<()> {
285 Err(Error::from_raw_os_error(22))
288 pub fn datasync(&self) -> io::Result<()> {
292 pub fn truncate(&self, _size: u64) -> io::Result<()> {
293 Err(Error::from_raw_os_error(22))
296 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
300 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
301 crate::io::default_read_vectored(|buf| self.read(buf), bufs)
305 pub fn is_read_vectored(&self) -> bool {
309 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
313 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
314 crate::io::default_write_vectored(|buf| self.write(buf), bufs)
318 pub fn is_write_vectored(&self) -> bool {
322 pub fn flush(&self) -> io::Result<()> {
326 pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> {
327 Err(Error::from_raw_os_error(22))
330 pub fn duplicate(&self) -> io::Result<File> {
331 Err(Error::from_raw_os_error(22))
334 pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> {
335 Err(Error::from_raw_os_error(22))
338 pub fn diverge(&self) -> ! {
344 pub fn new() -> DirBuilder {
348 pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
353 pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
357 pub fn unlink(path: &Path) -> io::Result<()> {
358 let name = cstr(path)?;
359 let _ = unsafe { cvt(abi::unlink(name.as_ptr()))? };
363 pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
367 pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> {
371 pub fn rmdir(_p: &Path) -> io::Result<()> {
375 pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
380 pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
384 pub fn symlink(_src: &Path, _dst: &Path) -> io::Result<()> {
388 pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
392 pub fn stat(_p: &Path) -> io::Result<FileAttr> {
396 pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
400 pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {