PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/npm/node_modules/fstream/lib/reader.js

https://gitlab.com/sturdynut/node
JavaScript | 255 lines | 176 code | 41 blank | 38 comment | 46 complexity | b176dff03f160ec8c4fa2332c70739b4 MD5 | raw file
  1. module.exports = Reader
  2. var fs = require('graceful-fs')
  3. var Stream = require('stream').Stream
  4. var inherits = require('inherits')
  5. var path = require('path')
  6. var getType = require('./get-type.js')
  7. var hardLinks = Reader.hardLinks = {}
  8. var Abstract = require('./abstract.js')
  9. // Must do this *before* loading the child classes
  10. inherits(Reader, Abstract)
  11. var LinkReader = require('./link-reader.js')
  12. function Reader (props, currentStat) {
  13. var self = this
  14. if (!(self instanceof Reader)) return new Reader(props, currentStat)
  15. if (typeof props === 'string') {
  16. props = { path: props }
  17. }
  18. if (!props.path) {
  19. self.error('Must provide a path', null, true)
  20. }
  21. // polymorphism.
  22. // call fstream.Reader(dir) to get a DirReader object, etc.
  23. // Note that, unlike in the Writer case, ProxyReader is going
  24. // to be the *normal* state of affairs, since we rarely know
  25. // the type of a file prior to reading it.
  26. var type
  27. var ClassType
  28. if (props.type && typeof props.type === 'function') {
  29. type = props.type
  30. ClassType = type
  31. } else {
  32. type = getType(props)
  33. ClassType = Reader
  34. }
  35. if (currentStat && !type) {
  36. type = getType(currentStat)
  37. props[type] = true
  38. props.type = type
  39. }
  40. switch (type) {
  41. case 'Directory':
  42. ClassType = require('./dir-reader.js')
  43. break
  44. case 'Link':
  45. // XXX hard links are just files.
  46. // However, it would be good to keep track of files' dev+inode
  47. // and nlink values, and create a HardLinkReader that emits
  48. // a linkpath value of the original copy, so that the tar
  49. // writer can preserve them.
  50. // ClassType = HardLinkReader
  51. // break
  52. case 'File':
  53. ClassType = require('./file-reader.js')
  54. break
  55. case 'SymbolicLink':
  56. ClassType = LinkReader
  57. break
  58. case 'Socket':
  59. ClassType = require('./socket-reader.js')
  60. break
  61. case null:
  62. ClassType = require('./proxy-reader.js')
  63. break
  64. }
  65. if (!(self instanceof ClassType)) {
  66. return new ClassType(props)
  67. }
  68. Abstract.call(self)
  69. self.readable = true
  70. self.writable = false
  71. self.type = type
  72. self.props = props
  73. self.depth = props.depth = props.depth || 0
  74. self.parent = props.parent || null
  75. self.root = props.root || (props.parent && props.parent.root) || self
  76. self._path = self.path = path.resolve(props.path)
  77. if (process.platform === 'win32') {
  78. self.path = self._path = self.path.replace(/\?/g, '_')
  79. if (self._path.length >= 260) {
  80. // how DOES one create files on the moon?
  81. // if the path has spaces in it, then UNC will fail.
  82. self._swallowErrors = true
  83. // if (self._path.indexOf(" ") === -1) {
  84. self._path = '\\\\?\\' + self.path.replace(/\//g, '\\')
  85. // }
  86. }
  87. }
  88. self.basename = props.basename = path.basename(self.path)
  89. self.dirname = props.dirname = path.dirname(self.path)
  90. // these have served their purpose, and are now just noisy clutter
  91. props.parent = props.root = null
  92. // console.error("\n\n\n%s setting size to", props.path, props.size)
  93. self.size = props.size
  94. self.filter = typeof props.filter === 'function' ? props.filter : null
  95. if (props.sort === 'alpha') props.sort = alphasort
  96. // start the ball rolling.
  97. // this will stat the thing, and then call self._read()
  98. // to start reading whatever it is.
  99. // console.error("calling stat", props.path, currentStat)
  100. self._stat(currentStat)
  101. }
  102. function alphasort (a, b) {
  103. return a === b ? 0
  104. : a.toLowerCase() > b.toLowerCase() ? 1
  105. : a.toLowerCase() < b.toLowerCase() ? -1
  106. : a > b ? 1
  107. : -1
  108. }
  109. Reader.prototype._stat = function (currentStat) {
  110. var self = this
  111. var props = self.props
  112. var stat = props.follow ? 'stat' : 'lstat'
  113. // console.error("Reader._stat", self._path, currentStat)
  114. if (currentStat) process.nextTick(statCb.bind(null, null, currentStat))
  115. else fs[stat](self._path, statCb)
  116. function statCb (er, props_) {
  117. // console.error("Reader._stat, statCb", self._path, props_, props_.nlink)
  118. if (er) return self.error(er)
  119. Object.keys(props_).forEach(function (k) {
  120. props[k] = props_[k]
  121. })
  122. // if it's not the expected size, then abort here.
  123. if (undefined !== self.size && props.size !== self.size) {
  124. return self.error('incorrect size')
  125. }
  126. self.size = props.size
  127. var type = getType(props)
  128. var handleHardlinks = props.hardlinks !== false
  129. // special little thing for handling hardlinks.
  130. if (handleHardlinks && type !== 'Directory' && props.nlink && props.nlink > 1) {
  131. var k = props.dev + ':' + props.ino
  132. // console.error("Reader has nlink", self._path, k)
  133. if (hardLinks[k] === self._path || !hardLinks[k]) {
  134. hardLinks[k] = self._path
  135. } else {
  136. // switch into hardlink mode.
  137. type = self.type = self.props.type = 'Link'
  138. self.Link = self.props.Link = true
  139. self.linkpath = self.props.linkpath = hardLinks[k]
  140. // console.error("Hardlink detected, switching mode", self._path, self.linkpath)
  141. // Setting __proto__ would arguably be the "correct"
  142. // approach here, but that just seems too wrong.
  143. self._stat = self._read = LinkReader.prototype._read
  144. }
  145. }
  146. if (self.type && self.type !== type) {
  147. self.error('Unexpected type: ' + type)
  148. }
  149. // if the filter doesn't pass, then just skip over this one.
  150. // still have to emit end so that dir-walking can move on.
  151. if (self.filter) {
  152. var who = self._proxy || self
  153. // special handling for ProxyReaders
  154. if (!self.filter.call(who, who, props)) {
  155. if (!self._disowned) {
  156. self.abort()
  157. self.emit('end')
  158. self.emit('close')
  159. }
  160. return
  161. }
  162. }
  163. // last chance to abort or disown before the flow starts!
  164. var events = ['_stat', 'stat', 'ready']
  165. var e = 0
  166. ;(function go () {
  167. if (self._aborted) {
  168. self.emit('end')
  169. self.emit('close')
  170. return
  171. }
  172. if (self._paused && self.type !== 'Directory') {
  173. self.once('resume', go)
  174. return
  175. }
  176. var ev = events[e++]
  177. if (!ev) {
  178. return self._read()
  179. }
  180. self.emit(ev, props)
  181. go()
  182. })()
  183. }
  184. }
  185. Reader.prototype.pipe = function (dest) {
  186. var self = this
  187. if (typeof dest.add === 'function') {
  188. // piping to a multi-compatible, and we've got directory entries.
  189. self.on('entry', function (entry) {
  190. var ret = dest.add(entry)
  191. if (ret === false) {
  192. self.pause()
  193. }
  194. })
  195. }
  196. // console.error("R Pipe apply Stream Pipe")
  197. return Stream.prototype.pipe.apply(this, arguments)
  198. }
  199. Reader.prototype.pause = function (who) {
  200. this._paused = true
  201. who = who || this
  202. this.emit('pause', who)
  203. if (this._stream) this._stream.pause(who)
  204. }
  205. Reader.prototype.resume = function (who) {
  206. this._paused = false
  207. who = who || this
  208. this.emit('resume', who)
  209. if (this._stream) this._stream.resume(who)
  210. this._read()
  211. }
  212. Reader.prototype._read = function () {
  213. this.error('Cannot read unknown type: ' + this.type)
  214. }