Skip to content
This repository was archived by the owner on Oct 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bcfs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bcfs"
version = "0.2.2"
version = "0.2.3"
license = "Apache-2.0"
authors = ["Oasis Labs <feedback@oasislabs.com>"]
edition = "2018"
Expand Down
121 changes: 46 additions & 75 deletions bcfs/src/bcfs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{
cell::{Cell, RefCell},
convert::TryFrom,
io::{Cursor, IoSlice, IoSliceMut, Read, Seek as _, SeekFrom, Write},
convert::TryFrom as _,
io::{Cursor, IoSlice, IoSliceMut, Read as _, Seek as _, SeekFrom, Write as _},
path::{Path, PathBuf},
str::FromStr as _,
};

use blockchain_traits::{AccountMeta, Address, PendingTransaction};
use blockchain_traits::PendingTransaction;
use oasis_types::Address;
use wasi_types::{
ErrNo, Fd, FdFlags, FdStat, FileDelta, FileSize, FileStat, FileType, OpenFlags, Rights, Whence,
};
Expand All @@ -15,32 +17,26 @@ use crate::{
Result,
};

pub struct BCFS<A: Address, M: AccountMeta> {
files: Vec<Option<File<A>>>,
home_addr: A,
_account_meta: std::marker::PhantomData<M>,
pub struct BCFS {
files: Vec<Option<File>>,
home_addr: Address,
}

impl<A: Address, M: AccountMeta> BCFS<A, M> {
impl BCFS {
/// Creates a new ptx FS with a backing `ptx` and hex stringified
/// owner address.
pub fn new<S: AsRef<str>>(
home_addr: A,
// ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
home_addr: Address,
// ptx: &mut dyn PendingTransaction< AccountMeta = M>,
blockchain_name: S,
) -> Self {
Self {
files: File::defaults(blockchain_name.as_ref()),
home_addr,
_account_meta: std::marker::PhantomData,
}
}

pub fn prestat(
&mut self,
_ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
fd: Fd,
) -> Result<&Path> {
pub fn prestat(&mut self, _ptx: &mut dyn PendingTransaction, fd: Fd) -> Result<&Path> {
match &self.file(fd)?.kind {
FileKind::Directory { path } => Ok(path),
_ => Err(ErrNo::BadF),
Expand All @@ -49,7 +45,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

pub fn open(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
curdir: Fd,
path: &Path,
open_flags: OpenFlags,
Expand Down Expand Up @@ -121,10 +117,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
Ok(fd)
}

pub fn tempfile(
&mut self,
_ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
) -> Result<Fd> {
pub fn tempfile(&mut self, _ptx: &mut dyn PendingTransaction) -> Result<Fd> {
let fd = self.alloc_fd()?;
self.files.push(Some(File {
kind: FileKind::Temporary,
Expand All @@ -136,20 +129,12 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
Ok(fd)
}

pub fn flush(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
fd: Fd,
) -> Result<()> {
pub fn flush(&mut self, ptx: &mut dyn PendingTransaction, fd: Fd) -> Result<()> {
self.do_flush(ptx, self.file(fd)?);
Ok(())
}

pub fn close(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
fd: Fd,
) -> Result<()> {
pub fn close(&mut self, ptx: &mut dyn PendingTransaction, fd: Fd) -> Result<()> {
self.flush(ptx, fd)?;
match self.files.get_mut(fd_usize(fd)) {
Some(f) if f.is_some() => {
Expand All @@ -163,7 +148,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
/// Removes the file at `path` and returns the number of bytes previously in the file.
pub fn unlink(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
curdir: Fd,
path: &Path,
) -> Result<u64> {
Expand Down Expand Up @@ -191,7 +176,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

pub fn seek(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
fd: Fd,
offset: FileDelta,
whence: Whence,
Expand Down Expand Up @@ -231,11 +216,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
}
}

pub fn fdstat(
&self,
_ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
fd: Fd,
) -> Result<FdStat> {
pub fn fdstat(&self, _ptx: &mut dyn PendingTransaction, fd: Fd) -> Result<FdStat> {
let file = self.file(fd)?;
Ok(FdStat {
file_type: FileType::RegularFile,
Expand All @@ -245,20 +226,12 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
})
}

pub fn filestat(
&self,
ptx: &dyn PendingTransaction<Address = A, AccountMeta = M>,
fd: Fd,
) -> Result<FileStat> {
pub fn filestat(&self, ptx: &dyn PendingTransaction, fd: Fd) -> Result<FileStat> {
let file = self.file(fd)?;
Self::populate_file(ptx, file, &mut *file.buf.borrow_mut())
}

pub fn tell(
&self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
fd: Fd,
) -> Result<FileSize> {
pub fn tell(&self, ptx: &mut dyn PendingTransaction, fd: Fd) -> Result<FileSize> {
let file = self.file(fd)?;
let mut buf = file.buf.borrow_mut();
if let FileCache::Absent(SeekFrom::End(_)) = &*buf {
Expand All @@ -276,7 +249,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

pub fn read_vectored(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
fd: Fd,
bufs: &mut [IoSliceMut],
) -> Result<usize> {
Expand All @@ -285,7 +258,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

pub fn pread_vectored(
&self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
fd: Fd,
bufs: &mut [IoSliceMut],
offset: FileSize,
Expand All @@ -295,7 +268,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

pub fn write_vectored(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
fd: Fd,
bufs: &[IoSlice],
) -> Result<usize> {
Expand All @@ -304,7 +277,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

pub fn pwrite_vectored(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
fd: Fd,
bufs: &[IoSlice],
offset: FileSize,
Expand All @@ -314,7 +287,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

pub fn renumber(
&mut self,
_ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
_ptx: &mut dyn PendingTransaction,
fd: Fd,
new_fd: Fd,
) -> Result<()> {
Expand All @@ -327,7 +300,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
}
}

pub fn sync(&mut self, ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>) {
pub fn sync(&mut self, ptx: &mut dyn PendingTransaction) {
// flush stdout and stderr
for file in self.files[1..=3].iter() {
if let Some(file) = file {
Expand All @@ -353,8 +326,8 @@ fn seekfrom_from_offset_whence(offset: FileDelta, whence: Whence) -> Result<Seek
})
}

impl<A: Address, M: AccountMeta> BCFS<A, M> {
fn canonicalize_path(&self, curdir: Fd, path: &Path) -> Result<(Option<A>, PathBuf)> {
impl BCFS {
fn canonicalize_path(&self, curdir: Fd, path: &Path) -> Result<(Option<Address>, PathBuf)> {
use std::path::Component;

if path.has_root() {
Expand All @@ -372,13 +345,15 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

let addr = if curdir_fileno == CHAIN_DIR_FILENO {
match comps.peek() {
Some(Component::Normal(maybe_addr)) => match maybe_addr.to_str().map(A::from_str) {
Some(Ok(addr)) => {
comps.next();
Some(addr)
Some(Component::Normal(maybe_addr)) => {
match maybe_addr.to_str().map(Address::from_str) {
Some(Ok(addr)) => {
comps.next();
Some(addr)
}
_ => None,
}
_ => None,
},
}
Some(Component::Prefix(_)) | Some(Component::RootDir) => return Err(ErrNo::NoEnt),
_ => None,
}
Expand Down Expand Up @@ -417,14 +392,14 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
}
}

fn file(&self, fd: Fd) -> Result<&File<A>> {
fn file(&self, fd: Fd) -> Result<&File> {
match self.files.get(fd_usize(fd)) {
Some(Some(file)) => Ok(file),
_ => Err(ErrNo::BadF),
}
}

fn file_mut(&mut self, fd: Fd) -> Result<&mut File<A>> {
fn file_mut(&mut self, fd: Fd) -> Result<&mut File> {
match self
.files
.get_mut(usize::try_from(u64::from(fd)).map_err(|_| ErrNo::BadF)?)
Expand Down Expand Up @@ -457,8 +432,8 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
}

fn populate_file(
ptx: &dyn PendingTransaction<Address = A, AccountMeta = M>,
file: &File<A>,
ptx: &dyn PendingTransaction,
file: &File,
cache: &mut FileCache,
) -> Result<FileStat> {
let file_size = match cache {
Expand All @@ -471,7 +446,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
None => return Err(ErrNo::NoEnt),
},
FileKind::Balance { addr } => match ptx.account_meta_at(&addr) {
Some(meta) => meta.balance().to_le_bytes().to_vec(),
Some(meta) => meta.balance.to_le_bytes().to_vec(),
None => return Err(ErrNo::NoEnt),
},
FileKind::Regular { key } => match ptx.state().get(&key) {
Expand Down Expand Up @@ -509,7 +484,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

fn do_pread_vectored(
&self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
fd: Fd,
bufs: &mut [IoSliceMut],
offset: Option<SeekFrom>,
Expand Down Expand Up @@ -544,7 +519,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {

fn do_pwrite_vectored(
&mut self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
ptx: &mut dyn PendingTransaction,
fd: Fd,
bufs: &[IoSlice],
offset: Option<SeekFrom>,
Expand Down Expand Up @@ -581,11 +556,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
Ok(nbytes)
}

fn do_flush(
&self,
ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
file: &File<A>,
) {
fn do_flush(&self, ptx: &mut dyn PendingTransaction, file: &File) {
if !file.dirty.get() {
return;
}
Expand Down Expand Up @@ -616,7 +587,7 @@ impl<A: Address, M: AccountMeta> BCFS<A, M> {
}) = f
{
let f = f.as_ref().unwrap();
if key != f_key || f as *const File<A> == file as *const File<A> {
if key != f_key || f as *const File == file as *const File {
continue;
}
let mut f_buf = f.buf.borrow_mut();
Expand Down
16 changes: 8 additions & 8 deletions bcfs/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::{
path::PathBuf,
};

use blockchain_traits::Address;
use oasis_types::Address;
use wasi_types::{FdFlags, FileStat};

pub struct File<A: Address> {
pub kind: FileKind<A>,
pub struct File {
pub kind: FileKind,

pub flags: FdFlags,

Expand All @@ -27,19 +27,19 @@ pub enum FileCache {
Present(Cursor<Vec<u8>>),
}

pub enum FileKind<A: Address> {
pub enum FileKind {
Stdin,
Stdout,
Stderr,
Log,
Temporary,
Regular { key: Vec<u8> },
Balance { addr: A },
Bytecode { addr: A },
Balance { addr: Address },
Bytecode { addr: Address },
Directory { path: PathBuf },
}

impl<A: Address> FileKind<A> {
impl FileKind {
pub fn is_log(&self) -> bool {
match self {
FileKind::Log => true,
Expand Down Expand Up @@ -88,7 +88,7 @@ macro_rules! special_file_ctor {
}
}

impl<A: Address> File<A> {
impl File {
special_file_ctor!(Stdin, Stdout, Stderr);
}

Expand Down
Loading