Skip to content

Commit

Permalink
Move from crypto::fs::async to crypto::fs, so we don't need to use r#…
Browse files Browse the repository at this point in the history
…async. Make sure we zeroize String from path

[async] fs, File and io API #97
  • Loading branch information
radumarias committed Dec 15, 2024
1 parent 997528c commit 921c1a9
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/file_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion examples/filesystem_dbg.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/fs_api.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod r#async;
pub mod fs;
pub mod path;
1 change: 0 additions & 1 deletion src/crypto/fs_api/async.rs

This file was deleted.

20 changes: 9 additions & 11 deletions src/crypto/fs_api/async/fs.rs → src/crypto/fs_api/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ impl File {
}
}

pub async fn metadata<P: AsRef<Path>>(path: P) -> std::io::Result<Metadata> {
pub async fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
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?;
Expand All @@ -373,10 +373,10 @@ pub async fn metadata<P: AsRef<Path>>(path: P) -> std::io::Result<Metadata> {
Ok(metadata)
}

pub async fn exists<P: AsRef<Path>>(path: P) -> std::io::Result<bool> {
pub async fn exists<P: AsRef<Path>>(path: P) -> io::Result<bool> {
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)
}

Expand Down Expand Up @@ -565,9 +565,7 @@ impl Metadata {

fn get_path_from_secret(path: SecretBox<String>) -> Vec<SecretBox<String>> {
let input = path.expose_secret();
let input = input.to_string();
let path = Path::new(&input);

let path = Path::new(&*input);
parse_path(path)
}

Expand Down Expand Up @@ -602,7 +600,7 @@ pub fn parse_path(path: &Path) -> Vec<SecretBox<String>> {
stack
}

async fn validate_path_exists(path: impl AsRef<Path>) -> std::io::Result<(SecretBox<String>, u64)> {
async fn get_parent_and_child(path: impl AsRef<Path>) -> io::Result<(u64, SecretBox<String>)> {
let mut dir_inode = 1;
let fs = get_fs().await?;

Expand All @@ -627,7 +625,7 @@ async fn validate_path_exists(path: impl AsRef<Path>) -> 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<Arc<EncryptedFs>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
6 changes: 3 additions & 3 deletions src/crypto/fs_api/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Path {
/// println!("{:?}", metadata.file_type());
/// ```
pub fn metadata(&self) -> Result<Metadata> {
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<Metadata> {
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Path {
///
/// Due to how the paths are canonicalized, they may leak.
pub fn try_exists(&self) -> Result<bool> {
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 {
Expand Down
18 changes: 6 additions & 12 deletions src/encryptedfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,16 @@ pub enum FsError {
ReadOnly,
}

impl std::convert::From<FsError> for io::Error {
impl From<FsError> 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()),
}
}
}
Expand Down

0 comments on commit 921c1a9

Please sign in to comment.