Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed https://github.com/ioflo/hio/issues/27 by adding check with os.path.isabs to name and base components #40

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/hio/base/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import tempfile
from contextlib import contextmanager


from .. import hioing
from . import doing
from .. import help
from ..help.helping import ocfn
Expand Down Expand Up @@ -234,6 +234,9 @@ def remake(self, *, name="", base="", temp=None, headDirPath=None, perm=None,
mode (str): file open mode when .filed such as "w+"
fext (str): File extension when .filed
"""
if os.path.isabs(base):
raise hioing.FilerError(f"Invalid {base=} not relative path.")

file = None
temp = True if temp else False

Expand All @@ -255,6 +258,9 @@ def remake(self, *, name="", base="", temp=None, headDirPath=None, perm=None,
if not ext:
name = f"{name}.{fext}"

if os.path.isabs(name):
raise hioing.FilerError(f"Invalid {name=} not relative path.")

if temp:
headDirPath = tempfile.mkdtemp(prefix=self.TempPrefix,
suffix=self.TempSuffix,
Expand Down Expand Up @@ -381,6 +387,8 @@ def exists(self, name="", base="", headDirPath=None, clean=False, filed=False, f


"""
if os.path.isabs(base):
raise hioing.FilerError(f"Invalid {base=} not relative path.")

# use class defaults here so can use makePath for other dirs and files
if headDirPath is None:
Expand All @@ -397,6 +405,9 @@ def exists(self, name="", base="", headDirPath=None, clean=False, filed=False, f
if not ext:
name = f"{name}.{fext}"

if os.path.isabs(name):
raise hioing.FilerError(f"Invalid {name=} not relative path.")

path = os.path.abspath(
os.path.expanduser(
os.path.join(headDirPath,
Expand Down
7 changes: 7 additions & 0 deletions src/hio/core/wiring.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import shutil
from contextlib import contextmanager

from .. import hioing
from ..base import doing

@contextmanager
Expand Down Expand Up @@ -159,6 +160,9 @@ def reopen(self, rxed=None, txed=None, samed=None, filed=None, fmt=None,
if self.opened:
self.close(clear=clear)

if self.prefix and os.path.isabs(self.prefix):
raise hioing.FilerError(f"Invalid .prefix={self.prefix} not relative path.")

# check for changes in creation parameters if need to recreate
if rxed is not None and rxed == self.rxed:
rxed = None # don't need to recreate because of rxed change
Expand All @@ -177,6 +181,9 @@ def reopen(self, rxed=None, txed=None, samed=None, filed=None, fmt=None,
if headDirPath is not None and headDirPath == self.headDirPath:
headDirPath = None # don't need to recreate path because of headDirPath change

if name and os.path.isabs(name):
raise hioing.FilerError(f"Invalid {name=} not relative path.")

# always recreates if dirPath is empty or if some creation parameter has changed
if (not self.dirPath or
rxed is not None or
Expand Down
8 changes: 8 additions & 0 deletions src/hio/hioing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class OglerError(HioError):
raise OglerError("error message")
"""

class FilerError(HioError):
"""
Error using or configuring Filer

Usage:
raise FilerError("error message")
"""


class Mixin():
"""
Expand Down
9 changes: 9 additions & 0 deletions tests/base/test_filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
import os

from hio import hioing
from hio.base import doing
from hio.base import filing

Expand Down Expand Up @@ -299,6 +300,13 @@ def test_filing():
assert not os.path.exists(filer.path) # not temp but clear=True


# Test bad file path components
with pytest.raises(hioing.FilerError):
filer = filing.Filer(name="/test", base="conf", filed=True)

with pytest.raises(hioing.FilerError):
filer = filing.Filer(name="test", base="/conf", filed=True)

"""Done Test"""


Expand Down Expand Up @@ -426,4 +434,5 @@ def test_filer_doer():

if __name__ == "__main__":
test_filing()
test_filer_doer()

Loading