summaryrefslogtreecommitdiff
path: root/doc/special_remotes/external/example.sh
blob: 53a00a557388dd4f9e5a66c013267af1d612cba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/sh
# git-annex external special remote program
# 
# This is basically the same as git-annex's built-in directory special remote.
# 
# Install in PATH as git-annex-remote-directorya
#
# 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 () {
	echo GETCONFIG "$1"
	read resp
	set -- $resp
	case "$1" in
		VALUE)
			RET="$2"
		;;
		*)
			RET=""
		;;
	esac
}

# Sets LOC to the location to use to store a key.
mylocation () {
	echo HASHDIR "$1"
	read resp
	set -- $resp
	case "$1" in
		VALUE)
			LOC="$hashdir/$1"
		;;
		*)
			LOC=
		;;
	esac
}

echo VERSION 1

while read line; do
	set -- $line
	case "$1" in
		INITREMOTE)
			# XXX 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
			# configuration settings.
			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)
			# XXX Use GETCONFIG to get configuration settings,
			# and do anything needed to get ready for using the
			# special remote here.
			getconfig directory
			mydirectory="$RET"
			echo PREPARE-SUCCESS
		;;
		TRANSFER)
			key="$3"
			file="$4"
			case "$2" in
				STORE)
					# XXX upload file here
					# XXX when possible, send PROGRESS
					calclocation "$key"
					mkdir -p "$(dirname "$LOC")"
					runcmd cp -v "$file" "$LOC"
					echo TRANSFER-SUCCESS STORE "$key"
				;;
				RETRIEVE)
					# XXX download file here
					calclocation "$key"
					runcmd cp -v "$LOC" "$file"
					echo TRANSFER-SUCCESS RETRIEVE "$key"
				;;
			esac
		;;
		CHECKPRESENT)
			key="$2"
			calclocation "$key"
			if [ -e "$LOC" ]; then
				echo CHECKPRESENT-SUCCESS "$key"
			else
				if [ -d "$mydirectory" ]; then
					echo CHECKPRESENT-FAILURE "$key"
				else
					# If 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
		;;
		REMOVE)
			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"
		;;
		*)
			# 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
		;;
	esac	
done

# XXX anything that needs to be done at shutdown can be done here