From df1edefb9db75bda165e5aa76159c1a7a6479fee Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 8 Jan 2023 00:32:15 +0530 Subject: [PATCH] feat: implement latest_tar for publiccodeyml --- federate/publiccodeyml/src/lib.rs | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/federate/publiccodeyml/src/lib.rs b/federate/publiccodeyml/src/lib.rs index 1142b9f..aefca24 100644 --- a/federate/publiccodeyml/src/lib.rs +++ b/federate/publiccodeyml/src/lib.rs @@ -256,4 +256,44 @@ impl Federate for PccFederate { Ok(path) } + + /// get latest tar ball + async fn latest_tar(&self) -> Result { + use std::fs::File; + use std::time::{SystemTime, UNIX_EPOCH}; + + use tar::Builder; + + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs(); + + let path = Path::new(&self.base_dir).join(format!("{now}.tar")); + let file = File::create(&path)?; + let mut a = Builder::new(file); + a.append_dir_all(".", self.get_content_path(false).await?) + .unwrap(); + a.finish().unwrap(); + + let mut times: Vec = Vec::with_capacity(10); + let mut dir = fs::read_dir(Path::new(&self.base_dir)).await?; + while let Some(d) = dir.next_entry().await? { + if d.path().is_dir() { + continue; + } + let file = d.file_name().into_string().unwrap(); + if file.ends_with(".tar") { + if let Some(time) = file.split(".tar").next() { + times.push(time.parse::().unwrap()); + } + } + } + + times.sort(); + + let latest = times.pop().unwrap(); + let latest = Path::new(&self.base_dir).join(format!("{}.tar", latest.to_string())); + Ok(latest) + } }