From: Ian Jackson Date: Sun, 15 May 2022 17:37:22 +0000 (+0100) Subject: bundle auto-zip: wip X-Git-Tag: otter-1.1.0~94 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=92236c901b4619249a1f2a8f1c982e6a607e264a;p=otter.git bundle auto-zip: wip Signed-off-by: Ian Jackson --- diff --git a/Cargo.lock b/Cargo.lock index ddf6c4c9..1adc166b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2773,6 +2773,7 @@ dependencies = [ "serde", "serde_with", "strum", + "walkdir", ] [[package]] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 0a73bb50..bd33d398 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -31,6 +31,7 @@ otter-base.version="=1.0.0" argparse="0.2" directories="4" ron="0.7" +walkdir="2.3" fehler="1" num-traits="0.2" diff --git a/cli/otter.rs b/cli/otter.rs index 91d99dd6..944d31c0 100644 --- a/cli/otter.rs +++ b/cli/otter.rs @@ -23,6 +23,7 @@ pub use std::rc::Rc; pub use argparse::{self,ArgumentParser,action::{TypedAction,ParseResult}}; pub use argparse::action::{Action,IFlagAction,IArgAction}; pub use derive_more::Display; +pub use walkdir::WalkDir; pub use otter::prelude::*; pub use otter::commands::*; diff --git a/cli/usebundles.rs b/cli/usebundles.rs index 91f83f3d..1f0461a4 100644 --- a/cli/usebundles.rs +++ b/cli/usebundles.rs @@ -18,9 +18,30 @@ pub struct BundleForUpload { impl BundleForUpload { #[throws(AE)] pub fn prepare(file: String) -> Self { - let f = File::open(&file) - .with_context(|| file.clone()) - .context("open bundle file")?; + let mut walk = WalkDir::new(&file) + .same_file_system(true) + .follow_links(true) + .sort_by_file_name() + .into_iter() + .peekable(); + + let peek = walk.peek().ok_or_else(|| anyhow!("unable to inspect"))?; + if_let!{ + Ok(peek) = peek; + else Err( + walk.next().unwrap().unwrap_err() + ).context("inspect")? + }; + if ! peek.file_type().is_dir() { + let f = File::open(&file).context("open")?; + Self::prepare_open_file(&file, f)? + } else { + Self::prepare_from_dir(&file, walk)? + } + } + + #[throws(AE)] + pub fn prepare_open_file(file: &str, f: File) -> Self { let size = f .metadata().context("fstat bundle file")? .len() @@ -31,7 +52,7 @@ impl BundleForUpload { let hash = bundles::Hash(hash.into()); let kind = bundles::Kind::only(); f.rewind().context("rewind bundle file")?; - BundleForUpload { file, f, size, hash, kind } + BundleForUpload { file: file.to_owned(), f, size, hash, kind } } #[throws(AE)] @@ -52,6 +73,16 @@ impl BundleForUpload { progress.clear(); bundle } + +// #[throws(AE)] + pub fn prepare_from_dir(_file: &str, walk: W) -> Result + where W: Iterator> + { + for ent in walk { + eprintln!("GOT {:?} {:?}", &ent, &ent.as_ref().unwrap().file_name()); + } + panic!("NYI") + } } mod upload_bundle {