Commit a563af18 authored by Xavier Thompson's avatar Xavier Thompson

Simplify rust version

parent 05d5030a
...@@ -4,10 +4,8 @@ use serde::Serialize; ...@@ -4,10 +4,8 @@ use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::{ use std::{
fs::DirEntry, fs::DirEntry,
fs::File,
io::{Read, Write},
path::PathBuf, path::PathBuf,
path::Path, io::{Read, Write, stdout},
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
...@@ -70,10 +68,8 @@ struct Tree { ...@@ -70,10 +68,8 @@ struct Tree {
fn construct_fs_tree( fn construct_fs_tree(
cur_tree: Option<Tree>, cur_tree: Option<Tree>,
path: &PathBuf, path: &PathBuf,
dev_whitelist: &Vec<u64>,
ignored_dirs: &Vec<PathBuf>,
) -> Result<Tree> { ) -> Result<Tree> {
let mut cur_tree = match cur_tree { let cur_tree = match cur_tree {
Some(cur_tree) => cur_tree, Some(cur_tree) => cur_tree,
None => Tree { None => Tree {
stat: Some(nix::sys::stat::lstat(path)?.into()), stat: Some(nix::sys::stat::lstat(path)?.into()),
...@@ -81,18 +77,6 @@ fn construct_fs_tree( ...@@ -81,18 +77,6 @@ fn construct_fs_tree(
}, },
}; };
if !dev_whitelist.iter().any(|x| match &cur_tree.stat {
Some(stat) if stat.st_dev == *x => true,
_ => false,
}) {
return Ok(cur_tree);
}
if ignored_dirs.iter().any(|x| path.starts_with(x)) {
cur_tree.ignored = true;
return Ok(cur_tree);
}
let entries: Vec<Result<DirEntry, _>> = match std::fs::read_dir(&path) { let entries: Vec<Result<DirEntry, _>> = match std::fs::read_dir(&path) {
Ok(x) => x, Ok(x) => x,
_ => return Ok(cur_tree), _ => return Ok(cur_tree),
...@@ -117,8 +101,6 @@ fn construct_fs_tree( ...@@ -117,8 +101,6 @@ fn construct_fs_tree(
tree = construct_fs_tree( tree = construct_fs_tree(
Some(tree), Some(tree),
&entry.path(), &entry.path(),
dev_whitelist,
ignored_dirs,
) )
.unwrap(); .unwrap();
} }
...@@ -189,35 +171,14 @@ fn construct_fs_tree( ...@@ -189,35 +171,14 @@ fn construct_fs_tree(
} }
fn main() -> Result<()> { fn main() -> Result<()> {
let ignored_dirs = ["/opt/slapgrid", "/srv/slapgrid"]
.iter()
.map(PathBuf::from)
.collect();
let disk_partitions = [".", "/", "/boot"];
let dev_whitelist = disk_partitions
.iter()
.filter_map(|p| match nix::sys::stat::lstat(&PathBuf::from(p)) {
Ok(stat) => Some(stat.st_dev),
Err(_) => None,
})
.collect();
let tree = construct_fs_tree( let tree = construct_fs_tree(
None, None,
&PathBuf::from("."), &PathBuf::from("."),
&dev_whitelist,
&ignored_dirs,
)?; )?;
let result = serde_json::to_string_pretty(&tree)?; let result = serde_json::to_string_pretty(&tree)?;
let path = Path::new("result.json"); stdout().write_all(result.as_bytes()).unwrap();
let mut file = File::create(&path).unwrap();
file.write_all(result.as_bytes()).unwrap();
Ok(()) Ok(())
} }
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