Skip to content

Commit

Permalink
withOpenFile is now withFileRegion
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Nov 16, 2023
1 parent 7159ca9 commit 85deeb3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
30 changes: 14 additions & 16 deletions Sources/NIOPosix/NonBlockingFileIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -906,54 +906,52 @@ extension NonBlockingFileIO {
}
}

/// Open file at `path` on a private thread pool, run an operation given the file handle and region and then close the file handle.
/// Open file at `path` and query its size on a private thread pool, run an operation given
/// the resulting file region and then close the file handle.
///
/// The open file operation runs on a private thread pool.
///
/// - note: The reason this provides the `NIOFileHandle` and the `FileRegion` is that both the opening of a file as well as
/// the querying of its size are blocking.
/// The will return the result of the operation.
///
/// - note: This function opens a file and queries it size which are both blocking operations
///
/// - parameters:
/// - path: The path of the file to be opened for reading.
/// - body: operation to run with file handle and region
/// - returns: return value of operation
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func withOpenFile<Result>(
public func withFileRegion<Result>(
path: String,
_ body: (_ fileHandle: NIOFileHandle, _ fileRegion: FileRegion) async throws -> Result
_ body: (_ fileRegion: FileRegion) async throws -> Result
) async throws -> Result {
let file = try await self.threadPool.runIfActive {
let fileRegion = try await self.threadPool.runIfActive {
let fh = try NIOFileHandle(path: path)
do {
let fr = try FileRegion(fileHandle: fh)
return UnsafeTransfer((handle: fh, region: fr))
return UnsafeTransfer(fr)
} catch {
_ = try? fh.close()
throw error
}
}
let result: Result
do {
result = try await body(file.wrappedValue.handle, file.wrappedValue.region)
result = try await body(fileRegion.wrappedValue)
} catch {
try file.wrappedValue.handle.close()
try fileRegion.wrappedValue.fileHandle.close()
throw error
}
try file.wrappedValue.handle.close()
try fileRegion.wrappedValue.fileHandle.close()
return result
}

/// Open file at `path` on a private thread pool, run an operation given the file handle and region and then close the file handle.
/// Open file at `path` on a private thread pool, run an operation given the file handle and then close the file handle.
///
/// The open file operation runs on a private thread pool.
///
/// This function will return the result of the operation.
///
/// - parameters:
/// - path: The path of the file to be opened for writing.
/// - mode: File access mode.
/// - flags: Additional POSIX flags.
/// - returns: NIOFileHandle`.
/// - returns: return value of operation
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func withFileHandle<Result>(
path: String,
Expand Down
16 changes: 8 additions & 8 deletions Tests/NIOPosixTests/NonBlockingFileIOTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1292,11 +1292,11 @@ extension NonBlockingFileIOTest {
func testAsyncFileOpenWorks() async throws {
let content = "123"
try await withTemporaryFile(content: content) { (fileHandle, path) -> Void in
try await self.fileIO.withOpenFile(path: path) { fh, fr in
try fh.withUnsafeFileDescriptor { fd in
try await self.fileIO.withFileRegion(path: path) { fr in
try fr.fileHandle.withUnsafeFileDescriptor { fd in
XCTAssertGreaterThanOrEqual(fd, 0)
}
XCTAssertTrue(fh.isOpen)
XCTAssertTrue(fr.fileHandle.isOpen)
XCTAssertEqual(0, fr.readerIndex)
XCTAssertEqual(3, fr.endIndex)
}
Expand All @@ -1306,11 +1306,11 @@ extension NonBlockingFileIOTest {
func testAsyncFileOpenWorksWithEmptyFile() async throws {
let content = ""
try await withTemporaryFile(content: content) { (fileHandle, path) -> Void in
try await self.fileIO.withOpenFile(path: path) { fh, fr in
try fh.withUnsafeFileDescriptor { fd in
try await self.fileIO.withFileRegion(path: path) { fr in
try fr.fileHandle.withUnsafeFileDescriptor { fd in
XCTAssertGreaterThanOrEqual(fd, 0)
}
XCTAssertTrue(fh.isOpen)
XCTAssertTrue(fr.fileHandle.isOpen)
XCTAssertEqual(0, fr.readerIndex)
XCTAssertEqual(0, fr.endIndex)
}
Expand All @@ -1319,7 +1319,7 @@ extension NonBlockingFileIOTest {

func testAsyncFileOpenFails() async throws {
do {
_ = try await self.fileIO.withOpenFile(path: "/dev/null/this/does/not/exist") { _,_ in}
_ = try await self.fileIO.withFileRegion(path: "/dev/null/this/does/not/exist") { _ in}
XCTFail("should've thrown")
} catch let e as IOError where e.errnoCode == ENOTDIR {
// OK
Expand Down Expand Up @@ -1526,7 +1526,7 @@ extension NonBlockingFileIOTest {
let threadPool = NIOThreadPool(numberOfThreads: 1)
let fileIO = NonBlockingFileIO(threadPool: threadPool)
do {
try await fileIO.withOpenFile(path: path) { _,_ in }
try await fileIO.withFileRegion(path: path) { _ in }
XCTFail("testAsyncThrowsErrorOnUnstartedPool: openFile should throw an error")
} catch {
}
Expand Down

0 comments on commit 85deeb3

Please sign in to comment.