don't chunk tar reads for no reason (faster)

This commit is contained in:
2024-11-12 18:37:46 +11:00
parent 089a7dcfe1
commit cee34c6f7a

View File

@ -24,7 +24,9 @@ public struct TarReader {
while true { while true {
let tarBlock = try stream.read(Self.tarBlockSize) let tarBlock = try stream.read(Self.tarBlockSize)
if tarBlock.isEmpty { break } if tarBlock.isEmpty { break }
if tarBlock.count < Self.tarBlockSize { throw TarError.unexpectedEndOfStream } guard tarBlock.count == Self.tarBlockSize else {
throw TarError.unexpectedEndOfStream
}
let type = UnicodeScalar(tarBlock[Self.tarTypeOffset]) let type = UnicodeScalar(tarBlock[Self.tarTypeOffset])
switch type { switch type {
@ -34,23 +36,24 @@ public struct TarReader {
let size = try Self.readSize(tarBlock) let size = try Self.readSize(tarBlock)
// Read file data // Read file data
var data = Data() var data = Data(count: size)
var bytesRemaining = size, readAmount = 0 if size > 0 {
while bytesRemaining > 0 { try data.withUnsafeMutableBytes {
//FIXME: just read the whole thing at once tbh guard size == (try stream.read(
readAmount = min(bytesRemaining, Self.tarBlockSize) $0.baseAddress!.assumingMemoryBound(to: UInt8.self),
let block = try stream.read(readAmount) maxLength: size)) else {
if block.count < readAmount { throw TarError.unexpectedEndOfStream } throw TarError.unexpectedEndOfStream
data += block
bytesRemaining -= readAmount
} }
entries.append(.file(name: name, data: data)) }
// Seek to next block boundry
// Seek to next block let blockN1 = Self.tarBlockSize - 1
let seekAmount = Self.tarBlockSize - readAmount let seekAmount = blockN1 - ((size + blockN1) % Self.tarBlockSize) // 511 ((size 1) & 0x1FF)
if seekAmount > 0 { if seekAmount > 0 {
try stream.seek(.current(seekAmount)) try stream.seek(.current(seekAmount))
} }
}
entries.append(.file(name: name, data: data))
case "5": case "5":
// Directory // Directory
let name = try Self.readName(tarBlock) let name = try Self.readName(tarBlock)