You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.4 KiB

6 years ago
use std::fs::{create_dir, read_dir};
use std::path::PathBuf;
use crate::config::Config;
use crate::error::*;
static DIRS: &[&str] = &[
"bin", "dev", "etc", "home", "lib", "lib64", "proc", "root", "run", "sbin", "sys", "tmp",
"usr", "var", "usr/bin",
];
pub fn check(config: &Config) -> MkrootResult<()> {
if !&config.root_dir.exists() {
return Ok(());
}
open(&config.root_dir, config.verbose)?;
for dir in DIRS {
let mut d = PathBuf::from(&config.root_dir);
d.push(&dir);
open(&d, config.verbose)?;
}
Ok(())
}
fn open(dir: &PathBuf, verbose: bool) -> MkrootResult<()> {
if dir.exists() {
if verbose {
println!("Checking directory {}", &dir.display());
}
if let Err(e) = read_dir(&dir) {
return Err(MkrootError::from(format!(
"Error opening directory {}: {}",
&dir.display(),
e
)));
}
}
Ok(())
}
pub fn create(config: &Config) -> MkrootResult<()> {
mkdir(&config.root_dir, config.verbose)?;
for dir in DIRS {
let mut d = PathBuf::from(&config.root_dir);
d.push(&dir);
mkdir(&d, config.verbose)?;
}
Ok(())
}
fn mkdir(dir: &PathBuf, verbose: bool) -> MkrootResult<()> {
if !dir.exists() {
if verbose {
println!("Creating directory {}", &dir.display());
}
if let Err(e) = create_dir(&dir) {
return Err(MkrootError::from(format!(
"Error creating directory {}: {}",
&dir.display(),
e
)));
}
}
Ok(())
}