summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNamilskyy <alive6863@gmail.com>2025-12-06 20:20:16 +0300
committerNamilskyy <alive6863@gmail.com>2025-12-06 20:20:16 +0300
commite7c49d685efe768c0c1d9c66da7b93b6e118b305 (patch)
treee1bfb33a34c3fe4cb84436e73b48463f5deb8f08 /src
parentc8520a5cf071eedc966e57abaf4ba2a334df83ee (diff)
Fixed logical errors, tryed to fix tests
Diffstat (limited to 'src')
-rw-r--r--src/cfg/config.rs4
-rw-r--r--src/main.rs160
-rw-r--r--src/net/http_package.rs37
-rw-r--r--src/net/i2p_package.rs342
-rw-r--r--src/pkgtoolkit/git_source.rs86
-rw-r--r--src/pkgtoolkit/index.rs1
-rw-r--r--src/pkgtoolkit/mod.rs5
-rw-r--r--src/pkgtoolkit/types.rs3
8 files changed, 374 insertions, 264 deletions
diff --git a/src/cfg/config.rs b/src/cfg/config.rs
index f14ec93..ce9df87 100644
--- a/src/cfg/config.rs
+++ b/src/cfg/config.rs
@@ -39,6 +39,8 @@ pub struct Repo {
pub destination: (String, String),
#[serde(rename = "repo_http_url")]
pub repo_http_url: Option<String>,
+ #[serde(rename = "i2p_http_proxy_port")]
+ pub i2p_http_proxy_port: u16,
// #[serde(rename = "arch")]
// pub arch = arch;
}
@@ -92,6 +94,7 @@ impl Config {
std::env::consts::ARCH
)
.into(),
+ i2p_http_proxy_port: 4444,
// Its a hurt place, you need to generate destinations by i2pd and paste here (to mesk.toml)
// Better to leave it empty or set it to (mesk, mesk), now destination conn not implemented
},
@@ -126,6 +129,7 @@ impl Config {
auto_update: true,
destination: (String::from("mesk"), String::from("mesk")),
repo_http_url: None,
+ i2p_http_proxy_port: 4444,
},
log: Log {
log_file: String::from("/var/log/mesk.log"),
diff --git a/src/main.rs b/src/main.rs
index b3367f3..2bdb29c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,12 +7,14 @@ use crate::net::{http_package::HTTPPackage, i2p_package::I2PPackage};
use crate::pkgtoolkit::Package;
use crate::pkgtoolkit::index::IndexOperations;
+use crate::pkgtoolkit::git_source::GitSource;
+use crate::pkgtoolkit::archive::ArchiveOperations;
+use crate::pkgtoolkit::build::BuildOperations;
use clap::{Args, Parser, Subcommand};
-use std::fs::File;
-use std::fs::create_dir_all;
use std::io::Write;
use std::path::Path;
+use toml;
#[derive(Parser)]
struct Cli {
@@ -70,70 +72,79 @@ struct RemoteInstallArgs {
clean: bool,
}
+
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
- // Changed return type to be more general
let cli: Cli = Cli::parse();
match &cli.command {
Commands::Validate { path } => {
println!("Validating {}", path);
+ match Package::check(path.to_string()) {
+ Ok(is_valid) => {
+ if is_valid {
+ println!("Package archive is valid.");
+ } else {
+ println!("Package archive is invalid.");
+ return Err(std::io::Error::other("Invalid package archive").into());
+ }
+ }
+ Err(e) => {
+ log::error!("Failed to validate package '{}': {}", path, e);
+ return Err(e.into());
+ }
+ }
return Ok(());
}
Commands::Build { pkgname } => {
- println!("Building {}", pkgname);
+ println!("Building package from archive: {}", pkgname);
+
+ let path = Path::new(&pkgname);
+ if !path.exists() {
+ return Err(std::io::Error::other(format!("Package archive not found: {}", pkgname)).into());
+ }
+
+ if !path.is_file() {
+ return Err(std::io::Error::other(format!("Path is not a file: {}", pkgname)).into());
+ }
+
+ println!("Extracting archive...");
+ Package::extract_archive(&pkgname)?;
+
+ let config = Config::parse().unwrap();
+ let cache_dir = &config.paths.cache_dir;
+ let install_toml_path = Path::new(cache_dir).join("INSTALL");
+
+ if !install_toml_path.exists() {
+ return Err(std::io::Error::other("INSTALL file not found in archive").into());
+ }
+
+ let install_content = std::fs::read_to_string(&install_toml_path)?;
+ let install_data: crate::pkgtoolkit::types::Install = toml::from_str(&install_content)?;
+
+ let mut pkg = install_data.package;
+
+ println!("Building package '{}'...", pkg.name);
+ pkg.build()?;
+ println!("Package '{}' built successfully.", pkg.name);
return Ok(());
}
- Commands::Install {
- pkgname,
- source: _,
- args,
- } => {
- let config = Config::parse().unwrap();
-
+ Commands::Install { pkgname, source: _, args } => {
+ let config = Config::parse().unwrap();
if args.http {
println!("Installing {} via HTTP", pkgname);
let mut http_client = HTTPPackage::new(config);
- match http_client.fetch_index_http().await {
- Ok(_) => {
- log::info!("Index fetched successfully.");
- }
- Err(e) => {
- log::error!("Failed to fetch index: {}", e);
- return Err(e);
- }
- }
- match http_client.fetch_package_http(pkgname).await {
- Ok(_) => {
- log::info!("Package '{}' installed successfully.", pkgname);
- }
- Err(e) => {
- log::error!("Failed to install package '{}': {}", pkgname, e);
- return Err(e);
- }
- }
+ http_client.fetch_index_http().await?;
+ log::info!("Index fetched successfully.");
+ http_client.fetch_package_http(pkgname).await?;
+ log::info!("Package '{}' installed successfully.", pkgname);
} else {
println!("Installing {} via I2P", pkgname);
let mut i2p_client = I2PPackage::new(config);
-
- match i2p_client.fetch_index().await {
- Ok(_) => {
- log::info!("Index fetched successfully.");
- }
- Err(e) => {
- log::error!("Failed to fetch index: {}", e);
- return Err(e);
- }
- }
- match i2p_client.fetch_package(pkgname).await {
- Ok(_) => {
- log::info!("Package '{}' installed successfully.", pkgname);
- }
- Err(e) => {
- log::error!("Failed to install package '{}': {}", pkgname, e);
- return Err(e);
- }
- }
+ i2p_client.fetch_index().await?;
+ log::info!("Index fetched successfully.");
+ i2p_client.fetch_package(pkgname).await?;
+ log::info!("Package '{}' installed successfully.", pkgname);
}
return Ok(());
}
@@ -142,15 +153,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
return Ok(());
}
Commands::GetSource { pkgname } => {
+ let config = Config::parse().unwrap();
println!("Getting source of {}", pkgname);
+
+
+ let source_path = GitSource::get_source_by_name(pkgname, &config)?;
+ println!("Source code successfully downloaded to: {}", source_path);
return Ok(());
}
- Commands::DefaultConfig {
- repo,
- cachedir,
- buildir,
- installed_db,
- } => {
+ Commands::DefaultConfig { repo, cachedir, buildir, installed_db } => {
println!("Generating config file");
if cachedir.is_none() && repo.is_none() && buildir.is_none() {
let config = Config::default().unwrap();
@@ -162,12 +173,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
log::warn!("Writing the default config to /etc/mesk/mesk.toml");
let path = Path::new("/etc/mesk/mesk.toml");
- create_dir_all(path.parent().unwrap())?;
- let mut file = File::create(path)?;
+ std::fs::create_dir_all(path.parent().unwrap())?;
+ let mut file = std::fs::File::create(path)?;
file.write_all(config.as_bytes())?;
println!("Config tool ending work.");
} else {
- let config = Config::generate(repo, cachedir, buildir, installed_db).unwrap();
+ let config = Config::generate(repo, cachedir, buildir, installed_db).unwrap();
println!("---- Start of generated config ----");
println!("{:?}", config);
@@ -176,27 +187,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
log::warn!("Writing the default config to /etc/mesk/mesk.toml");
let path = Path::new("/etc/mesk/mesk.toml");
- create_dir_all(path.parent().unwrap())?;
- let mut file = File::create(path)?;
- file.write_all(config.as_bytes())?;
+ std::fs::create_dir_all(path.parent().unwrap())?;
+ let mut file = std::fs::File::create(path)?;
+ file.write_all(config.as_bytes())?;
println!("Config tool ending work.");
}
return Ok(());
}
Commands::Update => {
- let config = Config::parse().unwrap();
+ let config = Config::parse().unwrap();
println!("Updating index from {}", config.repo.repo_url);
let mut i2p_client = I2PPackage::new(config);
- match i2p_client.fetch_index().await {
- Ok(_) => {
- println!("Index updated successfully.");
- }
- Err(e) => {
- log::error!("Failed to update index: {}", e);
- return Err(e);
- }
- }
+ i2p_client.fetch_index().await?;
+ println!("Index updated successfully.");
return Ok(());
}
Commands::Upgrade { pkgname } => {
@@ -208,22 +212,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
"CREATED BY: Asya and Namilsk as part of the Anthrill independent Global network distribution project"
);
println!(" ");
- println!("The Anthrill project repos: https://codeberg.org/NamelessTeam ");
+ println!("The Anthrill project repos: https://codeberg.org/NamelessTeam ");
+ return Ok(());
}
Commands::GenIndex { path } => {
println!("Generating index for {}", path);
- match Package::gen_index(path) {
- Ok(_) => {
- println!("Index generated successfully.");
- }
- Err(e) => {
- log::error!("Failed to generate index: {}", e);
- return Err(Box::new(e));
- }
- }
+
+ Package::gen_index(path)?;
+ println!("Index generated successfully.");
return Ok(());
}
}
- Ok(())
}
diff --git a/src/net/http_package.rs b/src/net/http_package.rs
index d31cc90..4566e05 100644
--- a/src/net/http_package.rs
+++ b/src/net/http_package.rs
@@ -12,6 +12,10 @@ use std::{collections::HashMap, path::Path};
use tar::Archive;
use tokio::{fs::File, io::AsyncWriteExt};
+use crate::pkgtoolkit::install::InstallOperations;
+use crate::pkgtoolkit::archive::ArchiveOperations;
+
+
pub struct HTTPPackage {
pub config: Config,
pub index_packages: Option<HashMap<String, Package>>,
@@ -143,7 +147,10 @@ impl HTTPPackage {
let mut package_map = HashMap::new();
for pkg in index_data.packages {
- let base_url = url::Url::parse(&self.config.repo.repo_url)?;
+ let base_url = match &self.config.repo.repo_http_url {
+ Some(url) => url::Url::parse(url)?,
+ None => url::Url::parse(&self.config.repo.repo_url)?,
+ };
let full_url = base_url.join(&pkg.url)?;
let mut pkg_clone = pkg.clone();
pkg_clone.url = full_url.to_string();
@@ -227,12 +234,13 @@ impl HTTPPackage {
/// Fetches a specific package identified by `package_name`.
/// Assumes `fetch_index_http` has been called and `self.index_packages` is populated to get the URL.
- /// Downloads the package file (.mesk) to the cache directory with a progress bar.
+ /// Downloads the package file (.mesk) to the cache directory with a progress bar,
+ /// extracts it, and installs the package.
///
/// # Errors
///
/// Returns an error if the index is not loaded, the package is not found in the index,
- /// the package URL is invalid, the request fails, or if there's an issue writing the file.
+ /// the package URL is invalid, the request fails, extraction fails, or installation fails.
pub async fn fetch_package_http(
&self,
package_name: &str,
@@ -259,7 +267,28 @@ impl HTTPPackage {
package_name,
file_path
);
- Ok(true)
+
+ // Extract the package archive
+ log::info!("Extracting package archive for '{}'...", package_name);
+ Package::extract_archive(&file_path.to_string_lossy())
+ .map_err(|e| format!("Failed to extract package archive: {}", e))?;
+
+ log::info!("Package archive extracted successfully.");
+
+ // Install the package
+ log::info!("Installing package '{}'...", package_name);
+ let mut package = package_info.clone();
+
+ match package.install() {
+ Ok(_) => {
+ log::info!("Package '{}' installed successfully.", package_name);
+ Ok(true)
+ }
+ Err(e) => {
+ log::error!("Failed to install package '{}': {}", package_name, e);
+ Err(format!("Installation failed: {}", e).into())
+ }
+ }
}
/// Fetches a specific package identified by `index` (likely the package name).
diff --git a/src/net/i2p_package.rs b/src/net/i2p_package.rs
index a9d39d2..cebfa34 100644
--- a/src/net/i2p_package.rs
+++ b/src/net/i2p_package.rs
@@ -1,23 +1,17 @@
use crate::cfg::config::Config;
use crate::pkgtoolkit::Package;
-use crate::pkgtoolkit::archive::ArchiveOperations;
-use serde::Deserialize;
-use tokio;
-
-/*
-use emissary_core::runtime::{
- AsyncRead,
- AsyncWrite,
-};
-*/
-
+use flate2::read::GzDecoder;
+use futures_util::stream::TryStreamExt;
use indicatif::{ProgressBar, ProgressStyle};
+use reqwest;
+use serde::Deserialize;
+use std::fs::File as StdFile;
use std::{collections::HashMap, path::Path};
-use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
+use tar::Archive;
+use tokio::{fs::File, io::AsyncWriteExt};
+use crate::pkgtoolkit::archive::ArchiveOperations;
+use crate::pkgtoolkit::install::InstallOperations;
-use url;
-use yosemite::Session;
-use yosemite::SessionOptions;
pub struct I2PPackage {
pub config: Config,
@@ -30,81 +24,69 @@ struct IndexData {
}
impl I2PPackage {
- /// Creates a new I2P object with the given configuration.
+ /// Creates a new I2PPackage object with the given configuration.
///
/// # Returns
///
- /// A new I2P object with the given configuration. The session is initially set to None and the connected status is set to false.
- pub fn new(cfg: Config) -> Self {
+ /// A new I2PPackage object with the given configuration.
+ pub fn new(config: Config) -> Self {
I2PPackage {
- config: cfg,
+ config,
index_packages: None,
}
}
- /// Downloads the INDEX.tar.gz file from the configured repository
- /// and stores it in the configured cache directory.
+ /// Creates a reqwest client configured to use the I2P HTTP proxy.
+ ///
+ /// # Returns
+ ///
+ /// A reqwest Client with proxy configuration for I2P.
+ fn create_proxy_client(&self) -> Result<reqwest::Client, Box<dyn std::error::Error>> {
+ let proxy_url = format!("http://127.0.0.1:{}", self.config.repo.i2p_http_proxy_port);
+ let proxy = reqwest::Proxy::http(&proxy_url)?;
+ let client = reqwest::Client::builder()
+ .proxy(proxy)
+ .build()?;
+ Ok(client)
+ }
+
+ /// Downloads the INDEX.tar.gz file from the configured I2P repository
+ /// and stores it in the configured cache directory with a progress bar.
///
/// # Errors
///
/// Returns an error if the request fails, if the response status is not successful, or if there's an issue while reading or writing the file.
pub async fn fetch_index(&mut self) -> Result<bool, Box<dyn std::error::Error>> {
- let repo_url_str = &self.config.repo.repo_url;
+ let repo_url = &self.config.repo.repo_url;
let cache_dir = &self.config.paths.cache_dir;
- let url = url::Url::parse(repo_url_str)?;
- let host = url.host_str().ok_or("No host in URL")?;
+ log::debug!("Cache directory: {:?}", cache_dir);
- let request_path = url.path();
- let request_path = if request_path.ends_with(".tar.gz") {
- request_path.to_string()
+ let index_url = if repo_url.ends_with(".tar.gz") {
+ repo_url.to_string()
} else {
- format!("{}/INDEX.tar.gz", request_path.trim_end_matches('/'))
+ format!("{}/INDEX.tar.gz", repo_url.trim_end_matches('/'))
};
- let session_options = SessionOptions::default();
- let mut session = Session::new(session_options).await?;
- let mut stream = session.connect(host).await?;
+ let client = self.create_proxy_client()?;
- let request = format!(
- "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",
- request_path, host
- );
- stream.write_all(request.as_bytes()).await?;
-
- let mut reader = BufReader::new(stream);
- let mut response_buffer = Vec::new();
- reader.read_to_end(&mut response_buffer).await?;
-
- let headers_end = response_buffer
- .windows(4)
- .position(|window| window == b"\r\n\r\n")
- .ok_or("Invalid response: no headers end")?;
-
- let headers_str = std::str::from_utf8(&response_buffer[..headers_end])?;
- if !headers_str.starts_with("HTTP/1.1 200") && !headers_str.starts_with("HTTP/1.0 200") {
- return Err(format!(
- "HTTP Error: {}",
- headers_str.lines().next().unwrap_or("Unknown")
- )
- .into());
- }
-
- let content_length = headers_str
- .lines()
- .find(|line| line.to_lowercase().starts_with("content-length:"))
- .and_then(|line| line.split_at(15).1.trim().parse::<u64>().ok())
+ // Make a HEAD request to get the content length for the progress bar
+ let head_response = client.head(&index_url).send().await?;
+ let content_length: u64 = head_response
+ .headers()
+ .get(reqwest::header::CONTENT_LENGTH)
+ .and_then(|ct_len| ct_len.to_str().ok())
+ .and_then(|ct_len| ct_len.parse().ok())
.unwrap_or(0);
- let body_start = headers_end + 4;
- let mut body_reader = std::io::Cursor::new(&response_buffer[body_start..]);
- let file_path = Path::new(cache_dir).join("INDEX.tar.gz");
-
+ // Create progress bar
let pb = if content_length > 0 {
let pb = ProgressBar::new(content_length);
- pb.set_style(ProgressStyle::default_bar()
- .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")?
- .progress_chars("#>-"));
+ pb.set_style(
+ ProgressStyle::default_bar()
+ .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")?
+ .progress_chars("#>-"),
+ );
pb
} else {
let pb = ProgressBar::new_spinner();
@@ -115,34 +97,41 @@ impl I2PPackage {
pb
};
- let mut file = tokio::fs::File::create(&file_path).await?;
-
- let chunk_size = 8192u64;
- let mut buffer = vec![0; chunk_size as usize];
+ // Send GET request and stream the response body
+ let response = client.get(&index_url).send().await?;
+ if !response.status().is_success() {
+ return Err(format!("HTTP Error: {}", response.status()).into());
+ }
- loop {
- let bytes_read_result = std::io::Read::read(&mut body_reader, &mut buffer);
- let bytes_read = match bytes_read_result {
- Ok(n) => n,
- Err(e) => return Err(e.into()),
- };
+ let mut stream = response.bytes_stream();
+ let file_path = Path::new(cache_dir).join("INDEX.tar.gz");
- if bytes_read == 0 {
- break;
- }
- file.write_all(&buffer[..bytes_read]).await?;
+ // Ensure cache_dir exists
+ tokio::fs::create_dir_all(cache_dir)
+ .await
+ .map_err(|e| format!("Failed to create cache dir: {}", e))?;
- pb.inc(bytes_read as u64);
+ let mut file = File::create(&file_path).await?;
+ let mut downloaded: u64 = 0;
- if bytes_read < chunk_size as usize {
- break;
- }
+ while let Some(chunk) = stream.try_next().await? {
+ file.write_all(&chunk).await?;
+ let chunk_len = chunk.len() as u64;
+ downloaded += chunk_len;
+ pb.set_position(downloaded);
}
pb.finish_with_message("INDEX.tar.gz download finished");
log::info!("Extracting INDEX.tar.gz to cache directory...");
- Package::extract_archive(&file_path.to_string_lossy())?;
+
+ let archive_file =
+ StdFile::open(&file_path).map_err(|e| format!("Failed to open archive: {}", e))?;
+ let gz_decoder = GzDecoder::new(archive_file);
+ let mut archive = Archive::new(gz_decoder);
+ archive
+ .unpack(cache_dir)
+ .map_err(|e| format!("Failed to unpack archive: {}", e))?;
let index_toml_path = Path::new(cache_dir).join("INDEX.toml");
if !index_toml_path.exists() {
@@ -151,13 +140,14 @@ impl I2PPackage {
return Ok(true);
}
- let index_content = std::fs::read_to_string(&index_toml_path)?;
- let index_data: IndexData = toml::from_str(&index_content)?;
+ let index_content = tokio::fs::read_to_string(&index_toml_path).await?;
+ log::debug!("Content of INDEX.toml:\n{}", index_content);
+
+ let index_data: IndexData = toml::from_str(&index_content)
+ .map_err(|e| format!("Failed to parse INDEX.toml: {}", e))?;
let mut package_map = HashMap::new();
for pkg in index_data.packages {
- // PKG_URL = /repo/package.mesk
- // FULL URL = "http://mesk.anthrill.i2p/i2p/repo/pkg.mesk"
let base_url = url::Url::parse(&self.config.repo.repo_url)?;
let full_url = base_url.join(&pkg.url)?;
let mut pkg_clone = pkg.clone();
@@ -178,25 +168,38 @@ impl I2PPackage {
/// An internal auxiliary function for downloading data and writing it to a file with a progress display.
///
/// # Arguments
- /// * `data' - Byte slice (&[u8]) with data to write.
- /// * `file_path' is the path to the file to write the data to.
- /// * `content_length' is the expected data size (for the progress bar). Maybe 0.
- /// * `description' - Description of the operation for the progress bar.
+ /// * `client` - The reqwest client to use for requests.
+ /// * `url` - The URL to download from.
+ /// * `file_path` - The path to the file to write the data to.
+ /// * `description` - Description of the operation for the progress bar.
///
/// # Errors
///
- /// Returns an error if there is a problem when creating or writing to a file.
- async fn download_and_write_file_with_progress(
- data: &[u8],
+ /// Returns an error if the request fails, if the response status is not successful, or if there's an issue while writing the file.
+ async fn download_file_with_progress(
+ client: &reqwest::Client,
+ url: &str,
file_path: &Path,
- content_length: u64,
description: &str,
) -> Result<(), Box<dyn std::error::Error>> {
+
+ let head_response = client.head(url).send().await?;
+ let content_length: u64 = head_response
+ .headers()
+ .get(reqwest::header::CONTENT_LENGTH)
+ .and_then(|ct_len| ct_len.to_str().ok())
+ .and_then(|ct_len| ct_len.parse().ok())
+ .unwrap_or(0);
let pb = if content_length > 0 {
let pb = ProgressBar::new(content_length);
- pb.set_style(ProgressStyle::default_bar()
- .template(&format!("{{spinner:.green}} [{{elapsed_precise}}] [{{bar:40.cyan/blue}}] {{bytes}}/{{total_bytes}} ({} {{eta}})", description))?
- .progress_chars("#>-"));
+ pb.set_style(
+ ProgressStyle::default_bar()
+ .template(&format!(
+ "{{spinner:.green}} [{{elapsed_precise}}] [{{bar:40.cyan/blue}}] {{bytes}}/{{total_bytes}} ({} {{eta}})",
+ description
+ ))?
+ .progress_chars("#>-"),
+ );
pb
} else {
let pb = ProgressBar::new_spinner();
@@ -206,117 +209,98 @@ impl I2PPackage {
))?);
pb
};
+ let response = client.get(url).send().await?;
+ if !response.status().is_success() {
+ return Err(format!("HTTP Error: {}", response.status()).into());
+ }
- let mut file = tokio::fs::File::create(&file_path).await?;
-
- let chunk_size = 8192usize;
- let mut pos = 0;
+ let mut stream = response.bytes_stream();
+ let mut file = File::create(&file_path).await?;
+ let mut downloaded: u64 = 0;
- while pos < data.len() {
- let end = std::cmp::min(pos + chunk_size, data.len());
- let chunk = &data[pos..end];
- file.write_all(chunk).await?;
- pb.inc(chunk.len() as u64);
- pos = end;
+ while let Some(chunk) = stream.try_next().await? {
+ file.write_all(&chunk).await?;
+ let chunk_len = chunk.len() as u64;
+ downloaded += chunk_len;
+ pb.set_position(downloaded);
}
pb.finish_with_message(format!("{} download finished", description));
Ok(())
}
- /// Fetches a specific package identified by `index` (likely the package name).
- /// Assumes `fetch_index` has been called and `self.index_packages` is populated.
- pub fn fetch_package_info(
- &self,
- package_name: &str,
- ) -> Result<&Package, Box<dyn std::error::Error>> {
- let packages = self
- .index_packages
- .as_ref()
- .ok_or("Index not loaded. Call fetch_index first.")?;
- let pkg_info = packages
- .get(package_name)
- .ok_or(format!("Package '{}' not found in index.", package_name))?;
- Ok(pkg_info)
- }
-
/// Fetches a specific package identified by `package_name`.
/// Assumes `fetch_index` has been called and `self.index_packages` is populated to get the URL.
- /// Downloads the package file (.mesk) to the cache directory.
+ /// Downloads the package file (.mesk) to the cache directory with a progress bar,
+ /// extracts it, and installs the package.
///
/// # Errors
///
/// Returns an error if the index is not loaded, the package is not found in the index,
- /// the package URL is invalid, the request fails, or if there's an issue writing the file.
- /// Why didn't I just abstract the download functionality into a separate function initially?
- /// Yes, I'm scared to work with fetch_index, even I don't often write such shit code.
+ /// the package URL is invalid, the request fails, extraction fails, or installation fails.
pub async fn fetch_package(
&self,
package_name: &str,
) -> Result<bool, Box<dyn std::error::Error>> {
let package_info = self.fetch_package_info(package_name)?;
- let url = url::Url::parse(&package_info.url)?;
- let host = url.host_str().ok_or("No host in package URL")?;
- let request_path = url.path();
+ let url = &package_info.url;
- let session_options = SessionOptions::default();
- let mut session = Session::new(session_options).await?;
- let mut stream = session.connect(host).await?;
+ let client = self.create_proxy_client()?;
- let request = format!(
- "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",
- request_path, host
- );
- stream.write_all(request.as_bytes()).await?;
-
- let mut reader = BufReader::new(stream);
- let mut response_buffer = Vec::new();
- reader.read_to_end(&mut response_buffer).await?;
-
- let headers_end = response_buffer
- .windows(4)
- .position(|window| window == b"\r\n\r\n")
- .ok_or("Invalid response: no headers end")?;
-
- let headers_str = std::str::from_utf8(&response_buffer[..headers_end])?;
- if !headers_str.starts_with("HTTP/1.1 200") && !headers_str.starts_with("HTTP/1.0 200") {
- return Err(format!(
- "HTTP Error: {}",
- headers_str.lines().next().unwrap_or("Unknown")
- )
- .into());
- }
-
- let content_length = headers_str
- .lines()
- .find(|line| line.to_lowercase().starts_with("content-length:"))
- .and_then(|line| line.split_at(15).1.trim().parse::<u64>().ok())
- .unwrap_or(0);
-
- let body_start = headers_end + 4;
- let body_bytes = &response_buffer[body_start..];
-
- let file_name = Path::new(request_path)
+ let file_name = Path::new(url)
.file_name()
- .ok_or("Could not determine filename from URL path")?
+ .ok_or("Could not determine filename from URL")?
.to_str()
.ok_or("Filename is not valid UTF-8")?;
let cache_dir = &self.config.paths.cache_dir;
let file_path = Path::new(cache_dir).join(file_name);
- Self::download_and_write_file_with_progress(
- body_bytes,
- &file_path,
- content_length,
- file_name,
- )
- .await?;
+ tokio::fs::create_dir_all(&cache_dir).await?;
+
+ Self::download_file_with_progress(&client, url, &file_path, file_name).await?;
log::info!(
"Package '{}' downloaded successfully to {:?}",
package_name,
file_path
);
- Ok(true)
+
+ // Extract the package archive
+ log::info!("Extracting package archive for '{}'...", package_name);
+ Package::extract_archive(&file_path.to_string_lossy())
+ .map_err(|e| format!("Failed to extract package archive: {}", e))?;
+
+ log::info!("Package archive extracted successfully.");
+
+ // Install the package
+ log::info!("Installing package '{}'...", package_name);
+ let mut package = package_info.clone();
+
+ match package.install() {
+ Ok(_) => {
+ log::info!("Package '{}' installed successfully.", package_name);
+ Ok(true)
+ }
+ Err(e) => {
+ log::error!("Failed to install package '{}': {}", package_name, e);
+ Err(format!("Installation failed: {}", e).into())
+ }
+ }
+ }
+
+ /// Fetches a specific package identified by `index` (likely the package name).
+ /// Assumes `fetch_index` has been called and `self.index_packages` is populated.
+ pub fn fetch_package_info(
+ &self,
+ package_name: &str,
+ ) -> Result<&Package, Box<dyn std::error::Error>> {
+ let packages = self
+ .index_packages
+ .as_ref()
+ .ok_or("Index not loaded. Call fetch_index first.")?;
+ let pkg_info = packages
+ .get(package_name)
+ .ok_or(format!("Package '{}' not found in index.", package_name))?;
+ Ok(pkg_info)
}
}
diff --git a/src/pkgtoolkit/git_source.rs b/src/pkgtoolkit/git_source.rs
new file mode 100644
index 0000000..2f33c14
--- /dev/null
+++ b/src/pkgtoolkit/git_source.rs
@@ -0,0 +1,86 @@
+use std::fs;
+use std::path::Path;
+use git2::Repository;
+use crate::pkgtoolkit::types::Package;
+use crate::cfg::config::Config;
+use toml;
+
+pub struct GitSource;
+
+impl GitSource {
+ /// Clone a git repository to a local directory
+ pub fn clone_repo(git_url: &str, target_dir: &Path) -> Result<(), git2::Error> {
+ Repository::clone(git_url, target_dir)?;
+ Ok(())
+ }
+
+ /// Get source code for a package from its git repository
+ pub fn get_package_source(package: &Package, config: &Config) -> Result<String, Box<dyn std::error::Error>> {
+ let git_repo = package.git_repo.as_ref()
+ .ok_or("Package does not have a git repository specified")?;
+
+ let source_dir = Path::new(&config.paths.cache_dir)
+ .join("sources")
+ .join(&package.name)
+ .join(&package.version);
+
+ fs::create_dir_all(&source_dir)?;
+
+ println!("Cloning {} from {}", package.name, git_repo);
+ Self::clone_repo(git_repo, &source_dir)?;
+
+ let source_path = source_dir.to_string_lossy().to_string();
+ println!("Source code downloaded to: {}", source_path);
+
+ Ok(source_path)
+ }
+
+ /// Get source code for a package by name, first checking INSTALL file, then falling back to index
+ pub fn get_source_by_name(pkg_name: &str, config: &Config) -> Result<String, Box<dyn std::error::Error>> {
+ // First try to get git_repo from INSTALL file in cache
+ let install_path = Path::new(&config.paths.cache_dir).join(format!("{}/INSTALL", pkg_name));
+
+ if install_path.exists() {
+ let install_content = fs::read_to_string(&install_path)?;
+ let install_data: crate::pkgtoolkit::types::Install = toml::from_str(&install_content)
+ .map_err(|e| format!("Failed to parse INSTALL file: {}", e))?;
+
+ // Check if InstallMeta has git_repo
+ if let Some(git_repo) = install_data.install.git_repo {
+ println!("Found git repository in INSTALL file: {}", git_repo);
+
+ let source_dir = Path::new(&config.paths.cache_dir)
+ .join("sources")
+ .join(&install_data.package.name)
+ .join(&install_data.package.version);
+
+ fs::create_dir_all(&source_dir)?;
+
+ println!("Cloning {} from {}", install_data.package.name, git_repo);
+ Self::clone_repo(&git_repo, &source_dir)?;
+
+ let source_path = source_dir.to_string_lossy().to_string();
+ println!("Source code downloaded to: {}", source_path);
+
+ return Ok(source_path);
+ }
+ }
+
+ // Fall back to index if INSTALL file doesn't exist or doesn't have git_repo
+ let index_path = Path::new(&config.paths.cache_dir).join("INDEX.toml");
+
+ if !index_path.exists() {
+ return Err("Index file not found. Please run 'mesk update' first.".into());
+ }
+
+ let index_content = fs::read_to_string(&index_path)?;
+ let index: crate::pkgtoolkit::types::Index = toml::from_str(&index_content)
+ .map_err(|e| format!("Failed to parse index: {}", e))?;
+
+ let package = index.packages.iter()
+ .find(|pkg| pkg.name == pkg_name)
+ .ok_or(format!("Package '{}' not found in index", pkg_name))?;
+
+ Self::get_package_source(package, config)
+ }
+}
diff --git a/src/pkgtoolkit/index.rs b/src/pkgtoolkit/index.rs
index 7b13533..78c37aa 100644
--- a/src/pkgtoolkit/index.rs
+++ b/src/pkgtoolkit/index.rs
@@ -95,6 +95,7 @@ impl IndexOperations for Package {
descr: Some(install_data.package.descr.unwrap_or_default()),
license: Some(install_data.package.license.unwrap_or_default()),
url: install_data.package.url,
+ git_repo: install_data.package.git_repo,
};
all_packages.push(pkg_for_index);
diff --git a/src/pkgtoolkit/mod.rs b/src/pkgtoolkit/mod.rs
index 2fc316a..1cadee3 100644
--- a/src/pkgtoolkit/mod.rs
+++ b/src/pkgtoolkit/mod.rs
@@ -1,6 +1,7 @@
// Core package toolkit modules
pub mod archive;
pub mod build;
+pub mod git_source;
pub mod index;
pub mod install;
pub mod types;
@@ -52,3 +53,7 @@ pub use install::InstallOperations;
// Index operations for package repository management
#[allow(unused_imports)]
pub use index::IndexOperations;
+
+// Git source operations for downloading package source code
+#[allow(unused_imports)]
+pub use git_source::GitSource;
diff --git a/src/pkgtoolkit/types.rs b/src/pkgtoolkit/types.rs
index 01a807a..83f47a3 100644
--- a/src/pkgtoolkit/types.rs
+++ b/src/pkgtoolkit/types.rs
@@ -17,6 +17,7 @@ pub struct Package {
pub descr: Option<String>,
pub license: Option<String>,
pub url: String,
+ pub git_repo: Option<String>,
}
#[derive(Deserialize, Debug, Clone)]
@@ -27,6 +28,8 @@ pub struct InstallMeta {
pub mode: String,
// Cancels the previous fields and installs them using the shell script
pub custom_script: Option<String>,
+ // Git repository URL for source code if needed
+ pub git_repo: Option<String>,
// pub files: Option<Vec<String>>,
}