Skip to content

Commit 8571aa5

Browse files
committed
refactor source and destination path handling of Fstab
1 parent ea3486f commit 8571aa5

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

libioc/Config/Jail/File/Fstab.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
import libioc.Config.Jail.File
3636

3737

38+
class FstabFsSpec(libioc.Types.AbsolutePath):
39+
"""Enforces an AbsolutePath or special device name."""
40+
41+
PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9]*$")
42+
43+
def __init__(self, sequence: str) -> None:
44+
if self.PATTERN.match(sequence) is not None:
45+
self = str(sequence) # type: ignore
46+
else:
47+
super().__init__(sequence)
48+
49+
3850
class FstabLine(dict):
3951
"""Model a line of an fstab file."""
4052

@@ -76,20 +88,10 @@ def __setitem__(
7688
value: typing.Union[str, libioc.Types.AbsolutePath]
7789
) -> None:
7890
"""Set an item of the FstabLine."""
79-
_type = None
8091
if key == "source":
81-
_type = libioc.Types.Path
92+
dict.__setitem__(self, key, FstabFsSpec(value))
8293
elif key == "destination":
83-
_type = libioc.Types.AbsolutePath
84-
85-
if _type is not None: # source or destination
86-
if isinstance(value, str) is True:
87-
absolute_path = _type(value)
88-
elif isinstance(value, _type) is True:
89-
absolute_path = value
90-
else:
91-
raise ValueError("String or AbsolutePath expected")
92-
dict.__setitem__(self, key, absolute_path)
94+
dict.__setitem__(self, key, libioc.Types.AbsolutePath(value))
9395
elif key in ["type", "options", "dump", "passnum", "comment"]:
9496
dict.__setitem__(self, key, value)
9597
else:

0 commit comments

Comments
 (0)