Skip to content

Commit

Permalink
Would it be enough to pront out?
Browse files Browse the repository at this point in the history
  • Loading branch information
paazmaya committed Dec 15, 2019
1 parent 4fc31e2 commit 5af2dd4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
9 changes: 8 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ const COLUMNS_IN_TABLE = 4;
const questions = Array(COLUMNS_IN_TABLE).fill('?').join(', ');
const INSERT_DATA = `INSERT INTO files VALUES (${questions})`;

const VERIFY = {
NOT_FOUND: 0,
PASS: 1,
FAIL: -1
};

module.exports = {
EXEC_OPTIONS,
OPENSSL_VERSION,
DEFAULT_ALG,
ITERATION_SIZE,
DEFAULT_DATABASE,
CREATE_TABLE,
INSERT_DATA
INSERT_DATA,
VERIFY
};
26 changes: 17 additions & 9 deletions lib/find-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,43 @@
* Licensed under the MIT license
*/

//const constants = require('./constants');
const constants = require('./constants');

/**
* Find matching files and validate hashes
*
* @param {array} list List of file meta data objects
* @param {sqlite3.Database} db Database instance
* @returns {sqlite3.Database|boolean} Database instance or false
* @returns {object} Validation information
*/
const findData = (list, db) => {
if (!(list instanceof Array) || list.length === 0) {
return false;
}

// There always should be just one as it is unique
const statement = db.prepare(`
SELECT * FROM files WHERE filepath = ?
SELECT * FROM files WHERE filepath = ? LIMIT 1
`);

const output = {};

list.forEach((item) => {
const rows = statement.all(item.filepath);
if (rows.length > 1) {
console.log(`File "${item.filepath}" had several (${rows.length}) matches in database`);
}
else if (rows.length === 0) {
const row = statement.get(item.filepath);
if (!row) {
console.log(`File "${item.filepath}" did not exists in database`);
output[item.filepath] = constants.VERIFY.NOT_FOUND;
}
else if (row.hash === item.hash) {
output[item.filepath] = constants.VERIFY.PASS;
}
else {
console.log(`File "${item.filepath}" does not match the one in database`);
output[item.filepath] = constants.VERIFY.FAIL;
}
});

return db;
return output;
};

module.exports = findData;

0 comments on commit 5af2dd4

Please sign in to comment.