diff --git a/.gitignore b/.gitignore index ee26e3b..feb92f8 100755 --- a/.gitignore +++ b/.gitignore @@ -14,8 +14,9 @@ node_modules npm-debug.log tests/clamscan-log -tests/infected/* -tests/bad_scan_dir/* +tests/infected +tests/bad_scan_dir +tests/mixed_scan_dir tests/good_files_list_tmp.txt tests/output diff --git a/lib/getFiles.js b/lib/getFiles.js index 9001008..1cfde7a 100644 --- a/lib/getFiles.js +++ b/lib/getFiles.js @@ -3,13 +3,28 @@ const { resolve } = require('path'); const { readdir } = require('fs').promises; -const getFiles = async (dir) => { - const dirents = await readdir(dir, { withFileTypes: true }); - const files = await Promise.all(dirents.map((dirent) => { - const res = resolve(dir, dirent.name); - return dirent.isDirectory() ? getFiles(res) : res; +/** + * Gets a listing of all files (no directories) within a given path. + * By default, it will retrieve files recursively. + * + * @param {string} dir - The directory to get all files of + * @param {boolean} [recursive=true] - If true (default), get all files recursively; False: only get files directly in path + * @returns {Array} - List of all requested path files + */ +const getFiles = async (dir, recursive = true) => { + const items = await readdir(dir, { withFileTypes: true }); + const files = await Promise.all(items.map((item) => { + const res = resolve(dir, item.name); + if (!recursive) { + if (!item.isDirectory()) return res; + return new Promise((resolve) => resolve(null)); + } + return item.isDirectory() ? getFiles(res) : res; })); - return files.flat(); + return files.filter(Boolean).flat(); + + // @todo change to this when package required Node 20+ + // const files = await fs.readdir(dir, { recursive: true }); } module.exports = getFiles; diff --git a/tests/index.js b/tests/index.js index b7ca952..88ba8d7 100755 --- a/tests/index.js +++ b/tests/index.js @@ -744,7 +744,7 @@ describe('isInfected', () => { it('should be okay when scanning a file with consecutive (or any) spaces in it while doing a local scan', async () => { // Make sure we're forced to scan locally - clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: true, quarantineInfected: false }); + clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: false, quarantineInfected: false }); // Write virus file with spaces in its name eicarGen.writeFileSpaced(); @@ -1144,7 +1144,7 @@ describe('scanFiles', () => { describe('edge cases', () => { it('should be fine when one of the filenames has consecutive spaces in it when locally scanning', async () => { // Make sure we're forced to scan locally - clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: true, quarantineInfected: false }); + clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: false, quarantineInfected: false }); eicarGen.writeFile(); eicarGen.writeFileSpaced(); @@ -1255,8 +1255,10 @@ describe('scanDir', () => { }); it('should supply badFiles array with scanned path when directory has infected files', (done) => { + clamscan.settings.scanRecursively = true; eicarGen.writeFile(); clamscan.scanDir(badScanDir, (err, goodFiles, badFiles) => { + // if (err) console.error(err); check(done, () => { expect(err).to.not.be.instanceof(Error); expect(badFiles).to.be.an('array'); @@ -1333,7 +1335,7 @@ describe('scanDir', () => { describe('edge cases', () => { it('should work when falling back to local scan and there is a file with consecutive spaces in it', async () => { // Make sure we're forced to scan locally - clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: true, quarantineInfected: false }); + clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: false, quarantineInfected: false }); eicarGen.writeFile(); eicarGen.writeFileSpaced();