diff --git a/Sources/NIO/NonBlockingFileIO.swift b/Sources/NIO/NonBlockingFileIO.swift index f426b3bc7..3e9f3a894 100644 --- a/Sources/NIO/NonBlockingFileIO.swift +++ b/Sources/NIO/NonBlockingFileIO.swift @@ -214,6 +214,46 @@ public struct NonBlockingFileIO { } } + /// Write `buffer` to `fileHandle` in `NonBlockingFileIO`'s private thread pool which is separate from any `EventLoop` thread. + /// + /// - parameters: + /// - fileHandle: The `FileHandle` to write to. + /// - buffer: The `ByteBuffer` to write. + /// - eventLoop: The `EventLoop` to create the returned `EventLoopFuture` from. + /// - returns: An `EventLoopFuture` which is fulfilled if the write was successful or fails on error. + public func write(fileHandle: FileHandle, + buffer: ByteBuffer, + eventLoop: EventLoop) -> EventLoopFuture<()> { + var byteCount = buffer.readableBytes + + guard byteCount > 0 else { + return eventLoop.newSucceededFuture(result: ()) + } + + return self.threadPool.runIfActive(eventLoop: eventLoop) { + var buf = buffer + while byteCount > 0 { + let n = try buf.readWithUnsafeReadableBytes { ptr in + precondition(ptr.count == byteCount) + let res = try fileHandle.withUnsafeFileDescriptor { descriptor in + try Posix.write(descriptor: descriptor, + pointer: ptr.baseAddress!, + size: byteCount) + } + switch res { + case .processed(let n): + assert(n >= 0, "write claims to have written a negative number of bytes \(n)") + return n + case .wouldBlock: + throw Error.descriptorSetToNonBlocking + } + } + + byteCount -= n + } + } + } + /// Open the file at `path` on a private thread pool which is separate from any `EventLoop` thread. /// /// This function will return (a future) of the `FileHandle` associated with the file opened and a `FileRegion` diff --git a/Tests/NIOTests/NonBlockingFileIOTest+XCTest.swift b/Tests/NIOTests/NonBlockingFileIOTest+XCTest.swift index 6a4c1bf9d..a10e01a63 100644 --- a/Tests/NIOTests/NonBlockingFileIOTest+XCTest.swift +++ b/Tests/NIOTests/NonBlockingFileIOTest+XCTest.swift @@ -45,6 +45,7 @@ extension NonBlockingFileIOTest { ("testFileRegionReadFromPipeFails", testFileRegionReadFromPipeFails), ("testReadFromNonBlockingPipeFails", testReadFromNonBlockingPipeFails), ("testSeekPointerIsSetToFront", testSeekPointerIsSetToFront), + ("testWriting", testWriting), ("testFileOpenWorks", testFileOpenWorks), ("testFileOpenWorksWithEmptyFile", testFileOpenWorksWithEmptyFile), ("testFileOpenFails", testFileOpenFails), diff --git a/Tests/NIOTests/NonBlockingFileIOTest.swift b/Tests/NIOTests/NonBlockingFileIOTest.swift index 85714a840..99f2a7911 100644 --- a/Tests/NIOTests/NonBlockingFileIOTest.swift +++ b/Tests/NIOTests/NonBlockingFileIOTest.swift @@ -450,6 +450,27 @@ class NonBlockingFileIOTest: XCTestCase { XCTAssertEqual(2, numCalls) } + func testWriting() throws { + var buffer = allocator.buffer(capacity: 3) + buffer.write(staticString: "123") + + try withTemporaryFile(content: "") { (fileHandle, path) in + try self.fileIO.write(fileHandle: fileHandle, + buffer: buffer, + eventLoop: self.eventLoop).wait() + let offset = try fileHandle.withUnsafeFileDescriptor { + try Posix.lseek(descriptor: $0, offset: 0, whence: SEEK_SET) + } + XCTAssertEqual(offset, 0) + + let readBuffer = try self.fileIO.read(fileHandle: fileHandle, + byteCount: 3, + allocator: self.allocator, + eventLoop: self.eventLoop).wait() + XCTAssertEqual(readBuffer.getString(at: 0, length: 3), "123") + } + } + func testFileOpenWorks() throws { let content = "123" try withTemporaryFile(content: content) { (fileHandle, path) -> Void in