Skip to content

Commit

Permalink
fix: _stat_mode can follow symlinks with relative path (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
khsrali authored Jul 16, 2024
1 parent 56ba1f9 commit 6cae414
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions firecrest/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pathlib import PurePosixPath
import stat
import tempfile
from typing import Callable, Iterator
from typing import Callable, Iterator, List

from firecrest import Firecrest, ClientCredentialsAuth
from firecrest.BasicClient import logger as FcLogger
Expand Down Expand Up @@ -310,6 +310,18 @@ def _lstat_mode(self) -> int:
self._cache.lst_mode = item._cache.lst_mode
return item._cache.lst_mode
raise FileNotFoundError(self)

def resolve(self) -> Self:
"""Resolve a path, removing '..' and '.' components."""
parts: List[str] = []
for part in self.parts:
if part == '..':
if parts:
parts.pop()
elif part != '.':
parts.append(part)

return self._new_path(PurePosixPath(*parts))

def _stat_mode(self) -> int:
"""Return the st_mode of the path, following symlinks."""
Expand Down Expand Up @@ -337,7 +349,11 @@ def _stat_mode(self) -> int:
if stat.S_ISLNK(mode):
if not item._cache.link_target:
raise FileNotFoundError(f"Symlink has no target path: {self}")
path = self._new_path(PurePosixPath(item._cache.link_target))
pureposixpath = PurePosixPath(item._cache.link_target)
if not pureposixpath.is_absolute():
path = path.parent.joinpath(pureposixpath).resolve()
else:
path = self._new_path(pureposixpath)
followed_links += 1
break
else:
Expand Down Expand Up @@ -470,6 +486,10 @@ def iterdir(self, hidden=True, recursive=False) -> Iterator[Self]:
),
)

def relpath(self, start: str | PurePosixPath) -> str:
"""Return a relative version of this path."""
return self._path.relative_to(start).as_posix()

# operations that modify a file

def chmod(self, mode: int) -> None:
Expand Down

0 comments on commit 6cae414

Please sign in to comment.