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.

sync.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. module.exports = globSync
  2. globSync.GlobSync = GlobSync
  3. var fs = require('fs')
  4. var minimatch = require('minimatch')
  5. var Minimatch = minimatch.Minimatch
  6. var Glob = require('./glob.js').Glob
  7. var util = require('util')
  8. var path = require('path')
  9. var assert = require('assert')
  10. var isAbsolute = require('path-is-absolute')
  11. var common = require('./common.js')
  12. var alphasort = common.alphasort
  13. var alphasorti = common.alphasorti
  14. var setopts = common.setopts
  15. var ownProp = common.ownProp
  16. var childrenIgnored = common.childrenIgnored
  17. function globSync (pattern, options) {
  18. if (typeof options === 'function' || arguments.length === 3)
  19. throw new TypeError('callback provided to sync glob\n'+
  20. 'See: https://github.com/isaacs/node-glob/issues/167')
  21. return new GlobSync(pattern, options).found
  22. }
  23. function GlobSync (pattern, options) {
  24. if (!pattern)
  25. throw new Error('must provide pattern')
  26. if (typeof options === 'function' || arguments.length === 3)
  27. throw new TypeError('callback provided to sync glob\n'+
  28. 'See: https://github.com/isaacs/node-glob/issues/167')
  29. if (!(this instanceof GlobSync))
  30. return new GlobSync(pattern, options)
  31. setopts(this, pattern, options)
  32. if (this.noprocess)
  33. return this
  34. var n = this.minimatch.set.length
  35. this.matches = new Array(n)
  36. for (var i = 0; i < n; i ++) {
  37. this._process(this.minimatch.set[i], i, false)
  38. }
  39. this._finish()
  40. }
  41. GlobSync.prototype._finish = function () {
  42. assert(this instanceof GlobSync)
  43. if (this.realpath) {
  44. var self = this
  45. this.matches.forEach(function (matchset, index) {
  46. var set = self.matches[index] = Object.create(null)
  47. for (var p in matchset) {
  48. try {
  49. p = self._makeAbs(p)
  50. var real = fs.realpathSync(p, self.realpathCache)
  51. set[real] = true
  52. } catch (er) {
  53. if (er.syscall === 'stat')
  54. set[self._makeAbs(p)] = true
  55. else
  56. throw er
  57. }
  58. }
  59. })
  60. }
  61. common.finish(this)
  62. }
  63. GlobSync.prototype._process = function (pattern, index, inGlobStar) {
  64. assert(this instanceof GlobSync)
  65. // Get the first [n] parts of pattern that are all strings.
  66. var n = 0
  67. while (typeof pattern[n] === 'string') {
  68. n ++
  69. }
  70. // now n is the index of the first one that is *not* a string.
  71. // See if there's anything else
  72. var prefix
  73. switch (n) {
  74. // if not, then this is rather simple
  75. case pattern.length:
  76. this._processSimple(pattern.join('/'), index)
  77. return
  78. case 0:
  79. // pattern *starts* with some non-trivial item.
  80. // going to readdir(cwd), but not include the prefix in matches.
  81. prefix = null
  82. break
  83. default:
  84. // pattern has some string bits in the front.
  85. // whatever it starts with, whether that's 'absolute' like /foo/bar,
  86. // or 'relative' like '../baz'
  87. prefix = pattern.slice(0, n).join('/')
  88. break
  89. }
  90. var remain = pattern.slice(n)
  91. // get the list of entries.
  92. var read
  93. if (prefix === null)
  94. read = '.'
  95. else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
  96. if (!prefix || !isAbsolute(prefix))
  97. prefix = '/' + prefix
  98. read = prefix
  99. } else
  100. read = prefix
  101. var abs = this._makeAbs(read)
  102. //if ignored, skip processing
  103. if (childrenIgnored(this, read))
  104. return
  105. var isGlobStar = remain[0] === minimatch.GLOBSTAR
  106. if (isGlobStar)
  107. this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)
  108. else
  109. this._processReaddir(prefix, read, abs, remain, index, inGlobStar)
  110. }
  111. GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
  112. var entries = this._readdir(abs, inGlobStar)
  113. // if the abs isn't a dir, then nothing can match!
  114. if (!entries)
  115. return
  116. // It will only match dot entries if it starts with a dot, or if
  117. // dot is set. Stuff like @(.foo|.bar) isn't allowed.
  118. var pn = remain[0]
  119. var negate = !!this.minimatch.negate
  120. var rawGlob = pn._glob
  121. var dotOk = this.dot || rawGlob.charAt(0) === '.'
  122. var matchedEntries = []
  123. for (var i = 0; i < entries.length; i++) {
  124. var e = entries[i]
  125. if (e.charAt(0) !== '.' || dotOk) {
  126. var m
  127. if (negate && !prefix) {
  128. m = !e.match(pn)
  129. } else {
  130. m = e.match(pn)
  131. }
  132. if (m)
  133. matchedEntries.push(e)
  134. }
  135. }
  136. var len = matchedEntries.length
  137. // If there are no matched entries, then nothing matches.
  138. if (len === 0)
  139. return
  140. // if this is the last remaining pattern bit, then no need for
  141. // an additional stat *unless* the user has specified mark or
  142. // stat explicitly. We know they exist, since readdir returned
  143. // them.
  144. if (remain.length === 1 && !this.mark && !this.stat) {
  145. if (!this.matches[index])
  146. this.matches[index] = Object.create(null)
  147. for (var i = 0; i < len; i ++) {
  148. var e = matchedEntries[i]
  149. if (prefix) {
  150. if (prefix.slice(-1) !== '/')
  151. e = prefix + '/' + e
  152. else
  153. e = prefix + e
  154. }
  155. if (e.charAt(0) === '/' && !this.nomount) {
  156. e = path.join(this.root, e)
  157. }
  158. this.matches[index][e] = true
  159. }
  160. // This was the last one, and no stats were needed
  161. return
  162. }
  163. // now test all matched entries as stand-ins for that part
  164. // of the pattern.
  165. remain.shift()
  166. for (var i = 0; i < len; i ++) {
  167. var e = matchedEntries[i]
  168. var newPattern
  169. if (prefix)
  170. newPattern = [prefix, e]
  171. else
  172. newPattern = [e]
  173. this._process(newPattern.concat(remain), index, inGlobStar)
  174. }
  175. }
  176. GlobSync.prototype._emitMatch = function (index, e) {
  177. var abs = this._makeAbs(e)
  178. if (this.mark)
  179. e = this._mark(e)
  180. if (this.matches[index][e])
  181. return
  182. if (this.nodir) {
  183. var c = this.cache[this._makeAbs(e)]
  184. if (c === 'DIR' || Array.isArray(c))
  185. return
  186. }
  187. this.matches[index][e] = true
  188. if (this.stat)
  189. this._stat(e)
  190. }
  191. GlobSync.prototype._readdirInGlobStar = function (abs) {
  192. // follow all symlinked directories forever
  193. // just proceed as if this is a non-globstar situation
  194. if (this.follow)
  195. return this._readdir(abs, false)
  196. var entries
  197. var lstat
  198. var stat
  199. try {
  200. lstat = fs.lstatSync(abs)
  201. } catch (er) {
  202. // lstat failed, doesn't exist
  203. return null
  204. }
  205. var isSym = lstat.isSymbolicLink()
  206. this.symlinks[abs] = isSym
  207. // If it's not a symlink or a dir, then it's definitely a regular file.
  208. // don't bother doing a readdir in that case.
  209. if (!isSym && !lstat.isDirectory())
  210. this.cache[abs] = 'FILE'
  211. else
  212. entries = this._readdir(abs, false)
  213. return entries
  214. }
  215. GlobSync.prototype._readdir = function (abs, inGlobStar) {
  216. var entries
  217. if (inGlobStar && !ownProp(this.symlinks, abs))
  218. return this._readdirInGlobStar(abs)
  219. if (ownProp(this.cache, abs)) {
  220. var c = this.cache[abs]
  221. if (!c || c === 'FILE')
  222. return null
  223. if (Array.isArray(c))
  224. return c
  225. }
  226. try {
  227. return this._readdirEntries(abs, fs.readdirSync(abs))
  228. } catch (er) {
  229. this._readdirError(abs, er)
  230. return null
  231. }
  232. }
  233. GlobSync.prototype._readdirEntries = function (abs, entries) {
  234. // if we haven't asked to stat everything, then just
  235. // assume that everything in there exists, so we can avoid
  236. // having to stat it a second time.
  237. if (!this.mark && !this.stat) {
  238. for (var i = 0; i < entries.length; i ++) {
  239. var e = entries[i]
  240. if (abs === '/')
  241. e = abs + e
  242. else
  243. e = abs + '/' + e
  244. this.cache[e] = true
  245. }
  246. }
  247. this.cache[abs] = entries
  248. // mark and cache dir-ness
  249. return entries
  250. }
  251. GlobSync.prototype._readdirError = function (f, er) {
  252. // handle errors, and cache the information
  253. switch (er.code) {
  254. case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
  255. case 'ENOTDIR': // totally normal. means it *does* exist.
  256. this.cache[this._makeAbs(f)] = 'FILE'
  257. break
  258. case 'ENOENT': // not terribly unusual
  259. case 'ELOOP':
  260. case 'ENAMETOOLONG':
  261. case 'UNKNOWN':
  262. this.cache[this._makeAbs(f)] = false
  263. break
  264. default: // some unusual error. Treat as failure.
  265. this.cache[this._makeAbs(f)] = false
  266. if (this.strict)
  267. throw er
  268. if (!this.silent)
  269. console.error('glob error', er)
  270. break
  271. }
  272. }
  273. GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
  274. var entries = this._readdir(abs, inGlobStar)
  275. // no entries means not a dir, so it can never have matches
  276. // foo.txt/** doesn't match foo.txt
  277. if (!entries)
  278. return
  279. // test without the globstar, and with every child both below
  280. // and replacing the globstar.
  281. var remainWithoutGlobStar = remain.slice(1)
  282. var gspref = prefix ? [ prefix ] : []
  283. var noGlobStar = gspref.concat(remainWithoutGlobStar)
  284. // the noGlobStar pattern exits the inGlobStar state
  285. this._process(noGlobStar, index, false)
  286. var len = entries.length
  287. var isSym = this.symlinks[abs]
  288. // If it's a symlink, and we're in a globstar, then stop
  289. if (isSym && inGlobStar)
  290. return
  291. for (var i = 0; i < len; i++) {
  292. var e = entries[i]
  293. if (e.charAt(0) === '.' && !this.dot)
  294. continue
  295. // these two cases enter the inGlobStar state
  296. var instead = gspref.concat(entries[i], remainWithoutGlobStar)
  297. this._process(instead, index, true)
  298. var below = gspref.concat(entries[i], remain)
  299. this._process(below, index, true)
  300. }
  301. }
  302. GlobSync.prototype._processSimple = function (prefix, index) {
  303. // XXX review this. Shouldn't it be doing the mounting etc
  304. // before doing stat? kinda weird?
  305. var exists = this._stat(prefix)
  306. if (!this.matches[index])
  307. this.matches[index] = Object.create(null)
  308. // If it doesn't exist, then just mark the lack of results
  309. if (!exists)
  310. return
  311. if (prefix && isAbsolute(prefix) && !this.nomount) {
  312. var trail = /[\/\\]$/.test(prefix)
  313. if (prefix.charAt(0) === '/') {
  314. prefix = path.join(this.root, prefix)
  315. } else {
  316. prefix = path.resolve(this.root, prefix)
  317. if (trail)
  318. prefix += '/'
  319. }
  320. }
  321. if (process.platform === 'win32')
  322. prefix = prefix.replace(/\\/g, '/')
  323. // Mark this as a match
  324. this.matches[index][prefix] = true
  325. }
  326. // Returns either 'DIR', 'FILE', or false
  327. GlobSync.prototype._stat = function (f) {
  328. var abs = this._makeAbs(f)
  329. var needDir = f.slice(-1) === '/'
  330. if (f.length > this.maxLength)
  331. return false
  332. if (!this.stat && ownProp(this.cache, abs)) {
  333. var c = this.cache[abs]
  334. if (Array.isArray(c))
  335. c = 'DIR'
  336. // It exists, but maybe not how we need it
  337. if (!needDir || c === 'DIR')
  338. return c
  339. if (needDir && c === 'FILE')
  340. return false
  341. // otherwise we have to stat, because maybe c=true
  342. // if we know it exists, but not what it is.
  343. }
  344. var exists
  345. var stat = this.statCache[abs]
  346. if (!stat) {
  347. var lstat
  348. try {
  349. lstat = fs.lstatSync(abs)
  350. } catch (er) {
  351. return false
  352. }
  353. if (lstat.isSymbolicLink()) {
  354. try {
  355. stat = fs.statSync(abs)
  356. } catch (er) {
  357. stat = lstat
  358. }
  359. } else {
  360. stat = lstat
  361. }
  362. }
  363. this.statCache[abs] = stat
  364. var c = stat.isDirectory() ? 'DIR' : 'FILE'
  365. this.cache[abs] = this.cache[abs] || c
  366. if (needDir && c !== 'DIR')
  367. return false
  368. return c
  369. }
  370. GlobSync.prototype._mark = function (p) {
  371. return common.mark(this, p)
  372. }
  373. GlobSync.prototype._makeAbs = function (f) {
  374. return common.makeAbs(this, f)
  375. }