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/unpack.js DELETED
@@ -1,923 +0,0 @@
1
- 'use strict'
2
-
3
- // the PEND/UNPEND stuff tracks whether we're ready to emit end/close yet.
4
- // but the path reservations are required to avoid race conditions where
5
- // parallelized unpack ops may mess with one another, due to dependencies
6
- // (like a Link depending on its target) or destructive operations (like
7
- // clobbering an fs object to create one of a different type.)
8
-
9
- const assert = require('assert')
10
- const Parser = require('./parse.js')
11
- const fs = require('fs')
12
- const fsm = require('fs-minipass')
13
- const path = require('path')
14
- const mkdir = require('./mkdir.js')
15
- const wc = require('./winchars.js')
16
- const pathReservations = require('./path-reservations.js')
17
- const stripAbsolutePath = require('./strip-absolute-path.js')
18
- const normPath = require('./normalize-windows-path.js')
19
- const stripSlash = require('./strip-trailing-slashes.js')
20
- const normalize = require('./normalize-unicode.js')
21
-
22
- const ONENTRY = Symbol('onEntry')
23
- const CHECKFS = Symbol('checkFs')
24
- const CHECKFS2 = Symbol('checkFs2')
25
- const PRUNECACHE = Symbol('pruneCache')
26
- const ISREUSABLE = Symbol('isReusable')
27
- const MAKEFS = Symbol('makeFs')
28
- const FILE = Symbol('file')
29
- const DIRECTORY = Symbol('directory')
30
- const LINK = Symbol('link')
31
- const SYMLINK = Symbol('symlink')
32
- const HARDLINK = Symbol('hardlink')
33
- const UNSUPPORTED = Symbol('unsupported')
34
- const CHECKPATH = Symbol('checkPath')
35
- const MKDIR = Symbol('mkdir')
36
- const ONERROR = Symbol('onError')
37
- const PENDING = Symbol('pending')
38
- const PEND = Symbol('pend')
39
- const UNPEND = Symbol('unpend')
40
- const ENDED = Symbol('ended')
41
- const MAYBECLOSE = Symbol('maybeClose')
42
- const SKIP = Symbol('skip')
43
- const DOCHOWN = Symbol('doChown')
44
- const UID = Symbol('uid')
45
- const GID = Symbol('gid')
46
- const CHECKED_CWD = Symbol('checkedCwd')
47
- const crypto = require('crypto')
48
- const getFlag = require('./get-write-flag.js')
49
- const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
50
- const isWindows = platform === 'win32'
51
- const DEFAULT_MAX_DEPTH = 1024
52
-
53
- // Unlinks on Windows are not atomic.
54
- //
55
- // This means that if you have a file entry, followed by another
56
- // file entry with an identical name, and you cannot re-use the file
57
- // (because it's a hardlink, or because unlink:true is set, or it's
58
- // Windows, which does not have useful nlink values), then the unlink
59
- // will be committed to the disk AFTER the new file has been written
60
- // over the old one, deleting the new file.
61
- //
62
- // To work around this, on Windows systems, we rename the file and then
63
- // delete the renamed file. It's a sloppy kludge, but frankly, I do not
64
- // know of a better way to do this, given windows' non-atomic unlink
65
- // semantics.
66
- //
67
- // See: https://github.com/npm/node-tar/issues/183
68
- /* istanbul ignore next */
69
- const unlinkFile = (path, cb) => {
70
- if (!isWindows) {
71
- return fs.unlink(path, cb)
72
- }
73
-
74
- const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
75
- fs.rename(path, name, er => {
76
- if (er) {
77
- return cb(er)
78
- }
79
- fs.unlink(name, cb)
80
- })
81
- }
82
-
83
- /* istanbul ignore next */
84
- const unlinkFileSync = path => {
85
- if (!isWindows) {
86
- return fs.unlinkSync(path)
87
- }
88
-
89
- const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
90
- fs.renameSync(path, name)
91
- fs.unlinkSync(name)
92
- }
93
-
94
- // this.gid, entry.gid, this.processUid
95
- const uint32 = (a, b, c) =>
96
- a === a >>> 0 ? a
97
- : b === b >>> 0 ? b
98
- : c
99
-
100
- // clear the cache if it's a case-insensitive unicode-squashing match.
101
- // we can't know if the current file system is case-sensitive or supports
102
- // unicode fully, so we check for similarity on the maximally compatible
103
- // representation. Err on the side of pruning, since all it's doing is
104
- // preventing lstats, and it's not the end of the world if we get a false
105
- // positive.
106
- // Note that on windows, we always drop the entire cache whenever a
107
- // symbolic link is encountered, because 8.3 filenames are impossible
108
- // to reason about, and collisions are hazards rather than just failures.
109
- const cacheKeyNormalize = path => stripSlash(normPath(normalize(path)))
110
- .toLowerCase()
111
-
112
- const pruneCache = (cache, abs) => {
113
- abs = cacheKeyNormalize(abs)
114
- for (const path of cache.keys()) {
115
- const pnorm = cacheKeyNormalize(path)
116
- if (pnorm === abs || pnorm.indexOf(abs + '/') === 0) {
117
- cache.delete(path)
118
- }
119
- }
120
- }
121
-
122
- const dropCache = cache => {
123
- for (const key of cache.keys()) {
124
- cache.delete(key)
125
- }
126
- }
127
-
128
- class Unpack extends Parser {
129
- constructor (opt) {
130
- if (!opt) {
131
- opt = {}
132
- }
133
-
134
- opt.ondone = _ => {
135
- this[ENDED] = true
136
- this[MAYBECLOSE]()
137
- }
138
-
139
- super(opt)
140
-
141
- this[CHECKED_CWD] = false
142
-
143
- this.reservations = pathReservations()
144
-
145
- this.transform = typeof opt.transform === 'function' ? opt.transform : null
146
-
147
- this.writable = true
148
- this.readable = false
149
-
150
- this[PENDING] = 0
151
- this[ENDED] = false
152
-
153
- this.dirCache = opt.dirCache || new Map()
154
-
155
- if (typeof opt.uid === 'number' || typeof opt.gid === 'number') {
156
- // need both or neither
157
- if (typeof opt.uid !== 'number' || typeof opt.gid !== 'number') {
158
- throw new TypeError('cannot set owner without number uid and gid')
159
- }
160
- if (opt.preserveOwner) {
161
- throw new TypeError(
162
- 'cannot preserve owner in archive and also set owner explicitly')
163
- }
164
- this.uid = opt.uid
165
- this.gid = opt.gid
166
- this.setOwner = true
167
- } else {
168
- this.uid = null
169
- this.gid = null
170
- this.setOwner = false
171
- }
172
-
173
- // default true for root
174
- if (opt.preserveOwner === undefined && typeof opt.uid !== 'number') {
175
- this.preserveOwner = process.getuid && process.getuid() === 0
176
- } else {
177
- this.preserveOwner = !!opt.preserveOwner
178
- }
179
-
180
- this.processUid = (this.preserveOwner || this.setOwner) && process.getuid ?
181
- process.getuid() : null
182
- this.processGid = (this.preserveOwner || this.setOwner) && process.getgid ?
183
- process.getgid() : null
184
-
185
- // prevent excessively deep nesting of subfolders
186
- // set to `Infinity` to remove this restriction
187
- this.maxDepth = typeof opt.maxDepth === 'number'
188
- ? opt.maxDepth
189
- : DEFAULT_MAX_DEPTH
190
-
191
- // mostly just for testing, but useful in some cases.
192
- // Forcibly trigger a chown on every entry, no matter what
193
- this.forceChown = opt.forceChown === true
194
-
195
- // turn ><?| in filenames into 0xf000-higher encoded forms
196
- this.win32 = !!opt.win32 || isWindows
197
-
198
- // do not unpack over files that are newer than what's in the archive
199
- this.newer = !!opt.newer
200
-
201
- // do not unpack over ANY files
202
- this.keep = !!opt.keep
203
-
204
- // do not set mtime/atime of extracted entries
205
- this.noMtime = !!opt.noMtime
206
-
207
- // allow .., absolute path entries, and unpacking through symlinks
208
- // without this, warn and skip .., relativize absolutes, and error
209
- // on symlinks in extraction path
210
- this.preservePaths = !!opt.preservePaths
211
-
212
- // unlink files and links before writing. This breaks existing hard
213
- // links, and removes symlink directories rather than erroring
214
- this.unlink = !!opt.unlink
215
-
216
- this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
217
- this.strip = +opt.strip || 0
218
- // if we're not chmodding, then we don't need the process umask
219
- this.processUmask = opt.noChmod ? 0 : process.umask()
220
- this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
221
-
222
- // default mode for dirs created as parents
223
- this.dmode = opt.dmode || (0o0777 & (~this.umask))
224
- this.fmode = opt.fmode || (0o0666 & (~this.umask))
225
-
226
- this.on('entry', entry => this[ONENTRY](entry))
227
- }
228
-
229
- // a bad or damaged archive is a warning for Parser, but an error
230
- // when extracting. Mark those errors as unrecoverable, because
231
- // the Unpack contract cannot be met.
232
- warn (code, msg, data = {}) {
233
- if (code === 'TAR_BAD_ARCHIVE' || code === 'TAR_ABORT') {
234
- data.recoverable = false
235
- }
236
- return super.warn(code, msg, data)
237
- }
238
-
239
- [MAYBECLOSE] () {
240
- if (this[ENDED] && this[PENDING] === 0) {
241
- this.emit('prefinish')
242
- this.emit('finish')
243
- this.emit('end')
244
- }
245
- }
246
-
247
- [CHECKPATH] (entry) {
248
- const p = normPath(entry.path)
249
- const parts = p.split('/')
250
-
251
- if (this.strip) {
252
- if (parts.length < this.strip) {
253
- return false
254
- }
255
- if (entry.type === 'Link') {
256
- const linkparts = normPath(entry.linkpath).split('/')
257
- if (linkparts.length >= this.strip) {
258
- entry.linkpath = linkparts.slice(this.strip).join('/')
259
- } else {
260
- return false
261
- }
262
- }
263
- parts.splice(0, this.strip)
264
- entry.path = parts.join('/')
265
- }
266
-
267
- if (isFinite(this.maxDepth) && parts.length > this.maxDepth) {
268
- this.warn('TAR_ENTRY_ERROR', 'path excessively deep', {
269
- entry,
270
- path: p,
271
- depth: parts.length,
272
- maxDepth: this.maxDepth,
273
- })
274
- return false
275
- }
276
-
277
- if (!this.preservePaths) {
278
- if (parts.includes('..') || isWindows && /^[a-z]:\.\.$/i.test(parts[0])) {
279
- this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
280
- entry,
281
- path: p,
282
- })
283
- return false
284
- }
285
-
286
- // strip off the root
287
- const [root, stripped] = stripAbsolutePath(p)
288
- if (root) {
289
- entry.path = stripped
290
- this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
291
- entry,
292
- path: p,
293
- })
294
- }
295
- }
296
-
297
- if (path.isAbsolute(entry.path)) {
298
- entry.absolute = normPath(path.resolve(entry.path))
299
- } else {
300
- entry.absolute = normPath(path.resolve(this.cwd, entry.path))
301
- }
302
-
303
- // if we somehow ended up with a path that escapes the cwd, and we are
304
- // not in preservePaths mode, then something is fishy! This should have
305
- // been prevented above, so ignore this for coverage.
306
- /* istanbul ignore if - defense in depth */
307
- if (!this.preservePaths &&
308
- entry.absolute.indexOf(this.cwd + '/') !== 0 &&
309
- entry.absolute !== this.cwd) {
310
- this.warn('TAR_ENTRY_ERROR', 'path escaped extraction target', {
311
- entry,
312
- path: normPath(entry.path),
313
- resolvedPath: entry.absolute,
314
- cwd: this.cwd,
315
- })
316
- return false
317
- }
318
-
319
- // an archive can set properties on the extraction directory, but it
320
- // may not replace the cwd with a different kind of thing entirely.
321
- if (entry.absolute === this.cwd &&
322
- entry.type !== 'Directory' &&
323
- entry.type !== 'GNUDumpDir') {
324
- return false
325
- }
326
-
327
- // only encode : chars that aren't drive letter indicators
328
- if (this.win32) {
329
- const { root: aRoot } = path.win32.parse(entry.absolute)
330
- entry.absolute = aRoot + wc.encode(entry.absolute.slice(aRoot.length))
331
- const { root: pRoot } = path.win32.parse(entry.path)
332
- entry.path = pRoot + wc.encode(entry.path.slice(pRoot.length))
333
- }
334
-
335
- return true
336
- }
337
-
338
- [ONENTRY] (entry) {
339
- if (!this[CHECKPATH](entry)) {
340
- return entry.resume()
341
- }
342
-
343
- assert.equal(typeof entry.absolute, 'string')
344
-
345
- switch (entry.type) {
346
- case 'Directory':
347
- case 'GNUDumpDir':
348
- if (entry.mode) {
349
- entry.mode = entry.mode | 0o700
350
- }
351
-
352
- // eslint-disable-next-line no-fallthrough
353
- case 'File':
354
- case 'OldFile':
355
- case 'ContiguousFile':
356
- case 'Link':
357
- case 'SymbolicLink':
358
- return this[CHECKFS](entry)
359
-
360
- case 'CharacterDevice':
361
- case 'BlockDevice':
362
- case 'FIFO':
363
- default:
364
- return this[UNSUPPORTED](entry)
365
- }
366
- }
367
-
368
- [ONERROR] (er, entry) {
369
- // Cwd has to exist, or else nothing works. That's serious.
370
- // Other errors are warnings, which raise the error in strict
371
- // mode, but otherwise continue on.
372
- if (er.name === 'CwdError') {
373
- this.emit('error', er)
374
- } else {
375
- this.warn('TAR_ENTRY_ERROR', er, { entry })
376
- this[UNPEND]()
377
- entry.resume()
378
- }
379
- }
380
-
381
- [MKDIR] (dir, mode, cb) {
382
- mkdir(normPath(dir), {
383
- uid: this.uid,
384
- gid: this.gid,
385
- processUid: this.processUid,
386
- processGid: this.processGid,
387
- umask: this.processUmask,
388
- preserve: this.preservePaths,
389
- unlink: this.unlink,
390
- cache: this.dirCache,
391
- cwd: this.cwd,
392
- mode: mode,
393
- noChmod: this.noChmod,
394
- }, cb)
395
- }
396
-
397
- [DOCHOWN] (entry) {
398
- // in preserve owner mode, chown if the entry doesn't match process
399
- // in set owner mode, chown if setting doesn't match process
400
- return this.forceChown ||
401
- this.preserveOwner &&
402
- (typeof entry.uid === 'number' && entry.uid !== this.processUid ||
403
- typeof entry.gid === 'number' && entry.gid !== this.processGid)
404
- ||
405
- (typeof this.uid === 'number' && this.uid !== this.processUid ||
406
- typeof this.gid === 'number' && this.gid !== this.processGid)
407
- }
408
-
409
- [UID] (entry) {
410
- return uint32(this.uid, entry.uid, this.processUid)
411
- }
412
-
413
- [GID] (entry) {
414
- return uint32(this.gid, entry.gid, this.processGid)
415
- }
416
-
417
- [FILE] (entry, fullyDone) {
418
- const mode = entry.mode & 0o7777 || this.fmode
419
- const stream = new fsm.WriteStream(entry.absolute, {
420
- flags: getFlag(entry.size),
421
- mode: mode,
422
- autoClose: false,
423
- })
424
- stream.on('error', er => {
425
- if (stream.fd) {
426
- fs.close(stream.fd, () => {})
427
- }
428
-
429
- // flush all the data out so that we aren't left hanging
430
- // if the error wasn't actually fatal. otherwise the parse
431
- // is blocked, and we never proceed.
432
- stream.write = () => true
433
- this[ONERROR](er, entry)
434
- fullyDone()
435
- })
436
-
437
- let actions = 1
438
- const done = er => {
439
- if (er) {
440
- /* istanbul ignore else - we should always have a fd by now */
441
- if (stream.fd) {
442
- fs.close(stream.fd, () => {})
443
- }
444
-
445
- this[ONERROR](er, entry)
446
- fullyDone()
447
- return
448
- }
449
-
450
- if (--actions === 0) {
451
- fs.close(stream.fd, er => {
452
- if (er) {
453
- this[ONERROR](er, entry)
454
- } else {
455
- this[UNPEND]()
456
- }
457
- fullyDone()
458
- })
459
- }
460
- }
461
-
462
- stream.on('finish', _ => {
463
- // if futimes fails, try utimes
464
- // if utimes fails, fail with the original error
465
- // same for fchown/chown
466
- const abs = entry.absolute
467
- const fd = stream.fd
468
-
469
- if (entry.mtime && !this.noMtime) {
470
- actions++
471
- const atime = entry.atime || new Date()
472
- const mtime = entry.mtime
473
- fs.futimes(fd, atime, mtime, er =>
474
- er ? fs.utimes(abs, atime, mtime, er2 => done(er2 && er))
475
- : done())
476
- }
477
-
478
- if (this[DOCHOWN](entry)) {
479
- actions++
480
- const uid = this[UID](entry)
481
- const gid = this[GID](entry)
482
- fs.fchown(fd, uid, gid, er =>
483
- er ? fs.chown(abs, uid, gid, er2 => done(er2 && er))
484
- : done())
485
- }
486
-
487
- done()
488
- })
489
-
490
- const tx = this.transform ? this.transform(entry) || entry : entry
491
- if (tx !== entry) {
492
- tx.on('error', er => {
493
- this[ONERROR](er, entry)
494
- fullyDone()
495
- })
496
- entry.pipe(tx)
497
- }
498
- tx.pipe(stream)
499
- }
500
-
501
- [DIRECTORY] (entry, fullyDone) {
502
- const mode = entry.mode & 0o7777 || this.dmode
503
- this[MKDIR](entry.absolute, mode, er => {
504
- if (er) {
505
- this[ONERROR](er, entry)
506
- fullyDone()
507
- return
508
- }
509
-
510
- let actions = 1
511
- const done = _ => {
512
- if (--actions === 0) {
513
- fullyDone()
514
- this[UNPEND]()
515
- entry.resume()
516
- }
517
- }
518
-
519
- if (entry.mtime && !this.noMtime) {
520
- actions++
521
- fs.utimes(entry.absolute, entry.atime || new Date(), entry.mtime, done)
522
- }
523
-
524
- if (this[DOCHOWN](entry)) {
525
- actions++
526
- fs.chown(entry.absolute, this[UID](entry), this[GID](entry), done)
527
- }
528
-
529
- done()
530
- })
531
- }
532
-
533
- [UNSUPPORTED] (entry) {
534
- entry.unsupported = true
535
- this.warn('TAR_ENTRY_UNSUPPORTED',
536
- `unsupported entry type: ${entry.type}`, { entry })
537
- entry.resume()
538
- }
539
-
540
- [SYMLINK] (entry, done) {
541
- this[LINK](entry, entry.linkpath, 'symlink', done)
542
- }
543
-
544
- [HARDLINK] (entry, done) {
545
- const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
546
- this[LINK](entry, linkpath, 'link', done)
547
- }
548
-
549
- [PEND] () {
550
- this[PENDING]++
551
- }
552
-
553
- [UNPEND] () {
554
- this[PENDING]--
555
- this[MAYBECLOSE]()
556
- }
557
-
558
- [SKIP] (entry) {
559
- this[UNPEND]()
560
- entry.resume()
561
- }
562
-
563
- // Check if we can reuse an existing filesystem entry safely and
564
- // overwrite it, rather than unlinking and recreating
565
- // Windows doesn't report a useful nlink, so we just never reuse entries
566
- [ISREUSABLE] (entry, st) {
567
- return entry.type === 'File' &&
568
- !this.unlink &&
569
- st.isFile() &&
570
- st.nlink <= 1 &&
571
- !isWindows
572
- }
573
-
574
- // check if a thing is there, and if so, try to clobber it
575
- [CHECKFS] (entry) {
576
- this[PEND]()
577
- const paths = [entry.path]
578
- if (entry.linkpath) {
579
- paths.push(entry.linkpath)
580
- }
581
- this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
582
- }
583
-
584
- [PRUNECACHE] (entry) {
585
- // if we are not creating a directory, and the path is in the dirCache,
586
- // then that means we are about to delete the directory we created
587
- // previously, and it is no longer going to be a directory, and neither
588
- // is any of its children.
589
- // If a symbolic link is encountered, all bets are off. There is no
590
- // reasonable way to sanitize the cache in such a way we will be able to
591
- // avoid having filesystem collisions. If this happens with a non-symlink
592
- // entry, it'll just fail to unpack, but a symlink to a directory, using an
593
- // 8.3 shortname or certain unicode attacks, can evade detection and lead
594
- // to arbitrary writes to anywhere on the system.
595
- if (entry.type === 'SymbolicLink') {
596
- dropCache(this.dirCache)
597
- } else if (entry.type !== 'Directory') {
598
- pruneCache(this.dirCache, entry.absolute)
599
- }
600
- }
601
-
602
- [CHECKFS2] (entry, fullyDone) {
603
- this[PRUNECACHE](entry)
604
-
605
- const done = er => {
606
- this[PRUNECACHE](entry)
607
- fullyDone(er)
608
- }
609
-
610
- const checkCwd = () => {
611
- this[MKDIR](this.cwd, this.dmode, er => {
612
- if (er) {
613
- this[ONERROR](er, entry)
614
- done()
615
- return
616
- }
617
- this[CHECKED_CWD] = true
618
- start()
619
- })
620
- }
621
-
622
- const start = () => {
623
- if (entry.absolute !== this.cwd) {
624
- const parent = normPath(path.dirname(entry.absolute))
625
- if (parent !== this.cwd) {
626
- return this[MKDIR](parent, this.dmode, er => {
627
- if (er) {
628
- this[ONERROR](er, entry)
629
- done()
630
- return
631
- }
632
- afterMakeParent()
633
- })
634
- }
635
- }
636
- afterMakeParent()
637
- }
638
-
639
- const afterMakeParent = () => {
640
- fs.lstat(entry.absolute, (lstatEr, st) => {
641
- if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
642
- this[SKIP](entry)
643
- done()
644
- return
645
- }
646
- if (lstatEr || this[ISREUSABLE](entry, st)) {
647
- return this[MAKEFS](null, entry, done)
648
- }
649
-
650
- if (st.isDirectory()) {
651
- if (entry.type === 'Directory') {
652
- const needChmod = !this.noChmod &&
653
- entry.mode &&
654
- (st.mode & 0o7777) !== entry.mode
655
- const afterChmod = er => this[MAKEFS](er, entry, done)
656
- if (!needChmod) {
657
- return afterChmod()
658
- }
659
- return fs.chmod(entry.absolute, entry.mode, afterChmod)
660
- }
661
- // Not a dir entry, have to remove it.
662
- // NB: the only way to end up with an entry that is the cwd
663
- // itself, in such a way that == does not detect, is a
664
- // tricky windows absolute path with UNC or 8.3 parts (and
665
- // preservePaths:true, or else it will have been stripped).
666
- // In that case, the user has opted out of path protections
667
- // explicitly, so if they blow away the cwd, c'est la vie.
668
- if (entry.absolute !== this.cwd) {
669
- return fs.rmdir(entry.absolute, er =>
670
- this[MAKEFS](er, entry, done))
671
- }
672
- }
673
-
674
- // not a dir, and not reusable
675
- // don't remove if the cwd, we want that error
676
- if (entry.absolute === this.cwd) {
677
- return this[MAKEFS](null, entry, done)
678
- }
679
-
680
- unlinkFile(entry.absolute, er =>
681
- this[MAKEFS](er, entry, done))
682
- })
683
- }
684
-
685
- if (this[CHECKED_CWD]) {
686
- start()
687
- } else {
688
- checkCwd()
689
- }
690
- }
691
-
692
- [MAKEFS] (er, entry, done) {
693
- if (er) {
694
- this[ONERROR](er, entry)
695
- done()
696
- return
697
- }
698
-
699
- switch (entry.type) {
700
- case 'File':
701
- case 'OldFile':
702
- case 'ContiguousFile':
703
- return this[FILE](entry, done)
704
-
705
- case 'Link':
706
- return this[HARDLINK](entry, done)
707
-
708
- case 'SymbolicLink':
709
- return this[SYMLINK](entry, done)
710
-
711
- case 'Directory':
712
- case 'GNUDumpDir':
713
- return this[DIRECTORY](entry, done)
714
- }
715
- }
716
-
717
- [LINK] (entry, linkpath, link, done) {
718
- // XXX: get the type ('symlink' or 'junction') for windows
719
- fs[link](linkpath, entry.absolute, er => {
720
- if (er) {
721
- this[ONERROR](er, entry)
722
- } else {
723
- this[UNPEND]()
724
- entry.resume()
725
- }
726
- done()
727
- })
728
- }
729
- }
730
-
731
- const callSync = fn => {
732
- try {
733
- return [null, fn()]
734
- } catch (er) {
735
- return [er, null]
736
- }
737
- }
738
- class UnpackSync extends Unpack {
739
- [MAKEFS] (er, entry) {
740
- return super[MAKEFS](er, entry, () => {})
741
- }
742
-
743
- [CHECKFS] (entry) {
744
- this[PRUNECACHE](entry)
745
-
746
- if (!this[CHECKED_CWD]) {
747
- const er = this[MKDIR](this.cwd, this.dmode)
748
- if (er) {
749
- return this[ONERROR](er, entry)
750
- }
751
- this[CHECKED_CWD] = true
752
- }
753
-
754
- // don't bother to make the parent if the current entry is the cwd,
755
- // we've already checked it.
756
- if (entry.absolute !== this.cwd) {
757
- const parent = normPath(path.dirname(entry.absolute))
758
- if (parent !== this.cwd) {
759
- const mkParent = this[MKDIR](parent, this.dmode)
760
- if (mkParent) {
761
- return this[ONERROR](mkParent, entry)
762
- }
763
- }
764
- }
765
-
766
- const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
767
- if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
768
- return this[SKIP](entry)
769
- }
770
-
771
- if (lstatEr || this[ISREUSABLE](entry, st)) {
772
- return this[MAKEFS](null, entry)
773
- }
774
-
775
- if (st.isDirectory()) {
776
- if (entry.type === 'Directory') {
777
- const needChmod = !this.noChmod &&
778
- entry.mode &&
779
- (st.mode & 0o7777) !== entry.mode
780
- const [er] = needChmod ? callSync(() => {
781
- fs.chmodSync(entry.absolute, entry.mode)
782
- }) : []
783
- return this[MAKEFS](er, entry)
784
- }
785
- // not a dir entry, have to remove it
786
- const [er] = callSync(() => fs.rmdirSync(entry.absolute))
787
- this[MAKEFS](er, entry)
788
- }
789
-
790
- // not a dir, and not reusable.
791
- // don't remove if it's the cwd, since we want that error.
792
- const [er] = entry.absolute === this.cwd ? []
793
- : callSync(() => unlinkFileSync(entry.absolute))
794
- this[MAKEFS](er, entry)
795
- }
796
-
797
- [FILE] (entry, done) {
798
- const mode = entry.mode & 0o7777 || this.fmode
799
-
800
- const oner = er => {
801
- let closeError
802
- try {
803
- fs.closeSync(fd)
804
- } catch (e) {
805
- closeError = e
806
- }
807
- if (er || closeError) {
808
- this[ONERROR](er || closeError, entry)
809
- }
810
- done()
811
- }
812
-
813
- let fd
814
- try {
815
- fd = fs.openSync(entry.absolute, getFlag(entry.size), mode)
816
- } catch (er) {
817
- return oner(er)
818
- }
819
- const tx = this.transform ? this.transform(entry) || entry : entry
820
- if (tx !== entry) {
821
- tx.on('error', er => this[ONERROR](er, entry))
822
- entry.pipe(tx)
823
- }
824
-
825
- tx.on('data', chunk => {
826
- try {
827
- fs.writeSync(fd, chunk, 0, chunk.length)
828
- } catch (er) {
829
- oner(er)
830
- }
831
- })
832
-
833
- tx.on('end', _ => {
834
- let er = null
835
- // try both, falling futimes back to utimes
836
- // if either fails, handle the first error
837
- if (entry.mtime && !this.noMtime) {
838
- const atime = entry.atime || new Date()
839
- const mtime = entry.mtime
840
- try {
841
- fs.futimesSync(fd, atime, mtime)
842
- } catch (futimeser) {
843
- try {
844
- fs.utimesSync(entry.absolute, atime, mtime)
845
- } catch (utimeser) {
846
- er = futimeser
847
- }
848
- }
849
- }
850
-
851
- if (this[DOCHOWN](entry)) {
852
- const uid = this[UID](entry)
853
- const gid = this[GID](entry)
854
-
855
- try {
856
- fs.fchownSync(fd, uid, gid)
857
- } catch (fchowner) {
858
- try {
859
- fs.chownSync(entry.absolute, uid, gid)
860
- } catch (chowner) {
861
- er = er || fchowner
862
- }
863
- }
864
- }
865
-
866
- oner(er)
867
- })
868
- }
869
-
870
- [DIRECTORY] (entry, done) {
871
- const mode = entry.mode & 0o7777 || this.dmode
872
- const er = this[MKDIR](entry.absolute, mode)
873
- if (er) {
874
- this[ONERROR](er, entry)
875
- done()
876
- return
877
- }
878
- if (entry.mtime && !this.noMtime) {
879
- try {
880
- fs.utimesSync(entry.absolute, entry.atime || new Date(), entry.mtime)
881
- } catch (er) {}
882
- }
883
- if (this[DOCHOWN](entry)) {
884
- try {
885
- fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
886
- } catch (er) {}
887
- }
888
- done()
889
- entry.resume()
890
- }
891
-
892
- [MKDIR] (dir, mode) {
893
- try {
894
- return mkdir.sync(normPath(dir), {
895
- uid: this.uid,
896
- gid: this.gid,
897
- processUid: this.processUid,
898
- processGid: this.processGid,
899
- umask: this.processUmask,
900
- preserve: this.preservePaths,
901
- unlink: this.unlink,
902
- cache: this.dirCache,
903
- cwd: this.cwd,
904
- mode: mode,
905
- })
906
- } catch (er) {
907
- return er
908
- }
909
- }
910
-
911
- [LINK] (entry, linkpath, link, done) {
912
- try {
913
- fs[link + 'Sync'](linkpath, entry.absolute)
914
- done()
915
- entry.resume()
916
- } catch (er) {
917
- return this[ONERROR](er, entry)
918
- }
919
- }
920
- }
921
-
922
- Unpack.Sync = UnpackSync
923
- module.exports = Unpack