From 70c5c64373120f6cd8ddff64342418c075f64a82 Mon Sep 17 00:00:00 2001 From: psfloyd Date: Fri, 1 Dec 2017 00:12:14 -0300 Subject: [PATCH] Fix for service files and script to work with directories with spaces in their names --- systemd/grive-changes@.service.in | 2 +- systemd/grive-sync.sh | 47 ++++++++++++++++--------------- systemd/grive-timer@.service.in | 2 +- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/systemd/grive-changes@.service.in b/systemd/grive-changes@.service.in index 172ab19..6b3ca29 100644 --- a/systemd/grive-changes@.service.in +++ b/systemd/grive-changes@.service.in @@ -2,7 +2,7 @@ Description=Google drive sync (changed files) [Service] -ExecStart=@GRIVE_SYNC_SH_BINARY@ listen "%i" +ExecStart=@GRIVE_SYNC_SH_BINARY@ listen "%I" Type=simple Restart=always RestartSec=30 diff --git a/systemd/grive-sync.sh b/systemd/grive-sync.sh index f070ac0..13cacfe 100755 --- a/systemd/grive-sync.sh +++ b/systemd/grive-sync.sh @@ -14,16 +14,16 @@ cd ~ ### ARGUMENT PARSING ### -SCRIPT=${0} +SCRIPT="${0}" -if [[ -z ${2} ]] || [[ ! -d ${2} ]] ; then +if [[ -z "${2}" ]] || [[ ! -d "${2}" ]] ; then echo "Need a directory name in the current users home directory as second argument. Aborting." exit 1 fi -DIRECTORY=$2 +DIRECTORY="$2" -if [[ -z ${1} ]] ; then +if [[ -z "${1}" ]] ; then echo "Need a command as first argument. Aborting." exit 1 else @@ -39,13 +39,13 @@ fi ### LOCKFILE BOILERPLATE ### -LOCKFILE="/run/user/$(id -u)/$(basename $0)_${DIRECTORY//\//_}" +LOCKFILE="/run/user/"$(id -u)"/"$(basename "$0")"_"${DIRECTORY//\//_}"" LOCKFD=99 # PRIVATE -_lock() { flock -$1 $LOCKFD; } -_no_more_locking() { _lock u; _lock xn && rm -f $LOCKFILE; } -_prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; } +_lock() { flock -"$1" "$LOCKFD"; } +_no_more_locking() { _lock u; _lock xn && rm -f "$LOCKFILE"; } +_prepare_locking() { eval "exec "$LOCKFD">\""$LOCKFILE"\""; trap _no_more_locking EXIT; } # ON START _prepare_locking @@ -61,7 +61,7 @@ unlock() { _lock u; } # drop a lock # make sure we sync a second time, too sync_directory() { - _directory=${1} + _directory="${1}" if ping -c1 -W1 -q accounts.google.com >/dev/null 2>&1; then true # pass @@ -70,18 +70,18 @@ sync_directory() { exit 0 fi - reset_timer_and_exit() { echo "Retriggered google drive sync" && touch -m $LOCKFILE && exit; } + reset_timer_and_exit() { echo "Retriggered google drive sync" && touch -m "$LOCKFILE" && exit; } exlock_now || reset_timer_and_exit TIME_AT_START=0 TIME_AT_END=1 - while [[ ${TIME_AT_START} -lt ${TIME_AT_END} ]]; do - echo "Syncing ${_directory}..." - TIME_AT_START=$(stat -c %Y $LOCKFILE) - grive -p ${_directory} 2>&1 | grep -v -E "^Reading local directories$|^Reading remote server file list$|^Synchronizing files$|^Finished!$" - TIME_AT_END=$(stat -c %Y $LOCKFILE) - echo "Sync of ${_directory} done." + while [[ "${TIME_AT_START}" -lt "${TIME_AT_END}" ]]; do + echo "Syncing "${_directory}"..." + TIME_AT_START="$(stat -c %Y "$LOCKFILE")" + grive -p "${_directory}" 2>&1 | grep -v -E "^Reading local directories$|^Reading remote server file list$|^Synchronizing files$|^Finished!$" + TIME_AT_END="$(stat -c %Y "$LOCKFILE")" + echo "Sync of "${_directory}" done." done # always exit ok, so that we never go into a wrong systemd state @@ -92,27 +92,28 @@ sync_directory() { listen_directory() { - _directory=${1} + _directory="${1}" type inotifywait >/dev/null 2>&1 || { echo >&2 "I require inotifywait but it's not installed. Aborting."; exit 1; } - echo "Listening for changes in ~/${_directory}" + echo "Listening for changes in ~/"${_directory}"" while true #run indefinitely do # Use a different call to not need to change exit into return - inotifywait -q -r -e modify,attrib,close_write,move,create,delete --exclude ".grive_state|.grive" ${_directory} > /dev/null 2>&1 && ${SCRIPT} sync ${_directory} + inotifywait -q -r -e modify,attrib,close_write,move,create,delete --exclude ".grive_state|.grive" "${_directory}" > /dev/null 2>&1 && ${SCRIPT} sync "${_directory}" + #echo ${SCRIPT} "${_directory}" done # always exit ok, so that we never go into a wrong systemd state exit 0 } -if [[ ${COMMAND} == listen ]] ; then - listen_directory ${DIRECTORY} +if [[ "${COMMAND}" == listen ]] ; then + listen_directory "${DIRECTORY}" else - sync_directory ${DIRECTORY} + sync_directory "${DIRECTORY}" fi # always exit ok, so that we never go into a wrong systemd state -exit 0 \ No newline at end of file +exit 0 diff --git a/systemd/grive-timer@.service.in b/systemd/grive-timer@.service.in index a6aae6a..56ae8b3 100644 --- a/systemd/grive-timer@.service.in +++ b/systemd/grive-timer@.service.in @@ -3,4 +3,4 @@ Description=Google drive sync After=network-online.target [Service] -ExecStart=@GRIVE_SYNC_SH_BINARY@ sync "%i" +ExecStart=@GRIVE_SYNC_SH_BINARY@ sync "%I"