Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glob.js 18KB

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