Skip to content

Commit

Permalink
tough: Use transports by reference
Browse files Browse the repository at this point in the history
Signed-off-by: iliana destroyer of worlds <[email protected]>
  • Loading branch information
iliana committed Nov 7, 2019
1 parent 82a368d commit 558487e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
3 changes: 2 additions & 1 deletion workspaces/tuftool/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ impl DownloadArgs {
};

// load repository
let transport = HttpTransport::new();
let repo_dir = TempDir::new("tuf").context(error::TempDir)?;
let repository = Repository::load(
HttpTransport::new(),
&transport,
Settings {
root: File::open(&root_path).context(error::OpenRoot { path: &root_path })?,
datastore: repo_dir.path(),
Expand Down
20 changes: 7 additions & 13 deletions workspaces/updater/tough/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Default for Limits {
/// You can create a `Repository` using the `load` method.
#[derive(Debug, Clone)]
pub struct Repository<'a, T: Transport> {
transport: T,
transport: &'a T,
consistent_snapshot: bool,
datastore: Datastore<'a>,
earliest_expiration: DateTime<Utc>,
Expand Down Expand Up @@ -136,15 +136,15 @@ impl<'a, T: Transport> Repository<'a, T> {
///
/// `metadata_base_url` and `target_base_url` are the HTTP(S) base URLs for where the client
/// can find metadata (such as root.json) and targets (as listed in targets.json).
pub fn load<R: Read>(transport: T, settings: Settings<'a, R>) -> Result<Self> {
pub fn load<R: Read>(transport: &'a T, settings: Settings<'a, R>) -> Result<Self> {
let metadata_base_url = parse_url(settings.metadata_base_url)?;
let target_base_url = parse_url(settings.target_base_url)?;

let datastore = Datastore(settings.datastore);

// 0. Load the trusted root metadata file + 1. Update the root metadata file
let root = load_root(
&transport,
transport,
settings.root,
&datastore,
settings.limits.max_root_size,
Expand All @@ -154,25 +154,19 @@ impl<'a, T: Transport> Repository<'a, T> {

// 2. Download the timestamp metadata file
let timestamp = load_timestamp(
&transport,
transport,
&root,
&datastore,
settings.limits.max_timestamp_size,
&metadata_base_url,
)?;

// 3. Download the snapshot metadata file
let snapshot = load_snapshot(
&transport,
&root,
&timestamp,
&datastore,
&metadata_base_url,
)?;
let snapshot = load_snapshot(transport, &root, &timestamp, &datastore, &metadata_base_url)?;

// 4. Download the targets metadata file
let targets = load_targets(
&transport,
transport,
&root,
&snapshot,
&datastore,
Expand Down Expand Up @@ -255,7 +249,7 @@ impl<'a, T: Transport> Repository<'a, T> {
};

Some(fetch_sha256(
&self.transport,
self.transport,
self.target_base_url.join(&file).context(error::JoinUrl {
path: file,
url: self.target_base_url.to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion workspaces/updater/tough/tests/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_tuf_reference_impl() {
let target_base_url = &dir_url(base.join("targets"));

let repo = Repository::load(
tough::FilesystemTransport,
&tough::FilesystemTransport,
Settings {
root: File::open(base.join("metadata").join("1.root.json")).unwrap(),
datastore: datastore.as_ref(),
Expand Down
7 changes: 4 additions & 3 deletions workspaces/updater/updog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ fn load_config() -> Result<Config> {
Ok(config)
}

fn load_repository(config: &Config) -> Result<HttpRepo<'_>> {
fn load_repository<'a>(transport: &'a HttpTransport, config: &'a Config) -> Result<HttpRepo<'a>> {
fs::create_dir_all("/var/lib/thar/updog").context(error::CreateMetadataCache)?;
Repository::load(
HttpTransport::new(),
transport,
Settings {
root: File::open(TRUSTED_ROOT_PATH).context(error::OpenRoot {
path: TRUSTED_ROOT_PATH,
Expand Down Expand Up @@ -436,7 +436,8 @@ fn main_inner() -> Result<()> {
serde_plain::from_str::<Command>(&arguments.subcommand).unwrap_or_else(|_| usage());

let config = load_config()?;
let repository = load_repository(&config)?;
let transport = HttpTransport::new();
let repository = load_repository(&transport, &config)?;
let manifest = load_manifest(&repository)?;
let (current_version, flavor) = running_version().unwrap();

Expand Down

0 comments on commit 558487e

Please sign in to comment.