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
|
|
|
}
|
|
|
|
|
2013-12-27 18:30:00 +00:00
|
|
|
# Stores a value in the remote's configuration.
|
|
|
|
setconfig () {
|
|
|
|
echo SETCONFIG "$1" "$2"
|
|
|
|
}
|
|
|
|
|
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 20:01:43 +00:00
|
|
|
# This remote doesn't need credentials to access it,
|
|
|
|
# but many of them will. Here's how to handle requiring the user
|
|
|
|
# set MYPASSWORD and MYLOGIN when running initremote. The creds
|
|
|
|
# will be stored securely for later use, so the user only needs
|
|
|
|
# to provide them once.
|
|
|
|
setupcreds () {
|
|
|
|
if [ -z "$MYPASSWORD" ] || [ -z "$MYLOGIN" ]; then
|
|
|
|
echo INITREMOTE-FAILURE "You need to set MYPASSWORD and MYLOGIN environment variables when running initremote."
|
|
|
|
else
|
|
|
|
echo SETCREDS mycreds "$MYLOGIN" "$MYPASSWORD"
|
|
|
|
echo INITREMOTE-SUCCESS
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
getcreds () {
|
|
|
|
echo GETCREDS mycreds
|
|
|
|
read resp
|
|
|
|
case "${resp%% *}" in
|
|
|
|
CREDS)
|
|
|
|
MYLOGIN="$(echo "$resp" | sed 's/^CREDS \([^ ]*\) .*/\1/')"
|
|
|
|
MYPASSWORD="$(echo "$resp" | sed 's/^CREDS [^ ]* //')"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:24:05 +00:00
|
|
|
dostore () {
|
|
|
|
local key="$1"
|
|
|
|
local file="$2"
|
|
|
|
local loc="$3"
|
|
|
|
mkdir -p "$(dirname "$loc")"
|
|
|
|
# Store in temp file first, so that CHECKPRESENT does not see it
|
|
|
|
# until it is all stored.
|
|
|
|
mkdir -p "$mydirectory/tmp"
|
|
|
|
tmp="$mydirectory/tmp/$key"
|
|
|
|
# XXX when at all possible, send PROGRESS while transferring
|
|
|
|
# the file.
|
|
|
|
rm -f "$tmp"
|
|
|
|
if runcmd cp "$file" "$tmp" \
|
|
|
|
&& runcmd mv -f "$tmp" "$loc"; then
|
|
|
|
echo TRANSFER-SUCCESS STORE "$key"
|
|
|
|
else
|
|
|
|
echo TRANSFER-FAILURE STORE "$key"
|
|
|
|
fi
|
|
|
|
rmdir "$mydirectory/tmp"
|
|
|
|
}
|
|
|
|
|
|
|
|
doretrieve () {
|
|
|
|
local key="$1"
|
|
|
|
local file="$2"
|
|
|
|
local loc="$3"
|
|
|
|
|
|
|
|
# XXX when easy to do, send PROGRESS while transferring the file
|
|
|
|
if [ -e "$loc" ]; then
|
|
|
|
if runcmd cp "$loc" "$file"; then
|
|
|
|
echo TRANSFER-SUCCESS RETRIEVE "$key"
|
|
|
|
else
|
|
|
|
echo TRANSFER-FAILURE RETRIEVE "$key"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo TRANSFER-FAILURE RETRIEVE "$key"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
docheckpresent () {
|
|
|
|
local key="$1"
|
|
|
|
local loc="$2"
|
|
|
|
|
|
|
|
if [ -e "$loc" ]; then
|
|
|
|
echo CHECKPRESENT-SUCCESS "$key"
|
|
|
|
else
|
|
|
|
if [ -d "$mydirectory" ]; then
|
|
|
|
echo CHECKPRESENT-FAILURE "$key"
|
|
|
|
else
|
|
|
|
# When the directory does not exist,
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
doremove () {
|
|
|
|
local key="$1"
|
|
|
|
local loc="$2"
|
|
|
|
|
|
|
|
# Note that it's not a failure to remove a
|
add LISTCONFIGS to external special remote protocol
Special remote programs that use GETCONFIG/SETCONFIG are recommended
to implement it.
The description is not yet used, but will be useful later when adding a way
to make initremote list all accepted configs.
configParser now takes a RemoteConfig parameter. Normally, that's not
needed, because configParser returns a parter, it does not parse it
itself. But, it's needed to look at externaltype and work out what
external remote program to run for LISTCONFIGS.
Note that, while externalUUID is changed to a Maybe UUID, checkExportSupported
used to use NoUUID. The code that now checks for Nothing used to behave
in some undefined way if the external program made requests that
triggered it.
Also, note that in externalSetup, once it generates external,
it parses the RemoteConfig strictly. That generates a
ParsedRemoteConfig, which is thrown away. The reason it's ok to throw
that away, is that, if the strict parse succeeded, the result must be
the same as the earlier, lenient parse.
initremote of an external special remote now runs the program three
times. First for LISTCONFIGS, then EXPORTSUPPORTED, and again
LISTCONFIGS+INITREMOTE. It would not be hard to eliminate at least
one of those, and it should be possible to only run the program once.
2020-01-17 19:30:14 +00:00
|
|
|
# file that is not present.
|
2017-09-08 18:24:05 +00:00
|
|
|
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-27 06:48:47 +00:00
|
|
|
# This has to come first, to get the protocol started.
|
2023-03-28 21:00:08 +00:00
|
|
|
echo VERSION 2
|
2013-12-26 22:14:52 +00:00
|
|
|
|
|
|
|
while read line; do
|
|
|
|
set -- $line
|
|
|
|
case "$1" in
|
add LISTCONFIGS to external special remote protocol
Special remote programs that use GETCONFIG/SETCONFIG are recommended
to implement it.
The description is not yet used, but will be useful later when adding a way
to make initremote list all accepted configs.
configParser now takes a RemoteConfig parameter. Normally, that's not
needed, because configParser returns a parter, it does not parse it
itself. But, it's needed to look at externaltype and work out what
external remote program to run for LISTCONFIGS.
Note that, while externalUUID is changed to a Maybe UUID, checkExportSupported
used to use NoUUID. The code that now checks for Nothing used to behave
in some undefined way if the external program made requests that
triggered it.
Also, note that in externalSetup, once it generates external,
it parses the RemoteConfig strictly. That generates a
ParsedRemoteConfig, which is thrown away. The reason it's ok to throw
that away, is that, if the strict parse succeeded, the result must be
the same as the earlier, lenient parse.
initremote of an external special remote now runs the program three
times. First for LISTCONFIGS, then EXPORTSUPPORTED, and again
LISTCONFIGS+INITREMOTE. It would not be hard to eliminate at least
one of those, and it should be possible to only run the program once.
2020-01-17 19:30:14 +00:00
|
|
|
LISTCONFIGS)
|
|
|
|
# One CONFIG line for each setting that we GETCONFIG
|
|
|
|
# later.
|
|
|
|
echo CONFIG directory store data here
|
|
|
|
echo CONFIGEND
|
|
|
|
;;
|
2013-12-26 22:14:52 +00:00
|
|
|
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-27 18:30:00 +00:00
|
|
|
|
2013-12-27 20:01:43 +00:00
|
|
|
# The directory provided by the user
|
|
|
|
# could be relative; make it absolute,
|
|
|
|
# and store that.
|
2013-12-26 22:14:52 +00:00
|
|
|
getconfig directory
|
2013-12-27 20:01:43 +00:00
|
|
|
mydirectory="$(readlink -f "$RET")" || true
|
2013-12-27 18:30:00 +00:00
|
|
|
setconfig directory "$mydirectory"
|
2013-12-26 22:14:52 +00:00
|
|
|
if [ -z "$mydirectory" ]; then
|
|
|
|
echo INITREMOTE-FAILURE "You need to set directory="
|
|
|
|
else
|
2013-12-27 18:30:00 +00:00
|
|
|
if mkdir -p "$mydirectory"; then
|
2013-12-27 20:01:43 +00:00
|
|
|
setupcreds
|
2013-12-27 18:30:00 +00:00
|
|
|
else
|
|
|
|
echo INITREMOTE-FAILURE "Failed to write to $mydirectory"
|
|
|
|
fi
|
2013-12-26 22:14:52 +00:00
|
|
|
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.
|
2013-12-29 17:39:25 +00:00
|
|
|
getcreds
|
2013-12-26 22:14:52 +00:00
|
|
|
getconfig directory
|
|
|
|
mydirectory="$RET"
|
2013-12-29 17:39:25 +00:00
|
|
|
if [ -d "$mydirectory" ]; then
|
|
|
|
echo PREPARE-SUCCESS
|
|
|
|
else
|
|
|
|
echo PREPARE-FAILURE "$mydirectory not found"
|
|
|
|
fi
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
TRANSFER)
|
2017-08-17 20:20:09 +00:00
|
|
|
op="$2"
|
2013-12-26 22:14:52 +00:00
|
|
|
key="$3"
|
2017-08-17 20:20:09 +00:00
|
|
|
shift 3
|
|
|
|
file="$@"
|
|
|
|
case "$op" in
|
2013-12-26 22:14:52 +00:00
|
|
|
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
|
|
|
calclocation "$key"
|
2017-09-08 18:24:05 +00:00
|
|
|
dostore "$key" "$file" "$LOC"
|
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-26 22:14:52 +00:00
|
|
|
calclocation "$key"
|
2017-09-08 18:24:05 +00:00
|
|
|
doretrieve "$key" "$file" "$LOC"
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
CHECKPRESENT)
|
|
|
|
key="$2"
|
|
|
|
calclocation "$key"
|
2017-09-08 18:24:05 +00:00
|
|
|
docheckpresent "$key" "$LOC"
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
|
|
|
REMOVE)
|
|
|
|
key="$2"
|
|
|
|
calclocation "$key"
|
2017-09-08 18:24:05 +00:00
|
|
|
doremove "$key" "$LOC"
|
|
|
|
;;
|
|
|
|
# The requests listed above are all the ones
|
|
|
|
# that are required to be supported, so it's fine
|
|
|
|
# to respond to any others with UNSUPPORTED-REQUEST.
|
|
|
|
|
|
|
|
# Let's also support exporting...
|
|
|
|
EXPORTSUPPORTED)
|
|
|
|
echo EXPORTSUPPORTED-SUCCESS
|
|
|
|
;;
|
|
|
|
EXPORT)
|
|
|
|
shift 1
|
|
|
|
exportlocation="$mydirectory/$@"
|
|
|
|
# No response to this one; this value is used below.
|
|
|
|
;;
|
|
|
|
TRANSFEREXPORT)
|
|
|
|
op="$2"
|
|
|
|
key="$3"
|
|
|
|
shift 3
|
|
|
|
file="$@"
|
|
|
|
case "$op" in
|
|
|
|
STORE)
|
|
|
|
# Store the file to the exportlocation
|
|
|
|
dostore "$key" "$file" "$exportlocation"
|
|
|
|
;;
|
|
|
|
RETRIEVE)
|
|
|
|
# Retrieve from the exportlocation,
|
|
|
|
# outputting to the file.
|
|
|
|
doretrieve "$key" "$exportlocation" "$file"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
CHECKPRESENTEXPORT)
|
|
|
|
key="$2"
|
|
|
|
docheckpresent "$key" "$exportlocation"
|
|
|
|
;;
|
|
|
|
REMOVEEXPORT)
|
|
|
|
key="$2"
|
|
|
|
doremove "$key" "$exportlocation"
|
|
|
|
;;
|
2017-09-15 17:15:47 +00:00
|
|
|
REMOVEEXPORTDIRECTORY)
|
|
|
|
shift 1
|
|
|
|
dir="$@"
|
|
|
|
if [ ! -d "$dir" ] || rm -rf "$mydirectory/$dir"; then
|
|
|
|
echo REMOVEEXPORTDIRECTORY-SUCCESS
|
|
|
|
else
|
|
|
|
echo REMOVEEXPORTDIRECTORY-FAILURE
|
|
|
|
fi
|
|
|
|
;;
|
2017-09-08 18:24:05 +00:00
|
|
|
RENAMEEXPORT)
|
|
|
|
key="$2"
|
|
|
|
shift 2
|
|
|
|
newexportlocation="$mydirectory/$@"
|
|
|
|
mkdir -p "$(dirname "$newexportlocation")"
|
|
|
|
if runcmd mv -f "$exportlocation" "$newexportlocation"; then
|
|
|
|
echo RENAMEEXPORT-SUCCESS "$key"
|
2013-12-27 06:48:47 +00:00
|
|
|
else
|
2017-09-08 18:24:05 +00:00
|
|
|
echo RENAMEEXPORT-FAILURE "$key"
|
2013-12-27 06:48:47 +00:00
|
|
|
fi
|
2013-12-26 22:14:52 +00:00
|
|
|
;;
|
2017-09-08 18:24:05 +00:00
|
|
|
|
2018-06-08 15:52:20 +00:00
|
|
|
# This is optional, only provided as an example.
|
|
|
|
GETINFO)
|
|
|
|
echo INFOFIELD "repository location"
|
|
|
|
echo INFOVALUE "$mydirectory"
|
|
|
|
echo INFOFIELD "login"
|
|
|
|
echo INFOVALUE "$MYLOGIN"
|
|
|
|
echo INFOEND
|
|
|
|
;;
|
|
|
|
|
2013-12-26 22:14:52 +00:00
|
|
|
*)
|
2013-12-27 06:08:29 +00:00
|
|
|
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
|