Skip to content

Commit

Permalink
Add rename syscall to FilesystemClient
Browse files Browse the repository at this point in the history
This patch adds a rename syscall to the FilesystemClient that allows
applications to move a file within the same storage location and within
the client namespace.
  • Loading branch information
robin-nitrokey committed Nov 28, 2023
1 parent f7b65c9 commit b178180
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added `init_raw` constructor for types generated by the `store!` macro.
- Added `FilesystemClient::entry_metadata` syscall.
- Added `FilesystemClient::rename` syscall.
- Added `Syscall` implementation for `Service`.
- Added `Syscall::try_into_new_client` service.
- Added serializable flag to `StorageAttributes` for key agreement.
Expand Down
8 changes: 8 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ generate_enums! {
ReadDirFilesNext: 14
ReadFile: 15
Metadata: 26
Rename: 27
// ReadCounter: 7
RandomBytes: 16
SerializeKey: 17
Expand Down Expand Up @@ -255,6 +256,11 @@ pub mod request {
- location: Location
- path: PathBuf

Rename:
- location: Location
- from: PathBuf
- to: PathBuf

RemoveFile:
- location: Location
- path: PathBuf
Expand Down Expand Up @@ -449,6 +455,8 @@ pub mod reply {
Metadata:
- metadata: Option<crate::types::Metadata>

Rename:

RemoveDir:

RemoveDirAll:
Expand Down
13 changes: 13 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,19 @@ pub trait FilesystemClient: PollClient {
self.request(request::Metadata { location, path })
}

/// Rename a file or directory.
///
/// If `to` exists, it must be the same type as `from` (i. e., both must be files or both must
/// be directories). If `to` is a directory, it must be empty.
fn rename(
&mut self,
location: Location,
from: PathBuf,
to: PathBuf,
) -> ClientResult<'_, reply::Rename, Self> {
self.request(request::Rename { location, from, to })
}

fn locate_file(
&mut self,
location: Location,
Expand Down
5 changes: 5 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ impl<P: Platform> ServiceResources<P> {
}))
}

Request::Rename(request) => {
filestore.rename(&request.from, &request.to, request.location)?;
Ok(Reply::Rename(reply::Rename {}))
}

Request::RandomBytes(request) => {
if request.count <= MAX_MESSAGE_LENGTH {
let mut bytes = Message::new();
Expand Down
11 changes: 11 additions & 0 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,17 @@ pub fn metadata(
}
}

#[inline(never)]
pub fn rename(store: impl Store, location: Location, from: &Path, to: &Path) -> Result<(), Error> {
debug_now!("renaming {} to {}", &from, &to);
match location {
Location::Internal => store.ifs().rename(from, to),
Location::External => store.efs().rename(from, to),
Location::Volatile => store.vfs().rename(from, to),
}
.map_err(|_| Error::FilesystemWriteFailure)
}

#[inline(never)]
pub fn remove_dir(store: impl Store, location: Location, path: &Path) -> bool {
debug_now!("remove_dir'ing {}", &path);
Expand Down
7 changes: 7 additions & 0 deletions src/store/filestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub trait Filestore {
fn write(&mut self, path: &Path, location: Location, data: &[u8]) -> Result<()>;
fn exists(&mut self, path: &Path, location: Location) -> bool;
fn metadata(&mut self, path: &Path, location: Location) -> Result<Option<Metadata>>;
fn rename(&mut self, from: &Path, to: &Path, location: Location) -> Result<()>;
fn remove_file(&mut self, path: &Path, location: Location) -> Result<()>;
fn remove_dir(&mut self, path: &Path, location: Location) -> Result<()>;
fn remove_dir_all(&mut self, path: &Path, location: Location) -> Result<usize>;
Expand Down Expand Up @@ -376,6 +377,12 @@ impl<S: Store> Filestore for ClientFilestore<S> {
store::metadata(self.store, location, &path)
}

fn rename(&mut self, from: &Path, to: &Path, location: Location) -> Result<()> {
let from = self.actual_path(from)?;
let to = self.actual_path(to)?;
store::rename(self.store, location, &from, &to)
}

fn remove_file(&mut self, path: &Path, location: Location) -> Result<()> {
let path = self.actual_path(path)?;

Expand Down

0 comments on commit b178180

Please sign in to comment.