|
|
@ -7,7 +7,6 @@ const pathExists = require('../path-exists').pathExists |
|
|
|
const utimes = require('../util/utimes').utimesMillis |
|
|
|
|
|
|
|
const notExist = Symbol('notExist') |
|
|
|
const existsReg = Symbol('existsReg') |
|
|
|
|
|
|
|
function copy (src, dest, opts, cb) { |
|
|
|
if (typeof opts === 'function' && !cb) { |
|
|
@ -29,56 +28,55 @@ function copy (src, dest, opts, cb) { |
|
|
|
see https://github.com/jprichardson/node-fs-extra/issues/269`)
|
|
|
|
} |
|
|
|
|
|
|
|
checkPaths(src, dest, (err, resolvedDest) => { |
|
|
|
checkPaths(src, dest, (err, destStat) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (opts.filter) return handleFilter(checkParentDir, resolvedDest, src, dest, opts, cb) |
|
|
|
return checkParentDir(resolvedDest, src, dest, opts, cb) |
|
|
|
if (opts.filter) return handleFilter(checkParentDir, destStat, src, dest, opts, cb) |
|
|
|
return checkParentDir(destStat, src, dest, opts, cb) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function checkParentDir (resolvedDest, src, dest, opts, cb) { |
|
|
|
function checkParentDir (destStat, src, dest, opts, cb) { |
|
|
|
const destParent = path.dirname(dest) |
|
|
|
pathExists(destParent, (err, dirExists) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (dirExists) return startCopy(resolvedDest, src, dest, opts, cb) |
|
|
|
if (dirExists) return startCopy(destStat, src, dest, opts, cb) |
|
|
|
mkdirp(destParent, err => { |
|
|
|
if (err) return cb(err) |
|
|
|
return startCopy(resolvedDest, src, dest, opts, cb) |
|
|
|
return startCopy(destStat, src, dest, opts, cb) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function startCopy (resolvedDest, src, dest, opts, cb) { |
|
|
|
if (opts.filter) return handleFilter(getStats, resolvedDest, src, dest, opts, cb) |
|
|
|
return getStats(resolvedDest, src, dest, opts, cb) |
|
|
|
} |
|
|
|
|
|
|
|
function handleFilter (onInclude, resolvedDest, src, dest, opts, cb) { |
|
|
|
function handleFilter (onInclude, destStat, src, dest, opts, cb) { |
|
|
|
Promise.resolve(opts.filter(src, dest)).then(include => { |
|
|
|
if (include) { |
|
|
|
if (resolvedDest) return onInclude(resolvedDest, src, dest, opts, cb) |
|
|
|
if (destStat) return onInclude(destStat, src, dest, opts, cb) |
|
|
|
return onInclude(src, dest, opts, cb) |
|
|
|
} |
|
|
|
return cb() |
|
|
|
}, error => cb(error)) |
|
|
|
} |
|
|
|
|
|
|
|
function getStats (resolvedDest, src, dest, opts, cb) { |
|
|
|
function startCopy (destStat, src, dest, opts, cb) { |
|
|
|
if (opts.filter) return handleFilter(getStats, destStat, src, dest, opts, cb) |
|
|
|
return getStats(destStat, src, dest, opts, cb) |
|
|
|
} |
|
|
|
|
|
|
|
function getStats (destStat, src, dest, opts, cb) { |
|
|
|
const stat = opts.dereference ? fs.stat : fs.lstat |
|
|
|
stat(src, (err, st) => { |
|
|
|
stat(src, (err, srcStat) => { |
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
if (st.isDirectory()) return onDir(st, resolvedDest, src, dest, opts, cb) |
|
|
|
else if (st.isFile() || |
|
|
|
st.isCharacterDevice() || |
|
|
|
st.isBlockDevice()) return onFile(st, resolvedDest, src, dest, opts, cb) |
|
|
|
else if (st.isSymbolicLink()) return onLink(resolvedDest, src, dest, opts, cb) |
|
|
|
if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts, cb) |
|
|
|
else if (srcStat.isFile() || |
|
|
|
srcStat.isCharacterDevice() || |
|
|
|
srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts, cb) |
|
|
|
else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts, cb) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function onFile (srcStat, resolvedDest, src, dest, opts, cb) { |
|
|
|
if (resolvedDest === notExist) return copyFile(srcStat, src, dest, opts, cb) |
|
|
|
else if (resolvedDest === existsReg) return mayCopyFile(srcStat, src, dest, opts, cb) |
|
|
|
function onFile (srcStat, destStat, src, dest, opts, cb) { |
|
|
|
if (destStat === notExist) return copyFile(srcStat, src, dest, opts, cb) |
|
|
|
return mayCopyFile(srcStat, src, dest, opts, cb) |
|
|
|
} |
|
|
|
|
|
|
@ -123,31 +121,14 @@ function setDestModeAndTimestamps (srcStat, dest, opts, cb) { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function onDir (srcStat, resolvedDest, src, dest, opts, cb) { |
|
|
|
if (resolvedDest === notExist) { |
|
|
|
if (isSrcSubdir(src, dest)) { |
|
|
|
return cb(new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`)) |
|
|
|
} |
|
|
|
return mkDirAndCopy(srcStat, src, dest, opts, cb) |
|
|
|
} else if (resolvedDest === existsReg) { |
|
|
|
if (isSrcSubdir(src, dest)) { |
|
|
|
return cb(new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`)) |
|
|
|
} |
|
|
|
return mayCopyDir(src, dest, opts, cb) |
|
|
|
function onDir (srcStat, destStat, src, dest, opts, cb) { |
|
|
|
if (destStat === notExist) return mkDirAndCopy(srcStat, src, dest, opts, cb) |
|
|
|
if (destStat && !destStat.isDirectory()) { |
|
|
|
return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)) |
|
|
|
} |
|
|
|
return copyDir(src, dest, opts, cb) |
|
|
|
} |
|
|
|
|
|
|
|
function mayCopyDir (src, dest, opts, cb) { |
|
|
|
fs.stat(dest, (err, st) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (!st.isDirectory()) { |
|
|
|
return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)) |
|
|
|
} |
|
|
|
return copyDir(src, dest, opts, cb) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function mkDirAndCopy (srcStat, src, dest, opts, cb) { |
|
|
|
fs.mkdir(dest, srcStat.mode, err => { |
|
|
|
if (err) return cb(err) |
|
|
@ -174,16 +155,16 @@ function copyDirItems (items, src, dest, opts, cb) { |
|
|
|
function copyDirItem (items, item, src, dest, opts, cb) { |
|
|
|
const srcItem = path.join(src, item) |
|
|
|
const destItem = path.join(dest, item) |
|
|
|
checkPaths(srcItem, destItem, (err, resolvedDest) => { |
|
|
|
checkPaths(srcItem, destItem, (err, destStat) => { |
|
|
|
if (err) return cb(err) |
|
|
|
startCopy(resolvedDest, srcItem, destItem, opts, err => { |
|
|
|
startCopy(destStat, srcItem, destItem, opts, err => { |
|
|
|
if (err) return cb(err) |
|
|
|
return copyDirItems(items, src, dest, opts, cb) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function onLink (resolvedDest, src, dest, opts, cb) { |
|
|
|
function onLink (destStat, src, dest, opts, cb) { |
|
|
|
fs.readlink(src, (err, resolvedSrc) => { |
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
@ -191,22 +172,28 @@ function onLink (resolvedDest, src, dest, opts, cb) { |
|
|
|
resolvedSrc = path.resolve(process.cwd(), resolvedSrc) |
|
|
|
} |
|
|
|
|
|
|
|
if (resolvedDest === notExist || resolvedDest === existsReg) { |
|
|
|
// if dest already exists, fs throws error anyway,
|
|
|
|
// so no need to guard against it here.
|
|
|
|
if (destStat === notExist) { |
|
|
|
return fs.symlink(resolvedSrc, dest, cb) |
|
|
|
} else { |
|
|
|
if (opts.dereference) { |
|
|
|
resolvedDest = path.resolve(process.cwd(), resolvedDest) |
|
|
|
} |
|
|
|
if (pathsAreIdentical(resolvedSrc, resolvedDest)) return cb() |
|
|
|
fs.readlink(dest, (err, resolvedDest) => { |
|
|
|
if (err) { |
|
|
|
// dest exists and is a regular file or directory,
|
|
|
|
// Windows may throw UNKNOWN error. If dest already exists,
|
|
|
|
// fs throws error anyway, so no need to guard against it here.
|
|
|
|
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlink(resolvedSrc, dest, cb) |
|
|
|
return cb(err) |
|
|
|
} |
|
|
|
if (opts.dereference) { |
|
|
|
resolvedDest = path.resolve(process.cwd(), resolvedDest) |
|
|
|
} |
|
|
|
if (isSrcSubdir(resolvedSrc, resolvedDest)) { |
|
|
|
return cb(new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)) |
|
|
|
} |
|
|
|
|
|
|
|
// prevent copy if src is a subdir of dest since unlinking
|
|
|
|
// dest in this case would result in removing src contents
|
|
|
|
// and therefore a broken symlink would be created.
|
|
|
|
fs.stat(dest, (err, st) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (st.isDirectory() && isSrcSubdir(resolvedDest, resolvedSrc)) { |
|
|
|
// do not copy if src is a subdir of dest since unlinking
|
|
|
|
// dest in this case would result in removing src contents
|
|
|
|
// and therefore a broken symlink would be created.
|
|
|
|
if (destStat.isDirectory() && isSrcSubdir(resolvedDest, resolvedSrc)) { |
|
|
|
return cb(new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)) |
|
|
|
} |
|
|
|
return copyLink(resolvedSrc, dest, cb) |
|
|
@ -223,53 +210,36 @@ function copyLink (resolvedSrc, dest, cb) { |
|
|
|
} |
|
|
|
|
|
|
|
// 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) { |
|
|
|
const srcArray = path.resolve(src).split(path.sep) |
|
|
|
const destArray = path.resolve(dest).split(path.sep) |
|
|
|
|
|
|
|
return srcArray.reduce((acc, current, i) => { |
|
|
|
return acc && destArray[i] === current |
|
|
|
}, true) |
|
|
|
return srcArray.reduce((acc, current, i) => acc && destArray[i] === current, true) |
|
|
|
} |
|
|
|
|
|
|
|
// check if dest exists and is a symlink.
|
|
|
|
function checkDest (dest, cb) { |
|
|
|
fs.readlink(dest, (err, resolvedPath) => { |
|
|
|
if (err) { |
|
|
|
if (err.code === 'ENOENT') return cb(null, notExist) |
|
|
|
|
|
|
|
// dest exists and is a regular file or directory, Windows may throw UNKNOWN error.
|
|
|
|
if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return cb(null, existsReg) |
|
|
|
|
|
|
|
return cb(err) |
|
|
|
} |
|
|
|
return cb(null, resolvedPath) // dest exists and is a symlink
|
|
|
|
function checkStats (src, dest, cb) { |
|
|
|
fs.stat(src, (err, srcStat) => { |
|
|
|
if (err) return cb(err) |
|
|
|
fs.stat(dest, (err, destStat) => { |
|
|
|
if (err) { |
|
|
|
if (err.code === 'ENOENT') return cb(null, {srcStat, destStat: notExist}) |
|
|
|
return cb(err) |
|
|
|
} |
|
|
|
return cb(null, {srcStat, destStat}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function pathsAreIdentical (src, dest) { |
|
|
|
const os = process.platform |
|
|
|
const resolvedSrc = path.resolve(src) |
|
|
|
const resolvedDest = path.resolve(dest) |
|
|
|
// case-insensitive paths
|
|
|
|
if (os === 'darwin' || os === 'win32') { |
|
|
|
return resolvedSrc.toLowerCase() === resolvedDest.toLowerCase() |
|
|
|
} |
|
|
|
return resolvedSrc === resolvedDest |
|
|
|
} |
|
|
|
|
|
|
|
function checkPaths (src, dest, cb) { |
|
|
|
checkDest(dest, (err, resolvedDest) => { |
|
|
|
checkStats(src, dest, (err, stats) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (resolvedDest === notExist || resolvedDest === existsReg) { |
|
|
|
if (pathsAreIdentical(src, dest)) return cb(new Error('Source and destination must not be the same.')) |
|
|
|
return cb(null, resolvedDest) |
|
|
|
} else { |
|
|
|
// check resolved dest path if dest is a symlink
|
|
|
|
if (pathsAreIdentical(src, resolvedDest)) return cb(new Error('Source and destination must not be the same.')) |
|
|
|
return cb(null, resolvedDest) |
|
|
|
const {srcStat, destStat} = stats |
|
|
|
if (destStat.ino && destStat.ino === srcStat.ino) { |
|
|
|
return cb(new Error('Source and destination must not be the same.')) |
|
|
|
} |
|
|
|
if (srcStat.isDirectory() && isSrcSubdir(src, dest)) { |
|
|
|
return cb(new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`)) |
|
|
|
} |
|
|
|
return cb(null, destStat) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|