Verified Commit aad9a3a1 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Add pkgar-repo crate

parent 542d0a9f
......@@ -3,5 +3,9 @@ members = [
"pkgar",
"pkgar-core",
"pkgar-keys",
"pkgar-repo",
]
[patch.crates-io]
ring = { git = "https://gitlab.redox-os.org/redox-os/ring.git", branch = "redox-0.16.20" }
tokio = { git = "https://gitlab.redox-os.org/redox-os/tokio.git", branch = "redox-1.17.0" }
[package]
name = "pkgar-repo"
version = "0.1.0"
description = "Redox Package Repository"
license = "MIT"
authors = ["Jeremy Soller <jackpot51@gmail.com>"]
repository = "https://gitlab.redox-os.org/redox-os/pkgar"
edition = "2018"
[dependencies]
pkgar = { version = "0.1.7", path = "../pkgar" }
pkgar-core = { version = "0.1.0", path = "../pkgar-core" }
reqwest = { version = "0.11.10", default_features = false, features = ["blocking", "rustls-tls"] }
sodiumoxide = { version = "=0.2.6", default_features = false }
pub use self::package::*;
mod package;
#[derive(Debug)]
pub enum Error {
Pkgar(pkgar::Error),
Reqwest(reqwest::Error),
}
impl From<std::io::Error> for Error {
fn from(other: std::io::Error) -> Self {
Self::Pkgar(other.into())
}
}
impl From<pkgar::Error> for Error {
fn from(other: pkgar::Error) -> Self {
Self::Pkgar(other)
}
}
impl From<pkgar_core::Error> for Error {
fn from(other: pkgar_core::Error) -> Self {
Self::Pkgar(other.into())
}
}
impl From<reqwest::Error> for Error {
fn from(other: reqwest::Error) -> Self {
Self::Reqwest(other)
}
}
use pkgar_core::{HEADER_SIZE, Header, PackageSrc};
use sodiumoxide::crypto::sign::PublicKey;
use std::io::Read;
use crate::Error;
pub struct PackageUrl<'a> {
client: &'a reqwest::blocking::Client,
url: String,
header: Header,
}
impl<'a> PackageUrl<'a> {
pub fn new(
client: &'a reqwest::blocking::Client,
url: String,
public_key: &PublicKey
) -> Result<Self, Error> {
let mut new = Self {
client,
url,
// Need a blank header to construct the PackageFile, since we need to
// use a method of PackageSrc in order to get the actual header...
header: unsafe {
*Header::new_unchecked(&[0; HEADER_SIZE])?
},
};
new.header = new.read_header(public_key)?;
Ok(new)
}
}
impl<'a> PackageSrc for PackageUrl<'a> {
type Err = Error;
fn header(&self) -> Header {
self.header
}
fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize, Self::Err> {
if buf.is_empty() {
return Ok(0);
}
let end_offset = offset + (buf.len() as u64 - 1);
let range = format!("bytes={}-{}", offset, end_offset);
eprint!("Request {} from {}", range, self.url);
let mut response = self.client.get(&self.url)
.header(reqwest::header::RANGE, range)
.send()?
.error_for_status()?;
eprintln!(" = {:?}", response.status());
response.read_exact(buf)?;
Ok(buf.len())
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment