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

bugfix/492 - Replace fs.link/unlink with fs.rename in fse.move #507

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/move/__tests__/move.test.js
Expand Up @@ -199,7 +199,7 @@ describe('move', () => {

fse.move(src, dest, err => {
assert.ifError(err)
assert.strictEqual(fs.link.callCount, 1)
assert.strictEqual(fs.rename.callCount, 1)

fs.readFile(dest, 'utf8', (err, contents) => {
const expected = /^sonic the hedgehog\r?\n$/
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('move', () => {

fse.move(src, dest, err => {
assert.ifError(err)
assert.strictEqual(fs.link.callCount, 1)
assert.strictEqual(fs.rename.callCount, 1)

fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
const expected = /^knuckles\r?\n$/
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('move', () => {

fse.move(src, dest, err => {
assert.ifError(err)
assert.strictEqual(fs.link.callCount, 1)
assert.strictEqual(fs.rename.callCount, 1)

fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
const expected = /^knuckles\r?\n$/
Expand Down
28 changes: 14 additions & 14 deletions lib/move/index.js
Expand Up @@ -33,20 +33,28 @@ function move (src, dest, options, callback) {
function doRename () {
if (path.resolve(src) === path.resolve(dest)) {
fs.access(src, callback)
} else if (overwrite) {
} else {
if (!overwrite && fs.existsSync(dest)) {
const err = new Error('EEXIST: file already exists')
err.code = 'EEXIST'
err.path = src
err.dest = dest
return callback(err)
}

fs.rename(src, dest, err => {
if (!err) return callback()

if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST') {
remove(dest, err => {
if (err) return callback(err)
options.overwrite = false // just overwriteed it, no need to do it again
options.overwrite = false // just removed it, so no need to overwrite
move(src, dest, options, callback)
})
return
}

// weird Windows shit
// weird Windows issue
if (err.code === 'EPERM') {
setTimeout(() => {
remove(dest, err => {
Expand All @@ -58,18 +66,10 @@ function move (src, dest, options, callback) {
return
}

if (err.code !== 'EXDEV') return callback(err)
moveAcrossDevice(src, dest, overwrite, callback)
})
} else {
fs.link(src, dest, err => {
if (err) {
if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
return moveAcrossDevice(src, dest, overwrite, callback)
}
return callback(err)
if (err.code === 'EXDEV' || err.code === 'EISDIR') {
return moveAcrossDevice(src, dest, overwrite, callback)
}
return fs.unlink(src, callback)
callback(err)
})
}
}
Expand Down