1 | 'use strict'
|
---|
2 | const MiniPass = require('minipass')
|
---|
3 | const normPath = require('./normalize-windows-path.js')
|
---|
4 |
|
---|
5 | const SLURP = Symbol('slurp')
|
---|
6 | module.exports = class ReadEntry extends MiniPass {
|
---|
7 | constructor (header, ex, gex) {
|
---|
8 | super()
|
---|
9 | // read entries always start life paused. this is to avoid the
|
---|
10 | // situation where Minipass's auto-ending empty streams results
|
---|
11 | // in an entry ending before we're ready for it.
|
---|
12 | this.pause()
|
---|
13 | this.extended = ex
|
---|
14 | this.globalExtended = gex
|
---|
15 | this.header = header
|
---|
16 | this.startBlockSize = 512 * Math.ceil(header.size / 512)
|
---|
17 | this.blockRemain = this.startBlockSize
|
---|
18 | this.remain = header.size
|
---|
19 | this.type = header.type
|
---|
20 | this.meta = false
|
---|
21 | this.ignore = false
|
---|
22 | switch (this.type) {
|
---|
23 | case 'File':
|
---|
24 | case 'OldFile':
|
---|
25 | case 'Link':
|
---|
26 | case 'SymbolicLink':
|
---|
27 | case 'CharacterDevice':
|
---|
28 | case 'BlockDevice':
|
---|
29 | case 'Directory':
|
---|
30 | case 'FIFO':
|
---|
31 | case 'ContiguousFile':
|
---|
32 | case 'GNUDumpDir':
|
---|
33 | break
|
---|
34 |
|
---|
35 | case 'NextFileHasLongLinkpath':
|
---|
36 | case 'NextFileHasLongPath':
|
---|
37 | case 'OldGnuLongPath':
|
---|
38 | case 'GlobalExtendedHeader':
|
---|
39 | case 'ExtendedHeader':
|
---|
40 | case 'OldExtendedHeader':
|
---|
41 | this.meta = true
|
---|
42 | break
|
---|
43 |
|
---|
44 | // NOTE: gnutar and bsdtar treat unrecognized types as 'File'
|
---|
45 | // it may be worth doing the same, but with a warning.
|
---|
46 | default:
|
---|
47 | this.ignore = true
|
---|
48 | }
|
---|
49 |
|
---|
50 | this.path = normPath(header.path)
|
---|
51 | this.mode = header.mode
|
---|
52 | if (this.mode)
|
---|
53 | this.mode = this.mode & 0o7777
|
---|
54 | this.uid = header.uid
|
---|
55 | this.gid = header.gid
|
---|
56 | this.uname = header.uname
|
---|
57 | this.gname = header.gname
|
---|
58 | this.size = header.size
|
---|
59 | this.mtime = header.mtime
|
---|
60 | this.atime = header.atime
|
---|
61 | this.ctime = header.ctime
|
---|
62 | this.linkpath = normPath(header.linkpath)
|
---|
63 | this.uname = header.uname
|
---|
64 | this.gname = header.gname
|
---|
65 |
|
---|
66 | if (ex)
|
---|
67 | this[SLURP](ex)
|
---|
68 | if (gex)
|
---|
69 | this[SLURP](gex, true)
|
---|
70 | }
|
---|
71 |
|
---|
72 | write (data) {
|
---|
73 | const writeLen = data.length
|
---|
74 | if (writeLen > this.blockRemain)
|
---|
75 | throw new Error('writing more to entry than is appropriate')
|
---|
76 |
|
---|
77 | const r = this.remain
|
---|
78 | const br = this.blockRemain
|
---|
79 | this.remain = Math.max(0, r - writeLen)
|
---|
80 | this.blockRemain = Math.max(0, br - writeLen)
|
---|
81 | if (this.ignore)
|
---|
82 | return true
|
---|
83 |
|
---|
84 | if (r >= writeLen)
|
---|
85 | return super.write(data)
|
---|
86 |
|
---|
87 | // r < writeLen
|
---|
88 | return super.write(data.slice(0, r))
|
---|
89 | }
|
---|
90 |
|
---|
91 | [SLURP] (ex, global) {
|
---|
92 | for (const k in ex) {
|
---|
93 | // we slurp in everything except for the path attribute in
|
---|
94 | // a global extended header, because that's weird.
|
---|
95 | if (ex[k] !== null && ex[k] !== undefined &&
|
---|
96 | !(global && k === 'path'))
|
---|
97 | this[k] = k === 'path' || k === 'linkpath' ? normPath(ex[k]) : ex[k]
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|