Improved mcrec corrupt file handling.
Ylian Saint-Hilaire committed
Mar 3, 2020 at 15:14 UTC
25a0af7383d16a344de285bfb56f1c5b60f22a73
1 file changed
+11
-2
mcrec.js
+11
-2
@@ -63,7 +63,15 @@ function createIndex(state, ptr) {
63
state.lastIndex += 10;
64
}
65
66
-function processBlock(state, block) {
66
+function processBlock(state, block, err) {
67
+ if (err != null) {
68
+ // Error reading the next block, exit now.
69
+ fs.close(state.recFile, function () {
70
+ for (var i in state) { delete state[i]; } // Clear the state.
71
+ log("Error.");
72
+ });
73
+ return;
74
+ }
75
if (block == null) {
76
// We are done, close this file.
77
writeIndex(state, function () {
@@ -254,13 +262,14 @@ function readNextBlock(state, func) {
262
if ((state.recFilePtr + 16) > state.recFileSize) { func(state, null); return; }
263
var r = {}, buf = Buffer.alloc(16);
264
fs.read(state.recFile, buf, 0, 16, state.recFilePtr, function (err, bytesRead, buf) {
265
+ if (bytesRead != 16) { func(state, null, true); return; } // Error
266
r.type = buf.readInt16BE(0);
267
r.flags = buf.readInt16BE(2);
268
r.size = buf.readInt32BE(4);
269
r.time = buf.readIntBE(8, 8);
270
r.date = new Date(r.time);
271
r.ptr = state.recFilePtr;
263
- if ((state.recFilePtr + 16 + r.size) > state.recFileSize) { func(state, null); return; }
272
+ if ((state.recFilePtr + 16 + r.size) > state.recFileSize) { func(state, null, true); return; } // Error
273
if (r.size == 0) {
274
r.data = null;
275
func(state, r);