From 42158472a5e6854888a16aa2e0216040ec2eae36 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Sat, 13 Apr 2024 22:51:39 +0200 Subject: [PATCH] dpkg-realpath: Rewrite in C This should make the code more robust against system issues on missing shell interpreters or realpath and readlink commands. --- po/POTFILES.in | 2 + src/Makefile.am | 7 +- src/dpkg-realpath.sh | 179 ------------------------------------- src/realpath/main.c | 242 +++++++++++++++++++++++++++++++++++++++++++++++++++ t/shellcheck.t | 1 - 5 files changed, 249 insertions(+), 182 deletions(-) delete mode 100755 src/dpkg-realpath.sh create mode 100644 src/realpath/main.c diff --git a/po/POTFILES.in b/po/POTFILES.in index a2386011a..34dea53f4 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -76,6 +76,8 @@ lib/dpkg/version.c src/common/force.c src/common/selinux.c +src/realpath/main.c + src/deb/build.c src/deb/extract.c src/deb/info.c diff --git a/src/Makefile.am b/src/Makefile.am index 6d0f2f24a..abb1d96cf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,6 +27,7 @@ bin_PROGRAMS = \ dpkg-deb \ dpkg-divert \ dpkg-query \ + dpkg-realpath \ dpkg-split \ dpkg-statoverride \ dpkg-trigger \ @@ -34,7 +35,6 @@ bin_PROGRAMS = \ bin_SCRIPTS = \ dpkg-maintscript-helper \ - dpkg-realpath \ # EOL pkglibexec_SCRIPTS = \ @@ -46,7 +46,6 @@ EXTRA_DIST += \ dpkg-db-backup.sh \ dpkg-db-keeper.sh \ dpkg-maintscript-helper.sh \ - dpkg-realpath.sh \ # EOL CLEANFILES += \ @@ -107,6 +106,10 @@ dpkg_query_SOURCES = \ query/main.c \ # EOL +dpkg_realpath_SOURCES = \ + realpath/main.c \ + # EOL + dpkg_split_SOURCES = \ split/dpkg-split.h \ split/info.c \ diff --git a/src/dpkg-realpath.sh b/src/dpkg-realpath.sh deleted file mode 100755 index 84438b49e..000000000 --- a/src/dpkg-realpath.sh +++ /dev/null @@ -1,179 +0,0 @@ -#!/bin/sh -# -# Copyright © 2020 Helmut Grohne -# Copyright © 2020 Guillem Jover -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -set -e - -PROGNAME=$(basename "$0") -version="unknown" -EOL="\n" - -PKGDATADIR_DEFAULT=src -PKGDATADIR="${DPKG_DATADIR:-$PKGDATADIR_DEFAULT}" - -# shellcheck source=src/sh/dpkg-error.sh -. "$PKGDATADIR/sh/dpkg-error.sh" - -show_version() -{ - cat <...] - -Options: - -z, --zero end output line with NUL, not newline. - --instdir set the root directory. - --root set the root directory. - --version show the version. - -?, --help show this help message. -END -} - -canonicalize() { - local src="$1" - local root="$DPKG_ROOT" - local loop=0 - local result="$root" - local dst - - # Check whether the path is relative and make it absolute otherwise. - if [ "$src" = "${src#/}" ]; then - src="$(pwd)/$src" - src="${src#"$root"}" - fi - - # Remove prefixed slashes. - while [ "$src" != "${src#/}" ]; do - src=${src#/} - done - while [ -n "$src" ]; do - # Get the first directory component. - prefix=${src%%/*} - # Remove the first directory component from src. - src=${src#"$prefix"} - # Remove prefixed slashes. - while [ "$src" != "${src#/}" ]; do - src=${src#/} - done - # Resolve the first directory component. - if [ "$prefix" = . ]; then - # Ignore, stay at the same directory. - : - elif [ "$prefix" = .. ]; then - # Go up one directory. - result=${result%/*} - if [ -n "$root" ] && [ "${result#"$root"}" = "$result" ]; then - result="$root" - fi - elif [ -h "$result/$prefix" ]; then - loop=$((loop + 1)) - if [ "$loop" -gt 25 ]; then - error "too many levels of symbolic links" - fi - # Resolve the symlink within $result. - dst=$(readlink "$result/$prefix") - case "$dst" in - /*) - # Absolute pathname, reset result back to $root. - result=$root - src="$dst${src:+/$src}" - # Remove prefixed slashes. - while [ "$src" != "${src#/}" ]; do - src=${src#/} - done - ;; - *) - # Relative pathname. - src="$dst${src:+/$src}" - ;; - esac - else - # Otherwise append the prefix. - result="$result/$prefix" - fi - done - # We are done, print the resolved pathname, w/o $root. - result="${result#"$root"}" - printf "%s$EOL" "${result:-/}" -} - -setup_colors - -DPKG_ROOT="${DPKG_ROOT:-}" -export DPKG_ROOT - -while [ $# -ne 0 ]; do - case "$1" in - -z|--zero) - EOL="\0" - ;; - --instdir|--root) - shift - DPKG_ROOT=$1 - ;; - --instdir=*) - DPKG_ROOT="${1#--instdir=}" - ;; - --root=*) - DPKG_ROOT="${1#--root=}" - ;; - --version) - show_version - exit 0 - ;; - --help|-\?) - show_usage - exit 0 - ;; - --) - shift - pathname="$1" - ;; - -*) - badusage "unknown option: $1" - ;; - *) - pathname="$1" - ;; - esac - shift -done - -# Normalize root directory. -DPKG_ROOT="${DPKG_ROOT:+$(realpath "$DPKG_ROOT")}" -# Remove default root dir. -if [ "$DPKG_ROOT" = "/" ]; then - DPKG_ROOT="" -fi - -[ -n "$pathname" ] || badusage "missing pathname" -if [ "${pathname#"$DPKG_ROOT"}" != "$pathname" ]; then - error "link '$pathname' includes root prefix '$DPKG_ROOT'" -fi - -canonicalize "$pathname" - -exit 0 diff --git a/src/realpath/main.c b/src/realpath/main.c new file mode 100644 index 000000000..387294a61 --- /dev/null +++ b/src/realpath/main.c @@ -0,0 +1,242 @@ +/* + * dpkg-realpath - print the resolved pathname with DPKG_ROOT support + * + * Copyright © 2020 Helmut Grohne + * Copyright © 2020-2024 Guillem Jover + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include +#include + +#include +#if HAVE_LOCALE_H +#include +#endif +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +static const char printforhelp[] = N_( +"Use --help for help about this utility."); + +static void DPKG_ATTR_NORET +printversion(const struct cmdinfo *cip, const char *value) +{ + printf(_("Debian %s version %s.\n"), dpkg_get_progname(), + PACKAGE_RELEASE); + + printf(_( +"This is free software; see the GNU General Public License version 2 or\n" +"later for copying conditions. There is NO warranty.\n")); + + m_output(stdout, _("")); + + exit(0); +} + +static void DPKG_ATTR_NORET +usage(const struct cmdinfo *cip, const char *value) +{ + printf(_( +"Usage: %s [