Minor updates to FileEntry / oputil

This commit is contained in:
Bryan Ashby 2018-11-04 15:01:27 -07:00
parent d7aabba847
commit 63b5eed504
2 changed files with 25 additions and 2 deletions

View File

@ -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, '_');

View File

@ -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 );
});
},