Skip to content

Commit

Permalink
Merge pull request #190 from bartland/CopySyncClobberROFile
Browse files Browse the repository at this point in the history
copySync to overwrite destination file if readonly and clobber true
  • Loading branch information
jprichardson committed Nov 2, 2015
2 parents d3b2e03 + 8753c3d commit de289db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})
})
})
})
Expand Down
9 changes: 7 additions & 2 deletions lib/copy-sync/copy-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ 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.chmodSync(destFile, parseInt('777', 8))
fs.unlinkSync(destFile)
} else {
throw Error('EEXIST')
}
}

var fdr = fs.openSync(srcFile, 'r')
Expand Down

0 comments on commit de289db

Please sign in to comment.