Skip to content

Commit

Permalink
move try/catch out of keysForTree(), fix broccolijs#9
Browse files Browse the repository at this point in the history
  • Loading branch information
luto committed May 13, 2014
1 parent a8086b0 commit 2d55093
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,14 @@ function keysForTree (fullPath, options) {
var _stack = options._stack
var _followSymlink = options._followSymlink
var relativePath = options.relativePath || '.'
var stats
var stats = getStatsForPath(fullPath, _followSymlink)
var statKeys

try {
if (_followSymlink) {
stats = fs.statSync(fullPath)
} else {
stats = fs.lstatSync(fullPath)
}
} catch (err) {
console.warn('Warning: failed to stat ' + fullPath)
// fullPath has probably ceased to exist. Leave `stats` undefined and
// proceed hashing.
}
var childKeys = []
if (stats) {
statKeys = ['stats', stats.mode]
} else {
console.warn('Warning: failed to stat ' + fullPath)
statKeys = ['stat failed']
}
if (stats && stats.isDirectory()) {
Expand All @@ -51,15 +41,7 @@ function keysForTree (fullPath, options) {
console.warn('Symlink directory loop detected at ' + fullPath + ' (note: loop detection may have false positives on Windows)')
} else {
if (_stack != null) _stack = _stack.concat([fileIdentity])
var entries
try {
entries = fs.readdirSync(fullPath).sort()
} catch (err) {
console.warn('Warning: Failed to read directory ' + fullPath)
console.warn(err.stack)
childKeys = ['readdir failed']
// That's all there is to say about this directory.
}
var entries = getFilesInPath(fullpath)
if (entries != null) {
for (var i = 0; i < entries.length; i++) {

Expand All @@ -69,6 +51,11 @@ function keysForTree (fullPath, options) {
})
childKeys = childKeys.concat(keys)
}
} else {
console.warn('Warning: Failed to read directory ' + fullPath)
console.warn(err.stack)
childKeys = ['readdir failed']
// That's all there is to say about this directory.
}
}
} else if (stats && stats.isSymbolicLink()) {
Expand All @@ -93,6 +80,32 @@ function keysForTree (fullPath, options) {
.concat(childKeys)
}

// these functions are part of keysForTree()
// they have been separated to fix performance issues in issue #9
function getStatsForPath (fullPath, followSymlink) {
var stats = null
try {
if (followSymlink) {
stats = fs.statSync(fullPath)
} else {
stats = fs.lstatSync(fullPath)
}
} catch (err) {
// fullPath has probably ceased to exist. Leave `stats` undefined and
// proceed hashing.
}
return stats
}

function getFilesInPath (fullPath) {
var entries = null
try {
entries = fs.readdirSync(fullPath).sort()
} catch (err) {
}
return entries
}


exports.hashStats = hashStats
function hashStats (stats, path) {
Expand Down

0 comments on commit 2d55093

Please sign in to comment.