From f78a331e9c18e11f5123c14cd7987a5dbd869a78 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 19 May 2022 18:19:37 +0530 Subject: [PATCH] feat: retrun latest archive in tar() --- Cargo.lock | 33 +++++++++++++++++++++++++ federate/federate-core/src/lib.rs | 4 +++ federate/federate-core/src/tests.rs | 4 +++ federate/publiccodeyml/Cargo.toml | 1 + federate/publiccodeyml/src/lib.rs | 38 +++++++++++++++++++++++++---- 5 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 61905ee..4d00b48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -920,6 +920,18 @@ dependencies = [ "url", ] +[[package]] +name = "filetime" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0408e2626025178a6a7f7ffc05a25bc47103229f19c113755de7bf63816290c" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi", +] + [[package]] name = "firestorm" version = "0.5.0" @@ -2054,6 +2066,7 @@ dependencies = [ "mktemp", "serde", "serde_yaml", + "tar", "thiserror", "tokio", "url", @@ -2860,6 +2873,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tar" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b55807c0344e1e6c04d7c965f5289c39a8d94ae23ed5c0b57aabac549f871c6" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "tempfile" version = "3.3.0" @@ -3667,6 +3691,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "xattr" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d1526bbe5aaeb5eb06885f4d987bcdfa5e23187055de9b83fe00156a821fabc" +dependencies = [ + "libc", +] + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/federate/federate-core/src/lib.rs b/federate/federate-core/src/lib.rs index 04da48a..ed59f39 100644 --- a/federate/federate-core/src/lib.rs +++ b/federate/federate-core/src/lib.rs @@ -16,6 +16,7 @@ * along with this program. If not, see . */ use std::path::Path; +use std::path::PathBuf; use std::result::Result; use async_trait::async_trait; @@ -57,4 +58,7 @@ pub trait Federate: Sync + Send { name: &str, hostname: &str, ) -> Result<(), Self::Error>; + + /// publish results in tar ball + async fn tar(&self) -> Result; } diff --git a/federate/federate-core/src/tests.rs b/federate/federate-core/src/tests.rs index 0455eb4..db20111 100644 --- a/federate/federate-core/src/tests.rs +++ b/federate/federate-core/src/tests.rs @@ -32,6 +32,10 @@ pub async fn adding_forge_works<'a, T: Federate>( // add repository ff.create_repository(&add_repo_msg).await.unwrap(); + + // tar() + ff.tar().await.unwrap(); + // delete repository ff.delete_repository(add_repo_msg.owner, add_repo_msg.name, add_repo_msg.hostname) .await diff --git a/federate/publiccodeyml/Cargo.toml b/federate/publiccodeyml/Cargo.toml index f189f55..f19d13b 100644 --- a/federate/publiccodeyml/Cargo.toml +++ b/federate/publiccodeyml/Cargo.toml @@ -17,6 +17,7 @@ serde_yaml = "0.8.24" tokio = { version = "1.18.2", features = ["fs"]} thiserror = "1.0.30" url = { version = "2.2.2", features = ["serde"] } +tar = "0.4.38" [dependencies.db-core] path = "../../db/db-core" diff --git a/federate/publiccodeyml/src/lib.rs b/federate/publiccodeyml/src/lib.rs index 9d63aa9..c3b6ff3 100644 --- a/federate/publiccodeyml/src/lib.rs +++ b/federate/publiccodeyml/src/lib.rs @@ -36,6 +36,8 @@ pub const INSTANCE_INFO_FILE: &str = "instance.yml"; pub const USER_INFO_FILE: &str = "user.yml"; pub const REPO_INFO_FILE: &str = "publiccode.yml"; +pub const CONTENTS_DIR: &str = "uncompressed"; + #[derive(Clone)] pub struct PccFederate { pub base_dir: String, @@ -43,15 +45,21 @@ pub struct PccFederate { impl PccFederate { pub async fn new(base_dir: String) -> FResult { - let path = Path::new(&base_dir); - if !path.exists() { - fs::create_dir_all(&path).await?; + let x = Self { base_dir }; + x.get_content_path(true).await?; + Ok(x) + } + + pub async fn get_content_path(&self, create_dirs: bool) -> FResult { + let path = Path::new(&self.base_dir).join(CONTENTS_DIR); + if create_dirs { + self.create_dir_if_not_exists(&path).await?; } - Ok(Self { base_dir }) + Ok(path) } pub async fn get_instance_path(&self, hostname: &str, create_dirs: bool) -> FResult { - let path = Path::new(&self.base_dir).join(hostname); + let path = self.get_content_path(false).await?.join(hostname); if create_dirs { self.create_dir_if_not_exists(&path).await?; } @@ -166,4 +174,24 @@ impl Federate for PccFederate { let path = self.get_repo_path(name, owner, hostname, false).await?; self.rm_util(&path).await } + + async fn 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(); + Ok(path) + } }