Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/stat: fix max call stack size exceeded #679

Merged
merged 1 commit into from May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions lib/util/__tests__/stat.test.js
@@ -0,0 +1,80 @@
'use strict'

const fs = require(process.cwd())
const os = require('os')
const path = require('path')
const assert = require('assert')
const semver = require('semver')
const stat = require('../stat.js')

const NODE_VERSION_WITH_BIGINT = '10.5.0'

/* global beforeEach, afterEach, describe, it */

describe('util/stat', () => {
let TEST_DIR

beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'util-stat')
fs.emptyDir(TEST_DIR, done)
})

afterEach(done => fs.remove(TEST_DIR, done))

describe('should use stats with bigint type for node versions >= 10.5.0 and number type for older versions', done => {
it('stat.checkPaths()', () => {
const nodeVersion = process.versions.node
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
stat.checkPaths(src, dest, 'copy', (err, stats) => {
assert.ifError(err)
const { srcStat } = stats
if (semver.gte(nodeVersion, NODE_VERSION_WITH_BIGINT)) {
assert.strictEqual(typeof srcStat.ino, 'bigint')
} else {
assert.strictEqual(typeof srcStat.ino, 'number')
}
})
})

it('stat.checkPathsSync()', () => {
const nodeVersion = process.versions.node
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
const { srcStat } = stat.checkPathsSync(src, dest, 'copy')
if (semver.gte(nodeVersion, NODE_VERSION_WITH_BIGINT)) {
assert.strictEqual(typeof srcStat.ino, 'bigint')
} else {
assert.strictEqual(typeof srcStat.ino, 'number')
}
})
})

describe('should stop at src or root path and not throw max call stack size error', done => {
it('stat.checkParentPaths()', () => {
const src = path.join(TEST_DIR, 'src')
let dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
dest = path.basename(dest)
const srcStat = fs.statSync(src)
stat.checkParentPaths(src, srcStat, dest, 'copy', err => {
assert.ifError(err)
})
})

it('stat.checkParentPathsSync()', () => {
const src = path.join(TEST_DIR, 'src')
let dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
dest = path.basename(dest)
const srcStat = fs.statSync(src)
stat.checkParentPathsSync(src, srcStat, dest, 'copy')
})
})
})
13 changes: 6 additions & 7 deletions lib/util/stat.js
Expand Up @@ -104,11 +104,9 @@ function checkPathsSync (src, dest, funcName) {
// 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) {
const destParent = path.dirname(dest)
if (destParent &&
(destParent === path.dirname(src) ||
destParent === path.parse(destParent).root)
) return cb()
const srcParent = path.resolve(path.dirname(src))
const destParent = path.resolve(path.dirname(dest))
if (destParent === srcParent || destParent === path.parse(destParent).root) return cb()
if (nodeSupportsBigInt()) {
fs.stat(destParent, { bigint: true }, (err, destStat) => {
if (err) {
Expand All @@ -135,8 +133,9 @@ function checkParentPaths (src, srcStat, dest, funcName, cb) {
}

function checkParentPathsSync (src, srcStat, dest, funcName) {
const destParent = path.dirname(dest)
if (destParent && (destParent === path.dirname(src) || destParent === path.parse(destParent).root)) return
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 {
if (nodeSupportsBigInt()) {
Expand Down