From e55bf714319c8ee26a857d29db68154caf792d49 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 29 Mar 2018 14:39:39 +0200 Subject: [PATCH] fileOps: Fixed crash on invalid filenames, added test for it --- node_modules/_app/fileOps/fileOps.js | 55 ++++++++++++----------- node_modules/_app/fileOps/fileOps.spec.js | 3 +- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/node_modules/_app/fileOps/fileOps.js b/node_modules/_app/fileOps/fileOps.js index 3cbc43b3..014f5577 100644 --- a/node_modules/_app/fileOps/fileOps.js +++ b/node_modules/_app/fileOps/fileOps.js @@ -73,35 +73,38 @@ var deleteFolderRecursiveSync = function(path) { var deleteFolderRecursive = function(path){ return new Promise((resolve, reject) => { fs.readdir(path, (err, files) => { - if(err) reject(err); - let thingsToDelete = files.length; - files.forEach(file => { - let curPath = path + "/" + file; - fs.stat(curPath, (err, stat) => { - if(!err){ - if(stat.isDirectory()){ - // recurse - deleteFolderRecursive(curPath).then(() => { - whenFilesDeleted(); - }); + if(err){ + reject(err); + } else { + let thingsToDelete = files.length; + files.forEach(file => { + let curPath = path + "/" + file; + fs.stat(curPath, (err, stat) => { + if(!err){ + if(stat.isDirectory()){ + // recurse + deleteFolderRecursive(curPath).then(() => { + whenFilesDeleted(); + }).catch(e => console.log(e)); + } else { + fs.unlink(curPath, () => { + // console.log("Deleted "+curPath); + whenFilesDeleted(); + }); + } } else { - fs.unlink(curPath, () => { - // console.log("Deleted "+curPath); - whenFilesDeleted(); - }); + resolve(false); } - } else { - resolve(false); - } - }); - }); - whenFilesDeleted(); - function whenFilesDeleted(){ - if(!thingsToDelete--){// var-- instead of --var because we are calling the function once too much and need the delay - fs.rmdir(path, () => { - resolve(); - // console.log("Deleted "+path); }); + }); + whenFilesDeleted(); + function whenFilesDeleted(){ + if(!thingsToDelete--){// var-- instead of --var because we are calling the function once too much and need the delay + fs.rmdir(path, () => { + resolve(); + // console.log("Deleted "+path); + }); + } } } }); diff --git a/node_modules/_app/fileOps/fileOps.spec.js b/node_modules/_app/fileOps/fileOps.spec.js index 4d6cf9d1..b3d731dd 100644 --- a/node_modules/_app/fileOps/fileOps.spec.js +++ b/node_modules/_app/fileOps/fileOps.spec.js @@ -115,6 +115,7 @@ describe("fileOps.js", function(){ describe("fileOps.deleteFolderRecursive(path)", function(){ it("deletes a folder and all files and folders in it, just like sync version", async function(){ setupTestingEnv(); + assert(fs.statSync(path.resolve(__dirname, "test"))); let x = await fileOps.deleteFolderRecursive(path.resolve(__dirname, "test")); assert.throws(function(){ fs.statSync(path.resolve(__dirname, "test")); @@ -122,7 +123,7 @@ describe("fileOps.js", function(){ assert(x === undefined, "Promise version of this function does not have a return value upon success"); return true; }).timeout(5000); - it("throws like a promise if you give it an invalid path", async function(){ + it("throws like a promise does if you give it an invalid path", async function(){ let x = await fileOps.deleteFolderRecursive("Look this path is fake!") .catch(e => { assert(e);