Something went wrong. Try again.
A general purpose .. programming language written in zig ijadux2.tngl.sh/Rinnescript
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232//! Package manager backing the module-level `@fetch` directive.//!//! Downloads are cached under $RINNE_CACHE_HOME (or $HOME/.cache/rinne/pkg),//! keyed by a short sha256 of (url, version). Supported targets://! - direct .rg file URLs -> fetched verbatim, cached as source.rg//! - .tar.gz / .tgz archive URLs -> extracted into <key>/src/, entry *.rg discovered//! - repo shorthands w/ version -> rewritten to host archive URLs pinned by tag://! github.com/u/r @v1.2.0 -> https://codeload.github.com/u/r/tar.gz/refs/tags/v1.2.0//! gitlab.com/g/r @v1.2.0 -> https://gitlab.com/g/r/-/archive/v1.2.0/r-v1.2.0.tar.gz//! codeberg.org/u/r @v1.2.0 -> https://codeberg.org/u/r/archive/v1.2.0.tar.gz//!//! Network access lives behind the fetch_impl seam so tests run offline.const std = @import("std");
pub const Error = error{ OutOfMemory, UnsupportedUrl, MissingVersion, MissingRef, DownloadFailed, PackageTooLarge, ChecksumMismatch, OfflineMode, BadArchive, CacheUnavailable, InvalidPackage, GitFailed, GitNotInstalled,};
/// Hard cap on a single download body (v1 buffers whole responses).pub const max_download_bytes: usize = 64 * 1024 * 1024;/// Hard cap on total extracted archive bytes.const max_extract_bytes: u64 = max_download_bytes * 8;
/// Injectable transport: copies the resource at `url` into `out`.pub const FetchFn = *const fn (pm: *PackageManager, url: []const u8, out: *std.ArrayList(u8)) anyerror!void;
const TargetKind = enum { direct_rg, archive };
const Target = struct { kind: TargetKind, /// Fully-resolved URL to download. fetch_url: []u8,};
fn startsWithScheme(url: []const u8, scheme: []const u8) bool { if (url.len < scheme.len + 3) return false; if (!std.mem.eql(u8, url[0..scheme.len], scheme)) return false; return url[scheme.len] == ':' and url[scheme.len + 1] == '/' and url[scheme.len + 2] == '/';}
fn isHttpScheme(url: []const u8) bool { return startsWithScheme(url, "http") or startsWithScheme(url, "https");}
/// "file:///abs/path" -> "/abs/path" (when the remainder starts with '/');/// "file://rel/path" -> "rel/path".pub fn filePathOf(url: []const u8) []const u8 { var rest = url; if (startsWithScheme(rest, "file")) rest = rest[7..]; return rest;}
pub fn isFileUrl(url: []const u8) bool { return startsWithScheme(url, "file");}/// Strips an optional scheme and returns the host portion (up to first '/').fn hostOf(url: []const u8) []const u8 { var rest = url; if (startsWithScheme(rest, "https")) { rest = rest["https://".len..]; } else if (startsWithScheme(rest, "http")) { rest = rest["http://".len..]; } const slash = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len; return rest[0..slash];}
fn isShorthandHost(host: []const u8) bool { const hosts = [_][]const u8{ "github.com", "gitlab.com", "codeberg.org" }; for (hosts) |h| { if (std.mem.eql(u8, host, h)) return true; } return false;}
/// Rewrites repo shorthands into archive URLs; classifies direct URLs./// `fetch_url` is owned by the caller (free with `allocator`).pub fn classify(allocator: std.mem.Allocator, url: []const u8, version: ?[]const u8) Error!Target { const host = hostOf(url); if (isShorthandHost(host)) { const ver = version orelse return Error.MissingVersion; var rest = url; if (startsWithScheme(rest, "https")) { rest = rest["https://".len..]; } else if (startsWithScheme(rest, "http")) { rest = rest["http://".len..]; } const after_host = if (rest.len > host.len) rest[host.len + 1 ..] else ""; var segs: [16][]const u8 = undefined; var nsegs: usize = 0; var it = std.mem.splitScalar(u8, after_host, '/'); while (it.next()) |seg| { if (seg.len == 0) continue; if (nsegs >= segs.len) break; segs[nsegs] = seg; nsegs += 1; } if (nsegs < 2) return Error.UnsupportedUrl; // GitHub/Codeberg: owner/repo (extra path ignored). GitLab allows // subgroups, so its repo is the LAST segment. var repo = if (std.mem.eql(u8, host, "gitlab.com")) segs[nsegs - 1] else segs[1]; if (std.mem.endsWith(u8, repo, ".git")) repo = repo[0 .. repo.len - 4];
const fetch_url = if (std.mem.eql(u8, host, "github.com")) try std.fmt.allocPrint(allocator, "https://codeload.github.com/{s}/{s}/tar.gz/refs/tags/{s}", .{ segs[0], repo, ver }) else if (std.mem.eql(u8, host, "gitlab.com")) blk: { var full = std.ArrayList(u8).empty; errdefer full.deinit(allocator); for (segs[0..nsegs], 0..) |seg, i| { if (i != 0) try full.append(allocator, '/'); try full.appendSlice(allocator, seg); } defer full.deinit(allocator); break :blk try std.fmt.allocPrint(allocator, "https://gitlab.com/{s}/-/archive/{s}/{s}-{s}.tar.gz", .{ full.items, ver, repo, ver }); } else try std.fmt.allocPrint(allocator, "https://codeberg.org/{s}/{s}/archive/{s}.tar.gz", .{ segs[0], repo, ver }); return .{ .kind = .archive, .fetch_url = fetch_url }; }
if (!isHttpScheme(url)) { // file:// URLs are read straight off the local disk: offline-friendly // for tests and lock-file driven cache reuse. if (isFileUrl(url)) { if (std.mem.endsWith(u8, url, ".rg")) return .{ .kind = .direct_rg, .fetch_url = try allocator.dupe(u8, url) }; if (std.mem.endsWith(u8, url, ".tar.gz") or std.mem.endsWith(u8, url, ".tgz")) return .{ .kind = .archive, .fetch_url = try allocator.dupe(u8, url) }; return Error.UnsupportedUrl; } return Error.UnsupportedUrl; } if (std.mem.endsWith(u8, url, ".rg")) return .{ .kind = .direct_rg, .fetch_url = try allocator.dupe(u8, url) }; if (std.mem.endsWith(u8, url, ".tar.gz") or std.mem.endsWith(u8, url, ".tgz")) return .{ .kind = .archive, .fetch_url = try allocator.dupe(u8, url) }; return Error.UnsupportedUrl;}
pub const PackageManager = struct { allocator: std.mem.Allocator, io: std.Io, /// Absolute path of the package cache root (owned). cache_dir: []u8, offline: bool = false, fetch_impl: FetchFn, /// Human-readable detail for the most recent error (owned; may be null). last_error: ?[]u8 = null,
pub fn deinit(self: *PackageManager) void { self.allocator.free(self.cache_dir); if (self.last_error) |m| self.allocator.free(m); self.last_error = null; }
/// Computes the cache root: rinne_cache_home wins, else $home/.cache/rinne/pkg. /// offline_env equal to "1" enables offline mode. pub fn init( allocator: std.mem.Allocator, io: std.Io, rinne_cache_home: ?[]const u8, home: ?[]const u8, offline_env: ?[]const u8, ) Error!PackageManager { const root: []const u8 = blk: { if (rinne_cache_home) |c| { if (c.len != 0) break :blk c; } const h = home orelse return Error.CacheUnavailable; break :blk std.fmt.allocPrint(allocator, "{s}/.cache/rinne/pkg", .{h}) catch return Error.OutOfMemory; }; const owned = allocator.dupe(u8, root) catch return Error.OutOfMemory; return .{ .allocator = allocator, .io = io, .cache_dir = owned, .offline = offline_env != null and std.mem.eql(u8, offline_env.?, "1"), .fetch_impl = defaultFetch, }; }
fn setError(self: *PackageManager, comptime fmt: []const u8, args: anytype) void { if (self.last_error) |m| self.allocator.free(m); self.last_error = std.fmt.allocPrint(self.allocator, fmt, args) catch null; }
fn keyFor(self: *PackageManager, url: []const u8, version: ?[]const u8) Error![32]u8 { const material = std.fmt.allocPrint(self.allocator, "{s}\x00{s}", .{ url, version orelse "" }) catch return Error.OutOfMemory; defer self.allocator.free(material); var digest: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(material, &digest, .{}); return digest; }
/// Resolves url (+optional pinned version, optional sha256_hex) to /// package source text. Caller owns the returned slice. pub fn resolve( self: *PackageManager, url: []const u8, version: ?[]const u8, sha256_hex: ?[]const u8, ) Error![]u8 { const target = try classify(self.allocator, url, version); defer self.allocator.free(target.fetch_url);
const digest = try self.keyFor(url, version); var key_hex: [32]u8 = undefined; const key_str = hexLower(&key_hex, digest[0..16]); const dir = std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ self.cache_dir, key_str }) catch return Error.OutOfMemory; defer self.allocator.free(dir);
if (self.cacheHit(dir, url, version, sha256_hex)) |src| return src;
if (self.offline) { self.setError("package '{s}' is not cached and RINNE_OFFLINE=1 is set", .{url}); return Error.OfflineMode; }
var body: std.ArrayList(u8) = .empty; defer body.deinit(self.allocator); if (isFileUrl(url)) { // Local resources are read straight from disk (no network). const fpath = filePathOf(url); const local_src = std.Io.Dir.cwd().readFileAlloc(self.io, fpath, self.allocator, .limited(max_download_bytes)) catch { self.setError("cannot read local package '{s}'", .{fpath}); return Error.DownloadFailed; }; defer self.allocator.free(local_src); body.appendSlice(self.allocator, local_src) catch return Error.OutOfMemory; } else { self.fetch_impl(self, target.fetch_url, &body) catch |e| { if (e == Error.PackageTooLarge) { self.setError("download from '{s}' exceeds the {d} MiB limit", .{ target.fetch_url, max_download_bytes / (1024 * 1024) }); return Error.PackageTooLarge; } self.setError("download from '{s}' failed ({s}); fetch it manually into {s}", .{ target.fetch_url, @errorName(e), self.cache_dir }); return Error.DownloadFailed; }; } if (body.items.len > max_download_bytes) return Error.PackageTooLarge;
if (sha256_hex) |want| { var digest_full: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(body.items, &digest_full, .{}); var got_hex: [64]u8 = undefined; const got_str = hexLower(&got_hex, &digest_full); if (!std.ascii.eqlIgnoreCase(want, got_str)) { self.setError("sha256 mismatch for '{s}': expected {s}, got {s}", .{ url, want, got_str }); return Error.ChecksumMismatch; } }
std.Io.Dir.cwd().createDirPath(self.io, dir) catch { self.setError("cannot create cache directory '{s}'", .{dir}); return Error.CacheUnavailable; };
const entry_abs: []u8 = switch (target.kind) { .direct_rg => blk: { const p = std.fmt.allocPrint(self.allocator, "{s}/source.rg", .{dir}) catch return Error.OutOfMemory; std.Io.Dir.cwd().writeFile(self.io, .{ .sub_path = p, .data = body.items }) catch { self.allocator.free(p); self.setError("cannot write cache file under '{s}'", .{dir}); return Error.CacheUnavailable; }; break :blk p; }, .archive => blk: { const src_root = std.fmt.allocPrint(self.allocator, "{s}/src", .{dir}) catch return Error.OutOfMemory; defer self.allocator.free(src_root); std.Io.Dir.cwd().createDirPath(self.io, src_root) catch { self.setError("cannot create cache directory '{s}'", .{src_root}); return Error.CacheUnavailable; }; var candidates: std.ArrayList([]u8) = .empty; errdefer { for (candidates.items) |c| self.allocator.free(c); candidates.deinit(self.allocator); } try self.extractTarGz(body.items, src_root, &candidates); defer { for (candidates.items) |c| self.allocator.free(c); candidates.deinit(self.allocator); } break :blk try self.allocator.dupe(u8, try self.pickEntry(&candidates)); }, }; defer self.allocator.free(entry_abs);
self.writeManifest(dir, url, version, target.fetch_url, entry_abs, sha256_hex);
return std.Io.Dir.cwd().readFileAlloc(self.io, entry_abs, self.allocator, .limited(max_download_bytes)) catch { self.setError("cannot read cached package source '{s}'", .{entry_abs}); return Error.CacheUnavailable; }; }
fn gitKeyFor(self: *PackageManager, git_url: []const u8, ref: []const u8) Error![32]u8 { const material = std.fmt.allocPrint(self.allocator, "git\x00{s}\x00{s}", .{ git_url, ref }) catch return Error.OutOfMemory; defer self.allocator.free(material); var digest: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(material, &digest, .{}); return digest; }
/// Runs `git -C <cwd?> argv...`, capturing stderr into last_error on /// failure. Returns true when the command exited 0. fn gitRun(self: *PackageManager, cwd: ?[]const u8, argv: []const []const u8) Error!bool { var argv_all: std.ArrayList([]const u8) = .empty; defer argv_all.deinit(self.allocator); try argv_all.appendSlice(self.allocator, &.{ "git", "-c", "core.askPass=true" }); if (cwd) |d| try argv_all.appendSlice(self.allocator, &.{ "-C", d }); try argv_all.appendSlice(self.allocator, argv); const res = std.process.run(self.allocator, self.io, .{ .argv = argv_all.items, .stdout_limit = .limited(16 * 1024 * 1024), .stderr_limit = .limited(16 * 1024 * 1024), }) catch |e| { self.setError("git {s}: {s}", .{ argv[0], @errorName(e) }); return Error.GitFailed; }; defer { self.allocator.free(res.stdout); self.allocator.free(res.stderr); } if (res.term == .exited and res.term.exited == 0) return true; self.setError("git {s} failed: {s}", .{ argv[0], std.mem.trim(u8, res.stderr, " \n") }); return Error.GitFailed; }
/// Cached git package hit: manifest must match (git, git_ref); entry sha256 /// must agree with any caller-supplied pin. Returns owned source, else null. fn gitCacheHit(self: *PackageManager, dir: []const u8, git_url: []const u8, ref: []const u8, sha256_hex: ?[]const u8) ?[]u8 { const mf_path = std.fmt.allocPrint(self.allocator, "{s}/manifest.json", .{dir}) catch return null; defer self.allocator.free(mf_path); const text = std.Io.Dir.cwd().readFileAlloc(self.io, mf_path, self.allocator, .limited(64 * 1024)) catch return null; defer self.allocator.free(text); var parsed = std.json.parseFromSlice(std.json.Value, self.allocator, text, .{}) catch return null; defer parsed.deinit(); const obj = switch (parsed.value) { .object => |o| o, else => return null, }; const m_git = obj.get("git") orelse return null; const m_ref = obj.get("git_ref") orelse return null; const m_entry = obj.get("entry") orelse return null; const m_sha = obj.get("sha256") orelse return null; if (m_git != .string or m_ref != .string or m_entry != .string or m_sha != .string) return null; if (!std.mem.eql(u8, m_git.string, git_url)) return null; if (!std.mem.eql(u8, m_ref.string, ref)) return null; if (sha256_hex) |want| { if (!std.ascii.eqlIgnoreCase(want, m_sha.string)) return null; } const entry_abs = std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ dir, m_entry.string }) catch return null; defer self.allocator.free(entry_abs); return std.Io.Dir.cwd().readFileAlloc(self.io, entry_abs, self.allocator, .limited(max_download_bytes)) catch null; }
/// Resolves a git repository (+ optional ref: tag, branch, or commit) to its /// entry .rg source. A null ref clones the default branch (HEAD). The /// entry may use `@include` on sibling files, so the whole checkout is kept /// in the cache keyed by (git, ref); the recorded commit makes the pin /// content-addressed. Caller owns the returned source. pub fn resolveGit(self: *PackageManager, git_url: []const u8, ref: ?[]const u8, sha256_hex: ?[]const u8) Error![]u8 { const r = ref orelse ""; const dir = try self.gitKeyDir(git_url, r); defer self.allocator.free(dir);
if (self.gitCacheHit(dir, git_url, r, sha256_hex)) |src| return src;
if (self.offline) { self.setError("git package '{s}@{s}' is not cached and RINNE_OFFLINE=1 is set", .{ git_url, if (r.len == 0) "HEAD" else r }); return Error.OfflineMode; }
std.Io.Dir.cwd().createDirPath(self.io, dir) catch { self.setError("cannot create cache directory '{s}'", .{dir}); return Error.CacheUnavailable; }; const work = std.fmt.allocPrint(self.allocator, "{s}/clone", .{dir}) catch return Error.OutOfMemory; defer self.allocator.free(work); // A clean checkout dir: stale clones from earlier resolutions of the // same package must not shadow a fresh fetch. std.Io.Dir.cwd().deleteTree(self.io, work) catch {};
const is_commit = ref != null and ref.?.len == 40; var commit: [41]u8 = undefined; commit[40] = 0; if (is_commit) { _ = try self.gitRun(null, &.{ "clone", "--quiet", git_url, work }); // The pinned commit is usually reachable in a full clone. If not, // try fetching it directly (only hosts that permit unadvertised // sha1 fetches will serve it); otherwise we report the failure. if (!try self.gitRun(work, &.{ "checkout", "--quiet", ref.? })) { if (!try self.gitRun(work, &.{ "fetch", "--quiet", "--depth", "1", "origin", ref.? })) return Error.GitFailed; if (!try self.gitRun(work, &.{ "checkout", "--quiet", ref.? })) return Error.GitFailed; } @memcpy(commit[0..40], ref.?); } else { if (ref) |some| { if (!try self.gitRun(null, &.{ "clone", "--quiet", "--depth", "1", "--branch", some, git_url, work })) { return Error.GitFailed; } } else { _ = try self.gitRun(null, &.{ "clone", "--quiet", git_url, work }); } const res = std.process.run(self.allocator, self.io, .{ .argv = &.{ "git", "-C", work, "rev-parse", "HEAD" }, .stdout_limit = .limited(16 * 1024 * 1024), .stderr_limit = .limited(16 * 1024 * 1024), }) catch return Error.GitFailed; defer { self.allocator.free(res.stdout); self.allocator.free(res.stderr); } if (!(res.term == .exited and res.term.exited == 0)) { self.setError("git rev-parse failed in '{s}'", .{work}); return Error.GitFailed; } const trimmed = std.mem.trim(u8, res.stdout, " \n"); if (trimmed.len != 40) { self.setError("git rev-parse returned an unexpected value for '{s}'", .{git_url}); return Error.GitFailed; } @memcpy(commit[0..40], trimmed); }
// Discover the entry .rg (root main.rg preferred, else the sole .rg). const entry_rel = try self.gitEntryRel(work); defer self.allocator.free(entry_rel); const entry_abs = std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ work, entry_rel }) catch return Error.OutOfMemory; defer self.allocator.free(entry_abs); const src = std.Io.Dir.cwd().readFileAlloc(self.io, entry_abs, self.allocator, .limited(max_download_bytes)) catch { self.setError("cannot read git package entry '{s}'", .{entry_abs}); return Error.CacheUnavailable; }; if (sha256_hex) |want| { var digest_full: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(src, &digest_full, .{}); var got_hex: [64]u8 = undefined; const got_str = hexLower(&got_hex, &digest_full); if (!std.ascii.eqlIgnoreCase(want, got_str)) { self.allocator.free(src); self.setError("sha256 mismatch for git package '{s}@{s}': expected {s}, got {s}", .{ git_url, r, want, got_str }); return Error.ChecksumMismatch; } } self.writeGitManifest(dir, git_url, r, commit[0..40], entry_rel, src); return src; }
/// Recursively finds the entry .rg inside a git checkout (excluding the /// .git directory). Returns the checkout-relative path. fn gitEntryRel(self: *PackageManager, work: []const u8) Error![]u8 { var candidates: std.ArrayList([]u8) = .empty; defer { for (candidates.items) |c| self.allocator.free(c); candidates.deinit(self.allocator); } var base = std.Io.Dir.cwd().openDir(self.io, work, .{ .iterate = true }) catch { self.setError("cannot open git checkout '{s}'", .{work}); return Error.CacheUnavailable; }; defer base.close(self.io);
var w = std.Io.Dir.walkSelectively(base, self.allocator) catch { self.setError("cannot walk git checkout '{s}'", .{work}); return Error.CacheUnavailable; }; defer w.deinit(); while (true) { const maybe_entry = w.next(self.io) catch break; const e = maybe_entry orelse break; if (e.kind == .directory) { if (std.mem.eql(u8, e.basename, ".git")) continue; w.enter(self.io, e) catch continue; continue; } if (e.kind == .file and std.mem.endsWith(u8, e.basename, ".rg")) { const rel = self.allocator.dupe(u8, e.path) catch return Error.OutOfMemory; candidates.append(self.allocator, rel) catch { self.allocator.free(rel); return Error.OutOfMemory; }; } } if (candidates.items.len == 0) { self.setError("git package at '{s}' contains no .rg files", .{work}); return Error.InvalidPackage; } for (candidates.items) |c| { if (std.mem.eql(u8, c, "main.rg")) return try self.allocator.dupe(u8, c); } if (candidates.items.len == 1) return try self.allocator.dupe(u8, candidates.items[0]); std.mem.sort([]u8, candidates.items, {}, struct { fn lt(_: void, a: []u8, b: []u8) bool { return std.mem.lessThan(u8, a, b); } }.lt); var msg: std.ArrayList(u8) = .empty; defer msg.deinit(self.allocator); const upper = @min(candidates.items.len, 3); for (candidates.items[0..upper]) |c| { msg.appendSlice(self.allocator, c) catch break; msg.appendSlice(self.allocator, " ") catch break; } self.setError("ambiguous git package: multiple .rg roots ({s}); add a main.rg", .{msg.items}); return Error.InvalidPackage; }
fn writeGitManifest( self: *PackageManager, dir: []const u8, git_url: []const u8, ref: []const u8, commit: []const u8, entry_rel: []const u8, src: []const u8, ) void { var sha_full: [64 + 1]u8 = undefined; var digest: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(src, &digest, .{}); const sha_str = hexLower(&sha_full, &digest); const mf_path = std.fmt.allocPrint(self.allocator, "{s}/manifest.json", .{dir}) catch return; defer self.allocator.free(mf_path); const e_git = jsonEscape(self.allocator, git_url) catch return; defer self.allocator.free(e_git); const e_ref = jsonEscape(self.allocator, ref) catch return; defer self.allocator.free(e_ref); const e_commit = jsonEscape(self.allocator, commit) catch return; defer self.allocator.free(e_commit); const e_entry = jsonEscape(self.allocator, entry_rel) catch return; defer self.allocator.free(e_entry); const e_sha = jsonEscape(self.allocator, sha_str) catch return; defer self.allocator.free(e_sha); const body = std.fmt.allocPrint(self.allocator, "{{\"git\":\"{s}\",\"git_ref\":\"{s}\",\"commit\":\"{s}\",\"entry\":\"{s}\",\"sha256\":\"{s}\"}}\n", .{ e_git, e_ref, e_commit, e_entry, e_sha }) catch return; defer self.allocator.free(body); std.Io.Dir.cwd().writeFile(self.io, .{ .sub_path = mf_path, .data = body }) catch {};
// Materialize the entry .rg so cache hits have a stable source to read. if (std.fs.path.dirname(entry_rel)) |parent| { const parent_abs = std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ dir, parent }) catch return; defer self.allocator.free(parent_abs); std.Io.Dir.cwd().createDirPath(self.io, parent_abs) catch {}; } const entry_abs = std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ dir, entry_rel }) catch return; defer self.allocator.free(entry_abs); std.Io.Dir.cwd().writeFile(self.io, .{ .sub_path = entry_abs, .data = src }) catch {}; }
/// Reads `<cache>/<key>/manifest.json` and returns the absolute path of the /// materialized entry it records. Owned by the caller. fn mfEntryPath(self: *PackageManager, dir: []const u8) Error![]u8 { const mf_path = std.fmt.allocPrint(self.allocator, "{s}/manifest.json", .{dir}) catch return Error.OutOfMemory; defer self.allocator.free(mf_path); const text = std.Io.Dir.cwd().readFileAlloc(self.io, mf_path, self.allocator, .limited(64 * 1024)) catch { self.setError("no cached entry manifest at '{s}'", .{mf_path}); return Error.CacheUnavailable; }; defer self.allocator.free(text); var parsed = std.json.parseFromSlice(std.json.Value, self.allocator, text, .{}) catch { self.setError("corrupt package manifest '{s}'", .{mf_path}); return Error.CacheUnavailable; }; defer parsed.deinit(); const obj = switch (parsed.value) { .object => |o| o, else => { self.setError("corrupt package manifest '{s}'", .{mf_path}); return Error.CacheUnavailable; }, }; const m_entry = obj.get("entry") orelse { self.setError("package manifest '{s}' has no entry", .{mf_path}); return Error.CacheUnavailable; }; if (m_entry != .string) { self.setError("package manifest '{s}' has a non-string entry", .{mf_path}); return Error.CacheUnavailable; } // Git manifests record a checkout-relative entry ("main.rg"); URL fetch // manifests record an absolute path. Resolve it either way. if (m_entry.string.len != 0 and m_entry.string[0] == '/') return self.allocator.dupe(u8, m_entry.string) catch Error.OutOfMemory; return std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ dir, m_entry.string }) catch Error.OutOfMemory; }
/// Absolute path of the entry a successful `resolve(url, version)` wrote to /// the cache (used to turn a fetched package back into a `file://` source). pub fn materializedPath(self: *PackageManager, url: []const u8, version: ?[]const u8) Error![]u8 { const digest = try self.keyFor(url, version); var key_hex: [32]u8 = undefined; const key_str = hexLower(&key_hex, digest[0..16]); const dir = std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ self.cache_dir, key_str }) catch return Error.OutOfMemory; defer self.allocator.free(dir); return self.mfEntryPath(dir); }
/// Cache directory for a (git_url, ref) package: <cache>/<key>. fn gitKeyDir(self: *PackageManager, git_url: []const u8, ref: []const u8) Error![]u8 { const digest = try self.gitKeyFor(git_url, ref); var key_hex: [32]u8 = undefined; const key_str = hexLower(&key_hex, digest[0..16]); return std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ self.cache_dir, key_str }) catch return Error.OutOfMemory; }
/// Absolute path of a git package's cached entry .rg (after resolveGit). pub fn gitMaterializedPath(self: *PackageManager, git_url: []const u8, ref: ?[]const u8) Error![]u8 { const r = ref orelse ""; const dir = try self.gitKeyDir(git_url, r); defer self.allocator.free(dir); return self.mfEntryPath(dir); }
/// Resolved commit sha recorded for a cached git package (after resolveGit). /// Owned by the caller. pub fn gitCommitOf(self: *PackageManager, git_url: []const u8, ref: ?[]const u8) Error![]u8 { const r = ref orelse ""; const dir = try self.gitKeyDir(git_url, r); defer self.allocator.free(dir); const mf_path = std.fmt.allocPrint(self.allocator, "{s}/manifest.json", .{dir}) catch return Error.OutOfMemory; defer self.allocator.free(mf_path); const text = std.Io.Dir.cwd().readFileAlloc(self.io, mf_path, self.allocator, .limited(64 * 1024)) catch { self.setError("no cached git manifest at '{s}'", .{mf_path}); return Error.CacheUnavailable; }; defer self.allocator.free(text); var parsed = std.json.parseFromSlice(std.json.Value, self.allocator, text, .{}) catch { self.setError("corrupt git manifest '{s}'", .{mf_path}); return Error.CacheUnavailable; }; defer parsed.deinit(); const obj = switch (parsed.value) { .object => |o| o, else => { self.setError("corrupt git manifest '{s}'", .{mf_path}); return Error.CacheUnavailable; }, }; const m_commit = obj.get("commit") orelse { self.setError("git manifest '{s}' has no commit", .{mf_path}); return Error.CacheUnavailable; }; if (m_commit != .string) { self.setError("git manifest '{s}' has a non-string commit", .{mf_path}); return Error.CacheUnavailable; } return self.allocator.dupe(u8, m_commit.string) catch Error.OutOfMemory; }
/// Returns cached source when a manifest matches (url, version) and any /// caller-supplied checksum agrees with the recorded one; null = miss. fn cacheHit(self: *PackageManager, dir: []const u8, url: []const u8, version: ?[]const u8, sha256_hex: ?[]const u8) ?[]u8 { const mf_path = std.fmt.allocPrint(self.allocator, "{s}/manifest.json", .{dir}) catch return null; defer self.allocator.free(mf_path); const text = std.Io.Dir.cwd().readFileAlloc(self.io, mf_path, self.allocator, .limited(64 * 1024)) catch return null; defer self.allocator.free(text); var parsed = std.json.parseFromSlice(std.json.Value, self.allocator, text, .{}) catch return null; defer parsed.deinit(); const obj = switch (parsed.value) { .object => |o| o, else => return null, }; const m_url = obj.get("url") orelse return null; const m_ver = obj.get("version") orelse return null; const m_entry = obj.get("entry") orelse return null; if (m_url != .string or m_ver != .string or m_entry != .string) return null; if (!std.mem.eql(u8, m_url.string, url)) return null; if (!std.mem.eql(u8, m_ver.string, version orelse "")) return null; // A caller-supplied checksum must match what we recorded; otherwise // fall through to a fresh verified download. if (sha256_hex) |want| { const m_sha = obj.get("sha256"); const rec: []const u8 = if (m_sha != null and m_sha.? == .string) m_sha.?.string else ""; if (rec.len == 0 or !std.ascii.eqlIgnoreCase(want, rec)) return null; } return std.Io.Dir.cwd().readFileAlloc(self.io, m_entry.string, self.allocator, .limited(max_download_bytes)) catch null; }
fn writeManifest( self: *PackageManager, dir: []const u8, url: []const u8, version: ?[]const u8, final_url: []const u8, entry_abs: []const u8, sha256_hex: ?[]const u8, ) void { const mf_path = std.fmt.allocPrint(self.allocator, "{s}/manifest.json", .{dir}) catch return; defer self.allocator.free(mf_path); const e_url = jsonEscape(self.allocator, url) catch return; defer self.allocator.free(e_url); const e_ver = jsonEscape(self.allocator, version orelse "") catch return; defer self.allocator.free(e_ver); const e_final = jsonEscape(self.allocator, final_url) catch return; defer self.allocator.free(e_final); const e_entry = jsonEscape(self.allocator, entry_abs) catch return; defer self.allocator.free(e_entry); const e_sha = jsonEscape(self.allocator, sha256_hex orelse "") catch return; defer self.allocator.free(e_sha); const body = std.fmt.allocPrint(self.allocator, "{{\"url\":\"{s}\",\"version\":\"{s}\",\"final_url\":\"{s}\",\"entry\":\"{s}\",\"sha256\":\"{s}\"}}\n", .{ e_url, e_ver, e_final, e_entry, e_sha }) catch return; defer self.allocator.free(body); // Best-effort: a missing manifest only costs a re-download. std.Io.Dir.cwd().writeFile(self.io, .{ .sub_path = mf_path, .data = body }) catch {}; }
/// Chooses the entry .rg among extracted candidates. The returned slice /// points into the candidates list (dupe before freeing that list). pub fn pickEntry(self: *PackageManager, candidates: *std.ArrayList([]u8)) Error![]u8 { if (candidates.items.len == 0) { self.setError("package archive contains no .rg files", .{}); return Error.InvalidPackage; } if (candidates.items.len == 1) return candidates.items[0]; for (candidates.items) |c| { const base = std.fs.path.basename(c); if (std.mem.eql(u8, base, "main.rg")) return c; } std.mem.sort([]u8, candidates.items, {}, struct { fn lt(_: void, a: []u8, b: []u8) bool { return std.mem.lessThan(u8, a, b); } }.lt); var msg: std.ArrayList(u8) = .empty; const upper = @min(candidates.items.len, 3); for (candidates.items[0..upper]) |c| { msg.appendSlice(self.allocator, std.fs.path.basename(c)) catch break; msg.appendSlice(self.allocator, " ") catch break; } self.setError("ambiguous package: multiple .rg roots ({s}); import a direct .rg URL or name one 'main.rg'", .{msg.items}); msg.deinit(self.allocator); return Error.InvalidPackage; }
/// Gunzips gz_bytes and extracts safe members under dest_abs. /// Appends absolute paths of extracted *.rg files to rg_out. pub fn extractTarGz( self: *PackageManager, gz_bytes: []const u8, dest_abs: []const u8, rg_out: *std.ArrayList([]u8), ) Error!void { var in_fixed = std.Io.Reader.fixed(gz_bytes); const window = self.allocator.alloc(u8, 65536) catch return Error.OutOfMemory; defer self.allocator.free(window); var dec = std.compress.flate.Decompress.init(&in_fixed, .gzip, window); const raw = dec.reader.allocRemaining(self.allocator, .limited(max_extract_bytes)) catch |e| { if (e == error.StreamTooLong) { self.setError("package expands beyond the extraction limit", .{}); return Error.PackageTooLarge; } self.setError("package is not a valid gzip stream", .{}); return Error.BadArchive; }; defer self.allocator.free(raw); try self.extractTar(raw, dest_abs, rg_out); }
/// Extracts a plain tar stream (unit-testable without a compressor). pub fn extractTar( self: *PackageManager, tar_bytes: []const u8, dest_abs: []const u8, rg_out: *std.ArrayList([]u8), ) Error!void { var total: u64 = 0; var name_buf: [std.fs.max_path_bytes]u8 = undefined; var link_buf: [std.fs.max_path_bytes]u8 = undefined; var fixed = std.Io.Reader.fixed(tar_bytes); var it = std.tar.Iterator.init(&fixed, .{ .file_name_buffer = &name_buf, .link_name_buffer = &link_buf, }); while (it.next() catch { self.setError("package archive is corrupt", .{}); return Error.BadArchive; }) |tf| { switch (tf.kind) { .directory => { const d = safeJoin(self.allocator, dest_abs, tf.name) catch return Error.BadArchive; defer self.allocator.free(d); std.Io.Dir.cwd().createDirPath(self.io, d) catch return Error.CacheUnavailable; }, .file => { if (tf.size > max_extract_bytes - total) return Error.PackageTooLarge; total += tf.size; const data = self.allocator.alloc(u8, @intCast(tf.size)) catch return Error.OutOfMemory; defer self.allocator.free(data); // streamRemaining (NOT raw reads) keeps the iterator's // unread-byte bookkeeping consistent across entries. var data_writer: std.Io.Writer = .fixed(data); it.streamRemaining(tf, &data_writer) catch return Error.BadArchive; const out_path = safeJoin(self.allocator, dest_abs, tf.name) catch return Error.BadArchive; defer self.allocator.free(out_path); if (std.fs.path.dirname(out_path)) |parent| { std.Io.Dir.cwd().createDirPath(self.io, parent) catch return Error.CacheUnavailable; } std.Io.Dir.cwd().writeFile(self.io, .{ .sub_path = out_path, .data = data }) catch return Error.CacheUnavailable; if (std.mem.endsWith(u8, tf.name, ".rg")) { const dup = self.allocator.dupe(u8, out_path) catch return Error.OutOfMemory; rg_out.append(self.allocator, dup) catch { self.allocator.free(dup); return Error.OutOfMemory; }; } }, else => continue, // symlinks/devices skipped outright } } }};
/// Joins base and rel rejecting path traversal: absolute members, "..",/// "." components, and NUL bytes are all rejected.fn safeJoin(allocator: std.mem.Allocator, base: []const u8, rel: []const u8) ![]u8 { if (rel.len == 0 or rel[0] == '/') return error.BadPath; var out: std.ArrayList(u8) = .empty; errdefer out.deinit(allocator); try out.appendSlice(allocator, base); var it = std.mem.splitScalar(u8, rel, '/'); while (it.next()) |comp| { if (comp.len == 0) continue; if (std.mem.eql(u8, comp, "..") or std.mem.eql(u8, comp, ".")) return error.BadPath; if (std.mem.indexOfScalar(u8, comp, 0) != null) return error.BadPath; try out.append(allocator, '/'); try out.appendSlice(allocator, comp); } return out.toOwnedSlice(allocator);}
fn jsonEscape(allocator: std.mem.Allocator, s: []const u8) ![]u8 { var out: std.ArrayList(u8) = .empty; errdefer out.deinit(allocator); for (s) |c| { switch (c) { '"' => try out.appendSlice(allocator, "\\\""), '\\' => try out.appendSlice(allocator, "\\\\"), '\n' => try out.appendSlice(allocator, "\\n"), '\r' => try out.appendSlice(allocator, "\\r"), '\t' => try out.appendSlice(allocator, "\\t"), else => { if (c < 0x20) { var buf: [6]u8 = undefined; _ = std.fmt.bufPrint(&buf, "\\u{x:0>4}", .{c}) catch unreachable; try out.appendSlice(allocator, &buf); } else { try out.append(allocator, c); } }, } } return out.toOwnedSlice(allocator);}
fn hexLower(out: []u8, bytes: []const u8) []const u8 { const charset = std.fmt.hex_charset; for (bytes, 0..) |b, i| { out[i * 2] = charset[b >> 4]; out[i * 2 + 1] = charset[b & 15]; } return out[0 .. bytes.len * 2];}
/// Production transport: one-shot HTTP fetch into an allocating writer,/// then copied into the caller's buffer (whole-body buffering is a/// documented v1 limitation).pub fn defaultFetch(pm: *PackageManager, url: []const u8, out: *std.ArrayList(u8)) anyerror!void { var client: std.http.Client = .{ .allocator = pm.allocator, .io = pm.io }; defer client.deinit(); var aw = std.Io.Writer.Allocating.init(pm.allocator); defer aw.deinit(); const result = client.fetch(.{ .location = .{ .url = url }, .response_writer = &aw.writer }) catch return Error.DownloadFailed; if (result.status != .ok) return Error.DownloadFailed; if (aw.written().len > max_download_bytes) return Error.PackageTooLarge; try out.appendSlice(pm.allocator, aw.written());}
// ---------------------------------------------------------------------------// Tests// ---------------------------------------------------------------------------
const testing = std.testing;
test "classify: shorthand hosts require versions and rewrite to archives" { const a = testing.allocator; try testing.expectError(Error.MissingVersion, classify(a, "github.com/user/lib", null)); try testing.expectError(Error.MissingVersion, classify(a, "https://codeberg.org/user/lib", null)); { const t = try classify(a, "github.com/user/lib", "v1.2.0"); defer a.free(t.fetch_url); try testing.expect(t.kind == .archive); try testing.expectEqualStrings("https://codeload.github.com/user/lib/tar.gz/refs/tags/v1.2.0", t.fetch_url); } { const t = try classify(a, "https://github.com/user/lib.git/tree/main", "v2"); defer a.free(t.fetch_url); try testing.expectEqualStrings("https://codeload.github.com/user/lib/tar.gz/refs/tags/v2", t.fetch_url); } { const t = try classify(a, "https://gitlab.com/group/sub/repo", "v1"); defer a.free(t.fetch_url); try testing.expectEqualStrings("https://gitlab.com/group/sub/repo/-/archive/v1/repo-v1.tar.gz", t.fetch_url); } { const t = try classify(a, "codeberg.org/user/lib", "v0.1.0"); defer a.free(t.fetch_url); try testing.expectEqualStrings("https://codeberg.org/user/lib/archive/v0.1.0.tar.gz", t.fetch_url); } { const t = try classify(a, "https://example.com/libs/math.rg", null); defer a.free(t.fetch_url); try testing.expect(t.kind == .direct_rg); try testing.expectEqualStrings("https://example.com/libs/math.rg", t.fetch_url); } { const t = try classify(a, "http://example.com/bundle.tgz", null); defer a.free(t.fetch_url); try testing.expect(t.kind == .archive); } try testing.expectError(Error.UnsupportedUrl, classify(a, "ftp://example.com/x.rg", null)); try testing.expectError(Error.UnsupportedUrl, classify(a, "https://example.com/notarg.txt", null)); try testing.expectError(Error.UnsupportedUrl, classify(a, "local/path/file.rg", null)); try testing.expectError(Error.UnsupportedUrl, classify(a, "github.com", "v1"));}
/// Minimal ustar writer for fixtures: 512-byte header + data + padding.const TestTar = struct { list: std.ArrayList(u8) = .empty,
fn deinit(self: *TestTar, a: std.mem.Allocator) void { self.list.deinit(a); }
fn addFile(self: *TestTar, a: std.mem.Allocator, name: []const u8, data: []const u8) !void { var hdr = [_]u8{0} ** 512; @memcpy(hdr[0..@min(name.len, 100)], name); _ = std.fmt.bufPrint(hdr[100..108], "{o:0>7}\x00", .{0o644}) catch unreachable; _ = std.fmt.bufPrint(hdr[124..136], "{o:0>11}\x00", .{data.len}) catch unreachable; _ = std.fmt.bufPrint(hdr[136..148], "{o:0>11}\x00", .{0}) catch unreachable; hdr[156] = '0'; // regular file @memcpy(hdr[257..263], "ustar\x00"); @memcpy(hdr[263..265], "00"); var sum: u64 = 0; @memset(hdr[148..156], ' '); for (hdr) |c| sum += c; _ = std.fmt.bufPrint(hdr[148..156], "{o:0>6}\x00 ", .{sum}) catch unreachable; try self.list.appendSlice(a, &hdr); try self.list.appendSlice(a, data); const pad = (512 - @mod(data.len, 512)) % 512; try self.list.appendNTimes(a, 0, pad); }
fn addDir(self: *TestTar, a: std.mem.Allocator, name: []const u8) !void { var hdr = [_]u8{0} ** 512; @memcpy(hdr[0..@min(name.len, 100)], name); _ = std.fmt.bufPrint(hdr[100..108], "{o:0>7}\x00", .{0o755}) catch unreachable; _ = std.fmt.bufPrint(hdr[124..136], "{o:0>11}\x00", .{0}) catch unreachable; _ = std.fmt.bufPrint(hdr[136..148], "{o:0>11}\x00", .{0}) catch unreachable; hdr[156] = '5'; // directory @memcpy(hdr[257..263], "ustar\x00"); @memcpy(hdr[263..265], "00"); var sum: u64 = 0; @memset(hdr[148..156], ' '); for (hdr) |c| sum += c; _ = std.fmt.bufPrint(hdr[148..156], "{o:0>6}\x00 ", .{sum}) catch unreachable; try self.list.appendSlice(a, &hdr); }
fn finish(self: *TestTar, a: std.mem.Allocator) !void { try self.list.appendNTimes(a, 0, 1024); // end-of-archive blocks }
fn bytes(self: *TestTar) []const u8 { return self.list.items; }};
const sample_pkg = "fn pkg_add(a: int, b: int) int {\n return a + b;\n}\n";
// Fake transport serving a direct .rg payload.var fake_calls: usize = 0;fn fakeDirectRg(pm: *PackageManager, url: []const u8, out: *std.ArrayList(u8)) anyerror!void { fake_calls += 1; try testing.expectEqualStrings("https://example.com/lib.rg", url); try out.appendSlice(pm.allocator, sample_pkg);}
fn testCacheRoot(buf: []u8) ![]u8 { const ts = std.Io.Timestamp.now(testing.io, .awake); return std.fmt.bufPrint(buf, "/tmp/rinne/rg/pkgtest-{d}", .{ts.nanoseconds});}
test "resolve: direct .rg downloads, verifies sha256, and caches" { var sha: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(sample_pkg, &sha, .{}); var hex_buf: [64]u8 = undefined; const sha_hex = hexLower(&hex_buf, &sha);
var root_buf: [128]u8 = undefined; const root = try testCacheRoot(&root_buf); var pm = PackageManager{ .allocator = testing.allocator, .io = testing.io, .cache_dir = try testing.allocator.dupe(u8, root), .fetch_impl = fakeDirectRg, }; defer pm.deinit();
const src1 = try pm.resolve("https://example.com/lib.rg", null, sha_hex); defer testing.allocator.free(src1); try testing.expectEqualStrings(sample_pkg, src1); try testing.expectEqual(@as(usize, 1), fake_calls);
// Second resolve is a pure cache hit. const src2 = try pm.resolve("https://example.com/lib.rg", null, sha_hex); defer testing.allocator.free(src2); try testing.expectEqualStrings(sample_pkg, src2); try testing.expectEqual(@as(usize, 1), fake_calls);
// Wrong sha fails; the cache is distrusted (recorded sha differs), so // this path re-downloads before failing verification. try testing.expectError(Error.ChecksumMismatch, pm.resolve("https://example.com/lib.rg", null, "deadbeef")); try testing.expect(std.mem.indexOf(u8, pm.last_error.?, "mismatch") != null); try testing.expectEqual(@as(usize, 2), fake_calls);
// A distinct version string forms a distinct cache key (re-fetch). const src3 = try pm.resolve("https://example.com/lib.rg", "v9", null); defer testing.allocator.free(src3); try testing.expectEqual(@as(usize, 3), fake_calls);}
test "resolve: archive extraction discovers main.rg and rejects traversal" { var root_buf: [128]u8 = undefined; const root = try testCacheRoot(&root_buf); var pm = PackageManager{ .allocator = testing.allocator, .io = testing.io, .cache_dir = try testing.allocator.dupe(u8, root), .fetch_impl = fakeDirectRg, // unused here }; defer pm.deinit();
var tt: TestTar = .{}; defer tt.deinit(testing.allocator); try tt.addDir(testing.allocator, "lib-1/"); try tt.addFile(testing.allocator, "lib-1/main.rg", sample_pkg); try tt.addFile(testing.allocator, "lib-1/util.rg", "// util\n"); try tt.finish(testing.allocator);
const src_root = try std.fmt.allocPrint(testing.allocator, "{s}/manual/src", .{root}); defer testing.allocator.free(src_root); var candidates: std.ArrayList([]u8) = .empty; defer { for (candidates.items) |c| testing.allocator.free(c); candidates.deinit(testing.allocator); } try pm.extractTar(tt.bytes(), src_root, &candidates); try testing.expectEqual(@as(usize, 2), candidates.items.len);
// main.rg preferred over util.rg. const picked = try pm.pickEntry(&candidates); try testing.expect(std.mem.indexOf(u8, picked, "main.rg") != null);
// Traversal members are rejected outright. var evil: TestTar = .{}; defer evil.deinit(testing.allocator); try evil.addFile(testing.allocator, "../evil.rg", "boom\n"); try evil.finish(testing.allocator); var evil_cands: std.ArrayList([]u8) = .empty; defer { for (evil_cands.items) |c| testing.allocator.free(c); evil_cands.deinit(testing.allocator); } const evil_root = try std.fmt.allocPrint(testing.allocator, "{s}/evil", .{root}); defer testing.allocator.free(evil_root); try testing.expectError(Error.BadArchive, pm.extractTar(evil.bytes(), evil_root, &evil_cands));}
test "resolve: offline mode refuses uncached packages" { var root_buf: [128]u8 = undefined; const root = try testCacheRoot(&root_buf); var pm = PackageManager{ .allocator = testing.allocator, .io = testing.io, .cache_dir = try testing.allocator.dupe(u8, root), .fetch_impl = fakeDirectRg, }; defer pm.deinit(); pm.offline = true; try testing.expectError(Error.OfflineMode, pm.resolve("https://example.com/lib.rg", null, null));}
test "classify: file:// URLs map to local paths by extension" { const a = testing.allocator; { const t = try classify(a, "file:///tmp/x/lib.rg", null); defer a.free(t.fetch_url); try testing.expect(t.kind == .direct_rg); try testing.expectEqualStrings("file:///tmp/x/lib.rg", t.fetch_url); } { const t = try classify(a, "file:///tmp/x/lib.tgz", null); defer a.free(t.fetch_url); try testing.expect(t.kind == .archive); } try testing.expectError(Error.UnsupportedUrl, classify(a, "file:///tmp/x/readme.txt", null)); try testing.expectEqualStrings("/tmp/x/lib.rg", filePathOf("file:///tmp/x/lib.rg")); try testing.expectEqualStrings("tmp/x/lib.rg", filePathOf("file://tmp/x/lib.rg"));}
test "resolve: file:// packages read from disk and cache" { var root_buf: [128]u8 = undefined; const root = try testCacheRoot(&root_buf); // Seed a local "remote". const remote_dir = try std.fmt.allocPrint(testing.allocator, "{s}/remote", .{root}); defer testing.allocator.free(remote_dir); std.Io.Dir.cwd().createDirPath(testing.io, remote_dir) catch unreachable; const remote_rg = try std.fmt.allocPrint(testing.allocator, "{s}/lib.rg", .{remote_dir}); defer testing.allocator.free(remote_rg); std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = remote_rg, .data = sample_pkg }) catch unreachable; const file_url = try std.fmt.allocPrint(testing.allocator, "file://{s}", .{remote_rg}); defer testing.allocator.free(file_url);
var pm = PackageManager{ .allocator = testing.allocator, .io = testing.io, .cache_dir = try testing.allocator.dupe(u8, root), .fetch_impl = fakeDirectRg, // unused for file:// }; defer pm.deinit();
const src1 = try pm.resolve(file_url, null, null); defer testing.allocator.free(src1); try testing.expectEqualStrings(sample_pkg, src1); const src2 = try pm.resolve(file_url, null, null); defer testing.allocator.free(src2); try testing.expectEqualStrings(sample_pkg, src2);
// Offline still serves it from cache. pm.offline = true; { const src3 = try pm.resolve(file_url, null, null); defer testing.allocator.free(src3); try testing.expectEqualStrings(sample_pkg, src3); }}
fn gitRunTest(argv: []const []const u8) ![]const u8 { const res = try std.process.run(testing.allocator, testing.io, .{ .argv = argv, .stdout_limit = .limited(16 * 1024 * 1024), .stderr_limit = .limited(16 * 1024 * 1024), }); if (!(res.term == .exited and res.term.exited == 0)) { testing.allocator.free(res.stdout); return error.GitFailed; } return res.stdout;}
fn gitRunTestVoid(argv: []const []const u8) !void { const out = try gitRunTest(argv); testing.allocator.free(out);}
test "resolveGit: clones a local repo, records commit, serves from cache" { var root_buf: [128]u8 = undefined; const root = try testCacheRoot(&root_buf);
// Seed a local git "remote" with a packages/main.rg entry. const remote = try std.fmt.allocPrint(testing.allocator, "{s}/remote", .{root}); defer testing.allocator.free(remote); std.Io.Dir.cwd().createDirPath(testing.io, remote) catch unreachable; { const g = remote; try gitRunTestVoid(&.{ "git", "-C", g, "init", "--quiet", "-b", "main" }); try gitRunTestVoid(&.{ "git", "-C", g, "config", "user.name", "rinne-test" }); try gitRunTestVoid(&.{ "git", "-C", g, "config", "user.email", "tests@rinne" }); const pkg_rg = try std.fmt.allocPrint(testing.allocator, "{s}/main.rg", .{g}); defer testing.allocator.free(pkg_rg); std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = pkg_rg, .data = sample_pkg }) catch unreachable; try gitRunTestVoid(&.{ "git", "-C", g, "add", "main.rg" }); try gitRunTestVoid(&.{ "git", "-C", g, "commit", "--quiet", "-m", "pkg" }); } const git_url = try std.fmt.allocPrint(testing.allocator, "file://{s}", .{remote}); defer testing.allocator.free(git_url); const commit_raw = try gitRunTest(&.{ "git", "-C", remote, "rev-parse", "HEAD" }); const commit = try testing.allocator.dupe(u8, std.mem.trim(u8, commit_raw, " \n")); defer testing.allocator.free(commit); testing.allocator.free(commit_raw);
var pm = PackageManager{ .allocator = testing.allocator, .io = testing.io, .cache_dir = try testing.allocator.dupe(u8, root), .fetch_impl = fakeDirectRg, // unused }; defer pm.deinit();
// By branch name. { const src = try pm.resolveGit(git_url, "main", null); defer testing.allocator.free(src); try testing.expectEqualStrings(sample_pkg, src); } // By pinned commit (resolves to the same entry). { const src = try pm.resolveGit(git_url, commit, null); defer testing.allocator.free(src); try testing.expectEqualStrings(sample_pkg, src); } // Cache hit under offline mode. pm.offline = true; { const src = try pm.resolveGit(git_url, commit, null); defer testing.allocator.free(src); try testing.expectEqualStrings(sample_pkg, src); } // A package whose sha256 pin disagrees is rejected. pm.offline = false; try testing.expectError(Error.ChecksumMismatch, pm.resolveGit(git_url, commit, "deadbeef"));}