Skip to content

Commit 466ba78

Browse files
committed
Work around unable to read all of the bytes error
1 parent 30684f1 commit 466ba78

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ let package = Package(
137137
.target(name: "LinuxPlatform", condition: .when(platforms: [.linux])),
138138
.target(name: "MacOSPlatform", condition: .when(platforms: [.macOS])),
139139
.product(name: "ArgumentParser", package: "swift-argument-parser"),
140+
.product(name: "_NIOFileSystem", package: "swift-nio"),
140141
],
141142
path: "Tools/build-swiftly-release"
142143
),

Tools/build-swiftly-release/BuildSwiftlyRelease.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import AsyncHTTPClient
33
import Foundation
44
import SwiftlyCore
55
import SystemPackage
6+
import NIOFileSystem
67

78
#if os(macOS)
89
import MacOSPlatform
@@ -16,7 +17,7 @@ let currentPlatform = MacOS()
1617
let currentPlatform = Linux()
1718
#endif
1819

19-
typealias fs = FileSystem
20+
typealias fs = SwiftlyCore.FileSystem
2021
typealias sys = SystemCommand
2122

2223
extension Runnable {
@@ -156,12 +157,14 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
156157
guard libarchiveResponse.status == .ok else {
157158
throw Error(message: "Download failed with status: \(libarchiveResponse.status)")
158159
}
159-
let buf = try await libarchiveResponse.body.collect(upTo: 20 * 1024 * 1024)
160-
guard let contents = buf.getBytes(at: 0, length: buf.readableBytes) else {
161-
throw Error(message: "Unable to read all of the bytes")
160+
161+
try await NIOFileSystem.FileSystem.shared.withFileHandle(forWritingAt: buildCheckoutsDir / "libarchive-\(libArchiveVersion).tar.gz", options: .newFile(replaceExisting: true)) { fileHandle in
162+
var pos: Int64 = 0
163+
164+
for try await buffer in libarchiveResponse.body {
165+
pos += try await fileHandle.write(contentsOf: buffer, toAbsoluteOffset: pos)
166+
}
162167
}
163-
let data = Data(contents)
164-
try data.write(to: buildCheckoutsDir / "libarchive-\(libArchiveVersion).tar.gz")
165168

166169
let libArchiveTarShaActual = try await sys.sha256sum(files: buildCheckoutsDir / "libarchive-\(libArchiveVersion).tar.gz").output(currentPlatform)
167170
guard let libArchiveTarShaActual, libArchiveTarShaActual.starts(with: libArchiveTarSha) else {

0 commit comments

Comments
 (0)