From e05c685e5a02ebb8ebf18049da4c8af4d9a7fb9c Mon Sep 17 00:00:00 2001 From: bartland Date: Fri, 30 Oct 2015 16:29:42 +1100 Subject: [PATCH 1/2] copySync updated to overwrite destination file if readonly and clobber set to true. Added test case. This commit fixes #183. --- lib/copy-sync/__tests__/copy-sync-file.test.js | 14 ++++++++++++++ lib/copy-sync/copy-file-sync.js | 8 ++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/copy-sync/__tests__/copy-sync-file.test.js b/lib/copy-sync/__tests__/copy-sync-file.test.js index 63a4889c..b6c869b3 100644 --- a/lib/copy-sync/__tests__/copy-sync-file.test.js +++ b/lib/copy-sync/__tests__/copy-sync-file.test.js @@ -183,6 +183,20 @@ describe('+ copySync()', function () { assert.strictEqual(destData, destDataNew) }) }) + + describe('> when clobber is true and dest is readonly', function () { + it('should copy the file and not throw an error', function () { + try { + fs.chmodSync(dest, parseInt('444', 8)) + fs.copySync(src, dest, {clobber: true}) + destData = fs.readFileSync(dest, 'utf8') + assert.strictEqual(srcData, destData) + } finally { + // destination file is readonly so just remove it so we don't affect other tests + fs.unlinkSync(dest) + } + }) + }) }) }) }) diff --git a/lib/copy-sync/copy-file-sync.js b/lib/copy-sync/copy-file-sync.js index 2dc31ef3..f331b3c2 100644 --- a/lib/copy-sync/copy-file-sync.js +++ b/lib/copy-sync/copy-file-sync.js @@ -7,8 +7,12 @@ function copyFileSync (srcFile, destFile, options) { var clobber = options.clobber var preserveTimestamps = options.preserveTimestamps - if (fs.existsSync(destFile) && !clobber) { - throw Error('EEXIST') + if (fs.existsSync(destFile)) { + if (clobber) { + fs.unlinkSync(destFile) + } else { + throw Error('EEXIST') + } } var fdr = fs.openSync(srcFile, 'r') From 8753c3d0bf68e996538eee3fbca394cf145767c2 Mon Sep 17 00:00:00 2001 From: bartland Date: Mon, 2 Nov 2015 23:44:15 +1100 Subject: [PATCH 2/2] Fix so copySync unlinking read only file will now work on nodejs 0.10.40. This commit fixes #183. --- lib/copy-sync/copy-file-sync.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/copy-sync/copy-file-sync.js b/lib/copy-sync/copy-file-sync.js index f331b3c2..c6c57118 100644 --- a/lib/copy-sync/copy-file-sync.js +++ b/lib/copy-sync/copy-file-sync.js @@ -9,6 +9,7 @@ function copyFileSync (srcFile, destFile, options) { if (fs.existsSync(destFile)) { if (clobber) { + fs.chmodSync(destFile, parseInt('777', 8)) fs.unlinkSync(destFile) } else { throw Error('EEXIST')