From 92ece08c5f709777564efda9111a352a886ccda4 Mon Sep 17 00:00:00 2001 From: Mike Auty Date: Wed, 14 Dec 2022 18:50:49 +0000 Subject: [PATCH] Core: Fix up file close issue --- volatility3/framework/layers/resources.py | 24 +++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/volatility3/framework/layers/resources.py b/volatility3/framework/layers/resources.py index 3d47e6616..a64fa7d7a 100644 --- a/volatility3/framework/layers/resources.py +++ b/volatility3/framework/layers/resources.py @@ -179,20 +179,18 @@ class ResourceAccessor(object): except AttributeError: # If our fp doesn't have an info member, carry on gracefully content_length = -1 - cache_file = open(temp_filename, "wb") - - count = 0 - block = fp.read(block_size) - while block: - count += len(block) - if self._progress_callback: - self._progress_callback( - count * 100 / max(count, int(content_length)), - f"Reading file {url}", - ) - cache_file.write(block) + with open(temp_filename, "wb") as cache_file: + count = 0 block = fp.read(block_size) - cache_file.close() + while block: + count += len(block) + if self._progress_callback: + self._progress_callback( + count * 100 / max(count, int(content_length)), + f"Reading file {url}", + ) + cache_file.write(block) + block = fp.read(block_size) else: vollog.debug(f"Using already cached file at: {temp_filename}") # Re-open the cache with a different mode