summaryrefslogtreecommitdiff
path: root/doc/special_remotes/external
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-12-27 02:48:47 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-12-27 02:49:00 -0400
commitd0d51528d0d90c218173593db17de1a480e0b15e (patch)
tree73495243716bfbf61b9e388a8817e42632863f85 /doc/special_remotes/external
parent352b4c673910a462fa4837db833c8f204923e453 (diff)
improve code, seems to work well now
Diffstat (limited to 'doc/special_remotes/external')
-rwxr-xr-xdoc/special_remotes/external/example.sh72
1 files changed, 43 insertions, 29 deletions
diff --git a/doc/special_remotes/external/example.sh b/doc/special_remotes/external/example.sh
index 53a00a557..c9412c506 100755
--- a/doc/special_remotes/external/example.sh
+++ b/doc/special_remotes/external/example.sh
@@ -3,7 +3,7 @@
#
# 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.
@@ -18,41 +18,39 @@ runcmd () {
# Gets a value from the remote's configuration, and stores it in RET
getconfig () {
- echo GETCONFIG "$1"
- read resp
- set -- $resp
- case "$1" in
- VALUE)
- RET="$2"
- ;;
- *)
- RET=""
- ;;
- esac
+ ask GETCONFIG "$1"
}
# Sets LOC to the location to use to store a key.
-mylocation () {
- echo HASHDIR "$1"
+calclocation () {
+ ask HASHDIR "$1"
+ LOC="$mydirectory/$hashdir/$RET"
+}
+
+# Asks for some value, and stores it in RET
+ask () {
+ echo "$1" "$2"
read resp
- set -- $resp
- case "$1" in
+ # Tricky POSIX shell code to split first word of the resp,
+ # preserving all other whitespace
+ case "${resp%% *}" in
VALUE)
- LOC="$hashdir/$1"
+ RET="${resp#[! ]*[ ]}"
;;
*)
- LOC=
+ RET=""
;;
esac
}
+# This has to come first, to get the protocol started.
echo VERSION 1
while read line; do
set -- $line
case "$1" in
INITREMOTE)
- # XXX do anything necessary to create resources
+ # Do anything necessary to create resources
# used by the remote. Try to be idempotent.
# Use GETCONFIG to get any needed configuration
# settings, and SETCONFIG to set any persistent
@@ -67,7 +65,7 @@ while read line; do
fi
;;
PREPARE)
- # XXX Use GETCONFIG to get configuration settings,
+ # Use GETCONFIG to get configuration settings,
# and do anything needed to get ready for using the
# special remote here.
getconfig directory
@@ -79,18 +77,27 @@ while read line; do
file="$4"
case "$2" in
STORE)
- # XXX upload file here
+ # Store the file to a location
+ # based on the key.
# XXX when possible, send PROGRESS
calclocation "$key"
mkdir -p "$(dirname "$LOC")"
- runcmd cp -v "$file" "$LOC"
- echo TRANSFER-SUCCESS STORE "$key"
+ if runcmd cp -v "$file" "$LOC"; then
+ echo TRANSFER-SUCCESS STORE "$key"
+ else
+ echo TRANSFER-FAILURE STORE "$key"
+ fi
;;
RETRIEVE)
- # XXX download file here
+ # Retrieve from a location based on
+ # the key, outputting to the file.
+ # XXX when easy, send PROGRESS
calclocation "$key"
- runcmd cp -v "$LOC" "$file"
- echo TRANSFER-SUCCESS RETRIEVE "$key"
+ if runcmd cp -v "$LOC" "$file"; then
+ echo TRANSFER-SUCCESS RETRIEVE "$key"
+ else
+ echo TRANSFER-FAILURE RETRIEVE "$key"
+ fi
;;
esac
;;
@@ -116,9 +123,16 @@ while read line; do
key="$2"
calclocation "$key"
# Note that it's not a failure to remove a
- # key that is not present, so -f is used.
- runcmd rm -f "$LOC"
- echo REMOVE-SUCCESS "$key"
+ # key that is not present.
+ if [ -e "$LOC" ]; then
+ 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