From d8717a6a9666ec80c8645d190d6f9c7ab73084ac Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Wed, 7 Feb 2018 23:56:01 -0500 Subject: 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 Signed-off-by: Allan McRae --- scripts/libmakepkg/util/util.sh.in | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'scripts/libmakepkg/util/util.sh.in') diff --git a/scripts/libmakepkg/util/util.sh.in b/scripts/libmakepkg/util/util.sh.in index 7ccd2f8c..f9af5f32 100644 --- a/scripts/libmakepkg/util/util.sh.in +++ b/scripts/libmakepkg/util/util.sh.in @@ -83,3 +83,20 @@ cd_safe() { exit 1 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 +} -- cgit v1.2.3-54-g00ecf