improve code, seems to work well now

This commit is contained in:
Joey Hess 2013-12-27 02:48:47 -04:00
parent 5d00fa523c
commit 5b7c38c90a

View file

@ -3,7 +3,7 @@
# #
# This is basically the same as git-annex's built-in directory special remote. # This is basically the same as git-annex's built-in directory special remote.
# #
# Install in PATH as git-annex-remote-directorya # Install in PATH as git-annex-remote-directory
# #
# Copyright 2013 Joey Hess; licenced under the GNU GPL version 3 or higher. # Copyright 2013 Joey Hess; licenced under the GNU GPL version 3 or higher.
@ -18,12 +18,24 @@ runcmd () {
# Gets a value from the remote's configuration, and stores it in RET # Gets a value from the remote's configuration, and stores it in RET
getconfig () { getconfig () {
echo GETCONFIG "$1" ask GETCONFIG "$1"
}
# Sets LOC to the location to use to store a key.
calclocation () {
ask HASHDIR "$1"
LOC="$mydirectory/$hashdir/$RET"
}
# Asks for some value, and stores it in RET
ask () {
echo "$1" "$2"
read resp read resp
set -- $resp # Tricky POSIX shell code to split first word of the resp,
case "$1" in # preserving all other whitespace
case "${resp%% *}" in
VALUE) VALUE)
RET="$2" RET="${resp#[! ]*[ ]}"
;; ;;
*) *)
RET="" RET=""
@ -31,28 +43,14 @@ getconfig () {
esac esac
} }
# Sets LOC to the location to use to store a key. # This has to come first, to get the protocol started.
mylocation () {
echo HASHDIR "$1"
read resp
set -- $resp
case "$1" in
VALUE)
LOC="$hashdir/$1"
;;
*)
LOC=
;;
esac
}
echo VERSION 1 echo VERSION 1
while read line; do while read line; do
set -- $line set -- $line
case "$1" in case "$1" in
INITREMOTE) INITREMOTE)
# XXX do anything necessary to create resources # Do anything necessary to create resources
# used by the remote. Try to be idempotent. # used by the remote. Try to be idempotent.
# Use GETCONFIG to get any needed configuration # Use GETCONFIG to get any needed configuration
# settings, and SETCONFIG to set any persistent # settings, and SETCONFIG to set any persistent
@ -67,7 +65,7 @@ while read line; do
fi fi
;; ;;
PREPARE) PREPARE)
# XXX Use GETCONFIG to get configuration settings, # Use GETCONFIG to get configuration settings,
# and do anything needed to get ready for using the # and do anything needed to get ready for using the
# special remote here. # special remote here.
getconfig directory getconfig directory
@ -79,18 +77,27 @@ while read line; do
file="$4" file="$4"
case "$2" in case "$2" in
STORE) STORE)
# XXX upload file here # Store the file to a location
# based on the key.
# XXX when possible, send PROGRESS # XXX when possible, send PROGRESS
calclocation "$key" calclocation "$key"
mkdir -p "$(dirname "$LOC")" mkdir -p "$(dirname "$LOC")"
runcmd cp -v "$file" "$LOC" if runcmd cp -v "$file" "$LOC"; then
echo TRANSFER-SUCCESS STORE "$key" echo TRANSFER-SUCCESS STORE "$key"
else
echo TRANSFER-FAILURE STORE "$key"
fi
;; ;;
RETRIEVE) RETRIEVE)
# XXX download file here # Retrieve from a location based on
# the key, outputting to the file.
# XXX when easy, send PROGRESS
calclocation "$key" calclocation "$key"
runcmd cp -v "$LOC" "$file" if runcmd cp -v "$LOC" "$file"; then
echo TRANSFER-SUCCESS RETRIEVE "$key" echo TRANSFER-SUCCESS RETRIEVE "$key"
else
echo TRANSFER-FAILURE RETRIEVE "$key"
fi
;; ;;
esac esac
;; ;;
@ -116,9 +123,16 @@ while read line; do
key="$2" key="$2"
calclocation "$key" calclocation "$key"
# Note that it's not a failure to remove a # Note that it's not a failure to remove a
# key that is not present, so -f is used. # key that is not present.
runcmd rm -f "$LOC" if [ -e "$LOC" ]; then
echo REMOVE-SUCCESS "$key" if runcmd rm -f "$LOC"; then
echo REMOVE-SUCCESS "$key"
else
echo REMOVE-FAILURE "$key"
fi
else
echo REMOVE-SUCCESS "$key"
fi
;; ;;
*) *)
# The requests listed above are all the ones # The requests listed above are all the ones