Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No option to close a Target #100

Open
Kafow opened this issue Dec 9, 2022 · 1 comment · May be fixed by #104
Open

No option to close a Target #100

Kafow opened this issue Dec 9, 2022 · 1 comment · May be fixed by #104
Assignees
Labels
enhancement New feature or request

Comments

@Kafow
Copy link

Kafow commented Dec 9, 2022

Hey,
When opening a target there is no option to close its resources that have opened on the filesystem.
For example, consider opening A qcow2 target:

  • In order to open a qcow2 target we will initialize qcow2container

    class QCow2Container(Container):
    def __init__(self, fh: Union[BinaryIO, Path], data_file=None, backing_file=None, *args, **kwargs):
    f = fh
    if not hasattr(fh, "read"):
    f = fh.open("rb")
    self.qcow2 = qcow2.QCow2(f, data_file, backing_file)
    super().__init__(fh, self.qcow2.size, *args, **kwargs)

    As you can see we are opening a file handle to the path specified:

    However, consider we wanna free that target, we will try to call something like

    for disk in target.disks:
        disk.close()

    But as it seems, the close function is not implemented in most of the containers, for example Qcow2Container:

    def close(self) -> None:
    pass

    So the only way to close a file handle right now is to do some workaround like this (considering you have different types of disks):

    for disk in target.disks:
        if hasattr(disk, "qcow2"):
            disk.qcow2.fh.close()
        if hasattr(disk, "vhdx"):
            disk.vhdx.fh.close()
        
        .....

    Instead of just doing a way cleaner code like:

    for disk in target.disks:
        disk.close()

    I might try and do a PR later fixing this issue.
    Thanks,
    Ofek

@Schamper
Copy link
Member

Hi @Kafow! That is indeed correct and something we wish to clean up. It hasn't been an issue for us so far because most of our use-cases for dissect.target are fairly short-lived programs, so dangling file handles weren't an issue (they'd be cleaned up as the program exits).

If you could create a PR with a start for fixing this, that'd be appreciated!

Kafow added a commit to Kafow/dissect.target that referenced this issue Dec 14, 2022
@Kafow Kafow linked a pull request Dec 14, 2022 that will close this issue
@Schamper Schamper added the enhancement New feature or request label Jan 23, 2023
Kafow added a commit to Kafow/dissect.target that referenced this issue Jan 24, 2023
@narimantos narimantos linked a pull request Apr 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants