-- lib/gpkg_build.lua -- -- The package builder (BUILD_DESIGN.txt): turns a recipe from a NAME-src -- repository into a .gpkg, built in gsandbox, which gpkg_common then -- installs like any fetched package. Used only through gpkg_common. -- -- Nothing a build produces is trusted: the output is copied privately -- while hashed and its manifest (read by the library) must be exactly -- the one handed in. What IS trusted is what decides the build: the -- bundle (hash from the signed index, then checked by gl_recipe_read), -- each source (the recipe's SHA256), the layers (packages verified -- against their signed indexes) and this file and build-driver.sh. local ffi = require("ffi") local bit = require("bit") local M = {} -- Set by M.init(): FFI handles and gpkg_common's helpers. local log, istore, txn, h function M.init(env) log, istore, txn = env.log, env.istore, env.txn h = env end local SYSTEM_CONF = "/etc/glacier/glacier.conf" local SYSTEM_CONFIG_DIR = "/etc/glacier/config" -- --------------------------------------------------------------------- -- Small helpers -- --------------------------------------------------------------------- local function lines(s) local out = {} for l in (s or ""):gmatch("([^\n]*)\n") do table.insert(out, l) end return out end local function exists(path) local f = io.open(path, "rb") if f then f:close() return true end local ok = os.execute("test -e " .. h.shell_quote(path)) return ok == true or ok == 0 end -- The exit status of a shell command line. local function status_of(cmd) local p = io.popen(cmd .. "; echo \"rc=$?\"") local out = p:read("*a") p:close() return tonumber(out:match("rc=(%d+)%s*$")) or 255 end local function sh_ok(cmd) return status_of(cmd .. " >/dev/null 2>&1") == 0 end -- A libconfig string literal. local function cfg_string(s) return '"' .. s:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"' end -- --------------------------------------------------------------------- -- Settings (BUILD_DESIGN.txt section 5) -- --------------------------------------------------------------------- -- One configuration file's build settings, or nil if it doesn't exist. -- Exits if it is malformed (or, read by syspkg, untrusted). local conf_cache = {} local function read_conf(path, kind, opts) if conf_cache[path] ~= nil then return conf_cache[path] or nil end if not exists(path) then conf_cache[path] = false return nil end if opts.scope == ffi.C.GL_SCOPE_SYS then h.require_trusted(path) end local c = ffi.new("gl_build_conf_t") if tonumber(istore.gl_conf_build(path, kind, c)) ~= 0 then log.lg_printf(2, "Malformed %s.", path) h.finish(1) end local function str(v) return v ~= nil and ffi.string(v) or nil end local r = { traits = lines(str(c.traits)), cflags = str(c.cflags), cxxflags = str(c.cxxflags), ldflags = str(c.ldflags), configure = lines(str(c.configure)), jobs = c.jobs > 0 and tonumber(c.jobs) or nil, build_user = str(c.build_user), build_root = lines(str(c.build_root)), path = path, } istore.gl_build_conf_free(c) conf_cache[path] = r return r end -- The user's base directory (, beside index and store). local function user_base(opts) return string.format("/glacier/usr/%d", opts.uid) end -- The configuration layers for package `name`, lowest first: -- { global = {...}, package = {...} } lists of read_conf results. local function conf_layers(name, opts) local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts) if opts.scope == ffi.C.GL_SCOPE_SYS then return { sys }, read_conf(SYSTEM_CONFIG_DIR .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts) end local base = user_base(opts) .. "/config" return { sys, read_conf(base .. "/glacier.conf", ffi.C.GL_CONF_USER, opts) }, read_conf(base .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts) end -- The system's settings a build can't do without: BUILD_ROOT, and for -- syspkg BUILD_USER (its uid too). Exits saying which is missing. local function system_settings(opts) local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts) if not sys then log.lg_printf(2, "Cannot read %s.", SYSTEM_CONF) h.finish(1) end if #sys.build_root == 0 then log.lg_printf(2, "BUILD_ROOT is not set in %s.", SYSTEM_CONF) h.finish(1) end local s = { build_root = sys.build_root } if opts.scope == ffi.C.GL_SCOPE_SYS then if not sys.build_user then log.lg_printf(2, "BUILD_USER is not set in %s.", SYSTEM_CONF) h.finish(1) end local p = io.popen("id -u -- " .. h.shell_quote(sys.build_user) .. " 2>/dev/null") local uid = tonumber(p and p:read("*l") or "") if p then p:close() end if not uid then log.lg_printf(2, "BUILD_USER %s in %s is not a user.", sys.build_user, SYSTEM_CONF) h.finish(1) elseif uid == 0 then log.lg_printf(2, "BUILD_USER in %s must not be root.", SYSTEM_CONF) h.finish(1) end s.build_user, s.build_uid = sys.build_user, uid end return s end --- The effective traits of building `name`, whose recipe (or -src index --- entry) declares `declared` ("NAME D\n" lines). Returns a table --- { on = { trait = bool }, list = sorted { "ssl", "-ldap" }, show = --- "[+ssl -ldap]" (or "" with no traits) }. Exits if a per-package file --- names a trait the recipe doesn't declare. function M.traits(name, declared, opts) local on, names = {}, {} for _, l in ipairs(lines(declared)) do local t, d = l:match("^(%S+) ([01])$") on[t] = d == "1" table.insert(names, t) end local function apply(conf, strict) if not conf then return end for _, s in ipairs(conf.traits) do local neg = s:sub(1, 1) == "-" local t = neg and s:sub(2) or s if on[t] ~= nil then on[t] = not neg elseif strict then log.lg_printf(2, "%s: %s has no trait %s.", conf.path, name, t) h.finish(1) end end end local globals, pkg = conf_layers(name, opts) for _, g in ipairs(globals) do apply(g, false) end apply(pkg, true) table.sort(names) local list, show = {}, {} for _, t in ipairs(names) do table.insert(list, on[t] and t or ("-" .. t)) table.insert(show, (on[t] and "+" or "-") .. t) end return { on = on, list = list, show = #show > 0 and ("[" .. table.concat(show, " ") .. "]") or "" } end --- Relation lines ("NAME VER[ IF]\n") with their IF resolved against --- traits `tr`: "NAME VER\n" for each that applies. function M.effective(rel, tr) local out = {} for _, l in ipairs(lines(rel)) do local n, v, c = l:match("^(%S+) (%S+) ?(%S*)$") local applies = c == "" or (c:sub(1, 1) == "-" and not tr.on[c:sub(2)]) or (c:sub(1, 1) ~= "-" and tr.on[c]) if applies then table.insert(out, n .. " " .. v .. "\n") end end return table.concat(out) end --- Check, before the prompt, everything a build of `name` needs from --- the configuration (exits on a problem): BUILD_ROOT, BUILD_USER. function M.check(opts) if opts.sysroot then log.lg_printf(2, "Building from source under -R is not supported.") h.finish(1) end system_settings(opts) -- gsandbox and the driver decide what a build produces, which -- root then installs: checked like syspkg's own files, here -- rather than at startup, since only builds need them. if opts.scope == ffi.C.GL_SCOPE_SYS then h.require_trusted(h.gsandbox_path) h.require_trusted(h.lib_dir .. "/build-driver.sh") end end -- --------------------------------------------------------------------- -- The build -- --------------------------------------------------------------------- -- /build/NAME, made and checked by the library; owned by the -- builder (`owner` = BUILD_USER's uid, syspkg) or the scope. local function build_dir(ctx, name, owner) local out = ffi.new("char[4096]") if txn.gl_scope_build_dir(ctx, name, owner or -1, out) ~= 0 then h.finish(1) -- the library said why end return ffi.string(out) end -- Remove a build's directory tree (the builder's files may be -- read-only to it: overlayfs's work dir is mode 000). local function remove_tree(path) os.execute("chmod -R u+rwX " .. h.shell_quote(path) .. " 2>/dev/null; rm -rf " .. h.shell_quote(path)) end -- Copy a file (cp, then the given mode). local function copy_file(src, dst, mode) return sh_ok("cp -- " .. h.shell_quote(src) .. " " .. h.shell_quote(dst)) and sh_ok("chmod " .. mode .. " " .. h.shell_quote(dst)) end -- Fetch a source URL (https through curl, file:// a copy) into dest -- hashed: true if it arrived and has hash `hex`. Redirects are -- followed, https only (release tarballs are mostly served that way); -- the hash decides. local MAX_SOURCE = 4 * 1024 * 1024 * 1024 local function fetch_source(url, dest, hex) local tmp = dest .. ".part" os.remove(tmp) local cmd local path = url:match("^file://(/.*)$") if path then cmd = "cp -- " .. h.shell_quote(path) .. " " .. h.shell_quote(tmp) else cmd = "curl -sfL --max-redirs 5 --proto =https --proto-redir =https --connect-timeout 30" .. " --max-filesize " .. MAX_SOURCE .. " -o " .. h.shell_quote(tmp) .. " -- " .. h.shell_quote(url) end if not sh_ok(cmd) then os.remove(tmp) return false, false end if h.sha256_file(tmp) ~= hex then os.remove(tmp) return false, true end return os.rename(tmp, dest) ~= nil end -- The libconfig manifest a build must produce. local function manifest_text(r, deps, confs, tr) local function rel(text) local out = {} for n, v in text:gmatch("(%S+) (%S+)\n") do table.insert(out, string.format('{ PKG_NAME = "%s"; PKG_VER = "%s"; }', n, v)) end return "(" .. table.concat(out, ", ") .. ")" end local t = {} for _, s in ipairs(tr.list) do table.insert(t, '"' .. s .. '"') end return table.concat({ "PKG_NAME = " .. cfg_string(r.name) .. ";", "PKG_VER = " .. cfg_string(r.ver) .. ";", "PKG_DESC = " .. cfg_string(r.desc) .. ";", "PKG_MAINT = " .. cfg_string(r.maint) .. ";", "PKG_COPYRIGHT = " .. cfg_string(r.copyright) .. ";", "PKG_REPO = " .. cfg_string(r.repo) .. ";", "PKG_DEPS = " .. rel(deps) .. ";", "PKG_CONFS = " .. rel(confs) .. ";", "PKG_TRAITS = [ " .. table.concat(t, ", ") .. " ];", }, "\n") .. "\n" end -- The driver's env file: shell assignments, every value quoted. local function env_text(r, settings, tr, sources, prefix) local q = h.shell_quote local args = {} for _, a in ipairs(lines(r.configure)) do table.insert(args, q(a)) end for _, l in ipairs(lines(r.trait_args)) do local t, side, a = l:match("^([^\t]+)\t([+-])\t(.*)$") if (side == "+") == (tr.on[t] == true) then table.insert(args, q(a)) end end for _, a in ipairs(settings.configure) do table.insert(args, q(a)) end local on = {} for _, s in ipairs(tr.list) do if s:sub(1, 1) ~= "-" then table.insert(on, s) end end local function kv(k, v) return k .. "=" .. q(v or "") .. "\n" end return kv("NAME", r.name) .. kv("VER", r.ver) .. kv("BUILD", r.build) .. kv("PREFIX", prefix) .. kv("SOURCES", table.concat(sources, " ")) .. kv("PATCHES", table.concat(lines(r.patches), " ")) .. kv("CONFIGURE_ARGS", table.concat(args, " ")) .. kv("TRAITS", table.concat(on, " ")) .. kv("JOBS", tostring(settings.jobs)) .. kv("CFLAGS", settings.cflags) .. kv("CXXFLAGS", settings.cxxflags) .. kv("LDFLAGS", settings.ldflags) end -- CFLAGS/CXXFLAGS/LDFLAGS/CONFIGURE/JOBS for package `name`: the -- per-package value replaces the global one; the user's global replaces -- the system's. local function flags_for(name, opts) local globals, pkg = conf_layers(name, opts) local s = { configure = pkg and pkg.configure or {}, jobs = 1 } for _, k in ipairs({ "cflags", "cxxflags", "ldflags" }) do for _, g in ipairs(globals) do if g and g[k] then s[k] = g[k] end end if pkg and pkg[k] then s[k] = pkg[k] end end for _, g in ipairs(globals) do if g and g.jobs then s.jobs = g.jobs end end return s end -- Whether the private copy `path`'s manifest (read by the library) is -- exactly what the build was handed. local function output_matches(path, r, deps, confs, tr) local m = ffi.new("gl_pkg_t") if istore.gl_read_pkg_manifest(path, m) ~= 0 then return false end local want_traits = #tr.list > 0 and (table.concat(tr.list, "\n") .. "\n") or "" local ok = ffi.string(m.pkg_name) == r.name and ffi.string(m.pkg_repo) == r.repo and string.format("%d.%d.%d", m.pkg_ver.maj, m.pkg_ver.min, m.pkg_ver.pat) == r.ver and h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") == deps and h.relation_lines(m.pkg_conflicts, "conf_name", "conf_ver") == confs and m.pkg_traits ~= nil and ffi.string(m.pkg_traits) == want_traits istore.gl_free_pkg(m) return ok end -- The recipe of a verified bundle copy, as a table, or nil and why. local function read_recipe(path) local r = ffi.new("gl_recipe_t") if tonumber(istore.gl_recipe_read(path, r)) ~= 0 then return nil, ffi.string(istore.gl_recipe_error()) end local t = {} for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright", "sources", "patches", "build", "configure", "traits", "trait_args", "deps", "build_deps", "confs" }) do t[k] = ffi.string(r[k]) end istore.gl_recipe_free(r) return t end -- The packages a build stacks as layers: BUILD_ROOT, the effective -- BUILD_DEPS and DEPS, and everything they depend on, each from -- `built` (this run's own builds), `kept` (installed source builds, -- M.kept) or the binary repositories, in that order, at a high enough -- version. A list of { name, path, sha (hex) }, top first. Exits if -- one can't be had. local function layer_plan(id, root_names, build_deps, deps, indexes, built, kept) local plan, seen = {}, {} local queue = {} for _, rel in ipairs({ deps, build_deps }) do for n, v in rel:gmatch("(%S+) (%S+)\n") do table.insert(queue, { n, v }) end end for _, n in ipairs(root_names) do table.insert(queue, { n, "0.0.0" }) end local k = 1 while k <= #queue do local n, v = queue[k][1], queue[k][2] k = k + 1 if not seen[n] then seen[n] = true local b = built[n] if not (b and h.vercmp(b.ver, v) >= 0) then b = kept[n] end if b and b.path and not b.sha then b.sha = h.sha256_file(b.path) or false end if b and b.sha ~= false and h.vercmp(b.ver, v) >= 0 then table.insert(plan, { name = n, ver = b.ver, path = b.path, sha = b.sha }) for dn, dv in b.deps:gmatch("(%S+) (%S+)\n") do table.insert(queue, { dn, dv }) end else -- indexes.layers: base too, even for gpkg local r, e = h.pick_binary(indexes.layers or indexes, n) if not e or h.vercmp(e.ver, v) < 0 then log.lg_printf(2, "%s needs %s %s or later to build, and no binary repository has it.", id, n, v) h.finish(1) end table.insert(plan, { name = n, ver = e.ver, repo = r, entry = e, sha = e.sha256:match("^sha256:(%x+)$") }) for dn, dv in e.deps:gmatch("(%S+) (%S+)\n") do table.insert(queue, { dn, dv }) end end end end return plan end --- Check, before the prompt, that every layer a build of plan item `it` --- needs can be had (exits otherwise). `planned` maps the names this --- run builds to { ver, deps }. function M.check_layers(it, indexes, planned, opts) local settings = system_settings(opts) local id = string.format("%s::%s %s", it.repo.name, it.entry.name, it.entry.ver) layer_plan(id, settings.build_root, M.effective(it.entry.build_deps, it.traits), it.deps, indexes, planned, M.kept(opts)) end --- Build plan item `it` (a -src repository's entry, with it.traits from --- M.traits) into a verified private copy of its .gpkg. `built` maps --- names this run has already built to { ver, path, sha }, for layers. --- Returns the copy and its sidecar text; exits on any failure. function M.build(it, indexes, built, opts) local e, r_src = it.entry, it.repo local id = string.format("%s::%s %s", r_src.name, e.name, e.ver) local settings = system_settings(opts) local owner = settings.build_uid local ctx = h.live_context(opts) local file_mode = opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600" -- The bundle: downloaded and hashed against the signed index, -- copied privately, then read and checked by the library. local cached, wrong = h.download(ctx, r_src, e, tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8)) local copy = cached and h.verify_and_copy(cached, e.sha256 .. "\n") local r, bad = nil, nil if copy then r, bad = read_recipe(copy) end if not cached and not wrong then log.lg_printf(2, "Cannot fetch %s.", id) h.finish(1) end if bad then log.lg_printf(2, "%s has a malformed recipe bundle: %s.", id, bad) if cached then os.remove(cached) end h.finish(1) end if not r or r.name ~= e.name or r.ver ~= e.ver or r.repo .. "-src" ~= r_src.name or r.deps ~= e.deps or r.confs ~= e.confs or r.build_deps ~= e.build_deps or r.traits ~= e.traits then log.lg_printf(2, "%s does not match its index.", id) if cached then os.remove(cached) end h.finish(1) end if cached then os.remove(cached) end local tr = it.traits local deps, confs = M.effective(r.deps, tr), M.effective(r.confs, tr) local build_deps = M.effective(r.build_deps, tr) local flags = flags_for(r.name, opts) -- The prefix the package is built for: where its usr/ will be -- linked, so it finds its own files at run time. /usr for base, -- else the scope's links (no -R here: these are runtime paths). local prefix = r.repo == "base" and "/usr" or (ffi.string(ctx.links_path):gsub("/+$", "")) -- Sources, by hash, in /build/sources. local sources_dir = build_dir(ctx, "sources") local sources = {} for url, hex in r.sources:gmatch("(%S+) sha256:(%x+)\n") do local file = url:match("([^/]+)$") local dest = sources_dir .. "/" .. hex if h.sha256_file(dest) ~= hex then local ok, mismatch = fetch_source(url, dest, hex) if not ok then log.lg_printf(2, mismatch and "A source of %s does not match its hash." or "Cannot fetch the sources of %s.", id) h.finish(1) end sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(dest)) end table.insert(sources, { file = file, path = dest }) end -- This build's input (the scope's) and work/output (the builder's). local tag = string.format("%s-%s-%d", r.name, r.ver, ffi.C.gpkg_build_getpid()) local in_dir = build_dir(ctx, "in-" .. tag) local work = build_dir(ctx, "work-" .. tag, owner) local out = build_dir(ctx, "out-" .. tag, owner) local function cleanup() remove_tree(in_dir) remove_tree(work) remove_tree(out) end local function fail(fmt, ...) log.lg_printf(2, fmt, ...) cleanup() h.finish(1) end -- Layers, extracted once each into /build/layers by the -- builder: NAME-VER-. local layers_dir = build_dir(ctx, "layers", owner) local layer_names = {} local user_flag = settings.build_user and (" -u " .. h.shell_quote(settings.build_user)) or "" for _, l in ipairs(layer_plan(id, settings.build_root, build_deps, deps, indexes, built, M.kept(opts))) do local lname = string.format("%s-%s-%s", l.name, l.ver, l.sha:sub(1, 8)) if not exists(layers_dir .. "/" .. lname) then local path = l.path if l.entry then path = h.download(ctx, l.repo, l.entry, tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8)) if not path then fail("Cannot fetch %s::%s %s.", l.repo.name, l.name, l.ver) end else -- A package this run built: its private copy, -- copied where the builder can read it. local p = in_dir .. "/" .. lname .. ".gpkg" if not copy_file(l.path, p, "644") then fail("Cannot write %s.", p) end path = p end local tmp = string.format(".%s.%d", lname, ffi.C.gpkg_build_getpid()) if not sh_ok(h.gsandbox .. " -X" .. user_flag .. " -p " .. h.shell_quote(path) .. " -d " .. h.shell_quote(layers_dir .. "/" .. tmp)) then fail("Cannot extract %s %s for %s.", l.name, l.ver, id) end if not os.rename(layers_dir .. "/" .. tmp, layers_dir .. "/" .. lname) then remove_tree(layers_dir .. "/" .. tmp) end end table.insert(layer_names, lname) end -- The input: bundle, sources, manifest, env, driver. local function put(name, text) local f = io.open(in_dir .. "/" .. name, "wb") if not f or not f:write(text) then fail("Cannot write %s/%s.", in_dir, name) end f:close() if not sh_ok("chmod 644 " .. h.shell_quote(in_dir .. "/" .. name)) then fail("Cannot write %s/%s.", in_dir, name) end end if not copy_file(copy, in_dir .. "/bundle.grcp", "644") or not sh_ok("mkdir -m 755 " .. h.shell_quote(in_dir .. "/sources")) then fail("Cannot write %s.", in_dir) end local files = {} for _, s in ipairs(sources) do if not copy_file(s.path, in_dir .. "/sources/" .. s.file, "644") then fail("Cannot write %s.", in_dir) end table.insert(files, s.file) end put("manifest.gpm.cfg", manifest_text(r, deps, confs, tr)) put("env", env_text(r, flags, tr, files, prefix)) local df = io.open(h.lib_dir .. "/build-driver.sh", "rb") local driver = df and df:read("*a") if df then df:close() end if not driver then fail("Cannot read %s/build-driver.sh.", h.lib_dir) end put("driver", driver) -- The build. Its output goes to /build/logs/NAME-VER.log, -- and to the terminal unless -q. local log_path = build_dir(ctx, "logs") .. "/" .. r.name .. "-" .. r.ver .. ".log" local cmd = h.gsandbox .. " -R" .. user_flag .. " -L " .. h.shell_quote(layers_dir) for _, l in ipairs(layer_names) do cmd = cmd .. " -l " .. h.shell_quote(l) end cmd = cmd .. " -w " .. h.shell_quote(work) .. " -i " .. h.shell_quote(in_dir) .. " -o " .. h.shell_quote(out) log.lg_printf(0, "Building %s%s...", id, tr.show ~= "" and (" " .. tr.show) or "") local st if not h.quiet() then local stf = in_dir .. "/.status" os.execute("{ " .. cmd .. " 2>&1; echo $? > " .. h.shell_quote(stf) .. "; } | tee " .. h.shell_quote(log_path)) local f = io.open(stf, "r") st = f and tonumber(f:read("*l")) or 255 if f then f:close() end else st = status_of(cmd .. " > " .. h.shell_quote(log_path) .. " 2>&1") end sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(log_path)) if st ~= 0 then fail("Building %s failed. Log: %s", id, log_path) end -- The output, untrusted: copied privately while hashed, then its -- manifest must be exactly the one handed in. local result = out .. "/" .. r.name .. "-" .. r.ver .. ".gpkg" local hex = h.sha256_file(result) local pkg = hex and h.verify_and_copy(result, "sha256:" .. hex .. "\n") if not pkg or not output_matches(pkg, r, deps, confs, tr) then fail("%s: the build produced another package.", id) end cleanup() log.lg_printf(3, "Built %s.", id) return pkg, "sha256:" .. hex .. "\n", hex end -- --------------------------------------------------------------------- -- Kept packages: the verified .gpkg of each installed source build, -- the bytes that were installed, so a later build can use it as a -- layer (no binary repository has it). /build/packages/ -- REPO@NAME-VER.gpkg ('@' is in no name), kept while that version is -- installed and built from source. -- --------------------------------------------------------------------- local function kept_name(repo, name, ver) return repo .. "@" .. name .. "-" .. ver .. ".gpkg" end -- /build/packages, or nil if there is none (not made here). local function kept_dir(ctx) local d = ffi.string(ctx.store_path):gsub("/store$", "") .. "/build/packages" return exists(d) and d or nil end -- The kept packages of the installed source builds: the file names a -- prune leaves, and (for layer_plan) name -> { ver, path, deps }. local function kept_scan(opts) local ctx = h.live_context(opts) local dir = kept_dir(ctx) if not dir then return nil, {}, {} end local want, by_name = {}, {} for _, i in ipairs(h.installed_entries(opts)) do if h.built_from(ctx, i.repo, i.name) then local f = kept_name(i.repo, i.name, i.ver) local path = dir .. "/" .. f want[f] = true local m = ffi.new("gl_pkg_t") if exists(path) and istore.gl_read_pkg_manifest(path, m) == 0 then by_name[i.name] = { ver = i.ver, path = path, deps = h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") } istore.gl_free_pkg(m) end end end return dir, want, by_name end --- The installed source builds a build can use as layers: name -> --- { ver, path, deps }. function M.kept(opts) local _, _, by_name = kept_scan(opts) return by_name end --- After a commit: keep the private copy `path` of a package this run --- built and installed (repo::name ver). A failure only warns: the --- package is installed, and only later builds would miss it. function M.keep(opts, path, repo, name, ver) local ctx = h.live_context(opts) local out = ffi.new("char[4096]") local ok = txn.gl_scope_build_dir(ctx, "packages", -1, out) == 0 local dest = ok and (ffi.string(out) .. "/" .. kept_name(repo, name, ver)) if not ok or not copy_file(path, dest .. ".part", opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600") or not os.rename(dest .. ".part", dest) then if dest then os.remove(dest .. ".part") end log.lg_printf(1, "Could not keep %s::%s %s for later builds.", repo, name, ver) end end --- After a commit: delete the kept packages that are no longer an --- installed source build (removed, replaced, or reinstalled from a --- binary repository). function M.prune_kept(opts) local dir, want = kept_scan(opts) if not dir then return end local p = io.popen("ls -A -- " .. h.shell_quote(dir)) for f in p:lines() do if not want[f] then os.remove(dir .. "/" .. f) end end p:close() end ffi.cdef[[ int gpkg_build_getpid(void) asm("getpid"); ]] return M