# HG changeset patch # User Franz Glasner # Date 1731109625 -3600 # Node ID 2310764e5c4eb7594e64e3f0fbbbb0188007db4a # Parent 70e08ff3db452cd9af64991e2f9687d72d5adaba fports: Implement a global "--jail/-j" option: execute the fports commands in a jail. The jail just needs to have an installed "pkg" command. diff -r 70e08ff3db45 -r 2310764e5c4e docs/man/man8/fports.rst --- a/docs/man/man8/fports.rst Fri Nov 08 12:57:59 2024 +0100 +++ b/docs/man/man8/fports.rst Sat Nov 09 00:47:05 2024 +0100 @@ -13,9 +13,9 @@ **fports** **-V**\|\ **--version** -**fports deptree** [**-l** `maxlevel`\|\ **--maxlevel**\=\ `maxlevel`] [**-r**\|\ **--reverse**] [**-t**\|\ **--list**\|\ **--transitive**] `package` ... +**fports** [**-j** `jail`\|\ **--jail**\=\ `jail`] **deptree** [**-l** `maxlevel`\|\ **--maxlevel**\=\ `maxlevel`] [**-r**\|\ **--reverse**] [**-t**\|\ **--list**\|\ **--transitive**] `package` ... -**fports detail** [**-A**] [**-b**\|\ **--nofreebsd**\|\ **--no-freebsd**] [**-f**\ |\ **--filter-unused**] [**-n**\|\ **--noauto**\|\ **--no-auto**] [**-m**\|\ **--mapped**] [`package` ...] +**fports** [**-j** `jail`\|\ **--jail**\=\ `jail`] **detail** [**-A**] [**-b**\|\ **--nofreebsd**\|\ **--no-freebsd**] [**-f**\ |\ **--filter-unused**] [**-n**\|\ **--noauto**\|\ **--no-auto**] [**-m**\|\ **--mapped**] [`package` ...] Description @@ -33,6 +33,18 @@ This tools is the successor of :manpage:`check-ports(8)`. +Global Options +-------------- + +These global options are implemented for all subcommands: + +.. option:: -j , --jail= + + The script will execute in the given jail `name or id`, where name + matches :command:`jls name` and id matches :command:`jls jid`. + See :manpage:`jail(8)` and :manpage:`jls(8)`. + + Subcommands ----------- @@ -47,7 +59,7 @@ Print the program name and version number to stdout and exit. -**fports deptree** [**-l** `maxlevel`\|\ **--maxlevel**\=\ `maxlevel`] [**-r**\|\ **--reverse**] [**-t**\|\ **--list**\|\ **--transitive**] `package` ... +**fports** [**-j** `jail`\|\ **--jail**\=\ `jail`] **deptree** [**-l** `maxlevel`\|\ **--maxlevel**\=\ `maxlevel`] [**-r**\|\ **--reverse**] [**-t**\|\ **--list**\|\ **--transitive**] `package` ... Print a dependency tree for every given `package`. A package tree is a hierarchical list of packages that `packages` depends on. @@ -71,7 +83,7 @@ that depend on a given `package`. -**fports detail** [**-A**] [**-b**\|\ **--nofreebsd**\|\ **--no-freebsd**] [**-f**\ |\ **--filter-unused**] [**-n**\|\ **--noauto**\|\ **--no-auto**] [**-m**\|\ **--mapped**] [`package` ...] +**fports** [**-j** `jail`\|\ **--jail**\=\ `jail`] **detail** [**-A**] [**-b**\|\ **--nofreebsd**\|\ **--no-freebsd**] [**-f**\ |\ **--filter-unused**] [**-n**\|\ **--noauto**\|\ **--no-auto**] [**-m**\|\ **--mapped**] [`package` ...] Print the status of all given or selected packages in the most detail possible. diff -r 70e08ff3db45 -r 2310764e5c4e sbin/fports --- a/sbin/fports Fri Nov 08 12:57:59 2024 +0100 +++ b/sbin/fports Sat Nov 09 00:47:05 2024 +0100 @@ -23,8 +23,8 @@ USAGE=' USAGE: fports -h|--help fports -V|--version - fports deptree [-l maxlevel|--maxlevel=maxlevel] [-r|--reverse] [-t|--list|--transitive] package... - fports detail [-A] [-b|--nofreebsd|--no-freebsd] [-f|--filter-unused] [-n|--noauto|--no-auto] [-m|--mapped] [package...] + fports [GLOBAL-OPTIONS] deptree [-l maxlevel|--maxlevel=maxlevel] [-r|--reverse] [-t|--list|--transitive] package... + fports [GLOBAL-OPTIONS] detail [-A] [-b|--nofreebsd|--no-freebsd] [-f|--filter-unused] [-n|--noauto|--no-auto] [-m|--mapped] [package...] GLOBAL OPTIONS: @@ -32,6 +32,9 @@ -h, --help Print this help message to stdout and exit. + -j JAIL, --jail=JAIL + Execute in given jail JAIL. + ' @@ -66,12 +69,18 @@ #: #: Implementation of the "deptree" command. #: +#: Args: +#: $1 (str, null): The jail to attach to +#: $2...: the command's options and arguments +#: command_deptree() { - local opt_reversed opt_maxlevel opt_flat + local opt_jail opt_reversed opt_maxlevel opt_flat # $@ local opt + opt_jail="${1}" + shift opt_maxlevel=0 opt_reversed=no opt_flat=no @@ -94,10 +103,14 @@ shift $((OPTIND-1)) OPTIND=1 + if [ -n "${opt_jail}" ]; then + can_call_command "${opt_jail}" || fatal "${EX_UNAVAILABLE}" "jail \`${opt_jail}' does not exist or attaching denied" + fi + if checkyesno opt_reversed; then - _command_deptree_reversed "${opt_maxlevel}" "${opt_flat}" "$@" + _command_deptree_reversed "${opt_jail}" "${opt_maxlevel}" "${opt_flat}" "$@" else - _command_deptree_normal "${opt_maxlevel}" "${opt_flat}" "$@" + _command_deptree_normal "${opt_jail}" "${opt_maxlevel}" "${opt_flat}" "$@" fi } @@ -106,13 +119,14 @@ #: Implementation of printing a "normal" dependency tree #: _command_deptree_normal() { - local maxlevel flat # $@ + local jail maxlevel flat # $@ local pkgdeps pkgqueue curdeps pkg n v flatdeps - maxlevel="${1}" - flat="${2}" - shift 2 + jail="${1}" + maxlevel="${2}" + flat="${3}" + shift 3 # shellcheck disable=SC2034 # pkgqueue seems unused pkgqueue='' @@ -120,7 +134,7 @@ # resolution for pkg in "$@"; do - if ! "${PKG}" query '%n' "${pkg}" 1>/dev/null 2>/dev/null ; then + if ! call_pkg "${opt_jail}" query '%n' "${pkg}" 1>/dev/null 2>/dev/null ; then farray_release pkgqueue fatal "${EX_DATAERR}" "Package not found: ${pkg}" fi @@ -137,7 +151,7 @@ farray_append curdeps "${n}=${v}" farray_append pkgqueue "${n}" done <' "${pkg}" "$(LC_ALL=C.UTF-8 "${PKG}" query '%v' "${pkg}")" "${flatdeps}" + _print_flatdeps '-->' "${pkg}" "$(call_pkg "${opt_jail}" query '%v' "${pkg}")" "${flatdeps}" falist_release "${flatdeps}" done else for pkg in "$@"; do - _print_dependency_tree 0 "${maxlevel}" '-->' "${pkg}" "$(LC_ALL=C.UTF-8 "${PKG}" query '%v' "${pkg}")" "${pkgdeps}" + _print_dependency_tree 0 "${maxlevel}" '-->' "${pkg}" "$(call_pkg "${opt_jail}" query '%v' "${pkg}")" "${pkgdeps}" done fi falist_release pkgdeps @@ -165,14 +179,15 @@ #: Implementation of printing a reversed dependency tree #: _command_deptree_reversed() { - local maxlevel flat # $@ + local jail maxlevel flat # $@ local pkgdeps pkgqueue curdeps pkg n v flatdeps - maxlevel="${1}" + jail="${1}" + maxlevel="${2}" # shellcheck disable=SC2034 # appears unused - flat="${2}" - shift 2 + flat="${3}" + shift 3 # shellcheck disable=SC2034 # pkgqueue seems unused pkgqueue='' @@ -180,7 +195,7 @@ # resolution for pkg in "$@"; do - if ! "${PKG}" query '%n' "${pkg}" 1>/dev/null 2>/dev/null ; then + if ! call_pkg "${opt_jail}" query '%n' "${pkg}" 1>/dev/null 2>/dev/null ; then farray_release pkgqueue fatal "${EX_DATAERR}" "Package not found: ${pkg}" fi @@ -197,7 +212,7 @@ farray_append curdeps "${n}=${v}" farray_append pkgqueue "${n}" done <" "${mapped_package}" if [ -n "${indexfile}" ]; then - pkgversion="$(parse_index_file_for_package_version "${indexfile}" "${mapped_package}")" - pkglabel="$(LC_ALL=C.UTF-8 "${PKG}" version --test-version "${instver}" "${pkgversion}")" + pkgversion="$(parse_index_file_for_package_version "${indexfile}" "${mapped_package}" "${opt_jail}")" + pkglabel="$(call_pkg "${opt_jail}" version --test-version "${instver}" "${pkgversion}")" print_detail_item "INDEX" "${pkgversion}" "${pkglabel}" '' 19 fi - farray_for_each repositories _mapped_package_repository_detail "${mapped_package}" "{instver}" "${opt_filterunused}" 19 + farray_for_each repositories _mapped_package_repository_detail "${mapped_package}" "{instver}" "${opt_filterunused}" "${opt_jail}" 19 fi } @@ -525,10 +553,12 @@ #: $3: The element value (i.e. repository name) #: $4 (str): The (master) package name #: $5 (flag): The global flat whether to filter repository printing -#: $6 (int, optional): The extra indent value to forward to called functions +#: $6 (str, null): The jail in which the package data live +#: $7 (int, optional): The extra indent value to forward to called functions #: _package_repository_detail() { - local repositories idx reponame package extraindent opt_filterunused + local repositories idx reponame package extraindent opt_filterunused \ + opt_jail local _dummy \ pkglabel pkgdescr pkgversion @@ -539,12 +569,13 @@ reponame="${3}" package="${4}" opt_filterunused="${5}" - extraindent="${6:-0}" - + opt_jail="${6}" + extraindent="${7:-0}" + read -r _dummy pkglabel pkgdescr </dev/null 2>/dev/null || return 1 + fi + return 0 +} + + +#: +#: A special form of `call_command` that eventually calls +#: :command:`pkg -j JAIL` directly. +#: +#: Args: +#: $1 (str, null): The jail in which to execute the command in $2... +#: If `$1` is null then the command is executed on the +#: current host. +#: $@: The args to :command:`pkg` +#: +#: Input (Globals): +#: - PKG: The path to the :command:`pkg` commant to call into. +#: +#: Returns: +#: int: Whatever the called command exists with +#: +call_pkg() { + local jail # $@ + + jail="${1}" + shift + if [ -z "${jail}" ]; then + LC_ALL=C.UTF-8 "${PKG}" "$@" + else + LC_ALL=C.UTF-8 "${PKG}" -j "${jail}" "$@" + fi +} + + +#: #: Determine some important dataset properties that are set locally #: #: Args: diff -r 70e08ff3db45 -r 2310764e5c4e share/local-bsdtools/ports.subr --- a/share/local-bsdtools/ports.subr Fri Nov 08 12:57:59 2024 +0100 +++ b/share/local-bsdtools/ports.subr Sat Nov 09 00:47:05 2024 +0100 @@ -68,6 +68,8 @@ #: #: Args: #: $1 (str): The variable where to create the alist +#: $2 (str, null): The jail from which to retrieve the package mapping +#: configuration #: #: Input (Globals): #: PACKAGE_MAPPING: See also :manpage:`package-mapping.conf(5)`. @@ -88,14 +90,15 @@ #: fmg-nextcloud-twofactor_totp-php71 nextcloud-twofactor_totp-php71 #: init_package_mapping() { - local _mapping + local _mapping _jail local _iname _mapped_package falist_create "$1" || return _mapping="$1" + _jail="$2" - if [ -r "${PACKAGE_MAPPING}" ] ; then + if call_command "${_jail}" /bin/test -r "${PACKAGE_MAPPING}" ; then while IFS=$' \t' read -r _iname _mapped_package ; do case "${_iname}" in '') @@ -111,7 +114,9 @@ fi ;; esac - done < "${PACKAGE_MAPPING}" + done <