This commit is contained in:
2025-07-16 08:24:58 +02:00
parent a2cb58867c
commit aeda56863b
22 changed files with 370 additions and 309 deletions

View File

@@ -1,7 +1,10 @@
use std::time::UNIX_EPOCH;
use anyhow::{Context, anyhow, bail};
use tokio::{fs, io::AsyncWriteExt as _};
use tokio::{
fs,
io::{self, AsyncWriteExt as _},
};
use crate::domain::warren::{
models::file::{
@@ -91,9 +94,13 @@ impl FileSystem {
/// Actually created a directory in the underlying file system
///
/// * `path`: The directory's absolute path (absolute not in relation to the root file system but `self.base_directory`)
async fn create_dir(&self, path: &AbsoluteFilePath) -> anyhow::Result<FilePath> {
async fn create_dir(&self, path: &AbsoluteFilePath) -> io::Result<FilePath> {
let file_path = self.get_target_path(path);
if fs::try_exists(&file_path).await? {
return Err(io::ErrorKind::AlreadyExists.into());
}
fs::create_dir(&file_path).await?;
Ok(file_path)
@@ -103,7 +110,7 @@ impl FileSystem {
///
/// * `path`: The directory's absolute path (absolute not in relation to the root file system but `self.base_directory`)
/// * `force`: Whether to delete directories that are not empty
async fn remove_dir(&self, path: &AbsoluteFilePath, force: bool) -> anyhow::Result<FilePath> {
async fn remove_dir(&self, path: &AbsoluteFilePath, force: bool) -> io::Result<FilePath> {
let file_path = self.get_target_path(path);
if force {
@@ -157,7 +164,7 @@ impl FileSystem {
};
if fs::try_exists(&new_path).await? {
bail!("File already exists");
bail!("File exists");
}
fs::rename(current_path, &new_path).await?;
@@ -182,35 +189,40 @@ impl FileSystemRepository for FileSystem {
&self,
request: CreateDirectoryRequest,
) -> Result<FilePath, CreateDirectoryError> {
let created_path = self.create_dir(request.path()).await.context(format!(
"Failed to create directory at path {}",
request.path()
))?;
Ok(created_path)
match self.create_dir(request.path()).await {
Ok(path) => Ok(path),
Err(e) => match e.kind() {
std::io::ErrorKind::AlreadyExists => Err(CreateDirectoryError::Exists),
_ => Err(anyhow!("Failed to create directory at path: {}", request.path()).into()),
},
}
}
async fn delete_directory(
&self,
request: DeleteDirectoryRequest,
) -> Result<FilePath, DeleteDirectoryError> {
let deleted_path = self
.remove_dir(request.path(), false)
.await
.context(format!("Failed to delete directory at {}", request.path()))?;
Ok(deleted_path)
match self.remove_dir(request.path(), request.force()).await {
Ok(deleted_path) => return Ok(deleted_path),
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => Err(DeleteDirectoryError::NotFound),
std::io::ErrorKind::DirectoryNotEmpty => Err(DeleteDirectoryError::NotEmpty),
_ => Err(anyhow!("Failed to delete directory at {}: {e:?}", request.path()).into()),
},
}
}
async fn create_file(&self, request: CreateFileRequest) -> Result<FilePath, CreateFileError> {
let file_path = self
.write_file(request.path(), request.data())
.await
.context(format!(
"Failed to write {} byte(s) to path {}",
request.data().len(),
request.path()
))?;
.map_err(|e| {
anyhow!(
"Failed to write {} byte(s) to path {}: {e:?}",
request.data().len(),
request.path()
)
})?;
Ok(file_path)
}
@@ -231,11 +243,13 @@ impl FileSystemRepository for FileSystem {
let new_path = self
.rename(request.path(), request.new_name())
.await
.context(format!(
"Failed to rename {} to {}",
request.path(),
request.new_name()
))?;
.map_err(|e| {
anyhow!(
"Failed to rename {} to {}: {e:?}",
request.path(),
request.new_name()
)
})?;
Ok(new_path)
}