| @@ -1,5 +1,6 @@ | |||||
| from . import bencode | from . import bencode | ||||
| import fnmatch | |||||
| from functools import reduce | from functools import reduce | ||||
| from hashlib import sha1 | from hashlib import sha1 | ||||
| import importlib.resources | import importlib.resources | ||||
| @@ -15,6 +16,11 @@ _encoding = 'utf-8' | |||||
| __all__ = [ 'validate', 'validate_file' ] | __all__ = [ 'validate', 'validate_file' ] | ||||
| _escapes = '*?[]' | |||||
| def glob_escape(s): | |||||
| return ''.join(x if x not in _escapes else '[%s]' % x for x in s) | |||||
| class Storage: | class Storage: | ||||
| '''A class to help read pieces of a torrent. | '''A class to help read pieces of a torrent. | ||||
| ''' | ''' | ||||
| @@ -123,7 +129,9 @@ def validate_file(fname): | |||||
| with open(fname, 'rb') as fp: | with open(fname, 'rb') as fp: | ||||
| torrent = bencode.bdecode(fp.read()) | torrent = bencode.bdecode(fp.read()) | ||||
| dirname = list(fname.parent.rglob(torrent['info']['name'].decode(_encoding)))[0] | |||||
| finddname = glob_escape(torrent['info']['name'].decode(_encoding)) | |||||
| dirname = list(fname.parent.rglob(finddname))[0] | |||||
| tordir = dirname.parent | tordir = dirname.parent | ||||
| @@ -244,6 +252,12 @@ class _TestCases(unittest.TestCase): | |||||
| self.assertFalse(bad) | self.assertFalse(bad) | ||||
| def test_escapeglob(self): | |||||
| for i in [ | |||||
| '*', '?', '[', '[]', '*?[][[*?', | |||||
| ]: | |||||
| self.assertTrue(fnmatch.fnmatch(i, glob_escape(i))) | |||||
| def test_verification(self): | def test_verification(self): | ||||
| # Testing for "missing" files | # Testing for "missing" files | ||||
| # piece size 2 (aka 4 bytes) | # piece size 2 (aka 4 bytes) | ||||