Commit 4d243c43 authored by Alexis Janon's avatar Alexis Janon
Browse files

Path verification preparation: added ref to controllers in {Create,Remove}Dir

Will be used to ensure that all filesystem operations are done in
authorized paths (i.e. paths with mounted controllers)
parent c670174e
use configuration::sysfscontroller::SysFsControllerList;
use result::*;
use std::fs;
use std::io;
use std::path;
use std::rc::Rc;
use transaction::*;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct CreateDir {
path: path::PathBuf,
controllers: Rc<SysFsControllerList>,
}
impl CreateDir {
pub fn new(path: path::PathBuf) -> Self {
CreateDir { path }
pub fn new(path: path::PathBuf, controllers: Rc<SysFsControllerList>) -> Self {
CreateDir { path, controllers }
}
}
......@@ -71,9 +74,10 @@ mod tests {
#[test]
fn test_already_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", EXISTING_NAME));
assert!(fs::create_dir(&path).is_ok());
let mut command = CreateDir::new(path.to_path_buf());
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_ok());
assert!(path.exists());
assert!(fs::remove_dir(&path).is_ok());
......@@ -81,8 +85,9 @@ mod tests {
#[test]
fn test_not_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", NOT_EXISTING_NAME));
let mut command = CreateDir::new(path.to_path_buf());
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_ok());
assert!(path.exists());
assert!(fs::remove_dir(&path).is_ok());
......@@ -90,17 +95,19 @@ mod tests {
#[test]
fn test_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", NOT_FOUND));
assert!(fs::create_dir(&path).is_ok());
let mut command = CreateDir::new(path.to_path_buf());
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_ok());
assert!(!path.exists());
}
#[test]
fn test_not_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", FOUND));
let mut command = CreateDir::new(path.to_path_buf());
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_ok());
assert!(!path.exists());
}
......
use configuration::sysfscontroller::SysFsControllerList;
use std::fs;
use std::io;
use std::path;
use std::rc::Rc;
use transaction::*;
// TODO: use an "inverse" wrapper struct for CreateDir with deref and newtype pattern?
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct RemoveDir {
path: path::PathBuf,
controllers: Rc<SysFsControllerList>,
}
impl RemoveDir {
pub fn new(path: path::PathBuf) -> Self {
RemoveDir { path }
pub fn new(path: path::PathBuf, controllers: Rc<SysFsControllerList>) -> Self {
RemoveDir { path, controllers }
}
}
......@@ -72,9 +75,10 @@ mod tests {
#[test]
fn test_already_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", EXISTING_NAME));
assert!(fs::create_dir(&path).is_ok());
let mut command = RemoveDir::new(path.to_path_buf());
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_ok());
assert!(path.exists());
assert!(fs::remove_dir(&path).is_ok());
......@@ -82,8 +86,9 @@ mod tests {
#[test]
fn test_not_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", NOT_EXISTING_NAME));
let mut command = RemoveDir::new(path.to_path_buf());
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_ok());
assert!(path.exists());
assert!(fs::remove_dir(&path).is_ok());
......@@ -91,17 +96,19 @@ mod tests {
#[test]
fn test_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", NOT_FOUND));
assert!(fs::create_dir(&path).is_ok());
let mut command = RemoveDir::new(path.to_path_buf());
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_ok());
assert!(!path.exists());
}
#[test]
fn test_not_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let path = PathBuf::from(format!("/tmp/{}", FOUND));
let mut command = RemoveDir::new(path.to_path_buf());
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_ok());
assert!(!path.exists());
}
......
......@@ -39,7 +39,7 @@ impl CreateSlice {
.map(SysFsController::get_path)
.map(path::Path::to_path_buf)
.map(|path| path.join(&name))
.map(CreateDir::new)
.map(|path| CreateDir::new(path, Rc::clone(&controllers)))
.map(Box::new)
.map(|b| b as Box<Command>)
.collect();
......
......@@ -16,7 +16,7 @@ impl RemoveSlice {
.map(SysFsController::get_path)
.map(path::Path::to_path_buf)
.map(|path| path.join(&name))
.map(RemoveDir::new)
.map(|path| RemoveDir::new(path, Rc::clone(&controllers)))
.map(Box::new)
.map(|b| b as Box<Command>)
.collect();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment