Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ is on main, the release is published with that section as its notes (AGENTS.md,

### Command line

- `orchard resize` no longer fails with "http status: 403" after many runs
from one network: it finds the newest Resize APFS through the release page,
not GitHub's API, which allows 60 lookups an hour.
- `orchard resize --amethyst` also works on iOS 12.0 and 12.1: Resize APFS
0.5.1 brings the Swift runtime those versions lack.
- The iPhone 5's GPU firmware and its graphics event template now build from
Expand Down
66 changes: 49 additions & 17 deletions crates/runtime/src/resize_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use plist::Value;
use std::path::PathBuf;
use std::time::Duration;

const LATEST: &str = "https://api.github.com/repos/OrchardLinux/resize-apfs/releases/latest";
/// Redirects to the newest release's page, whose last part is its tag. The
/// release pages, not GitHub's API: the API allows 60 unauthenticated
/// requests an hour per address and then answers 403.
const LATEST: &str = "https://github.com/OrchardLinux/resize-apfs/releases/latest";
const DOWNLOAD: &str = "https://github.com/OrchardLinux/resize-apfs/releases/download";
const TIMEOUT: Duration = Duration::from_secs(30);
/// The app's members in the release, below its top folder.
const APP: &str = "out/device/ResizeAPFS.app/";
Expand Down Expand Up @@ -82,25 +86,30 @@ fn get(url: &str, limit: u64) -> Result<Vec<u8>> {
response.body_mut().with_config().limit(limit).read_to_vec().map_err(network)
}

/// The tag in the address the newest release's page redirects to.
fn tag_of(location: &str) -> Option<String> {
let tag = location.trim_end_matches('/').rsplit_once("/releases/tag/")?.1;
(!tag.is_empty() && tag.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_'))).then(|| tag.to_owned())
}

/// The newest release: its version, and its ZIP with the checksum the
/// release publishes beside it.
/// release publishes beside it (`ResizeAPFS-<tag>.zip` and `.zip.sha256`).
fn newest() -> Result<(String, Receipt)> {
let release: serde_json::Value = serde_json::from_slice(&get(LATEST, 1 << 20)?).map_err(|_| format("GitHub's answer is not JSON"))?;
let version = release["tag_name"].as_str().ok_or_else(|| format("no version"))?.to_owned();
let asset = |suffix: &str| {
release["assets"].as_array().into_iter().flatten().find(|a| a["name"].as_str().is_some_and(|n| n.ends_with(suffix)))
};
let zip = asset(".zip").ok_or_else(|| format("no ZIP"))?;
let sums = asset(".zip.sha256").and_then(|a| a["browser_download_url"].as_str()).ok_or_else(|| format("no checksum"))?;
let sums = String::from_utf8(get(sums, 4096)?).map_err(|_| format("the checksum is not text"))?;
let network = |e: ureq::Error| Error::new(Code::Network, e.to_string()).context(LATEST);
let agent: ureq::Agent = ureq::Agent::config_builder()
.https_only(true)
.max_redirects(0)
.timeout_global(Some(TIMEOUT))
.build()
.into();
let response = agent.get(LATEST).call().map_err(network)?;
let location = response.headers().get("Location").and_then(|v| v.to_str().ok()).ok_or_else(|| format("no newest release"))?;
let version = tag_of(location).ok_or_else(|| format(format!("no release tag in {location}")))?;
let zip = format!("{DOWNLOAD}/{version}/ResizeAPFS-{version}.zip");
let sums = String::from_utf8(get(&format!("{zip}.sha256"), 4096)?).map_err(|_| format("the checksum is not text"))?;
let sha256 = sums.split_whitespace().next().filter(|h| h.len() == 64).ok_or_else(|| format("the checksum is not a SHA-256"))?;
let receipt = Receipt {
url: zip["browser_download_url"].as_str().ok_or_else(|| format("no download address"))?.to_owned(),
bytes: zip["size"].as_u64().ok_or_else(|| format("no size"))?,
sha256: sha256.to_owned(),
expanded_bytes: None,
};
Ok((version, receipt))
let bytes = orchard_cache::source::Source::size(&http::Http::probe(&zip, TIMEOUT)?);
Ok((version, Receipt { url: zip, bytes, sha256: sha256.to_owned(), expanded_bytes: None }))
}

/// The newest release of the app, downloaded once and checked.
Expand Down Expand Up @@ -197,3 +206,26 @@ impl App {
Ok(path)
}
}

#[cfg(test)]
mod lookup_tests {
use super::*;

#[test]
fn the_tag_comes_from_the_release_page_address() {
assert_eq!(tag_of("https://github.com/OrchardLinux/resize-apfs/releases/tag/v0.5.1").as_deref(), Some("v0.5.1"));
assert_eq!(tag_of("https://github.com/OrchardLinux/resize-apfs/releases/tag/v0.5.1/").as_deref(), Some("v0.5.1"));
assert_eq!(tag_of("https://github.com/OrchardLinux/resize-apfs/releases"), None);
assert_eq!(tag_of("https://github.com/OrchardLinux/resize-apfs/releases/tag/"), None);
assert_eq!(tag_of("https://github.com/OrchardLinux/resize-apfs/releases/tag/../x?y"), None);
}

/// Against GitHub: `cargo test -p orchard-runtime -- --ignored newest_release`.
#[test]
#[ignore]
fn newest_release_is_found_without_the_api() {
let (version, receipt) = newest().unwrap();
assert!(version.starts_with('v'), "{version}");
assert!(receipt.bytes > 1 << 20 && receipt.sha256.len() == 64, "{receipt:?}");
}
}
Loading