rewrite volume check/create scripts

add option to skip ownership change (fix for Docker Desktop "Synchronized file shares
" feature)
This commit is contained in:
trikimiki 2025-02-26 18:31:18 +02:00
parent b9e8fae8ab
commit acc5764565
3 changed files with 68 additions and 15 deletions

View File

@ -182,29 +182,77 @@ function permissionList() {
} }
function checkFolders() { function checkFolders() {
CREATE=false
SKIP_CHOWN=false
for i in "$@"
do
case $i in
--create)
CREATE=true
shift
;;
--skipChown)
SKIP_CHOWN=true
shift
;;
*)
# unknown option
;;
esac
done
EXIT_CODE=0 EXIT_CODE=0
PERMISSION_LIST=$(permissionList) || exit $? PERMISSION_LIST=$(permissionList) || exit $?
set -e set -e
while read -r USR GRP DIR while read -r USR GRP DIR
do do
if [ -z "$DIR" ]; then # skip empty lines IS_EXIST_CHECK_PASSED=false
IS_OWNER_CHECK_PASSED=false
# skip empty lines
if [ -z "$DIR" ]; then
continue continue
fi fi
MESSAGE="Checking user ${USR} group ${GRP} dir ${DIR}"
if [[ -d "$DIR" ]] && # checks section
[[ $(ls -ldn "$DIR" | awk '{print $3}') -eq "$USR" ]] && echo "Checking if dir ${DIR} exists..."
[[ $(ls -ldn "$DIR" | awk '{print $4}') -eq "$GRP" ]] if [[ -d "$DIR" ]]; then
then echo "> OK"
MESSAGE="$MESSAGE OK" IS_EXIST_CHECK_PASSED=true
if [ "$SKIP_CHOWN" = false ]; then
echo "Checking user ${USR} group ${GRP} ownership for dir ${DIR}..."
if [[ $(ls -ldn "$DIR" | awk '{print $3}') -eq "$USR" ]] && [[ $(ls -ldn "$DIR" | awk '{print $4}') -eq "$GRP" ]]; then
echo "> OK"
IS_OWNER_CHECK_PASSED=true
else else
if [ "$1" = "--create" ]; then echo "...ownership check failed"
echo "Create and chown: user ${USR} group ${GRP} dir ${DIR}" if [ "$CREATE" = false ]; then
mkdir -p "$DIR" && sudo chown -R "$USR":"$GRP" "$DIR"
else
echo "$MESSAGE FAILED"
EXIT_CODE=1 EXIT_CODE=1
fi fi
fi fi
fi
else
echo "...does not exist"
if [ "$CREATE" = false ]; then
EXIT_CODE=1
fi
fi
# create/chown section
if [ "$CREATE" = true ]; then
if [ "$IS_EXIST_CHECK_PASSED" = false ]; then
echo "...will create dir ${DIR}"
if [ "$SKIP_CHOWN" = false ]; then
echo "...will change ownership to user ${USR} group ${GRP} for dir ${DIR}"
mkdir -p "$DIR" && sudo chown -R "$USR":"$GRP" "$DIR" && echo "> OK"
else
mkdir -p "$DIR" && echo "> OK"
fi
elif [ "$IS_OWNER_CHECK_PASSED" = false ] && [ "$SKIP_CHOWN" = false ]; then
echo "...will change ownership to user ${USR} group ${GRP} for dir ${DIR}"
sudo chown -R "$USR":"$GRP" "$DIR" && echo "> OK"
fi
fi
done < <(echo "$PERMISSION_LIST") done < <(echo "$PERMISSION_LIST")
return $EXIT_CODE return $EXIT_CODE
} }

View File

@ -17,5 +17,10 @@
set -e set -e
source compose-utils.sh source compose-utils.sh
checkFolders || exit $? if checkFolders "$@" ; then
echo "OK" echo "------"
echo "All checks have passed"
else
echo "------"
echo "Some checks did not pass - check the output"
fi

View File

@ -17,4 +17,4 @@
set -e set -e
source compose-utils.sh source compose-utils.sh
checkFolders --create checkFolders --create "$@"