From 921c1a968ccc3298ba476584b5ea1acc1409a791 Mon Sep 17 00:00:00 2001 From: Radu Marias Date: Sun, 15 Dec 2024 22:08:28 +0200 Subject: [PATCH] Move from crypto::fs::async to crypto::fs, so we don't need to use r#async. Make sure we zeroize String from path [async] fs, File and io API #97 --- examples/file_layer.rs | 2 +- examples/filesystem_dbg.rs | 2 +- src/crypto/fs_api.rs | 2 +- src/crypto/fs_api/async.rs | 1 - src/crypto/fs_api/{async => }/fs.rs | 20 +++++++++----------- src/crypto/fs_api/{async => }/fs/test.rs | 2 +- src/crypto/fs_api/path.rs | 6 +++--- src/encryptedfs.rs | 18 ++++++------------ 8 files changed, 22 insertions(+), 31 deletions(-) delete mode 100644 src/crypto/fs_api/async.rs rename src/crypto/fs_api/{async => }/fs.rs (96%) rename src/crypto/fs_api/{async => }/fs/test.rs (99%) diff --git a/examples/file_layer.rs b/examples/file_layer.rs index ceabc9ae..37bb1388 100644 --- a/examples/file_layer.rs +++ b/examples/file_layer.rs @@ -5,7 +5,7 @@ use std::path::Path; use std::str::FromStr; use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt}; // use tokio::fs::OpenOptions; -use rencfs::crypto::fs_api::r#async::fs::OpenOptions; +use rencfs::crypto::fs_api::fs::OpenOptions; use rencfs::encryptedfs::PasswordProvider; static ROOT_CIPHER_FS_DATA_DIR: &str = "/tmp/rencfs/file_layer/fs_cipher"; diff --git a/examples/filesystem_dbg.rs b/examples/filesystem_dbg.rs index 2d669d48..671b97f2 100644 --- a/examples/filesystem_dbg.rs +++ b/examples/filesystem_dbg.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use rencfs::crypto::fs_api::r#async::fs::OpenOptions; +use rencfs::crypto::fs_api::fs::OpenOptions; use rencfs::crypto::Cipher; use rencfs::encryptedfs::{ CreateFileAttr, EncryptedFs, FileType, FsError, FsResult, PasswordProvider, diff --git a/src/crypto/fs_api.rs b/src/crypto/fs_api.rs index 54177e59..115e022d 100644 --- a/src/crypto/fs_api.rs +++ b/src/crypto/fs_api.rs @@ -1,2 +1,2 @@ -pub mod r#async; +pub mod fs; pub mod path; diff --git a/src/crypto/fs_api/async.rs b/src/crypto/fs_api/async.rs deleted file mode 100644 index d521fbd7..00000000 --- a/src/crypto/fs_api/async.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod fs; diff --git a/src/crypto/fs_api/async/fs.rs b/src/crypto/fs_api/fs.rs similarity index 96% rename from src/crypto/fs_api/async/fs.rs rename to src/crypto/fs_api/fs.rs index 5d8a3d5c..012433d9 100644 --- a/src/crypto/fs_api/async/fs.rs +++ b/src/crypto/fs_api/fs.rs @@ -358,13 +358,13 @@ impl File { } } -pub async fn metadata>(path: P) -> std::io::Result { +pub async fn metadata>(path: P) -> io::Result { let fs = get_fs().await?; - let (file_name, dir_inode) = validate_path_exists(&path).await?; + let (dir_inode, child) = get_parent_and_child(&path).await?; let attr = fs - .find_by_name(dir_inode, &file_name) + .find_by_name(dir_inode, &child) .await? .ok_or_else(|| FsError::InodeNotFound)?; let file_attr = fs.get_attr(attr.ino).await?; @@ -373,10 +373,10 @@ pub async fn metadata>(path: P) -> std::io::Result { Ok(metadata) } -pub async fn exists>(path: P) -> std::io::Result { +pub async fn exists>(path: P) -> io::Result { let fs = get_fs().await?; - let (file_name, dir_inode) = validate_path_exists(&path).await?; - let file_exists = fs.find_by_name(dir_inode, &file_name).await?.is_some(); + let (dir_inode, child) = get_parent_and_child(&path).await?; + let file_exists = fs.exists_by_name(dir_inode, &child)?; Ok(file_exists) } @@ -565,9 +565,7 @@ impl Metadata { fn get_path_from_secret(path: SecretBox) -> Vec> { let input = path.expose_secret(); - let input = input.to_string(); - let path = Path::new(&input); - + let path = Path::new(&*input); parse_path(path) } @@ -602,7 +600,7 @@ pub fn parse_path(path: &Path) -> Vec> { stack } -async fn validate_path_exists(path: impl AsRef) -> std::io::Result<(SecretBox, u64)> { +async fn get_parent_and_child(path: impl AsRef) -> io::Result<(u64, SecretBox)> { let mut dir_inode = 1; let fs = get_fs().await?; @@ -627,7 +625,7 @@ async fn validate_path_exists(path: impl AsRef) -> std::io::Result<(Secret .ok_or_else(|| FsError::InvalidInput("No filename"))? .to_owned(); - Ok((file_name, dir_inode)) + Ok((dir_inode, file_name)) } async fn get_fs() -> FsResult> { diff --git a/src/crypto/fs_api/async/fs/test.rs b/src/crypto/fs_api/fs/test.rs similarity index 99% rename from src/crypto/fs_api/async/fs/test.rs rename to src/crypto/fs_api/fs/test.rs index b4b7bb0c..146237f9 100644 --- a/src/crypto/fs_api/async/fs/test.rs +++ b/src/crypto/fs_api/fs/test.rs @@ -6,7 +6,7 @@ use std::str::FromStr; use shush_rs::SecretString; use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncSeekExt, AsyncWriteExt}; -use crate::crypto::fs_api::r#async::fs::OpenOptions; +use crate::crypto::fs_api::fs::OpenOptions; use crate::encryptedfs::{CreateFileAttr, FileType, PasswordProvider}; use crate::test_common::{get_fs, run_test, TestSetup}; diff --git a/src/crypto/fs_api/path.rs b/src/crypto/fs_api/path.rs index cadaf8ed..1f86130d 100644 --- a/src/crypto/fs_api/path.rs +++ b/src/crypto/fs_api/path.rs @@ -3,7 +3,7 @@ use crate::async_util; -use crate::crypto::fs_api::r#async::fs::{Metadata, OpenOptions}; +use crate::crypto::fs_api::fs::{Metadata, OpenOptions}; use crate::encryptedfs::{EncryptedFs, FsError, FsResult}; use std::borrow::Borrow; use std::collections::TryReserveError; @@ -173,7 +173,7 @@ impl Path { /// println!("{:?}", metadata.file_type()); /// ``` pub fn metadata(&self) -> Result { - async_util::call_async(crate::crypto::fs_api::r#async::fs::metadata(self)) + async_util::call_async(crate::crypto::fs_api::fs::metadata(self)) } pub fn symlink_metadata(&self) -> Result { @@ -236,7 +236,7 @@ impl Path { /// /// Due to how the paths are canonicalized, they may leak. pub fn try_exists(&self) -> Result { - async_util::call_async(crate::crypto::fs_api::r#async::fs::exists(self)) + async_util::call_async(crate::crypto::fs_api::fs::exists(self)) } pub fn is_file(&self) -> bool { diff --git a/src/encryptedfs.rs b/src/encryptedfs.rs index 86bb0bac..b644635e 100644 --- a/src/encryptedfs.rs +++ b/src/encryptedfs.rs @@ -307,22 +307,16 @@ pub enum FsError { ReadOnly, } -impl std::convert::From for io::Error { +impl From for io::Error { fn from(err: FsError) -> Self { match err { - FsError::InodeNotFound => { - std::io::Error::new(std::io::ErrorKind::NotFound, "Inode not found") - } + FsError::InodeNotFound => io::Error::new(io::ErrorKind::NotFound, "not found"), FsError::AlreadyExists => { - std::io::Error::new(std::io::ErrorKind::AlreadyExists, "File already exists") - } - FsError::InvalidInput(msg) => { - std::io::Error::new(std::io::ErrorKind::InvalidInput, msg) - } - FsError::ReadOnly => { - std::io::Error::new(std::io::ErrorKind::PermissionDenied, "Read only.") + io::Error::new(io::ErrorKind::AlreadyExists, "already exists") } - _ => std::io::Error::new(io::ErrorKind::Other, err.to_string()), + FsError::InvalidInput(msg) => io::Error::new(io::ErrorKind::InvalidInput, msg), + FsError::ReadOnly => io::Error::new(std::io::ErrorKind::PermissionDenied, "read only."), + _ => io::Error::new(io::ErrorKind::Other, err.to_string()), } } }