forked from swiftlang/swift-sdk-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use _NIOFileSystem for file system operations.
Motivation: Add support for _NIOFileSystem to move away from OSFileSystem as per issue swiftlang#76. Modifications: Add SDKFileSystem that uses _NIOFileSystem. Change default AsyncFileSystem to SDKFileSystem Result: Using Swift NIO for file system operations.
- Loading branch information
1 parent
ea3e824
commit 61caa68
Showing
6 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Sources/Helpers/Vendor/_AsyncFileSystem/SDKFileSystem.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@preconcurrency import _NIOFileSystem | ||
|
||
public actor SDKFileSystem: AsyncFileSystem { | ||
public init(readChunkSize: Int = defaultChunkSize) { | ||
self.readChunkSize = readChunkSize | ||
} | ||
public static let defaultChunkSize = 512 * 1024 | ||
let readChunkSize: Int | ||
public func exists(_ path: SystemPackage.FilePath) async -> Bool { | ||
do { | ||
guard let _ = try await FileSystem.shared.info(forFileAt: path) else { | ||
return false | ||
} | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
public func withOpenReadableFile<T: Sendable>( | ||
_ path: SystemPackage.FilePath, _ body: @Sendable (OpenReadableFile) async throws -> T | ||
) async throws -> T { | ||
let fh = try await FileSystem.shared.openFile(forReadingAt: path) | ||
do { | ||
let result = try await body(OpenReadableFile(chunkSize: readChunkSize, fileHandle: .nio(fh))) | ||
try await fh.close() | ||
return result | ||
} catch { | ||
try await fh.close() | ||
throw error.attach(path) | ||
} | ||
} | ||
|
||
public func withOpenWritableFile<T: Sendable>( | ||
_ path: SystemPackage.FilePath, _ body: @Sendable (OpenWritableFile) async throws -> T | ||
) async throws -> T { | ||
let fh = try await FileSystem.shared.openFile(forWritingAt: path, options: .newFile(replaceExisting: true)) | ||
do { | ||
let result = try await body(OpenWritableFile(storage:.nio(fh),path:path)) | ||
try await fh.close() | ||
return result | ||
} catch { | ||
try await fh.close() | ||
throw error.attach(path) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters