[pacman-dev] [PATCH] makepkg: replace sed in-place with built-in substitution
Read PKGBUILD into an array and replace the pkgver and pkgrel with bash parameter substitution, then use shell redirection to write to to the file. Because shell redirection follows symlinks, this accomplishes the same thing as the previous default of using the GNU-specific --follow-symlinks sed flag. Remove SEDPATH and SEDINPLACEFLAGS from the build systems as they are not used elsewhere. --- build-aux/edit-script.sh.in | 2 -- configure.ac | 11 ----------- meson.build | 11 ----------- meson_options.txt | 3 --- scripts/Makefile.am | 2 -- scripts/makepkg.sh.in | 7 ++++--- 6 files changed, 4 insertions(+), 32 deletions(-) diff --git a/build-aux/edit-script.sh.in b/build-aux/edit-script.sh.in index 640d32f8..7423a223 100644 --- a/build-aux/edit-script.sh.in +++ b/build-aux/edit-script.sh.in @@ -20,8 +20,6 @@ mode=$3 -e "s|@DEBUGSUFFIX[@]|@DEBUGSUFFIX@|g" \ -e "s|@INODECMD[@]|@INODECMD@|g" \ -e "s|@FILECMD[@]|@FILECMD@|g" \ - -e "s|@SEDINPLACEFLAGS[@]|@SEDINPLACEFLAGS@|g" \ - -e "s|@SEDPATH[@]|@SEDPATH@|g" \ -e "s|@configure_input[@]|Generated from ${input##*/}; do not edit by hand.|g" \ "$input" >"$output" diff --git a/configure.ac b/configure.ac index 305432b3..e59f82e9 100644 --- a/configure.ac +++ b/configure.ac @@ -369,7 +369,6 @@ AC_CHECK_MEMBERS([struct statfs.f_flags],,,[[#include <sys/param.h> GCC_VISIBILITY_CC # Host-dependant definitions -DEFAULT_SEDINPLACEFLAGS=" --follow-symlinks -i" INODECMD="stat -c '%i %n'" STRIP_BINARIES="--strip-all" STRIP_SHARED="--strip-unneeded" @@ -377,30 +376,21 @@ STRIP_STATIC="--strip-debug" case "${host_os}" in *bsd*) INODECMD="stat -f '%i %N'" - DEFAULT_SEDINPLACEFLAGS=" -i \"\"" ;; darwin*) host_os_darwin=yes INODECMD="/usr/bin/stat -f '%i %N'" - DEFAULT_SEDINPLACEFLAGS=" -i ''" STRIP_BINARIES="" STRIP_SHARED="-S" STRIP_STATIC="-S" ;; esac AM_CONDITIONAL([DARWIN], test "x$host_os_darwin" = "xyes") -AC_PATH_PROGS([SEDPATH], [sed], [sed], [/usr/bin$PATH_SEPARATOR/bin] ) AC_SUBST(INODECMD) AC_SUBST(STRIP_BINARIES) AC_SUBST(STRIP_SHARED) AC_SUBST(STRIP_STATIC) -# Flags for sed in place -if test "${SEDINPLACEFLAGS+set}" != "set"; then - SEDINPLACEFLAGS="${DEFAULT_SEDINPLACEFLAGS}" -fi -AC_ARG_VAR(SEDINPLACEFLAGS, [flags for sed, overriding the default]) - # Variables plugged into makepkg.conf CARCH="${host%%-*}" CHOST="${host}" @@ -576,7 +566,6 @@ ${PACKAGE_NAME}: Architecture : ${CARCH} Host Type : ${CHOST} File inode command : ${INODECMD} - In-place sed command : ${SEDPATH} ${SEDINPLACEFLAGS} File seccomp command : ${FILECMD} libalpm version : ${LIB_VERSION} diff --git a/meson.build b/meson.build index 8c296cb8..36f87ed2 100644 --- a/meson.build +++ b/meson.build @@ -221,7 +221,6 @@ config_h = configure_file( add_project_arguments('-include', 'config.h', language : 'c') filecmd = 'file' -default_sedinplaceflags = ' --follow-symlinks -i' inodecmd = 'stat -c \'%i %n\'' strip_binaries = '--strip-all' strip_shared = '--strip-unneeded' @@ -237,18 +236,11 @@ endif os = host_machine.system() if os.startswith('darwin') inodecmd = '/usr/bin/stat -f \'%i %N\'' - default_sedinplaceflags = ' -i \'\'' strip_binaries = '' strip_shared = '-s' strip_static = '-s' elif os.contains('bsd') or os == 'dragonfly' inodecmd = 'stat -f \'%i %N\'' - default_sedinplaceflags = ' -i \'\'' -endif - -sedinplaceflags = get_option('sedinplaceflags') -if sedinplaceflags == 'auto' - sedinplaceflags = default_sedinplaceflags endif chost = run_command(cc, '-dumpmachine').stdout().strip() @@ -277,8 +269,6 @@ substs.set('TEMPLATE_DIR', get_option('makepkg-template-dir')) substs.set('DEBUGSUFFIX', get_option('debug-suffix')) substs.set('INODECMD', inodecmd) substs.set('FILECMD', filecmd) -substs.set('SEDINPLACEFLAGS', sedinplaceflags) -substs.set('SEDPATH', SED.path()) substs.set('LIBMAKEPKGDIR', LIBMAKEPKGDIR) substs.set('STRIP_BINARIES', strip_binaries) substs.set('STRIP_SHARED', strip_shared) @@ -430,7 +420,6 @@ message('\n '.join([ ' Architecture : @0@'.format(carch), ' Host Type : @0@'.format(chost), ' File inode command : @0@'.format(inodecmd), - ' In-place sed command : @0@ @1@'.format(SED.path(), sedinplaceflags), ' File seccomp command : @0@'.format(filecmd), ' libalpm version : @0@'.format(libalpm_version), ' pacman version : @0@'.format(PACKAGE_VERSION), diff --git a/meson_options.txt b/meson_options.txt index 2b92ca1a..4d8cc300 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -54,6 +54,3 @@ option('i18n', type : 'boolean', value : true, # tools option('file-seccomp', type: 'feature', value: 'auto', description: 'determine whether file is seccomp-enabled') - -option('sedinplaceflags', type : 'string', value : 'auto', - description : 'flags to pass to sed to edit a file in-place') diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 63d09767..3cb6c133 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -186,8 +186,6 @@ edit = sed \ -e 's|@DEBUGSUFFIX[@]|$(DEBUGSUFFIX)|g' \ -e "s|@INODECMD[@]|$(INODECMD)|g" \ -e "s|@FILECMD[@]|$(FILECMD)|g" \ - -e 's|@SEDINPLACEFLAGS[@]|$(SEDINPLACEFLAGS)|g' \ - -e 's|@SEDPATH[@]|$(SEDPATH)|g' \ -e 's|@SCRIPTNAME[@]|$@|g' \ -e 's|@configure_input[@]|Generated from $<; do not edit by hand.|g' diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index c49ac57a..b9e83458 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -199,13 +199,14 @@ update_pkgver() { fi if [[ -n $newpkgver && $newpkgver != "$pkgver" ]]; then - if [[ -f $BUILDFILE && -w $BUILDFILE ]]; then - if ! @SEDPATH@ @SEDINPLACEFLAGS@ "s:^pkgver=[^ ]*:pkgver=$newpkgver:" "$BUILDFILE"; then + if [[ -w $BUILDFILE ]]; then + mapfile -t buildfile < "$BUILDFILE" + buildfile=("${buildfile[@]/#pkgver=*([^ ])/pkgver=$newpkgver}") + if ! printf '%s\n' "${buildfile[@]/#pkgrel=*([^ ])/pkgrel=1}" > "$BUILDFILE"; then error "$(gettext "Failed to update %s from %s to %s")" \ "pkgver" "$pkgver" "$newpkgver" exit $E_PKGBUILD_ERROR fi - @SEDPATH@ @SEDINPLACEFLAGS@ "s:^pkgrel=[^ ]*:pkgrel=1:" "$BUILDFILE" source_safe "$BUILDFILE" local fullver=$(get_full_version) msg "$(gettext "Updated version: %s")" "$pkgbase $fullver" -- 2.23.0
On 6/11/19 10:18 am, Ethan Sommer wrote:
Read PKGBUILD into an array and replace the pkgver and pkgrel with bash parameter substitution, then use shell redirection to write to to the file. Because shell redirection follows symlinks, this accomplishes the same thing as the previous default of using the GNU-specific --follow-symlinks sed flag.
Remove SEDPATH and SEDINPLACEFLAGS from the build systems as they are not used elsewhere. ---
I like the idea, but am concerned about unintended consequences... I saw the following mentioned on IRC: - potential for changed line endings - added newline at the end of files without one - removing any null characters I'm leaning on the side of these being fine, but need to mull on it some more. Anything else I missed? <snip>
diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index c49ac57a..b9e83458 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -199,13 +199,14 @@ update_pkgver() { fi
if [[ -n $newpkgver && $newpkgver != "$pkgver" ]]; then - if [[ -f $BUILDFILE && -w $BUILDFILE ]]; then - if ! @SEDPATH@ @SEDINPLACEFLAGS@ "s:^pkgver=[^ ]*:pkgver=$newpkgver:" "$BUILDFILE"; then + if [[ -w $BUILDFILE ]]; then + mapfile -t buildfile < "$BUILDFILE" + buildfile=("${buildfile[@]/#pkgver=*([^ ])/pkgver=$newpkgver}") + if ! printf '%s\n' "${buildfile[@]/#pkgrel=*([^ ])/pkgrel=1}" > "$BUILDFILE"; then
split into two lines: buildfile=("${buildfile[@]/#pkgrel=*([^ ])/pkgrel=1}") if ! print '%s\n" "${buildfile[@]}"...
error "$(gettext "Failed to update %s from %s to %s")" \ "pkgver" "$pkgver" "$newpkgver" exit $E_PKGBUILD_ERROR fi - @SEDPATH@ @SEDINPLACEFLAGS@ "s:^pkgrel=[^ ]*:pkgrel=1:" "$BUILDFILE" source_safe "$BUILDFILE" local fullver=$(get_full_version) msg "$(gettext "Updated version: %s")" "$pkgbase $fullver"
On 11/5/19 8:18 PM, Allan McRae wrote:
On 6/11/19 10:18 am, Ethan Sommer wrote:
Read PKGBUILD into an array and replace the pkgver and pkgrel with bash parameter substitution, then use shell redirection to write to to the file. Because shell redirection follows symlinks, this accomplishes the same thing as the previous default of using the GNU-specific --follow-symlinks sed flag.
Remove SEDPATH and SEDINPLACEFLAGS from the build systems as they are not used elsewhere. ---
I like the idea, but am concerned about unintended consequences...
I saw the following mentioned on IRC: - potential for changed line endings
You mean essentially dos2unix? The PKGBUILD would not be valid bash if it had the wrong type of line endings, bash would attempt to read lots of $'\r' as actual commands and stuff. That being said, a $'\r' in an embedded string could break, I suppose.
- added newline at the end of files without one
I would actually like to force people to have newlines at the end of their bash files. :(
- removing any null characters
(I don't actually expect those to exist. The PKGBUILD would have to be read by something other than makepkg, the only use case offhand I can think of for embedded null characters in a shell script is those foo.run installer things which extract a tarball archive from the end of the script file and then exit before hitting the appended tarball data.)
I'm leaning on the side of these being fine, but need to mull on it some more. As long as we don't care about embedded $'\r' in string data I *think* we should be fine.
-- Eli Schwartz Bug Wrangler and Trusted User
On 11/5/19 8:28 PM, Eli Schwartz wrote:
On 11/5/19 8:18 PM, Allan McRae wrote:
On 6/11/19 10:18 am, Ethan Sommer wrote:
Read PKGBUILD into an array and replace the pkgver and pkgrel with bash parameter substitution, then use shell redirection to write to to the file. Because shell redirection follows symlinks, this accomplishes the same thing as the previous default of using the GNU-specific --follow-symlinks sed flag.
Remove SEDPATH and SEDINPLACEFLAGS from the build systems as they are not used elsewhere. ---
I like the idea, but am concerned about unintended consequences...
I saw the following mentioned on IRC: - potential for changed line endings
You mean essentially dos2unix? The PKGBUILD would not be valid bash if it had the wrong type of line endings, bash would attempt to read lots of $'\r' as actual commands and stuff. That being said, a $'\r' in an embedded string could break, I suppose.
Turns out we even check for this. if [[ $(<"$BUILDFILE") = *$'\r'* ]]; then error "$(gettext "%s contains %s characters and cannot be sourced.")" "$BUILDFILE" "CRLF" -- Eli Schwartz Bug Wrangler and Trusted User
Reads PKGBUILD into an array and replaces the pkgver and pkgrel with bash parameter substitution, then uses shell redirection to write to to the file. Because shell redirection follows symlinks, this accomplishes the same thing as the previous default of using the GNU-specific --follow-symlinks sed flag. Removes SEDPATH and SEDINPLACEFLAGS from the build systems as they are not used elsewhere. Signed-off-by: Ethan Sommer <e5ten.arch@gmail.com> --- build-aux/edit-script.sh.in | 2 -- configure.ac | 11 ----------- meson.build | 11 ----------- meson_options.txt | 3 --- scripts/Makefile.am | 2 -- scripts/makepkg.sh.in | 8 +++++--- 6 files changed, 5 insertions(+), 32 deletions(-) diff --git a/build-aux/edit-script.sh.in b/build-aux/edit-script.sh.in index 640d32f8..7423a223 100644 --- a/build-aux/edit-script.sh.in +++ b/build-aux/edit-script.sh.in @@ -20,8 +20,6 @@ mode=$3 -e "s|@DEBUGSUFFIX[@]|@DEBUGSUFFIX@|g" \ -e "s|@INODECMD[@]|@INODECMD@|g" \ -e "s|@FILECMD[@]|@FILECMD@|g" \ - -e "s|@SEDINPLACEFLAGS[@]|@SEDINPLACEFLAGS@|g" \ - -e "s|@SEDPATH[@]|@SEDPATH@|g" \ -e "s|@configure_input[@]|Generated from ${input##*/}; do not edit by hand.|g" \ "$input" >"$output" diff --git a/configure.ac b/configure.ac index 305432b3..e59f82e9 100644 --- a/configure.ac +++ b/configure.ac @@ -369,7 +369,6 @@ AC_CHECK_MEMBERS([struct statfs.f_flags],,,[[#include <sys/param.h> GCC_VISIBILITY_CC # Host-dependant definitions -DEFAULT_SEDINPLACEFLAGS=" --follow-symlinks -i" INODECMD="stat -c '%i %n'" STRIP_BINARIES="--strip-all" STRIP_SHARED="--strip-unneeded" @@ -377,30 +376,21 @@ STRIP_STATIC="--strip-debug" case "${host_os}" in *bsd*) INODECMD="stat -f '%i %N'" - DEFAULT_SEDINPLACEFLAGS=" -i \"\"" ;; darwin*) host_os_darwin=yes INODECMD="/usr/bin/stat -f '%i %N'" - DEFAULT_SEDINPLACEFLAGS=" -i ''" STRIP_BINARIES="" STRIP_SHARED="-S" STRIP_STATIC="-S" ;; esac AM_CONDITIONAL([DARWIN], test "x$host_os_darwin" = "xyes") -AC_PATH_PROGS([SEDPATH], [sed], [sed], [/usr/bin$PATH_SEPARATOR/bin] ) AC_SUBST(INODECMD) AC_SUBST(STRIP_BINARIES) AC_SUBST(STRIP_SHARED) AC_SUBST(STRIP_STATIC) -# Flags for sed in place -if test "${SEDINPLACEFLAGS+set}" != "set"; then - SEDINPLACEFLAGS="${DEFAULT_SEDINPLACEFLAGS}" -fi -AC_ARG_VAR(SEDINPLACEFLAGS, [flags for sed, overriding the default]) - # Variables plugged into makepkg.conf CARCH="${host%%-*}" CHOST="${host}" @@ -576,7 +566,6 @@ ${PACKAGE_NAME}: Architecture : ${CARCH} Host Type : ${CHOST} File inode command : ${INODECMD} - In-place sed command : ${SEDPATH} ${SEDINPLACEFLAGS} File seccomp command : ${FILECMD} libalpm version : ${LIB_VERSION} diff --git a/meson.build b/meson.build index 8c296cb8..36f87ed2 100644 --- a/meson.build +++ b/meson.build @@ -221,7 +221,6 @@ config_h = configure_file( add_project_arguments('-include', 'config.h', language : 'c') filecmd = 'file' -default_sedinplaceflags = ' --follow-symlinks -i' inodecmd = 'stat -c \'%i %n\'' strip_binaries = '--strip-all' strip_shared = '--strip-unneeded' @@ -237,18 +236,11 @@ endif os = host_machine.system() if os.startswith('darwin') inodecmd = '/usr/bin/stat -f \'%i %N\'' - default_sedinplaceflags = ' -i \'\'' strip_binaries = '' strip_shared = '-s' strip_static = '-s' elif os.contains('bsd') or os == 'dragonfly' inodecmd = 'stat -f \'%i %N\'' - default_sedinplaceflags = ' -i \'\'' -endif - -sedinplaceflags = get_option('sedinplaceflags') -if sedinplaceflags == 'auto' - sedinplaceflags = default_sedinplaceflags endif chost = run_command(cc, '-dumpmachine').stdout().strip() @@ -277,8 +269,6 @@ substs.set('TEMPLATE_DIR', get_option('makepkg-template-dir')) substs.set('DEBUGSUFFIX', get_option('debug-suffix')) substs.set('INODECMD', inodecmd) substs.set('FILECMD', filecmd) -substs.set('SEDINPLACEFLAGS', sedinplaceflags) -substs.set('SEDPATH', SED.path()) substs.set('LIBMAKEPKGDIR', LIBMAKEPKGDIR) substs.set('STRIP_BINARIES', strip_binaries) substs.set('STRIP_SHARED', strip_shared) @@ -430,7 +420,6 @@ message('\n '.join([ ' Architecture : @0@'.format(carch), ' Host Type : @0@'.format(chost), ' File inode command : @0@'.format(inodecmd), - ' In-place sed command : @0@ @1@'.format(SED.path(), sedinplaceflags), ' File seccomp command : @0@'.format(filecmd), ' libalpm version : @0@'.format(libalpm_version), ' pacman version : @0@'.format(PACKAGE_VERSION), diff --git a/meson_options.txt b/meson_options.txt index 2b92ca1a..4d8cc300 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -54,6 +54,3 @@ option('i18n', type : 'boolean', value : true, # tools option('file-seccomp', type: 'feature', value: 'auto', description: 'determine whether file is seccomp-enabled') - -option('sedinplaceflags', type : 'string', value : 'auto', - description : 'flags to pass to sed to edit a file in-place') diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 63d09767..3cb6c133 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -186,8 +186,6 @@ edit = sed \ -e 's|@DEBUGSUFFIX[@]|$(DEBUGSUFFIX)|g' \ -e "s|@INODECMD[@]|$(INODECMD)|g" \ -e "s|@FILECMD[@]|$(FILECMD)|g" \ - -e 's|@SEDINPLACEFLAGS[@]|$(SEDINPLACEFLAGS)|g' \ - -e 's|@SEDPATH[@]|$(SEDPATH)|g' \ -e 's|@SCRIPTNAME[@]|$@|g' \ -e 's|@configure_input[@]|Generated from $<; do not edit by hand.|g' diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index c49ac57a..22df1b9f 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -199,13 +199,15 @@ update_pkgver() { fi if [[ -n $newpkgver && $newpkgver != "$pkgver" ]]; then - if [[ -f $BUILDFILE && -w $BUILDFILE ]]; then - if ! @SEDPATH@ @SEDINPLACEFLAGS@ "s:^pkgver=[^ ]*:pkgver=$newpkgver:" "$BUILDFILE"; then + if [[ -w $BUILDFILE ]]; then + mapfile -t buildfile < "$BUILDFILE" + buildfile=("${buildfile[@]/#pkgver=*([^ ])/pkgver=$newpkgver}") + buildfile=("${buildfile[@]/#pkgrel=*([^ ])/pkgrel=1}") + if ! printf '%s\n' "${buildfile[@]}" > "$BUILDFILE"; then error "$(gettext "Failed to update %s from %s to %s")" \ "pkgver" "$pkgver" "$newpkgver" exit $E_PKGBUILD_ERROR fi - @SEDPATH@ @SEDINPLACEFLAGS@ "s:^pkgrel=[^ ]*:pkgrel=1:" "$BUILDFILE" source_safe "$BUILDFILE" local fullver=$(get_full_version) msg "$(gettext "Updated version: %s")" "$pkgbase $fullver" -- 2.24.0
participants (3)
-
Allan McRae
-
Eli Schwartz
-
Ethan Sommer