2017-05-24 15:10:37 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
// most of this code was written by Andrew Kelley
|
|
|
|
// licensed under the BSD license: see
|
|
|
|
// https://github.com/andrewrk/node-mv/blob/master/package.json
|
|
|
|
|
|
|
|
// this needs a cleanup
|
|
|
|
|
|
|
|
const u = require('universalify').fromCallback
|
|
|
|
const fs = require('graceful-fs')
|
|
|
|
const ncp = require('../copy/ncp')
|
|
|
|
const path = require('path')
|
|
|
|
const remove = require('../remove').remove
|
|
|
|
const mkdirp = require('../mkdirs').mkdirs
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function move (src, dest, options, callback) {
|
2017-05-24 15:10:37 +02:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const overwrite = options.overwrite || options.clobber || false
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
isSrcSubdir(src, dest, (err, itIs) => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
if (itIs) return callback(new Error(`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`))
|
2017-05-24 15:10:37 +02:00
|
|
|
mkdirp(path.dirname(dest), err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
doRename()
|
|
|
|
})
|
2017-08-14 05:01:11 +02:00
|
|
|
})
|
2017-05-24 15:10:37 +02:00
|
|
|
|
|
|
|
function doRename () {
|
2017-08-14 05:01:11 +02:00
|
|
|
if (path.resolve(src) === path.resolve(dest)) {
|
|
|
|
fs.access(src, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
} else if (overwrite) {
|
2017-08-14 05:01:11 +02:00
|
|
|
fs.rename(src, dest, err => {
|
2017-05-24 15:10:37 +02:00
|
|
|
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
|
2017-08-14 05:01:11 +02:00
|
|
|
move(src, dest, options, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// weird Windows shit
|
|
|
|
if (err.code === 'EPERM') {
|
|
|
|
setTimeout(() => {
|
|
|
|
remove(dest, err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
options.overwrite = false
|
2017-08-14 05:01:11 +02:00
|
|
|
move(src, dest, options, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
})
|
|
|
|
}, 200)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.code !== 'EXDEV') return callback(err)
|
2017-08-14 05:01:11 +02:00
|
|
|
moveAcrossDevice(src, dest, overwrite, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
})
|
|
|
|
} else {
|
2017-08-14 05:01:11 +02:00
|
|
|
fs.link(src, dest, err => {
|
2017-05-24 15:10:37 +02:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
|
2017-08-14 05:01:11 +02:00
|
|
|
return moveAcrossDevice(src, dest, overwrite, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
return callback(err)
|
2017-05-24 15:10:37 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
return fs.unlink(src, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function moveAcrossDevice (src, dest, overwrite, callback) {
|
|
|
|
fs.stat(src, (err, stat) => {
|
|
|
|
if (err) return callback(err)
|
2017-05-24 15:10:37 +02:00
|
|
|
|
|
|
|
if (stat.isDirectory()) {
|
2017-08-14 05:01:11 +02:00
|
|
|
moveDirAcrossDevice(src, dest, overwrite, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
} else {
|
2017-08-14 05:01:11 +02:00
|
|
|
moveFileAcrossDevice(src, dest, overwrite, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function moveFileAcrossDevice (src, dest, overwrite, callback) {
|
2017-05-24 15:10:37 +02:00
|
|
|
const flags = overwrite ? 'w' : 'wx'
|
2017-08-14 05:01:11 +02:00
|
|
|
const ins = fs.createReadStream(src)
|
2017-05-24 15:10:37 +02:00
|
|
|
const outs = fs.createWriteStream(dest, { flags })
|
|
|
|
|
|
|
|
ins.on('error', err => {
|
|
|
|
ins.destroy()
|
|
|
|
outs.destroy()
|
|
|
|
outs.removeListener('close', onClose)
|
|
|
|
|
|
|
|
// may want to create a directory but `out` line above
|
|
|
|
// creates an empty file for us: See #108
|
|
|
|
// don't care about error here
|
|
|
|
fs.unlink(dest, () => {
|
|
|
|
// note: `err` here is from the input stream errror
|
|
|
|
if (err.code === 'EISDIR' || err.code === 'EPERM') {
|
2017-08-14 05:01:11 +02:00
|
|
|
moveDirAcrossDevice(src, dest, overwrite, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
} else {
|
|
|
|
callback(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
outs.on('error', err => {
|
|
|
|
ins.destroy()
|
|
|
|
outs.destroy()
|
|
|
|
outs.removeListener('close', onClose)
|
|
|
|
callback(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
outs.once('close', onClose)
|
|
|
|
ins.pipe(outs)
|
|
|
|
|
|
|
|
function onClose () {
|
2017-08-14 05:01:11 +02:00
|
|
|
fs.unlink(src, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function moveDirAcrossDevice (src, dest, overwrite, callback) {
|
2017-05-24 15:10:37 +02:00
|
|
|
const options = {
|
|
|
|
overwrite: false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overwrite) {
|
|
|
|
remove(dest, err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
startNcp()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
startNcp()
|
|
|
|
}
|
|
|
|
|
|
|
|
function startNcp () {
|
2017-08-14 05:01:11 +02:00
|
|
|
ncp(src, dest, options, err => {
|
2017-05-24 15:10:37 +02:00
|
|
|
if (err) return callback(err)
|
2017-08-14 05:01:11 +02:00
|
|
|
remove(src, callback)
|
2017-05-24 15:10:37 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
// return true if dest is a subdir of src, otherwise false.
|
|
|
|
// extract dest base dir and check if that is the same as src basename
|
|
|
|
function isSrcSubdir (src, dest, cb) {
|
|
|
|
fs.stat(src, (err, st) => {
|
|
|
|
if (err) return cb(err)
|
|
|
|
if (st.isDirectory()) {
|
|
|
|
const baseDir = dest.split(path.dirname(src) + path.sep)[1]
|
|
|
|
if (baseDir) {
|
|
|
|
const destBasename = baseDir.split(path.sep)[0]
|
|
|
|
if (destBasename) return cb(null, src !== dest && dest.indexOf(src) > -1 && destBasename === path.basename(src))
|
|
|
|
return cb(null, false)
|
|
|
|
}
|
|
|
|
return cb(null, false)
|
|
|
|
}
|
|
|
|
return cb(null, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-24 15:10:37 +02:00
|
|
|
module.exports = {
|
|
|
|
move: u(move)
|
|
|
|
}
|