sync.js 11 KB

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