123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- use crate::error::{MountError, AfsFreeError, AfsMkdirError};
- use crate::raw_fn;
- /// Afs Instance
- pub struct AfsInstance (i32);
- impl AfsInstance {
- /// Create a new AfsInstance from raw handle
- pub fn new_from_handle(handle: i32) -> Self {
- Self(handle)
- }
- /// Mount the AfsInstance to specified mount point
- pub fn mount(&self, mnt_path: &str) -> Result<(), MountError> {
- let r = unsafe {
- raw_fn::vfs_mount(self.0, mnt_path.as_ptr(), mnt_path.len())
- };
- match r {
- 0 => Ok(()),
- -1 => Err(MountError::InvalidAfsHandle),
- -2 => Err(MountError::InternalError(
- String::from("vfs_mount: reading MntPath from memory out of bounds")
- )),
- -3 => Err(MountError::MountPointAlreadyMounted),
- -4 => Err(MountError::InternalError(
- String::from("vfs_mount: reading MntPath from memory out of bounds")
- )),
- _ => Err(MountError::InternalError(
- format!("vfs_mount: unknown error: code = {}", r)
- )),
- }
- }
- /// Free the AfsInstance
- /// The lifecycle of wasm_init is shorter than the lifecycle of the entire ngvfs,
- /// so we can't utilize Rust's Drop trait to automatically release the AfsInstance object,
- /// and you need to manually call free to release it if there is an unneeded AfsInstance object.
- /// After this free function has been called, do not use this object again.
- pub fn free(&self) -> Result<(), AfsFreeError> {
- let r = unsafe {
- raw_fn::afs_free(self.0)
- };
- match r {
- 0 => Ok(()),
- -1 => Err(AfsFreeError::InvalidAfsHandle),
- _ => Err(AfsFreeError::InternalError(
- format!("vfs_free: unknown error: code = {}", r)
- ))
- }
- }
- pub fn get_handle_value(&self) -> i32 {
- self.0
- }
- /// Create directory on AfsInstance. Works like `mkdir -p`.
- pub fn mkdir(&self, path: &str, mode: i32) -> Result<(), AfsMkdirError> {
- let r = unsafe {
- raw_fn::afs_mkdir(self.0, path.as_ptr(), path.len(), mode)
- };
- match r {
- 0 => Ok(()),
- -2 => Err(AfsMkdirError::InvalidAfsHandle),
- -3 => Err(AfsMkdirError::InternalError(
- String::from("afs_mkdir: reading path from memory out of bounds")
- )),
- -1 => Err(AfsMkdirError::InternalError(
- String::from("afs_mkdir: failed create directory: see logs.")
- )),
- _ => Err(AfsMkdirError::InternalError(
- format!("afs_mkdir: unknown error: code = {}", r)
- )),
- }
- }
- /// New afero.OsFs Instance
- pub fn new_afero_osfs() -> Self {
- let r = unsafe {
- raw_fn::afs_create_osfs()
- };
- if r > 0 {
- Self(r)
- }else{
- panic!("failed to create afero.OsFs: err_code={}", r)
- }
- }
- /// New afero.MemFs Instance
- pub fn new_afero_memfs() -> Self {
- let r = unsafe {
- raw_fn::afs_create_memfs()
- };
- if r > 0 {
- Self(r)
- }else{
- panic!("failed to create afero.MemFs: err_code={}", r)
- }
- }
- /// New afero.BasePathFs Instance.
- pub fn new_afero_bpfs(base_fs: &AfsInstance, base_path: &str) -> Self {
- let r = unsafe {
- raw_fn::afs_create_bpfs(base_fs.0, base_path.as_ptr(), base_path.len())
- };
- if r > 0 {
- Self(r)
- }else{
- panic!("failed to create afero.BasePathFs: err_code={}", r)
- }
- }
- /// New afero.RegexpFs Instance
- pub fn new_afero_regfs(base_fs: &AfsInstance, regexp: &str) -> Self {
- let r = unsafe {
- raw_fn::afs_create_regfs(base_fs.0, regexp.as_ptr(), regexp.len())
- };
- if r > 0 {
- Self(r)
- }else{
- panic!("failed to create afero.RegexpFs: err_code={}", r)
- }
- }
- /// New afero.ReadOnlyFs Instance
- pub fn new_afero_rofs(base_fs: &AfsInstance) -> Self {
- let r = unsafe {
- raw_fn::afs_create_rofs(base_fs.0)
- };
- if r > 0 {
- Self(r)
- }else{
- panic!("failed to create afero.ReadOnlyFs: err_code={}", r)
- }
- }
- /// New afero.CopyOnWriteFs Instance
- pub fn new_afero_cowfs(base_ro_fs: &AfsInstance, base_wr_fs: &AfsInstance) -> Self {
- let r = unsafe {
- raw_fn::afs_create_cowfs(base_ro_fs.0, base_wr_fs.0)
- };
- if r > 0 {
- Self(r)
- }else{
- panic!("failed to create afero.CopyOnWriteFs: err_code={}", r)
- }
- }
- /// New afero.CacheOnReadFs Instance. cacheSeconds is the cache time in seconds.
- /// if cache_time_secs == 0, cache is permanent.
- /// if cache_time_secs < 0, will cause error.
- pub fn new_afero_corfs(base_ro_fs: &AfsInstance, base_wr_fs: &AfsInstance, cache_time_secs: i32) -> Self {
- let r = unsafe {
- raw_fn::afs_create_corfs(base_ro_fs.0, base_wr_fs.0, cache_time_secs)
- };
- if r > 0 {
- Self(r)
- }else{
- panic!("failed to create afero.CacheOnReadFs: err_code={}", r)
- }
- }
- }
|