-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathIORequest.swift
More file actions
486 lines (450 loc) · 16.7 KB
/
Copy pathIORequest.swift
File metadata and controls
486 lines (450 loc) · 16.7 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#if compiler(>=6.2) && $Lifetimes
#if os(Linux)
internal import CSystem
@usableFromInline
internal enum IORequestCore {
case nop // nothing here
case openat(
atDirectory: FileDescriptor,
path: FilePath,
FileDescriptor.AccessMode,
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
context: UInt64 = 0
)
case openatSlot(
atDirectory: FileDescriptor,
path: FilePath,
FileDescriptor.AccessMode,
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
intoSlot: IORing.RegisteredFile,
context: UInt64 = 0
)
case read(
file: FileDescriptor,
buffer: IORing.RegisteredBuffer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case readUnregistered(
file: FileDescriptor,
buffer: UnsafeMutableRawBufferPointer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case readSlot(
file: IORing.RegisteredFile,
buffer: IORing.RegisteredBuffer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case readUnregisteredSlot(
file: IORing.RegisteredFile,
buffer: UnsafeMutableRawBufferPointer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case write(
file: FileDescriptor,
buffer: IORing.RegisteredBuffer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case writeUnregistered(
file: FileDescriptor,
buffer: UnsafeMutableRawBufferPointer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case writeSlot(
file: IORing.RegisteredFile,
buffer: IORing.RegisteredBuffer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case writeUnregisteredSlot(
file: IORing.RegisteredFile,
buffer: UnsafeMutableRawBufferPointer,
offset: UInt64 = 0,
context: UInt64 = 0
)
case close(
FileDescriptor,
context: UInt64 = 0
)
case closeSlot(
IORing.RegisteredFile,
context: UInt64 = 0
)
case unlinkAt(
atDirectory: FileDescriptor,
path: FilePath,
context: UInt64 = 0
)
case cancel(
flags:UInt32
)
case cancelContext(
flags: UInt32,
targetContext: UInt64
)
case cancelFD(
flags: UInt32,
targetFD: FileDescriptor
)
case cancelFDSlot(
flags: UInt32,
target: IORing.RegisteredFile
)
}
@inline(__always) @inlinable
internal func makeRawRequest_readWrite_registered(
file: FileDescriptor,
buffer: IORing.RegisteredBuffer,
offset: UInt64,
context: UInt64 = 0,
request: consuming RawIORequest
) -> RawIORequest {
request.fileDescriptor = file
request.buffer = buffer.unsafeBuffer
request.rawValue.buf_index = UInt16(exactly: buffer.index)!
request.offset = offset
request.rawValue.user_data = context
return request
}
@inline(__always) @inlinable
internal func makeRawRequest_readWrite_registered_slot(
file: IORing.RegisteredFile,
buffer: IORing.RegisteredBuffer,
offset: UInt64,
context: UInt64 = 0,
request: consuming RawIORequest
) -> RawIORequest {
request.rawValue.fd = Int32(exactly: file.index)!
request.flags = .fixedFile
request.buffer = buffer.unsafeBuffer
request.rawValue.buf_index = UInt16(exactly: buffer.index)!
request.offset = offset
request.rawValue.user_data = context
return request
}
@inline(__always) @inlinable
internal func makeRawRequest_readWrite_unregistered(
file: FileDescriptor,
buffer: UnsafeMutableRawBufferPointer,
offset: UInt64,
context: UInt64 = 0,
request: consuming RawIORequest
) -> RawIORequest {
request.fileDescriptor = file
request.buffer = buffer
request.offset = offset
request.rawValue.user_data = context
return request
}
@inline(__always) @inlinable
internal func makeRawRequest_readWrite_unregistered_slot(
file: IORing.RegisteredFile,
buffer: UnsafeMutableRawBufferPointer,
offset: UInt64,
context: UInt64 = 0,
request: consuming RawIORequest
) -> RawIORequest {
request.rawValue.fd = Int32(exactly: file.index)!
request.flags = .fixedFile
request.buffer = buffer
request.offset = offset
request.rawValue.user_data = context
return request
}
extension IORing {
public struct Request {
@usableFromInline var core: IORequestCore
@inlinable internal init(core inCore: IORequestCore) {
core = inCore
}
@inlinable internal consuming func extractCore() -> IORequestCore {
return core
}
}
}
extension IORing.Request {
@inlinable public static func nop(context: UInt64 = 0) -> IORing.Request {
.init(core: .nop)
}
@inlinable public static func read(
_ file: IORing.RegisteredFile,
into buffer: IORing.RegisteredBuffer,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .readSlot(file: file, buffer: buffer, offset: offset, context: context))
}
@inlinable public static func read(
_ file: FileDescriptor,
into buffer: IORing.RegisteredBuffer,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .read(file: file, buffer: buffer, offset: offset, context: context))
}
@inlinable public static func read(
_ file: IORing.RegisteredFile,
into buffer: UnsafeMutableRawBufferPointer,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .readUnregisteredSlot(file: file, buffer: buffer, offset: offset, context: context))
}
@inlinable public static func read(
_ file: FileDescriptor,
into buffer: UnsafeMutableRawBufferPointer,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .readUnregistered(file: file, buffer: buffer, offset: offset, context: context))
}
@inlinable public static func write(
_ buffer: IORing.RegisteredBuffer,
into file: IORing.RegisteredFile,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .writeSlot(file: file, buffer: buffer, offset: offset, context: context))
}
@inlinable public static func write(
_ buffer: IORing.RegisteredBuffer,
into file: FileDescriptor,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .write(file: file, buffer: buffer, offset: offset, context: context))
}
@inlinable public static func write(
_ buffer: UnsafeMutableRawBufferPointer,
into file: IORing.RegisteredFile,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .writeUnregisteredSlot(
file: file, buffer: buffer, offset: offset, context: context))
}
@inlinable public static func write(
_ buffer: UnsafeMutableRawBufferPointer,
into file: FileDescriptor,
at offset: UInt64 = 0,
context: UInt64 = 0
) -> IORing.Request {
.init(
core: .writeUnregistered(file: file, buffer: buffer, offset: offset, context: context)
)
}
@inlinable public static func close(
_ file: FileDescriptor,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .close(file, context: context))
}
@inlinable public static func close(
_ file: IORing.RegisteredFile,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .closeSlot(file, context: context))
}
@inlinable public static func open(
_ path: FilePath,
in directory: FileDescriptor,
into slot: IORing.RegisteredFile,
mode: FileDescriptor.AccessMode,
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
context: UInt64 = 0
) -> IORing.Request {
.init(
core: .openatSlot(
atDirectory: directory, path: path, mode, options: options,
permissions: permissions, intoSlot: slot, context: context))
}
@inlinable public static func open(
_ path: FilePath,
in directory: FileDescriptor,
mode: FileDescriptor.AccessMode,
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
context: UInt64 = 0
) -> IORing.Request {
.init(
core: .openat(
atDirectory: directory, path: path, mode, options: options,
permissions: permissions, context: context
))
}
@inlinable public static func unlink(
_ path: FilePath,
in directory: FileDescriptor,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .unlinkAt(atDirectory: directory, path: path, context: context))
}
// Cancel
/*
* ASYNC_CANCEL flags.
*
* IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key
* IORING_ASYNC_CANCEL_FD Key off 'fd' for cancellation rather than the
* request 'user_data'
* IORING_ASYNC_CANCEL_ANY Match any request
* IORING_ASYNC_CANCEL_FD_FIXED 'fd' passed in is a fixed descriptor
* IORING_ASYNC_CANCEL_USERDATA Match on user_data, default for no other key
* IORING_ASYNC_CANCEL_OP Match request based on opcode
*/
@inlinable internal static var SWIFT_IORING_ASYNC_CANCEL_ALL: UInt32 { 1 << 0 }
@inlinable internal static var SWIFT_IORING_ASYNC_CANCEL_FD: UInt32 { 1 << 1 }
@inlinable internal static var SWIFT_IORING_ASYNC_CANCEL_ANY: UInt32 { 1 << 2 }
@inlinable internal static var SWIFT_IORING_ASYNC_CANCEL_FD_FIXED: UInt32 { 1 << 3 }
@inlinable internal static var SWIFT_IORING_ASYNC_CANCEL_USERDATA: UInt32 { 1 << 4 }
@inlinable internal static var SWIFT_IORING_ASYNC_CANCEL_OP: UInt32 { 1 << 5 }
public enum CancellationMatch {
case all
case first
}
@inlinable public static func cancel(
_ matchAll: CancellationMatch,
matchingContext: UInt64
) -> IORing.Request {
switch matchAll {
case .all:
.init(core: .cancelContext(flags: SWIFT_IORING_ASYNC_CANCEL_ALL | SWIFT_IORING_ASYNC_CANCEL_USERDATA, targetContext: matchingContext))
case .first:
.init(core: .cancelContext(flags: SWIFT_IORING_ASYNC_CANCEL_USERDATA, targetContext: matchingContext))
}
}
@inlinable public static func cancel(
_ matchAll: CancellationMatch,
matching: FileDescriptor
) -> IORing.Request {
switch matchAll {
case .all:
.init(core: .cancelFD(flags: SWIFT_IORING_ASYNC_CANCEL_ALL | SWIFT_IORING_ASYNC_CANCEL_FD, targetFD: matching))
case .first:
.init(core: .cancelFD(flags: SWIFT_IORING_ASYNC_CANCEL_FD, targetFD: matching))
}
}
@inlinable public static func cancel(
_ matchAll: CancellationMatch,
matching: IORing.RegisteredFile
) -> IORing.Request {
switch matchAll {
case .all:
.init(core: .cancelFDSlot(flags: SWIFT_IORING_ASYNC_CANCEL_ALL | SWIFT_IORING_ASYNC_CANCEL_FD_FIXED, target: matching))
case .first:
.init(core: .cancelFDSlot(flags: SWIFT_IORING_ASYNC_CANCEL_FD_FIXED, target: matching))
}
}
@inlinable public static func cancel(
_ matchAll: CancellationMatch,
) -> IORing.Request {
switch matchAll {
case .all:
.init(core: .cancel(flags: SWIFT_IORING_ASYNC_CANCEL_ALL))
case .first:
.init(core: .cancel(flags: SWIFT_IORING_ASYNC_CANCEL_ANY))
}
}
@inline(__always) @inlinable
internal consuming func makeRawRequest(
pathBuffers: PendingPathBuffers
) -> RawIORequest {
var request = RawIORequest()
switch extractCore() {
case .nop:
request.operation = .nop
case .openatSlot(
let atDirectory, let path, let mode, let options, let permissions, let fileSlot,
let context):
// TODO: use rawValue less
request.operation = .openAt
request.fileDescriptor = atDirectory
request.rawValue.addr = UInt64(
UInt(bitPattern: pathBuffers.pin(path)))
request.rawValue.open_flags = UInt32(bitPattern: options.rawValue | mode.rawValue)
request.rawValue.len = permissions?.rawValue ?? 0
request.rawValue.file_index = UInt32(fileSlot.index + 1)
request.rawValue.user_data = context
case .openat(
let atDirectory, let path, let mode, let options, let permissions, let context):
request.operation = .openAt
request.fileDescriptor = atDirectory
request.rawValue.addr = UInt64(
UInt(bitPattern: pathBuffers.pin(path)))
request.rawValue.open_flags = UInt32(bitPattern: options.rawValue | mode.rawValue)
request.rawValue.len = permissions?.rawValue ?? 0
request.rawValue.user_data = context
case .write(let file, let buffer, let offset, let context):
request.operation = .writeFixed
return makeRawRequest_readWrite_registered(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .writeSlot(let file, let buffer, let offset, let context):
request.operation = .writeFixed
return makeRawRequest_readWrite_registered_slot(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .writeUnregistered(let file, let buffer, let offset, let context):
request.operation = .write
return makeRawRequest_readWrite_unregistered(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .writeUnregisteredSlot(let file, let buffer, let offset, let context):
request.operation = .write
return makeRawRequest_readWrite_unregistered_slot(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .read(let file, let buffer, let offset, let context):
request.operation = .readFixed
return makeRawRequest_readWrite_registered(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .readSlot(let file, let buffer, let offset, let context):
request.operation = .readFixed
return makeRawRequest_readWrite_registered_slot(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .readUnregistered(let file, let buffer, let offset, let context):
request.operation = .read
return makeRawRequest_readWrite_unregistered(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .readUnregisteredSlot(let file, let buffer, let offset, let context):
request.operation = .read
return makeRawRequest_readWrite_unregistered_slot(
file: file, buffer: buffer, offset: offset, context: context, request: request)
case .close(let file, let context):
request.operation = .close
request.fileDescriptor = file
request.rawValue.user_data = context
case .closeSlot(let file, let context):
request.operation = .close
request.rawValue.file_index = UInt32(file.index + 1)
request.rawValue.user_data = context
case .unlinkAt(let atDirectory, let path, let context):
request.operation = .unlinkAt
request.fileDescriptor = atDirectory
request.rawValue.addr = UInt64(
UInt(bitPattern: pathBuffers.pin(path)))
request.rawValue.user_data = context
case .cancelContext(let flags, let targetContext):
request.operation = .asyncCancel
request.cancel_flags = flags
request.addr = targetContext
case .cancelFD(let flags, let targetFD):
request.operation = .asyncCancel
request.cancel_flags = flags
request.fileDescriptor = targetFD
case .cancelFDSlot(let flags, let target):
request.operation = .asyncCancel
request.cancel_flags = flags
request.rawValue.fd = Int32(target.index)
case .cancel(let flags):
request.operation = .asyncCancel
request.cancel_flags = flags
}
return request
}
}
#endif // os(Linux)
#endif // compiler(>=6.2) && $Lifetimes