Syncthing

system service with own user

a watcher that makes everything be owned by _syncthing:_syncthing

it still will take it a while to take care of new files as kqueue isn't capable of that.

Get https://emcrisostomo.github.io/fswatch/

the_syncthing_helper.sh

#!/bin/sh
(echo $$ > /var/st_daemon.pid && exec nice fswatch -r --event 16380 -0 $1) |
nice xargs -r -n1 -0 chown -R _syncthing:_syncthing

/etc/rc.d/st_helper

#!/bin/ksh

daemon="/etc/the_syncthing_helper.sh"
daemon_flags="/var/syncthing/*"
daemon_user=root

. /etc/rc.d/rc.subr

rc_reload=NO

rc_bg=YES

pexp='.*syncthing_helper.*'

rc_check() {
    test -f /var/st_daemon.pid &&
    ps -o pid -p "`cat /var/st_daemon.pid`" >/dev/null
}

rc_stop() {
    head -1 /var/st_daemon.pid | xargs -r -n1 -J % pkill -P % fswatch
}

rc_cmd $1

a script that makes everything be owned by _syncthing:_syncthing periodically (too slow)

*/3 * * * * _syncthing -ns /etc/the_syncthing_script.sh

#!/bin/sh
PREOWN='chown '
FINALOWN='chown 1000 {}'
case `id -u` in
        0 | `id -u _syncthing`)
                ;;
        1000)
                PREOWN="doas chown 1000"
                FINALOWN=true
                ;;
        *)
                >&2 echo "bad user $(whoami)"
                exit 1
                ;;
esac

# find everything in Biezace owned by _syncthing
# chown to group syncthing
# chown to us if needed for chmod
# chmod g+w
# if directory, chmod ug+x
# chown to user if not already
# stderr all failures

ECHO="-exec echo"

find /var/syncthing/Biezace \
-user _syncthing \
\( \
-exec ${PREOWN}:_syncthing {} \; -o \
        $ECHO 1 {} \; \) \
\( \
-exec chmod g+w {} \; -o \
        $ECHO 2 {} \; \) \
\( \(   \(      -type d \
                -exec chmod ug+x {} \; -o \
                        $ECHO 3 {} \; \
        \) \
        -or \
        -exec true \; \
\) -o \
        $ECHO 4 {} \; \) \
\( \
-exec $FINALOWN \; -o \
        $ECHO 5 {} \; \) \
| ifne -n false  # moreutils; so cron -n sends fail email