Skip to content

Commit 72dbe2b

Browse files
committed
fix comments
1 parent 8bf43f2 commit 72dbe2b

File tree

5 files changed

+100
-87
lines changed

5 files changed

+100
-87
lines changed

Sources/Containerization/SandboxContext/SandboxContext.grpc.swift

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public protocol Com_Apple_Containerization_Sandbox_V3_SandboxContextClientProtoc
165165
) -> UnaryCall<Com_Apple_Containerization_Sandbox_V3_KillRequest, Com_Apple_Containerization_Sandbox_V3_KillResponse>
166166

167167
func notifyFileSystemEvent(
168-
_ request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest,
169-
callOptions: CallOptions?
170-
) -> UnaryCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>
168+
callOptions: CallOptions?,
169+
handler: @escaping (Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse) -> Void
170+
) -> BidirectionalStreamingCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>
171171
}
172172

173173
extension Com_Apple_Containerization_Sandbox_V3_SandboxContextClientProtocol {
@@ -646,19 +646,22 @@ extension Com_Apple_Containerization_Sandbox_V3_SandboxContextClientProtocol {
646646

647647
/// Notify guest of filesystem events from host.
648648
///
649+
/// Callers should use the `send` method on the returned object to send messages
650+
/// to the server. The caller should send an `.end` after the final message has been sent.
651+
///
649652
/// - Parameters:
650-
/// - request: Request to send to NotifyFileSystemEvent.
651653
/// - callOptions: Call options.
652-
/// - Returns: A `UnaryCall` with futures for the metadata, status and response.
654+
/// - handler: A closure called when each response is received from the server.
655+
/// - Returns: A `ClientStreamingCall` with futures for the metadata and status.
653656
public func notifyFileSystemEvent(
654-
_ request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest,
655-
callOptions: CallOptions? = nil
656-
) -> UnaryCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse> {
657-
return self.makeUnaryCall(
657+
callOptions: CallOptions? = nil,
658+
handler: @escaping (Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse) -> Void
659+
) -> BidirectionalStreamingCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse> {
660+
return self.makeBidirectionalStreamingCall(
658661
path: Com_Apple_Containerization_Sandbox_V3_SandboxContextClientMetadata.Methods.notifyFileSystemEvent.path,
659-
request: request,
660662
callOptions: callOptions ?? self.defaultCallOptions,
661-
interceptors: self.interceptors?.makeNotifyFileSystemEventInterceptors() ?? []
663+
interceptors: self.interceptors?.makeNotifyFileSystemEventInterceptors() ?? [],
664+
handler: handler
662665
)
663666
}
664667
}
@@ -857,9 +860,8 @@ public protocol Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncClientP
857860
) -> GRPCAsyncUnaryCall<Com_Apple_Containerization_Sandbox_V3_KillRequest, Com_Apple_Containerization_Sandbox_V3_KillResponse>
858861

859862
func makeNotifyFileSystemEventCall(
860-
_ request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest,
861863
callOptions: CallOptions?
862-
) -> GRPCAsyncUnaryCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>
864+
) -> GRPCAsyncBidirectionalStreamingCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>
863865
}
864866

865867
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
@@ -1185,12 +1187,10 @@ extension Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncClientProtoco
11851187
}
11861188

11871189
public func makeNotifyFileSystemEventCall(
1188-
_ request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest,
11891190
callOptions: CallOptions? = nil
1190-
) -> GRPCAsyncUnaryCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse> {
1191-
return self.makeAsyncUnaryCall(
1191+
) -> GRPCAsyncBidirectionalStreamingCall<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse> {
1192+
return self.makeAsyncBidirectionalStreamingCall(
11921193
path: Com_Apple_Containerization_Sandbox_V3_SandboxContextClientMetadata.Methods.notifyFileSystemEvent.path,
1193-
request: request,
11941194
callOptions: callOptions ?? self.defaultCallOptions,
11951195
interceptors: self.interceptors?.makeNotifyFileSystemEventInterceptors() ?? []
11961196
)
@@ -1511,13 +1511,25 @@ extension Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncClientProtoco
15111511
)
15121512
}
15131513

