diff --git a/src/hio/base/filing.py b/src/hio/base/filing.py index ff80798..fdd423a 100644 --- a/src/hio/base/filing.py +++ b/src/hio/base/filing.py @@ -9,7 +9,7 @@ import tempfile from contextlib import contextmanager - +from .. import hioing from . import doing from .. import help from ..help.helping import ocfn @@ -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 @@ -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, @@ -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: @@ -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, diff --git a/src/hio/core/wiring.py b/src/hio/core/wiring.py index 133dc62..af77ff3 100644 --- a/src/hio/core/wiring.py +++ b/src/hio/core/wiring.py @@ -11,6 +11,7 @@ import shutil from contextlib import contextmanager +from .. import hioing from ..base import doing @contextmanager @@ -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 @@ -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 diff --git a/src/hio/hioing.py b/src/hio/hioing.py index db56f0c..1bcb4fd 100644 --- a/src/hio/hioing.py +++ b/src/hio/hioing.py @@ -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(): """ diff --git a/tests/base/test_filing.py b/tests/base/test_filing.py index ed8dc70..ed56a40 100644 --- a/tests/base/test_filing.py +++ b/tests/base/test_filing.py @@ -8,6 +8,7 @@ import pytest import os +from hio import hioing from hio.base import doing from hio.base import filing @@ -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""" @@ -426,4 +434,5 @@ def test_filer_doer(): if __name__ == "__main__": test_filing() + test_filer_doer()