Skip to content

Commit

Permalink
Use NIOFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Dec 21, 2023
1 parent fab0fb8 commit 48481a7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ let package = Package(
.library(name: "SotoSignerV4", targets: ["SotoSignerV4"]),
],
dependencies: [
.package(url: "https://github.com/glbrntt/swift-nio.git", branch: "filesystem"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0"..<"4.0.0"),
.package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.0.1"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-metrics.git", "1.0.0"..<"3.0.0"),
.package(url: "https://github.com/apple/swift-nio.git", branch: "main"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.7.2"),
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.13.1"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.19.0"),
Expand All @@ -52,6 +52,7 @@ let package = Package(
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "Metrics", package: "swift-metrics"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "_NIOFileSystem", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "NIOSSL", package: "swift-nio-ssl"),
Expand Down
22 changes: 3 additions & 19 deletions Sources/SotoCore/Credential/ConfigFileLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import struct Foundation.UUID
import INIParser
import Logging
import NIOCore
import NIOPosix
import NIOFileSystem
#if os(Linux)
import Glibc
#else
Expand Down Expand Up @@ -95,37 +95,21 @@ enum ConfigFileLoader {
configFilePath: String,
profile: String
) async throws -> SharedCredentials {
let fileIO = NonBlockingFileIO(threadPool: NIOThreadPool.singleton)

let credentialsByteBuffer: ByteBuffer
do {
// Load credentials file
credentialsByteBuffer = try await self.loadFile(path: credentialsFilePath, using: fileIO)
credentialsByteBuffer = try await ByteBuffer(contentsOf: FilePath(credentialsFilePath), maximumSizeAllowed: .megabytes(1024))
} catch {
// Throw `.noProvider` error if credential file cannot be loaded
throw CredentialProviderError.noProvider
}

// Load profile config file
let configByteBuffer = try? await self.loadFile(path: configFilePath, using: fileIO)
let configByteBuffer = try? await ByteBuffer(contentsOf: FilePath(configFilePath), maximumSizeAllowed: .megabytes(1024))

return try self.parseSharedCredentials(from: credentialsByteBuffer, configByteBuffer: configByteBuffer, for: profile)
}

/// Load a file from disk without blocking the current thread
/// - Parameters:
/// - path: path for the file to load
/// - eventLoop: event loop to run everything on
/// - fileIO: non-blocking file IO
/// - Returns: Event loop future with file contents in a byte-buffer
static func loadFile(path: String, using fileIO: NonBlockingFileIO) async throws -> ByteBuffer {
let path = self.expandTildeInFilePath(path)

return try await fileIO.withFileRegion(path: path) { region in
try await fileIO.read(fileRegion: region, allocator: ByteBufferAllocator())
}
}

// MARK: - Byte Buffer parsing (INIParser)

/// Parse credentials from files (passed in as byte-buffers).
Expand Down

0 comments on commit 48481a7

Please sign in to comment.