Files
os/crates/sf-ipc/src/store.rs
2026-10-25 14:11:49 +01:00

181 lines
5.7 KiB
Rust

//! The app store's files, shared by the launcher and sf-update (docs/store.md).
//!
//! /var/lib/smartfox/store.toml what the user wants: sources, apps and
//! the permissions they approved (the
//! launcher writes it, user tv)
//! /var/lib/sf-pkg/store/catalog.json what the sources offer, and why an
//! update is held back (sf-update, root)
//! /var/lib/sf-pkg/store/apps/<id> installed store apps (sf-update)
use std::collections::BTreeMap;
use std::path::Path;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use crate::manifest::Permission;
pub const WANTED_FILE: &str = "/var/lib/smartfox/store.toml";
pub const CATALOG_FILE: &str = "/var/lib/sf-pkg/store/catalog.json";
/// Package (folder) name of a store app in /var/lib/sf-pkg.
pub const PACKAGE_PREFIX: &str = "store-";
/// The file a catalog repo keeps on its default branch.
pub const INDEX_FILE: &str = "smartfox-store.toml";
/// Largest store.toml sf-update reads.
pub const MAX_WANTED: u64 = 256 * 1024;
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Wanted {
/// Repos the user added: app repos and catalogs.
#[serde(default)]
pub sources: Vec<String>,
/// Installed (or to be installed) apps by id.
#[serde(default)]
pub apps: BTreeMap<String, WantedApp>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct WantedApp {
/// The repo the app comes from (canonical https URL).
pub source: String,
/// Permissions the user approved; an update that asks for more waits
/// for a new approval.
#[serde(default)]
pub permissions: Vec<Permission>,
}
impl Wanted {
pub fn parse(text: &str) -> Result<Wanted> {
toml::from_str(text).context("store.toml")
}
pub fn load(path: &Path) -> Result<Wanted> {
match std::fs::read_to_string(path) {
Ok(text) => Wanted::parse(&text),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Wanted::default()),
Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
}
}
/// Writes atomically (the updater may read it at any time).
pub fn save(&self, path: &Path) -> Result<()> {
let tmp = path.with_extension("toml.tmp");
std::fs::write(&tmp, toml::to_string_pretty(self)?).with_context(|| format!("writing {}", tmp.display()))?;
std::fs::rename(&tmp, path).with_context(|| format!("writing {}", path.display()))?;
Ok(())
}
}
/// `smartfox-store.toml` in a catalog repo.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Index {
#[serde(default)]
pub name: String,
#[serde(default)]
pub description: String,
/// App repo addresses.
#[serde(default)]
pub apps: Vec<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Catalog {
/// Unix time of the last refresh (0: never).
#[serde(default)]
pub updated_at: u64,
#[serde(default)]
pub sources: Vec<SourceInfo>,
#[serde(default)]
pub apps: Vec<CatalogApp>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SourceInfo {
/// As in store.toml.
pub url: String,
/// "app" or "catalog"
#[serde(default)]
pub kind: String,
#[serde(default)]
pub name: String,
/// Apps found in it.
#[serde(default)]
pub apps: usize,
#[serde(default)]
pub error: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CatalogApp {
pub id: String,
pub name: String,
pub version: String,
#[serde(default)]
pub kind: String,
#[serde(default)]
pub category: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub author: String,
#[serde(default)]
pub homepage: String,
#[serde(default)]
pub license: String,
#[serde(default)]
pub accent: String,
#[serde(default)]
pub permissions: Vec<Permission>,
/// The app's repo (canonical https URL).
pub source: String,
/// The catalog it was listed in, if any.
#[serde(default)]
pub catalog: Option<String>,
pub package_url: String,
/// Installed version, if any.
#[serde(default)]
pub installed: Option<String>,
/// Why the newest version isn't installed: "permissions" (asks for more
/// than approved).
#[serde(default)]
pub blocked: Option<String>,
}
impl Catalog {
pub fn load(path: &Path) -> Option<Catalog> {
serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok()
}
pub fn app(&self, id: &str) -> Option<&CatalogApp> {
self.apps.iter().find(|a| a.id == id)
}
}
/// Permissions in `want` that `approved` doesn't have.
pub fn missing_permissions(want: &[Permission], approved: &[Permission]) -> Vec<Permission> {
want.iter().filter(|p| !approved.contains(p)).copied().collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wanted_round_trip() {
let w = Wanted::parse(
r#"
sources = ["https://github.com/fox/store"]
[apps."io.github.fox.hello"]
source = "https://github.com/fox/hello"
permissions = ["network", "audio"]
"#,
)
.unwrap();
assert_eq!(w.apps["io.github.fox.hello"].permissions, [Permission::Network, Permission::Audio]);
let again = Wanted::parse(&toml::to_string_pretty(&w).unwrap()).unwrap();
assert_eq!(w, again);
assert!(Wanted::parse("[apps.x]\nsource = 'a'\npermissions = ['root']\n").is_err());
assert_eq!(missing_permissions(&[Permission::Network, Permission::Gpu], &[Permission::Network]), [Permission::Gpu]);
}
}