Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Changed
- Decouple FSKit item IDs from backend entry IDs.
- Refactor bridge socket I/O to `async/await`.

### Fixed
- Replace HFS epoch (1904-01-01) sentinel timestamps with `timeNow`.

Expand Down
67 changes: 44 additions & 23 deletions FSKitExt/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ final class Bridge: FSUnaryFileSystem, FSUnaryFileSystemOperations {
replyHandler: @escaping (FSProbeResult?, (any Error)?) -> Void
) {
log.d("probeResource")
do {
let response = try socket.send(
content: .getResourceIdentifier(Pb_GetResourceIdentifier())
)
if case .resourceIdentifier(let value) = response {
Task {
do {
let response = try await socket.send(
content: .getResourceIdentifier(Pb_GetResourceIdentifier())
)
guard case .resourceIdentifier(let value) = response else {
throw BackendError.unexpectedResponse(
operation: "probeResource"
)
}

replyHandler(
FSProbeResult.usable(
name: value.name,
Expand All @@ -39,14 +45,13 @@ final class Bridge: FSUnaryFileSystem, FSUnaryFileSystemOperations {
),
nil
)
return
} catch {
log.e(
"probeResource: failure (error = \(error.localizedDescription))"
)
replyHandler(nil, error)
}
} catch {
log.e(
"probeResource: failure (error = \(error.localizedDescription))"
)
}
replyHandler(nil, nil)
}

func loadResource(
Expand All @@ -55,23 +60,28 @@ final class Bridge: FSUnaryFileSystem, FSUnaryFileSystemOperations {
replyHandler: @escaping (FSVolume?, (any Error)?) -> Void
) {
log.d("loadResource")
do {
let response = try socket.send(
content: .getVolumeIdentifier(Pb_GetVolumeIdentifier())
)
if case .volumeIdentifier(let value) = response {
Task {
do {
let response = try await socket.send(
content: .getVolumeIdentifier(Pb_GetVolumeIdentifier())
)
guard case .volumeIdentifier(let value) = response else {
throw BackendError.unexpectedResponse(
operation: "loadResource"
)
}

let volume = Volume(value)
volume.load()
try await volume.load()
containerStatus = .ready
replyHandler(volume, nil)
return
} catch {
log.e(
"loadResource: failure (error = \(error.localizedDescription))"
)
replyHandler(nil, error)
}
} catch {
log.e(
"loadResource: failure (error = \(error.localizedDescription))"
)
}
replyHandler(nil, nil)
}

func unloadResource(
Expand All @@ -87,3 +97,14 @@ final class Bridge: FSUnaryFileSystem, FSUnaryFileSystemOperations {
log.d("didFinishLoading")
}
}

enum BackendError: LocalizedError {
case unexpectedResponse(operation: String)

var errorDescription: String? {
switch self {
case .unexpectedResponse(let operation):
return "Unexpected backend response during \(operation)."
}
}
}
8 changes: 8 additions & 0 deletions FSKitExt/FSKitExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ extension Logger {
self.e("\(function): failure (code = \(code))")
}
}

extension NSLock {
func withLock<T>(_ body: () throws -> T) rethrows -> T {
lock()
defer { unlock() }
return try body()
}
}
Loading