Only set permissions when needed

This will speed up boot by only run chown when needed.
Solution taken from https://github.com/linuxserver/docker-tvheadend/blob/master/root/etc/cont-init.d/30-config#L22
This commit is contained in:
Rasmus 2019-08-19 14:11:04 +02:00 committed by Ryan Kuba
parent 3142fd2d70
commit 875eccd1bb

View File

@ -1,6 +1,27 @@
#!/usr/bin/with-contenv bash
# permissions
chown -R abc:abc \
/app \
/config
# function to randomly sample 5 files for their owner and only chown if not abc
chowner () {
files=(${1}/*)
for i in {1..5}; do
user=$(stat -c '%U' $(printf "%s\n" "${files[RANDOM % ${#files[@]}]}"))
if [ "${user}" != "abc" ]; then
chown -R abc:abc ${1}
break
fi
done
}
# permissions
echo "Setting permissions"
abc_dirs=( \
/app \
/config \
)
for i in "${abc_dirs[@]}"; do
if [ "$(ls -A ${i})" ]; then
chowner ${i}
else
chown -R abc:abc ${i}
fi
done