From a9650ae28736aedd62680f4f705525021185d519 Mon Sep 17 00:00:00 2001
From: Karol Kania <44871508+karolkania@users.noreply.github.com>
Date: Fri, 28 Feb 2025 12:52:07 +0100
Subject: [PATCH] Update gravity.sh

fix the `if statement` that doesn't seem to work for neither of alpine's ash / bash - applying some workaround with the `stat` command

Signed-off-by: Karol Kania <44871508+karolkania@users.noreply.github.com>
---
 gravity.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gravity.sh b/gravity.sh
index 0a34599b..cc8c6ba1 100755
--- a/gravity.sh
+++ b/gravity.sh
@@ -541,14 +541,18 @@ gravity_DownloadBlocklists() {
     # it (in case it doesn't exist)
     # First, check if the directory is writable
     directory="$(dirname -- "${saveLocation}")"
-    if [ ! -w "${directory}" ]; then
+    directory_permissions=$(stat -c %a ${directory})
+    # echo "directory_permissions: ${directory_permissions}"
+    if [ $directory_permissions -lt 700 ]; then
       echo -e "  ${CROSS} Unable to write to ${directory}"
       echo "      Please run pihole -g as root"
       echo ""
       continue
     fi
     # Then, check if the file is writable (if it exists)
-    if [ -e "${saveLocation}" ] && [ ! -w "${saveLocation}" ]; then
+    saveLocation_permissions=$(stat -c %a ${saveLocation})
+    # echo "saveLocation_permissions: ${saveLocation_permissions}"
+    if [ -e "${saveLocation}" ] && [ ${saveLocation_permissions} -lt 600 ]; then
       echo -e "  ${CROSS} Unable to write to ${saveLocation}"
       echo "      Please run pihole -g as root"
       echo ""