glob.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // Approach:
  2. //
  3. // 1. Get the minimatch set
  4. // 2. For each pattern in the set, PROCESS(pattern, false)
  5. // 3. Store matches per-set, then uniq them
  6. //
  7. // PROCESS(pattern, inGlobStar)
  8. // Get the first [n] items from pattern that are all strings
  9. // Join these together. This is PREFIX.
  10. // If there is no more remaining, then stat(PREFIX) and
  11. // add to matches if it succeeds. END.
  12. //
  13. // If inGlobStar and PREFIX is symlink and points to dir
  14. // set ENTRIES = []
  15. // else readdir(PREFIX) as ENTRIES
  16. // If fail, END
  17. //
  18. // with ENTRIES
  19. // If pattern[n] is GLOBSTAR
  20. // // handle the case where the globstar match is empty
  21. // // by pruning it out, and testing the resulting pattern
  22. // PROCESS(pattern[0..n] + pattern[n+1 .. $], false)
  23. // // handle other cases.
  24. // for ENTRY in ENTRIES (not dotfiles)
  25. // // attach globstar + tail onto the entry
  26. // // Mark that this entry is a globstar match
  27. // PROCESS(pattern[0..n] + ENTRY + pattern[n .. $], true)
  28. //
  29. // else // not globstar
  30. // for ENTRY in ENTRIES (not dotfiles, unless pattern[n] is dot)
  31. // Test ENTRY against pattern[n]
  32. // If fails, continue
  33. // If passes, PROCESS(pattern[0..n] + item + pattern[n+1 .. $])
  34. //
  35. // Caveat:
  36. // Cache all stats and readdirs results to minimize syscall. Since all
  37. // we ever care about is existence and directory-ness, we can just keep
  38. // `true` for files, and [children,...] for directories, or `false` for
  39. // things that don't exist.
  40. module.exports = glob
  41. var fs = require('fs')
  42. var rp = require('fs.realpath')
  43. var minimatch = require('minimatch')
  44. var Minimatch = minimatch.Minimatch
  45. var inherits = require('inherits')
  46. var EE = require('events').EventEmitter
  47. var path = require('path')
  48. var assert = require('assert')
  49. var isAbsolute = require('path-is-absolute')
  50. var globSync = require('./sync.js')
  51. var common = require('./common.js')
  52. var alphasort = common.alphasort
  53. var alphasorti = common.alphasorti
  54. var setopts = common.setopts
  55. var ownProp = common.ownProp
  56. var inflight = require('inflight')
  57. var util = require('util')
  58. var childrenIgnored = common.childrenIgnored
  59. var isIgnored = common.isIgnored
  60. var once = require('once')
  61. function glob (pattern, options, cb) {
  62. if (typeof options === 'function') cb = options, options = {}
  63. if (!options) options = {}
  64. if (options.sync) {
  65. if (cb)
  66. throw new TypeError('callback provided to sync glob')
  67. return globSync(pattern, options)
  68. }
  69. return new Glob(pattern, options, cb)
  70. }
  71. glob.sync = globSync
  72. var GlobSync = glob.GlobSync = globSync.GlobSync
  73. // old api surface
  74. glob.glob = glob
  75. function extend (origin, add) {
  76. if (add === null || typeof add !== 'object') {
  77. return origin
  78. }
  79. var keys = Object.keys(add)
  80. var i = keys.length
  81. while (i--) {
  82. origin[keys[i]] = add[keys[i]]
  83. }
  84. return origin
  85. }
  86. glob.hasMagic = function (pattern, options_) {
  87. var options = extend({}, options_)
  88. options.noprocess = true
  89. var g = new Glob(pattern, options)
  90. var set = g.minimatch.set
  91. if (!pattern)
  92. return false
  93. if (set.length > 1)
  94. return true
  95. for (var j = 0; j < set[0].length; j++) {
  96. if (typeof set[0][j] !== 'string')
  97. return true
  98. }
  99. return false
  100. }
  101. glob.Glob = Glob
  102. inherits(Glob, EE)
  103. function Glob (pattern, options, cb) {
  104. if (typeof options === 'function') {
  105. cb = options
  106. options = null
  107. }
  108. if (options && options.sync) {
  109. if (cb)
  110. throw new TypeError('callback provided to sync glob')
  111. return new GlobSync(pattern, options)
  112. }
  113. if (!(this instanceof Glob))
  114. return new Glob(pattern, options, cb)
  115. setopts(this, pattern, options)
  116. this._didRealPath = false
  117. // process each pattern in the minimatch set
  118. var n = this.minimatch.set.length
  119. // The matches are stored as {<filename>: true,...} so that
  120. // duplicates are automagically pruned.
  121. // Later, we do an Object.keys() on these.
  122. // Keep them as a list so we can fill in when nonull is set.
  123. this.matches = new Array(n)
  124. if (typeof cb === 'function') {
  125. cb = once(cb)
  126. this.on('error', cb)
  127. this.on('end', function (matches) {
  128. cb(null, matches)
  129. })
  130. }
  131. var self = this
  132. this._processing = 0
  133. this._emitQueue = []
  134. this._processQueue = []
  135. this.paused = false
  136. if (this.noprocess)
  137. return this
  138. if (n === 0)
  139. return done()
  140. var sync = true
  141. for (var i = 0; i < n; i ++) {
  142. this._process(this.minimatch.set[i], i, false, done)
  143. }
  144. sync = false
  145. function done () {
  146. --self._processing
  147. if (self._processing <= 0) {
  148. if (sync) {
  149. process.nextTick(function () {
  150. self._finish()
  151. })
  152. } else {
  153. self._finish()
  154. }
  155. }
  156. }
  157. }
  158. Glob.prototype._finish = function () {
  159. assert(this instanceof Glob)
  160. if (this.aborted)
  161. return
  162. if (this.realpath && !this._didRealpath)
  163. return this._realpath()
  164. common.finish(this)
  165. this.emit('end', this.found)
  166. }
  167. Glob.prototype._realpath = function () {
  168. if (this._didRealpath)
  169. return
  170. this._didRealpath = true
  171. var n = this.matches.length
  172. if (n === 0)
  173. return this._finish()
  174. var self = this
  175. for (var i = 0; i < this.matches.length; i++)
  176. this._realpathSet(i, next)
  177. function next () {
  178. if (--n === 0)
  179. self._finish()
  180. }
  181. }
  182. Glob.prototype._realpathSet = function (index, cb) {
  183. var matchset = this.matches[index]
  184. if (!matchset)
  185. return cb()
  186. var found = Object.keys(matchset)
  187. var self = this
  188. var n = found.length
  189. if (n === 0)
  190. return cb()
  191. var set = this.matches[index] = Object.create(null)
  192. found.forEach(function (p, i) {
  193. // If there's a problem with the stat, then it means that
  194. // one or more of the links in the realpath couldn't be
  195. // resolved. just return the abs value in that case.
  196. p = self._makeAbs(p)
  197. rp.realpath(p, self.realpathCache, function (er, real) {
  198. if (!er)
  199. set[real] = true
  200. else if (er.syscall === 'stat')
  201. set[p] = true
  202. else
  203. self.emit('error', er) // srsly wtf right here
  204. if (--n === 0) {
  205. self.matches[index] = set
  206. cb()
  207. }
  208. })
  209. })
  210. }
  211. Glob.prototype._mark = function (p) {
  212. return common.mark(this, p)
  213. }
  214. Glob.prototype._makeAbs = function (f) {
  215. return common.makeAbs(this, f)
  216. }
  217. Glob.prototype.abort = function () {
  218. this.aborted = true
  219. this.emit('abort')
  220. }
  221. Glob.prototype.pause = function () {
  222. if (!this.paused) {
  223. this.paused = true
  224. this.emit('pause')
  225. }
  226. }
  227. Glob.prototype.resume = function () {
  228. if (this.paused) {
  229. this.emit('resume')
  230. this.paused = false
  231. if (this._emitQueue.length) {
  232. var eq = this._emitQueue.slice(0)
  233. this._emitQueue.length = 0
  234. for (var i = 0; i < eq.length; i ++) {
  235. var e = eq[i]
  236. this._emitMatch(e[0], e[1])
  237. }
  238. }
  239. if (this._processQueue.length) {
  240. var pq = this._processQueue.slice(0)
  241. this._processQueue.length = 0
  242. for (var i = 0; i < pq.length; i ++) {
  243. var p = pq[i]
  244. this._processing--
  245. this._process(p[0], p[1], p[2], p[3])
  246. }
  247. }
  248. }
  249. }
  250. Glob.prototype._process = function (pattern, index, inGlobStar, cb) {
  251. assert(this instanceof Glob)
  252. assert(typeof cb === 'function')
  253. if (this.aborted)
  254. return
  255. this._processing++
  256. if (this.paused) {
  257. this._processQueue.push([pattern, index, inGlobStar, cb])
  258. return
  259. }
  260. //console.error('PROCESS %d', this._processing, pattern)
  261. // Get the first [n] parts of pattern that are all strings.
  262. var n = 0
  263. while (typeof pattern[n] === 'string') {
  264. n ++
  265. }
  266. // now n is the index of the first one that is *not* a string.
  267. // see if there's anything else
  268. var prefix
  269. switch (n) {
  270. // if not, then this is rather simple
  271. case pattern.length:
  272. this._processSimple(pattern.join('/'), index, cb)
  273. return
  274. case 0:
  275. // pattern *starts* with some non-trivial item.
  276. // going to readdir(cwd), but not include the prefix in matches.
  277. prefix = null
  278. break
  279. default:
  280. // pattern has some string bits in the front.
  281. // whatever it starts with, whether that's 'absolute' like /foo/bar,
  282. // or 'relative' like '../baz'
  283. prefix = pattern.slice(0, n).join('/')
  284. break
  285. }
  286. var remain = pattern.slice(n)
  287. // get the list of entries.
  288. var read
  289. if (prefix === null)
  290. read = '.'
  291. else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
  292. if (!prefix || !isAbsolute(prefix))
  293. prefix = '/' + prefix
  294. read = prefix
  295. } else
  296. read = prefix
  297. var abs = this._makeAbs(read)
  298. //if ignored, skip _processing
  299. if (childrenIgnored(this, read))
  300. return cb()
  301. var isGlobStar = remain[0] === minimatch.GLOBSTAR
  302. if (isGlobStar)
  303. this._processGlobStar(prefix, read, abs, remain, index, inGlobStar, cb)
  304. else
  305. this._processReaddir(prefix, read, abs, remain, index, inGlobStar, cb)
  306. }
  307. Glob.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar, cb) {
  308. var self = this
  309. this._readdir(abs, inGlobStar, function (er, entries) {
  310. return self._processReaddir2(prefix, read, abs, remain, index, inGlobStar, entries, cb)
  311. })
  312. }
  313. Glob.prototype._processReaddir2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
  314. // if the abs isn't a dir, then nothing can match!
  315. if (!entries)
  316. return cb()
  317. // It will only match dot entries if it starts with a dot, or if
  318. // dot is set. Stuff like @(.foo|.bar) isn't allowed.
  319. var pn = remain[0]
  320. var negate = !!this.minimatch.negate
  321. var rawGlob = pn._glob
  322. var dotOk = this.dot || rawGlob.charAt(0) === '.'
  323. var matchedEntries = []
  324. for (var i = 0; i < entries.length; i++) {
  325. var e = entries[i]
  326. if (e.charAt(0) !== '.' || dotOk) {
  327. var m
  328. if (negate && !prefix) {
  329. m = !e.match(pn)
  330. } else {
  331. m = e.match(pn)
  332. }
  333. if (m)
  334. matchedEntries.push(e)
  335. }
  336. }
  337. //console.error('prd2', prefix, entries, remain[0]._glob, matchedEntries)
  338. var len = matchedEntries.length
  339. // If there are no matched entries, then nothing matches.
  340. if (len === 0)
  341. return cb()
  342. // if this is the last remaining pattern bit, then no need for
  343. // an additional stat *unless* the user has specified mark or
  344. // stat explicitly. We know they exist, since readdir returned
  345. // them.
  346. if (remain.length === 1 && !this.mark && !this.stat) {
  347. if (!this.matches[index])
  348. this.matches[index] = Object.create(null)
  349. for (var i = 0; i < len; i ++) {
  350. var e = matchedEntries[i]
  351. if (prefix) {
  352. if (prefix !== '/')
  353. e = prefix + '/' + e
  354. else
  355. e = prefix + e
  356. }
  357. if (e.charAt(0) === '/' && !this.nomount) {
  358. e = path.join(this.root, e)
  359. }
  360. this._emitMatch(index, e)
  361. }
  362. // This was the last one, and no stats were needed
  363. return cb()
  364. }
  365. // now test all matched entries as stand-ins for that part
  366. // of the pattern.
  367. remain.shift()
  368. for (var i = 0; i < len; i ++) {
  369. var e = matchedEntries[i]
  370. var newPattern
  371. if (prefix) {
  372. if (prefix !== '/')
  373. e = prefix + '/' + e
  374. else
  375. e = prefix + e
  376. }
  377. this._process([e].concat(remain), index, inGlobStar, cb)
  378. }
  379. cb()
  380. }
  381. Glob.prototype._emitMatch = function (index, e) {
  382. if (this.aborted)
  383. return
  384. if (isIgnored(this, e))
  385. return
  386. if (this.paused) {
  387. this._emitQueue.push([index, e])
  388. return
  389. }
  390. var abs = isAbsolute(e) ? e : this._makeAbs(e)
  391. if (this.mark)
  392. e = this._mark(e)
  393. if (this.absolute)
  394. e = abs
  395. if (this.matches[index][e])
  396. return
  397. if (this.nodir) {
  398. var c = this.cache[abs]
  399. if (c === 'DIR' || Array.isArray(c))
  400. return
  401. }
  402. this.matches[index][e] = true
  403. var st = this.statCache[abs]
  404. if (st)
  405. this.emit('stat', e, st)
  406. this.emit('match', e)
  407. }
  408. Glob.prototype._readdirInGlobStar = function (abs, cb) {
  409. if (this.aborted)
  410. return
  411. // follow all symlinked directories forever
  412. // just proceed as if this is a non-globstar situation
  413. if (this.follow)
  414. return this._readdir(abs, false, cb)
  415. var lstatkey = 'lstat\0' + abs
  416. var self = this
  417. var lstatcb = inflight(lstatkey, lstatcb_)
  418. if (lstatcb)
  419. fs.lstat(abs, lstatcb)
  420. function lstatcb_ (er, lstat) {
  421. if (er && er.code === 'ENOENT')
  422. return cb()
  423. var isSym = lstat && lstat.isSymbolicLink()
  424. self.symlinks[abs] = isSym
  425. // If it's not a symlink or a dir, then it's definitely a regular file.
  426. // don't bother doing a readdir in that case.
  427. if (!isSym && lstat && !lstat.isDirectory()) {
  428. self.cache[abs] = 'FILE'
  429. cb()
  430. } else
  431. self._readdir(abs, false, cb)
  432. }
  433. }
  434. Glob.prototype._readdir = function (abs, inGlobStar, cb) {
  435. if (this.aborted)
  436. return
  437. cb = inflight('readdir\0'+abs+'\0'+inGlobStar, cb)
  438. if (!cb)
  439. return
  440. //console.error('RD %j %j', +inGlobStar, abs)
  441. if (inGlobStar && !ownProp(this.symlinks, abs))
  442. return this._readdirInGlobStar(abs, cb)
  443. if (ownProp(this.cache, abs)) {
  444. var c = this.cache[abs]
  445. if (!c || c === 'FILE')
  446. return cb()
  447. if (Array.isArray(c))
  448. return cb(null, c)
  449. }
  450. var self = this
  451. fs.readdir(abs, readdirCb(this, abs, cb))
  452. }
  453. function readdirCb (self, abs, cb) {
  454. return function (er, entries) {
  455. if (er)
  456. self._readdirError(abs, er, cb)
  457. else
  458. self._readdirEntries(abs, entries, cb)
  459. }
  460. }
  461. Glob.prototype._readdirEntries = function (abs, entries, cb) {
  462. if (this.aborted)
  463. return
  464. // if we haven't asked to stat everything, then just
  465. // assume that everything in there exists, so we can avoid
  466. // having to stat it a second time.
  467. if (!this.mark && !this.stat) {
  468. for (var i = 0; i < entries.length; i ++) {
  469. var e = entries[i]
  470. if (abs === '/')
  471. e = abs + e
  472. else
  473. e = abs + '/' + e
  474. this.cache[e] = true
  475. }
  476. }
  477. this.cache[abs] = entries
  478. return cb(null, entries)
  479. }
  480. Glob.prototype._readdirError = function (f, er, cb) {
  481. if (this.aborted)
  482. return
  483. // handle errors, and cache the information
  484. switch (er.code) {
  485. case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
  486. case 'ENOTDIR': // totally normal. means it *does* exist.
  487. var abs = this._makeAbs(f)
  488. this.cache[abs] = 'FILE'
  489. if (abs === this.cwdAbs) {
  490. var error = new Error(er.code + ' invalid cwd ' + this.cwd)
  491. error.path = this.cwd
  492. error.code = er.code
  493. this.emit('error', error)
  494. this.abort()
  495. }
  496. break
  497. case 'ENOENT': // not terribly unusual
  498. case 'ELOOP':
  499. case 'ENAMETOOLONG':
  500. case 'UNKNOWN':
  501. this.cache[this._makeAbs(f)] = false
  502. break
  503. default: // some unusual error. Treat as failure.
  504. this.cache[this._makeAbs(f)] = false
  505. if (this.strict) {
  506. this.emit('error', er)
  507. // If the error is handled, then we abort
  508. // if not, we threw out of here
  509. this.abort()
  510. }
  511. if (!this.silent)
  512. console.error('glob error', er)
  513. break
  514. }
  515. return cb()
  516. }
  517. Glob.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar, cb) {
  518. var self = this
  519. this._readdir(abs, inGlobStar, function (er, entries) {
  520. self._processGlobStar2(prefix, read, abs, remain, index, inGlobStar, entries, cb)
  521. })
  522. }
  523. Glob.prototype._processGlobStar2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
  524. //console.error('pgs2', prefix, remain[0], entries)
  525. // no entries means not a dir, so it can never have matches
  526. // foo.txt/** doesn't match foo.txt
  527. if (!entries)
  528. return cb()
  529. // test without the globstar, and with every child both below
  530. // and replacing the globstar.
  531. var remainWithoutGlobStar = remain.slice(1)
  532. var gspref = prefix ? [ prefix ] : []
  533. var noGlobStar = gspref.concat(remainWithoutGlobStar)
  534. // the noGlobStar pattern exits the inGlobStar state
  535. this._process(noGlobStar, index, false, cb)
  536. var isSym = this.symlinks[abs]
  537. var len = entries.length
  538. // If it's a symlink, and we're in a globstar, then stop
  539. if (isSym && inGlobStar)
  540. return cb()
  541. for (var i = 0; i < len; i++) {
  542. var e = entries[i]
  543. if (e.charAt(0) === '.' && !this.dot)
  544. continue
  545. // these two cases enter the inGlobStar state
  546. var instead = gspref.concat(entries[i], remainWithoutGlobStar)
  547. this._process(instead, index, true, cb)
  548. var below = gspref.concat(entries[i], remain)
  549. this._process(below, index, true, cb)
  550. }
  551. cb()
  552. }
  553. Glob.prototype._processSimple = function (prefix, index, cb) {
  554. // XXX review this. Shouldn't it be doing the mounting etc
  555. // before doing stat? kinda weird?
  556. var self = this
  557. this._stat(prefix, function (er, exists) {
  558. self._processSimple2(prefix, index, er, exists, cb)
  559. })
  560. }
  561. Glob.prototype._processSimple2 = function (prefix, index, er, exists, cb) {
  562. //console.error('ps2', prefix, exists)
  563. if (!this.matches[index])
  564. this.matches[index] = Object.create(null)
  565. // If it doesn't exist, then just mark the lack of results
  566. if (!exists)
  567. return cb()
  568. if (prefix && isAbsolute(prefix) && !this.nomount) {
  569. var trail = /[\/\\]$/.test(prefix)
  570. if (prefix.charAt(0) === '/') {
  571. prefix = path.join(this.root, prefix)
  572. } else {
  573. prefix = path.resolve(this.root, prefix)
  574. if (trail)
  575. prefix += '/'
  576. }
  577. }
  578. if (process.platform === 'win32')
  579. prefix = prefix.replace(/\\/g, '/')
  580. // Mark this as a match
  581. this._emitMatch(index, prefix)
  582. cb()
  583. }
  584. // Returns either 'DIR', 'FILE', or false
  585. Glob.prototype._stat = function (f, cb) {
  586. var abs = this._makeAbs(f)
  587. var needDir = f.slice(-1) === '/'
  588. if (f.length > this.maxLength)
  589. return cb()
  590. if (!this.stat && ownProp(this.cache, abs)) {
  591. var c = this.cache[abs]
  592. if (Array.isArray(c))
  593. c = 'DIR'
  594. // It exists, but maybe not how we need it
  595. if (!needDir || c === 'DIR')
  596. return cb(null, c)
  597. if (needDir && c === 'FILE')
  598. return cb()
  599. // otherwise we have to stat, because maybe c=true
  600. // if we know it exists, but not what it is.
  601. }
  602. var exists
  603. var stat = this.statCache[abs]
  604. if (stat !== undefined) {
  605. if (stat === false)
  606. return cb(null, stat)
  607. else {
  608. var type = stat.isDirectory() ? 'DIR' : 'FILE'
  609. if (needDir && type === 'FILE')
  610. return cb()
  611. else
  612. return cb(null, type, stat)
  613. }
  614. }
  615. var self = this
  616. var statcb = inflight('stat\0' + abs, lstatcb_)
  617. if (statcb)
  618. fs.lstat(abs, statcb)
  619. function lstatcb_ (er, lstat) {
  620. if (lstat && lstat.isSymbolicLink()) {
  621. // If it's a symlink, then treat it as the target, unless
  622. // the target does not exist, then treat it as a file.
  623. return fs.stat(abs, function (er, stat) {
  624. if (er)
  625. self._stat2(f, abs, null, lstat, cb)
  626. else
  627. self._stat2(f, abs, er, stat, cb)
  628. })
  629. } else {
  630. self._stat2(f, abs, er, lstat, cb)
  631. }
  632. }
  633. }
  634. Glob.prototype._stat2 = function (f, abs, er, stat, cb) {
  635. if (er && (er.code === 'ENOENT' || er.code === 'ENOTDIR')) {
  636. this.statCache[abs] = false
  637. return cb()
  638. }
  639. var needDir = f.slice(-1) === '/'
  640. this.statCache[abs] = stat
  641. if (abs.slice(-1) === '/' && stat && !stat.isDirectory())
  642. return cb(null, false, stat)
  643. var c = true
  644. if (stat)
  645. c = stat.isDirectory() ? 'DIR' : 'FILE'
  646. this.cache[abs] = this.cache[abs] || c
  647. if (needDir && c === 'FILE')
  648. return cb()
  649. return cb(null, c, stat)
  650. }