-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBridge.swift
More file actions
110 lines (97 loc) · 3.16 KB
/
Copy pathBridge.swift
File metadata and controls
110 lines (97 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import FSKit
import Foundation
import os
final class Bridge: FSUnaryFileSystem, FSUnaryFileSystemOperations {
static let shared = Bridge()
private let log = Logger(subsystem: "FSKitExt", category: "Bridge")
private let socket = Socket.shared
private override init() {
super.init()
do {
let port = try Bundle.main.getServerPort()
socket.initialize(host: "localhost", port: port)
} catch {
log.e("Failed to configure socket: \(error.localizedDescription)")
}
}
func probeResource(
resource: FSResource,
replyHandler: @escaping (FSProbeResult?, (any Error)?) -> Void
) {
log.d("probeResource")
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,
containerID: FSContainerIdentifier(
uuid: UUID(uuidString: value.containerID) ?? UUID()
)
),
nil
)
} catch {
log.e(
"probeResource: failure (error = \(error.localizedDescription))"
)
replyHandler(nil, error)
}
}
}
func loadResource(
resource: FSResource,
options: FSTaskOptions,
replyHandler: @escaping (FSVolume?, (any Error)?) -> Void
) {
log.d("loadResource")
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)
try await volume.load()
containerStatus = .ready
replyHandler(volume, nil)
} catch {
log.e(
"loadResource: failure (error = \(error.localizedDescription))"
)
replyHandler(nil, error)
}
}
}
func unloadResource(
resource: FSResource,
options: FSTaskOptions,
replyHandler reply: @escaping ((any Error)?) -> Void
) {
log.d("unloadResource")
reply(nil)
}
func didFinishLoading() {
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)."
}
}
}