Compare commits
7 commits
396d4b77bc
...
d58e46d65e
Author | SHA1 | Date | |
---|---|---|---|
d58e46d65e | |||
b32c10f080 | |||
a9057a8831 | |||
12d55b98e5 | |||
a77ed422eb | |||
9c76ddc3e1 | |||
483d274e5b |
5 changed files with 300 additions and 34 deletions
|
@ -9,6 +9,7 @@ repository = "https://git.kemitix.net/kemitix/kxio"
|
|||
exclude = [".cargo_home"]
|
||||
|
||||
[features]
|
||||
default = ["fs", "network"]
|
||||
fs = []
|
||||
network = []
|
||||
|
||||
|
@ -30,6 +31,7 @@ thiserror = "1.0"
|
|||
|
||||
# fs
|
||||
tempfile = "3.10"
|
||||
path-clean = "1.0"
|
||||
|
||||
# error handling
|
||||
derive_more = { version = "1.0.0-beta.6", features = ["from", "display"] }
|
||||
|
|
18
src/fs/like.rs
Normal file
18
src/fs/like.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use super::Result;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub trait FileSystemLike {
|
||||
fn base(&self) -> &Path;
|
||||
|
||||
fn dir_create(&self, path: &Path) -> Result<()>;
|
||||
fn dir_create_all(&self, path: &Path) -> Result<()>;
|
||||
|
||||
fn file_read_to_string(&self, path: &Path) -> Result<String>;
|
||||
fn file_write(&self, path: &Path, contents: &str) -> Result<()>;
|
||||
|
||||
fn path_exists(&self, path: &Path) -> Result<bool>;
|
||||
fn path_is_dir(&self, path: &Path) -> Result<bool>;
|
||||
fn path_is_file(&self, path: &Path) -> Result<bool>;
|
||||
fn path_of(&self, path: PathBuf) -> Result<PathBuf>;
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use derive_more::From;
|
||||
|
||||
use crate::fs::like::FileSystemLike;
|
||||
|
||||
mod like;
|
||||
mod real;
|
||||
mod temp;
|
||||
|
||||
|
@ -29,7 +32,6 @@ pub enum FileSystem {
|
|||
Real(real::RealFileSystem),
|
||||
Temp(temp::TempFileSystem),
|
||||
}
|
||||
|
||||
impl std::ops::Deref for FileSystem {
|
||||
type Target = dyn FileSystemLike;
|
||||
|
||||
|
@ -40,11 +42,3 @@ impl std::ops::Deref for FileSystem {
|
|||
}
|
||||
}
|
||||
}
|
||||
pub trait FileSystemLike {
|
||||
fn base(&self) -> &Path;
|
||||
fn path_of(&self, path: PathBuf) -> Result<PathBuf>;
|
||||
fn file_write(&self, path: &Path, contents: &str) -> Result<()>;
|
||||
fn file_read_to_string(&self, path: &Path) -> Result<String>;
|
||||
fn path_exists(&self, path: &Path) -> Result<bool>;
|
||||
fn path_is_file(&self, path: &Path) -> Result<bool>;
|
||||
}
|
||||
|
|
|
@ -12,15 +12,15 @@ impl super::FileSystemLike for RealFileSystem {
|
|||
fn base(&self) -> &Path {
|
||||
&self.base
|
||||
}
|
||||
fn path_of(&self, path: PathBuf) -> super::Result<PathBuf> {
|
||||
let path_of = self.base.as_path().join(path);
|
||||
self.validate(&path_of)?;
|
||||
Ok(path_of)
|
||||
|
||||
fn dir_create(&self, path: &Path) -> super::Result<()> {
|
||||
self.validate(path)?;
|
||||
std::fs::create_dir(path).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn file_write(&self, path: &Path, contents: &str) -> super::Result<()> {
|
||||
fn dir_create_all(&self, path: &Path) -> super::Result<()> {
|
||||
self.validate(path)?;
|
||||
std::fs::write(path, contents).map_err(Into::into)
|
||||
std::fs::create_dir_all(path).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn file_read_to_string(&self, path: &Path) -> super::Result<String> {
|
||||
|
@ -28,25 +28,54 @@ impl super::FileSystemLike for RealFileSystem {
|
|||
std::fs::read_to_string(path).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn path_is_file(&self, path: &Path) -> super::Result<bool> {
|
||||
fn file_write(&self, path: &Path, contents: &str) -> super::Result<()> {
|
||||
self.validate(path)?;
|
||||
Ok(path.is_file())
|
||||
std::fs::write(path, contents).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn path_exists(&self, path: &Path) -> super::Result<bool> {
|
||||
self.validate(path)?;
|
||||
Ok(path.exists())
|
||||
}
|
||||
|
||||
fn path_is_dir(&self, path: &Path) -> super::Result<bool> {
|
||||
self.validate(path)?;
|
||||
Ok(path.is_dir())
|
||||
}
|
||||
|
||||
fn path_is_file(&self, path: &Path) -> super::Result<bool> {
|
||||
self.validate(path)?;
|
||||
Ok(path.is_file())
|
||||
}
|
||||
|
||||
fn path_of(&self, path: PathBuf) -> super::Result<PathBuf> {
|
||||
let path_of = self.base.as_path().join(path);
|
||||
self.validate(&path_of)?;
|
||||
Ok(path_of)
|
||||
}
|
||||
}
|
||||
|
||||
impl RealFileSystem {
|
||||
fn validate(&self, path: &std::path::Path) -> super::Result<()> {
|
||||
fn validate(&self, path: &Path) -> super::Result<()> {
|
||||
let path = self.clean_path(path)?;
|
||||
if !path.starts_with(&self.base) {
|
||||
return Err(super::Error::PathTraversal {
|
||||
base: self.base.clone(),
|
||||
path: path.to_path_buf(),
|
||||
path,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clean_path(&self, path: &Path) -> super::Result<PathBuf> {
|
||||
// let path = path.as_ref();
|
||||
use path_clean::PathClean;
|
||||
let abs_path = if path.is_absolute() {
|
||||
path.to_path_buf()
|
||||
} else {
|
||||
std::env::current_dir()?.join(path)
|
||||
}
|
||||
.clean();
|
||||
Ok(abs_path)
|
||||
}
|
||||
}
|
||||
|
|
251
src/tests/fs.rs
251
src/tests/fs.rs
|
@ -1,20 +1,243 @@
|
|||
use std::path::PathBuf;
|
||||
use assert2::let_assert;
|
||||
|
||||
use crate::fs;
|
||||
|
||||
type TestResult = Result<(), crate::fs::Error>;
|
||||
type TestResult = Result<(), fs::Error>;
|
||||
|
||||
#[test]
|
||||
fn write_read_file_exists() -> TestResult {
|
||||
let temp_fs = fs::temp()?;
|
||||
let name: PathBuf = temp_fs.path_of("foo".into())?;
|
||||
temp_fs.file_write(&name, "content")?;
|
||||
let c = temp_fs.file_read_to_string(&name)?;
|
||||
assert_eq!(c, "content");
|
||||
let exists = temp_fs.path_exists(&name)?;
|
||||
assert!(exists);
|
||||
let is_file = temp_fs.path_is_file(&name)?;
|
||||
assert!(is_file);
|
||||
mod path_of {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn validate_fails_on_path_traversal() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
|
||||
Ok(())
|
||||
let_assert!(Err(fs::Error::PathTraversal { base, path: _path }) = fs.path_of("..".into()));
|
||||
assert_eq!(base, fs.base());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod file {
|
||||
use super::*;
|
||||
#[test]
|
||||
/// Write to a file, read it, verify it exists, is a file and has the expected contents
|
||||
fn write_read_file_exists() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let pathbuf = fs.base().join("foo");
|
||||
|
||||
let_assert!(Ok(_) = fs.file_write(&pathbuf, "content"));
|
||||
let_assert!(
|
||||
Ok(c) = fs.file_read_to_string(&pathbuf),
|
||||
"file_read_to_string"
|
||||
);
|
||||
assert_eq!(c, "content");
|
||||
|
||||
let_assert!(Ok(exists) = fs.path_exists(&pathbuf));
|
||||
assert!(exists);
|
||||
|
||||
let_assert!(Ok(is_file) = fs.path_is_file(&pathbuf));
|
||||
assert!(is_file);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod dir_create {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn should_create_a_dir() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let pathbuf = fs.base().join("subdir");
|
||||
|
||||
let_assert!(Ok(_) = fs.dir_create(&pathbuf));
|
||||
|
||||
let_assert!(Ok(exists) = fs.path_exists(&pathbuf));
|
||||
assert!(exists);
|
||||
|
||||
let_assert!(Ok(is_dir) = fs.path_is_dir(&pathbuf));
|
||||
assert!(is_dir);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_fail_on_path_traversal() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("..").join("foo");
|
||||
let_assert!(
|
||||
Err(fs::Error::PathTraversal {
|
||||
base: _base,
|
||||
path: _path
|
||||
}) = fs.dir_create(&path)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod dir_create_all {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_create_a_dir() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let pathbuf = fs.base().join("subdir").join("child");
|
||||
|
||||
let_assert!(Ok(_) = fs.dir_create_all(&pathbuf));
|
||||
|
||||
let_assert!(Ok(exists) = fs.path_exists(&pathbuf));
|
||||
assert!(exists, "path exists");
|
||||
|
||||
let_assert!(Ok(is_dir) = fs.path_is_dir(&pathbuf));
|
||||
assert!(is_dir, "path is a directory");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_fail_on_path_traversal() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("..").join("foo");
|
||||
let_assert!(
|
||||
Err(fs::Error::PathTraversal {
|
||||
base: _base,
|
||||
path: _path
|
||||
}) = fs.dir_create_all(&path)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod path_exists {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn should_be_true_when_it_exists() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
let_assert!(Ok(_) = fs.file_write(&path, "bar"));
|
||||
let_assert!(Ok(exists) = fs.path_exists(&path));
|
||||
assert!(exists);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_be_false_when_it_does_not_exist() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
let_assert!(Ok(exists) = fs.path_exists(&path));
|
||||
assert!(!exists);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_fail_on_path_traversal() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("..").join("foo");
|
||||
let_assert!(
|
||||
Err(fs::Error::PathTraversal {
|
||||
base: _base,
|
||||
path: _path
|
||||
}) = fs.path_exists(&path)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod path_is_dir {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn should_be_true_when_is_a_dir() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
let_assert!(Ok(_) = fs.dir_create(&path));
|
||||
let_assert!(Ok(is_dir) = fs.path_is_dir(&path));
|
||||
assert!(is_dir);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_be_false_when_is_a_file() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
let_assert!(Ok(_) = fs.file_write(&path, "bar"));
|
||||
let_assert!(Ok(is_dir) = fs.path_is_dir(&path));
|
||||
assert!(!is_dir);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn should_be_false_when_is_a_link() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
// TODO create a link
|
||||
// let_assert!(Ok(_) = fs.file_write(&path, "bar"));
|
||||
let_assert!(Ok(is_dir) = fs.path_is_dir(&path));
|
||||
assert!(!is_dir);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_fail_on_path_traversal() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("..").join("foo");
|
||||
let_assert!(
|
||||
Err(fs::Error::PathTraversal {
|
||||
base: _base,
|
||||
path: _path
|
||||
}) = fs.path_is_dir(&path)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod path_is_file {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn should_be_true_when_is_a_file() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
let_assert!(Ok(_) = fs.file_write(&path, "bar"));
|
||||
|
||||
let_assert!(Ok(is_file) = fs.path_is_file(&path));
|
||||
assert!(is_file);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_be_false_when_is_a_dir() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
let_assert!(Ok(_) = fs.dir_create(&path));
|
||||
let_assert!(Ok(is_file) = fs.path_is_file(&path));
|
||||
assert!(!is_file);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn should_be_false_when_is_a_link() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("foo");
|
||||
// TODO create a link
|
||||
// let_assert!(Ok(_) = fs.file_write(&path, "bar"));
|
||||
let_assert!(Ok(is_file) = fs.path_is_file(&path));
|
||||
assert!(!is_file);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn should_fail_on_path_traversal() -> TestResult {
|
||||
let fs = fs::temp()?;
|
||||
let path = fs.base().join("..").join("foo");
|
||||
let_assert!(
|
||||
Err(fs::Error::PathTraversal {
|
||||
base: _base,
|
||||
path: _path
|
||||
}) = fs.path_is_file(&path)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue