1 use crate::convert::TryFrom;
2 use crate::ffi::{CStr, CString, OsString};
4 use crate::hash::{Hash, Hasher};
5 use crate::io::{self, Error, ErrorKind};
6 use crate::io::{BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
7 use crate::os::unix::ffi::OsStrExt;
8 use crate::path::{Path, PathBuf};
9 use crate::sys::common::small_c_string::run_path_with_cstr;
11 use crate::sys::hermit::abi;
12 use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
13 use crate::sys::hermit::fd::FileDesc;
14 use crate::sys::time::SystemTime;
15 use crate::sys::unsupported;
17 pub use crate::sys_common::fs::{copy, try_exists};
18 //pub use crate::sys_common::fs::remove_dir_all;
21 pub struct File(FileDesc);
23 pub struct FileAttr(!);
25 pub struct ReadDir(!);
27 pub struct DirEntry(!);
29 #[derive(Clone, Debug)]
30 pub struct OpenOptions {
42 #[derive(Copy, Clone, Debug, Default)]
43 pub struct FileTimes {}
45 pub struct FilePermissions(!);
47 pub struct FileType(!);
50 pub struct DirBuilder {}
53 pub fn size(&self) -> u64 {
57 pub fn perm(&self) -> FilePermissions {
61 pub fn file_type(&self) -> FileType {
65 pub fn modified(&self) -> io::Result<SystemTime> {
69 pub fn accessed(&self) -> io::Result<SystemTime> {
73 pub fn created(&self) -> io::Result<SystemTime> {
78 impl Clone for FileAttr {
79 fn clone(&self) -> FileAttr {
84 impl FilePermissions {
85 pub fn readonly(&self) -> bool {
89 pub fn set_readonly(&mut self, _readonly: bool) {
94 impl Clone for FilePermissions {
95 fn clone(&self) -> FilePermissions {
100 impl PartialEq for FilePermissions {
101 fn eq(&self, _other: &FilePermissions) -> bool {
106 impl Eq for FilePermissions {}
108 impl fmt::Debug for FilePermissions {
109 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 pub fn set_accessed(&mut self, _t: SystemTime) {}
116 pub fn set_modified(&mut self, _t: SystemTime) {}
120 pub fn is_dir(&self) -> bool {
124 pub fn is_file(&self) -> bool {
128 pub fn is_symlink(&self) -> bool {
133 impl Clone for FileType {
134 fn clone(&self) -> FileType {
139 impl Copy for FileType {}
141 impl PartialEq for FileType {
142 fn eq(&self, _other: &FileType) -> bool {
147 impl Eq for FileType {}
149 impl Hash for FileType {
150 fn hash<H: Hasher>(&self, _h: &mut H) {
155 impl fmt::Debug for FileType {
156 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 impl fmt::Debug for ReadDir {
162 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 impl Iterator for ReadDir {
168 type Item = io::Result<DirEntry>;
170 fn next(&mut self) -> Option<io::Result<DirEntry>> {
176 pub fn path(&self) -> PathBuf {
180 pub fn file_name(&self) -> OsString {
184 pub fn metadata(&self) -> io::Result<FileAttr> {
188 pub fn file_type(&self) -> io::Result<FileType> {
194 pub fn new() -> OpenOptions {
208 pub fn read(&mut self, read: bool) {
211 pub fn write(&mut self, write: bool) {
214 pub fn append(&mut self, append: bool) {
215 self.append = append;
217 pub fn truncate(&mut self, truncate: bool) {
218 self.truncate = truncate;
220 pub fn create(&mut self, create: bool) {
221 self.create = create;
223 pub fn create_new(&mut self, create_new: bool) {
224 self.create_new = create_new;
227 fn get_access_mode(&self) -> io::Result<i32> {
228 match (self.read, self.write, self.append) {
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),
234 (false, false, false) => {
235 Err(io::const_io_error!(ErrorKind::InvalidInput, "invalid access mode"))
240 fn get_creation_mode(&self) -> io::Result<i32> {
241 match (self.write, self.append) {
244 if self.truncate || self.create || self.create_new {
245 return Err(io::const_io_error!(
246 ErrorKind::InvalidInput,
247 "invalid creation mode",
252 if self.truncate && !self.create_new {
253 return Err(io::const_io_error!(
254 ErrorKind::InvalidInput,
255 "invalid creation mode",
261 Ok(match (self.create, self.truncate, self.create_new) {
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,
272 pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
273 run_path_with_cstr(path, |path| File::open_c(&path, opts))
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()?;
281 if flags & O_CREAT == O_CREAT {
287 let fd = unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? };
288 Ok(File(FileDesc::new(fd as i32)))
291 pub fn file_attr(&self) -> io::Result<FileAttr> {
292 Err(Error::from_raw_os_error(22))
295 pub fn fsync(&self) -> io::Result<()> {
296 Err(Error::from_raw_os_error(22))
299 pub fn datasync(&self) -> io::Result<()> {
303 pub fn truncate(&self, _size: u64) -> io::Result<()> {
304 Err(Error::from_raw_os_error(22))
307 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
311 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
312 crate::io::default_read_vectored(|buf| self.read(buf), bufs)
316 pub fn is_read_vectored(&self) -> bool {
320 pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
321 crate::io::default_read_buf(|buf| self.read(buf), cursor)
324 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
328 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
329 crate::io::default_write_vectored(|buf| self.write(buf), bufs)
333 pub fn is_write_vectored(&self) -> bool {
337 pub fn flush(&self) -> io::Result<()> {
341 pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> {
342 Err(Error::from_raw_os_error(22))
345 pub fn duplicate(&self) -> io::Result<File> {
346 Err(Error::from_raw_os_error(22))
349 pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> {
350 Err(Error::from_raw_os_error(22))
353 pub fn set_times(&self, _times: FileTimes) -> io::Result<()> {
354 Err(Error::from_raw_os_error(22))
359 pub fn new() -> DirBuilder {
363 pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
368 pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
372 pub fn unlink(path: &Path) -> io::Result<()> {
373 run_path_with_cstr(path, |path| cvt(unsafe { abi::unlink(path.as_ptr()) }).map(|_| ()))
376 pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
380 pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> {
384 pub fn rmdir(_p: &Path) -> io::Result<()> {
388 pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
393 pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
397 pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
401 pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
405 pub fn stat(_p: &Path) -> io::Result<FileAttr> {
409 pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
413 pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {