summaryrefslogtreecommitdiff
path: root/Remote/BitTorrent.hs
blob: aaedcd0ef00b2628a3ed16d60f1177589e0a9bf9 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{- BitTorrent remote.
 -
 - Copyright 2014 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Remote.BitTorrent (remote) where

import Common.Annex
import Types.Remote
import qualified Annex
import qualified Git
import qualified Git.Construct
import Config.Cost
import Logs.Web
import Logs.Trust.Basic
import Types.TrustLevel
import Types.UrlContents
import Types.CleanupActions
import Utility.Metered
import Utility.Tmp
import Backend.URL
import Annex.Perms
import qualified Annex.Url as Url

import qualified Data.Map as M
import Network.URI

-- Dummy uuid for bittorrent. Do not alter.
bitTorrentUUID :: UUID
bitTorrentUUID = UUID "00000000-0000-0000-0000-000000000002"

remote :: RemoteType
remote = RemoteType {
	typename = "bittorrent",
	enumerate = list,
	generate = gen,
	setup = error "not supported"
}

-- There is only one bittorrent remote, and it always exists.
list :: Annex [Git.Repo]
list = do
	r <- liftIO $ Git.Construct.remoteNamed "bittorrent" Git.Construct.fromUnknown
	return [r]

gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
gen r _ c gc = 
	return $ Just Remote
		{ uuid = bitTorrentUUID
		, cost = expensiveRemoteCost
		, name = Git.repoDescribe r
		, storeKey = uploadKey
		, retrieveKeyFile = downloadKey
		, retrieveKeyFileCheap = downloadKeyCheap
		, removeKey = dropKey
		, checkPresent = checkKey
		, checkPresentCheap = False
		, whereisKey = Nothing
		, remoteFsck = Nothing
		, repairRepo = Nothing
		, config = c
		, gitconfig = gc
		, localpath = Nothing
		, repo = r
		, readonly = True
		, availability = GloballyAvailable
		, remotetype = remote
		, mkUnavailable = return Nothing
		, getInfo = return []
		, claimUrl = Just (pure . isSupportedUrl)
		, checkUrl = Just checkTorrentUrl
		}

downloadKey :: Key -> AssociatedFile -> FilePath -> MeterUpdate -> Annex Bool
downloadKey key _file dest p = do
	defaultUnTrusted
	get . map (torrentUrlNum . fst . getDownloader) =<< getBitTorrentUrls key
  where
	get [] = do
		warning "no known torrent url"
		return False
	get urls = do
		showOutput -- make way for download progress bar
		untilTrue urls $ \(u, filenum) -> do
			registerTorrentCleanup u
			checkDependencies
			unlessM (downloadTorrentFile u) $
				error "could not download torrent file"
			downloadTorrentContent u dest filenum p

downloadKeyCheap :: Key -> FilePath -> Annex Bool
downloadKeyCheap _ _ = return False

uploadKey :: Key -> AssociatedFile -> MeterUpdate -> Annex Bool
uploadKey _ _ _ = do
	warning "upload to bittorrent not supported"
	return False

dropKey :: Key -> Annex Bool
dropKey k = do
	mapM_ (setUrlMissing bitTorrentUUID k) =<< getBitTorrentUrls k
	return True

{- This is a very poor check, but checking if a torrent has enough seeders
 - with all the pieces etc is quite hard.. and even if implemented, it
 - tells us nothing about the later state of the torrent.
 -
 - This is why this remote needs to default to untrusted!
 -}
checkKey :: Key -> Annex Bool
checkKey key = not . null <$> getBitTorrentUrls key

-- Makes this remote UnTrusted, unless it already has a trust set.
defaultUnTrusted :: Annex ()
defaultUnTrusted = whenM (isNothing . M.lookup bitTorrentUUID <$> trustMapRaw) $
	trustSet bitTorrentUUID UnTrusted

getBitTorrentUrls :: Key -> Annex [URLString]
getBitTorrentUrls key = filter supported <$> getUrls key
  where
	supported u =
		let (u', dl) = (getDownloader u)
		in dl == OtherDownloader && isSupportedUrl u'

isSupportedUrl :: URLString -> Bool
isSupportedUrl u = isTorrentMagnetUrl u || isTorrentUrl u

isTorrentUrl :: URLString -> Bool
isTorrentUrl = maybe False (\u -> ".torrent" `isSuffixOf` uriPath u) . parseURI

isTorrentMagnetUrl :: URLString -> Bool
isTorrentMagnetUrl u = "magnet:" `isPrefixOf` u && checkbt (parseURI u)
  where
	checkbt (Just uri) | "xt=urn:btih:" `isInfixOf` uriQuery uri = True
	checkbt _ = False

checkTorrentUrl :: URLString -> Annex UrlContents
checkTorrentUrl u = do
	checkDependencies
	registerTorrentCleanup u
	ifM (downloadTorrentFile u)
		( torrentContents u
		, error "could not download torrent file"
		)

{- To specify which file inside a multi-url torrent, the file number is
 - appended to the url. -}
torrentUrlWithNum :: URLString -> Int -> URLString
torrentUrlWithNum u n = u ++ "#" ++ show n

torrentUrlNum :: URLString -> (URLString, Int)
torrentUrlNum u =
	let (n, ru) = separate (== '#') (reverse u)
	in (reverse ru, fromMaybe 1 $ readish $ reverse n)

{- A Key corresponding to the URL of a torrent file. -}
torrentUrlKey :: URLString -> Annex Key
torrentUrlKey u = fromUrl (fst $ torrentUrlNum u) Nothing

{- Temporary directory used to download a torrent. -}
tmpTorrentDir :: URLString -> Annex FilePath
tmpTorrentDir u = do
	d <- fromRepo gitAnnexTmpMiscDir
	f <- keyFile <$> torrentUrlKey u
	return (d </> f)

{- Temporary filename to use to store the torrent file. -}
tmpTorrentFile :: URLString -> Annex FilePath
tmpTorrentFile u = fromRepo . gitAnnexTmpObjectLocation =<< torrentUrlKey u

{- A cleanup action is registered to delete the torrent file and its
 - associated temp directory when git-annex exits.
 -
 - This allows multiple actions that use the same torrent file and temp
 - directory to run in a single git-annex run.
 -}
registerTorrentCleanup :: URLString -> Annex ()
registerTorrentCleanup u = Annex.addCleanup (TorrentCleanup u) $ do
	liftIO . nukeFile =<< tmpTorrentFile u
	d <- tmpTorrentDir u
	liftIO $ whenM (doesDirectoryExist d) $
		removeDirectoryRecursive d

{- Downloads the torrent file. (Not its contents.) -}
downloadTorrentFile :: URLString -> Annex Bool
downloadTorrentFile u = do
	torrent <- tmpTorrentFile u
	ifM (liftIO $ doesFileExist torrent)
		( return True
		, do
			showAction "downloading torrent file"
			showOutput
			createAnnexDirectory (parentDir torrent)
			if isTorrentMagnetUrl u
				then do
					tmpdir <- tmpTorrentDir u
					let metadir = tmpdir </> "meta"
					createAnnexDirectory metadir
					ok <- downloadMagnetLink u metadir torrent
					liftIO $ removeDirectoryRecursive metadir
					return ok
				else do
					misctmp <- fromRepo gitAnnexTmpMiscDir
					withTmpFileIn misctmp "torrent" $ \f _h -> do
						ok <- Url.withUrlOptions $ Url.download u f
						when ok $
							liftIO $ renameFile f torrent
						return ok
		)

downloadMagnetLink :: URLString -> FilePath -> FilePath -> Annex Bool
downloadMagnetLink u metadir dest = ifM download
	( liftIO $ do
		ts <- filter (".torrent" `isPrefixOf`)
			<$> dirContents metadir
		case ts of
			(t:[]) -> do
				renameFile t dest
				return True
			_ -> return False
	, return False
	)
  where
	download = runAria
		[ Param "--bt-metadata-only"
		, Param "--bt-save-metadata"
		, Param u
		, Param "--seed-time=0"
		, Param "-d"
		, File metadir
		]

downloadTorrentContent :: URLString -> FilePath -> Int -> MeterUpdate -> Annex Bool
downloadTorrentContent u dest filenum p = do
	torrent <- tmpTorrentFile u
	tmpdir <- tmpTorrentDir u
	createAnnexDirectory tmpdir
	f <- wantedfile torrent
	showOutput
	ifM (download torrent tmpdir <&&> liftIO (doesFileExist (tmpdir </> f)))
		( do
			liftIO $ renameFile (tmpdir </> f) dest
			return True
		, return False
		)
  where
	-- TODO parse aria's output and update progress meter
	download torrent tmpdir = runAria
		[ Param $ "--select-file=" ++ show filenum
		, File torrent
		, Param "-d"
		, File tmpdir
		, Param "--seed-time=0"
		]
	
	{- 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 based on the filenum.
	 -}
	wantedfile torrent = do
		fs <- liftIO $ map fst <$> torrentFileSizes torrent
		if length fs >= filenum
			then return (fs !! filenum)
			else error "Number of files in torrent seems to have changed."

checkDependencies :: Annex ()
checkDependencies = do
	missing <- liftIO $ filterM (not <$$> inPath) ["aria2c", "btshowmetainfo"]
	unless (null missing) $
		error $ "need to install additional software in order to download from bittorrent: " ++ unwords missing

runAria :: [CommandParam] -> Annex Bool
runAria ps = do
	opts <- map Param . annexAriaTorrentOptions <$> Annex.getGitConfig
	liftIO $ boolSystem "aria2c" (ps ++ opts)

btshowmetainfo :: FilePath -> String -> IO [String]
btshowmetainfo torrent field = 
	findfield [] . lines <$> readProcess "btshowmetainfo" [torrent]
  where
	findfield c [] = reverse c
	findfield c (l:ls)
		| l == fieldkey = multiline c ls
		| fieldkey `isPrefixOf` l =
			findfield ((drop (length fieldkey) l):c) ls
		| otherwise = findfield c ls

	multiline c (l:ls)
		| "   " `isPrefixOf` l = multiline (drop 3 l:c) ls
		| otherwise = findfield c ls
	multiline c [] = findfield c []

	fieldkey = field ++ take (14 - length field) (repeat '.') ++ ": "

{- Examines the torrent file and gets the list of files in it,
 - and their sizes.
 -}
torrentFileSizes :: FilePath -> IO [(FilePath, Integer)]
torrentFileSizes torrent = do
	files <- getfield "files"
	if null files
		then do
			fnl <- getfield "file name"
			szl <- map readish <$> getfield "file size"
			case (fnl, szl) of
				((fn:[]), (Just sz:[])) -> return [(scrub fn, sz)]
		 		_ -> parsefailed (show (fnl, szl))
		else do
			v <- btshowmetainfo torrent "directory name"
			case v of
				(d:[]) -> return $ map (splitsize d) files
				_ -> parsefailed (show v)
  where
	getfield = btshowmetainfo torrent

	parsefailed s = error $ "failed to parse btshowmetainfo output for torrent file: " ++ show s

	-- btshowmetainfo outputs a list of "filename (size)"
	splitsize d l = (scrub (d </> fn), sz)
	  where
		sz = fromMaybe (parsefailed l) $ readish $ 
			reverse $ takeWhile (/= '(') $ dropWhile (== ')') $
				reverse l
		fn = reverse $ drop 2 $
			dropWhile (/= '(') $ dropWhile (== ')') $ reverse l

	scrub f = if isAbsolute f || any (== "..") (splitPath f)
		then error "found unsafe filename in torrent!"
		else f

torrentContents :: URLString -> Annex UrlContents
torrentContents u = convert
	<$> (liftIO . torrentFileSizes =<< tmpTorrentFile u)
  where
	convert [(fn, sz)] = UrlContents (Just sz) (Just (mkSafeFilePath fn))
	convert l = UrlMulti $ map mkmulti (zip l [1..])

	mkmulti ((fn, sz), n) = 
		(torrentUrlWithNum u n, Just sz, mkSafeFilePath fn)