Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Sources/SWBUtil/FSProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,9 @@ class LocalFS: FSProxy, @unchecked Sendable {

func write(_ path: Path, contents: (FileDescriptor) async throws -> Void) async throws {
let fd = try FileDescriptor.open(FilePath(path.str), .writeOnly, options: [.create, .truncate], permissions: [.ownerReadWrite, .groupRead, .otherRead])
do {
return try await fd.closeAfter {
try await contents(fd)
} catch {
_ = try? fd.close()
throw error
}
try fd.close()
}

func append(_ path: Path, contents: ByteString) throws {
Expand Down
28 changes: 15 additions & 13 deletions Tests/SWBUtilTests/FileHandleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,30 @@ import SystemPackage
// Read while triggering an I/O error
do {
let fd = try Path.root.str.withPlatformString { try FileDescriptor.open($0, .readOnly) }
let fh = FileHandle(fileDescriptor: fd.rawValue, closeOnDealloc: false)
try await fd.closeAfter {
let fh = FileHandle(fileDescriptor: fd.rawValue, closeOnDealloc: false)

if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
var it = fh.bytes().makeAsyncIterator()
try fd.close()
if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
var it = fh.bytes().makeAsyncIterator()

await #expect(throws: (any Error).self) {
while let _ = try await it.next() {
// Can't read a directory
await #expect(throws: (any Error).self) {
while let _ = try await it.next() {
}
}
}
} else {
var it = fh._bytes().makeAsyncIterator()
try fd.close()
} else {
var it = fh._bytes().makeAsyncIterator()

await #expect(throws: (any Error).self) {
while let _ = try await it.next() {
// Can't read a directory
await #expect(throws: (any Error).self) {
while let _ = try await it.next() {
}
}
}
}
}

// Read part of a file, then close the handle
// Read part of a file, then cancel the task
do {
let condition = CancellableWaitCondition()

Expand Down
Loading