From 7f616765b10b201019f3c79e500badf96ea6e71c Mon Sep 17 00:00:00 2001 From: Lilly Rosaline Date: Sun, 13 Feb 2022 14:18:46 -0600 Subject: [PATCH] refactors --- .../etc/enby/manuals/example-package.gmi | 0 src/main.rs | 20 ++--- src/repository.rs | 78 ++++++++++++------- 3 files changed, 62 insertions(+), 36 deletions(-) rename example-package/root/{opt => }/etc/enby/manuals/example-package.gmi (100%) diff --git a/example-package/root/opt/etc/enby/manuals/example-package.gmi b/example-package/root/etc/enby/manuals/example-package.gmi similarity index 100% rename from example-package/root/opt/etc/enby/manuals/example-package.gmi rename to example-package/root/etc/enby/manuals/example-package.gmi diff --git a/src/main.rs b/src/main.rs index c54b53a..e07bf9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,22 +9,19 @@ mod package; use crate::repository::find_package_in_repositories; fn main() { - let operation = std::env::args().nth(1).expect("too few arguments!"); - let args: String = std::env::args().collect(); - let package_data = crate::package::read_package(std::fs::read_to_string("example-package/Package.toml").expect("Failed to read file")); - dbg!(package_data); + let operation = std::env::args().nth(1).expect("too few arguments!"); // Get the operation match operation.as_str() { - "upgrade" | "u" => { + "upgrade" | "u" => { // Will upgrade the selected package(s). } - "install" | "i" => { - if std::env::args().nth(2).expect("too few arguments!") == "--local" { + "install" | "i" => { // Install the selected package(s). + if std::env::args().nth(2).expect("too few arguments!") == "--archive" { // Indicates that the package(s) are archives instead of folders. } else { - let package_name = std::env::args().nth(2).expect("too few arguments!"); - find_package_in_repositories(&package_name, None); + let package_name = std::env::args().nth(2).expect("too few arguments!"); // Get the first package name + find_package_in_repositories(&package_name, None); // Go through and find the package println!("Reading package list"); - let package_list = sled::open("/var/gaypack/packages").expect("Failed to open package list"); + let package_list = sled::open("/var/gaypk/packages").expect("Failed to open package list"); if package_list.contains_key(&package_name).expect("Unexpected error") { println!("Package was found in package list, checking for updates"); } @@ -38,6 +35,9 @@ fn main() { } "query" | "q" => { + } + "config" | "c" => { + } "repository" | "repo" => { diff --git a/src/repository.rs b/src/repository.rs index 8316c75..d047171 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -1,38 +1,64 @@ +use futures::executor::block_on; +use reqwest::StatusCode; use std::collections::HashMap; use std::fs::File; use std::io::BufRead; -use futures::executor::block_on; -use reqwest::StatusCode; -pub fn find_package_in_repositories(package: &str, installed: Option) -> Option> { - let mut hits: Vec = vec!(); - let client = reqwest::Client::new(); - let repo_file = File::open("/var/gaypack/repositories.list").expect("Failed to open repository list"); - let mut candidates: HashMap = HashMap::new(); +/// Returns a map of the repositories that have a given package as well as the version that they have. +pub fn find_package_in_repositories(package: &str, installed: Option, ) -> Option> { + let client = reqwest::Client::new(); // The reqwest client which will make GET requests to remove servers. + let repo_file = + File::open("/var/gaypk/repositories.list").expect("Failed to open repository list"); // The repositories.list file, which contains a list of all known and trusted repositories. + let mut candidates: HashMap = HashMap::new(); // This map will store a list of the repositories along with the latest version of the package they have. + + if installed.is_some() { + candidates.insert("installed".to_string(), installed.unwrap()); + } // If the package is installed, put the installed version in. for repository in std::io::BufReader::new(repo_file).lines() { - let repository = repository.expect("Unexpected error"); // i find this line kinda funny for some reason - let resp = block_on(client.get(format!("{}/gaypack/{}/latest/Package.toml", repository, package)).send()).expect("GET request failed"); - let status = resp.status(); - let text = block_on(resp.text()).expect("Failed to receive packet body"); - if installed.is_some() { - candidates.insert("installed".to_string(), installed.unwrap()); - } - let package_data = &crate::package::read_package(text); - dbg!(package_data); + // Iterate through all the repositories in repositories.list + let repository = repository.unwrap(); + + let resp = block_on( + client + .get(format!("{repository}/gaypk/{package}/Package.toml.latest")) + .send(), + ) + .expect("GET request failed"); // Send a GET request to the current repository, asking for the package's latest known package.toml. TODO protect against version number spoofing + + let status = resp.status(); // Get the status code. + let text = block_on(resp.text()).expect("Failed to receive packet body"); // Get the message body. + + let package_data = &crate::package::read_package(text); // Parse through TOML to a PackageData. + if status == StatusCode::from_u16(200).unwrap() { + // Only add repositories that return 200 candidates.insert(repository, package_data.revision); + } else { + println!("Repository {repository} returned {status}"); } - }; - let mut sorted: Vec<_> = candidates.iter().collect(); - sorted.sort_by_key(|entry| entry.1); - let mut highest_version: u64 = 0; - for (repository, version) in sorted.iter() { - if **version > highest_version {highest_version = **version}; - hits.push(repository.as_str().to_string()); } - if candidates.get("installed") == Some(highest_version) { + let mut sorted: Vec<_> = candidates.iter().collect(); // Convert candidates into a vector for sorting + + sorted.sort_by_key(|entry| entry.1); // Sort by version number, must be stable to sort in order of priority + + let mut candidates = HashMap::new(); // Clear out the old candidates map + for (k, v) in sorted.iter() { + // Iterate through sorted and transfer everything to the new candidates map + candidates.insert(k.as_str().to_string(), **v); // This is an awful line of code and i hate it + } + + let mut highest_version: u64 = 0; // The highest version so far + for (_, version) in candidates.iter() { + // Iterate through and find the highest version + if *version > highest_version { + highest_version = *version + }; + } + + if candidates.get("installed") == Some(&highest_version) { + // If the highest version is equal to the installed version, return None. If not, return the list of candidates for further processing None } else { - Some(hits) + Some(candidates) } -} \ No newline at end of file +}