Skip to content

Commit 5cf4616

Browse files
committed
feat(move-file): add move fallback for weird errors
1 parent bdd00bf commit 5cf4616

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

lib/util/move-file.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict'
22

3-
var fs = require('graceful-fs')
3+
const fs = require('graceful-fs')
4+
const Promise = require('bluebird')
5+
let move
6+
let pinflight
47

58
module.exports = moveFile
6-
function moveFile (src, dest, cb) {
9+
function moveFile (src, dest) {
710
// This isn't quite an fs.rename -- the assumption is that
811
// if `dest` already exists, and we get certain errors while
912
// trying to move it, we should just not bother.
@@ -13,16 +16,35 @@ function moveFile (src, dest, cb) {
1316
// content their own way.
1417
//
1518
// Note that, as the name suggests, this strictly only supports file moves.
16-
fs.link(src, dest, function (err) {
17-
if (err) {
18-
if (err.code === 'EEXIST' || err.code === 'EBUSY') {
19-
// file already exists, so whatever
20-
} else if (err.code === 'EPERM' && process.platform === 'win32') {
21-
// file handle stayed open even past graceful-fs limits
22-
} else {
23-
return cb(err)
19+
return Promise.fromNode(cb => {
20+
fs.link(src, dest, err => {
21+
if (err) {
22+
if (err.code === 'EEXIST' || err.code === 'EBUSY') {
23+
// file already exists, so whatever
24+
} else if (err.code === 'EPERM' && process.platform === 'win32') {
25+
// file handle stayed open even past graceful-fs limits
26+
} else {
27+
return cb(err)
28+
}
2429
}
30+
return fs.unlink(src, cb)
31+
})
32+
}).catch(err => {
33+
if (process.platform !== 'win32') {
34+
throw err
35+
} else {
36+
if (!pinflight) { pinflight = require('promise-inflight') }
37+
return pinflight('cacache-move-file:' + dest, () => {
38+
return Promise.promisify(fs.stat)(dest).catch(err => {
39+
if (err !== 'ENOENT') {
40+
// Something else is wrong here. Bail bail bail
41+
throw err
42+
}
43+
// file doesn't already exist! let's try a rename -> copy fallback
44+
if (!move) { move = require('@npmcorp/move') }
45+
return move(src, dest, { Promise, fs })
46+
})
47+
})
2548
}
26-
fs.unlink(src, cb)
2749
})
2850
}

0 commit comments

Comments
 (0)