4
1

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 rp = require('fs.realpath')
  42. var minimatch = require('minimatch')
  43. var Minimatch = minimatch.Minimatch
  44. var inherits = require('inherits')
  45. var EE = require('events').EventEmitter
  46. var path = require('path')
  47. var assert = require('assert')
  48. var isAbsolute = require('path-is-absolute')
  49. var globSync = require('./sync.js')
  50. var common = require('./common.js')
  51. var setopts = common.setopts
  52. var ownProp = common.ownProp
  53. var inflight = require('inflight')
  54. var util = require('util')
  55. var childrenIgnored = common.childrenIgnored
  56. var isIgnored = common.isIgnored
  57. var once = require('once')
  58. function glob (pattern, options, cb) {
  59. if (typeof options === 'function') cb = options, options = {}
  60. if (!options) options = {}
  61. if (options.sync) {
  62. if (cb)
  63. throw new TypeError('callback provided to sync glob')
  64. return globSync(pattern, options)
  65. }
  66. return new Glob(pattern, options, cb)
  67. }
  68. glob.sync = globSync
  69. var GlobSync = glob.GlobSync = globSync.GlobSync
  70. // old api surface
  71. glob.glob = glob
  72. function extend (origin, add) {
  73. if (add === null || typeof add !== 'object') {
  74. return origin
  75. }
  76. var keys = Object.keys(add)
  77. var i = keys.length
  78. while (i--) {
  79. origin[keys[i]] = add[keys[i]]
  80. }
  81. return origin
  82. }
  83. glob.hasMagic = function (pattern, options_) {
  84. var options = extend({}, options_)
  85. options.noprocess = true
  86. var g = new Glob(pattern, options)
  87. var set = g.minimatch.set
  88. if (!pattern)
  89. return false
  90. if (set.length > 1)
  91. return true
  92. for (var j = 0; j < set[0].length; j++) {
  93. if (typeof set[0][j] !== 'string')
  94. return true
  95. }
  96. return false
  97. }
  98. glob.Glob = Glob
  99. inherits(Glob, EE)
  100. function Glob (pattern, options, cb) {
  101. if (typeof options === 'function') {
  102. cb = options
  103. options = null
  104. }
  105. if (options && options.sync) {
  106. if (cb)
  107. throw new TypeError('callback provided to sync glob')
  108. return new GlobSync(pattern, options)
  109. }
  110. if (!(this instanceof Glob))
  111. return new Glob(pattern, options, cb)
  112. setopts(this, pattern, options)
  113. this._didRealPath = false
  114. // process each pattern in the minimatch set
  115. var n = this.minimatch.set.length
  116. // The matches are stored as {<filename>: true,...} so that
  117. // duplicates are automagically pruned.
  118. // Later, we do an Object.keys() on these.
  119. // Keep them as a list so we can fill in when nonull is set.
  120. this.matches = new Array(n)
  121. if (typeof cb === 'function') {
  122. cb = once(cb)
  123. this.on('error', cb)
  124. this.on('end', function (matches) {
  125. cb(null, matches)
  126. })
  127. }
  128. var self = this
  129. this._processing = 0
  130. this._emitQueue = []
  131. this._processQueue = []
  132. this.paused = false
  133. if (this.noprocess)
  134. return this
  135. if (n === 0)
  136. return done()
  137. var sync = true
  138. for (var i = 0; i < n; i ++) {
  139. this._process(this.minimatch.set[i], i, false, done)
  140. }
  141. sync = false
  142. function done () {
  143. --self._processing
  144. if (self._processing <= 0) {
  145. if (sync) {
  146. process.nextTick(function () {
  147. self._finish()
  148. })
  149. } else {
  150. self._finish()
  151. }
  152. }
  153. }
  154. }
  155. Glob.prototype._finish = function () {
  156. assert(this instanceof Glob)
  157. if (this.aborted)
  158. return
  159. if (this.realpath && !this._didRealpath)
  160. return this._realpath()
  161. common.finish(this)
  162. this.emit('end', this.found)
  163. }
  164. Glob.prototype._realpath = function () {
  165. if (this._didRealpath)
  166. return
  167. this._didRealpath = true
  168. var n = this.matches.length
  169. if (n === 0)
  170. return this._finish()
  171. var self = this
  172. for (var i = 0; i < this.matches.length; i++)
  173. this._realpathSet(i, next)
  174. function next () {
  175. if (--n === 0)
  176. self._finish()
  177. }
  178. }
  179. Glob.prototype._realpathSet = function (index, cb) {
  180. var matchset = this.matches[index]
  181. if (!matchset)
  182. return cb()
  183. var found = Object.keys(matchset)
  184. var self = this
  185. var n = found.length
  186. if (n === 0)
  187. return cb()
  188. var set = this.matches[index] = Object.create(null)
  189. found.forEach(function (p, i) {
  190. // If there's a problem with the stat, then it means that
  191. // one or more of the links in the realpath couldn't be
  192. // resolved. just return the abs value in that case.
  193. p = self._makeAbs(p)
  194. rp.realpath(p, self.realpathCache, function (er, real) {
  195. if (!er)
  196. set[real] = true
  197. else if (er.syscall === 'stat')
  198. set[p] = true
  199. else
  200. self.emit('error', er) // srsly wtf right here
  201. if (--n === 0) {
  202. self.matches[index] = set
  203. cb()
  204. }
  205. })
  206. })
  207. }
  208. Glob.prototype._mark = function (p) {
  209. return common.mark(this, p)
  210. }
  211. Glob.prototype._makeAbs = function (f) {
  212. return common.makeAbs(this, f)
  213. }
  214. Glob.prototype.abort = function () {
  215. this.aborted = true
  216. this.emit('abort')
  217. }
  218. Glob.prototype.pause = function () {
  219. if (!this.paused) {
  220. this.paused = true
  221. this.emit('pause')
  222. }
  223. }
  224. Glob.prototype.resume = function () {
  225. if (this.paused) {
  226. this.emit('resume')
  227. this.paused = false
  228. if (this._emitQueue.length) {
  229. var eq = this._emitQueue.slice(0)
  230. this._emitQueue.length = 0
  231. for (var i = 0; i < eq.length; i ++) {
  232. var e = eq[i]
  233. this._emitMatch(e[0], e[1])
  234. }
  235. }
  236. if (this._processQueue.length) {
  237. var pq = this._processQueue.slice(0)
  238. this._processQueue.length = 0
  239. for (var i = 0; i < pq.length; i ++) {
  240. var p = pq[i]
  241. this._processing--
  242. this._process(p[0], p[1], p[2], p[3])
  243. }
  244. }
  245. }
  246. }
  247. Glob.prototype._process = function (pattern, index, inGlobStar, cb) {
  248. assert(this instanceof Glob)
  249. assert(typeof cb === 'function')
  250. if (this.aborted)
  251. return
  252. this._processing++
  253. if (this.paused) {
  254. this._processQueue.push([pattern, index, inGlobStar, cb])
  255. return
  256. }
  257. //console.error('PROCESS %d', this._processing, pattern)
  258. // Get the first [n] parts of pattern that are all strings.
  259. var n = 0
  260. while (typeof pattern[n] === 'string') {
  261. n ++
  262. }
  263. // now n is the index of the first one that is *not* a string.
  264. // see if there's anything else
  265. var prefix
  266. switch (n) {
  267. // if not, then this is rather simple
  268. case pattern.length:
  269. this._processSimple(pattern.join('/'), index, cb)
  270. return
  271. case 0:
  272. // pattern *starts* with some non-trivial item.
  273. // going to readdir(cwd), but not include the prefix in matches.
  274. prefix = null
  275. break
  276. default:
  277. // pattern has some string bits in the front.
  278. // whatever it starts with, whether that's 'absolute' like /foo/bar,
  279. // or 'relative' like '../baz'
  280. prefix = pattern.slice(0, n).join('/')
  281. break
  282. }
  283. var remain = pattern.slice(n)
  284. // get the list of entries.
  285. var read
  286. if (prefix === null)
  287. read = '.'
  288. else if (isAbsolute(prefix) ||
  289. isAbsolute(pattern.map(function (p) {
  290. return typeof p === 'string' ? p : '[*]'
  291. }).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. self.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. self.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. self.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 self.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. }