dissect.target icon indicating copy to clipboard operation
dissect.target copied to clipboard

No option to close a Target

Open Kafow opened this issue 3 years ago • 1 comments

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 https://github.com/fox-it/dissect.target/blob/d1fcfe4428136c9c08f5104eb79e9df57341c632/dissect/target/containers/qcow2.py#L10-L17 As you can see we are opening a file handle to the path specified: https://github.com/fox-it/dissect.target/blob/d1fcfe4428136c9c08f5104eb79e9df57341c632/dissect/target/containers/qcow2.py#L14

    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: https://github.com/fox-it/dissect.target/blob/d1fcfe4428136c9c08f5104eb79e9df57341c632/dissect/target/containers/qcow2.py#L39-L40 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

Kafow avatar Dec 09 '22 12:12 Kafow

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!

Schamper avatar Dec 12 '22 20:12 Schamper