- sf-update (root): lists sf-shell / sf-app-* in the Gitea generic registry, downloads, checks the Ed25519 signature and unpacks to /var/lib/sf-pkg, switches via current/previous/staged symlinks; the factory copies on the read-only root stay as fallback. A new shell is on trial and rolls back after 3 failed starts. - Launcher: apps from factory + installed packages (newer version wins), Settings > System > Updates (check, install now), shell trial confirm. - Image: sf-update units (boot apply, 6-hourly download, launcher requests), sf-shell-exec wrapper for sf-remote, factory versions, registry token. - CI: .gitea/workflows/packages.yml builds, signs and uploads packages whose version isn't published yet (tools/pkg/packages.sh). - docs/updates.md: protocol and layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
4.5 KiB
Rust
133 lines
4.5 KiB
Rust
//! Gitea generic package registry client.
|
|
//!
|
|
//! list: GET {server}/api/v1/packages/{owner}?type=generic&q=sf-
|
|
//! download: GET {server}/api/packages/{owner}/generic/{name}/{version}/{name}-{version}.tar.zst (+ .sig)
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::io::Read;
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::Duration;
|
|
|
|
use anyhow::{Context, Result, bail};
|
|
use base64::Engine;
|
|
use serde::Deserialize;
|
|
use sf_ipc::manifest::version_cmp;
|
|
|
|
/// Largest package we'll download.
|
|
const MAX_PACKAGE: u64 = 1024 * 1024 * 1024;
|
|
|
|
use crate::config::Config;
|
|
|
|
/// Package names this updater handles.
|
|
pub fn is_ours(name: &str) -> bool {
|
|
name == "sf-shell" || name.strip_prefix("sf-app-").is_some_and(|id| !id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_'))
|
|
}
|
|
|
|
/// Release versions only: digits and dots (prereleases like 1.2.0-rc1 are skipped).
|
|
pub fn is_release(version: &str) -> bool {
|
|
!version.is_empty() && version.split('.').all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())) && version.split('.').count() <= 3
|
|
}
|
|
|
|
fn agent() -> ureq::Agent {
|
|
ureq::Agent::config_builder()
|
|
.timeout_connect(Some(Duration::from_secs(15)))
|
|
.timeout_recv_body(Some(Duration::from_secs(1800)))
|
|
.timeout_recv_response(Some(Duration::from_secs(60)))
|
|
.https_only(false)
|
|
.build()
|
|
.into()
|
|
}
|
|
|
|
fn get(cfg: &Config, agent: &ureq::Agent, url: &str) -> Result<ureq::http::Response<ureq::Body>> {
|
|
let mut req = agent.get(url);
|
|
if let Some(auth) = &cfg.auth {
|
|
let b64 = base64::engine::general_purpose::STANDARD.encode(auth);
|
|
req = req.header("Authorization", format!("Basic {b64}"));
|
|
}
|
|
req.call().with_context(|| format!("GET {}", strip_query(url)))
|
|
}
|
|
|
|
fn strip_query(url: &str) -> &str {
|
|
url.split('?').next().unwrap_or(url)
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct PackageVersion {
|
|
name: String,
|
|
version: String,
|
|
}
|
|
|
|
/// Newest release version of every package on the server.
|
|
pub fn latest_versions(cfg: &Config) -> Result<BTreeMap<String, String>> {
|
|
let agent = agent();
|
|
let mut latest: BTreeMap<String, String> = BTreeMap::new();
|
|
for page in 1..=100 {
|
|
let url = format!("{}/api/v1/packages/{}?type=generic&q=sf-&limit=50&page={page}", cfg.server, cfg.owner);
|
|
let text = get(cfg, &agent, &url)?.body_mut().read_to_string()?;
|
|
let list: Vec<PackageVersion> = serde_json::from_str(&text).context("package list")?;
|
|
if list.is_empty() {
|
|
break;
|
|
}
|
|
for p in list {
|
|
if !is_ours(&p.name) || !is_release(&p.version) {
|
|
continue;
|
|
}
|
|
match latest.get(&p.name) {
|
|
Some(v) if version_cmp(v, &p.version).is_ge() => {}
|
|
_ => {
|
|
latest.insert(p.name, p.version);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(latest)
|
|
}
|
|
|
|
/// File name of a package in the registry.
|
|
pub fn file_name(name: &str, version: &str) -> String {
|
|
format!("{name}-{version}.tar.zst")
|
|
}
|
|
|
|
/// Downloads a package and its signature into `dir`.
|
|
pub fn download(cfg: &Config, name: &str, version: &str, dir: &Path) -> Result<(PathBuf, Vec<u8>)> {
|
|
if !is_ours(name) || !is_release(version) {
|
|
bail!("refusing package {name} {version}");
|
|
}
|
|
let agent = agent();
|
|
let file = file_name(name, version);
|
|
let base = format!("{}/api/packages/{}/generic/{name}/{version}", cfg.server, cfg.owner);
|
|
|
|
let mut sig = Vec::new();
|
|
get(cfg, &agent, &format!("{base}/{file}.sig"))?.into_body().into_reader().take(1024).read_to_end(&mut sig)?;
|
|
|
|
let path = dir.join(&file);
|
|
let mut out = std::fs::File::create(&path).with_context(|| format!("creating {}", path.display()))?;
|
|
let resp = get(cfg, &agent, &format!("{base}/{file}"))?;
|
|
let mut body = resp.into_body().into_reader().take(MAX_PACKAGE + 1);
|
|
let n = std::io::copy(&mut body, &mut out).with_context(|| format!("downloading {file}"))?;
|
|
if n > MAX_PACKAGE {
|
|
bail!("{file} is larger than {MAX_PACKAGE} bytes");
|
|
}
|
|
out.sync_all()?;
|
|
log::info!("downloaded {file} ({n} bytes)");
|
|
Ok((path, sig))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn names_and_versions() {
|
|
assert!(is_ours("sf-shell"));
|
|
assert!(is_ours("sf-app-youtube"));
|
|
assert!(!is_ours("sf-app-"));
|
|
assert!(!is_ours("sf-app-../x"));
|
|
assert!(!is_ours("other"));
|
|
assert!(is_release("1.2.3"));
|
|
assert!(!is_release("1.2.3-rc1"));
|
|
assert!(!is_release("1..2"));
|
|
assert!(!is_release("../1"));
|
|
}
|
|
}
|