Skip to content

Commit

Permalink
Raise on file paths ending with separator only for specific methods
Browse files Browse the repository at this point in the history
- fixes #366
  • Loading branch information
mrbean-bremen committed Apr 9, 2018
1 parent 2cab8af commit db379fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
29 changes: 18 additions & 11 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1627,17 +1627,16 @@ def exists(self, file_path, check_link=False):
raise TypeError
if not file_path:
return False
ends_with_sep = self.ends_with_path_separator(file_path)
try:
if (self.ends_with_path_separator(file_path) and
self.isfile(file_path)):
return False
file_path = self.resolve_path(file_path)
except (IOError, OSError):
return False
if file_path == self.root.name:
return True

# the path separator is removed by resolve_path, so we handle it here
if ends_with_sep and self.isfile(file_path):
return False
path_components = self._path_components(file_path)
current_dir = self.root
for component in path_components:
Expand Down Expand Up @@ -1862,14 +1861,18 @@ def get_object(self, file_path):
file_path = self.absnormpath(self._original_path(file_path))
return self.get_object_from_normpath(file_path)

def resolve(self, file_path, follow_symlinks=True, allow_fd=False):
def resolve(self, file_path, follow_symlinks=True, allow_fd=False,
allow_trailing_separator=True):
"""Search for the specified filesystem object, resolving all links.
Args:
file_path: Specifies the target FakeFile object to retrieve.
follow_symlinks: If `False`, the link itself is resolved,
otherwise the object linked to.
allow_fd: If `True`, `file_path` may be an open file descriptor
allow_trailing_separator: If False and file_path is ending with
a separator and not pointing to a directory, raises OSError
under Posix.
Returns:
The FakeFile object corresponding to `file_path`.
Expand All @@ -1886,16 +1889,19 @@ def resolve(self, file_path, follow_symlinks=True, allow_fd=False):
if follow_symlinks:
file_path = make_string_path(file_path)
return self.get_object_from_normpath(self.resolve_path(file_path))
return self.lresolve(file_path)
return self.lresolve(file_path, allow_trailing_separator)

def lresolve(self, path):
def lresolve(self, path, allow_trailing_separator=True):
"""Search for the specified object, resolving only parent links.
This is analogous to the stat/lstat difference. This resolves links
*to* the object but not of the final object itself.
Args:
path: Specifies target FakeFile object to retrieve.
allow_trailing_separator: If False and file_path is ending with
a separator and not pointing to a directory, raises OSError
under Posix.
Returns:
The FakeFile object corresponding to path.
Expand Down Expand Up @@ -1925,8 +1931,8 @@ def lresolve(self, path):
self.raise_io_error(errno.ENOTDIR, path)
self.raise_io_error(errno.ENOENT, path)
obj = parent_obj.get_entry(child_name)
if (not self.is_windows_fs and ends_with_sep and
not isinstance(obj, FakeDirectory)):
if (not self.is_windows_fs and not allow_trailing_separator and
ends_with_sep and not isinstance(obj, FakeDirectory)):
self.raise_os_error(errno.EINVAL, path)
return obj
except KeyError:
Expand Down Expand Up @@ -2491,7 +2497,7 @@ def readlink(self, path):
if path is None:
raise TypeError
try:
link_obj = self.lresolve(path)
link_obj = self.lresolve(path, allow_trailing_separator=False)
except IOError as exc:
self.raise_os_error(exc.errno, path)
if S_IFMT(link_obj.st_mode) != S_IFLNK:
Expand Down Expand Up @@ -2599,7 +2605,8 @@ def _is_of_type(self, path, st_flag, follow_symlinks=True):
if path is None:
raise TypeError
try:
obj = self.resolve(path, follow_symlinks)
obj = self.resolve(path, follow_symlinks,
allow_trailing_separator=False)
if obj:
return S_IFMT(obj.st_mode) == st_flag
except (IOError, OSError):
Expand Down
8 changes: 8 additions & 0 deletions tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ def test_read_link_ending_with_sep_posix(self):
self.assert_raises_os_error(errno.EINVAL,
self.os.readlink, link_path + self.os.sep)

def test_lstat_symlink_with_trailing_sep(self):
# regression test for #366
self.skip_if_symlink_not_supported()
link_path = self.make_path('foo')
self.os.symlink(self.base_path, link_path)
# used to raise
self.assertTrue(self.os.lstat(link_path + self.os.sep).st_mode)

def test_read_link_ending_with_sep_windows(self):
self.check_windows_only()
self.skip_if_symlink_not_supported()
Expand Down

0 comments on commit db379fc

Please sign in to comment.