diff --git a/lib/copy/__tests__/copy-broken-symlink.test.js b/lib/copy/__tests__/copy-broken-symlink.test.js index 81e46dea..68d989d5 100644 --- a/lib/copy/__tests__/copy-broken-symlink.test.js +++ b/lib/copy/__tests__/copy-broken-symlink.test.js @@ -5,7 +5,7 @@ const os = require('os') const fse = require('../..') const path = require('path') const assert = require('assert') -const copy = require('../copy') +const { copy } = require('../') /* global afterEach, beforeEach, describe, it */ diff --git a/lib/copy/__tests__/copy-preserve-timestamp.test.js b/lib/copy/__tests__/copy-preserve-timestamp.test.js index 6f1336f4..64ee5a3d 100644 --- a/lib/copy/__tests__/copy-preserve-timestamp.test.js +++ b/lib/copy/__tests__/copy-preserve-timestamp.test.js @@ -3,7 +3,7 @@ const fs = require('../../') const os = require('os') const path = require('path') -const copy = require('../copy') +const { copy } = require('../') const utimesSync = require('../../util/utimes').utimesMillisSync const assert = require('assert') diff --git a/lib/copy/__tests__/ncp/broken-symlink.test.js b/lib/copy/__tests__/ncp/broken-symlink.test.js index 956daa86..7a8db699 100644 --- a/lib/copy/__tests__/ncp/broken-symlink.test.js +++ b/lib/copy/__tests__/ncp/broken-symlink.test.js @@ -3,7 +3,7 @@ const fs = require('fs') const os = require('os') const fse = require('../../..') -const ncp = require('../../copy') +const { copy: ncp } = require('../../') const path = require('path') const assert = require('assert') diff --git a/lib/copy/__tests__/ncp/ncp-error-perm.test.js b/lib/copy/__tests__/ncp/ncp-error-perm.test.js index 34b6af1a..563e0aba 100644 --- a/lib/copy/__tests__/ncp/ncp-error-perm.test.js +++ b/lib/copy/__tests__/ncp/ncp-error-perm.test.js @@ -5,7 +5,7 @@ const fs = require('fs') const os = require('os') const fse = require('../../..') -const ncp = require('../../copy') +const { copy: ncp } = require('../../') const path = require('path') const assert = require('assert') diff --git a/lib/copy/__tests__/ncp/ncp.test.js b/lib/copy/__tests__/ncp/ncp.test.js index 2b85437a..5de57c09 100644 --- a/lib/copy/__tests__/ncp/ncp.test.js +++ b/lib/copy/__tests__/ncp/ncp.test.js @@ -1,7 +1,7 @@ 'use strict' const fs = require('fs') -const ncp = require('../../copy') +const { copy: ncp } = require('../../') const path = require('path') const rimraf = require('rimraf') const assert = require('assert') diff --git a/lib/copy/__tests__/ncp/symlink.test.js b/lib/copy/__tests__/ncp/symlink.test.js index 5c188b6f..6845ce38 100644 --- a/lib/copy/__tests__/ncp/symlink.test.js +++ b/lib/copy/__tests__/ncp/symlink.test.js @@ -3,7 +3,7 @@ const fs = require('fs') const os = require('os') const fse = require('../../..') -const ncp = require('../../copy') +const { copy: ncp } = require('../../') const path = require('path') const assert = require('assert') diff --git a/lib/copy/copy.js b/lib/copy/copy.js index bc188fb6..56560210 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -1,23 +1,17 @@ 'use strict' -const fs = require('graceful-fs') +const fs = require('../fs') const path = require('path') -const mkdirs = require('../mkdirs').mkdirs -const pathExists = require('../path-exists').pathExists -const utimesMillis = require('../util/utimes').utimesMillis +const { mkdirs } = require('../mkdirs') +const { pathExists } = require('../path-exists') +const { utimesMillis } = require('../util/utimes') const stat = require('../util/stat') -function copy (src, dest, opts, cb) { - if (typeof opts === 'function' && !cb) { - cb = opts - opts = {} - } else if (typeof opts === 'function') { +async function copy (src, dest, opts = {}) { + if (typeof opts === 'function') { opts = { filter: opts } } - cb = cb || function () {} - opts = opts || {} - opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber @@ -30,209 +24,152 @@ function copy (src, dest, opts, cb) { ) } - stat.checkPaths(src, dest, 'copy', opts, (err, stats) => { - if (err) return cb(err) - const { srcStat, destStat } = stats - stat.checkParentPaths(src, srcStat, dest, 'copy', err => { - if (err) return cb(err) - runFilter(src, dest, opts, (err, include) => { - if (err) return cb(err) - if (!include) return cb() - - checkParentDir(destStat, src, dest, opts, cb) - }) - }) - }) -} + const { srcStat, destStat } = await stat.checkPaths(src, dest, 'copy', opts) + + await stat.checkParentPaths(src, srcStat, dest, 'copy') + + const include = await runFilter(src, dest, opts) + + if (!include) return -function checkParentDir (destStat, src, dest, opts, cb) { + // check if the parent of dest exists, and create it if it doesn't exist const destParent = path.dirname(dest) - pathExists(destParent, (err, dirExists) => { - if (err) return cb(err) - if (dirExists) return getStats(destStat, src, dest, opts, cb) - mkdirs(destParent, err => { - if (err) return cb(err) - return getStats(destStat, src, dest, opts, cb) - }) - }) -} + const dirExists = await pathExists(destParent) + if (!dirExists) { + await mkdirs(destParent) + } -function runFilter (src, dest, opts, cb) { - if (!opts.filter) return cb(null, true) - Promise.resolve(opts.filter(src, dest)) - .then(include => cb(null, include), error => cb(error)) + await getStatsAndPerformCopy(destStat, src, dest, opts) } -function getStats (destStat, src, dest, opts, cb) { - const stat = opts.dereference ? fs.stat : fs.lstat - stat(src, (err, srcStat) => { - if (err) return cb(err) - - 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) - else if (srcStat.isSocket()) return cb(new Error(`Cannot copy a socket file: ${src}`)) - else if (srcStat.isFIFO()) return cb(new Error(`Cannot copy a FIFO pipe: ${src}`)) - return cb(new Error(`Unknown file: ${src}`)) - }) +async function runFilter (src, dest, opts) { + if (!opts.filter) return true + return opts.filter(src, dest) } -function onFile (srcStat, destStat, src, dest, opts, cb) { - if (!destStat) return copyFile(srcStat, src, dest, opts, cb) - return mayCopyFile(srcStat, src, dest, opts, cb) +async function getStatsAndPerformCopy (destStat, src, dest, opts) { + const statFn = opts.dereference ? fs.stat : fs.lstat + const srcStat = await statFn(src) + + if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts) + + if ( + srcStat.isFile() || + srcStat.isCharacterDevice() || + srcStat.isBlockDevice() + ) return onFile(srcStat, destStat, src, dest, opts) + + if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts) + if (srcStat.isSocket()) throw new Error(`Cannot copy a socket file: ${src}`) + if (srcStat.isFIFO()) throw new Error(`Cannot copy a FIFO pipe: ${src}`) + throw new Error(`Unknown file: ${src}`) } -function mayCopyFile (srcStat, src, dest, opts, cb) { +async function onFile (srcStat, destStat, src, dest, opts) { + if (!destStat) return copyFile(srcStat, src, dest, opts) + if (opts.overwrite) { - fs.unlink(dest, err => { - if (err) return cb(err) - return copyFile(srcStat, src, dest, opts, cb) - }) - } else if (opts.errorOnExist) { - return cb(new Error(`'${dest}' already exists`)) - } else return cb() + await fs.unlink(dest) + return copyFile(srcStat, src, dest, opts) + } + if (opts.errorOnExist) { + throw new Error(`'${dest}' already exists`) + } } -function copyFile (srcStat, src, dest, opts, cb) { - fs.copyFile(src, dest, err => { - if (err) return cb(err) - if (opts.preserveTimestamps) return handleTimestampsAndMode(srcStat.mode, src, dest, cb) - return setDestMode(dest, srcStat.mode, cb) - }) -} +async function copyFile (srcStat, src, dest, opts) { + await fs.copyFile(src, dest) + if (opts.preserveTimestamps) { + // Make sure the file is writable before setting the timestamp + // otherwise open fails with EPERM when invoked with 'r+' + // (through utimes call) + if (fileIsNotWritable(srcStat.mode)) { + await makeFileWritable(dest, srcStat.mode) + } -function handleTimestampsAndMode (srcMode, src, dest, cb) { - // Make sure the file is writable before setting the timestamp - // otherwise open fails with EPERM when invoked with 'r+' - // (through utimes call) - if (fileIsNotWritable(srcMode)) { - return makeFileWritable(dest, srcMode, err => { - if (err) return cb(err) - return setDestTimestampsAndMode(srcMode, src, dest, cb) - }) + // Set timestamps and mode correspondingly + + // Note that The initial srcStat.atime cannot be trusted + // because it is modified by the read(2) system call + // (See https://nodejs.org/api/fs.html#fs_stat_time_values) + const updatedSrcStat = await fs.stat(src) + await utimesMillis(dest, updatedSrcStat.atime, updatedSrcStat.mtime) } - return setDestTimestampsAndMode(srcMode, src, dest, cb) + + return fs.chmod(dest, srcStat.mode) } function fileIsNotWritable (srcMode) { return (srcMode & 0o200) === 0 } -function makeFileWritable (dest, srcMode, cb) { - return setDestMode(dest, srcMode | 0o200, cb) -} - -function setDestTimestampsAndMode (srcMode, src, dest, cb) { - setDestTimestamps(src, dest, err => { - if (err) return cb(err) - return setDestMode(dest, srcMode, cb) - }) +function makeFileWritable (dest, srcMode) { + return fs.chmod(dest, srcMode | 0o200) } -function setDestMode (dest, srcMode, cb) { - return fs.chmod(dest, srcMode, cb) -} +async function onDir (srcStat, destStat, src, dest, opts) { + // the dest directory might not exist, create it + if (!destStat) { + await fs.mkdir(dest) + } -function setDestTimestamps (src, dest, cb) { - // The initial srcStat.atime cannot be trusted - // because it is modified by the read(2) system call - // (See https://nodejs.org/api/fs.html#fs_stat_time_values) - fs.stat(src, (err, updatedSrcStat) => { - if (err) return cb(err) - return utimesMillis(dest, updatedSrcStat.atime, updatedSrcStat.mtime, cb) - }) -} + // loop through the files in the current directory to copy everything + for (const item of await fs.readdir(src)) { + const srcItem = path.join(src, item) + const destItem = path.join(dest, item) -function onDir (srcStat, destStat, src, dest, opts, cb) { - if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts, cb) - return copyDir(src, dest, opts, cb) -} + // skip the item if it is matches by the filter function + const include = await runFilter(srcItem, destItem, opts) + if (!include) continue -function mkDirAndCopy (srcMode, src, dest, opts, cb) { - fs.mkdir(dest, err => { - if (err) return cb(err) - copyDir(src, dest, opts, err => { - if (err) return cb(err) - return setDestMode(dest, srcMode, cb) - }) - }) -} + const { destStat } = await stat.checkPaths(srcItem, destItem, 'copy', opts) -function copyDir (src, dest, opts, cb) { - fs.readdir(src, (err, items) => { - if (err) return cb(err) - return copyDirItems(items, src, dest, opts, cb) - }) -} + // If the item is a copyable file, `getStatsAndPerformCopy` will copy it + // If the item is a directory, `getStatsAndPerformCopy` will call `onDir` recursively + await getStatsAndPerformCopy(destStat, srcItem, destItem, opts) + } -function copyDirItems (items, src, dest, opts, cb) { - const item = items.pop() - if (!item) return cb() - return copyDirItem(items, item, src, dest, opts, cb) + if (!destStat) { + await fs.chmod(dest, srcStat.mode) + } } -function copyDirItem (items, item, src, dest, opts, cb) { - const srcItem = path.join(src, item) - const destItem = path.join(dest, item) - runFilter(srcItem, destItem, opts, (err, include) => { - if (err) return cb(err) - if (!include) return copyDirItems(items, src, dest, opts, cb) - - stat.checkPaths(srcItem, destItem, 'copy', opts, (err, stats) => { - if (err) return cb(err) - const { destStat } = stats - getStats(destStat, srcItem, destItem, opts, err => { - if (err) return cb(err) - return copyDirItems(items, src, dest, opts, cb) - }) - }) - }) -} +async function onLink (destStat, src, dest, opts) { + let resolvedSrc = await fs.readlink(src) + if (opts.dereference) { + resolvedSrc = path.resolve(process.cwd(), resolvedSrc) + } + if (!destStat) { + return fs.symlink(resolvedSrc, dest) + } -function onLink (destStat, src, dest, opts, cb) { - fs.readlink(src, (err, resolvedSrc) => { - if (err) return cb(err) - if (opts.dereference) { - resolvedSrc = path.resolve(process.cwd(), resolvedSrc) - } + let resolvedDest = null + try { + resolvedDest = await fs.readlink(dest) + } catch (e) { + // 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 (e.code === 'EINVAL' || e.code === 'UNKNOWN') return fs.symlink(resolvedSrc, dest) + throw e + } + if (opts.dereference) { + resolvedDest = path.resolve(process.cwd(), resolvedDest) + } + if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) { + throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`) + } - if (!destStat) { - return fs.symlink(resolvedSrc, dest, cb) - } else { - 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 (stat.isSrcSubdir(resolvedSrc, resolvedDest)) { - return cb(new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)) - } - - // 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 (stat.isSrcSubdir(resolvedDest, resolvedSrc)) { - return cb(new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)) - } - return copyLink(resolvedSrc, dest, cb) - }) - } - }) -} + // 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 (stat.isSrcSubdir(resolvedDest, resolvedSrc)) { + throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`) + } -function copyLink (resolvedSrc, dest, cb) { - fs.unlink(dest, err => { - if (err) return cb(err) - return fs.symlink(resolvedSrc, dest, cb) - }) + // copy the link + await fs.unlink(dest) + return fs.symlink(resolvedSrc, dest) } module.exports = copy diff --git a/lib/copy/index.js b/lib/copy/index.js index 45c07a2f..2e31d278 100644 --- a/lib/copy/index.js +++ b/lib/copy/index.js @@ -1,6 +1,6 @@ 'use strict' -const u = require('universalify').fromCallback +const u = require('universalify').fromPromise module.exports = { copy: u(require('./copy')), copySync: require('./copy-sync') diff --git a/lib/util/__tests__/utimes.test.js b/lib/util/__tests__/utimes.test.js index d28ece19..02c65d1f 100644 --- a/lib/util/__tests__/utimes.test.js +++ b/lib/util/__tests__/utimes.test.js @@ -6,6 +6,8 @@ const fse = require('../..') const path = require('path') const assert = require('assert') const proxyquire = require('proxyquire') +const u = require('universalify').fromCallback + let gracefulFsStub let utimes @@ -33,7 +35,7 @@ describe('utimes', () => { fse.emptyDir(TEST_DIR, done) // reset stubs gracefulFsStub = {} - utimes = proxyquire('../utimes', { 'graceful-fs': gracefulFsStub }) + utimes = proxyquire('../utimes', { '../fs': gracefulFsStub }) }) describe('utimesMillis()', () => { @@ -68,25 +70,25 @@ describe('utimes', () => { it('should close open file desciptors after encountering an error', done => { const fakeFd = Math.random() - gracefulFsStub.open = (pathIgnored, flagsIgnored, modeIgnored, callback) => { + gracefulFsStub.open = u((pathIgnored, flagsIgnored, modeIgnored, callback) => { if (typeof modeIgnored === 'function') callback = modeIgnored process.nextTick(() => callback(null, fakeFd)) - } + }) let closeCalled = false - gracefulFsStub.close = (fd, callback) => { + gracefulFsStub.close = u((fd, callback) => { assert.strictEqual(fd, fakeFd) closeCalled = true if (callback) process.nextTick(callback) - } + }) let testError - gracefulFsStub.futimes = (fd, atimeIgnored, mtimeIgnored, callback) => { + gracefulFsStub.futimes = u((fd, atimeIgnored, mtimeIgnored, callback) => { process.nextTick(() => { testError = new Error('A test error') callback(testError) }) - } + }) utimes.utimesMillis('ignored', 'ignored', 'ignored', err => { assert.strictEqual(err, testError) diff --git a/lib/util/stat.js b/lib/util/stat.js index 2efd1c33..dfd37d9c 100644 --- a/lib/util/stat.js +++ b/lib/util/stat.js @@ -2,7 +2,7 @@ const fs = require('../fs') const path = require('path') -const util = require('util') +const u = require('universalify').fromPromise function getStats (src, dest, opts) { const statFunc = opts.dereference @@ -32,35 +32,32 @@ function getStatsSync (src, dest, opts) { return { srcStat, destStat } } -function checkPaths (src, dest, funcName, opts, cb) { - util.callbackify(getStats)(src, dest, opts, (err, stats) => { - if (err) return cb(err) - const { srcStat, destStat } = stats - - if (destStat) { - if (areIdentical(srcStat, destStat)) { - const srcBaseName = path.basename(src) - const destBaseName = path.basename(dest) - if (funcName === 'move' && - srcBaseName !== destBaseName && - srcBaseName.toLowerCase() === destBaseName.toLowerCase()) { - return cb(null, { srcStat, destStat, isChangingCase: true }) - } - return cb(new Error('Source and destination must not be the same.')) - } - if (srcStat.isDirectory() && !destStat.isDirectory()) { - return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)) - } - if (!srcStat.isDirectory() && destStat.isDirectory()) { - return cb(new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`)) +async function checkPaths (src, dest, funcName, opts) { + const { srcStat, destStat } = await getStats(src, dest, opts) + if (destStat) { + if (areIdentical(srcStat, destStat)) { + const srcBaseName = path.basename(src) + const destBaseName = path.basename(dest) + if (funcName === 'move' && + srcBaseName !== destBaseName && + srcBaseName.toLowerCase() === destBaseName.toLowerCase()) { + return { srcStat, destStat, isChangingCase: true } } + throw new Error('Source and destination must not be the same.') } - - if (srcStat.isDirectory() && isSrcSubdir(src, dest)) { - return cb(new Error(errMsg(src, dest, funcName))) + if (srcStat.isDirectory() && !destStat.isDirectory()) { + throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`) + } + if (!srcStat.isDirectory() && destStat.isDirectory()) { + throw new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`) } - return cb(null, { srcStat, destStat }) - }) + } + + if (srcStat.isDirectory() && isSrcSubdir(src, dest)) { + throw new Error(errMsg(src, dest, funcName)) + } + + return { srcStat, destStat } } function checkPathsSync (src, dest, funcName, opts) { @@ -95,20 +92,24 @@ function checkPathsSync (src, dest, funcName, opts) { // It works for all file types including symlinks since it // checks the src and dest inodes. It starts from the deepest // parent and stops once it reaches the src parent or the root path. -function checkParentPaths (src, srcStat, dest, funcName, cb) { +async function checkParentPaths (src, srcStat, dest, funcName) { const srcParent = path.resolve(path.dirname(src)) const destParent = path.resolve(path.dirname(dest)) - if (destParent === srcParent || destParent === path.parse(destParent).root) return cb() - fs.stat(destParent, { bigint: true }, (err, destStat) => { - if (err) { - if (err.code === 'ENOENT') return cb() - return cb(err) - } - if (areIdentical(srcStat, destStat)) { - return cb(new Error(errMsg(src, dest, funcName))) - } - return checkParentPaths(src, srcStat, destParent, funcName, cb) - }) + if (destParent === srcParent || destParent === path.parse(destParent).root) return + + let destStat + try { + destStat = await fs.stat(destParent, { bigint: true }) + } catch (err) { + if (err.code === 'ENOENT') return + throw err + } + + if (areIdentical(srcStat, destStat)) { + throw new Error(errMsg(src, dest, funcName)) + } + + return checkParentPaths(src, srcStat, destParent, funcName) } function checkParentPathsSync (src, srcStat, dest, funcName) { @@ -145,10 +146,13 @@ function errMsg (src, dest, funcName) { } module.exports = { - checkPaths, + // checkPaths + checkPaths: u(checkPaths), checkPathsSync, - checkParentPaths, + // checkParent + checkParentPaths: u(checkParentPaths), checkParentPathsSync, + // Misc isSrcSubdir, areIdentical } diff --git a/lib/util/utimes.js b/lib/util/utimes.js index 75395def..87f4588c 100644 --- a/lib/util/utimes.js +++ b/lib/util/utimes.js @@ -1,17 +1,27 @@ 'use strict' -const fs = require('graceful-fs') +const fs = require('../fs') +const u = require('universalify').fromPromise -function utimesMillis (path, atime, mtime, callback) { +async function utimesMillis (path, atime, mtime) { // if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback) - fs.open(path, 'r+', (err, fd) => { - if (err) return callback(err) - fs.futimes(fd, atime, mtime, futimesErr => { - fs.close(fd, closeErr => { - if (callback) callback(futimesErr || closeErr) - }) - }) - }) + const fd = await fs.open(path, 'r+') + + let closeErr = null + + try { + await fs.futimes(fd, atime, mtime) + } finally { + try { + await fs.close(fd) + } catch (e) { + closeErr = e + } + } + + if (closeErr) { + throw closeErr + } } function utimesMillisSync (path, atime, mtime) { @@ -21,6 +31,6 @@ function utimesMillisSync (path, atime, mtime) { } module.exports = { - utimesMillis, + utimesMillis: u(utimesMillis), utimesMillisSync }