Skip to content

Commit

Permalink
!draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Aug 17, 2021
1 parent 50f3af4 commit f117fec
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 188 deletions.
57 changes: 43 additions & 14 deletions lib/vfs/src/host_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::debug;

impl FileDescriptor {
#[cfg(unix)]
fn as_raw(&self) -> Result<RawFd> {
self.0.try_into().map_err(|_| FsError::InvalidFd)
}

#[cfg(windows)]
fn as_raw(&self) -> Result<RawHandle> {
self.0.try_into().map_err(|_| FsError::InvalidFd)
}
}

trait TryIntoFileDescriptor {
type Error;

Expand All @@ -49,6 +37,15 @@ where
}
}

#[cfg(unix)]
impl TryInto<RawFd> for FileDescriptor {
type Error = FsError;

fn try_into(self) -> std::result::Result<RawFd, Self::Error> {
self.0.try_into().map_err(|_| FsError::InvalidFd)
}
}

#[cfg(windows)]
impl<T> TryIntoFileDescriptor for T
where
Expand All @@ -65,7 +62,16 @@ where
}
}

#[derive(Debug, Default, Clone)]
#[cfg(windows)]
impl TryInto<RawHandle> for FileDescriptor {
type Error = FsError;

fn try_into(self) -> std::result::Result<RawHandle, Self::Error> {
self.0.try_into().map_err(|_| FsError::InvalidFd)
}
}

#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct FileSystem;

Expand Down Expand Up @@ -296,15 +302,19 @@ impl File {
/// creates a new host file from a `std::fs::File` and a path
pub fn new(file: fs::File, host_path: PathBuf, read: bool, write: bool, append: bool) -> Self {
let mut flags = 0;

if read {
flags |= Self::READ;
}

if write {
flags |= Self::WRITE;
}

if append {
flags |= Self::APPEND;
}

Self {
inner: file,
host_path,
Expand All @@ -321,31 +331,39 @@ impl Read for File {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.inner.read_to_end(buf)
}

fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
self.inner.read_to_string(buf)
}

fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.inner.read_exact(buf)
}
}

impl Seek for File {
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
self.inner.seek(pos)
}
}

impl Write for File {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}

fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.inner.write_all(buf)
}

fn write_fmt(&mut self, fmt: ::std::fmt::Arguments) -> io::Result<()> {
self.inner.write_fmt(fmt)
}
Expand Down Expand Up @@ -391,6 +409,7 @@ impl VirtualFile for File {
fn unlink(&mut self) -> Result<()> {
fs::remove_file(&self.host_path).map_err(Into::into)
}

fn sync_to_disk(&self) -> Result<()> {
self.inner.sync_all().map_err(Into::into)
}
Expand All @@ -403,7 +422,7 @@ impl VirtualFile for File {
#[cfg(unix)]
fn host_file_bytes_available(host_fd: FileDescriptor) -> Result<usize> {
let mut bytes_found = 0 as libc::c_int;
let result = unsafe { libc::ioctl(host_fd.as_raw()?, libc::FIONREAD, &mut bytes_found) };
let result = unsafe { libc::ioctl(host_fd.try_into()?, libc::FIONREAD, &mut bytes_found) };

match result {
// success
Expand All @@ -425,47 +444,56 @@ fn host_file_bytes_available(_host_fd: FileDescriptor) -> Result<usize> {
#[derive(Debug, Default)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct Stdout;

impl Read for Stdout {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from stdout",
))
}

fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> io::Result<usize> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from stdout",
))
}

fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from stdout",
))
}

fn read_exact(&mut self, _buf: &mut [u8]) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from stdout",
))
}
}

impl Seek for Stdout {
fn seek(&mut self, _pos: io::SeekFrom) -> io::Result<u64> {
Err(io::Error::new(io::ErrorKind::Other, "can not seek stdout"))
}
}

impl Write for Stdout {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
io::stdout().write(buf)
}

fn flush(&mut self) -> io::Result<()> {
io::stdout().flush()
}

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
io::stdout().write_all(buf)
}

fn write_fmt(&mut self, fmt: ::std::fmt::Arguments) -> io::Result<()> {
io::stdout().write_fmt(fmt)
}
Expand Down Expand Up @@ -512,6 +540,7 @@ impl VirtualFile for Stdout {
#[derive(Debug, Default)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct Stderr;

impl Read for Stderr {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(
Expand Down
14 changes: 14 additions & 0 deletions lib/vfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@ pub mod mem_fs;
pub struct FileDescriptor(usize);

pub trait FileSystem: fmt::Debug + Send + Sync + 'static {
/// Read a directory.
fn read_dir(&self, path: &Path) -> Result<ReadDir>;

/// Create a directory.
fn create_dir(&self, path: &Path) -> Result<()>;

/// Delete a directory.
fn remove_dir(&self, path: &Path) -> Result<()>;

/// Rename a file.
fn rename(&self, from: &Path, to: &Path) -> Result<()>;

/// Fetch the metadata of a file.
fn metadata(&self, path: &Path) -> Result<Metadata>;

/// This method gets metadata without following symlinks in the path.
/// Currently identical to `metadata` because symlinks aren't implemented
/// yet.
fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
self.metadata(path)
}

/// Delete a file.
fn remove_file(&self, path: &Path) -> Result<()>;

/// Create `OpenOptions`.
fn new_open_options(&self) -> OpenOptions;
}

Expand Down
Loading

0 comments on commit f117fec

Please sign in to comment.