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 12KB

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