From d29a737f3f0f1e3eccb9c74bf8e75b46ce94c5b1 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Thu, 17 Apr 2025 07:14:54 +1000 Subject: [PATCH] Fix Bug #3212: Handle error opening file when computing file hash (#3214) * Despite code validation and the file just being written to disk, and the check passing that the file exists, handle that we now cannot open the file that was just created and wrote data to. Catch any error when attempting to open file to generate the file hash --- src/util.d | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/util.d b/src/util.d index 6eb20d69..44bcf583 100644 --- a/src/util.d +++ b/src/util.d @@ -136,7 +136,15 @@ void safeRemove(const(char)[] path) { // Returns the SHA1 hash hex string of a file string computeSha1Hash(string path) { SHA1 sha; - auto file = File(path, "rb"); + File file; + + try { + file = File(path, "rb"); + } catch (ErrnoException e) { + // log that we could not generate a hash + addLogEntry("Failed to open file to compute SHA1 Hash: " ~ path ~ " - " ~ e.msg); + } + scope(exit) file.close(); // Ensure file is closed post read foreach (ubyte[] data; chunks(file, 4096)) { sha.put(data); @@ -150,7 +158,15 @@ string computeSha1Hash(string path) { // Returns the quickXorHash base64 string of a file string computeQuickXorHash(string path) { QuickXor qxor; - auto file = File(path, "rb"); + File file; + + try { + file = File(path, "rb"); + } catch (ErrnoException e) { + // log that we could not generate a hash + addLogEntry("Failed to open file to compute QuickXor Hash: " ~ path ~ " - " ~ e.msg); + } + scope(exit) file.close(); // Ensure file is closed post read foreach (ubyte[] data; chunks(file, 4096)) { qxor.put(data); @@ -164,7 +180,15 @@ string computeQuickXorHash(string path) { // Returns the SHA256 hex string of a file string computeSHA256Hash(string path) { SHA256 sha256; - auto file = File(path, "rb"); + File file; + + try { + file = File(path, "rb"); + } catch (ErrnoException e) { + // log that we could not generate a hash + addLogEntry("Failed to open file to compute SHA256 Hash: " ~ path ~ " - " ~ e.msg); + } + scope(exit) file.close(); // Ensure file is closed post read foreach (ubyte[] data; chunks(file, 4096)) { sha256.put(data);