Skip to content

Commit

Permalink
Misc/Custom Parsers (DIS-2162)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecinestpasunepipe committed Oct 8, 2024
1 parent 3ac5b79 commit dadfa55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions dissect/target/filesystems/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ def open(self) -> BinaryIO:
Returns:
A file-like object holding a byte representation of :attr:`parser_items`.
"""

if isinstance(self.parser_items, ConfigurationParser):
# Currently trying to open the underlying entry
return self.entry.open()

if isinstance(self.parser_items, bytes):
return io.BytesIO(self.parser_items)

Check warning on line 256 in dissect/target/filesystems/config.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/config.py#L256

Added line #L256 was not covered by tests

output_data = self._write_value_mapping(self.parser_items)
return io.BytesIO(bytes(output_data, "utf-8"))

Expand Down
17 changes: 14 additions & 3 deletions dissect/target/helpers/configutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def parse_file(self, fh: TextIO) -> None:
def get(self, item: str, default: Optional[Any] = None) -> Any:
return self.parsed_data.get(item, default)

def read_file(self, fh: TextIO) -> None:
def read_file(self, fh: TextIO | BytesIO) -> None:
"""Parse a configuration file.
Raises:
Expand Down Expand Up @@ -303,6 +303,14 @@ def parse_file(self, fh: TextIO) -> None:
self.parsed_data = {"content": fh.read(), "size": str(fh.tell())}


class Bin(ConfigurationParser):

"""Read the file into ``binary`` and show the number of bytes read"""

def parse_file(self, fh: BytesIO) -> None:
self.parsed_data = {"binary": fh.read(), "size": fh.tell()}

Check warning on line 311 in dissect/target/helpers/configutil.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/configutil.py#L311

Added line #L311 was not covered by tests


class Xml(ConfigurationParser):
"""Parses an XML file. Ignores any constructor parameters passed from ``ConfigurationParser`."""

Expand Down Expand Up @@ -759,6 +767,7 @@ def create_parser(self, options: Optional[ParserOptions] = None) -> Configuratio
"nsswitch.conf": ParserConfig(Default, separator=(":",)),
"lsb-release": ParserConfig(Default),
"catalog": ParserConfig(Xml),
"ld.so.cache": ParserConfig(Bin),
"fstab": ParserConfig(
CSVish,
separator=(r"\s",),
Expand Down Expand Up @@ -832,9 +841,11 @@ def parse_config(
parser_type = _select_parser(entry, hint)

parser = parser_type.create_parser(options)

with entry.open() as fh:
open_file = io.TextIOWrapper(fh, encoding="utf-8")
if not isinstance(parser, Bin):
open_file = io.TextIOWrapper(fh, encoding="utf-8")
else:
open_file = io.BytesIO(fh.read())

Check warning on line 848 in dissect/target/helpers/configutil.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/configutil.py#L848

Added line #L848 was not covered by tests
parser.read_file(open_file)

return parser
Expand Down

0 comments on commit dadfa55

Please sign in to comment.