1
0

ConvertedMetainfo.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.1 (the License). You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License. You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. # Written by Uoti Urpala
  11. # required for Python 2.2
  12. from __future__ import generators
  13. import os
  14. import sys
  15. import logging
  16. import urlparse
  17. from BTL.hash import sha
  18. import socket
  19. #debug=True
  20. global_logger = logging.getLogger("BTL.ConvertedMetainfo")
  21. from BTL.translation import _
  22. from BTL.obsoletepythonsupport import *
  23. from BTL.bencode import bencode
  24. from BTL import btformats
  25. from BTL import BTFailure, InfoHashType
  26. from BTL.platform import get_filesystem_encoding, encode_for_filesystem
  27. from BTL.defer import ThreadedDeferred
  28. WINDOWS_UNSUPPORTED_CHARS = u'"*/:<>?\|'
  29. windows_translate = {}
  30. for x in WINDOWS_UNSUPPORTED_CHARS:
  31. windows_translate[ord(x)] = u'-'
  32. noncharacter_translate = {}
  33. for i in xrange(0xD800, 0xE000):
  34. noncharacter_translate[i] = ord('-')
  35. for i in xrange(0xFDD0, 0xFDF0):
  36. noncharacter_translate[i] = ord('-')
  37. for i in (0xFFFE, 0xFFFF):
  38. noncharacter_translate[i] = ord('-')
  39. del x, i
  40. def generate_names(name, is_dir):
  41. if is_dir:
  42. prefix = name + '.'
  43. suffix = ''
  44. else:
  45. pos = name.rfind('.')
  46. if pos == -1:
  47. pos = len(name)
  48. prefix = name[:pos] + '.'
  49. suffix = name[pos:]
  50. i = 0
  51. while True:
  52. yield prefix + str(i) + suffix
  53. i += 1
  54. class ConvertedMetainfo(object):
  55. def __init__(self, metainfo):
  56. """metainfo is a dict. When read from a metainfo (i.e.,
  57. .torrent file), the file must first be bdecoded before
  58. being passed to ConvertedMetainfo."""
  59. self.bad_torrent_wrongfield = False
  60. self.bad_torrent_unsolvable = False
  61. self.bad_torrent_noncharacter = False
  62. self.bad_conversion = False
  63. self.bad_windows = False
  64. self.bad_path = False
  65. self.reported_errors = False
  66. # All of the following values should be considered READONLY.
  67. # Modifications to the metainfo that should be written should
  68. # occur to the underlying metainfo dict directly.
  69. self.is_batch = False
  70. self.orig_files = None
  71. self.files_fs = None
  72. self.total_bytes = 0
  73. self.sizes = []
  74. self.comment = None
  75. self.title = None # descriptive title text for whole torrent
  76. self.creation_date = None
  77. self.metainfo = metainfo
  78. self.encoding = None
  79. self.caches = None
  80. btformats.check_message(metainfo, check_paths=False)
  81. info = metainfo['info']
  82. self.is_private = info.has_key("private") and info['private']
  83. if 'encoding' in metainfo:
  84. self.encoding = metainfo['encoding']
  85. elif 'codepage' in metainfo:
  86. self.encoding = 'cp%s' % metainfo['codepage']
  87. if self.encoding is not None:
  88. try:
  89. for s in u'this is a test', u'these should also work in any encoding: 0123456789\0':
  90. assert s.encode(self.encoding).decode(self.encoding) == s
  91. except:
  92. self.encoding = 'iso-8859-1'
  93. self.bad_torrent_unsolvable = True
  94. if info.has_key('length'):
  95. self.total_bytes = info['length']
  96. self.sizes.append(self.total_bytes)
  97. if info.has_key('content_type'):
  98. self.content_type = info['content_type']
  99. else:
  100. self.content_type = None # hasattr or None. Which is better?
  101. else:
  102. self.is_batch = True
  103. r = []
  104. self.orig_files = []
  105. self.sizes = []
  106. self.content_types = []
  107. i = 0
  108. # info['files'] is a list of dicts containing keys:
  109. # 'length', 'path', and 'content_type'. The 'content_type'
  110. # key is optional.
  111. for f in info['files']:
  112. l = f['length']
  113. self.total_bytes += l
  114. self.sizes.append(l)
  115. self.content_types.append(f.get('content_type'))
  116. path = self._get_attr(f, 'path')
  117. if len(path[-1]) == 0:
  118. if l > 0:
  119. raise BTFailure(_("Bad file path component: ")+x)
  120. # BitComet makes .torrent files with directories
  121. # listed along with the files, which we don't support
  122. # yet, in part because some idiot interpreted this as
  123. # a bug in BitComet rather than a feature.
  124. path.pop(-1)
  125. for x in path:
  126. if not btformats.allowed_path_re.match(x):
  127. raise BTFailure(_("Bad file path component: ")+x)
  128. self.orig_files.append('/'.join(path))
  129. k = []
  130. for u in path:
  131. tf2 = self._to_fs_2(u)
  132. k.append((tf2, u))
  133. r.append((k,i))
  134. i += 1
  135. # If two or more file/subdirectory names in the same directory
  136. # would map to the same name after encoding conversions + Windows
  137. # workarounds, change them. Files are changed as
  138. # 'a.b.c'->'a.b.0.c', 'a.b.1.c' etc, directories or files without
  139. # '.' as 'a'->'a.0', 'a.1' etc. If one of the multiple original
  140. # names was a "clean" conversion, that one is always unchanged
  141. # and the rest are adjusted.
  142. r.sort()
  143. self.files_fs = [None] * len(r)
  144. prev = [None]
  145. res = []
  146. stack = [{}]
  147. for x in r:
  148. j = 0
  149. x, i = x
  150. while x[j] == prev[j]:
  151. j += 1
  152. del res[j:]
  153. del stack[j+1:]
  154. name = x[j][0][1]
  155. if name in stack[-1]:
  156. for name in generate_names(x[j][1], j != len(x) - 1):
  157. name = self._to_fs(name)
  158. if name not in stack[-1]:
  159. break
  160. stack[-1][name] = None
  161. res.append(name)
  162. for j in xrange(j + 1, len(x)):
  163. name = x[j][0][1]
  164. stack.append({name: None})
  165. res.append(name)
  166. self.files_fs[i] = os.path.join(*res)
  167. prev = x
  168. self.name = self._get_attr(info, 'name')
  169. self.name_fs = self._to_fs(self.name)
  170. self.piece_length = info['piece length']
  171. self.announce = metainfo.get('announce')
  172. self.announce_list = metainfo.get('announce-list')
  173. if 'announce-list' not in metainfo and 'announce' not in metainfo:
  174. self.is_trackerless = True
  175. else:
  176. self.is_trackerless = False
  177. self.nodes = metainfo.get('nodes', [('router.bittorrent.com', 6881)])
  178. self.title = metainfo.get('title')
  179. self.comment = metainfo.get('comment')
  180. self.creation_date = metainfo.get('creation date')
  181. self.locale = metainfo.get('locale')
  182. self.safe = metainfo.get('safe')
  183. self.url_list = metainfo.get('url-list', [])
  184. if not isinstance(self.url_list, list):
  185. self.url_list = [self.url_list, ]
  186. self.caches = metainfo.get('caches')
  187. self.hashes = [info['pieces'][x:x+20] for x in xrange(0,
  188. len(info['pieces']), 20)]
  189. self.infohash = InfoHashType(sha(bencode(info)).digest())
  190. def show_encoding_errors(self, errorfunc):
  191. self.reported_errors = True
  192. if self.bad_torrent_unsolvable:
  193. errorfunc(logging.ERROR,
  194. _("This .torrent file has been created with a broken "
  195. "tool and has incorrectly encoded filenames. Some or "
  196. "all of the filenames may appear different from what "
  197. "the creator of the .torrent file intended."))
  198. elif self.bad_torrent_noncharacter:
  199. errorfunc(logging.ERROR,
  200. _("This .torrent file has been created with a broken "
  201. "tool and has bad character values that do not "
  202. "correspond to any real character. Some or all of the "
  203. "filenames may appear different from what the creator "
  204. "of the .torrent file intended."))
  205. elif self.bad_torrent_wrongfield:
  206. errorfunc(logging.ERROR,
  207. _("This .torrent file has been created with a broken "
  208. "tool and has incorrectly encoded filenames. The "
  209. "names used may still be correct."))
  210. elif self.bad_conversion:
  211. errorfunc(logging.WARNING,
  212. _('The character set used on the local filesystem ("%s") '
  213. 'cannot represent all characters used in the '
  214. 'filename(s) of this torrent. Filenames have been '
  215. 'changed from the original.') % get_filesystem_encoding())
  216. elif self.bad_windows:
  217. errorfunc(logging.WARNING,
  218. _("The Windows filesystem cannot handle some "
  219. "characters used in the filename(s) of this torrent. "
  220. "Filenames have been changed from the original."))
  221. elif self.bad_path:
  222. errorfunc(logging.WARNING,
  223. _("This .torrent file has been created with a broken "
  224. "tool and has at least 1 file with an invalid file "
  225. "or directory name. However since all such files "
  226. "were marked as having length 0 those files are "
  227. "just ignored."))
  228. # At least BitComet seems to make bad .torrent files that have
  229. # fields in an unspecified non-utf8 encoding. Some of those have separate
  230. # 'field.utf-8' attributes. Less broken .torrent files have an integer
  231. # 'codepage' key or a string 'encoding' key at the root level.
  232. def _get_attr(self, d, attrib):
  233. def _decode(o, encoding):
  234. if encoding is None:
  235. encoding = 'utf8'
  236. if isinstance(o, str):
  237. try:
  238. s = o.decode(encoding)
  239. except:
  240. self.bad_torrent_wrongfield = True
  241. s = o.decode(encoding, 'replace')
  242. t = s.translate(noncharacter_translate)
  243. if t != s:
  244. self.bad_torrent_noncharacter = True
  245. return t
  246. if isinstance(o, dict):
  247. return dict([ (k, _decode(v, k.endswith('.utf-8') and None or encoding)) for k, v in o.iteritems() ])
  248. if isinstance(o, list):
  249. return [ _decode(i, encoding) for i in o ]
  250. return o
  251. # we prefer utf8 if we can find it. at least it declares its encoding
  252. v = _decode(d.get(attrib + '.utf-8'), 'utf8')
  253. if v is None:
  254. v = _decode(d[attrib], self.encoding)
  255. return v
  256. def _fix_windows(self, name, t=windows_translate):
  257. bad = False
  258. r = name.translate(t)
  259. # for some reason name cannot end with '.' or space
  260. if r[-1] in '. ':
  261. r = r + '-'
  262. if r != name:
  263. self.bad_windows = True
  264. bad = True
  265. return (r, bad)
  266. def _to_fs(self, name):
  267. return self._to_fs_2(name)[1]
  268. def _to_fs_2(self, name):
  269. if sys.platform.startswith('win'):
  270. name, bad = self._fix_windows(name)
  271. r, bad = encode_for_filesystem(name)
  272. self.bad_conversion = bad
  273. return (bad, r)
  274. def to_data(self):
  275. return bencode(self.metainfo)
  276. def check_for_resume(self, path):
  277. """
  278. Determine whether this torrent was previously downloaded to
  279. path. Returns:
  280. -1: STOP! gross mismatch of files
  281. 0: MAYBE a resume, maybe not
  282. 1: almost definitely a RESUME - file contents, sizes, and count match exactly
  283. """
  284. STOP = -1
  285. MAYBE = 0
  286. RESUME = 1
  287. if self.is_batch != os.path.isdir(path):
  288. return STOP
  289. disk_files = {}
  290. if self.is_batch:
  291. metainfo_files = dict(zip(self.files_fs, self.sizes))
  292. metainfo_dirs = set()
  293. for f in self.files_fs:
  294. metainfo_dirs.add(os.path.split(f)[0])
  295. # BUG: do this in a thread, so it doesn't block the UI
  296. for (dirname, dirs, files) in os.walk(path):
  297. here = dirname[len(path)+1:]
  298. for f in files:
  299. p = os.path.join(here, f)
  300. if p in metainfo_files:
  301. disk_files[p] = os.stat(os.path.join(dirname, f))[6]
  302. if disk_files[p] > metainfo_files[p]:
  303. # file on disk that's bigger than the
  304. # corresponding one in the torrent
  305. return STOP
  306. else:
  307. # file on disk that's not in the torrent
  308. return STOP
  309. for i, d in enumerate(dirs):
  310. if d not in metainfo_dirs:
  311. # directory on disk that's not in the torrent
  312. return STOP
  313. else:
  314. if os.access(path, os.F_OK):
  315. disk_files[self.name_fs] = os.stat(path)[6]
  316. metainfo_files = {self.name_fs : self.sizes[0]}
  317. if len(disk_files) == 0:
  318. # no files on disk, definitely not a resume
  319. return STOP
  320. if set(disk_files.keys()) != set(metainfo_files.keys()):
  321. # check files
  322. if len(metainfo_files) > len(disk_files):
  323. #file in the torrent that's not on disk
  324. return MAYBE
  325. else:
  326. # check sizes
  327. ret = RESUME
  328. for f, s in disk_files.iteritems():
  329. if disk_files[f] < metainfo_files[f]:
  330. # file on disk that's smaller than the
  331. # corresponding one in the torrent
  332. ret = MAYBE
  333. else:
  334. # file sizes match exactly
  335. continue
  336. return ret
  337. def get_tracker_ips(self, wrap_task):
  338. """Returns the list of tracker IP addresses or the empty list if the
  339. torrent is trackerless. This extracts the tracker ip addresses
  340. from the urls in the announce or announce list."""
  341. df = ThreadedDeferred(wrap_task, self._get_tracker_ips, daemon=True)
  342. return df
  343. def _get_tracker_ips(self):
  344. if hasattr(self, "_tracker_ips"): # cache result.
  345. return self._tracker_ips
  346. if self.announce is not None:
  347. urls = [self.announce]
  348. elif self.announce_list is not None: # list of lists.
  349. urls = []
  350. for ulst in self.announce_list:
  351. urls.extend(ulst)
  352. else: # trackerless
  353. assert self.is_trackerless
  354. return []
  355. tracker_ports = [urlparse.urlparse(url)[1] for url in urls]
  356. trackers = [tp.split(':')[0] for tp in tracker_ports]
  357. self._tracker_ips = []
  358. for t in trackers:
  359. try:
  360. ip_list = socket.gethostbyname_ex(t)[2]
  361. self._tracker_ips.extend(ip_list)
  362. except socket.gaierror:
  363. global_logger.error( _("Cannot find tracker with name %s") % t )
  364. return self._tracker_ips