module main import crypto.sha256 import vmakepkg import os fn C.mkdtemp(template &char) &char // A package is pulled from an arbitrary repo — a directory or a git url — // under the config's pkgbuild build dir, reviewed whole, built with the // system's makepkg and installed. A bash PKGBUILD runs makepkg exactly as the // original spells it; a PKGBUILD.lip gets a generated PKGBUILD whose phases // run as blip through this very binary. struct ConfigBuild { pub mut: show_help bool @[long: help; short: h] noconfirm bool @[long: noconfirm] no_pkgbuild_confirm bool @[long: nopkgbuildconfirm] query []string @[tail] } // generated_review is the review of a generated package: the source (the // lip or the .vsh) and the PKGBUILD plus .install it produced, so the user // agrees with everything makepkg will execute fn generated_review(package string, source_name string, source_text string, pkg_text string, install_name string, install_text string) ReviewEvent { mut parts := [ReviewFile{ name: source_name text: source_text }, ReviewFile{ name: 'PKGBUILD' text: pkg_text generated: true }] if install_text != '' { parts << ReviewFile{ name: install_name text: install_text generated: true } } return ReviewEvent{ package: package files: parts } } // install_name_of reads the install file a generated PKGBUILD's head names, // spelled the way makepkg accepts one: '=' with spaces, quotes of either // kind; '' when it names none. it names a file beside the PKGBUILD: anything // that leaves the build dir already (an absolute path, a '..' climb) is not a // name this reader agrees to read. makepkg reads the line through bash, so a // name spelled with a variable or followed by a comment comes back as // written, naming no file here — review_the_rest covers that fn install_name_of(pkg_text string) !string { mut name := '' for line in pkg_text.split('\n') { s := line.trim_space() if !s.starts_with('install') { continue } rest := s[7..].trim_space() if !rest.starts_with('=') { continue } val := rest[1..].trim_space().trim('\'"') if val == '' { continue } if val.starts_with('/') || val in ['.', '..'] || val.starts_with('../') || val.contains('/../') || val.ends_with('/..') { return error('b: the install file name "${val}" is not a plain name beside the PKGBUILD') } name = val } return name } // review_the_rest completes a generated PKGBUILD's review the way read_review // shows a plain one's: every .install file of the build dir, not only the // one install_name_of could resolve, and the build dir's files. the install // scriptlet runs as root at install time, so one makepkg packs under a name // this reader could not resolve must not get there unseen; such a name is // said, beside the files that may be it fn review_the_rest(mut review ReviewEvent, work_dir string, install_name string, resolved bool) ! { mut names := os.ls(work_dir)! names.sort() on_view := review.files.map(it.name) for f in names { if f == '.git' { continue } review.listing << f if f.ends_with('.install') && f !in on_view && os.is_file('${work_dir}/${f}') { review.files << ReviewFile{ name: f text: os.read_file('${work_dir}/${f}')! } } } if install_name != '' && !resolved { review.warnings << 'the install file "${install_name}" could not be resolved: every .install file is shown' } } // pull_dir copies a local repo into its build dir as the building user: a // root-owned copy would stop makepkg from touching anything fn pull_dir(user string, src string, dst string) ! { run_as_user(user, ['mkdir', '-p', os.dir(dst)])! run_as_user(user, ['rm', '-rf', dst])! run_as_user(user, ['cp', '-R', src, dst])! } // write_as_user lays a generated file (a PKGBUILD or a .install out of a // PKGBUILD.lip) into the work dir with the building user's ownership, the way // pull_dir gives the clone theirs: a file that belongs to the root process // would make makepkg's reads and the cleanups beside it trip. the text goes // into a read-only temp file of the root process, and install copies it over. // the temp file lives in a directory mkdtemp made for this one write: its // name cannot be guessed and nobody else may write in it, so no link can be // planted where the root process is about to write fn write_as_user(user string, path string, text string) ! { template := os.join_path(os.temp_dir(), 'vpac.XXXXXX').clone() if C.mkdtemp(&char(template.str)) == unsafe { nil } { return error('could not make a temp dir: ${os.posix_get_error_msg(C.errno)}') } defer { os.rmdir_all(template) or { went_on('remove the temp dir ${template}', err) } } // the building user reads the file through it, and writes nothing in it os.chmod(template, 0o755)! tmp := os.join_path(template, 'text') os.write_file(tmp, text)! os.chmod(tmp, 0o444)! run_as_user(user, ['install', '-m', '644', tmp, path])! } // repo_name is the name a repo goes by: the last part of its path or url, // without a .git suffix. an scp-style git address (user@host:path) counts // from after the colon. empty when the target names nothing, like / or . fn repo_name(source string) string { name := os.base(source.trim_right('/')).all_after_last(':').trim_string_right('.git') return if name in ['', '.', '..', '/'] { '' } else { name } } // is_scp_remote reports scp-style git remotes (user@host:path): a colon that // comes before the first slash, where the colon of a host:port transfer url // or of a plain path with a colon in it would not. git itself accepts both // spellings of that, so the clone does not need the target spelled further fn is_scp_remote(target string) bool { colon := target.index(':') or { return false } // no slash: the colon splits the host from the path slash := target.index('/') or { return true } return colon < slash } // is_git_url reports whether git clone can fetch the target: a transfer url, // a name ending in .git, or an scp-style remote fn is_git_url(target string) bool { return target.ends_with('.git') || target.contains('://') || is_scp_remote(target) } // repo_work_dir is where a b target is pulled and built, and the name the // repo goes by. the dir is the name and a hash of the source, so two repos // of one name (a fork, a mirror, two checkouts) and a pkgbuild package of // that name never share it, and the same repo always lands in the same dir. // a directory is keyed on its real path, so ./x and /abs/x are one repo fn repo_work_dir(user string, target string) !(string, string) { source := if os.is_dir(target) { os.real_path(target) } else { target.trim_right('/') } name := repo_name(source) if name == '' { return error('b: "${target}" names no repo to build') } return name, pkgbuild_build_dir(user, '${name}-${sha256.hexhash(source)[..8]}')! } // build_repo_target pulls one target, reviews it, builds and installs it. // a target is an existing directory (a checkout, taken as the repo stands) // or a git url to clone; bare names belong to the pkgbuild repos, which the // --pkgbuild installs already cover, so the error points there fn build_repo_target(config ConfigBuild, target string, mut ui TransUI) ! { user := invoker_user()! base, work_dir := repo_work_dir(user, target)! // with --nopkgbuildconfirm nothing is asked, and the review is still // shown: the record of what was built stays in the output ask := !config.no_pkgbuild_confirm ui.pause() run_as_user(user, ['mkdir', '-p', os.dir(work_dir)])! defer { ui.sync() } if os.is_dir(target) { pull_dir(user, target, work_dir)! } else if is_git_url(target) { // git is the one tool a clone cannot happen without: an absent one is // said plainly, below user's name, before anything pulls run_as_user(user, ['sh', '-c', 'git --version >/dev/null']) or { return error('git is needed to clone ${target} as user ${user}, but it was not found (or failed): ${err.msg()}') } run_as_user(user, ['rm', '-rf', work_dir])! run_as_user(user, ['git', 'clone', '--depth', '${pkgbuild_depth}', target, work_dir])! } else { return error('b: "${target}" is neither a directory nor a git url; pkgbuild repos are covered by vpac i --pkgbuild ${target}') } // makepkg builds either spelling; a bash PKGBUILD keeps the // plain makepkg flow, the lip one generates its PKGBUILD through this // binary's blip runner, and a PKGBUILD.vsh asks its own script to write // the PKGBUILD through v work_lip := '${work_dir}/PKGBUILD.lip' mut work_vsh := '${work_dir}/PKGBUILD.vsh' if os.exists(work_lip) { bb := vmakepkg.lip_make(work_lip, os.executable())! install_name := if bb.install != '' { '${bb.base}.install' } else { '' } review := generated_review(base, os.base(work_lip), os.read_file(work_lip)!, bb.text, install_name, bb.install) show_review(review, '') if ask && !ui.noyes('build and install ${base}?') { return } write_as_user(user, '${work_dir}/PKGBUILD', bb.text)! if bb.install != '' { write_as_user(user, '${work_dir}/${bb.base}.install', bb.install)! } } else if os.exists(work_vsh) { // v run compiles its temp to the script's own name inside the work // dir, right on top of the PKGBUILD the script writes, so the script // hides before anything runs hsh := '${work_dir}/.vsh' run_as_user(user, ['mv', work_vsh, hsh])! work_vsh = hsh // v lives where every tool of this project lives, and runuser's // sanitized path may not carry it, so the script runs by full path vbin := if os.getenv(env_v) != '' { os.getenv(env_v) } else { os.find_abs_path_of_executable('v') or { return error('the v compiler is not in the path; it is needed to run a PKGBUILD.vsh') } } // the temp compile goes to the side of the PKGBUILD the script writes. // the compiled runner is only for the generation: it is gone before // the review lists the dir and before makepkg could pack it, whether // the generation went through or not generated := run_as_user(user, ['sh', '-c', 'export VPAC_VSH_PATH=${vmakepkg.shell_quote(work_vsh)}; cd ${vmakepkg.shell_quote(work_dir)} && ${vmakepkg.shell_quote(vbin)} -o ${vmakepkg.shell_quote('${work_dir}/.vshgen')} run ${vmakepkg.shell_quote(work_vsh)} generate']) run_as_user(user, ['rm', '-f', '${work_dir}/.vshgen']) or { warn('could not remove the generator ${work_dir}/.vshgen: ${err.msg()}') } generated! if !os.exists('${work_dir}/PKGBUILD') { return error('${work_vsh} wrote no PKGBUILD') } pkg_text := os.read_file('${work_dir}/PKGBUILD')! install_name := install_name_of(pkg_text)! install_path := '${work_dir}/${install_name}' resolved := install_name != '' && os.is_file(install_path) install_text := if resolved { os.read_file(install_path)! } else { '' } mut review := generated_review(base, 'PKGBUILD.vsh', os.read_file(work_vsh)!, pkg_text, install_name, install_text) review_the_rest(mut review, work_dir, install_name, resolved)! show_review(review, '') if ask && !ui.noyes('build and install ${base}?') { return } } else if os.exists('${work_dir}/PKGBUILD') { show_review(read_review(base, work_dir)!, 'review ${base}') if ask && !ui.noyes('build and install ${base}?') { return } } else { return error('${work_dir} holds neither PKGBUILD, PKGBUILD.lip nor PKGBUILD.vsh') } build_phase(base, 'start') run_as_user(user, ['sh', '-c', makepkg_line(work_dir)])! build_phase(base, 'done') // everything makepkg produced goes into one install: a split package's // set lands with one transaction, one listing and one go-ahead, the way // the pkgbuild installs batch their own archives := built_packages(user, work_dir)! if archives.len == 0 { return error('b: makepkg built nothing for ${base} that its --packagelist names') } install(ConfigInstall{ query: archives noconfirm: config.noconfirm })! } // build_repo is the mode's entry: every query is one repo to build fn build_repo(config ConfigBuild) ! { need_query(config.query, 'repo (a directory or a git url)')! // --noconfirm would take the build question's preset, which is no, and // build nothing with a clean exit: the refusal says so first, as the // pkgbuild installs' does if config.noconfirm && !config.no_pkgbuild_confirm { return error('a build is asked about: drop --noconfirm, or pass --nopkgbuildconfirm, to build ${config.query.join(', ')}') } mut ui := new_trans_ui(noconfirm: config.noconfirm) defer { ui.stop() } for target in config.query { build_repo_target(config, target, mut ui)! } }