From 7ee6ac8da2e7c547f54f09bcc02c7d630ff63f04 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Paul=20H=C3=A4nsch?= <paul@plutz.net>
Date: Wed, 31 May 2023 23:12:42 +0200
Subject: [PATCH] performance bugfix: fix broken acl caching, speed improvement

---
 acl.sh | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/acl.sh b/acl.sh
index 1b25d5e..1017c6e 100755
--- a/acl.sh
+++ b/acl.sh
@@ -10,19 +10,11 @@ acl_cachepath=''
 acl_collection=''
 
 acl_collect(){
-  local path="${1:-${PATH_INFO}}"
+  local path="$1"
   # Get directory part of PATH_INFO
   local path="${path%/*}/./"
   local pagefile head acl
 
-  if [ "$acl_cachepath" = "$path" ]; then
-    printf '%s\n' "$ACL_OVERRIDE" "$acl_collection" "$ACL_DEFAULT"
-    return 0
-  else
-    acl_cachepath="$path"
-    acl_collection=''
-  fi
-
   printf '%s\n' "$ACL_OVERRIDE"
 
   while :; do
@@ -53,7 +45,6 @@ acl_collect(){
     ' <"$pagefile")"
 
     printf %s\\n "${acl}"
-    acl_collection="${acl_collection}${acl}${BR}"
   done
 
   printf '%s\n' "$ACL_DEFAULT"
@@ -63,6 +54,11 @@ acl_read(){
   local page="${1:-${PATH_INFO}}"
   local acl
 
+  if [ "$acl_cachepath" != "$page" ]; then
+    acl_cachepath="$page"
+    acl_collection="$(acl_collect "$page")"
+  fi
+
   while read -r acl; do
     case ${acl##*:} in
       read|*,read,*|read,*|*,read)
@@ -85,8 +81,8 @@ acl_read(){
       "+All:read") return 0;;
       "-All:read") return 1;;
     esac
-  done <<-EOF
-	$(acl_collect "$page")
+   done <<-EOF
+	${acl_collection}
 	EOF
   return 1
 }
@@ -95,6 +91,11 @@ acl_write(){
   local page="${1:-${PATH_INFO}}"
   local acl
 
+  if [ "$acl_cachepath" != "$page" ]; then
+    acl_cachepath="$page"
+    acl_collection="$(acl_collect "$page")"
+  fi
+
   while read -r acl; do
     case ${acl##*:} in
       write|*,write,*|write,*|*,write)
@@ -118,7 +119,7 @@ acl_write(){
       "-All:write") return 1;;
     esac
   done <<-EOF
-	$(acl_collect "$page")
+	${acl_collection}
 	EOF
   return 1
 }
-- 
2.39.5