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, ReadBuf, 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 pub struct FilePermissions(!);
46 pub struct FileType(!);
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
::const_io_error
!(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
::const_io_error
!(
240 ErrorKind
::InvalidInput
,
241 "invalid creation mode",
246 if self.truncate
&& !self.create_new
{
247 return Err(io
::const_io_error
!(
248 ErrorKind
::InvalidInput
,
249 "invalid creation mode",
255 Ok(match (self.create
, self.truncate
, self.create_new
) {
256 (false, false, false) => 0,
257 (true, false, false) => O_CREAT
,
258 (false, true, false) => O_TRUNC
,
259 (true, true, false) => O_CREAT
| O_TRUNC
,
260 (_
, _
, true) => O_CREAT
| O_EXCL
,
266 pub fn open(path
: &Path
, opts
: &OpenOptions
) -> io
::Result
<File
> {
267 let path
= cstr(path
)?
;
268 File
::open_c(&path
, opts
)
271 pub fn open_c(path
: &CStr
, opts
: &OpenOptions
) -> io
::Result
<File
> {
272 let mut flags
= opts
.get_access_mode()?
;
273 flags
= flags
| opts
.get_creation_mode()?
;
276 if flags
& O_CREAT
== O_CREAT
{
282 let fd
= unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? }
;
283 Ok(File(FileDesc
::new(fd
as i32)))
286 pub fn file_attr(&self) -> io
::Result
<FileAttr
> {
287 Err(Error
::from_raw_os_error(22))
290 pub fn fsync(&self) -> io
::Result
<()> {
291 Err(Error
::from_raw_os_error(22))
294 pub fn datasync(&self) -> io
::Result
<()> {
298 pub fn truncate(&self, _size
: u64) -> io
::Result
<()> {
299 Err(Error
::from_raw_os_error(22))
302 pub fn read(&self, buf
: &mut [u8]) -> io
::Result
<usize> {
306 pub fn read_vectored(&self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
307 crate::io
::default_read_vectored(|buf
| self.read(buf
), bufs
)
311 pub fn is_read_vectored(&self) -> bool
{
315 pub fn read_buf(&self, buf
: &mut ReadBuf
<'_
>) -> io
::Result
<()> {
316 crate::io
::default_read_buf(|buf
| self.read(buf
), buf
)
319 pub fn write(&self, buf
: &[u8]) -> io
::Result
<usize> {
323 pub fn write_vectored(&self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
324 crate::io
::default_write_vectored(|buf
| self.write(buf
), bufs
)
328 pub fn is_write_vectored(&self) -> bool
{
332 pub fn flush(&self) -> io
::Result
<()> {
336 pub fn seek(&self, _pos
: SeekFrom
) -> io
::Result
<u64> {
337 Err(Error
::from_raw_os_error(22))
340 pub fn duplicate(&self) -> io
::Result
<File
> {
341 Err(Error
::from_raw_os_error(22))
344 pub fn set_permissions(&self, _perm
: FilePermissions
) -> io
::Result
<()> {
345 Err(Error
::from_raw_os_error(22))
350 pub fn new() -> DirBuilder
{
354 pub fn mkdir(&self, _p
: &Path
) -> io
::Result
<()> {
359 pub fn readdir(_p
: &Path
) -> io
::Result
<ReadDir
> {
363 pub fn unlink(path
: &Path
) -> io
::Result
<()> {
364 let name
= cstr(path
)?
;
365 let _
= unsafe { cvt(abi::unlink(name.as_ptr()))? }
;
369 pub fn rename(_old
: &Path
, _new
: &Path
) -> io
::Result
<()> {
373 pub fn set_perm(_p
: &Path
, perm
: FilePermissions
) -> io
::Result
<()> {
377 pub fn rmdir(_p
: &Path
) -> io
::Result
<()> {
381 pub fn remove_dir_all(_path
: &Path
) -> io
::Result
<()> {
386 pub fn readlink(_p
: &Path
) -> io
::Result
<PathBuf
> {
390 pub fn symlink(_original
: &Path
, _link
: &Path
) -> io
::Result
<()> {
394 pub fn link(_original
: &Path
, _link
: &Path
) -> io
::Result
<()> {
398 pub fn stat(_p
: &Path
) -> io
::Result
<FileAttr
> {
402 pub fn lstat(_p
: &Path
) -> io
::Result
<FileAttr
> {
406 pub fn canonicalize(_p
: &Path
) -> io
::Result
<PathBuf
> {