Skip to content

Commit d29a737

Browse files
authored
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
1 parent 025c756 commit d29a737

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/util.d

+27-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ void safeRemove(const(char)[] path) {
136136
// Returns the SHA1 hash hex string of a file
137137
string computeSha1Hash(string path) {
138138
SHA1 sha;
139-
auto file = File(path, "rb");
139+
File file;
140+
141+
try {
142+
file = File(path, "rb");
143+
} catch (ErrnoException e) {
144+
// log that we could not generate a hash
145+
addLogEntry("Failed to open file to compute SHA1 Hash: " ~ path ~ " - " ~ e.msg);
146+
}
147+
140148
scope(exit) file.close(); // Ensure file is closed post read
141149
foreach (ubyte[] data; chunks(file, 4096)) {
142150
sha.put(data);
@@ -150,7 +158,15 @@ string computeSha1Hash(string path) {
150158
// Returns the quickXorHash base64 string of a file
151159
string computeQuickXorHash(string path) {
152160
QuickXor qxor;
153-
auto file = File(path, "rb");
161+
File file;
162+
163+
try {
164+
file = File(path, "rb");
165+
} catch (ErrnoException e) {
166+
// log that we could not generate a hash
167+
addLogEntry("Failed to open file to compute QuickXor Hash: " ~ path ~ " - " ~ e.msg);
168+
}
169+
154170
scope(exit) file.close(); // Ensure file is closed post read
155171
foreach (ubyte[] data; chunks(file, 4096)) {
156172
qxor.put(data);
@@ -164,7 +180,15 @@ string computeQuickXorHash(string path) {
164180
// Returns the SHA256 hex string of a file
165181
string computeSHA256Hash(string path) {
166182
SHA256 sha256;
167-
auto file = File(path, "rb");
183+
File file;
184+
185+
try {
186+
file = File(path, "rb");
187+
} catch (ErrnoException e) {
188+
// log that we could not generate a hash
189+
addLogEntry("Failed to open file to compute SHA256 Hash: " ~ path ~ " - " ~ e.msg);
190+
}
191+
168192
scope(exit) file.close(); // Ensure file is closed post read
169193
foreach (ubyte[] data; chunks(file, 4096)) {
170194
sha256.put(data);

0 commit comments

Comments
 (0)