Skip to content

Commit

Permalink
Remove manadatory busybox injection
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Oct 11, 2023
1 parent 276b4a9 commit 9d06217
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 42 deletions.
1 change: 0 additions & 1 deletion binharness/common/busybox.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def install(self: BusyboxInjection, environment: Environment) -> None:
self._environment = environment
else:
raise
environment.busybox_injection = self

def mktemp(
self: BusyboxInjection, directory: bool = False # noqa: FBT001, FBT002
Expand Down
13 changes: 8 additions & 5 deletions binharness/common/qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Generator
from typing import TYPE_CHECKING, Generator, cast

from binharness import IO
from binharness.types.executor import InjectableExecutor
from binharness.types.injection import ExecutableInjection
from binharness.util import read_lines
from binharness.util import generate_random_suffix, read_lines

if TYPE_CHECKING:
from binharness import Process, Target
Expand Down Expand Up @@ -35,7 +36,9 @@ def run_with_log(
cwd: Path | None = None,
) -> tuple[Process, Generator[bytes, None, None]]:
"""Run the injection in the environment and return a log generator."""
logfile = self.environment.busybox_injection.mktemp()
logfile = (
self.environment.get_tempdir() / f"qemu-{generate_random_suffix()}.log"
)
proc = self.run(
"-D",
str(logfile),
Expand All @@ -45,8 +48,8 @@ def run_with_log(
)

def log_generator() -> Generator[bytes, None, None]:
cat_proc = self.environment.busybox_injection.cat(logfile)
yield from read_lines(cat_proc.stdout)
file = self.environment.open_file(logfile, "rb")
yield from read_lines(cast(IO[bytes], file))

# Cleanup log file
self.environment.run_command("rm", logfile)
Expand Down
27 changes: 1 addition & 26 deletions binharness/types/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
if TYPE_CHECKING:
from pathlib import Path

from binharness import IO, BusyboxInjection, Process


class BusyboxInjectionNotInstalledError(Exception):
"""BusyBox is not installed in the environment."""
from binharness import IO, Process


class Environment(ABC):
Expand All @@ -22,27 +18,6 @@ class Environment(ABC):
well as running commands.
"""

_busybox_injection: BusyboxInjection | None

def __init__(self: Environment) -> None:
"""Create an Environment."""
self._busybox_injection = None

@property
def busybox_injection(self: Environment) -> BusyboxInjection:
"""Return the BusyboxInjection for the environment."""
if self._busybox_injection is None:
raise BusyboxInjectionNotInstalledError
return self._busybox_injection

@busybox_injection.setter
def busybox_injection(
self: Environment,
busybox_injection: BusyboxInjection,
) -> None:
"""Set the BusyboxInjection for the environment."""
self._busybox_injection = busybox_injection

@abstractmethod
def run_command(
self: Environment,
Expand Down
13 changes: 3 additions & 10 deletions binharness/types/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from typing import TYPE_CHECKING

from binharness.types.environment import BusyboxInjectionNotInstalledError

if TYPE_CHECKING:
from pathlib import Path

Expand Down Expand Up @@ -47,14 +45,9 @@ def install(self: Injection, environment: Environment) -> None:
if self._environment is not None:
raise InjectionAlreadyInstalledError
if self.env_path is None:
try:
bb_injection = environment.busybox_injection
except BusyboxInjectionNotInstalledError:
from binharness.common.busybox import BusyboxInjection

bb_injection = BusyboxInjection()
bb_injection.install(environment)
self.env_path = bb_injection.mktemp(directory=True)
self.env_path = environment.get_tempdir() / (
self.__class__.__name__ + ".bh-inj"
)

environment.inject_files([(self.host_path, self.env_path)])
self._environment = environment
Expand Down
7 changes: 7 additions & 0 deletions binharness/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""binharness.util - Utility functions for binharness."""
from __future__ import annotations

import random
import shlex
import string
from pathlib import Path
from typing import TYPE_CHECKING, Generator, Sequence

Expand Down Expand Up @@ -43,3 +45,8 @@ def read_lines(file: IO[bytes]) -> Generator[bytes, None, None]:
while b"\n" in buffer:
line, buffer = buffer.split(b"\n", 1)
yield line + b"\n"


def generate_random_suffix(n: int = 6) -> str:
"""Generate a random suffix."""
return "".join(random.choices(string.ascii_letters, k=n)) # noqa: S311

0 comments on commit 9d06217

Please sign in to comment.