Skip to content

Commit

Permalink
fix for tarfs '.' exception
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed May 5, 2019
1 parent 5f0a475 commit d7480b0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Restored deprecated `setfile` method with deprecation warning to change to `writefile`
- Fixed exception when a tarfile contains a path called '.' https://github.com/PyFilesystem/pyfilesystem2/issues/275

## [2.4.4] - 2019-02-23

Expand Down
4 changes: 4 additions & 0 deletions fs/osfs.py
Expand Up @@ -143,6 +143,10 @@ def __init__(
"virtual": False,
}

if platform.system() == "Darwin":
# Test doesn't work on OSX
del _meta["case_insensitive"]

if _WINDOWS_PLATFORM: # pragma: no cover
_meta["invalid_path_chars"] = (
"".join(six.unichr(n) for n in range(31)) + '\\:*?"<>|'
Expand Down
18 changes: 14 additions & 4 deletions fs/tarfs.py
Expand Up @@ -20,7 +20,7 @@
from .info import Info
from .iotools import RawWrapper
from .opener import open_fs
from .path import dirname, relpath, basename, isbase, parts, frombase
from .path import dirname, relpath, basename, isbase, normpath, parts, frombase
from .wrapfs import WrapFS
from .permissions import Permissions

Expand Down Expand Up @@ -272,9 +272,19 @@ def __init__(self, file, encoding="utf-8"):
else:
self._tar = tarfile.open(fileobj=file, mode="r")

self._directory = OrderedDict(
(relpath(self._decode(info.name)).rstrip("/"), info) for info in self._tar
)
self._directory_cache = None

@property
def _directory(self):
"""Lazy directory cache."""
if self._directory_cache is None:
_decode = self._decode
self._directory_cache = OrderedDict(
(_decode(info.name).strip("/"), info)
for info in self._tar
if normpath(info.name)
)
return self._directory_cache

def __repr__(self):
# type: () -> Text
Expand Down
24 changes: 24 additions & 0 deletions tests/test_tarfs.py
Expand Up @@ -188,6 +188,30 @@ def test_getinfo(self):
self.assertTrue(top.get("tar", "is_file"))


class TestBrokenDir(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.tmpfs = open_fs("temp://tarfstest")

@classmethod
def tearDownClass(cls):
cls.tmpfs.close()

def setUp(self):
self.tempfile = self.tmpfs.open("test.tar", "wb+")
with tarfile.open(mode="w", fileobj=self.tempfile) as tf:
tf.addfile(tarfile.TarInfo("."), io.StringIO)
self.tempfile.seek(0)
self.fs = tarfs.TarFS(self.tempfile)

def tearDown(self):
self.fs.close()
self.tempfile.close()

def test_listdir(self):
self.assertEqual(self.fs.listdir("/"), [])


class TestImplicitDirectories(unittest.TestCase):
"""Regression tests for #160.
"""
Expand Down

0 comments on commit d7480b0

Please sign in to comment.