diff --git a/src/background/findFiles.js b/src/background/findFiles.js new file mode 100644 index 0000000..59c8baf --- /dev/null +++ b/src/background/findFiles.js @@ -0,0 +1,3 @@ +const glob = require("glob") + +module.exports = (path) => new Promise((resolve) => glob(path, (error, files) => resolve(files))) \ No newline at end of file diff --git a/src/background/sphinx.js b/src/background/sphinx.js index 632ead4..527344e 100644 --- a/src/background/sphinx.js +++ b/src/background/sphinx.js @@ -8,6 +8,8 @@ const fs = require('fs') const iconv = require('iconv-lite') const { spawn, exec } = require('child_process') const appConfig = require('./config') +const findFiles = require('./findFiles') +const _ = require('lodash') const writeSphinxConfig = (path, dbPath) => { let config = ` @@ -173,11 +175,21 @@ module.exports = (callback, dataDirectory, onClose) => { sphinx.stdout.on('data', (data) => { console.log(`sphinx: ${data}`) + + // don't listen if we are in fixing mode + if(sphinx.fixing) + return + if (data.includes('accepting connections')) { console.log('catched sphinx start') if(callback) callback() } + + if(data.includes('invalid meta file')) + { + sphinx.fixDatabase() + } const checkOptimized = String(data).match(/index ([\w]+): optimized/) if(checkOptimized) @@ -214,6 +226,47 @@ module.exports = (callback, dataDirectory, onClose) => { } }) + sphinx.fixDatabase = async () => { + if(sphinx.fixing) + return + sphinx.fixing = true + + // close db + await new Promise((resolve) => { + sphinx.stop(resolve, true) + console.log('revent start') + }) + + const checkNullFile = (file) => new Promise((resolve) => { + let f = fs.createReadStream(file) + f.on('data', (chunk) => { + for(const byte of chunk) + if(byte != 0) + { + resolve(true) + f.destroy() + return + } + }).on('end', () => { + resolve(false) + }); + }) + + // check meta files + const probablyCoruptedFiles = await findFiles(`${sphinx.directoryPath}/**/*.+(meta|ram)`) + let brokenFiles = await Promise.all(probablyCoruptedFiles.map(file => checkNullFile(file))) + brokenFiles = probablyCoruptedFiles.filter((file, index) => !brokenFiles[index]) + + brokenFiles.forEach(file => { + console.log('FIXDB: clean file because of broken', file) + fs.unlinkSync(file) + }) + + sphinx.fixing = false + + _.merge(sphinx, sphinx.start(callback)); + } + return sphinx }