You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

write-entry.js 15KB

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