From 56428e3fac8456046a37b2c0b27215ac79e75422 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 19 Oct 2023 23:24:01 +0800 Subject: [PATCH] Refactor `copy` API in async/await --- lib/copy/copy.js | 312 ++++++++++++++++++++++----------------------- lib/copy/index.js | 2 +- lib/util/stat.js | 55 ++++++++ lib/util/utimes.js | 34 +++++ 4 files changed, 240 insertions(+), 163 deletions(-) diff --git a/lib/copy/copy.js b/lib/copy/copy.js index bc188fb63..7dca983a4 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -2,20 +2,29 @@ const fs = require('graceful-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 { utimesMillisAsync } = 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') { +const { promisify } = require('util') + +// TODO: re-exports promisified version of graceful-fs functions from lib/fs/promise.js +const fs$stat = promisify(fs.stat) +const fs$lstat = promisify(fs.lstat) +const fs$unlink = promisify(fs.unlink) +const fs$copyFile = promisify(fs.copyFile) +const fs$mkdir = promisify(fs.mkdir) +const fs$chmod = promisify(fs.chmod) +const fs$readdir = promisify(fs.readdir) +const fs$readlink = promisify(fs.readlink) +const fs$symlink = promisify(fs.symlink) + +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 @@ -30,209 +39,188 @@ 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() + const { srcStat, destStat } = await stat.checkPathsAsync(src, dest, 'copy', opts) - checkParentDir(destStat, src, dest, opts, cb) - }) - }) - }) + await stat.checkParentPathsAsync(src, srcStat, dest, 'copy') + + const include = await runFilter(src, dest, opts) + + if (!include) return + + return checkParentDir(destStat, src, dest, opts) } -function checkParentDir (destStat, src, dest, opts, cb) { +async function checkParentDir (destStat, src, dest, opts) { 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) return getStats(destStat, src, dest, opts) + + const parentDirExists = await pathExists(destParent) + if (parentDirExists) return getStats(destStat, src, dest, opts) + + await mkdirs(destParent) + + return getStats(destStat, src, dest, opts) } -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)) +async function runFilter (src, dest, opts) { + if (!opts.filter) return true + return opts.filter(src, dest) } -function getStats (destStat, src, dest, opts, cb) { - const stat = opts.dereference ? fs.stat : fs.lstat - stat(src, (err, srcStat) => { - if (err) return cb(err) +async function getStats (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, 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}`)) - }) + 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 onFile (srcStat, destStat, src, dest, opts, cb) { - if (!destStat) return copyFile(srcStat, src, dest, opts, cb) - return mayCopyFile(srcStat, src, dest, opts, cb) +function onFile (srcStat, destStat, src, dest, opts) { + if (!destStat) return copyFile(srcStat, src, dest, opts) + return mayCopyFile(srcStat, src, dest, opts) } -function mayCopyFile (srcStat, src, dest, opts, cb) { +async function mayCopyFile (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) { + return handleTimestampsAndMode(srcStat.mode, src, dest) + } + return setDestMode(dest, srcStat.mode) } -function handleTimestampsAndMode (srcMode, src, dest, cb) { +async function handleTimestampsAndMode (srcMode, src, dest) { // 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) - }) + await makeFileWritable(dest, srcMode) + return setDestTimestampsAndMode(srcMode, src, dest) } - return setDestTimestampsAndMode(srcMode, src, dest, cb) + return setDestTimestampsAndMode(srcMode, src, dest) } function fileIsNotWritable (srcMode) { return (srcMode & 0o200) === 0 } -function makeFileWritable (dest, srcMode, cb) { - return setDestMode(dest, srcMode | 0o200, cb) +function makeFileWritable (dest, srcMode) { + return setDestMode(dest, srcMode | 0o200) } -function setDestTimestampsAndMode (srcMode, src, dest, cb) { - setDestTimestamps(src, dest, err => { - if (err) return cb(err) - return setDestMode(dest, srcMode, cb) - }) +async function setDestTimestampsAndMode (srcMode, src, dest) { + await setDestTimestamps(src, dest) + return setDestMode(dest, srcMode) } -function setDestMode (dest, srcMode, cb) { - return fs.chmod(dest, srcMode, cb) +function setDestMode (dest, srcMode) { + return fs$chmod(dest, srcMode) } -function setDestTimestamps (src, dest, cb) { +async function setDestTimestamps (src, dest) { // 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) - }) + // (See https://nodejs.org/api/fs.html#fs$stat_time_values) + const updatedSrcStat = await fs$stat(src) + + return utimesMillisAsync(dest, updatedSrcStat.atime, updatedSrcStat.mtime) } -function onDir (srcStat, destStat, src, dest, opts, cb) { - if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts, cb) - return copyDir(src, dest, opts, cb) +function onDir (srcStat, destStat, src, dest, opts) { + if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts) + return copyDir(src, dest, opts) } -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) - }) - }) +async function mkDirAndCopy (srcMode, src, dest, opts) { + await fs$mkdir(dest) + await copyDir(src, dest, opts) + + return setDestMode(dest, srcMode) } -function copyDir (src, dest, opts, cb) { - fs.readdir(src, (err, items) => { - if (err) return cb(err) - return copyDirItems(items, src, dest, opts, cb) - }) +async function copyDir (src, dest, opts) { + const items = await fs$readdir(src) + return copyDirItems(items, src, dest, opts) } -function copyDirItems (items, src, dest, opts, cb) { +function copyDirItems (items, src, dest, opts) { const item = items.pop() - if (!item) return cb() - return copyDirItem(items, item, src, dest, opts, cb) + if (!item) return + return copyDirItem(items, item, src, dest, opts) } -function copyDirItem (items, item, src, dest, opts, cb) { +async function copyDirItem (items, item, src, dest, opts) { 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) - }) - }) - }) -} - -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) - } - - 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) - }) - } - }) -} - -function copyLink (resolvedSrc, dest, cb) { - fs.unlink(dest, err => { - if (err) return cb(err) - return fs.symlink(resolvedSrc, dest, cb) - }) + + const include = await runFilter(srcItem, destItem, opts) + if (!include) return copyDirItems(items, src, dest, opts) + + const { destStat } = await stat.checkPathsAsync(srcItem, destItem, 'copy', opts) + + await getStats(destStat, srcItem, destItem, opts) + + return copyDirItems(items, src, dest, opts) +} + +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) + } + + let resolvedDest = null + try { + resolvedDest = await fs$readlink(dest) + } catch (e) { + 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}'.`) + } + + // 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}'.`) + } + + return copyLink(resolvedSrc, dest) +} + +async function copyLink (resolvedSrc, dest) { + 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 45c07a2fb..2e31d2786 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/stat.js b/lib/util/stat.js index 2efd1c33f..e375bfff5 100644 --- a/lib/util/stat.js +++ b/lib/util/stat.js @@ -32,6 +32,7 @@ function getStatsSync (src, dest, opts) { return { srcStat, destStat } } +// TODO: remove callback-based internal utility `checkPaths` once all API has migrated to Promise-based `checkPathsAsync` function checkPaths (src, dest, funcName, opts, cb) { util.callbackify(getStats)(src, dest, opts, (err, stats) => { if (err) return cb(err) @@ -63,6 +64,34 @@ function checkPaths (src, dest, funcName, opts, cb) { }) } +async function checkPathsAsync (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() && !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}'.`) + } + } + + if (srcStat.isDirectory() && isSrcSubdir(src, dest)) { + throw new Error(errMsg(src, dest, funcName)) + } + + return { srcStat, destStat } +} + function checkPathsSync (src, dest, funcName, opts) { const { srcStat, destStat } = getStatsSync(src, dest, opts) @@ -95,6 +124,7 @@ 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. +// TODO: remove callback-based internal utility `checkPaths` once all API has migrated to Promise-based `checkPathsAsync` function checkParentPaths (src, srcStat, dest, funcName, cb) { const srcParent = path.resolve(path.dirname(src)) const destParent = path.resolve(path.dirname(dest)) @@ -111,6 +141,26 @@ function checkParentPaths (src, srcStat, dest, funcName, cb) { }) } +async function checkParentPathsAsync (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 + + 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 checkParentPathsAsync(src, srcStat, destParent, funcName) +} + function checkParentPathsSync (src, srcStat, dest, funcName) { const srcParent = path.resolve(path.dirname(src)) const destParent = path.resolve(path.dirname(dest)) @@ -145,10 +195,15 @@ function errMsg (src, dest, funcName) { } module.exports = { + // checkPaths checkPaths, checkPathsSync, + checkPathsAsync, + // checkParent checkParentPaths, checkParentPathsSync, + checkParentPathsAsync, + // Misc isSrcSubdir, areIdentical } diff --git a/lib/util/utimes.js b/lib/util/utimes.js index 75395deff..2cea485d8 100644 --- a/lib/util/utimes.js +++ b/lib/util/utimes.js @@ -1,7 +1,13 @@ 'use strict' const fs = require('graceful-fs') +const { promisify } = require('util') +const open = promisify(fs.open) +const futimes = promisify(fs.futimes) +const close = promisify(fs.close) + +// TODO: remove `utimesMillis` once all internal usage has switched to the Promise-based `utimesMillisAsync` API function utimesMillis (path, atime, mtime, callback) { // if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback) fs.open(path, 'r+', (err, fd) => { @@ -14,6 +20,33 @@ function utimesMillis (path, atime, mtime, callback) { }) } +async function utimesMillisAsync (path, atime, mtime) { + // if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback) + const fd = await open(path, 'r+') + + let futimesErr = null + try { + await futimes(fd, atime, mtime) + } catch (e) { + futimesErr = e + } + + let closeErr = null + + try { + await close(fd) + } catch (e) { + closeErr = e + } + + if (futimesErr) { + throw futimesErr + } + if (closeErr) { + throw closeErr + } +} + function utimesMillisSync (path, atime, mtime) { const fd = fs.openSync(path, 'r+') fs.futimesSync(fd, atime, mtime) @@ -22,5 +55,6 @@ function utimesMillisSync (path, atime, mtime) { module.exports = { utimesMillis, + utimesMillisAsync, utimesMillisSync }