glob.js 19 KB

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