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
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.
13 // If inGlobStar and PREFIX is symlink and points to dir
15 // else readdir(PREFIX) as 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)
29 // else // not globstar
30 // for ENTRY in ENTRIES (not dotfiles, unless pattern[n] is dot)
31 // Test ENTRY against pattern[n]
33 // If passes, PROCESS(pattern[0..n] + item + pattern[n+1 .. $])
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.
43 var fs = require('fs')
44 var minimatch = require('minimatch')
45 var Minimatch = minimatch.Minimatch
46 var inherits = require('inherits')
47 var EE = require('events').EventEmitter
48 var path = require('path')
49 var assert = require('assert')
50 var isAbsolute = require('path-is-absolute')
51 var globSync = require('./sync.js')
52 var common = require('./common.js')
53 var alphasort = common.alphasort
54 var alphasorti = common.alphasorti
55 var setopts = common.setopts
56 var ownProp = common.ownProp
57 var inflight = require('inflight')
58 var util = require('util')
59 var childrenIgnored = common.childrenIgnored
60 var isIgnored = common.isIgnored
62 var once = require('once')
64 function glob (pattern, options, cb) {
65 if (typeof options === 'function') cb = options, options = {}
66 if (!options) options = {}
70 throw new TypeError('callback provided to sync glob')
71 return globSync(pattern, options)
74 return new Glob(pattern, options, cb)
78 var GlobSync = glob.GlobSync = globSync.GlobSync
83 function extend (origin, add) {
84 if (add === null || typeof add !== 'object') {
88 var keys = Object.keys(add)
91 origin[keys[i]] = add[keys[i]]
96 glob.hasMagic = function (pattern, options_) {
97 var options = extend({}, options_)
98 options.noprocess = true
100 var g = new Glob(pattern, options)
101 var set = g.minimatch.set
105 for (var j = 0; j < set[0].length; j++) {
106 if (typeof set[0][j] !== 'string')
115 function Glob (pattern, options, cb) {
116 if (typeof options === 'function') {
121 if (options && options.sync) {
123 throw new TypeError('callback provided to sync glob')
124 return new GlobSync(pattern, options)
127 if (!(this instanceof Glob))
128 return new Glob(pattern, options, cb)
130 setopts(this, pattern, options)
131 this._didRealPath = false
133 // process each pattern in the minimatch set
134 var n = this.minimatch.set.length
136 // The matches are stored as {<filename>: true,...} so that
137 // duplicates are automagically pruned.
138 // Later, we do an Object.keys() on these.
139 // Keep them as a list so we can fill in when nonull is set.
140 this.matches = new Array(n)
142 if (typeof cb === 'function') {
145 this.on('end', function (matches) {
151 var n = this.minimatch.set.length
153 this.matches = new Array(n)
156 this._processQueue = []
165 for (var i = 0; i < n; i ++) {
166 this._process(this.minimatch.set[i], i, false, done)
171 if (self._processing <= 0)
176 Glob.prototype._finish = function () {
177 assert(this instanceof Glob)
181 if (this.realpath && !this._didRealpath)
182 return this._realpath()
185 this.emit('end', this.found)
188 Glob.prototype._realpath = function () {
189 if (this._didRealpath)
192 this._didRealpath = true
194 var n = this.matches.length
196 return this._finish()
199 for (var i = 0; i < this.matches.length; i++)
200 this._realpathSet(i, next)
208 Glob.prototype._realpathSet = function (index, cb) {
209 var matchset = this.matches[index]
213 var found = Object.keys(matchset)
220 var set = this.matches[index] = Object.create(null)
221 found.forEach(function (p, i) {
222 // If there's a problem with the stat, then it means that
223 // one or more of the links in the realpath couldn't be
224 // resolved. just return the abs value in that case.
226 fs.realpath(p, self.realpathCache, function (er, real) {
229 else if (er.syscall === 'stat')
232 self.emit('error', er) // srsly wtf right here
235 self.matches[index] = set
242 Glob.prototype._mark = function (p) {
243 return common.mark(this, p)
246 Glob.prototype._makeAbs = function (f) {
247 return common.makeAbs(this, f)
250 Glob.prototype.abort = function () {
255 Glob.prototype.pause = function () {
262 Glob.prototype.resume = function () {
266 if (this._emitQueue.length) {
267 var eq = this._emitQueue.slice(0)
268 this._emitQueue.length = 0
269 for (var i = 0; i < eq.length; i ++) {
271 this._emitMatch(e[0], e[1])
274 if (this._processQueue.length) {
275 var pq = this._processQueue.slice(0)
276 this._processQueue.length = 0
277 for (var i = 0; i < pq.length; i ++) {
280 this._process(p[0], p[1], p[2], p[3])
286 Glob.prototype._process = function (pattern, index, inGlobStar, cb) {
287 assert(this instanceof Glob)
288 assert(typeof cb === 'function')
295 this._processQueue.push([pattern, index, inGlobStar, cb])
299 //console.error('PROCESS %d', this._processing, pattern)
301 // Get the first [n] parts of pattern that are all strings.
303 while (typeof pattern[n] === 'string') {
306 // now n is the index of the first one that is *not* a string.
308 // see if there's anything else
311 // if not, then this is rather simple
313 this._processSimple(pattern.join('/'), index, cb)
317 // pattern *starts* with some non-trivial item.
318 // going to readdir(cwd), but not include the prefix in matches.
323 // pattern has some string bits in the front.
324 // whatever it starts with, whether that's 'absolute' like /foo/bar,
325 // or 'relative' like '../baz'
326 prefix = pattern.slice(0, n).join('/')
330 var remain = pattern.slice(n)
332 // get the list of entries.
336 else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
337 if (!prefix || !isAbsolute(prefix))
338 prefix = '/' + prefix
343 var abs = this._makeAbs(read)
345 //if ignored, skip _processing
346 if (childrenIgnored(this, read))
349 var isGlobStar = remain[0] === minimatch.GLOBSTAR
351 this._processGlobStar(prefix, read, abs, remain, index, inGlobStar, cb)
353 this._processReaddir(prefix, read, abs, remain, index, inGlobStar, cb)
356 Glob.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar, cb) {
358 this._readdir(abs, inGlobStar, function (er, entries) {
359 return self._processReaddir2(prefix, read, abs, remain, index, inGlobStar, entries, cb)
363 Glob.prototype._processReaddir2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
365 // if the abs isn't a dir, then nothing can match!
369 // It will only match dot entries if it starts with a dot, or if
370 // dot is set. Stuff like @(.foo|.bar) isn't allowed.
372 var negate = !!this.minimatch.negate
373 var rawGlob = pn._glob
374 var dotOk = this.dot || rawGlob.charAt(0) === '.'
376 var matchedEntries = []
377 for (var i = 0; i < entries.length; i++) {
379 if (e.charAt(0) !== '.' || dotOk) {
381 if (negate && !prefix) {
387 matchedEntries.push(e)
391 //console.error('prd2', prefix, entries, remain[0]._glob, matchedEntries)
393 var len = matchedEntries.length
394 // If there are no matched entries, then nothing matches.
398 // if this is the last remaining pattern bit, then no need for
399 // an additional stat *unless* the user has specified mark or
400 // stat explicitly. We know they exist, since readdir returned
403 if (remain.length === 1 && !this.mark && !this.stat) {
404 if (!this.matches[index])
405 this.matches[index] = Object.create(null)
407 for (var i = 0; i < len; i ++) {
408 var e = matchedEntries[i]
416 if (e.charAt(0) === '/' && !this.nomount) {
417 e = path.join(this.root, e)
419 this._emitMatch(index, e)
421 // This was the last one, and no stats were needed
425 // now test all matched entries as stand-ins for that part
428 for (var i = 0; i < len; i ++) {
429 var e = matchedEntries[i]
437 this._process([e].concat(remain), index, inGlobStar, cb)
442 Glob.prototype._emitMatch = function (index, e) {
446 if (this.matches[index][e])
449 if (isIgnored(this, e))
453 this._emitQueue.push([index, e])
457 var abs = this._makeAbs(e)
460 var c = this.cache[abs]
461 if (c === 'DIR' || Array.isArray(c))
468 this.matches[index][e] = true
470 var st = this.statCache[abs]
472 this.emit('stat', e, st)
474 this.emit('match', e)
477 Glob.prototype._readdirInGlobStar = function (abs, cb) {
481 // follow all symlinked directories forever
482 // just proceed as if this is a non-globstar situation
484 return this._readdir(abs, false, cb)
486 var lstatkey = 'lstat\0' + abs
488 var lstatcb = inflight(lstatkey, lstatcb_)
491 fs.lstat(abs, lstatcb)
493 function lstatcb_ (er, lstat) {
497 var isSym = lstat.isSymbolicLink()
498 self.symlinks[abs] = isSym
500 // If it's not a symlink or a dir, then it's definitely a regular file.
501 // don't bother doing a readdir in that case.
502 if (!isSym && !lstat.isDirectory()) {
503 self.cache[abs] = 'FILE'
506 self._readdir(abs, false, cb)
510 Glob.prototype._readdir = function (abs, inGlobStar, cb) {
514 cb = inflight('readdir\0'+abs+'\0'+inGlobStar, cb)
518 //console.error('RD %j %j', +inGlobStar, abs)
519 if (inGlobStar && !ownProp(this.symlinks, abs))
520 return this._readdirInGlobStar(abs, cb)
522 if (ownProp(this.cache, abs)) {
523 var c = this.cache[abs]
524 if (!c || c === 'FILE')
527 if (Array.isArray(c))
532 fs.readdir(abs, readdirCb(this, abs, cb))
535 function readdirCb (self, abs, cb) {
536 return function (er, entries) {
538 self._readdirError(abs, er, cb)
540 self._readdirEntries(abs, entries, cb)
544 Glob.prototype._readdirEntries = function (abs, entries, cb) {
548 // if we haven't asked to stat everything, then just
549 // assume that everything in there exists, so we can avoid
550 // having to stat it a second time.
551 if (!this.mark && !this.stat) {
552 for (var i = 0; i < entries.length; i ++) {
562 this.cache[abs] = entries
563 return cb(null, entries)
566 Glob.prototype._readdirError = function (f, er, cb) {
570 // handle errors, and cache the information
572 case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
573 case 'ENOTDIR': // totally normal. means it *does* exist.
574 this.cache[this._makeAbs(f)] = 'FILE'
577 case 'ENOENT': // not terribly unusual
581 this.cache[this._makeAbs(f)] = false
584 default: // some unusual error. Treat as failure.
585 this.cache[this._makeAbs(f)] = false
587 this.emit('error', er)
588 // If the error is handled, then we abort
589 // if not, we threw out of here
593 console.error('glob error', er)
600 Glob.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar, cb) {
602 this._readdir(abs, inGlobStar, function (er, entries) {
603 self._processGlobStar2(prefix, read, abs, remain, index, inGlobStar, entries, cb)
608 Glob.prototype._processGlobStar2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
609 //console.error('pgs2', prefix, remain[0], entries)
611 // no entries means not a dir, so it can never have matches
612 // foo.txt/** doesn't match foo.txt
616 // test without the globstar, and with every child both below
617 // and replacing the globstar.
618 var remainWithoutGlobStar = remain.slice(1)
619 var gspref = prefix ? [ prefix ] : []
620 var noGlobStar = gspref.concat(remainWithoutGlobStar)
622 // the noGlobStar pattern exits the inGlobStar state
623 this._process(noGlobStar, index, false, cb)
625 var isSym = this.symlinks[abs]
626 var len = entries.length
628 // If it's a symlink, and we're in a globstar, then stop
629 if (isSym && inGlobStar)
632 for (var i = 0; i < len; i++) {
634 if (e.charAt(0) === '.' && !this.dot)
637 // these two cases enter the inGlobStar state
638 var instead = gspref.concat(entries[i], remainWithoutGlobStar)
639 this._process(instead, index, true, cb)
641 var below = gspref.concat(entries[i], remain)
642 this._process(below, index, true, cb)
648 Glob.prototype._processSimple = function (prefix, index, cb) {
649 // XXX review this. Shouldn't it be doing the mounting etc
650 // before doing stat? kinda weird?
652 this._stat(prefix, function (er, exists) {
653 self._processSimple2(prefix, index, er, exists, cb)
656 Glob.prototype._processSimple2 = function (prefix, index, er, exists, cb) {
658 //console.error('ps2', prefix, exists)
660 if (!this.matches[index])
661 this.matches[index] = Object.create(null)
663 // If it doesn't exist, then just mark the lack of results
667 if (prefix && isAbsolute(prefix) && !this.nomount) {
668 var trail = /[\/\\]$/.test(prefix)
669 if (prefix.charAt(0) === '/') {
670 prefix = path.join(this.root, prefix)
672 prefix = path.resolve(this.root, prefix)
678 if (process.platform === 'win32')
679 prefix = prefix.replace(/\\/g, '/')
681 // Mark this as a match
682 this._emitMatch(index, prefix)
686 // Returns either 'DIR', 'FILE', or false
687 Glob.prototype._stat = function (f, cb) {
688 var abs = this._makeAbs(f)
689 var needDir = f.slice(-1) === '/'
691 if (f.length > this.maxLength)
694 if (!this.stat && ownProp(this.cache, abs)) {
695 var c = this.cache[abs]
697 if (Array.isArray(c))
700 // It exists, but maybe not how we need it
701 if (!needDir || c === 'DIR')
704 if (needDir && c === 'FILE')
707 // otherwise we have to stat, because maybe c=true
708 // if we know it exists, but not what it is.
712 var stat = this.statCache[abs]
713 if (stat !== undefined) {
715 return cb(null, stat)
717 var type = stat.isDirectory() ? 'DIR' : 'FILE'
718 if (needDir && type === 'FILE')
721 return cb(null, type, stat)
726 var statcb = inflight('stat\0' + abs, lstatcb_)
728 fs.lstat(abs, statcb)
730 function lstatcb_ (er, lstat) {
731 if (lstat && lstat.isSymbolicLink()) {
732 // If it's a symlink, then treat it as the target, unless
733 // the target does not exist, then treat it as a file.
734 return fs.stat(abs, function (er, stat) {
736 self._stat2(f, abs, null, lstat, cb)
738 self._stat2(f, abs, er, stat, cb)
741 self._stat2(f, abs, er, lstat, cb)
746 Glob.prototype._stat2 = function (f, abs, er, stat, cb) {
748 this.statCache[abs] = false
752 var needDir = f.slice(-1) === '/'
753 this.statCache[abs] = stat
755 if (abs.slice(-1) === '/' && !stat.isDirectory())
756 return cb(null, false, stat)
758 var c = stat.isDirectory() ? 'DIR' : 'FILE'
759 this.cache[abs] = this.cache[abs] || c
761 if (needDir && c !== 'DIR')
764 return cb(null, c, stat)