| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License.. |
| |
| use crate::os::unix::prelude::*; |
| |
| use crate::ffi::{CStr, CString, OsStr, OsString}; |
| use crate::fmt; |
| use crate::io::{self, Error, IoSlice, IoSliceMut, ReadBuf, SeekFrom}; |
| use crate::mem; |
| use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd}; |
| use crate::path::{Path, PathBuf}; |
| use crate::ptr; |
| use crate::sync::Arc; |
| use crate::sys::fd::FileDesc; |
| use crate::sys::time::SystemTime; |
| use crate::sys::{cvt, cvt_r}; |
| use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; |
| |
| use sgx_libc::{c_int, dirent64, mode_t, off64_t, stat64, time_t}; |
| |
| pub use crate::sys_common::fs::try_exists; |
| |
| pub struct File(FileDesc); |
| |
| #[derive(Clone)] |
| pub struct FileAttr { |
| stat: stat64, |
| } |
| |
| // all DirEntry's will have a reference to this struct |
| struct InnerReadDir { |
| dirp: Dir, |
| root: PathBuf, |
| } |
| |
| pub struct ReadDir { |
| inner: Arc<InnerReadDir>, |
| end_of_stream: bool, |
| } |
| |
| struct Dir(*mut libc::DIR); |
| |
| unsafe impl Send for Dir {} |
| unsafe impl Sync for Dir {} |
| |
| pub struct DirEntry { |
| entry: dirent64, |
| dir: Arc<InnerReadDir>, |
| } |
| |
| #[derive(Clone, Debug)] |
| pub struct OpenOptions { |
| // generic |
| read: bool, |
| write: bool, |
| append: bool, |
| truncate: bool, |
| create: bool, |
| create_new: bool, |
| // system-specific |
| custom_flags: i32, |
| mode: mode_t, |
| } |
| |
| #[derive(Clone, PartialEq, Eq, Debug)] |
| pub struct FilePermissions { |
| mode: mode_t, |
| } |
| |
| #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] |
| pub struct FileType { |
| mode: mode_t, |
| } |
| |
| #[derive(Debug)] |
| pub struct DirBuilder { |
| mode: mode_t, |
| } |
| |
| impl FileAttr { |
| fn from_stat64(stat: stat64) -> Self { |
| Self { stat } |
| } |
| } |
| |
| impl FileAttr { |
| pub fn size(&self) -> u64 { |
| self.stat.st_size as u64 |
| } |
| |
| pub fn perm(&self) -> FilePermissions { |
| FilePermissions { mode: (self.stat.st_mode as mode_t) } |
| } |
| |
| pub fn file_type(&self) -> FileType { |
| FileType { mode: self.stat.st_mode as mode_t } |
| } |
| } |
| |
| impl FileAttr { |
| pub fn modified(&self) -> io::Result<SystemTime> { |
| Ok(SystemTime::from(libc::timespec { |
| tv_sec: self.stat.st_mtime as time_t, |
| tv_nsec: self.stat.st_mtime_nsec as _, |
| })) |
| } |
| |
| pub fn accessed(&self) -> io::Result<SystemTime> { |
| Ok(SystemTime::from(libc::timespec { |
| tv_sec: self.stat.st_atime as time_t, |
| tv_nsec: self.stat.st_atime_nsec as _, |
| })) |
| } |
| |
| pub fn created(&self) -> io::Result<SystemTime> { |
| Err(io::const_io_error!( |
| io::ErrorKind::Unsupported, |
| "creation time is not available on this platform \ |
| currently", |
| )) |
| } |
| } |
| |
| impl AsInner<stat64> for FileAttr { |
| fn as_inner(&self) -> &stat64 { |
| &self.stat |
| } |
| } |
| |
| impl FilePermissions { |
| pub fn readonly(&self) -> bool { |
| // check if any class (owner, group, others) has write permission |
| self.mode & 0o222 == 0 |
| } |
| |
| pub fn set_readonly(&mut self, readonly: bool) { |
| if readonly { |
| // remove write permission for all classes; equivalent to `chmod a-w <file>` |
| self.mode &= !0o222; |
| } else { |
| // add write permission for all classes; equivalent to `chmod a+w <file>` |
| self.mode |= 0o222; |
| } |
| } |
| pub fn mode(&self) -> u32 { |
| self.mode as u32 |
| } |
| } |
| |
| impl FileType { |
| pub fn is_dir(&self) -> bool { |
| self.is(libc::S_IFDIR) |
| } |
| pub fn is_file(&self) -> bool { |
| self.is(libc::S_IFREG) |
| } |
| pub fn is_symlink(&self) -> bool { |
| self.is(libc::S_IFLNK) |
| } |
| |
| pub fn is(&self, mode: mode_t) -> bool { |
| self.mode & libc::S_IFMT == mode |
| } |
| } |
| |
| impl FromInner<u32> for FilePermissions { |
| fn from_inner(mode: u32) -> FilePermissions { |
| FilePermissions { mode: mode as mode_t } |
| } |
| } |
| |
| impl fmt::Debug for ReadDir { |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| // This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame. |
| // Thus the result will be e g 'ReadDir("/home")' |
| fmt::Debug::fmt(&*self.inner.root, f) |
| } |
| } |
| |
| impl Iterator for ReadDir { |
| type Item = io::Result<DirEntry>; |
| |
| fn next(&mut self) -> Option<io::Result<DirEntry>> { |
| if self.end_of_stream { |
| return None; |
| } |
| |
| unsafe { |
| let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) }; |
| let mut entry_ptr = ptr::null_mut(); |
| loop { |
| let err = libc::readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr); |
| if err != 0 { |
| if entry_ptr.is_null() { |
| // We encountered an error (which will be returned in this iteration), but |
| // we also reached the end of the directory stream. The `end_of_stream` |
| // flag is enabled to make sure that we return `None` in the next iteration |
| // (instead of looping forever) |
| self.end_of_stream = true; |
| } |
| return Some(Err(Error::from_raw_os_error(err))); |
| } |
| if entry_ptr.is_null() { |
| return None; |
| } |
| if ret.name_bytes() != b"." && ret.name_bytes() != b".." { |
| return Some(Ok(ret)); |
| } |
| } |
| } |
| } |
| } |
| |
| impl Drop for Dir { |
| fn drop(&mut self) { |
| let r = unsafe { libc::closedir(self.0) }; |
| debug_assert_eq!(r, 0); |
| } |
| } |
| |
| impl DirEntry { |
| pub fn path(&self) -> PathBuf { |
| self.dir.root.join(self.file_name_os_str()) |
| } |
| |
| pub fn file_name(&self) -> OsString { |
| self.file_name_os_str().to_os_string() |
| } |
| |
| pub fn metadata(&self) -> io::Result<FileAttr> { |
| let fd = cvt(unsafe { libc::dirfd(self.dir.dirp.0) })?; |
| let name = self.name_cstr().as_ptr(); |
| let mut stat: stat64 = unsafe { mem::zeroed() }; |
| cvt(unsafe { libc::fstatat64(fd, name, &mut stat, libc::AT_SYMLINK_NOFOLLOW) })?; |
| Ok(FileAttr::from_stat64(stat)) |
| } |
| |
| pub fn file_type(&self) -> io::Result<FileType> { |
| match self.entry.d_type { |
| libc::DT_CHR => Ok(FileType { mode: libc::S_IFCHR }), |
| libc::DT_FIFO => Ok(FileType { mode: libc::S_IFIFO }), |
| libc::DT_LNK => Ok(FileType { mode: libc::S_IFLNK }), |
| libc::DT_REG => Ok(FileType { mode: libc::S_IFREG }), |
| libc::DT_SOCK => Ok(FileType { mode: libc::S_IFSOCK }), |
| libc::DT_DIR => Ok(FileType { mode: libc::S_IFDIR }), |
| libc::DT_BLK => Ok(FileType { mode: libc::S_IFBLK }), |
| _ => self.metadata().map(|m| m.file_type()), |
| } |
| } |
| |
| pub fn ino(&self) -> u64 { |
| self.entry.d_ino as u64 |
| } |
| |
| fn name_bytes(&self) -> &[u8] { |
| unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()).to_bytes() } |
| } |
| |
| fn name_cstr(&self) -> &CStr { |
| unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()) } |
| } |
| |
| pub fn file_name_os_str(&self) -> &OsStr { |
| OsStr::from_bytes(self.name_bytes()) |
| } |
| } |
| |
| impl OpenOptions { |
| pub fn new() -> OpenOptions { |
| OpenOptions { |
| // generic |
| read: false, |
| write: false, |
| append: false, |
| truncate: false, |
| create: false, |
| create_new: false, |
| // system-specific |
| custom_flags: 0, |
| mode: 0o666, |
| } |
| } |
| |
| pub fn read(&mut self, read: bool) { |
| self.read = read; |
| } |
| pub fn write(&mut self, write: bool) { |
| self.write = write; |
| } |
| pub fn append(&mut self, append: bool) { |
| self.append = append; |
| } |
| pub fn truncate(&mut self, truncate: bool) { |
| self.truncate = truncate; |
| } |
| pub fn create(&mut self, create: bool) { |
| self.create = create; |
| } |
| pub fn create_new(&mut self, create_new: bool) { |
| self.create_new = create_new; |
| } |
| |
| pub fn custom_flags(&mut self, flags: i32) { |
| self.custom_flags = flags; |
| } |
| pub fn mode(&mut self, mode: u32) { |
| self.mode = mode as mode_t; |
| } |
| |
| fn get_access_mode(&self) -> io::Result<c_int> { |
| match (self.read, self.write, self.append) { |
| (true, false, false) => Ok(libc::O_RDONLY), |
| (false, true, false) => Ok(libc::O_WRONLY), |
| (true, true, false) => Ok(libc::O_RDWR), |
| (false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND), |
| (true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND), |
| (false, false, false) => Err(Error::from_raw_os_error(libc::EINVAL)), |
| } |
| } |
| |
| fn get_creation_mode(&self) -> io::Result<c_int> { |
| match (self.write, self.append) { |
| (true, false) => {} |
| (false, false) => { |
| if self.truncate || self.create || self.create_new { |
| return Err(Error::from_raw_os_error(libc::EINVAL)); |
| } |
| } |
| (_, true) => { |
| if self.truncate && !self.create_new { |
| return Err(Error::from_raw_os_error(libc::EINVAL)); |
| } |
| } |
| } |
| |
| Ok(match (self.create, self.truncate, self.create_new) { |
| (false, false, false) => 0, |
| (true, false, false) => libc::O_CREAT, |
| (false, true, false) => libc::O_TRUNC, |
| (true, true, false) => libc::O_CREAT | libc::O_TRUNC, |
| (_, _, true) => libc::O_CREAT | libc::O_EXCL, |
| }) |
| } |
| } |
| |
| impl File { |
| pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> { |
| let path = cstr(path)?; |
| File::open_c(&path, opts) |
| } |
| |
| pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> { |
| let flags = libc::O_CLOEXEC |
| | opts.get_access_mode()? |
| | opts.get_creation_mode()? |
| | (opts.custom_flags as c_int & !libc::O_ACCMODE); |
| // The third argument of `open64` is documented to have type `mode_t`. On |
| // some platforms (like macOS, where `open64` is actually `open`), `mode_t` is `u16`. |
| // However, since this is a variadic function, C integer promotion rules mean that on |
| // the ABI level, this still gets passed as `c_int` (aka `u32` on Unix platforms). |
| let fd = cvt_r(|| unsafe { libc::open64(path.as_ptr(), flags, opts.mode as c_int) })?; |
| Ok(File(unsafe { FileDesc::from_raw_fd(fd) })) |
| } |
| |
| pub fn file_attr(&self) -> io::Result<FileAttr> { |
| let fd = self.as_raw_fd(); |
| let mut stat: stat64 = unsafe { mem::zeroed() }; |
| cvt(unsafe { libc::fstat64(fd, &mut stat) })?; |
| Ok(FileAttr::from_stat64(stat)) |
| } |
| |
| pub fn fsync(&self) -> io::Result<()> { |
| cvt_r(|| unsafe { os_fsync(self.as_raw_fd()) })?; |
| return Ok(()); |
| |
| unsafe fn os_fsync(fd: c_int) -> c_int { |
| libc::fsync(fd) |
| } |
| } |
| |
| pub fn datasync(&self) -> io::Result<()> { |
| cvt_r(|| unsafe { os_datasync(self.as_raw_fd()) })?; |
| return Ok(()); |
| |
| unsafe fn os_datasync(fd: c_int) -> c_int { |
| libc::fdatasync(fd) |
| } |
| } |
| |
| pub fn truncate(&self, size: u64) -> io::Result<()> { |
| use crate::convert::TryInto; |
| let size: off64_t = |
| size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; |
| cvt_r(|| unsafe { libc::ftruncate64(self.as_raw_fd(), size) }).map(drop) |
| } |
| |
| pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { |
| self.0.read(buf) |
| } |
| |
| pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
| self.0.read_vectored(bufs) |
| } |
| |
| #[inline] |
| pub fn is_read_vectored(&self) -> bool { |
| self.0.is_read_vectored() |
| } |
| |
| pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> { |
| self.0.read_at(buf, offset) |
| } |
| |
| pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { |
| self.0.read_buf(buf) |
| } |
| |
| pub fn write(&self, buf: &[u8]) -> io::Result<usize> { |
| self.0.write(buf) |
| } |
| |
| pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
| self.0.write_vectored(bufs) |
| } |
| |
| #[inline] |
| pub fn is_write_vectored(&self) -> bool { |
| self.0.is_write_vectored() |
| } |
| |
| pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> { |
| self.0.write_at(buf, offset) |
| } |
| |
| pub fn flush(&self) -> io::Result<()> { |
| Ok(()) |
| } |
| |
| pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> { |
| let (whence, pos) = match pos { |
| // Casting to `i64` is fine, too large values will end up as |
| // negative which will cause an error in `lseek64`. |
| SeekFrom::Start(off) => (libc::SEEK_SET, off as i64), |
| SeekFrom::End(off) => (libc::SEEK_END, off), |
| SeekFrom::Current(off) => (libc::SEEK_CUR, off), |
| }; |
| |
| let n = cvt(unsafe { libc::lseek64(self.as_raw_fd(), pos, whence) })?; |
| Ok(n as u64) |
| } |
| |
| pub fn duplicate(&self) -> io::Result<File> { |
| self.0.duplicate().map(File) |
| } |
| |
| pub fn set_permissions(&self, perm: FilePermissions) -> io::Result<()> { |
| cvt_r(|| unsafe { libc::fchmod(self.as_raw_fd(), perm.mode) })?; |
| Ok(()) |
| } |
| } |
| |
| impl DirBuilder { |
| pub fn new() -> DirBuilder { |
| DirBuilder { mode: 0o777 } |
| } |
| |
| pub fn mkdir(&self, p: &Path) -> io::Result<()> { |
| let p = cstr(p)?; |
| cvt(unsafe { libc::mkdir(p.as_ptr(), self.mode) })?; |
| Ok(()) |
| } |
| |
| pub fn set_mode(&mut self, mode: u32) { |
| self.mode = mode as mode_t; |
| } |
| } |
| |
| fn cstr(path: &Path) -> io::Result<CString> { |
| Ok(CString::new(path.as_os_str().as_bytes())?) |
| } |
| |
| impl AsInner<FileDesc> for File { |
| fn as_inner(&self) -> &FileDesc { |
| &self.0 |
| } |
| } |
| |
| impl AsInnerMut<FileDesc> for File { |
| fn as_inner_mut(&mut self) -> &mut FileDesc { |
| &mut self.0 |
| } |
| } |
| |
| impl IntoInner<FileDesc> for File { |
| fn into_inner(self) -> FileDesc { |
| self.0 |
| } |
| } |
| |
| impl FromInner<FileDesc> for File { |
| fn from_inner(file_desc: FileDesc) -> Self { |
| Self(file_desc) |
| } |
| } |
| |
| impl AsFd for File { |
| fn as_fd(&self) -> BorrowedFd<'_> { |
| self.0.as_fd() |
| } |
| } |
| |
| impl AsRawFd for File { |
| fn as_raw_fd(&self) -> RawFd { |
| self.0.as_raw_fd() |
| } |
| } |
| |
| impl IntoRawFd for File { |
| fn into_raw_fd(self) -> RawFd { |
| self.0.into_raw_fd() |
| } |
| } |
| |
| impl FromRawFd for File { |
| unsafe fn from_raw_fd(raw_fd: RawFd) -> Self { |
| Self(FromRawFd::from_raw_fd(raw_fd)) |
| } |
| } |
| |
| impl fmt::Debug for File { |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| fn get_path(fd: c_int) -> Option<PathBuf> { |
| let mut p = PathBuf::from("/proc/self/fd"); |
| p.push(&fd.to_string()); |
| readlink(&p).ok() |
| } |
| |
| fn get_mode(fd: c_int) -> Option<(bool, bool)> { |
| let mode = unsafe { libc::fcntl_arg0(fd, libc::F_GETFL) }; |
| if mode == -1 { |
| return None; |
| } |
| match mode & libc::O_ACCMODE { |
| libc::O_RDONLY => Some((true, false)), |
| libc::O_RDWR => Some((true, true)), |
| libc::O_WRONLY => Some((false, true)), |
| _ => None, |
| } |
| } |
| |
| let fd = self.as_raw_fd(); |
| let mut b = f.debug_struct("File"); |
| b.field("fd", &fd); |
| if let Some(path) = get_path(fd) { |
| b.field("path", &path); |
| } |
| if let Some((read, write)) = get_mode(fd) { |
| b.field("read", &read).field("write", &write); |
| } |
| b.finish() |
| } |
| } |
| |
| pub fn readdir(p: &Path) -> io::Result<ReadDir> { |
| let root = p.to_path_buf(); |
| let p = cstr(p)?; |
| unsafe { |
| let ptr = libc::opendir(p.as_ptr()); |
| if ptr.is_null() { |
| Err(Error::last_os_error()) |
| } else { |
| let inner = InnerReadDir { dirp: Dir(ptr), root }; |
| Ok(ReadDir { |
| inner: Arc::new(inner), |
| end_of_stream: false, |
| }) |
| } |
| } |
| } |
| |
| pub fn unlink(p: &Path) -> io::Result<()> { |
| let p = cstr(p)?; |
| cvt(unsafe { libc::unlink(p.as_ptr()) })?; |
| Ok(()) |
| } |
| |
| pub fn rename(old: &Path, new: &Path) -> io::Result<()> { |
| let old = cstr(old)?; |
| let new = cstr(new)?; |
| cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) })?; |
| Ok(()) |
| } |
| |
| pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> { |
| let p = cstr(p)?; |
| cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) })?; |
| Ok(()) |
| } |
| |
| pub fn rmdir(p: &Path) -> io::Result<()> { |
| let p = cstr(p)?; |
| cvt(unsafe { libc::rmdir(p.as_ptr()) })?; |
| Ok(()) |
| } |
| |
| pub fn readlink(p: &Path) -> io::Result<PathBuf> { |
| let c_path = cstr(p)?; |
| let p = c_path.as_ptr(); |
| |
| let mut buf = Vec::with_capacity(256); |
| |
| loop { |
| let buf_read = |
| cvt(unsafe { libc::readlink(p, buf.as_mut_ptr() as *mut _, buf.capacity()) })? as usize; |
| |
| unsafe { |
| buf.set_len(buf_read); |
| } |
| |
| if buf_read != buf.capacity() { |
| buf.shrink_to_fit(); |
| |
| return Ok(PathBuf::from(OsString::from_vec(buf))); |
| } |
| |
| // Trigger the internal buffer resizing logic of `Vec` by requiring |
| // more space than the current capacity. The length is guaranteed to be |
| // the same as the capacity due to the if statement above. |
| buf.reserve(1); |
| } |
| } |
| |
| pub fn symlink(original: &Path, link: &Path) -> io::Result<()> { |
| let original = cstr(original)?; |
| let link = cstr(link)?; |
| cvt(unsafe { libc::symlink(original.as_ptr(), link.as_ptr()) })?; |
| Ok(()) |
| } |
| |
| pub fn link(original: &Path, link: &Path) -> io::Result<()> { |
| let original = cstr(original)?; |
| let link = cstr(link)?; |
| cvt(unsafe { libc::linkat(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?; |
| Ok(()) |
| } |
| |
| pub fn stat(p: &Path) -> io::Result<FileAttr> { |
| let p = cstr(p)?; |
| let mut stat: stat64 = unsafe { mem::zeroed() }; |
| cvt(unsafe { libc::stat64(p.as_ptr(), &mut stat as *mut _) })?; |
| Ok(FileAttr::from_stat64(stat)) |
| } |
| |
| pub fn lstat(p: &Path) -> io::Result<FileAttr> { |
| let p = cstr(p)?; |
| let mut stat: stat64 = unsafe { mem::zeroed() }; |
| cvt(unsafe { libc::lstat64(p.as_ptr(), &mut stat as *mut _) })?; |
| Ok(FileAttr::from_stat64(stat)) |
| } |
| |
| pub fn canonicalize(p: &Path) -> io::Result<PathBuf> { |
| let path = CString::new(p.as_os_str().as_bytes())?; |
| let buf; |
| unsafe { |
| let r = libc::realpath(path.as_ptr()); |
| if r.is_null() { |
| return Err(io::Error::last_os_error()); |
| } |
| buf = CStr::from_ptr(r).to_bytes().to_vec(); |
| libc::free(r as *mut _); |
| } |
| Ok(PathBuf::from(OsString::from_vec(buf))) |
| } |
| |
| fn open_from(from: &Path) -> io::Result<(crate::fs::File, crate::fs::Metadata)> { |
| use crate::untrusted::fs::File; |
| use crate::sys_common::fs::NOT_FILE_ERROR; |
| |
| let reader = File::open(from)?; |
| let metadata = reader.metadata()?; |
| if !metadata.is_file() { |
| return Err(NOT_FILE_ERROR); |
| } |
| Ok((reader, metadata)) |
| } |
| |
| fn open_to_and_set_permissions( |
| to: &Path, |
| reader_metadata: crate::fs::Metadata, |
| ) -> io::Result<(crate::fs::File, crate::fs::Metadata)> { |
| use crate::fs::OpenOptions; |
| |
| let perm = reader_metadata.permissions(); |
| let writer = OpenOptions::new() |
| // create the file with the correct mode right away |
| .mode(perm.mode()) |
| .write(true) |
| .create(true) |
| .truncate(true) |
| .open(to)?; |
| let writer_metadata = writer.metadata()?; |
| if writer_metadata.is_file() { |
| // Set the correct file permissions, in case the file already existed. |
| // Don't set the permissions on already existing non-files like |
| // pipes/FIFOs or device nodes. |
| writer.set_permissions(perm)?; |
| } |
| Ok((writer, writer_metadata)) |
| } |
| |
| pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { |
| let (mut reader, reader_metadata) = open_from(from)?; |
| let (mut writer, _) = open_to_and_set_permissions(to, reader_metadata)?; |
| |
| io::copy::copy(&mut reader, &mut writer) |
| } |
| |
| pub fn chown(_path: &Path, _uid: u32, _gid: u32) -> io::Result<()> { |
| super::unsupported::unsupported() |
| } |
| |
| pub fn fchown(_fd: c_int, _uid: u32, _gid: u32) -> io::Result<()> { |
| super::unsupported::unsupported() |
| } |
| |
| pub fn lchown(_path: &Path, _uid: u32, _gid: u32) -> io::Result<()> { |
| super::unsupported::unsupported() |
| } |
| |
| pub fn chroot(_dir: &Path) -> io::Result<()> { |
| super::unsupported::unsupported() |
| } |
| |
| mod libc { |
| pub use sgx_libc::ocall::{ |
| chmod, closedir, dirfd, fchmod, fcntl_arg0, fdatasync, free, fstat64, fstatat64, fsync, |
| ftruncate64, linkat, lseek64, lstat64, mkdir, open64, opendir, readdir64_r, readlink, |
| realpath, rename, rmdir, stat64, symlink, unlink, |
| }; |
| pub use sgx_libc::*; |
| } |
| |
| pub use remove_dir_impl::remove_dir_all; |
| |
| mod remove_dir_impl { |
| use super::{cstr, lstat, Dir, DirEntry, InnerReadDir, ReadDir}; |
| use crate::ffi::CStr; |
| use crate::io; |
| use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; |
| use crate::os::unix::prelude::{OwnedFd, RawFd}; |
| use crate::path::{Path, PathBuf}; |
| use crate::sync::Arc; |
| use crate::sys::{cvt, cvt_r}; |
| use libc::{fdopendir, openat, unlinkat}; |
| |
| pub fn openat_nofollow_dironly(parent_fd: Option<RawFd>, p: &CStr) -> io::Result<OwnedFd> { |
| let fd = cvt_r(|| unsafe { |
| openat( |
| parent_fd.unwrap_or(libc::AT_FDCWD), |
| p.as_ptr(), |
| libc::O_CLOEXEC | libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_DIRECTORY, |
| ) |
| })?; |
| Ok(unsafe { OwnedFd::from_raw_fd(fd) }) |
| } |
| |
| fn fdreaddir(dir_fd: OwnedFd) -> io::Result<(ReadDir, RawFd)> { |
| let ptr = unsafe { fdopendir(dir_fd.as_raw_fd()) }; |
| if ptr.is_null() { |
| return Err(io::Error::last_os_error()); |
| } |
| let dirp = Dir(ptr); |
| // file descriptor is automatically closed by libc::closedir() now, so give up ownership |
| let new_parent_fd = dir_fd.into_raw_fd(); |
| // a valid root is not needed because we do not call any functions involving the full path |
| // of the DirEntrys. |
| let dummy_root = PathBuf::new(); |
| Ok(( |
| ReadDir { |
| inner: Arc::new(InnerReadDir { dirp, root: dummy_root }), |
| end_of_stream: false, |
| }, |
| new_parent_fd, |
| )) |
| } |
| |
| fn is_dir(ent: &DirEntry) -> Option<bool> { |
| match ent.entry.d_type { |
| libc::DT_UNKNOWN => None, |
| libc::DT_DIR => Some(true), |
| _ => Some(false), |
| } |
| } |
| |
| fn remove_dir_all_recursive(parent_fd: Option<RawFd>, p: &Path) -> io::Result<()> { |
| let pcstr = cstr(p)?; |
| |
| // entry is expected to be a directory, open as such |
| let fd = openat_nofollow_dironly(parent_fd, &pcstr)?; |
| |
| // open the directory passing ownership of the fd |
| let (dir, fd) = fdreaddir(fd)?; |
| for child in dir { |
| let child = child?; |
| match is_dir(&child) { |
| Some(true) => { |
| remove_dir_all_recursive(Some(fd), Path::new(&child.file_name()))?; |
| } |
| Some(false) => { |
| cvt(unsafe { unlinkat(fd, child.name_cstr().as_ptr(), 0) })?; |
| } |
| None => match cvt(unsafe { unlinkat(fd, child.name_cstr().as_ptr(), 0) }) { |
| // type unknown - try to unlink |
| Err(err) |
| if err.raw_os_error() == Some(libc::EISDIR) |
| || err.raw_os_error() == Some(libc::EPERM) => |
| { |
| // if the file is a directory unlink fails with EISDIR on Linux and EPERM everyhwere else |
| remove_dir_all_recursive(Some(fd), Path::new(&child.file_name()))?; |
| } |
| result => { |
| result?; |
| } |
| }, |
| } |
| } |
| |
| // unlink the directory after removing its contents |
| cvt(unsafe { |
| unlinkat(parent_fd.unwrap_or(libc::AT_FDCWD), pcstr.as_ptr(), libc::AT_REMOVEDIR) |
| })?; |
| Ok(()) |
| } |
| |
| pub fn remove_dir_all(p: &Path) -> io::Result<()> { |
| // We cannot just call remove_dir_all_recursive() here because that would not delete a passed |
| // symlink. No need to worry about races, because remove_dir_all_recursive() does not recurse |
| // into symlinks. |
| let attr = lstat(p)?; |
| if attr.file_type().is_symlink() { |
| crate::fs::remove_file(p) |
| } else { |
| remove_dir_all_recursive(None, p) |
| } |
| } |
| |
| mod libc { |
| pub use sgx_libc::ocall::{fdopendir, openat, unlinkat}; |
| pub use sgx_libc::*; |
| } |
| } |