write-entry.js 15 KB

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