nextcloud-docker-inotify/s6-rc-inotify/run

65 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

2025-02-12 22:11:50 +01:00
#!/usr/bin/with-contenv bash
# shellcheck shell=bash
2025-02-12 22:13:14 +01:00
# Check if inotify exists. If not, install it.
2025-02-12 22:11:50 +01:00
if ! command -v inotifywait 2>&1 >/dev/null; then
echo "No inotifywait found. install inotify tools"
apk add inotify-tools
fi
2025-02-12 22:13:14 +01:00
# define stuff that should be excluded some watches.
2025-02-12 22:11:50 +01:00
BASEEXCLUDE='.*\.swp|.*\.swx'
2025-02-12 22:13:14 +01:00
# the main function where all of the magic happens. takes 4 arguments:
# - the folder to watch
# - stuff to exclude
# - two arguments to replace things in the paths coming from inotify, to turn them into a path nextcloud understands.
2025-02-12 22:11:50 +01:00
detect(){
WATCH=$1
EXCLUDE=$2
REPLACEIN=$3
REPLACEWITH=$4
echo "watching $WATCH and excluding $EXCLUDE"
2025-02-12 22:13:14 +01:00
# this uses inotifywait to watch for changes
# then it runs the code in the while block to handle file paths it spits out
2025-02-13 00:14:14 +01:00
inotifywait -mre close_write,delete \
--format '%e|%w|%f' \
--exclude "$EXCLUDE" \
"$WATCH" | while read RAWEVENT \
/
2025-02-12 22:11:50 +01:00
do
2025-02-12 22:13:14 +01:00
# inotifywait spits out the events like "eventtype|/directory|filename"
# here we split them into it's three parts.
2025-02-12 22:11:50 +01:00
IFS='|'
read -a SPLITEVENT <<< "$RAWEVENT"
EVENT=${SPLITEVENT[0]}
DIR=${SPLITEVENT[1]}
FILE=${SPLITEVENT[2]}
2025-02-12 22:13:14 +01:00
# by default, scan the whole directory
# we can't scan a file that's been deleted
2025-02-12 22:11:50 +01:00
REPLACEDFILE=$DIR
2025-02-12 22:13:14 +01:00
# if the event wasn't a delete, we append the filename
# so we only scan the file that was changed.
2025-02-12 22:11:50 +01:00
if [[ $EVENT != 'DELETE' ]]; then
REPLACEDFILE+=$FILE
fi
2025-02-12 22:13:14 +01:00
# replace the beginning of the paths to the filename.
# inotify spits out the filesystem path
# nextcloud expects the path relative to the user directory
2025-02-12 22:11:50 +01:00
if [ -n "$REPLACEIN" ] && [ -n "$REPLACEWITH" ]; then
REPLACEDFILE=${REPLACEDFILE/$REPLACEIN/$REPLACEWITH}
fi
2025-02-12 22:13:14 +01:00
# actually run the OCC command
2025-02-12 22:11:50 +01:00
occ files:scan --path=\"$REPLACEDFILE\" --shallow
done
}
2025-02-12 22:13:14 +01:00
# run the scan function on the folders I want scanned
2025-02-12 22:11:50 +01:00
detect /data/riksolo/files $BASEEXCLUDE /data/riksolo /riksolo