From 793c61eec0557729c4c8fb77c63b4149e09dfe26 Mon Sep 17 00:00:00 2001 From: seanmwalker Date: Fri, 17 Jan 2014 00:45:26 -0600 Subject: [PATCH 1/3] Updating wrench rmdirSyncRecursive to support permissions issues for windows. --- lib/wrench.js | 30 +++++++++++++++++++++--------- tests/rmdirSyncRecursive.js | 22 ++++++++++++++++++++++ tests/runner.js | 3 ++- 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 tests/rmdirSyncRecursive.js diff --git a/lib/wrench.js b/lib/wrench.js index d2664ec..44bda99 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -12,7 +12,8 @@ */ var fs = require("fs"), - _path = require("path"); + _path = require("path"), + isWindows = !!process.platform.match(/^win/); /* wrench.readdirSyncRecursive("directory_path"); * @@ -122,7 +123,7 @@ exports.readdirRecursive = function(baseDir, fn) { -/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent); +/* wrench.rmdirSyncRecursive("directory_path", failSilent); * * Recursively dives through directories and obliterates everything about it. This is a * Sync-function, which blocks things until it's done. No idea why anybody would want an @@ -140,16 +141,27 @@ exports.rmdirSyncRecursive = function(path, failSilent) { /* Loop through and delete everything in the sub-tree after checking it */ for(var i = 0; i < files.length; i++) { - var currFile = fs.lstatSync(_path.join(path, files[i])); + var file = _path.join(path, files[i]); + var currFile = fs.lstatSync(file); - if(currFile.isDirectory()) // Recursive function back to the beginning - exports.rmdirSyncRecursive(_path.join(path, files[i])); + if(currFile.isDirectory()) { + // Recursive function back to the beginning + exports.rmdirSyncRecursive(file); + } else if(currFile.isSymbolicLink()) { + // Unlink symlinks + if (isWindows) { + fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved.. + } - else if(currFile.isSymbolicLink()) // Unlink symlinks - fs.unlinkSync(_path.join(path, files[i])); + fs.unlinkSync(file); + } else { + // Assume it's a file - perhaps a try/catch belongs here? + if (isWindows) { + fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved.. + } - else // Assume it's a file - perhaps a try/catch belongs here? - fs.unlinkSync(_path.join(path, files[i])); + fs.unlinkSync(file); + } } /* Now that we know everything in the sub-tree has been deleted, we can delete the main diff --git a/tests/rmdirSyncRecursive.js b/tests/rmdirSyncRecursive.js new file mode 100644 index 0000000..db4e9ef --- /dev/null +++ b/tests/rmdirSyncRecursive.js @@ -0,0 +1,22 @@ +var testCase = require('nodeunit').testCase; +var fs = require('fs'); +var wrench = require('../lib/wrench'); +var path = require('path'); + +module.exports = testCase({ + test_rmdirSyncRecursive: function(test) { + var dir = __dirname + '/_tmp/foo/bar'; + + wrench.mkdirSyncRecursive(dir, 0777); + + test.equals(fs.existsSync(dir), true, 'Dir should exist - mkdirSyncRecursive not working?'); + + wrench.rmdirSyncRecursive(dir); + + test.equals(fs.existsSync(dir), false, 'Dir should not exist now...'); + + test.done(); + }, +}); + +// vim: et ts=4 sw=4 diff --git a/tests/runner.js b/tests/runner.js index 8f90dae..e321a15 100644 --- a/tests/runner.js +++ b/tests/runner.js @@ -4,5 +4,6 @@ module.exports = { group_mkdir: require('./mkdir'), group_readdir: require('./readdir'), - group_copydir: require('./copydirsync_unix') + group_copydir: require('./copydirsync_unix'), + group_rmdir: require('./rmdirSyncRecursive') }; From df61c7017f6abd116a5bcf3d139e084b83913323 Mon Sep 17 00:00:00 2001 From: seanmwalker Date: Fri, 17 Jan 2014 01:28:55 -0600 Subject: [PATCH 2/3] Updating test for rmdirSyncRecursive to check for files with alternative permissions being removed properly. --- lib/wrench.js | 5 ++++ tests/rmdirSyncRecursive.js | 56 +++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/wrench.js b/lib/wrench.js index 44bda99..d32b854 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -135,6 +135,8 @@ exports.rmdirSyncRecursive = function(path, failSilent) { try { files = fs.readdirSync(path); } catch (err) { + console.log('Error "' + err.message + '"\n'); + if(failSilent) return; throw new Error(err.message); } @@ -150,6 +152,7 @@ exports.rmdirSyncRecursive = function(path, failSilent) { } else if(currFile.isSymbolicLink()) { // Unlink symlinks if (isWindows) { + console.log('Remove file "' + file + '"\n'); fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved.. } @@ -157,6 +160,7 @@ exports.rmdirSyncRecursive = function(path, failSilent) { } else { // Assume it's a file - perhaps a try/catch belongs here? if (isWindows) { + console.log('Remove file "' + file + '"\n'); fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved.. } @@ -164,6 +168,7 @@ exports.rmdirSyncRecursive = function(path, failSilent) { } } + console.log('Remove dir "' + path + '"\n'); /* Now that we know everything in the sub-tree has been deleted, we can delete the main directory. Huzzah for the shopkeep. */ return fs.rmdirSync(path); diff --git a/tests/rmdirSyncRecursive.js b/tests/rmdirSyncRecursive.js index db4e9ef..f6b9442 100644 --- a/tests/rmdirSyncRecursive.js +++ b/tests/rmdirSyncRecursive.js @@ -5,18 +5,70 @@ var path = require('path'); module.exports = testCase({ test_rmdirSyncRecursive: function(test) { - var dir = __dirname + '/_tmp/foo/bar'; + var dir = __dirname + '/_tmp2/foo/bar'; - wrench.mkdirSyncRecursive(dir, 0777); + wrench.mkdirSyncRecursive(dir, '777'); + + var f1Path = path.join(dir, 'test1.txt'); + var f2Path = path.join(path.dirname(dir), 'test2.txt'); + var f3Path = path.join(path.dirname(path.dirname(dir)), 'test3.txt'); + + fs.writeFileSync(f1Path, 'foo bar baz'); + fs.writeFileSync(f2Path, 'foo bar baz'); + fs.writeFileSync(f3Path, 'foo bar baz'); + + fs.chmodSync(f1Path, '777'); + fs.chmodSync(f2Path, '777'); + fs.chmodSync(f3Path, '777'); test.equals(fs.existsSync(dir), true, 'Dir should exist - mkdirSyncRecursive not working?'); + test.equals(fs.existsSync(f1Path), true, 'File should exist'); + test.equals(fs.existsSync(f2Path), true, 'File should exist'); + test.equals(fs.existsSync(f3Path), true, 'File should exist'); wrench.rmdirSyncRecursive(dir); test.equals(fs.existsSync(dir), false, 'Dir should not exist now...'); + test.equals(fs.existsSync(f1Path), false, 'File should not exist'); + test.equals(fs.existsSync(f2Path), true, 'File should exist'); + test.equals(fs.existsSync(f3Path), true, 'File should exist'); + + wrench.rmdirSyncRecursive(path.dirname(path.dirname(dir))); test.done(); }, + + test_rmdirSyncRecursiveFromRoot: function(test) { + var dir = __dirname + '/_tmp3/foo/bar'; + + wrench.mkdirSyncRecursive(dir, '777'); + + var f1Path = path.join(dir, 'test1.txt'); + var f2Path = path.join(path.dirname(dir), 'test2.txt'); + var f3Path = path.join(path.dirname(path.dirname(dir)), 'test3.txt'); + + fs.writeFileSync(f1Path, 'foo bar baz'); + fs.writeFileSync(f2Path, 'foo bar baz'); + fs.writeFileSync(f3Path, 'foo bar baz'); + + fs.chmodSync(f1Path, '777'); + fs.chmodSync(f2Path, '777'); + fs.chmodSync(f3Path, '777'); + + test.equals(fs.existsSync(dir), true, 'Dir should exist - mkdirSyncRecursive not working?'); + test.equals(fs.existsSync(f1Path), true, 'File should exist'); + test.equals(fs.existsSync(f2Path), true, 'File should exist'); + test.equals(fs.existsSync(f3Path), true, 'File should exist'); + + wrench.rmdirSyncRecursive(path.dirname(path.dirname(dir))); + + test.equals(fs.existsSync(dir), false, 'Dir should not exist now...'); + test.equals(fs.existsSync(f1Path), false, 'File should not exist'); + test.equals(fs.existsSync(f2Path), false, 'File should not exist'); + test.equals(fs.existsSync(f3Path), false, 'File should not exist'); + + test.done(); + } }); // vim: et ts=4 sw=4 From 99106ac7383232eaab68cd5c5475d2bdb2ea06f3 Mon Sep 17 00:00:00 2001 From: seanmwalker Date: Fri, 17 Jan 2014 01:33:47 -0600 Subject: [PATCH 3/3] Remove console.log statements to clean up the code. --- lib/wrench.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/wrench.js b/lib/wrench.js index d32b854..c546d43 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -135,7 +135,6 @@ exports.rmdirSyncRecursive = function(path, failSilent) { try { files = fs.readdirSync(path); } catch (err) { - console.log('Error "' + err.message + '"\n'); if(failSilent) return; throw new Error(err.message); @@ -152,7 +151,6 @@ exports.rmdirSyncRecursive = function(path, failSilent) { } else if(currFile.isSymbolicLink()) { // Unlink symlinks if (isWindows) { - console.log('Remove file "' + file + '"\n'); fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved.. } @@ -160,7 +158,6 @@ exports.rmdirSyncRecursive = function(path, failSilent) { } else { // Assume it's a file - perhaps a try/catch belongs here? if (isWindows) { - console.log('Remove file "' + file + '"\n'); fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved.. } @@ -168,7 +165,6 @@ exports.rmdirSyncRecursive = function(path, failSilent) { } } - console.log('Remove dir "' + path + '"\n'); /* Now that we know everything in the sub-tree has been deleted, we can delete the main directory. Huzzah for the shopkeep. */ return fs.rmdirSync(path);