Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 10 additions & 0 deletions flow/record/fieldtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from flow.record.base import FieldType, Record

RE_NORMALIZE_PATH = re.compile(r"[\\/]+")
RE_WINDOWS_PATH = re.compile(r"^[a-zA-Z]:[\\/]")

UTC = timezone.utc

Expand Down Expand Up @@ -602,13 +603,22 @@ def _is_posixlike_path(path: Any) -> bool:
if isinstance(path, pathlib.PurePath):
obj = getattr(path, "parser", None) or path._flavour
return "\\" not in (obj.sep, obj.altsep)
if isinstance(path, str):
if RE_WINDOWS_PATH.match(path):
return False
return "/" in path and "\\" not in path
return False


def _is_windowslike_path(path: Any) -> bool:
if isinstance(path, pathlib.PurePath):
obj = getattr(path, "parser", None) or path._flavour
return "\\" in (obj.sep, obj.altsep)
if isinstance(path, str):
if RE_WINDOWS_PATH.match(path):
return True
if "\\" in path:
return True
return False


Expand Down
10 changes: 8 additions & 2 deletions tests/fieldtypes/test_fieldtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ class PureCustomPath(pathlib.PurePath):
(custom_pure_path(sep="/", altsep="")("/foo/bar"), True),
(custom_pure_path(sep="\\", altsep="/")(r"C:\foo\bar"), False),
(custom_pure_path(sep=":", altsep="\\")(r"C:\foo\bar"), False),
("/foo/bar", False),
("/foo/bar", True),
(r"C:\foo\bar", False),
(r"C:/foo/bar", False),
(r"\??\C:\Windows\System32\calc.exe", False),
],
)
def test__is_posixlike_path(path_: pathlib.PurePath | str, is_posix: bool) -> None:
Expand All @@ -594,6 +597,9 @@ def test__is_posixlike_path(path_: pathlib.PurePath | str, is_posix: bool) -> No
(custom_pure_path(sep="\\", altsep="/")(r"C:\foo\bar"), True),
(custom_pure_path(sep=":", altsep="\\")(r"C:\foo\bar"), True),
("/foo/bar", False),
(r"C:\foo\bar", True),
(r"C:/foo/bar", True),
(r"\??\C:\Windows\System32\calc.exe", True),
],
)
def test__is_windowslike_path(path_: pathlib.PurePath, is_windows: bool) -> None:
Expand Down Expand Up @@ -677,7 +683,7 @@ def test_path() -> None:
),
(
("/some/path", pathlib.PureWindowsPath("win/path"), pathlib.PurePosixPath("pos/path")),
flow.record.fieldtypes.windows_path,
flow.record.fieldtypes.posix_path,
),
(
(pathlib.PurePosixPath("pos/path"), pathlib.PureWindowsPath("win/path")),
Expand Down