Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
fix(tree): Graceful catch stat on EACCES and ENOENT errors
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 28, 2015
1 parent 5b22b19 commit 253b36a
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ var fs = Promise.promisifyAll(require('fs'))

const DATE_FORMAT = 'llll'

const DEFAULT_STAT = {
directory: false,
type: 'unknown',
size: 0,
mtime: 0,
lastModified: '',
atime: 0,
lastAccessed: '',
ctime: 0,
lastChanged: '',
depth: 0
}

var gracefulCatch = function(root, path) {
return function(err) {

if(err.code != 'EACCES' && err.code != 'ENOENT') {
return Promise.reject(err)
}

if(err.code == 'EACCES')
console.error('No file access (check rights on %s)', path)
else
console.error('No file entry (file %s does not exist ?!)', path)

return Promise.resolve(root)
}
}

/**
* Build Breadcrumb from paths
* @param string root
Expand Down Expand Up @@ -47,7 +76,7 @@ var buildBreadcrumb = function(root, path) {
*/
var directorySize = function(file, root, options) {

var filePath = p.resolve(root.path, file)
var path = p.resolve(root.path, file)
var depth = file.split(p.sep).length

if(root.depth < depth) {
Expand All @@ -59,10 +88,10 @@ var directorySize = function(file, root, options) {
return Promise.resolve(root)
}

return fs.statAsync(filePath)
return fs.statAsync(path)
.then(function(stat) {
if(stat.isDirectory()) {
let items = fs.readdirAsync(filePath)
let items = fs.readdirAsync(path)

for(let i in options.filters) {
items = items.filter(options.filters[i])
Expand All @@ -74,19 +103,7 @@ var directorySize = function(file, root, options) {
return Promise.resolve(root)
}
})
.catch(function(err) {

if(err.code != 'EACCES' && err.code != 'ENOENT') {
return Promise.reject(err)
}

if(err.code == 'EACCES')
console.error('No file access (check rights)', filePath)
else
console.error('No file entry (file does not exist ?!)', filePath)

return Promise.resolve(root)
})
.catch(gracefulCatch(root, path))
}

var recursiveReaddir = function(root, options) {
Expand All @@ -101,7 +118,7 @@ var recursiveReaddir = function(root, options) {

return fs.statAsync(path)
.then(function(stat) {
var depth = root.replace(options.root, '').split(p.sep).length
let depth = root.replace(options.root, '').split(p.sep).length

if(depth > options.maxDepth)
return path
Expand All @@ -112,6 +129,7 @@ var recursiveReaddir = function(root, options) {

return path
})
.catch(gracefulCatch(root, path))
}).then(function(paths) {
paths.push(root)
return [].concat.apply([], paths)
Expand Down Expand Up @@ -261,19 +279,15 @@ var tree = function(path, options) {

if(stat.isDirectory()) {
info.directory = true
info.type = 'directory'
info.depth = 0
info.type = 'directory'
}

// debug('info', info)

return info
})
.catch(function(err) {
if(err.code === 'ENOENT') {
console.error('Stat ENOENT', err)
}
})
.catch(gracefulCatch(DEFAULT_STAT, path))
}, concurrency)
.filter(options.searchFilter ? options.searchFilter : function(e) { return e })
.map(options.sort === 'size' ? directorySize : function(e) { return e })
Expand Down

0 comments on commit 253b36a

Please sign in to comment.