- 
                Notifications
    
You must be signed in to change notification settings  - Fork 71
 
Open
Labels
enhancementNew feature or requestNew feature or request
Description
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
dissect.target/dissect/target/containers/qcow2.py
Lines 10 to 17 in d1fcfe4
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:
f = fh.open("rb") 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:
dissect.target/dissect/target/containers/qcow2.py
Lines 39 to 40 in d1fcfe4
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 
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request