Skip to content

Commit

Permalink
implement aux fn's
Browse files Browse the repository at this point in the history
  • Loading branch information
George-Miao committed Jun 20, 2024
1 parent 07b1c32 commit f4689cb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
81 changes: 76 additions & 5 deletions core/src/services/compfs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use compio::dispatcher::Dispatcher;
use compio::{dispatcher::Dispatcher, fs::OpenOptions};

use super::{core::CompfsCore, lister::CompfsLister, reader::CompfsReader, writer::CompfsWriter};

Expand Down Expand Up @@ -112,16 +112,86 @@ impl Access for CompfsBackend {

copy: true,
rename: true,
blocking: true,

..Default::default()
});

am
}

async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> {
let path = self.core.prepare_path(path);

self.core
.exec(move || async move { compio::fs::create_dir_all(path).await })
.await?;

Ok(RpCreateDir::default())
}

async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
let path = self.core.prepare_path(path);

let meta = self
.core
.exec(move || async move { compio::fs::metadata(path).await })
.await?;
let ty = meta.file_type();
let mode = if ty.is_dir() {
EntryMode::DIR
} else if ty.is_file() {
EntryMode::FILE
} else {
EntryMode::Unknown
};
let last_mod = meta.modified().map_err(new_std_io_error)?.into();
let ret = Metadata::new(mode).with_last_modified(last_mod);

Ok(RpStat::new(ret))
}

async fn delete(&self, path: &str, _: OpDelete) -> Result<RpDelete> {
let path = self.core.prepare_path(path);

self.core
.exec(move || async move { compio::fs::remove_file(path).await })
.await?;

Ok(RpDelete::default())
}

async fn copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {
let from = self.core.prepare_path(from);
let to = self.core.prepare_path(to);

self.core
.exec(move || async move {
let from = OpenOptions::new().read(true).open(from).await?;
let to = OpenOptions::new().write(true).create(true).open(to).await?;

let (mut from, mut to) = (Cursor::new(from), Cursor::new(to));
compio::io::copy(&mut from, &mut to).await?;

Ok(())
})
.await?;

Ok(RpCopy::default())
}

async fn rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
let from = self.core.prepare_path(from);
let to = self.core.prepare_path(to);

self.core
.exec(move || async move { compio::fs::rename(from, to).await })
.await?;

Ok(RpRename::default())
}

async fn read(&self, path: &str, op: OpRead) -> Result<(RpRead, Self::Reader)> {
let path = self.core.root.join(path.trim_end_matches('/'));
let path = self.core.prepare_path(path);

let file = self
.core
Expand All @@ -133,7 +203,7 @@ impl Access for CompfsBackend {
}

async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
let path = self.core.root.join(path.trim_end_matches('/'));
let path = self.core.prepare_path(path);
let append = args.append();
let file = self
.core
Expand All @@ -153,7 +223,8 @@ impl Access for CompfsBackend {
}

async fn list(&self, path: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
let path = self.core.root.join(path.trim_end_matches('/'));
let path = self.core.prepare_path(path);

let read_dir = match self
.core
.exec_blocking(move || std::fs::read_dir(path))
Expand Down
4 changes: 4 additions & 0 deletions core/src/services/compfs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub(super) struct CompfsCore {
}

impl CompfsCore {
pub fn prepare_path(&self, path: &str) -> PathBuf {
self.root.join(path.trim_end_matches('/'))
}

pub async fn exec<Fn, Fut, R>(&self, f: Fn) -> crate::Result<R>
where
Fn: FnOnce() -> Fut + Send + 'static,
Expand Down

0 comments on commit f4689cb

Please sign in to comment.