summaryrefslogtreecommitdiff
path: root/doc/special_remotes/external/git-annex-remote-torrent
blob: d7897d7726b1534d4ef2c7f0b1d43a31a0b6f9a4 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
# This is a demo git-annex external special remote program,
# which adds basic torrent download support to git-annex.
#
# Uses aria2c. Also needs the original bittorrent (or bittornado) for the
# btshowmetainfo command.
# 
# Install in PATH as git-annex-remote-torrent
#
# Enable remote by running:
#  git annex initremote torrent type=external encryption=none externaltype=torrent
#  git annex untrust torrent
#
# Copyright 2014 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 response and stores it in $RET
getvalue () {
	read resp
	# Tricky POSIX shell code to split first word of the resp,
	# preserving all other whitespace
	case "${resp%% *}" in
		VALUE)
			RET="$(echo "$resp" | sed 's/^VALUE \?//')"
		;;
		*)
		RET=""
		;;
	esac
}

# Get a list of all known torrent urls for a key,
# storing it in a temp file.
geturls () {
	key="$1"
	tmp="$2"

	echo GETURLS "$key"
	getvalue
	while [ -n "$RET" ]; do
		if istorrent "$RET"; then
			echo "$RET" >> "$tmp"
		fi
		getvalue
	done
}

# Does the url end in .torrent?
# Note that we use #N on the url to indicate which file
# from a multi-file torrent is wanted.
istorrent () {
	echo "$1" | egrep -q "\.torrent(#.*)?$"
}

# Download a single file from a torrent.
#
# Note: Does not support resuming interrupted transfers.
# Note: Does not feed progress info back to git-annex, and since
# the destination file is only populated at the end, git-annex will fail
# to display a progress bar for this download.
downloadtorrent () {
	torrent="$1"
	n="$2"
	dest="$3"

	tmpdir="$(mktemp -d)"

	# aria2c will create part of the directory structure
	# contained in the torrent. It may download parts of other files
	# in addition to the one we asked for. So, we need to find
	# out the filename we want, and look for it.
	wantdir="$(btshowmetainfo "$torrent" | grep "^directory name: " | sed "s/^directory name: //" || true)"
	if [ -n "$wantdir" ]; then
		wantfile="$(btshowmetainfo "$torrent" | grep '^   ' | sed 's/^   //' | head -n "$n" | tail -n 1 | sed 's/ ([0-9]*)$//')"
		if ! runcmd aria2c --select-file="$n" "$torrent" -d "$tmpdir"; then
			false
		fi
	else
		wantfile="$(btshowmetainfo "$torrent" | egrep "^file name.*: " | sed "s/^file name.*: //")"
		wantdir=.
		if ! runcmd aria2c "$torrent" -d "$tmpdir"; then
			false
		fi
	fi
	if [ -e "$tmpdir/$wantdir/$wantfile" ]; then
		mv "$tmpdir/$wantdir/$wantfile" "$dest"
		rm -rf "$tmpdir"
	else
		rm -rf "$tmpdir"
		false
	fi
}

# This has to come first, to get the protocol started.
echo VERSION 1

while read line; do
	set -- $line
	case "$1" in
		INITREMOTE)
			echo INITREMOTE-SUCCESS
		;;
		PREPARE)
			echo PREPARE-SUCCESS
		;;
		CLAIMURL)
			url="$2"
			if istorrent "$url"; then
				echo CLAIMURL-SUCCESS
			else
				echo CLAIMURL-FAILURE
			fi
		;;
		CHECKURL)
			url="$2"
			# List contents of torrent.
			tmp=$(mktemp)
			if ! runcmd curl -o "$tmp" "$url"; then
				echo CHECKURL-FAILURE
			else
				oldIFS="$IFS"
			IFS="
"
				printf "CHECKURL-MULTI"
				n=0
				for l in $(btshowmetainfo "$tmp" | grep '^   ' | sed 's/^   //'); do
					# Note that the file cannot contain spaces.
					file="$(echo "$l" | sed 's/ ([0-9]*)$//' | sed 's/ /_/g')"
					size="$(echo "$l" | sed 's/.* (\([0-9]*\))$/\1/')"
					n=$(expr $n + 1)
					printf " $url#$n $size $file"
				done
				if [ "$n" = 0 ]; then
					file="$(btshowmetainfo "$tmp" | egrep "^file name.*: " | sed "s/^file name.*: //")"
					size="$(btshowmetainfo "$tmp" | egrep "^file size.*: " | sed "s/^file size.*: \([0-9]*\).*/\1/")"
					printf " $url $size $file"
				fi
				printf "\n"
				IFS="$oldIFS"
			fi
			rm -f "$tmp"
		;;
		TRANSFER)
			op="$2"
			key="$3"
			shift 3
			file="$@"
			case "$op" in
				STORE)
					echo TRANSFER-FAILURE STORE "$key" "upload not supported"
				;;
				RETRIEVE)
					urltmp=$(mktemp)
					geturls "$key" "$urltmp"
					url="$(head "$urltmp")" || true
					rm -f "$urltmp"
					if [ -z "$url" ]; then
						echo TRANSFER-FAILURE RETRIEVE "$key" "no known torrent urls for this key"
					else
						tmp=$(mktemp)
						if ! runcmd curl -o "$tmp" "$url"; then
							echo TRANSFER-FAILURE RETRIEVE "$key" "failed downloading torrent file from $url"
						else
							filenum="$(echo "$url" | sed 's/(.*#\(\d*\)/\1/')"
							if downloadtorrent "$tmp" "$filenum" "$file"; then
								echo TRANSFER-SUCCESS RETRIEVE "$key"
							else
								echo TRANSFER-FAILURE RETRIEVE "$key" "failed to download torrent contents from $url"
							fi
						fi
						rm -f "$tmp"					
					fi
				;;
			esac
		;;
		CHECKPRESENT)
			key="$2"
			# Let's just assume that torrents are never present
			# for simplicity.
			echo CHECKPRESENT-UNKNOWN "$key" "cannot reliably check torrent status"
		;;
		REMOVE)
			key="$2"
			# Remove all torrent urls for the key.
			tmp=$(mktemp)
			geturls "$key" "$tmp"
			for url in $(cat "$tmp"); do
				echo SETURLMISSING "$key" "$url"
			done
			rm -f "$tmp"
			echo REMOVE-SUCCESS "$key"
		;;
		*)
			echo UNSUPPORTED-REQUEST
		;;
	esac	
done