Start work on output collector

This commit is contained in:
Austen Adler 2023-11-15 23:29:11 -05:00
parent d3deecfb45
commit 2ea91652b7
6 changed files with 81 additions and 8 deletions

1
Cargo.lock generated
View File

@ -15,6 +15,7 @@ dependencies = [
"anyhow",
"crossbeam",
"ncdufmt",
"serde_json",
"tar",
]

View File

@ -15,4 +15,5 @@ members = [
anyhow = "1.0.75"
crossbeam = { version = "0.8.2", features = ["crossbeam-channel"] }
ncdufmt = {path="./ncdufmt/"}
serde_json = "1.0.108"
tar = "0.4.40"

View File

@ -19,7 +19,7 @@ use serde::{Deserialize, Serialize};
// This is based on https://dev.yorhel.nl/ncdu/jsonfmt
#[derive(Debug, PartialEq, Clone)]
struct NcduFile {
pub struct NcduFile {
pub major_version: MajorVersion,
pub minor_version: MinorVersion,
pub header_metadata: HeaderMetadata,
@ -125,10 +125,10 @@ pub struct HeaderMetadata {
impl HeaderMetadata {
/// Construct a HeaderMetadata with the current time
pub fn new(progname: String, progver: String) -> Self {
pub fn new(progname: impl AsRef<str>, progver: impl AsRef<str>) -> Self {
Self {
progname,
progver,
progname: progname.as_ref().to_string(),
progver: progver.as_ref().to_string(),
timestamp: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.as_ref()
@ -140,8 +140,8 @@ impl HeaderMetadata {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Directory {
info: InfoBlock,
contents: Vec<FileOrDirectory>,
pub info: InfoBlock,
pub contents: Vec<FileOrDirectory>,
}
impl Serialize for Directory {
@ -295,6 +295,24 @@ pub struct InfoBlock {
pub extended_info_block: Option<ExtendedInfoBlock>,
}
impl InfoBlock {
pub fn new(name: impl AsRef<str>) -> Self {
Self {
name: name.as_ref().to_string(),
dev: Default::default(),
asize: Default::default(),
dsize: Default::default(),
hlnkc: Default::default(),
ino: Default::default(),
nlink: Default::default(),
read_error: Default::default(),
excluded: Default::default(),
notreg: Default::default(),
extended_info_block: Default::default(),
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct ExtendedInfoBlock {
/// Number, user ID who owns the file. Accepted values are in the range 0 <= uid < 2^31.

51
src/collector.rs Normal file
View File

@ -0,0 +1,51 @@
use std::io::Write;
use crate::types;
use anyhow::Result;
use ncdufmt::{MajorVersion, MinorVersion, HeaderMetadata, InfoBlock, NcduFile, Directory, FileOrDirectory};
/// Builds an ncdu file from a stream
pub fn build_ncdu_file(rx: crossbeam::channel::Receiver<types::Entry>/*, _writer: impl Write*/) -> Result<NcduFile> {
let mut ret = NcduFile {
major_version: MajorVersion::default(),
minor_version: MinorVersion::default(),
header_metadata: HeaderMetadata::new("test-program", "0.1.1"),
contents: Directory {
info: InfoBlock::new("test-filename.tar".to_string()),
contents: Vec::default(),
}
};
while let Ok(entry) = rx.recv() {
let mut e = InfoBlock::new(entry.path.to_string_lossy());
e.asize = entry.size;
ret.contents.contents.push(FileOrDirectory::File(e) );
}
Ok(ret)
}
#[cfg(test)]
mod tests {
use std::fs::File;
use super::*;
#[test]
fn simple_tree() {
// Check that this tar file of mine has a Cargo.lock and 10 entries
let reader = File::open("./test-tar.tar").unwrap();
let (tx, rx) = crossbeam::channel::bounded(100);
crate::tar::get_entries(tx, reader).unwrap();
let ret = build_ncdu_file(rx).unwrap();
eprintln!("{ret:#?}");
eprintln!("{}", serde_json::to_string(&ret).unwrap());
panic!();
}
}

View File

@ -1,5 +1,7 @@
mod tar;
mod types;
mod collector;
fn main() {
println!("Hello, world!");
}

View File

@ -9,8 +9,8 @@ use tar::Archive;
///
/// The tar file must be in a decompressed state
pub fn get_entries(
reader: impl Read,
tx: crossbeam::channel::Sender<types::Entry>,
reader: impl Read,
) -> Result<()> {
let mut archive = Archive::new(reader);
@ -38,7 +38,7 @@ mod tests {
let reader = File::open("./test-tar.tar").unwrap();
let (tx, rx) = crossbeam::channel::bounded(100);
get_entries(reader, tx).unwrap();
get_entries(tx, reader).unwrap();
let mut results = vec![];
while let Ok(entry) = rx.recv() {