makepkg: refactor checking for write permissions into a utility function

Additionally provide a separate error for failure to create the
directory vs lack of write permissions on a pre-existing directory.

This also means we now consistently try to create any nonexistent *DEST
directories as needed before aborting with E_FS_PERMISSIONS. Previously
only $BUILDDIR received that kindness.

Fixes FS#43537

Signed-off-by: Eli Schwartz <eschwartz@archlinux.org>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Eli Schwartz 2018-02-07 23:56:01 -05:00 committed by Allan McRae
parent 0565cebfc3
commit d8717a6a96
2 changed files with 27 additions and 21 deletions

View file

@ -83,3 +83,20 @@ cd_safe() {
exit 1 exit 1
fi fi
} }
# Try to create directory if one does not yet exist. Fails if the directory
# exists but has no write permissions, or if there is an existing file with
# the same name.
ensure_writable_dir() {
local dirtype="$1" dirpath="$2"
if ! mkdir -p "$dirpath" 2>/dev/null; then
if [[ -d $dirpath && ! -w $dirpath ]]; then
error "$(gettext "You do not have write permission for the directory \$%s (%s).")" "$dirtype" "$dirpath"
else
error "$(gettext "Failed to create the directory \$%s (%s).")" "$dirtype" "$dirpath"
fi
return 1
fi
return 0
}

View file

@ -1365,35 +1365,25 @@ else
fi fi
if [[ ! -d $BUILDDIR ]]; then # check that all settings directories are user-writable
if ! mkdir -p "$BUILDDIR"; then if ! ensure_writable_dir "BUILDDIR" "$BUILDDIR"; then
error "$(gettext "You do not have write permission to create packages in %s.")" "$BUILDDIR"
plain "$(gettext "Aborting...")" plain "$(gettext "Aborting...")"
exit $E_FS_PERMISSIONS exit $E_FS_PERMISSIONS
fi
chmod a-s "$BUILDDIR"
fi fi
if [[ ! -w $BUILDDIR ]]; then chmod a-s "$BUILDDIR"
error "$(gettext "You do not have write permission to create packages in %s.")" "$BUILDDIR"
if (( ! (NOBUILD || GENINTEG) )) && ! ensure_writable_dir "PKGDEST" "$PKGDEST"; then
plain "$(gettext "Aborting...")" plain "$(gettext "Aborting...")"
exit $E_FS_PERMISSIONS exit $E_FS_PERMISSIONS
fi fi
if (( ! (NOBUILD || GENINTEG) )) && [[ ! -w $PKGDEST ]]; then if ! ensure_writable_dir "SRCDEST" "$SRCDEST" ; then
error "$(gettext "You do not have write permission to store packages in %s.")" "$PKGDEST"
plain "$(gettext "Aborting...")"
exit $E_FS_PERMISSIONS
fi
if [[ ! -w $SRCDEST ]] ; then
error "$(gettext "You do not have write permission to store downloads in %s.")" "$SRCDEST"
plain "$(gettext "Aborting...")" plain "$(gettext "Aborting...")"
exit $E_FS_PERMISSIONS exit $E_FS_PERMISSIONS
fi fi
if (( SOURCEONLY )); then if (( SOURCEONLY )); then
if [[ ! -w $SRCPKGDEST ]]; then if ! ensure_writable_dir "SRCPKGDEST" "$SRCPKGDEST"; then
error "$(gettext "You do not have write permission to store source tarballs in %s.")" "$SRCPKGDEST"
plain "$(gettext "Aborting...")" plain "$(gettext "Aborting...")"
exit $E_FS_PERMISSIONS exit $E_FS_PERMISSIONS
fi fi
@ -1403,8 +1393,7 @@ if (( SOURCEONLY )); then
IGNOREARCH=1 IGNOREARCH=1
fi fi
if (( LOGGING )) && [[ ! -w $LOGDEST ]]; then if (( LOGGING )) && ! ensure_writable_dir "LOGDEST" "$LOGDEST"; then
error "$(gettext "You do not have write permission to store logs in %s.")" "$LOGDEST"
plain "$(gettext "Aborting...")" plain "$(gettext "Aborting...")"
exit $E_FS_PERMISSIONS exit $E_FS_PERMISSIONS
fi fi