tar 6.2.1 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/README.md +30 -16
  2. package/dist/commonjs/create.d.ts +8 -0
  3. package/dist/commonjs/create.d.ts.map +1 -0
  4. package/dist/commonjs/create.js +106 -0
  5. package/dist/commonjs/create.js.map +1 -0
  6. package/dist/commonjs/cwd-error.d.ts +8 -0
  7. package/dist/commonjs/cwd-error.d.ts.map +1 -0
  8. package/dist/commonjs/cwd-error.js +18 -0
  9. package/dist/commonjs/cwd-error.js.map +1 -0
  10. package/dist/commonjs/extract.d.ts +8 -0
  11. package/dist/commonjs/extract.d.ts.map +1 -0
  12. package/dist/commonjs/extract.js +141 -0
  13. package/dist/commonjs/extract.js.map +1 -0
  14. package/dist/commonjs/get-write-flag.d.ts +2 -0
  15. package/dist/commonjs/get-write-flag.d.ts.map +1 -0
  16. package/dist/commonjs/get-write-flag.js +29 -0
  17. package/dist/commonjs/get-write-flag.js.map +1 -0
  18. package/dist/commonjs/header.d.ts +54 -0
  19. package/dist/commonjs/header.d.ts.map +1 -0
  20. package/dist/commonjs/header.js +308 -0
  21. package/dist/commonjs/header.js.map +1 -0
  22. package/dist/commonjs/index.d.ts +19 -0
  23. package/dist/commonjs/index.d.ts.map +1 -0
  24. package/dist/commonjs/index.js +54 -0
  25. package/dist/commonjs/index.js.map +1 -0
  26. package/dist/commonjs/large-numbers.d.ts +4 -0
  27. package/dist/commonjs/large-numbers.d.ts.map +1 -0
  28. package/dist/commonjs/large-numbers.js +99 -0
  29. package/dist/commonjs/large-numbers.js.map +1 -0
  30. package/dist/commonjs/list.d.ts +8 -0
  31. package/dist/commonjs/list.d.ts.map +1 -0
  32. package/dist/commonjs/list.js +167 -0
  33. package/dist/commonjs/list.js.map +1 -0
  34. package/dist/commonjs/mkdir.d.ts +27 -0
  35. package/dist/commonjs/mkdir.d.ts.map +1 -0
  36. package/dist/commonjs/mkdir.js +209 -0
  37. package/dist/commonjs/mkdir.js.map +1 -0
  38. package/dist/commonjs/mode-fix.d.ts +2 -0
  39. package/dist/commonjs/mode-fix.d.ts.map +1 -0
  40. package/dist/commonjs/mode-fix.js +29 -0
  41. package/dist/commonjs/mode-fix.js.map +1 -0
  42. package/dist/commonjs/normalize-unicode.d.ts +2 -0
  43. package/dist/commonjs/normalize-unicode.d.ts.map +1 -0
  44. package/dist/commonjs/normalize-unicode.js +17 -0
  45. package/dist/commonjs/normalize-unicode.js.map +1 -0
  46. package/dist/commonjs/normalize-windows-path.d.ts +2 -0
  47. package/dist/commonjs/normalize-windows-path.d.ts.map +1 -0
  48. package/dist/commonjs/normalize-windows-path.js +12 -0
  49. package/dist/commonjs/normalize-windows-path.js.map +1 -0
  50. package/dist/commonjs/options.d.ts +545 -0
  51. package/dist/commonjs/options.d.ts.map +1 -0
  52. package/dist/commonjs/options.js +55 -0
  53. package/dist/commonjs/options.js.map +1 -0
  54. package/dist/commonjs/pack.d.ts +98 -0
  55. package/dist/commonjs/pack.d.ts.map +1 -0
  56. package/dist/commonjs/pack.js +461 -0
  57. package/dist/commonjs/pack.js.map +1 -0
  58. package/dist/commonjs/package.json +3 -0
  59. package/dist/commonjs/parse.d.ts +84 -0
  60. package/dist/commonjs/parse.d.ts.map +1 -0
  61. package/dist/commonjs/parse.js +571 -0
  62. package/dist/commonjs/parse.js.map +1 -0
  63. package/dist/commonjs/path-reservations.d.ts +11 -0
  64. package/dist/commonjs/path-reservations.d.ts.map +1 -0
  65. package/dist/commonjs/path-reservations.js +169 -0
  66. package/dist/commonjs/path-reservations.js.map +1 -0
  67. package/dist/commonjs/pax.d.ts +27 -0
  68. package/dist/commonjs/pax.d.ts.map +1 -0
  69. package/dist/commonjs/pax.js +158 -0
  70. package/dist/commonjs/pax.js.map +1 -0
  71. package/dist/commonjs/read-entry.d.ts +37 -0
  72. package/dist/commonjs/read-entry.d.ts.map +1 -0
  73. package/dist/commonjs/read-entry.js +139 -0
  74. package/dist/commonjs/read-entry.js.map +1 -0
  75. package/dist/commonjs/replace.d.ts +5 -0
  76. package/dist/commonjs/replace.d.ts.map +1 -0
  77. package/dist/commonjs/replace.js +223 -0
  78. package/dist/commonjs/replace.js.map +1 -0
  79. package/dist/commonjs/strip-absolute-path.d.ts +2 -0
  80. package/dist/commonjs/strip-absolute-path.d.ts.map +1 -0
  81. package/dist/commonjs/strip-absolute-path.js +29 -0
  82. package/dist/commonjs/strip-absolute-path.js.map +1 -0
  83. package/dist/commonjs/strip-trailing-slashes.d.ts +2 -0
  84. package/dist/commonjs/strip-trailing-slashes.d.ts.map +1 -0
  85. package/dist/commonjs/strip-trailing-slashes.js +18 -0
  86. package/dist/commonjs/strip-trailing-slashes.js.map +1 -0
  87. package/dist/commonjs/symlink-error.d.ts +9 -0
  88. package/dist/commonjs/symlink-error.d.ts.map +1 -0
  89. package/dist/commonjs/symlink-error.js +19 -0
  90. package/dist/commonjs/symlink-error.js.map +1 -0
  91. package/dist/commonjs/types.d.ts +7 -0
  92. package/dist/commonjs/types.d.ts.map +1 -0
  93. package/dist/commonjs/types.js +50 -0
  94. package/dist/commonjs/types.js.map +1 -0
  95. package/dist/commonjs/unpack.d.ts +98 -0
  96. package/dist/commonjs/unpack.d.ts.map +1 -0
  97. package/dist/commonjs/unpack.js +921 -0
  98. package/dist/commonjs/unpack.js.map +1 -0
  99. package/dist/commonjs/update.d.ts +3 -0
  100. package/dist/commonjs/update.d.ts.map +1 -0
  101. package/dist/commonjs/update.js +47 -0
  102. package/dist/commonjs/update.js.map +1 -0
  103. package/dist/commonjs/warn-method.d.ts +20 -0
  104. package/dist/commonjs/warn-method.d.ts.map +1 -0
  105. package/dist/commonjs/warn-method.js +31 -0
  106. package/dist/commonjs/warn-method.js.map +1 -0
  107. package/dist/commonjs/winchars.d.ts +3 -0
  108. package/dist/commonjs/winchars.d.ts.map +1 -0
  109. package/dist/commonjs/winchars.js +14 -0
  110. package/dist/commonjs/winchars.js.map +1 -0
  111. package/dist/commonjs/write-entry.d.ts +125 -0
  112. package/dist/commonjs/write-entry.d.ts.map +1 -0
  113. package/dist/commonjs/write-entry.js +657 -0
  114. package/dist/commonjs/write-entry.js.map +1 -0
  115. package/dist/esm/create.d.ts +8 -0
  116. package/dist/esm/create.d.ts.map +1 -0
  117. package/dist/esm/create.js +99 -0
  118. package/dist/esm/create.js.map +1 -0
  119. package/dist/esm/cwd-error.d.ts +8 -0
  120. package/dist/esm/cwd-error.d.ts.map +1 -0
  121. package/dist/esm/cwd-error.js +14 -0
  122. package/dist/esm/cwd-error.js.map +1 -0
  123. package/dist/esm/extract.d.ts +8 -0
  124. package/dist/esm/extract.d.ts.map +1 -0
  125. package/dist/esm/extract.js +111 -0
  126. package/dist/esm/extract.js.map +1 -0
  127. package/dist/esm/get-write-flag.d.ts +2 -0
  128. package/dist/esm/get-write-flag.d.ts.map +1 -0
  129. package/dist/esm/get-write-flag.js +23 -0
  130. package/dist/esm/get-write-flag.js.map +1 -0
  131. package/dist/esm/header.d.ts +54 -0
  132. package/dist/esm/header.d.ts.map +1 -0
  133. package/dist/esm/header.js +281 -0
  134. package/dist/esm/header.js.map +1 -0
  135. package/dist/esm/index.d.ts +19 -0
  136. package/dist/esm/index.d.ts.map +1 -0
  137. package/dist/esm/index.js +20 -0
  138. package/dist/esm/index.js.map +1 -0
  139. package/dist/esm/large-numbers.d.ts +4 -0
  140. package/dist/esm/large-numbers.d.ts.map +1 -0
  141. package/dist/esm/large-numbers.js +94 -0
  142. package/dist/esm/large-numbers.js.map +1 -0
  143. package/dist/esm/list.d.ts +8 -0
  144. package/dist/esm/list.d.ts.map +1 -0
  145. package/dist/esm/list.js +137 -0
  146. package/dist/esm/list.js.map +1 -0
  147. package/dist/esm/mkdir.d.ts +27 -0
  148. package/dist/esm/mkdir.d.ts.map +1 -0
  149. package/dist/esm/mkdir.js +201 -0
  150. package/dist/esm/mkdir.js.map +1 -0
  151. package/dist/esm/mode-fix.d.ts +2 -0
  152. package/dist/esm/mode-fix.d.ts.map +1 -0
  153. package/dist/esm/mode-fix.js +25 -0
  154. package/dist/esm/mode-fix.js.map +1 -0
  155. package/dist/esm/normalize-unicode.d.ts +2 -0
  156. package/dist/esm/normalize-unicode.d.ts.map +1 -0
  157. package/dist/esm/normalize-unicode.js +13 -0
  158. package/dist/esm/normalize-unicode.js.map +1 -0
  159. package/dist/esm/normalize-windows-path.d.ts +2 -0
  160. package/dist/esm/normalize-windows-path.d.ts.map +1 -0
  161. package/{lib → dist/esm}/normalize-windows-path.js +5 -4
  162. package/dist/esm/normalize-windows-path.js.map +1 -0
  163. package/dist/esm/options.d.ts +545 -0
  164. package/dist/esm/options.d.ts.map +1 -0
  165. package/dist/esm/options.js +48 -0
  166. package/dist/esm/options.js.map +1 -0
  167. package/dist/esm/pack.d.ts +98 -0
  168. package/dist/esm/pack.d.ts.map +1 -0
  169. package/dist/esm/pack.js +429 -0
  170. package/dist/esm/pack.js.map +1 -0
  171. package/dist/esm/package.json +3 -0
  172. package/dist/esm/parse.d.ts +84 -0
  173. package/dist/esm/parse.d.ts.map +1 -0
  174. package/dist/esm/parse.js +567 -0
  175. package/dist/esm/parse.js.map +1 -0
  176. package/dist/esm/path-reservations.d.ts +11 -0
  177. package/dist/esm/path-reservations.d.ts.map +1 -0
  178. package/dist/esm/path-reservations.js +165 -0
  179. package/dist/esm/path-reservations.js.map +1 -0
  180. package/dist/esm/pax.d.ts +27 -0
  181. package/dist/esm/pax.d.ts.map +1 -0
  182. package/dist/esm/pax.js +154 -0
  183. package/dist/esm/pax.js.map +1 -0
  184. package/dist/esm/read-entry.d.ts +37 -0
  185. package/dist/esm/read-entry.d.ts.map +1 -0
  186. package/dist/esm/read-entry.js +135 -0
  187. package/dist/esm/read-entry.js.map +1 -0
  188. package/dist/esm/replace.d.ts +5 -0
  189. package/dist/esm/replace.d.ts.map +1 -0
  190. package/dist/esm/replace.js +216 -0
  191. package/dist/esm/replace.js.map +1 -0
  192. package/dist/esm/strip-absolute-path.d.ts +2 -0
  193. package/dist/esm/strip-absolute-path.d.ts.map +1 -0
  194. package/dist/esm/strip-absolute-path.js +25 -0
  195. package/dist/esm/strip-absolute-path.js.map +1 -0
  196. package/dist/esm/strip-trailing-slashes.d.ts +2 -0
  197. package/dist/esm/strip-trailing-slashes.d.ts.map +1 -0
  198. package/dist/esm/strip-trailing-slashes.js +14 -0
  199. package/dist/esm/strip-trailing-slashes.js.map +1 -0
  200. package/dist/esm/symlink-error.d.ts +9 -0
  201. package/dist/esm/symlink-error.d.ts.map +1 -0
  202. package/dist/esm/symlink-error.js +15 -0
  203. package/dist/esm/symlink-error.js.map +1 -0
  204. package/dist/esm/types.d.ts +7 -0
  205. package/dist/esm/types.d.ts.map +1 -0
  206. package/dist/esm/types.js +45 -0
  207. package/dist/esm/types.js.map +1 -0
  208. package/dist/esm/unpack.d.ts +98 -0
  209. package/dist/esm/unpack.d.ts.map +1 -0
  210. package/dist/esm/unpack.js +890 -0
  211. package/dist/esm/unpack.js.map +1 -0
  212. package/dist/esm/update.d.ts +3 -0
  213. package/dist/esm/update.d.ts.map +1 -0
  214. package/dist/esm/update.js +43 -0
  215. package/dist/esm/update.js.map +1 -0
  216. package/dist/esm/warn-method.d.ts +20 -0
  217. package/dist/esm/warn-method.d.ts.map +1 -0
  218. package/dist/esm/warn-method.js +27 -0
  219. package/dist/esm/warn-method.js.map +1 -0
  220. package/dist/esm/winchars.d.ts +3 -0
  221. package/dist/esm/winchars.d.ts.map +1 -0
  222. package/dist/esm/winchars.js +9 -0
  223. package/dist/esm/winchars.js.map +1 -0
  224. package/dist/esm/write-entry.d.ts +125 -0
  225. package/dist/esm/write-entry.d.ts.map +1 -0
  226. package/dist/esm/write-entry.js +625 -0
  227. package/dist/esm/write-entry.js.map +1 -0
  228. package/package.json +255 -40
  229. package/index.js +0 -18
  230. package/lib/create.js +0 -111
  231. package/lib/extract.js +0 -113
  232. package/lib/get-write-flag.js +0 -20
  233. package/lib/header.js +0 -304
  234. package/lib/high-level-opt.js +0 -29
  235. package/lib/large-numbers.js +0 -104
  236. package/lib/list.js +0 -139
  237. package/lib/mkdir.js +0 -229
  238. package/lib/mode-fix.js +0 -27
  239. package/lib/normalize-unicode.js +0 -12
  240. package/lib/pack.js +0 -432
  241. package/lib/parse.js +0 -552
  242. package/lib/path-reservations.js +0 -156
  243. package/lib/pax.js +0 -150
  244. package/lib/read-entry.js +0 -107
  245. package/lib/replace.js +0 -246
  246. package/lib/strip-absolute-path.js +0 -24
  247. package/lib/strip-trailing-slashes.js +0 -13
  248. package/lib/types.js +0 -44
  249. package/lib/unpack.js +0 -923
  250. package/lib/update.js +0 -40
  251. package/lib/warn-mixin.js +0 -24
  252. package/lib/winchars.js +0 -23
  253. package/lib/write-entry.js +0 -546
