feat: retrun latest archive in tar()

This commit is contained in:
Aravinth Manivannan 2022-05-19 18:19:37 +05:30
parent 593f3d5c17
commit f78a331e9c
5 changed files with 75 additions and 5 deletions

33
Cargo.lock generated
View file

@ -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"

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<PathBuf, Self::Error>;
}

View file

@ -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

View file

@ -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"

View file

@ -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<Self> {
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)
}
Ok(Self { base_dir })
pub async fn get_content_path(&self, create_dirs: bool) -> FResult<PathBuf> {
let path = Path::new(&self.base_dir).join(CONTENTS_DIR);
if create_dirs {
self.create_dir_if_not_exists(&path).await?;
}
Ok(path)
}
pub async fn get_instance_path(&self, hostname: &str, create_dirs: bool) -> FResult<PathBuf> {
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<PathBuf, Self::Error> {
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)
}
}