Commit be438b7d authored by Jeremy Soller's avatar Jeremy Soller
Browse files

WIP: Upgrading logic

parent 26ec0bd5
......@@ -13,10 +13,11 @@ path = "src/bin/pkg.rs"
[dependencies]
termion = "1.1"
hyper-rustls = "0.3"
tar = { git = "https://github.com/redox-os/tar-rs" }
serde = "1.0"
serde_derive = "1.0"
tar = { git = "https://github.com/redox-os/tar-rs" }
toml = "0.4"
version-compare = "0.0.4"
[dependencies.hyper]
version = "0.10"
......
......@@ -16,6 +16,7 @@ fn help() -> io::Result<()> {
write!(io::stderr(), " install [package] - install a package\n")?;
write!(io::stderr(), " list [package] - list package contents\n")?;
write!(io::stderr(), " sign [file] - get a file signature\n")?;
write!(io::stderr(), " upgrade - upgrade all packages\n")?;
Ok(())
}
......@@ -156,6 +157,16 @@ fn main() {
process::exit(1);
}
},
"upgrade" => {
match repo.upgrade() {
Ok(()) => {
let _ = write!(io::stderr(), "pkg: upgrade: succeeded\n");
},
Err(err) => {
let _ = write!(io::stderr(), "pkg: upgrade: failed: {}\n", err);
}
}
},
_ => {
let _ = write!(io::stderr(), "pkg: {}: unknown operation\n", op);
let _ = help();
......
......@@ -7,6 +7,7 @@ extern crate tar;
#[macro_use]
extern crate serde_derive;
extern crate toml;
extern crate version_compare;
use octavo::octavo_digest::Digest;
use octavo::octavo_digest::sha3::Sha512;
......@@ -14,9 +15,10 @@ use std::str;
use std::fs::{self, File};
use std::io::{self, stderr, Read, Write};
use std::path::Path;
use version_compare::{VersionCompare, CompOp};
pub use download::download;
pub use packagemeta::PackageMeta;
pub use packagemeta::{PackageMeta, PackageMetaList};
pub use package::Package;
mod download;
......@@ -160,6 +162,48 @@ impl Repo {
Ok(tardir)
}
pub fn upgrade(&self) -> io::Result<()> {
let mut local_list = PackageMetaList::new();
if Path::new("/pkg/").is_dir() {
for entry_res in fs::read_dir("/pkg/")? {
let entry = entry_res?;
let mut toml = String::new();
File::open(entry.path())?.read_to_string(&mut toml)?;
if let Ok(package) = PackageMeta::from_toml(&toml) {
local_list.packages.insert(package.name, package.version);
}
}
}
let tomlfile = self.sync("repo.toml")?;
let mut toml = String::new();
File::open(tomlfile)?.read_to_string(&mut toml)?;
let remote_list = PackageMetaList::from_toml(&toml).map_err(|err| {
io::Error::new(io::ErrorKind::InvalidData, format!("TOML error: {}", err))
})?;
for (package, version) in local_list.packages.iter() {
let remote_version = remote_list.packages.get(package).map_or("", |s| &s);
match VersionCompare::compare(version, remote_version).map_err(|_err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("{}: version parsing error when comparing {} and {}", package, version, remote_version)
)
})? {
CompOp::Lt => println!("{}: {} is older than {}", package, version, remote_version),
CompOp::Eq => println!("{}: {} is up to date with {}", package, version, remote_version),
CompOp::Gt => println!("{}: {} is newer than {}", package, version, remote_version),
other => println!("{}: did not expect {:?} when comparing {} and {}", package, other, version, remote_version),
}
}
Ok(())
}
pub fn add_remote(&mut self, remote: &str) {
self.remotes.push(remote.to_string());
}
......
use std::collections::BTreeMap;
use toml::{self, to_string, from_str};
#[derive(Serialize, Deserialize)]
......@@ -8,7 +9,7 @@ pub struct PackageMeta {
}
impl PackageMeta {
pub fn new(name: &str, version: &str, target: &str) -> PackageMeta {
pub fn new(name: &str, version: &str, target: &str) -> Self {
PackageMeta {
name: name.to_string(),
version: version.to_string(),
......@@ -25,3 +26,25 @@ impl PackageMeta {
to_string(self).unwrap()
}
}
#[derive(Serialize, Deserialize)]
pub struct PackageMetaList {
pub packages: BTreeMap<String, String>,
}
impl PackageMetaList {
pub fn new() -> Self {
PackageMetaList {
packages: BTreeMap::new()
}
}
pub fn from_toml(text: &str) -> Result<Self, toml::de::Error> {
from_str(text)
}
pub fn to_toml(&self) -> String {
// to_string *should* be safe to unwrap for this struct
to_string(self).unwrap()
}
}
Markdown is supported
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