Skip to content

Commit 7a2d48d

Browse files
committed
add safe_chown function to check if dir/file exists and if it already has the correct ownership before chowning it
1 parent d518f88 commit 7a2d48d

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

pkg/docker/entrypoint.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ if ! whoami > /dev/null 2>&1; then
2727
fi
2828
fi
2929

30+
# Helper: chown a path only if it exists and isn't already owned correctly
31+
safe_chown() {
32+
local target="$1"
33+
local owner="$2:$3" # UID:GID
34+
35+
# Skip if path doesn't exist
36+
[ -e "$target" ] || return 0
37+
38+
# Get current ownership
39+
local current_uid current_gid
40+
current_uid=$(stat -c '%u' "$target")
41+
current_gid=$(stat -c '%g' "$target")
42+
43+
# Skip if already owned correctly
44+
if [ "$current_uid" = "$1" ] && [ "$current_gid" = "$2" ]; then
45+
return 0
46+
fi
47+
48+
chown -R "$owner" "$target"
49+
}
50+
3051
# usage: file_env VAR [DEFAULT] ie: file_env 'XYZ_DB_PASSWORD' 'example'
3152
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
3253
# "$XYZ_DB_PASSWORD" from a file, for Docker's secrets feature)
@@ -197,7 +218,9 @@ fi
197218
TIMEOUT=$(cd /pgadmin4 && /venv/bin/python3 -c 'import config; print(config.SESSION_EXPIRATION_TIME * 60 * 60 * 24)')
198219

199220
if [ "$(id -u)" = "0" ]; then
200-
chown -R "$PUID:$PGID" /run/pgadmin /var/lib/pgadmin /pgadmin4/config_distro.py /certs
221+
for path in /run/pgadmin /var/lib/pgadmin "$CONFIG_DISTRO_FILE_PATH" /certs; do
222+
safe_chown "$path" "$PUID" "$PGID"
223+
done
201224
fi
202225

203226
# NOTE: currently pgadmin can run only with 1 worker due to sessions implementation

0 commit comments

Comments
 (0)