link-reader.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Basically just a wrapper around an fs.readlink
  2. //
  3. // XXX: Enhance this to support the Link type, by keeping
  4. // a lookup table of {<dev+inode>:<path>}, so that hardlinks
  5. // can be preserved in tarballs.
  6. module.exports = LinkReader
  7. var fs = require("graceful-fs")
  8. , fstream = require("../fstream.js")
  9. , inherits = require("inherits")
  10. , mkdir = require("mkdirp")
  11. , Reader = require("./reader.js")
  12. inherits(LinkReader, Reader)
  13. function LinkReader (props) {
  14. var me = this
  15. if (!(me instanceof LinkReader)) throw new Error(
  16. "LinkReader must be called as constructor.")
  17. if (!((props.type === "Link" && props.Link) ||
  18. (props.type === "SymbolicLink" && props.SymbolicLink))) {
  19. throw new Error("Non-link type "+ props.type)
  20. }
  21. Reader.call(me, props)
  22. }
  23. // When piping a LinkReader into a LinkWriter, we have to
  24. // already have the linkpath property set, so that has to
  25. // happen *before* the "ready" event, which means we need to
  26. // override the _stat method.
  27. LinkReader.prototype._stat = function (currentStat) {
  28. var me = this
  29. fs.readlink(me._path, function (er, linkpath) {
  30. if (er) return me.error(er)
  31. me.linkpath = me.props.linkpath = linkpath
  32. me.emit("linkpath", linkpath)
  33. Reader.prototype._stat.call(me, currentStat)
  34. })
  35. }
  36. LinkReader.prototype._read = function () {
  37. var me = this
  38. if (me._paused) return
  39. // basically just a no-op, since we got all the info we need
  40. // from the _stat method
  41. if (!me._ended) {
  42. me.emit("end")
  43. me.emit("close")
  44. me._ended = true
  45. }
  46. }