1514-
public func notifyFileSystemEvent(
1515-
_ request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest,
1514+
public func notifyFileSystemEvent<RequestStream>(
1515+
_ requests: RequestStream,
15161516
callOptions: CallOptions? = nil
1517-
) async throws -> Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse {
1518-
return try await self.performAsyncUnaryCall(
1517+
) -> GRPCAsyncResponseStream<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse> where RequestStream: Sequence, RequestStream.Element == Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest {
1518+
return self.performAsyncBidirectionalStreamingCall(
15191519
path: Com_Apple_Containerization_Sandbox_V3_SandboxContextClientMetadata.Methods.notifyFileSystemEvent.path,
1520-
request: request,
1520+
requests: requests,
1521+
callOptions: callOptions ?? self.defaultCallOptions,
1522+
interceptors: self.interceptors?.makeNotifyFileSystemEventInterceptors() ?? []
1523+
)
1524+
}
1525+
1526+
public func notifyFileSystemEvent<RequestStream>(
1527+
_ requests: RequestStream,
1528+
callOptions: CallOptions? = nil
1529+
) -> GRPCAsyncResponseStream<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse> where RequestStream: AsyncSequence & Sendable, RequestStream.Element == Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest {
1530+
return self.performAsyncBidirectionalStreamingCall(
1531+
path: Com_Apple_Containerization_Sandbox_V3_SandboxContextClientMetadata.Methods.notifyFileSystemEvent.path,
1532+
requests: requests,
15211533
callOptions: callOptions ?? self.defaultCallOptions,
15221534
interceptors: self.interceptors?.makeNotifyFileSystemEventInterceptors() ?? []
15231535
)
@@ -1820,7 +1832,7 @@ public enum Com_Apple_Containerization_Sandbox_V3_SandboxContextClientMetadata {
18201832
public static let notifyFileSystemEvent = GRPCMethodDescriptor(
18211833
name: "NotifyFileSystemEvent",
18221834
path: "/com.apple.containerization.sandbox.v3.SandboxContext/NotifyFileSystemEvent",
1823-
type: GRPCCallType.unary
1835+
type: GRPCCallType.bidirectionalStreaming
18241836
)
18251837
}
18261838
}
@@ -1911,7 +1923,7 @@ public protocol Com_Apple_Containerization_Sandbox_V3_SandboxContextProvider: Ca
19111923
func kill(request: Com_Apple_Containerization_Sandbox_V3_KillRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Com_Apple_Containerization_Sandbox_V3_KillResponse>
19121924

19131925
/// Notify guest of filesystem events from host.
1914-
func notifyFileSystemEvent(request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>
1926+
func notifyFileSystemEvent(context: StreamingResponseCallContext<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>) -> EventLoopFuture<(StreamEvent<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest>) -> Void>
19151927
}
19161928

19171929
extension Com_Apple_Containerization_Sandbox_V3_SandboxContextProvider {
@@ -2161,12 +2173,12 @@ extension Com_Apple_Containerization_Sandbox_V3_SandboxContextProvider {
21612173
)
21622174

21632175
case "NotifyFileSystemEvent":
2164-
return UnaryServerHandler(
2176+
return BidirectionalStreamingServerHandler(
21652177
context: context,
21662178
requestDeserializer: ProtobufDeserializer<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest>(),
21672179
responseSerializer: ProtobufSerializer<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>(),
21682180
interceptors: self.interceptors?.makeNotifyFileSystemEventInterceptors() ?? [],
2169-
userFunction: self.notifyFileSystemEvent(request:context:)
2181+
observerFactory: self.notifyFileSystemEvent(context:)
21702182
)
21712183

21722184
default:
@@ -2342,9 +2354,10 @@ public protocol Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvide
23422354

23432355
/// Notify guest of filesystem events from host.
23442356
func notifyFileSystemEvent(
2345-
request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest,
2357+
requestStream: GRPCAsyncRequestStream<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest>,
2358+
responseStream: GRPCAsyncResponseStreamWriter<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>,
23462359
context: GRPCAsyncServerCallContext
2347-
) async throws -> Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse
2360+
) async throws
23482361
}
23492362

23502363
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
@@ -2606,7 +2619,7 @@ extension Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvider {
26062619
requestDeserializer: ProtobufDeserializer<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest>(),
26072620
responseSerializer: ProtobufSerializer<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>(),
26082621
interceptors: self.interceptors?.makeNotifyFileSystemEventInterceptors() ?? [],
2609-
wrapping: { try await self.notifyFileSystemEvent(request: $0, context: $1) }
2622+
wrapping: { try await self.notifyFileSystemEvent(requestStream: $0, responseStream: $1, context: $2) }
26102623
)
26112624

26122625
default:
@@ -2921,7 +2934,7 @@ public enum Com_Apple_Containerization_Sandbox_V3_SandboxContextServerMetadata {
29212934
public static let notifyFileSystemEvent = GRPCMethodDescriptor(
29222935
name: "NotifyFileSystemEvent",
29232936
path: "/com.apple.containerization.sandbox.v3.SandboxContext/NotifyFileSystemEvent",
2924-
type: GRPCCallType.unary
2937+
type: GRPCCallType.bidirectionalStreaming
29252938
)
29262939
}
29272940
}

Sources/Containerization/SandboxContext/SandboxContext.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ service SandboxContext {
6363
// Send a signal to a process via the PID.
6464
rpc Kill(KillRequest) returns (KillResponse);
6565
// Notify guest of filesystem events from host.
66-
rpc NotifyFileSystemEvent(NotifyFileSystemEventRequest) returns (NotifyFileSystemEventResponse);
66+
rpc NotifyFileSystemEvent(stream NotifyFileSystemEventRequest) returns (stream NotifyFileSystemEventResponse);
6767
}
6868

6969
message Stdio {

Sources/Containerization/Vminitd.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,19 @@ extension Vminitd {
376376
$0.path = path
377377
$0.eventType = eventType
378378
}
379-
return try await client.notifyFileSystemEvent(request)
379+
380+
let requests = AsyncStream<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest> { continuation in
381+
continuation.yield(request)
382+
continuation.finish()
383+
}
384+
385+
let responses = client.notifyFileSystemEvent(requests)
386+
387+
for try await response in responses {
388+
return response
389+
}
390+
391+
throw ContainerizationError(.internalError, message: "No response received from notifyFileSystemEvent")
380392
}
381393
}
382394

Sources/Integration/VMTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,24 +368,24 @@ extension IntegrationSuite {
368368
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
369369
let agent = Vminitd(connection: connection, group: group)
370370

371-
// Test 1: CREATE event
372-
print("Testing CREATE event...")
373-
let createResponse = try await agent.notifyFileSystemEvent(path: "/mnt/test/new_file.txt", eventType: .create)
371+
// Test 1: CREATE event on existing file
372+
print("Testing CREATE event on existing file...")
373+
let createResponse = try await agent.notifyFileSystemEvent(path: "/mnt/test/existing.txt", eventType: .create)
374374
guard createResponse.success else {
375375
throw IntegrationError.assert(msg: "CREATE event failed: \(createResponse.error)")
376376
}
377377

378-
// Verify file was created in guest
378+
// Verify CREATE event triggered inotify (file should still exist)
379379
let buffer1 = BufferWriter()
380380
let process1 = try await container.exec("check-create") { config in
381-
config.arguments = ["/bin/ls", "-la", "/mnt/test/new_file.txt"]
381+
config.arguments = ["/bin/stat", "/mnt/test/existing.txt"]
382382
config.stdout = buffer1
383383
}
384384

385385
try await process1.start()
386386
let status1 = try await process1.wait()
387387
guard status1 == 0 else {
388-
throw IntegrationError.assert(msg: "File creation verification failed")
388+
throw IntegrationError.assert(msg: "File stat verification failed for CREATE event")
389389
}
390390

391391
// Test 2: MODIFY event on existing file

vminitd/Sources/vminitd/Server+GRPC.swift

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -945,35 +945,41 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
945945
}
946946

947947
func notifyFileSystemEvent(
948-
request: Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest,
948+
requestStream: GRPCAsyncRequestStream<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventRequest>,
949+
responseStream: GRPCAsyncResponseStreamWriter<Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse>,
949950
context: GRPCAsyncServerCallContext
950-
) async throws -> Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse {
951-
log.debug(
952-
"notifyFileSystemEvent",
953-
metadata: [
954-
"path": "\(request.path)",
955-
"eventType": "\(request.eventType)",
956-
])
957-
958-
do {
959-
try await generateSyntheticInotifyEvent(
960-
path: request.path,
961-
eventType: request.eventType
962-
)
963-
964-
return .with {
965-
$0.success = true
966-
}
967-
} catch {
968-
log.error(
951+
) async throws {
952+
for try await request in requestStream {
953+
log.debug(
969954
"notifyFileSystemEvent",
970955
metadata: [
971-
"error": "\(error)"
956+
"path": "\(request.path)",
957+
"eventType": "\(request.eventType)",
972958
])
973959

974-
return .with {
975-
$0.success = false
976-
$0.error = error.localizedDescription
960+
do {
961+
try await generateSyntheticInotifyEvent(
962+
path: request.path,
963+
eventType: request.eventType
964+
)
965+
966+
let response = Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse.with {
967+
$0.success = true
968+
}
969+
try await responseStream.send(response)
970+
971+
} catch {
972+
log.error(
973+
"notifyFileSystemEvent",
974+
metadata: [
975+
"error": "\(error)"
976+
])
977+
978+
let response = Com_Apple_Containerization_Sandbox_V3_NotifyFileSystemEventResponse.with {
979+
$0.success = false
980+
$0.error = error.localizedDescription
981+
}
982+
try await responseStream.send(response)
977983
}
978984
}
979985
}
@@ -982,32 +988,14 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
982988
path: String,
983989
eventType: Com_Apple_Containerization_Sandbox_V3_FileSystemEventType
984990
) async throws {
985-
switch eventType {
986-
case .modify:
987-
// Touch file to update timestamp -> generates IN_ATTRIB event
988-
let now = Date()
989-
try FileManager.default.setAttributes(
990-
[.modificationDate: now],
991-
ofItemAtPath: path
992-
)
993-
994-
case .create:
995-
// Use chmod with same permissions to generate IN_ATTRIB event
996-
let attributes = try FileManager.default.attributesOfItem(atPath: path)
997-
let permissions = attributes[.posixPermissions] as? NSNumber ?? NSNumber(value: 0o644)
998-
try FileManager.default.setAttributes(
999-
[.posixPermissions: permissions],
1000-
ofItemAtPath: path
1001-
)
1002-
1003-
case .delete:
1004-
// We can't generate delete events for files that don't exist
1005-
// This would need to be handled by the application layer
1006-
log.warning("Delete events cannot be synthesized for existing files")
1007-
1008-
default:
1009-
log.warning("Unsupported filesystem event type: \(eventType)")
991+
let attributes = try FileManager.default.attributesOfItem(atPath: path)
992+
guard let permissions = attributes[.posixPermissions] as? NSNumber else {
993+
throw GRPCStatus(code: .internalError, message: "Failed to get file permissions for path: \(path)")
1010994
}
995+
try FileManager.default.setAttributes(
996+
[.posixPermissions: permissions],
997+
ofItemAtPath: path
998+
)
1011999
}
10121000
}
10131001

0 commit comments

Comments
 (0)