Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copySync to overwrite destination file if readonly and clobber true #190

Merged
merged 2 commits into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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