Skip to content

Commit

Permalink
outsource repo init into pacman utils lib (#41)
Browse files Browse the repository at this point in the history
* outsource repo init into pacman utils lib
* repo init function takes path and repo name
* check if files exist and not just folder
  • Loading branch information
Lukas-Heiligenbrunner authored Aug 13, 2024
1 parent 65de74c commit c19a728
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 39 deletions.
2 changes: 0 additions & 2 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ tokio = "1.38.0"
anyhow = "1.0.76"

reqwest = { version = "0.12.5", features = ["blocking", "gzip"] }
flate2 = "1.0.30"
tar = "0.4.41"

rocket = "0.5.1"
rocket_okapi = { features = ["swagger"], git = "https://github.com/beyera/okapi.git", branch = "beyera/update-rocket-0.5.1" }
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use crate::api::init::{init_api, init_repo};
use crate::builder::init::init_build_queue;
use crate::builder::types::Action;
use crate::db::init::init_db;
use crate::repo::init::init_repo_files;
use crate::scheduler::aur_version_update::start_aur_version_checking;
use crate::utils::logger::init_logger;
use log::{info, warn};
use std::path::PathBuf;
use tokio::sync::broadcast;

static START_BANNER: &str = r"
Expand All @@ -37,7 +37,7 @@ async fn main() {
.map_err(|e| format!("Failed to initialize database: {}", e))
.unwrap();

init_repo_files().await.unwrap();
pacman_repo_utils::init_repo(&PathBuf::from("./repo"), "repo").unwrap();

let build_queue_handle = init_build_queue(db.clone(), tx.clone());
let version_check_handle = start_aur_version_checking(db.clone());
Expand Down
1 change: 1 addition & 0 deletions backend/src/pacman-repo-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "pacman-repo-utils"
description = "Plug in replacement for repo-add pacman tools"
version = "0.1.0"
edition = "2021"

Expand Down
8 changes: 8 additions & 0 deletions backend/src/pacman-repo-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
mod pkginfo;
mod repo_add;
mod repo_database;
mod repo_init;
mod repo_remove;

use crate::repo_add::repo_add_impl;
use crate::repo_init::init_repo_impl;
use crate::repo_remove::repo_remove_impl;
use std::path::PathBuf;

pub fn repo_add(pkgfile: &str, db_archive: String, files_archive: String) -> anyhow::Result<()> {
repo_add_impl(pkgfile, db_archive, files_archive)
Expand All @@ -16,3 +20,7 @@ pub fn repo_remove(
) -> anyhow::Result<()> {
repo_remove_impl(filename, db_archive, files_archive)
}

pub fn init_repo(path: &PathBuf, name: &str) -> anyhow::Result<()> {
init_repo_impl(path, name)
}
39 changes: 39 additions & 0 deletions backend/src/pacman-repo-utils/src/repo_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use anyhow::anyhow;
use flate2::read::GzEncoder;
use flate2::Compression;
use log::info;
use std::fs;
use std::fs::File;
use std::os::unix::fs::symlink;
use std::path::PathBuf;

pub fn init_repo_impl(path: &PathBuf, name: &str) -> anyhow::Result<()> {
let db_file = path.join(format!("{}.db.tar.gz", name));
let files_file = path.join(format!("{}.files.tar.gz", name));

// create repo folder
if fs::metadata(&db_file).is_ok() || fs::metadata(&files_file).is_ok() {
info!("Pacman repo archive already exists");
return Ok(());
}

info!("Initializing empty pacman Repo archive");
fs::create_dir(path)?;

let tar_gz = File::create(&db_file)?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.finish()
.map_err(|_| anyhow!("failed to create repo archive"))?;
symlink(&db_file, path.join(format!("{}.db", name)))
.map_err(|_| anyhow!("failed to create repo symlink"))?;

let tar_gz = File::create(&files_file)?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.finish()
.map_err(|_| anyhow!("failed to create repo archive"))?;
symlink(&files_file, path.join(format!("{}.files", name)))
.map_err(|_| anyhow!("failed to create repo symlink"))?;
Ok(())
}
32 changes: 0 additions & 32 deletions backend/src/repo/init.rs

This file was deleted.

1 change: 0 additions & 1 deletion backend/src/repo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod init;
pub mod utils;

0 comments on commit c19a728

Please sign in to comment.