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.
dirstat/src/main.rs

121 lines
3.0 KiB

#![allow(dead_code)]
#[macro_use] extern crate pin_project;
#[macro_use] extern crate lazy_static;
#[cfg(feature="inspect")] use serde::{Serialize, Deserialize};
use color_eyre::{
eyre::{
self,
eyre,
WrapErr as _,
},
Help as _,
SectionExt as _,
};
#[macro_use] mod ext;
pub use ext::prelude::*;
mod data;
4 years ago
mod config;
mod state;
mod arg;
4 years ago
mod work;
#[cfg(feature="inspect")] mod serial;
#[cfg(feature="defer-drop")] mod defer_drop;
async fn normal(cfg: config::Config) -> eyre::Result<()>
{
let state = state::State::new(cfg
.validate()
.wrap_err(eyre!("Invalid config"))
.with_suggestion(|| "Try running `--help`")?);
let (graph, cfg) = tokio::select!{
x = work::work_on_all(state) => {x}
_ = tokio::signal::ctrl_c() => {
return Err(eyre!("Interrupt signalled, exiting"));
}
};
let graph = tokio::task::spawn_blocking(move || {
let mut graph = graph.into_hierarchical();
graph.compute_recursive_sizes();
graph
}).await.expect("Failed to compute hierarchy from graph");
#[cfg(debug_assertions)] eprintln!("{:?}", graph);
if cfg.is_using_stdout() {
eprintln!("Max size file: {:?}", graph.path_max_size_for(data::FsKind::File));
eprintln!("Max size dir: {:?}", graph.path_max_size_for(data::FsKind::Directory));
eprintln!("Max size all: {:?}", graph.path_max_size());
} else {
println!("Max size file: {:?}", graph.path_max_size_for(data::FsKind::File));
println!("Max size dir: {:?}", graph.path_max_size_for(data::FsKind::Directory));
println!("Max size all: {:?}", graph.path_max_size());
}
#[cfg(feature="inspect")]
match cfg.serialise_output.as_ref().map(|ser_out| {
type BoxedWrite = Box<dyn tokio::io::AsyncWrite + Unpin>;
use futures::FutureExt;
match ser_out {
Some(output_file) => {
use tokio::fs::OpenOptions;
async move {
let stream = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(output_file).await
.wrap_err(eyre!("Failed to open file for writing"))
.with_section(|| format!("{:?}", output_file).header("File was"))?;
Ok::<BoxedWrite, eyre::Report>(Box::new(stream))
}.boxed()
},
None => async move { Ok::<BoxedWrite, _>(Box::new(tokio::io::stdout())) }.boxed(),
}
}) {
Some(stream_fut) => {
let stream = stream_fut.await?;
serial::write_async(stream, &graph).await
.wrap_err(eyre!("Failed to serialise graph to stream"))?;
},
_ => (),
}
Ok(())
}
async fn parse_mode() -> eyre::Result<()>
{
match arg::parse_args().wrap_err(eyre!("Failed to parse args"))?
{
arg::Mode::Normal(cfg) => {
normal(cfg).await
},
arg::Mode::Help => arg::help(),
}
}
#[tokio::main]
async fn main() -> eyre::Result<()> {
color_eyre::install()?;
4 years ago
parse_mode().await
.wrap_err(eyre!("Fatal error"))?;
/* let max_size = graph.directories().map(|x| x.size()).max();
4 years ago
println!("Max size: {:?}", max_size);*/
//println!("{:?}", graph);
Ok(())
}