Add Store support and unsigned app releases
This commit is contained in:
14
crates/sf-pack/Cargo.toml
Normal file
14
crates/sf-pack/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "sf-pack"
|
||||
description = "Fennec OS store app packaging (docs/store.md)"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
sf-ipc.workspace = true
|
||||
anyhow.workspace = true
|
||||
serde.workspace = true
|
||||
toml.workspace = true
|
||||
tar = "0.4"
|
||||
ruzstd = "0.8"
|
||||
220
crates/sf-pack/src/main.rs
Normal file
220
crates/sf-pack/src/main.rs
Normal file
@@ -0,0 +1,220 @@
|
||||
//! sf-pack: builds Fennec OS store packages (docs/store.md).
|
||||
//!
|
||||
//! sf-pack build <app-dir> [out-dir] app.toml + <id>-<version>.tar.zst
|
||||
//! sf-pack verify <out-dir> checks what `build` made
|
||||
//! sf-pack run <app-dir> runs the app the way the TV does: in the
|
||||
//! store sandbox (Linux, needs bwrap)
|
||||
//!
|
||||
//! The two files in out-dir are the release assets: upload them to a
|
||||
//! release of the app's repo (GitHub, Gitea or GitLab).
|
||||
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use sf_ipc::AppManifest;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
if let Err(e) = run(&args) {
|
||||
eprintln!("sf-pack: {e:#}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run(args: &[String]) -> Result<()> {
|
||||
let arg = |i: usize| args.get(i).map(String::as_str);
|
||||
match arg(0) {
|
||||
Some("build") => {
|
||||
let dir = arg(1).context("usage: sf-pack build <app-dir> [out-dir]")?;
|
||||
build(Path::new(dir), Path::new(arg(2).unwrap_or("dist")))
|
||||
}
|
||||
Some("verify") => verify(Path::new(arg(1).unwrap_or("dist"))),
|
||||
Some("run") => run_app(Path::new(arg(1).context("usage: sf-pack run <app-dir>")?)),
|
||||
_ => bail!("usage: sf-pack build <app-dir> [out-dir] | verify [out-dir] | run <app-dir>"),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_manifest(dir: &Path) -> Result<AppManifest> {
|
||||
let path = dir.join("app.toml");
|
||||
let text = std::fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))?;
|
||||
let mut app: AppManifest = toml::from_str(&text).with_context(|| format!("parsing {}", path.display()))?;
|
||||
app.dir = dir.to_path_buf();
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
fn is_release(v: &str) -> bool {
|
||||
!v.is_empty() && v.split('.').count() <= 4 && v.split('.').all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit()))
|
||||
}
|
||||
|
||||
fn build(dir: &Path, out: &Path) -> Result<()> {
|
||||
let app = read_manifest(dir)?;
|
||||
app.check_store_rules().context("app.toml")?;
|
||||
if !is_release(&app.version) {
|
||||
bail!("version {:?} must be a release version like 1.2.3", app.version);
|
||||
}
|
||||
if app.icon.contains('.') && app.icon_file().is_none() {
|
||||
bail!("icon {} is missing from {}", app.icon, dir.display());
|
||||
}
|
||||
if let Some(exe) = app.resolved_exec(Path::new("/app")).first().and_then(|p| p.strip_prefix("/app/")) {
|
||||
let path = dir.join(exe);
|
||||
if !path.is_file() {
|
||||
bail!("exec {} is missing from {}", exe, dir.display());
|
||||
}
|
||||
check_executable(&path)?;
|
||||
}
|
||||
|
||||
std::fs::create_dir_all(out)?;
|
||||
let file = format!("{}-{}.tar.zst", app.id, app.version);
|
||||
let tar = pack_tar(dir, &app)?;
|
||||
let zst = ruzstd::encoding::compress_to_vec(&tar[..], ruzstd::encoding::CompressionLevel::Fastest);
|
||||
std::fs::write(out.join(&file), &zst)?;
|
||||
std::fs::copy(dir.join("app.toml"), out.join("app.toml"))?;
|
||||
println!("{}: {} ({} KiB)", app.id, out.join(&file).display(), zst.len() / 1024);
|
||||
println!("Release assets: app.toml, {file}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn check_executable(path: &Path) -> Result<()> {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
if std::fs::metadata(path)?.permissions().mode() & 0o111 == 0 {
|
||||
bail!("{} is not executable (chmod +x)", path.display());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn check_executable(_path: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The package: the app folder plus pkg.toml, reproducible (sorted, no
|
||||
/// owners, fixed time).
|
||||
fn pack_tar(dir: &Path, app: &AppManifest) -> Result<Vec<u8>> {
|
||||
let mut files = Vec::new();
|
||||
collect(dir, Path::new(""), &mut files)?;
|
||||
files.sort();
|
||||
let mut b = tar::Builder::new(Vec::new());
|
||||
b.follow_symlinks(false);
|
||||
let pkg = format!("name = \"{}\"\nversion = \"{}\"\n", app.id, app.version);
|
||||
let mut h = header(tar::EntryType::Regular, 0o644, pkg.len() as u64);
|
||||
b.append_data(&mut h, "pkg.toml", pkg.as_bytes())?;
|
||||
for rel in files {
|
||||
let path = dir.join(&rel);
|
||||
let meta = std::fs::symlink_metadata(&path)?;
|
||||
let mode = file_mode(&meta);
|
||||
if meta.file_type().is_symlink() {
|
||||
let mut h = header(tar::EntryType::Symlink, 0o777, 0);
|
||||
b.append_link(&mut h, &rel, std::fs::read_link(&path)?)?;
|
||||
} else if meta.is_dir() {
|
||||
let mut h = header(tar::EntryType::Directory, 0o755, 0);
|
||||
b.append_data(&mut h, &rel, std::io::empty())?;
|
||||
} else {
|
||||
// Windows has no x bit: whatever is under bin/ is executable.
|
||||
let exec = mode & 0o111 != 0 || rel.starts_with("bin");
|
||||
let mut h = header(tar::EntryType::Regular, if exec { 0o755 } else { 0o644 }, meta.len());
|
||||
b.append_data(&mut h, &rel, std::fs::File::open(&path)?)?;
|
||||
}
|
||||
}
|
||||
Ok(b.into_inner()?)
|
||||
}
|
||||
|
||||
fn header(kind: tar::EntryType, mode: u32, size: u64) -> tar::Header {
|
||||
let mut h = tar::Header::new_gnu();
|
||||
h.set_entry_type(kind);
|
||||
h.set_mode(mode);
|
||||
h.set_size(size);
|
||||
h.set_mtime(0);
|
||||
h.set_uid(0);
|
||||
h.set_gid(0);
|
||||
h
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn file_mode(meta: &std::fs::Metadata) -> u32 {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
meta.permissions().mode()
|
||||
}
|
||||
|
||||
/// On Windows nothing is executable; binaries under bin/ are.
|
||||
#[cfg(not(unix))]
|
||||
fn file_mode(_meta: &std::fs::Metadata) -> u32 {
|
||||
0o644
|
||||
}
|
||||
|
||||
fn collect(root: &Path, rel: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
|
||||
for e in std::fs::read_dir(root.join(rel))? {
|
||||
let e = e?;
|
||||
let name = e.file_name();
|
||||
if name == "pkg.toml" && rel.as_os_str().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let r = rel.join(&name);
|
||||
out.push(r.clone());
|
||||
if e.file_type()?.is_dir() {
|
||||
collect(root, &r, out)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify(out: &Path) -> Result<()> {
|
||||
let app = read_manifest(out)?;
|
||||
app.check_store_rules()?;
|
||||
let file = out.join(format!("{}-{}.tar.zst", app.id, app.version));
|
||||
let data = std::fs::read(&file).with_context(|| format!("reading {}", file.display()))?;
|
||||
let mut tar = Vec::new();
|
||||
ruzstd::decoding::StreamingDecoder::new(&data[..]).map_err(|e| anyhow::anyhow!("zstd: {e}"))?.read_to_end(&mut tar)?;
|
||||
let mut archive = tar::Archive::new(&tar[..]);
|
||||
let names: Vec<String> = archive.entries()?.filter_map(|e| e.ok()?.path().ok().map(|p| p.to_string_lossy().into_owned())).collect();
|
||||
if !names.iter().any(|n| n == "pkg.toml") || !names.iter().any(|n| n == "app.toml") {
|
||||
bail!("pkg.toml or app.toml missing from the package");
|
||||
}
|
||||
println!("{} {}: package OK, {} files", app.id, app.version, names.len());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn builds_and_verifies() {
|
||||
let tmp = std::env::temp_dir().join(format!("sf-pack-test-{}", std::process::id()));
|
||||
let app = tmp.join("app");
|
||||
std::fs::create_dir_all(app.join("bin")).unwrap();
|
||||
std::fs::write(
|
||||
app.join("app.toml"),
|
||||
"id = \"io.github.fox.hello\"\nname = \"Hello\"\nversion = \"1.2.3\"\nkind = \"exec\"\nexec = [\"bin/hello\"]\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(app.join("bin/hello"), "#!/bin/sh\necho hi\n").unwrap();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(app.join("bin/hello"), std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
}
|
||||
let out = tmp.join("dist");
|
||||
build(&app, &out).unwrap();
|
||||
verify(&out).unwrap();
|
||||
std::fs::remove_dir_all(&tmp).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs an app folder as a store app: same sandbox, same permissions.
|
||||
fn run_app(dir: &Path) -> Result<()> {
|
||||
let mut app = read_manifest(dir)?;
|
||||
app.check_store_rules().context("app.toml")?;
|
||||
app.origin = sf_ipc::Origin::Store;
|
||||
let settings = sf_ipc::Settings::default();
|
||||
let data = std::env::temp_dir().join("sf-pack-run");
|
||||
let opts = sf_ipc::launch::LaunchOptions { data_dir: &data, fullscreen: false, bounds: None, query: None };
|
||||
let mut cmd = sf_ipc::launch::command(&app, &settings, &opts)?;
|
||||
eprintln!("data folder ($HOME inside): {}", sf_ipc::paths::app_data_dir(&app.id).display());
|
||||
if std::env::var_os("SF_PACK_SHOW").is_some() {
|
||||
eprintln!("{} {}", cmd.get_program().to_string_lossy(), cmd.get_args().map(|a| a.to_string_lossy()).collect::<Vec<_>>().join(" "));
|
||||
}
|
||||
let status = cmd.status().context("starting the app")?;
|
||||
std::process::exit(status.code().unwrap_or(1));
|
||||
}
|
||||
Reference in New Issue
Block a user