~ubuntu-branches/ubuntu/utopic/node-tar/utopic

« back to all changes in this revision

Viewing changes to lib/entry.js

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2012-03-17 23:37:48 UTC
  • Revision ID: package-import@ubuntu.com-20120317233748-7c12so2ivmg2ufm8
Tags: upstream-0.1.13
Import upstream version 0.1.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// A passthrough read/write stream that sets its properties
 
2
// based on a header, extendedHeader, and globalHeader
 
3
//
 
4
// Can be either a file system object of some sort, or
 
5
// a pax/ustar metadata entry.
 
6
 
 
7
module.exports = Entry
 
8
 
 
9
var TarHeader = require("./header.js")
 
10
  , tar = require("../tar")
 
11
  , assert = require("assert").ok
 
12
  , Stream = require("stream").Stream
 
13
  , inherits = require("inherits")
 
14
  , fstream = require("fstream").Abstract
 
15
 
 
16
function Entry (header, extended, global) {
 
17
  Stream.call(this)
 
18
  this.readable = true
 
19
  this.writable = true
 
20
 
 
21
  this._needDrain = false
 
22
  this._paused = false
 
23
  this._reading = false
 
24
  this._ending = false
 
25
  this._ended = false
 
26
  this._remaining = 0
 
27
  this._queue = []
 
28
  this._index = 0
 
29
  this._queueLen = 0
 
30
 
 
31
  this._read = this._read.bind(this)
 
32
 
 
33
  this.props = {}
 
34
  this._header = header
 
35
  this._extended = extended || {}
 
36
 
 
37
  // globals can change throughout the course of
 
38
  // a file parse operation.  Freeze it at its current state.
 
39
  this._global = {}
 
40
  var me = this
 
41
  Object.keys(global || {}).forEach(function (g) {
 
42
    me._global[g] = global[g]
 
43
  })
 
44
 
 
45
  this._setProps()
 
46
}
 
47
 
 
48
inherits(Entry, Stream,
 
49
{ write: function (c) {
 
50
    if (this._ending) this.error("write() after end()", null, true)
 
51
    if (this._remaining === 0) {
 
52
      this.error("invalid bytes past eof")
 
53
    }
 
54
 
 
55
    // often we'll get a bunch of \0 at the end of the last write,
 
56
    // since chunks will always be 512 bytes when reading a tarball.
 
57
    if (c.length > this._remaining) {
 
58
      c = c.slice(0, this._remaining)
 
59
    }
 
60
    this._remaining -= c.length
 
61
 
 
62
    // put it on the stack.
 
63
    var ql = this._queueLen
 
64
    this._queue.push(c)
 
65
    this._queueLen ++
 
66
 
 
67
    this._read()
 
68
 
 
69
    // either paused, or buffered
 
70
    if (this._paused || ql > 0) {
 
71
      this._needDrain = true
 
72
      return false
 
73
    }
 
74
 
 
75
    return true
 
76
  }
 
77
 
 
78
, end: function (c) {
 
79
    if (c) this.write(c)
 
80
    this._ending = true
 
81
    this._read()
 
82
  }
 
83
 
 
84
, pause: function () {
 
85
    this._paused = true
 
86
    this.emit("pause")
 
87
  }
 
88
 
 
89
, resume: function () {
 
90
    // console.error("    Tar Entry resume", this.path)
 
91
    this.emit("resume")
 
92
    this._paused = false
 
93
    this._read()
 
94
    return this._queueLen - this._index > 1
 
95
  }
 
96
 
 
97
  // This is bound to the instance
 
98
, _read: function () {
 
99
    // console.error("    Tar Entry _read", this.path)
 
100
 
 
101
    if (this._paused || this._reading || this._ended) return
 
102
 
 
103
    // set this flag so that event handlers don't inadvertently
 
104
    // get multiple _read() calls running.
 
105
    this._reading = true
 
106
 
 
107
    // have any data to emit?
 
108
    if (this._index < this._queueLen) {
 
109
      var chunk = this._queue[this._index ++]
 
110
      this.emit("data", chunk)
 
111
    }
 
112
 
 
113
    // check if we're drained
 
114
    if (this._index >= this._queueLen) {
 
115
      this._queue.length = this._queueLen = this._index = 0
 
116
      if (this._needDrain) {
 
117
        this._needDrain = false
 
118
        this.emit("drain")
 
119
      }
 
120
      if (this._ending) {
 
121
        this._ended = true
 
122
        this.emit("end")
 
123
      }
 
124
    }
 
125
 
 
126
    // if the queue gets too big, then pluck off whatever we can.
 
127
    // this should be fairly rare.
 
128
    var mql = this._maxQueueLen
 
129
    if (this._queueLen > mql && this._index > 0) {
 
130
      mql = Math.min(this._index, mql)
 
131
      this._index -= mql
 
132
      this._queueLen -= mql
 
133
      this._queue = this._queue.slice(mql)
 
134
    }
 
135
 
 
136
    this._reading = false
 
137
  }
 
138
 
 
139
, _setProps: function () {
 
140
    // props = extended->global->header->{}
 
141
    var header = this._header
 
142
      , extended = this._extended
 
143
      , global = this._global
 
144
      , props = this.props
 
145
 
 
146
    // first get the values from the normal header.
 
147
    var fields = tar.fields
 
148
    for (var f = 0; fields[f] !== null; f ++) {
 
149
      var field = fields[f]
 
150
        , val = header[field]
 
151
      if (typeof val !== "undefined") props[field] = val
 
152
    }
 
153
 
 
154
    // next, the global header for this file.
 
155
    // numeric values, etc, will have already been parsed.
 
156
    ;[global, extended].forEach(function (p) {
 
157
      Object.keys(p).forEach(function (f) {
 
158
        if (typeof p[f] !== "undefined") props[f] = p[f]
 
159
      })
 
160
    })
 
161
 
 
162
    // no nulls allowed in path or linkpath
 
163
    ;["path", "linkpath"].forEach(function (p) {
 
164
      if (props.hasOwnProperty(p)) {
 
165
        props[p] = props[p].split("\0")[0]
 
166
      }
 
167
    })
 
168
 
 
169
 
 
170
    // set date fields to be a proper date
 
171
    ;["mtime", "ctime", "atime"].forEach(function (p) {
 
172
      if (props.hasOwnProperty(p)) {
 
173
        props[p] = new Date(props[p] * 1000)
 
174
      }
 
175
    })
 
176
 
 
177
    // set the type so that we know what kind of file to create
 
178
    var type
 
179
    switch (tar.types[props.type]) {
 
180
      case "OldFile":
 
181
      case "ContiguousFile":
 
182
        type = "File"
 
183
        break
 
184
 
 
185
      case "GNUDumpDir":
 
186
        type = "Directory"
 
187
        break
 
188
 
 
189
      case undefined:
 
190
        type = "Unknown"
 
191
        break
 
192
 
 
193
      case "Link":
 
194
      case "SymbolicLink":
 
195
      case "CharacterDevice":
 
196
      case "BlockDevice":
 
197
      case "Directory":
 
198
      case "FIFO":
 
199
      default:
 
200
        type = tar.types[props.type]
 
201
    }
 
202
 
 
203
    this.type = type
 
204
    this.path = props.path
 
205
    this.size = props.size
 
206
 
 
207
    // size is special, since it signals when the file needs to end.
 
208
    this._remaining = props.size
 
209
  }
 
210
, warn: fstream.warn
 
211
, error: fstream.error
 
212
})