1'use strict' 2 3const Buffer = require('./buffer.js') 4 5// XXX: This shares a lot in common with extract.js 6// maybe some DRY opportunity here? 7 8// tar -t 9const hlo = require('./high-level-opt.js') 10const Parser = require('./parse.js') 11const fs = require('fs') 12const fsm = require('fs-minipass') 13const path = require('path') 14const stripSlash = require('./strip-trailing-slashes.js') 15 16const t = module.exports = (opt_, files, cb) => { 17 if (typeof opt_ === 'function') 18 cb = opt_, files = null, opt_ = {} 19 else if (Array.isArray(opt_)) 20 files = opt_, opt_ = {} 21 22 if (typeof files === 'function') 23 cb = files, files = null 24 25 if (!files) 26 files = [] 27 else 28 files = Array.from(files) 29 30 const opt = hlo(opt_) 31 32 if (opt.sync && typeof cb === 'function') 33 throw new TypeError('callback not supported for sync tar functions') 34 35 if (!opt.file && typeof cb === 'function') 36 throw new TypeError('callback only supported with file option') 37 38 if (files.length) 39 filesFilter(opt, files) 40 41 if (!opt.noResume) 42 onentryFunction(opt) 43 44 return opt.file && opt.sync ? listFileSync(opt) 45 : opt.file ? listFile(opt, cb) 46 : list(opt) 47} 48 49const onentryFunction = opt => { 50 const onentry = opt.onentry 51 opt.onentry = onentry ? e => { 52 onentry(e) 53 e.resume() 54 } : e => e.resume() 55} 56 57// construct a filter that limits the file entries listed 58// include child entries if a dir is included 59const filesFilter = (opt, files) => { 60 const map = new Map(files.map(f => [stripSlash(f), true])) 61 const filter = opt.filter 62 63 const mapHas = (file, r) => { 64 const root = r || path.parse(file).root || '.' 65 const ret = file === root ? false 66 : map.has(file) ? map.get(file) 67 : mapHas(path.dirname(file), root) 68 69 map.set(file, ret) 70 return ret 71 } 72 73 opt.filter = filter 74 ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file)) 75 : file => mapHas(stripSlash(file)) 76} 77 78const listFileSync = opt => { 79 const p = list(opt) 80 const file = opt.file 81 let threw = true 82 let fd 83 try { 84 const stat = fs.statSync(file) 85 const readSize = opt.maxReadSize || 16*1024*1024 86 if (stat.size < readSize) { 87 p.end(fs.readFileSync(file)) 88 } else { 89 let pos = 0 90 const buf = Buffer.allocUnsafe(readSize) 91 fd = fs.openSync(file, 'r') 92 while (pos < stat.size) { 93 let bytesRead = fs.readSync(fd, buf, 0, readSize, pos) 94 pos += bytesRead 95 p.write(buf.slice(0, bytesRead)) 96 } 97 p.end() 98 } 99 threw = false 100 } finally { 101 if (threw && fd) 102 try { fs.closeSync(fd) } catch (er) {} 103 } 104} 105 106const listFile = (opt, cb) => { 107 const parse = new Parser(opt) 108 const readSize = opt.maxReadSize || 16*1024*1024 109 110 const file = opt.file 111 const p = new Promise((resolve, reject) => { 112 parse.on('error', reject) 113 parse.on('end', resolve) 114 115 fs.stat(file, (er, stat) => { 116 if (er) 117 reject(er) 118 else { 119 const stream = new fsm.ReadStream(file, { 120 readSize: readSize, 121 size: stat.size 122 }) 123 stream.on('error', reject) 124 stream.pipe(parse) 125 } 126 }) 127 }) 128 return cb ? p.then(cb, cb) : p 129} 130 131const list = opt => new Parser(opt) 132