From 63b5eed504d1b643a0b987ab3c72f68c66cb40b9 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 4 Nov 2018 15:01:27 -0700 Subject: [PATCH] Minor updates to FileEntry / oputil --- core/file_entry.js | 25 ++++++++++++++++++++++++- core/oputil/oputil_file_base.js | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/core/file_entry.js b/core/file_entry.js index 0f519e10..bb7a4e12 100644 --- a/core/file_entry.js +++ b/core/file_entry.js @@ -369,7 +369,7 @@ module.exports = class FileEntry { return Object.keys(FILE_WELL_KNOWN_META); } - static findFileBySha(sha, cb) { + static findBySha(sha, cb) { // full or partial SHA-256 fileDb.all( `SELECT file_id @@ -397,6 +397,29 @@ module.exports = class FileEntry { ); } + // Attempt to fine a file by an *existing* full path. + // Checkums may have changed and are not validated here. + static findByFullPath(fullPath, cb) { + // first, basic by-filename lookup. + FileEntry.findByFileNameWildcard(paths.basename(fuillPath), (err, entries) => { + if(err) { + return cb(err); + } + if(!entries || !entries.length || entries.length > 1) { + return cb(Errors.DoesNotExist('No matches')); + } + + // ensure the *full* path has not changed + // :TODO: if FS is case-insensitive, we probably want a better check here + const possibleMatch = entries[0]; + if(possibleMatch.fullPath === fullPath) { + return cb(null, possibleMatch); + } + + return cb(Errors.DoesNotExist('No matches')); + }); + } + static findByFileNameWildcard(wc, cb) { // convert any * -> % and ? -> _ for SQLite syntax - see https://www.sqlite.org/lang_expr.html wc = wc.replace(/\*/g, '%').replace(/\?/g, '_'); diff --git a/core/oputil/oputil_file_base.js b/core/oputil/oputil_file_base.js index 8568372c..9dfcdeca 100644 --- a/core/oputil/oputil_file_base.js +++ b/core/oputil/oputil_file_base.js @@ -319,7 +319,7 @@ function getFileEntries(pattern, cb) { return callback(null, entries); // already got it by FILE_ID } - FileEntry.findFileBySha(pattern, (err, fileEntry) => { + FileEntry.findBySha(pattern, (err, fileEntry) => { return callback(null, fileEntry ? [ fileEntry ] : null ); }); },