NAH 1.0.6
Native Application Host - Library API Reference
Loading...
Searching...
No Matches
packaging.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <vector>
6
7namespace nah {
8
9// ============================================================================
10// Deterministic Packaging (per SPEC L1826-L1845)
11// ============================================================================
12
13// Entry type in a tar archive
14enum class TarEntryType {
17 Symlink, // NOT permitted - detection only
18 Hardlink, // NOT permitted - detection only
19 Other // NOT permitted - detection only
20};
21
22// A tar entry for deterministic packing
23struct TarEntry {
24 std::string path; // Relative path within archive
26 std::vector<uint8_t> data; // File content (empty for directories)
27 bool executable = false; // True if file should be 0755
28};
29
30// Result of a pack operation
31struct PackResult {
32 bool ok = false;
33 std::string error;
34 std::vector<uint8_t> archive_data; // The complete .tar.gz archive
35};
36
37// Result of an unpack operation
39 bool ok = false;
40 std::string error;
41 std::vector<std::string> entries; // Paths of extracted entries
42};
43
44// ============================================================================
45// Deterministic Tar+Gzip Archive Creation
46// ============================================================================
47
48// Create a deterministic gzip-compressed tar archive from entries
49// Per SPEC L1826-L1845:
50// - Entry ordering: lexicographic by full path, directories before files
51// - Metadata: uid=0, gid=0, uname="", gname="", mtime=0
52// - Permissions: dirs=0755, files=0644 (or 0755 if executable)
53// - Gzip: mtime=0, no filename, OS=255
54// - Symlinks/hardlinks NOT permitted (error if present)
55PackResult create_deterministic_archive(const std::vector<TarEntry>& entries);
56
57// Collect entries from a directory for packing
58// Returns entries sorted in deterministic order
59// Fails if symlinks or hardlinks are encountered
61 bool ok = false;
62 std::string error;
63 std::vector<TarEntry> entries;
64};
65
67
68// Convenience: pack a directory to an archive
70
71// ============================================================================
72// Safe Archive Extraction
73// ============================================================================
74
75// Extraction safety checks per SPEC L1836-L1845:
76// - Reject absolute paths
77// - Reject paths with .. or escaping extraction root
78// - Reject symlinks, hardlinks, device files, FIFOs, sockets
79// - Materialize only regular files and directories
80
81// Validate a path for extraction safety
83 bool safe = false;
84 std::string error;
85 std::string normalized_path; // Normalized relative path
86};
87
89 const std::string& extraction_root);
90
91// Extract a gzip tar archive to a staging directory
92// Uses safety validation on all entries
93// If any entry fails validation, extraction fails and staging is cleaned up
94UnpackResult extract_archive_safe(const std::vector<uint8_t>& archive_data,
95 const std::string& staging_dir);
96
97// Extract from a file path
99 const std::string& staging_dir);
100
101// ============================================================================
102// NAP Package Operations (per SPEC L2637-L2680)
103// ============================================================================
104
106 bool ok = false;
107 std::string error;
108
109 // Extracted manifest info
110 std::string app_id;
111 std::string app_version;
112 std::string nak_id;
113 std::string nak_version_req;
114 std::string entrypoint;
115
116 // Package structure
118 bool has_manifest_file = false;
119 std::string manifest_source; // "embedded:<binary>" or "file:manifest.nah"
120 std::vector<std::string> binaries;
121 std::vector<std::string> libraries;
122 std::vector<std::string> assets;
123};
124
125// Validate and inspect a NAP package without extracting
127NapPackageInfo inspect_nap_package(const std::vector<uint8_t>& archive_data);
128
129// Pack a directory as a NAP package
130// Validates structure and manifest presence
131PackResult pack_nap(const std::string& dir_path);
132
133// ============================================================================
134// NAK Pack Operations (per SPEC L2685-L2760)
135// ============================================================================
136
138 bool ok = false;
139 std::string error;
140
141 // From META/nak.toml
142 std::string schema;
143 std::string nak_id;
144 std::string nak_version;
145 std::string resource_root;
146 std::vector<std::string> lib_dirs;
147 bool has_loader = false;
148 std::string loader_exec_path;
149 std::vector<std::string> loader_args_template;
150 std::string execution_cwd;
151
152 // Package structure
153 std::vector<std::string> resources;
154 std::vector<std::string> libraries;
155 std::vector<std::string> binaries;
156};
157
158// Validate and inspect a NAK pack without extracting
160NakPackInfo inspect_nak_pack(const std::vector<uint8_t>& archive_data);
161
162// Pack a directory as a NAK pack
163// Validates structure and META/nak.toml presence
164PackResult pack_nak(const std::string& dir_path);
165
166// ============================================================================
167// Installation Operations
168// ============================================================================
169
171 std::string nah_root = "/nah";
172 std::string profile_name; // Optional: profile for NAK selection
173 bool force = false; // Overwrite existing installation
174 bool skip_verification = false; // Skip signature verification
175
176 // Provenance (for remote materialization)
177 std::string source; // Original source (URL or path), recorded in install record
178 std::string installed_by; // Who installed this (e.g., "ci-pipeline")
179 std::string expected_hash; // Expected SHA-256 hash (required for HTTPS)
180};
181
183 bool ok = false;
184 std::string error;
185 std::string install_root; // e.g., /nah/apps/com.example.app-1.0.0
186 std::string record_path; // e.g., /nah/registry/apps/com.example.app@1.0.0.toml
187 std::string instance_id;
188 std::string nak_id;
189 std::string nak_version;
190 std::string app_id;
191 std::string app_version;
192 std::string package_hash; // SHA-256 of the package
193};
194
195// Install a NAP package from a local file path
196// Per SPEC:
197// 1. Extract to staging directory
198// 2. Validate manifest and structure
199// 3. Select NAK version at install time
200// 4. Atomically rename to final location
201// 5. Write App Install Record atomically
204
205// Install a NAP package from any source
206// Accepts:
207// - Local file path (e.g., "./app.nap", "/path/to/app.nap")
208// - file: URL (e.g., "file:./app.nap")
209// - https: URL with SHA-256 (e.g., "https://example.com/app.nap#sha256=abc...")
210// For HTTPS sources, SHA-256 verification is mandatory.
211// Provenance is automatically recorded in the App Install Record.
212AppInstallResult install_app(const std::string& source,
214
216 std::string nah_root = "/nah";
217 bool force = false; // Overwrite existing installation
218
219 // Provenance (for remote materialization)
220 std::string source; // Original source (URL or path), recorded in install record
221 std::string installed_by; // Who installed this (e.g., "ci-pipeline")
222 std::string expected_hash; // Expected SHA-256 hash (required for HTTPS)
223};
224
226 bool ok = false;
227 std::string error;
228 std::string install_root; // e.g., /nah/naks/com.example.nak/1.0.0
229 std::string record_path; // e.g., /nah/registry/naks/com.example.nak@1.0.0.toml
230 std::string nak_id;
231 std::string nak_version;
232 std::string package_hash; // SHA-256 of the package
233};
234
235// Install a NAK pack from a local file path
236// Per SPEC:
237// 1. Extract to staging directory
238// 2. Validate META/nak.toml schema and required fields
239// 3. Atomically rename to final location
240// 4. Write NAK Install Record atomically with resolved absolute paths
243
244// Install a NAK pack from any source
245// Accepts:
246// - Local file path (e.g., "./sdk.nak", "/path/to/sdk.nak")
247// - file: URL (e.g., "file:./sdk.nak")
248// - https: URL with SHA-256 (e.g., "https://example.com/sdk.nak#sha256=abc...")
249// For HTTPS sources, SHA-256 verification is mandatory.
250// Provenance is automatically recorded in the NAK Install Record.
251NakInstallResult install_nak(const std::string& source,
253
254// ============================================================================
255// Uninstallation Operations
256// ============================================================================
257
259 bool ok = false;
260 std::string error;
261};
262
263// Uninstall an application
264// Removes app directory and install record atomically
265UninstallResult uninstall_app(const std::string& nah_root,
266 const std::string& app_id,
267 const std::string& version = "");
268
269// Uninstall a NAK
270// Fails if any installed apps reference this NAK version
271UninstallResult uninstall_nak(const std::string& nah_root,
272 const std::string& nak_id,
273 const std::string& version);
274
275// ============================================================================
276// Verification Operations
277// ============================================================================
278
280 bool ok = false;
281 std::string error;
282 std::vector<std::string> issues;
283 bool manifest_valid = false;
284 bool structure_valid = false;
285 bool nak_available = false;
286};
287
288// Verify an installed application
289VerifyResult verify_app(const std::string& nah_root,
290 const std::string& app_id,
291 const std::string& version = "");
292
293} // namespace nah
Result type for fallible operations.
Definition nahhost.hpp:109
VerifyResult verify_app(const std::string &nah_root, const std::string &app_id, const std::string &version="")
PackResult pack_nap(const std::string &dir_path)
NakInstallResult install_nak(const std::string &source, const NakInstallOptions &options)
UnpackResult extract_archive_safe(const std::vector< uint8_t > &archive_data, const std::string &staging_dir)
PackResult pack_directory(const std::string &dir_path)
UninstallResult uninstall_nak(const std::string &nah_root, const std::string &nak_id, const std::string &version)
PathValidation validate_extraction_path(const std::string &entry_path, const std::string &extraction_root)
UninstallResult uninstall_app(const std::string &nah_root, const std::string &app_id, const std::string &version="")
AppInstallResult install_app(const std::string &source, const AppInstallOptions &options)
AppInstallResult install_nap_package(const std::string &package_path, const AppInstallOptions &options)
PackResult create_deterministic_archive(const std::vector< TarEntry > &entries)
NakInstallResult install_nak_pack(const std::string &pack_path, const NakInstallOptions &options)
TarEntryType
Definition packaging.hpp:14
PackResult pack_nak(const std::string &dir_path)
CollectResult collect_directory_entries(const std::string &dir_path)
NakPackInfo inspect_nak_pack(const std::string &pack_path)
NapPackageInfo inspect_nap_package(const std::string &package_path)
std::string expected_hash
std::string instance_id
std::string record_path
std::string nak_version
std::string install_root
std::string package_hash
std::string app_version
std::vector< TarEntry > entries
Definition packaging.hpp:63
std::string error
Definition packaging.hpp:62
std::string expected_hash
std::string install_root
std::string nak_version
std::string record_path
std::string package_hash
std::vector< std::string > loader_args_template
std::string execution_cwd
std::string nak_version
std::string loader_exec_path
std::string schema
std::vector< std::string > binaries
std::string nak_id
std::string error
std::vector< std::string > lib_dirs
std::vector< std::string > libraries
std::string resource_root
std::vector< std::string > resources
std::vector< std::string > libraries
std::vector< std::string > assets
std::string manifest_source
std::vector< std::string > binaries
std::string app_version
std::string nak_version_req
std::string entrypoint
std::vector< uint8_t > archive_data
Definition packaging.hpp:34
std::string error
Definition packaging.hpp:33
std::string error
Definition packaging.hpp:84
std::string normalized_path
Definition packaging.hpp:85
std::vector< uint8_t > data
Definition packaging.hpp:26
std::string path
Definition packaging.hpp:24
TarEntryType type
Definition packaging.hpp:25
std::vector< std::string > entries
Definition packaging.hpp:41
std::string error
Definition packaging.hpp:40
std::vector< std::string > issues
std::string error