package/lib/update.js DELETED
@@ -1,40 +0,0 @@
1
- 'use strict'
2
-
3
- // tar -u
4
-
5
- const hlo = require('./high-level-opt.js')
6
- const r = require('./replace.js')
7
- // just call tar.r with the filter and mtimeCache
8
-
9
- module.exports = (opt_, files, cb) => {
10
- const opt = hlo(opt_)
11
-
12
- if (!opt.file) {
13
- throw new TypeError('file is required')
14
- }
15
-
16
- if (opt.gzip || opt.brotli || opt.file.endsWith('.br') || opt.file.endsWith('.tbr')) {
17
- throw new TypeError('cannot append to compressed archives')
18
- }
19
-
20
- if (!files || !Array.isArray(files) || !files.length) {
21
- throw new TypeError('no files or directories specified')
22
- }
23
-
24
- files = Array.from(files)
25
-
26
- mtimeFilter(opt)
27
- return r(opt, files, cb)
28
- }
29
-
30
- const mtimeFilter = opt => {
31
- const filter = opt.filter
32
-
33
- if (!opt.mtimeCache) {
34
- opt.mtimeCache = new Map()
35
- }
36
-
37
- opt.filter = filter ? (path, stat) =>
38
- filter(path, stat) && !(opt.mtimeCache.get(path) > stat.mtime)
39
- : (path, stat) => !(opt.mtimeCache.get(path) > stat.mtime)
40
- }
package/lib/warn-mixin.js DELETED
@@ -1,24 +0,0 @@
1
- 'use strict'
2
- module.exports = Base => class extends Base {
3
- warn (code, message, data = {}) {
4
- if (this.file) {
5
- data.file = this.file
6
- }
7
- if (this.cwd) {
8
- data.cwd = this.cwd
9
- }
10
- data.code = message instanceof Error && message.code || code
11
- data.tarCode = code
12
- if (!this.strict && data.recoverable !== false) {
13
- if (message instanceof Error) {
14
- data = Object.assign(message, data)
15
- message = message.message
16
- }
17
- this.emit('warn', data.tarCode, message, data)
18
- } else if (message instanceof Error) {
19
- this.emit('error', Object.assign(message, data))
20
- } else {
21
- this.emit('error', Object.assign(new Error(`${code}: ${message}`), data))
22
- }
23
- }
24
- }
package/lib/winchars.js DELETED
@@ -1,23 +0,0 @@
1
- 'use strict'
2
-
3
- // When writing files on Windows, translate the characters to their
4
- // 0xf000 higher-encoded versions.
5
-
6
- const raw = [
7
- '|',
8
- '<',
9
- '>',
10
- '?',
11
- ':',
12
- ]
13
-
14
- const win = raw.map(char =>
15
- String.fromCharCode(0xf000 + char.charCodeAt(0)))
16
-
17
- const toWin = new Map(raw.map((char, i) => [char, win[i]]))
18
- const toRaw = new Map(win.map((char, i) => [char, raw[i]]))
19
-
20
- module.exports = {
21
- encode: s => raw.reduce((s, c) => s.split(c).join(toWin.get(c)), s),
22
- decode: s => win.reduce((s, c) => s.split(c).join(toRaw.get(c)), s),
23
- }
@@ -1,546 +0,0 @@
1
- 'use strict'
2
- const { Minipass } = require('minipass')
3
- const Pax = require('./pax.js')
4
- const Header = require('./header.js')
5
- const fs = require('fs')
6
- const path = require('path')
7
- const normPath = require('./normalize-windows-path.js')
8
- const stripSlash = require('./strip-trailing-slashes.js')
9
-
10
- const prefixPath = (path, prefix) => {
11
- if (!prefix) {
12
- return normPath(path)
13
- }
14
- path = normPath(path).replace(/^\.(\/|$)/, '')
15
- return stripSlash(prefix) + '/' + path
16
- }
17
-
18
- const maxReadSize = 16 * 1024 * 1024
19
- const PROCESS = Symbol('process')
20
- const FILE = Symbol('file')
21
- const DIRECTORY = Symbol('directory')
22
- const SYMLINK = Symbol('symlink')
23
- const HARDLINK = Symbol('hardlink')
24
- const HEADER = Symbol('header')
25
- const READ = Symbol('read')
26
- const LSTAT = Symbol('lstat')
27
- const ONLSTAT = Symbol('onlstat')
28
- const ONREAD = Symbol('onread')
29
- const ONREADLINK = Symbol('onreadlink')
30
- const OPENFILE = Symbol('openfile')
31
- const ONOPENFILE = Symbol('onopenfile')
32
- const CLOSE = Symbol('close')
33
- const MODE = Symbol('mode')
34
- const AWAITDRAIN = Symbol('awaitDrain')
35
- const ONDRAIN = Symbol('ondrain')
36
- const PREFIX = Symbol('prefix')
37
- const HAD_ERROR = Symbol('hadError')
38
- const warner = require('./warn-mixin.js')
39
- const winchars = require('./winchars.js')
40
- const stripAbsolutePath = require('./strip-absolute-path.js')
41
-
42
- const modeFix = require('./mode-fix.js')
43
-
44
- const WriteEntry = warner(class WriteEntry extends Minipass {
45
- constructor (p, opt) {
46
- opt = opt || {}
47
- super(opt)
48
- if (typeof p !== 'string') {
49
- throw new TypeError('path is required')
50
- }
51
- this.path = normPath(p)
52
- // suppress atime, ctime, uid, gid, uname, gname
53
- this.portable = !!opt.portable
54
- // until node has builtin pwnam functions, this'll have to do
55
- this.myuid = process.getuid && process.getuid() || 0
56
- this.myuser = process.env.USER || ''
57
- this.maxReadSize = opt.maxReadSize || maxReadSize
58
- this.linkCache = opt.linkCache || new Map()
59
- this.statCache = opt.statCache || new Map()
60
- this.preservePaths = !!opt.preservePaths
61
- this.cwd = normPath(opt.cwd || process.cwd())
62
- this.strict = !!opt.strict
63
- this.noPax = !!opt.noPax
64
- this.noMtime = !!opt.noMtime
65
- this.mtime = opt.mtime || null
66
- this.prefix = opt.prefix ? normPath(opt.prefix) : null
67
-
68
- this.fd = null
69
- this.blockLen = null
70
- this.blockRemain = null
71
- this.buf = null
72
- this.offset = null
73
- this.length = null
74
- this.pos = null
75
- this.remain = null
76
-
77
- if (typeof opt.onwarn === 'function') {
78
- this.on('warn', opt.onwarn)
79
- }
80
-
81
- let pathWarn = false
82
- if (!this.preservePaths) {
83
- const [root, stripped] = stripAbsolutePath(this.path)
84
- if (root) {
85
- this.path = stripped
86
- pathWarn = root
87
- }
88
- }
89
-
90
- this.win32 = !!opt.win32 || process.platform === 'win32'
91
- if (this.win32) {
92
- // force the \ to / normalization, since we might not *actually*
93
- // be on windows, but want \ to be considered a path separator.
94
- this.path = winchars.decode(this.path.replace(/\\/g, '/'))
95
- p = p.replace(/\\/g, '/')
96
- }
97
-
98
- this.absolute = normPath(opt.absolute || path.resolve(this.cwd, p))
99
-
100
- if (this.path === '') {
101
- this.path = './'
102
- }
103
-
104
- if (pathWarn) {
105
- this.warn('TAR_ENTRY_INFO', `stripping ${pathWarn} from absolute path`, {
106
- entry: this,
107
- path: pathWarn + this.path,
108
- })
109
- }
110
-
111
- if (this.statCache.has(this.absolute)) {
112
- this[ONLSTAT](this.statCache.get(this.absolute))
113
- } else {
114
- this[LSTAT]()
115
- }
116
- }
117
-
118
- emit (ev, ...data) {
119
- if (ev === 'error') {
120
- this[HAD_ERROR] = true
121
- }
122
- return super.emit(ev, ...data)
123
- }
124
-
125
- [LSTAT] () {
126
- fs.lstat(this.absolute, (er, stat) => {
127
- if (er) {
128
- return this.emit('error', er)
129
- }
130
- this[ONLSTAT](stat)
131
- })
132
- }
133
-
134
- [ONLSTAT] (stat) {
135
- this.statCache.set(this.absolute, stat)
136
- this.stat = stat
137
- if (!stat.isFile()) {
138
- stat.size = 0
139
- }
140
- this.type = getType(stat)
141
- this.emit('stat', stat)
142
- this[PROCESS]()
143
- }
144
-
145
- [PROCESS] () {
146
- switch (this.type) {
147
- case 'File': return this[FILE]()
148
- case 'Directory': return this[DIRECTORY]()
149
- case 'SymbolicLink': return this[SYMLINK]()
150
- // unsupported types are ignored.
151
- default: return this.end()
152
- }
153
- }
154
-
155
- [MODE] (mode) {
156
- return modeFix(mode, this.type === 'Directory', this.portable)
157
- }
158
-
159
- [PREFIX] (path) {
160
- return prefixPath(path, this.prefix)
161
- }
162
-
163
- [HEADER] () {
164
- if (this.type === 'Directory' && this.portable) {
165
- this.noMtime = true
166
- }
167
-
168
- this.header = new Header({
169
- path: this[PREFIX](this.path),
170
- // only apply the prefix to hard links.
171
- linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
172
- : this.linkpath,
173
- // only the permissions and setuid/setgid/sticky bitflags
174
- // not the higher-order bits that specify file type
175
- mode: this[MODE](this.stat.mode),
176
- uid: this.portable ? null : this.stat.uid,
177
- gid: this.portable ? null : this.stat.gid,
178
- size: this.stat.size,
179
- mtime: this.noMtime ? null : this.mtime || this.stat.mtime,
180
- type: this.type,
181
- uname: this.portable ? null :
182
- this.stat.uid === this.myuid ? this.myuser : '',
183
- atime: this.portable ? null : this.stat.atime,
184
- ctime: this.portable ? null : this.stat.ctime,
185
- })
186
-
187
- if (this.header.encode() && !this.noPax) {
188
- super.write(new Pax({
189
- atime: this.portable ? null : this.header.atime,
190
- ctime: this.portable ? null : this.header.ctime,
191
- gid: this.portable ? null : this.header.gid,
192
- mtime: this.noMtime ? null : this.mtime || this.header.mtime,
193
- path: this[PREFIX](this.path),
194
- linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
195
- : this.linkpath,
196
- size: this.header.size,
197
- uid: this.portable ? null : this.header.uid,
198
- uname: this.portable ? null : this.header.uname,
199
- dev: this.portable ? null : this.stat.dev,
200
- ino: this.portable ? null : this.stat.ino,
201
- nlink: this.portable ? null : this.stat.nlink,
202
- }).encode())
203
- }
204
- super.write(this.header.block)
205
- }
206
-
207
- [DIRECTORY] () {
208
- if (this.path.slice(-1) !== '/') {
209
- this.path += '/'
210
- }
211
- this.stat.size = 0
212
- this[HEADER]()
213
- this.end()
214
- }
215
-
216
- [SYMLINK] () {
217
- fs.readlink(this.absolute, (er, linkpath) => {
218
- if (er) {
219
- return this.emit('error', er)
220
- }
221
- this[ONREADLINK](linkpath)
222
- })
223
- }
224
-
225
- [ONREADLINK] (linkpath) {
226
- this.linkpath = normPath(linkpath)
227
- this[HEADER]()
228
- this.end()
229
- }
230
-
231
- [HARDLINK] (linkpath) {
232
- this.type = 'Link'
233
- this.linkpath = normPath(path.relative(this.cwd, linkpath))
234
- this.stat.size = 0
235
- this[HEADER]()
236
- this.end()
237
- }
238
-
239
- [FILE] () {
240
- if (this.stat.nlink > 1) {
241
- const linkKey = this.stat.dev + ':' + this.stat.ino
242
- if (this.linkCache.has(linkKey)) {
243
- const linkpath = this.linkCache.get(linkKey)
244
- if (linkpath.indexOf(this.cwd) === 0) {
245
- return this[HARDLINK](linkpath)
246
- }
247
- }
248
- this.linkCache.set(linkKey, this.absolute)
249
- }
250
-
251
- this[HEADER]()
252
- if (this.stat.size === 0) {
253
- return this.end()
254
- }
255
-
256
- this[OPENFILE]()
257
- }
258
-
259
- [OPENFILE] () {
260
- fs.open(this.absolute, 'r', (er, fd) => {
261
- if (er) {
262
- return this.emit('error', er)
263
- }
264
- this[ONOPENFILE](fd)
265
- })
266
- }
267
-
268
- [ONOPENFILE] (fd) {
269
- this.fd = fd
270
- if (this[HAD_ERROR]) {
271
- return this[CLOSE]()
272
- }
273
-
274
- this.blockLen = 512 * Math.ceil(this.stat.size / 512)
275
- this.blockRemain = this.blockLen
276
- const bufLen = Math.min(this.blockLen, this.maxReadSize)
277
- this.buf = Buffer.allocUnsafe(bufLen)
278
- this.offset = 0
279
- this.pos = 0
280
- this.remain = this.stat.size
281
- this.length = this.buf.length
282
- this[READ]()
283
- }
284
-
285
- [READ] () {
286
- const { fd, buf, offset, length, pos } = this
287
- fs.read(fd, buf, offset, length, pos, (er, bytesRead) => {
288
- if (er) {
289
- // ignoring the error from close(2) is a bad practice, but at
290
- // this point we already have an error, don't need another one
291
- return this[CLOSE](() => this.emit('error', er))
292
- }
293
- this[ONREAD](bytesRead)
294
- })
295
- }
296
-
297
- [CLOSE] (cb) {
298
- fs.close(this.fd, cb)
299
- }
300
-
301
- [ONREAD] (bytesRead) {
302
- if (bytesRead <= 0 && this.remain > 0) {
303
- const er = new Error('encountered unexpected EOF')
304
- er.path = this.absolute
305
- er.syscall = 'read'
306
- er.code = 'EOF'
307
- return this[CLOSE](() => this.emit('error', er))
308
- }
309
-
310
- if (bytesRead > this.remain) {
311
- const er = new Error('did not encounter expected EOF')
312
- er.path = this.absolute
313
- er.syscall = 'read'
314
- er.code = 'EOF'
315
- return this[CLOSE](() => this.emit('error', er))
316
- }
317
-
318
- // null out the rest of the buffer, if we could fit the block padding
319
- // at the end of this loop, we've incremented bytesRead and this.remain
320
- // to be incremented up to the blockRemain level, as if we had expected
321
- // to get a null-padded file, and read it until the end. then we will
322
- // decrement both remain and blockRemain by bytesRead, and know that we
323
- // reached the expected EOF, without any null buffer to append.
324
- if (bytesRead === this.remain) {
325
- for (let i = bytesRead; i < this.length && bytesRead < this.blockRemain; i++) {
326
- this.buf[i + this.offset] = 0
327
- bytesRead++
328
- this.remain++
329
- }
330
- }
331
-
332
- const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
333
- this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
334
-
335
- const flushed = this.write(writeBuf)
336
- if (!flushed) {
337
- this[AWAITDRAIN](() => this[ONDRAIN]())
338
- } else {
339
- this[ONDRAIN]()
340
- }
341
- }
342
-
343
- [AWAITDRAIN] (cb) {
344
- this.once('drain', cb)
345
- }
346
-
347
- write (writeBuf) {
348
- if (this.blockRemain < writeBuf.length) {
349
- const er = new Error('writing more data than expected')
350
- er.path = this.absolute
351
- return this.emit('error', er)
352
- }
353
- this.remain -= writeBuf.length
354
- this.blockRemain -= writeBuf.length
355
- this.pos += writeBuf.length
356
- this.offset += writeBuf.length
357
- return super.write(writeBuf)
358
- }
359
-
360
- [ONDRAIN] () {
361
- if (!this.remain) {
362
- if (this.blockRemain) {
363
- super.write(Buffer.alloc(this.blockRemain))
364
- }
365
- return this[CLOSE](er => er ? this.emit('error', er) : this.end())
366
- }
367
-
368
- if (this.offset >= this.length) {
369
- // if we only have a smaller bit left to read, alloc a smaller buffer
370
- // otherwise, keep it the same length it was before.
371
- this.buf = Buffer.allocUnsafe(Math.min(this.blockRemain, this.buf.length))
372
- this.offset = 0
373
- }
374
- this.length = this.buf.length - this.offset
375
- this[READ]()
376
- }
377
- })
378
-
379
- class WriteEntrySync extends WriteEntry {
380
- [LSTAT] () {
381
- this[ONLSTAT](fs.lstatSync(this.absolute))
382
- }
383
-
384
- [SYMLINK] () {
385
- this[ONREADLINK](fs.readlinkSync(this.absolute))
386
- }
387
-
388
- [OPENFILE] () {
389
- this[ONOPENFILE](fs.openSync(this.absolute, 'r'))
390
- }
391
-
392
- [READ] () {
393
- let threw = true
394
- try {
395
- const { fd, buf, offset, length, pos } = this
396
- const bytesRead = fs.readSync(fd, buf, offset, length, pos)
397
- this[ONREAD](bytesRead)
398
- threw = false
399
- } finally {
400
- // ignoring the error from close(2) is a bad practice, but at
401
- // this point we already have an error, don't need another one
402
- if (threw) {
403
- try {
404
- this[CLOSE](() => {})
405
- } catch (er) {}
406
- }
407
- }
408
- }
409
-
410
- [AWAITDRAIN] (cb) {
411
- cb()
412
- }
413
-
414
- [CLOSE] (cb) {
415
- fs.closeSync(this.fd)
416
- cb()
417
- }
418
- }
419
-
420
- const WriteEntryTar = warner(class WriteEntryTar extends Minipass {
421
- constructor (readEntry, opt) {
422
- opt = opt || {}
423
- super(opt)
424
- this.preservePaths = !!opt.preservePaths
425
- this.portable = !!opt.portable
426
- this.strict = !!opt.strict
427
- this.noPax = !!opt.noPax
428
- this.noMtime = !!opt.noMtime
429
-
430
- this.readEntry = readEntry
431
- this.type = readEntry.type
432
- if (this.type === 'Directory' && this.portable) {
433
- this.noMtime = true
434
- }
435
-
436
- this.prefix = opt.prefix || null
437
-
438
- this.path = normPath(readEntry.path)
439
- this.mode = this[MODE](readEntry.mode)
440
- this.uid = this.portable ? null : readEntry.uid
441
- this.gid = this.portable ? null : readEntry.gid
442
- this.uname = this.portable ? null : readEntry.uname
443
- this.gname = this.portable ? null : readEntry.gname
444
- this.size = readEntry.size
445
- this.mtime = this.noMtime ? null : opt.mtime || readEntry.mtime
446
- this.atime = this.portable ? null : readEntry.atime
447
- this.ctime = this.portable ? null : readEntry.ctime
448
- this.linkpath = normPath(readEntry.linkpath)
449
-
450
- if (typeof opt.onwarn === 'function') {
451
- this.on('warn', opt.onwarn)
452
- }
453
-
454
- let pathWarn = false
455
- if (!this.preservePaths) {
456
- const [root, stripped] = stripAbsolutePath(this.path)
457
- if (root) {
458
- this.path = stripped
459
- pathWarn = root
460
- }
461
- }
462
-
463
- this.remain = readEntry.size
464
- this.blockRemain = readEntry.startBlockSize
465
-
466
- this.header = new Header({
467
- path: this[PREFIX](this.path),
468
- linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
469
- : this.linkpath,
470
- // only the permissions and setuid/setgid/sticky bitflags
471
- // not the higher-order bits that specify file type
472
- mode: this.mode,
473
- uid: this.portable ? null : this.uid,
474
- gid: this.portable ? null : this.gid,
475
- size: this.size,
476
- mtime: this.noMtime ? null : this.mtime,
477
- type: this.type,
478
- uname: this.portable ? null : this.uname,
479
- atime: this.portable ? null : this.atime,
480
- ctime: this.portable ? null : this.ctime,
481
- })
482
-
483
- if (pathWarn) {
484
- this.warn('TAR_ENTRY_INFO', `stripping ${pathWarn} from absolute path`, {
485
- entry: this,
486
- path: pathWarn + this.path,
487
- })
488
- }
489
-
490
- if (this.header.encode() && !this.noPax) {
491
- super.write(new Pax({
492
- atime: this.portable ? null : this.atime,
493
- ctime: this.portable ? null : this.ctime,
494
- gid: this.portable ? null : this.gid,
495
- mtime: this.noMtime ? null : this.mtime,
496
- path: this[PREFIX](this.path),
497
- linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
498
- : this.linkpath,
499
- size: this.size,
500
- uid: this.portable ? null : this.uid,
501
- uname: this.portable ? null : this.uname,
502
- dev: this.portable ? null : this.readEntry.dev,
503
- ino: this.portable ? null : this.readEntry.ino,
504
- nlink: this.portable ? null : this.readEntry.nlink,
505
- }).encode())
506
- }
507
-
508
- super.write(this.header.block)
509
- readEntry.pipe(this)
510
- }
511
-
512
- [PREFIX] (path) {
513
- return prefixPath(path, this.prefix)
514
- }
515
-
516
- [MODE] (mode) {
517
- return modeFix(mode, this.type === 'Directory', this.portable)
518
- }
519
-
520
- write (data) {
521
- const writeLen = data.length
522
- if (writeLen > this.blockRemain) {
523
- throw new Error('writing more to entry than is appropriate')
524
- }
525
- this.blockRemain -= writeLen
526
- return super.write(data)
527
- }
528
-
529
- end () {
530
- if (this.blockRemain) {
531
- super.write(Buffer.alloc(this.blockRemain))
532
- }
533
- return super.end()
534
- }
535
- })
536
-
537
- WriteEntry.Sync = WriteEntrySync
538
- WriteEntry.Tar = WriteEntryTar
539
-
540
- const getType = stat =>
541
- stat.isFile() ? 'File'
542
- : stat.isDirectory() ? 'Directory'
543
- : stat.isSymbolicLink() ? 'SymbolicLink'
544
- : 'Unsupported'
545
-
546
- module.exports = WriteEntry