diff options
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/validators/kvm.py | 15 | ||||
-rw-r--r-- | kvmd/validators/os.py | 19 |
2 files changed, 22 insertions, 12 deletions
diff --git a/kvmd/validators/kvm.py b/kvmd/validators/kvm.py index 3a1c437b..df09d06e 100644 --- a/kvmd/validators/kvm.py +++ b/kvmd/validators/kvm.py @@ -20,17 +20,16 @@ # ========================================================================== # -import re - from typing import Any from .. import keymap -from . import check_not_none_string from . import check_string_in_list from .basic import valid_number +from .os import valid_printable_filename + # ===== def valid_atx_power_action(arg: Any) -> str: @@ -42,15 +41,7 @@ def valid_atx_button(arg: Any) -> str: def valid_msd_image_name(arg: Any) -> str: - if len(str(arg).strip()) == 0: - arg = None - arg = check_not_none_string(arg, "MSD image name", strip=True) - arg = re.sub(r"[^\w\.+@()\[\]-]", "_", arg) - if arg == ".": - arg = "_" - if arg == "..": - arg = "__" - return arg[:255] + return valid_printable_filename(arg, name="MSD image name") # pragma: nocover def valid_log_seek(arg: Any) -> int: diff --git a/kvmd/validators/os.py b/kvmd/validators/os.py index 505df8e2..d37ae5d4 100644 --- a/kvmd/validators/os.py +++ b/kvmd/validators/os.py @@ -51,6 +51,25 @@ def valid_abs_path_exists(arg: Any, name: str="") -> str: return valid_abs_path(arg, exists=True, name=name) +def valid_printable_filename(arg: Any, name: str="") -> str: + if not name: + name = "printable filename" + + if len(str(arg).strip()) == 0: + arg = None + arg = check_not_none_string(arg, name) + + if "/" in arg or "\0" in arg or arg in [".", ".."]: + raise_error(arg, name) + + arg = "".join( + (ch if ch.isprintable() else "_") + for ch in arg[:255] + ) + return arg + + +# ===== def valid_unix_mode(arg: Any) -> int: return int(valid_number(arg, min=0, name="UNIX mode")) |