From ea51b2613f0d2ec3e24ebeea2d32294b7c285175 Mon Sep 17 00:00:00 2001
From: Kazunori Kimura <norytama.k2@gmail.com>
Date: Mon, 27 Dec 2021 14:35:00 +0900
Subject: [PATCH] handle PG_DATADIR is empty (or too many files)

Previous implementation passed nothing to `chmod` when
PG_DATADIR is empty - this results in `chmod` raises syntax error.
We also can see `Argument list too long` error when
PG_DATADIR contains too many files / directries
(restriction defined by ARG_MAX)

Use xargs to avoid errors above
---
 runtime/functions | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/runtime/functions b/runtime/functions
index 65069e3..f6e7042 100755
--- a/runtime/functions
+++ b/runtime/functions
@@ -29,8 +29,8 @@ create_datadir() {
   echo "Initializing datadir..."
   mkdir -p ${PG_HOME}
   if [[ -d ${PG_DATADIR} ]]; then
-    chmod 0600 $( find ${PG_DATADIR} -type f )
-    chmod 0700 $( find ${PG_DATADIR} -type d )
+    find ${PG_DATADIR} -type f | xargs chmod 0600
+    find ${PG_DATADIR} -type d | xargs chmod 0700
   fi
   chown -R ${PG_USER}:${PG_USER} ${PG_HOME}
 }