2013-12-26 22:14:52 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# git-annex external special remote program
|
|
|
|
#
|
|
|
|
# This is basically the same as git-annex's built-in directory special remote.
|
|
|
|
#
|
2013-12-27 06:48:47 +00:00
|
|
|
# Install in PATH as git-annex-remote-directory
|
2013-12-26 22:14:52 +00:00
|
|
|
#
|
|
|
|
# Copyright 2013 Joey Hess; licenced under the GNU GPL version 3 or higher.
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# This program speaks a line-based protocol on stdin and stdout.
|
|
|
|
# When running any commands, their stdout should be redirected to stderr
|
|
|
|
# (or /dev/null) to avoid messing up the protocol.
|
|
|
|
runcmd () {
|
|
|
|
"$@" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
# Gets a value from the remote's configuration, and stores it in RET
|
|
|
|
getconfig () {
|
2013-12-27 06:48:47 +00:00
|
|
|
ask GETCONFIG "$1"
|
2013-12-26 22:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Sets LOC to the location to use to store a key.
|
2013-12-27 06:48:47 +00:00
|
|
|
calclocation () {
|
2013-12-27 18:04:51 +00:00
|
|
|
ask DIRHASH "$1"
|
2013-12-27 18:06:33 +00:00
|
|
|
LOC="$mydirectory/$RET/$1"
|
2013-12-27 06:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Asks for some value, and stores it in RET
|
|
|
|
ask () {
|
|
|
|
echo "$1" "$2"
|
2013-12-26 22:14:52 +00:00
|
|
|
read resp
|
2013-12-27 06:48:47 +00:00
|
|
|
# Tricky POSIX shell code to split first word of the resp,
|
|
|
|
# preserving all other whitespace
|
|
|
|
case "${resp%% *}" in
|
2013-12-26 22:14:52 +00:00
|
|
|
VALUE)
|
2013-12-27 18:04:51 +00:00
|
|
|
RET="$(echo "$resp" | sed 's/^VALUE \?//')"
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
*)
|
2013-12-27 06:48:47 +00:00
|
|
|
RET=""
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2013-12-27 06:48:47 +00:00
|
|
|
# This has to come first, to get the protocol started.
|
2013-12-26 22:14:52 +00:00
|
|
|
echo VERSION 1
|
|
|
|
|
|
|
|
while read line; do
|
|
|
|
set -- $line
|
|
|
|
case "$1" in
|
|
|
|
INITREMOTE)
|
2013-12-27 06:48:47 +00:00
|
|
|
# Do anything necessary to create resources
|
2013-12-26 22:14:52 +00:00
|
|
|
# used by the remote. Try to be idempotent.
|
2013-12-27 06:56:34 +00:00
|
|
|
#
|
2013-12-26 22:14:52 +00:00
|
|
|
# Use GETCONFIG to get any needed configuration
|
|
|
|
# settings, and SETCONFIG to set any persistent
|
|
|
|
# configuration settings.
|
2013-12-27 06:56:34 +00:00
|
|
|
#
|
|
|
|
# (Note that this is not run every time, only when
|
|
|
|
# git annex initremote or git annex enableremote is
|
|
|
|
# run.)
|
2013-12-26 22:14:52 +00:00
|
|
|
getconfig directory
|
|
|
|
mydirectory="$RET"
|
|
|
|
if [ -z "$mydirectory" ]; then
|
|
|
|
echo INITREMOTE-FAILURE "You need to set directory="
|
|
|
|
else
|
|
|
|
mkdir -p "$mydirectory"
|
|
|
|
echo INITREMOTE-SUCCESS
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
PREPARE)
|
2013-12-27 06:48:47 +00:00
|
|
|
# Use GETCONFIG to get configuration settings,
|
2013-12-26 22:14:52 +00:00
|
|
|
# and do anything needed to get ready for using the
|
|
|
|
# special remote here.
|
|
|
|
getconfig directory
|
|
|
|
mydirectory="$RET"
|
2013-12-27 06:12:33 +00:00
|
|
|
echo PREPARE-SUCCESS
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
TRANSFER)
|
|
|
|
key="$3"
|
|
|
|
file="$4"
|
|
|
|
case "$2" in
|
|
|
|
STORE)
|
2013-12-27 06:48:47 +00:00
|
|
|
# Store the file to a location
|
|
|
|
# based on the key.
|
2013-12-26 22:14:52 +00:00
|
|
|
# XXX when possible, send PROGRESS
|
|
|
|
calclocation "$key"
|
|
|
|
mkdir -p "$(dirname "$LOC")"
|
2013-12-27 06:48:47 +00:00
|
|
|
if runcmd cp -v "$file" "$LOC"; then
|
|
|
|
echo TRANSFER-SUCCESS STORE "$key"
|
|
|
|
else
|
|
|
|
echo TRANSFER-FAILURE STORE "$key"
|
|
|
|
fi
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
RETRIEVE)
|
2013-12-27 06:48:47 +00:00
|
|
|
# Retrieve from a location based on
|
|
|
|
# the key, outputting to the file.
|
2013-12-27 06:56:34 +00:00
|
|
|
# XXX when easy to do, send PROGRESS
|
2013-12-26 22:14:52 +00:00
|
|
|
calclocation "$key"
|
2013-12-27 06:48:47 +00:00
|
|
|
if runcmd cp -v "$LOC" "$file"; then
|
|
|
|
echo TRANSFER-SUCCESS RETRIEVE "$key"
|
|
|
|
else
|
|
|
|
echo TRANSFER-FAILURE RETRIEVE "$key"
|
|
|
|
fi
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
CHECKPRESENT)
|
|
|
|
key="$2"
|
|
|
|
calclocation "$key"
|
|
|
|
if [ -e "$LOC" ]; then
|
|
|
|
echo CHECKPRESENT-SUCCESS "$key"
|
|
|
|
else
|
|
|
|
if [ -d "$mydirectory" ]; then
|
|
|
|
echo CHECKPRESENT-FAILURE "$key"
|
|
|
|
else
|
2013-12-27 06:56:34 +00:00
|
|
|
# When the directory does not exist,
|
2013-12-26 22:14:52 +00:00
|
|
|
# the remote is not available.
|
|
|
|
# (A network remote would similarly
|
|
|
|
# fail with CHECKPRESENT-UNKNOWN
|
|
|
|
# if it couldn't be contacted).
|
|
|
|
echo CHECKPRESENT-UNKNOWN "$key" "this remote is not currently available"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
REMOVE)
|
|
|
|
key="$2"
|
|
|
|
calclocation "$key"
|
|
|
|
# Note that it's not a failure to remove a
|
2013-12-27 06:48:47 +00:00
|
|
|
# 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
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
*)
|
2013-12-27 06:08:29 +00:00
|
|
|
# The requests listed above are all the ones
|
|
|
|
# that are required to be supported, so it's fine
|
|
|
|
# to say that any other request is unsupported.
|
|
|
|
echo UNSUPPORTED-REQUEST
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# XXX anything that needs to be done at shutdown can be done here
|