Skip to content

Commit

Permalink
feat(core): basic copy
Browse files Browse the repository at this point in the history
Signed-off-by: suyanhanx <suyanhanx@gmail.com>
  • Loading branch information
suyanhanx committed Apr 2, 2023
1 parent 5787e99 commit 3b4d56c
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/examples/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async fn main() -> Result<()> {
.try_init();

// Pick a builder and configure it.
let builder = services::Memory::default();
let mut builder = services::Fs::default();
builder.root("/tmp");

// Init an operator
let op = Operator::new(builder)?
Expand All @@ -41,9 +42,14 @@ async fn main() -> Result<()> {

debug!("operator: {op:?}");

op.is_exist("test").await?;

// Write data into object test.
op.write("test", "Hello, World!").await?;

// try copy
op.copy("test", "test2").await?;

// Read data from object.
let bs = op.read("test").await?;
info!("content: {}", String::from_utf8_lossy(&bs));
Expand Down
41 changes: 41 additions & 0 deletions core/src/layers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,47 @@ impl<A: Accessor> LayeredAccessor for LoggingAccessor<A> {
})
}

async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
debug!(
target: LOGGING_TARGET,
"service={} operation={} from={} to={} -> started",
self.scheme,
Operation::Copy,
from,
to
);

self.inner
.copy(from, to, args)
.await
.map(|v| {
debug!(
target: LOGGING_TARGET,
"service={} operation={} from={} to={} -> finished",
self.scheme,
Operation::Copy,
from,
to
);
v
})
.map_err(|err| {
if let Some(lvl) = self.err_level(&err) {
log!(
target: LOGGING_TARGET,
lvl,
"service={} operation={} from={} to={} -> {}: {err:?}",
self.scheme,
Operation::Copy,
from,
to,
self.err_status(&err)
)
};
err
})
}

async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
debug!(
target: LOGGING_TARGET,
Expand Down
15 changes: 15 additions & 0 deletions core/src/raw/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ pub trait Accessor: Send + Sync + Debug + Unpin + 'static {
))
}

/// Invoke the `copy` operation on the specified `from` path and `to` path.
async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
let (_, _, _) = (from, to, args);

Err(Error::new(
ErrorKind::Unsupported,
"operation is not supported",
))
}

/// Invoke the `stat` operation on the specified path.
///
/// Require [`AccessorCapability::Read`]
Expand Down Expand Up @@ -369,6 +379,11 @@ impl<T: Accessor + ?Sized> Accessor for Arc<T> {
async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
self.as_ref().write(path, args).await
}

async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
self.as_ref().copy(from, to, args).await
}

async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
self.as_ref().stat(path, args).await
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/raw/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ pub trait LayeredAccessor: Send + Sync + Debug + Unpin + 'static {

async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)>;

async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
self.inner().copy(from, to, args).await
}

async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
self.inner().stat(path, args).await
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/raw/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum Operation {
Read,
/// Operation for [`crate::raw::Accessor::write`]
Write,
/// Operation for [`crate::raw::Accessor::copy`]
Copy,
/// Operation for [`crate::raw::Accessor::stat`]
Stat,
/// Operation for [`crate::raw::Accessor::delete`]
Expand Down Expand Up @@ -84,6 +86,7 @@ impl From<Operation> for &'static str {
Operation::Create => "create",
Operation::Read => "read",
Operation::Write => "write",
Operation::Copy => "copy",
Operation::Stat => "stat",
Operation::Delete => "delete",
Operation::List => "list",
Expand Down
11 changes: 11 additions & 0 deletions core/src/raw/rps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ impl RpWrite {
}
}

/// Reply for `copy` operation.
#[derive(Debug, Clone, Default)]
pub struct RpCopy {}

impl RpCopy {
/// Create a new reply for copy.
pub fn new() -> Self {
Self {}
}
}

#[cfg(test)]
mod tests {
use anyhow::Result;
Expand Down
11 changes: 11 additions & 0 deletions core/src/services/fs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,17 @@ impl Accessor for FsBackend {
Ok((RpWrite::new(), FsWriter::new(target_path, tmp_path, f)))
}

async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
tokio::fs::copy(
self.root.join(from.trim_end_matches('/')),
self.root.join(to.trim_end_matches('/')),
)
.await
.map_err(|e| Error::new(ErrorKind::Unexpected, e.to_string().as_ref()))?;

Ok(RpCopy::default())
}

async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
let p = self.root.join(path.trim_end_matches('/'));

Expand Down
7 changes: 7 additions & 0 deletions core/src/types/operator/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,13 @@ impl Operator {
self.write_with(path, OpWrite::new(), bs).await
}

/// Copy a file from `from` to `to`.
pub async fn copy(&self, from: &str, to: &str) -> Result<()> {
self.inner().copy(from, to, OpCopy::new()).await?;

Ok(())
}

/// Write multiple bytes into path.
///
/// # Notes
Expand Down
11 changes: 11 additions & 0 deletions core/src/types/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,14 @@ impl OpWrite {
self
}
}

/// Args for `copy` operation.
#[derive(Debug, Clone, Default)]
pub struct OpCopy {}

impl OpCopy {
/// Create a new `OpCopy`.
pub fn new() -> Self {
Self::default()
}
}

0 comments on commit 3b4d56c

Please sign in to comment.