File: FileDescriptor.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (475 lines) | stat: -rw-r--r-- 17,118 bytes parent folder | download
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
/*
 This source file is part of the Swift System open source project

 Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
 Licensed under Apache License v2.0 with Runtime Library Exception

 See https://swift.org/LICENSE.txt for license information
*/

/// An abstract handle to an input or output data resource,
/// such as a file or a socket.
///
/// You are responsible for managing the lifetime and validity
/// of `FileDescriptor` values,
/// in the same way as you manage a raw C file handle.
@frozen
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
public struct FileDescriptor: RawRepresentable, Hashable, Codable {
  /// The raw C file handle.
  @_alwaysEmitIntoClient
  public let rawValue: CInt

  /// Creates a strongly-typed file handle from a raw C file handle.
  @_alwaysEmitIntoClient
  public init(rawValue: CInt) { self.rawValue = rawValue }
}

// Standard file descriptors
extension FileDescriptor {
  /// The standard input file descriptor, with a numeric value of 0.
  @_alwaysEmitIntoClient
  public static var standardInput: FileDescriptor { .init(rawValue: 0) }

  /// The standard output file descriptor, with a numeric value of 1.
  @_alwaysEmitIntoClient
  public static var standardOutput: FileDescriptor { .init(rawValue: 1) }

  /// The standard error file descriptor, with a numeric value of 2.
  @_alwaysEmitIntoClient
  public static var standardError: FileDescriptor { .init(rawValue: 2) }
}

/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension FileDescriptor {
  /// The desired read and write access for a newly opened file.
  @frozen
  public struct AccessMode: RawRepresentable, Hashable, Codable {
    /// The raw C access mode.
    @_alwaysEmitIntoClient
    public var rawValue: CInt

    /// Creates a strongly-typed access mode from a raw C access mode.
    @_alwaysEmitIntoClient
    public init(rawValue: CInt) { self.rawValue = rawValue }

    /// Opens the file for reading only.
    ///
    /// The corresponding C constant is `O_RDONLY`.
    @_alwaysEmitIntoClient
    public static var readOnly: AccessMode { AccessMode(rawValue: _O_RDONLY) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "readOnly")
    public static var O_RDONLY: AccessMode { readOnly }

    /// Opens the file for writing only.
    ///
    /// The corresponding C constant is `O_WRONLY`.
    @_alwaysEmitIntoClient
    public static var writeOnly: AccessMode { AccessMode(rawValue: _O_WRONLY) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "writeOnly")
    public static var O_WRONLY: AccessMode { writeOnly }

    /// Opens the file for reading and writing.
    ///
    /// The corresponding C constant is `O_RDWR`.
    @_alwaysEmitIntoClient
    public static var readWrite: AccessMode { AccessMode(rawValue: _O_RDWR) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "readWrite")
    public static var O_RDWR: AccessMode { readWrite }
  }

  /// Options that specify behavior for a newly-opened file.
  @frozen
  public struct OpenOptions: OptionSet, Hashable, Codable {
    /// The raw C options.
    @_alwaysEmitIntoClient
    public var rawValue: CInt

    /// Create a strongly-typed options value from raw C options.
    @_alwaysEmitIntoClient
    public init(rawValue: CInt) { self.rawValue = rawValue }

    @_alwaysEmitIntoClient
    private init(_ raw: CInt) { self.init(rawValue: raw) }

#if !os(Windows)
    /// Indicates that opening the file doesn't
    /// wait for the file or device to become available.
    ///
    /// If this option is specified,
    /// the system doesn't wait for the device or file
    /// to be ready or available.
    /// If the
    /// <doc:FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-2266j>
    /// call would result in the process being blocked for some reason,
    /// that method returns immediately.
    /// This flag also has the effect of making all
    /// subsequent input and output operations on the open file nonblocking.
    ///
    /// The corresponding C constant is `O_NONBLOCK`.
    @_alwaysEmitIntoClient
    public static var nonBlocking: OpenOptions { OpenOptions(_O_NONBLOCK) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "nonBlocking")
    public static var O_NONBLOCK: OpenOptions { nonBlocking }
#endif

    /// Indicates that each write operation appends to the file.
    ///
    /// If this option is specified,
    /// each time you write to the file,
    /// the new data is written at the end of the file,
    /// after all existing file data.
    ///
    /// The corresponding C constant is `O_APPEND`.
    @_alwaysEmitIntoClient
    public static var append: OpenOptions { OpenOptions(_O_APPEND) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "append")
    public static var O_APPEND: OpenOptions { append }

    /// Indicates that opening the file creates the file if it doesn't exist.
    ///
    /// The corresponding C constant is `O_CREAT`.
    @_alwaysEmitIntoClient
    public static var create: OpenOptions { OpenOptions(_O_CREAT) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "create")
    public static var O_CREAT: OpenOptions { create }

    /// Indicates that opening the file truncates the file if it exists.
    ///
    /// If this option is specified and the file exists,
    /// the file is truncated to zero bytes
    /// before any other operations are performed.
    ///
    /// The corresponding C constant is `O_TRUNC`.
    @_alwaysEmitIntoClient
    public static var truncate: OpenOptions { OpenOptions(_O_TRUNC) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "truncate")
    public static var O_TRUNC: OpenOptions { truncate }

    /// Indicates that opening the file creates the file,
    /// expecting that it doesn't exist.
    ///
    /// If this option and ``create`` are both specified and the file exists,
    /// <doc:FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-2266j>
    /// returns an error instead of creating the file.
    /// You can use this, for example,
    /// to implement a simple exclusive-access locking mechanism.
    ///
    /// If this option and ``create`` are both specified
    /// and the last component of the file's path is a symbolic link,
    /// <doc:FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-2266j>
    /// fails even if the symbolic link points to a nonexistent name.
    ///
    /// The corresponding C constant is `O_EXCL`.
    @_alwaysEmitIntoClient
    public static var exclusiveCreate: OpenOptions { OpenOptions(_O_EXCL) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "exclusiveCreate")
    public static var O_EXCL: OpenOptions { exclusiveCreate }

#if SYSTEM_PACKAGE_DARWIN
    /// Indicates that opening the file
    /// atomically obtains a shared lock on the file.
    ///
    /// Setting this option or the ``exclusiveLock`` option
    /// obtains a lock with `flock(2)` semantics.
    /// If you're creating a file using the ``create`` option,
    /// the request for the lock always succeeds
    /// except on file systems that don't support locking.
    ///
    /// The corresponding C constant is `O_SHLOCK`.
    @_alwaysEmitIntoClient
    public static var sharedLock: OpenOptions { OpenOptions(_O_SHLOCK) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "sharedLock")
    public static var O_SHLOCK: OpenOptions { sharedLock }

    /// Indicates that opening the file
    /// atomically obtains an exclusive lock.
    ///
    /// Setting this option or the ``sharedLock`` option.
    /// obtains a lock with `flock(2)` semantics.
    /// If you're creating a file using the ``create`` option,
    /// the request for the lock always succeeds
    /// except on file systems that don't support locking.
    ///
    /// The corresponding C constant is `O_EXLOCK`.
    @_alwaysEmitIntoClient
    public static var exclusiveLock: OpenOptions { OpenOptions(_O_EXLOCK) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "exclusiveLock")
    public static var O_EXLOCK: OpenOptions { exclusiveLock }
#endif

#if !os(Windows)
    /// Indicates that opening the file doesn't follow symlinks.
    ///
    /// If you specify this option
    /// and the file path you pass to
    /// <doc:FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-2266j>
    /// is a symbolic link,
    /// then that open operation fails.
    ///
    /// The corresponding C constant is `O_NOFOLLOW`.
    @_alwaysEmitIntoClient
    public static var noFollow: OpenOptions { OpenOptions(_O_NOFOLLOW) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "noFollow")
    public static var O_NOFOLLOW: OpenOptions { noFollow }

    /// Indicates that opening the file only succeeds if the file is a directory.
    ///
    /// If you specify this option and the file path you pass to
    /// <doc:FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-2266j>
    /// is a not a directory, then that open operation fails.
    ///
    /// The corresponding C constant is `O_DIRECTORY`.
    @_alwaysEmitIntoClient
    public static var directory: OpenOptions { OpenOptions(_O_DIRECTORY) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "directory")
    public static var O_DIRECTORY: OpenOptions { directory }
#endif

#if SYSTEM_PACKAGE_DARWIN
    /// Indicates that opening the file
    /// opens symbolic links instead of following them.
    ///
    /// If you specify this option
    /// and the file path you pass to
    /// <doc:FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-2266j>
    /// is a symbolic link,
    /// then the link itself is opened instead of what it links to.
    ///
    /// The corresponding C constant is `O_SYMLINK`.
    @_alwaysEmitIntoClient
    public static var symlink: OpenOptions { OpenOptions(_O_SYMLINK) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "symlink")
    public static var O_SYMLINK: OpenOptions { symlink }

    /// Indicates that opening the file monitors a file for changes.
    ///
    /// Specify this option when opening a file for event notifications,
    /// such as a file handle returned by the `kqueue(2)` function,
    /// rather than for reading or writing.
    /// Files opened with this option
    /// don't prevent their containing volume from being unmounted.
    ///
    /// The corresponding C constant is `O_EVTONLY`.
    @_alwaysEmitIntoClient
    public static var eventOnly: OpenOptions { OpenOptions(_O_EVTONLY) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "eventOnly")
    public static var O_EVTONLY: OpenOptions { eventOnly }
#endif

#if !os(Windows)
    /// Indicates that executing a program closes the file.
    ///
    /// Normally, file descriptors remain open
    /// across calls to the `exec(2)` family of functions.
    /// If you specify this option,
    /// the file descriptor is closed when replacing this process
    /// with another process.
    ///
    /// The state of the file
    /// descriptor flags can be inspected using `F_GETFD`,
    /// as described in the `fcntl(2)` man page.
    ///
    /// The corresponding C constant is `O_CLOEXEC`.
    @_alwaysEmitIntoClient
    public static var closeOnExec: OpenOptions { OpenOptions(_O_CLOEXEC) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "closeOnExec")
    public static var O_CLOEXEC: OpenOptions { closeOnExec }
#endif
  }

  /// Options for specifying what a file descriptor's offset is relative to.
  @frozen
  public struct SeekOrigin: RawRepresentable, Hashable, Codable {
    /// The raw C value.
    @_alwaysEmitIntoClient
    public var rawValue: CInt

    /// Create a strongly-typed seek origin from a raw C value.
    @_alwaysEmitIntoClient
    public init(rawValue: CInt) { self.rawValue = rawValue }

    /// Indicates that the offset should be set to the specified value.
    ///
    /// The corresponding C constant is `SEEK_SET`.
    @_alwaysEmitIntoClient
    public static var start: SeekOrigin { SeekOrigin(rawValue: _SEEK_SET) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "start")
    public static var SEEK_SET: SeekOrigin { start }

    /// Indicates that the offset should be set
    /// to the specified number of bytes after the current location.
    ///
    /// The corresponding C constant is `SEEK_CUR`.
    @_alwaysEmitIntoClient
    public static var current: SeekOrigin { SeekOrigin(rawValue: _SEEK_CUR) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "current")
    public static var SEEK_CUR: SeekOrigin { current }

    /// Indicates that the offset should be set
    /// to the size of the file plus the specified number of bytes.
    ///
    /// The corresponding C constant is `SEEK_END`.
    @_alwaysEmitIntoClient
    public static var end: SeekOrigin { SeekOrigin(rawValue: _SEEK_END) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "end")
    public static var SEEK_END: SeekOrigin { end }

// TODO: These are available on some versions of Linux with appropriate
// macro defines.
#if SYSTEM_PACKAGE_DARWIN
    /// Indicates that the offset should be set
    /// to the next hole after the specified number of bytes.
    ///
    /// For information about what is considered a hole,
    /// see the `lseek(2)` man page.
    ///
    /// The corresponding C constant is `SEEK_HOLE`.
    @_alwaysEmitIntoClient
    public static var nextHole: SeekOrigin { SeekOrigin(rawValue: _SEEK_HOLE) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "nextHole")
    public static var SEEK_HOLE: SeekOrigin { nextHole }

    /// Indicates that the offset should be set
    /// to the start of the next file region
    /// that isn't a hole
    /// and is greater than or equal to the supplied offset.
    ///
    /// The corresponding C constant is `SEEK_DATA`.
    @_alwaysEmitIntoClient
    public static var nextData: SeekOrigin { SeekOrigin(rawValue: _SEEK_DATA) }

    @_alwaysEmitIntoClient
    @available(*, unavailable, renamed: "nextData")
    public static var SEEK_DATA: SeekOrigin { nextData }
#endif

  }
}

/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension FileDescriptor.AccessMode
  : CustomStringConvertible, CustomDebugStringConvertible
{
  /// A textual representation of the access mode.
  @inline(never)
  public var description: String {
    switch self {
    case .readOnly: return "readOnly"
    case .writeOnly: return "writeOnly"
    case .readWrite: return "readWrite"
    default: return "\(Self.self)(rawValue: \(self.rawValue))"
    }
  }

  /// A textual representation of the access mode, suitable for debugging
  public var debugDescription: String { self.description }
}

/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension FileDescriptor.SeekOrigin
  : CustomStringConvertible, CustomDebugStringConvertible
{
  /// A textual representation of the seek origin.
  @inline(never)
  public var description: String {
    switch self {
    case .start: return "start"
    case .current: return "current"
    case .end: return "end"
#if SYSTEM_PACKAGE_DARWIN
    case .nextHole: return "nextHole"
    case .nextData: return "nextData"
#endif
    default: return "\(Self.self)(rawValue: \(self.rawValue))"
    }
  }

  /// A textual representation of the seek origin, suitable for debugging.
  public var debugDescription: String { self.description }
}

/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
extension FileDescriptor.OpenOptions
  : CustomStringConvertible, CustomDebugStringConvertible
{
  /// A textual representation of the open options.
  @inline(never)
  public var description: String {
#if SYSTEM_PACKAGE_DARWIN
    let descriptions: [(Element, StaticString)] = [
      (.nonBlocking, ".nonBlocking"),
      (.append, ".append"),
      (.create, ".create"),
      (.truncate, ".truncate"),
      (.exclusiveCreate, ".exclusiveCreate"),
      (.sharedLock, ".sharedLock"),
      (.exclusiveLock, ".exclusiveLock"),
      (.noFollow, ".noFollow"),
      (.symlink, ".symlink"),
      (.eventOnly, ".eventOnly"),
      (.closeOnExec, ".closeOnExec")
    ]
#elseif os(Windows)
    let descriptions: [(Element, StaticString)] = [
      (.append, ".append"),
      (.create, ".create"),
      (.truncate, ".truncate"),
      (.exclusiveCreate, ".exclusiveCreate"),
    ]
#else
    let descriptions: [(Element, StaticString)] = [
      (.nonBlocking, ".nonBlocking"),
      (.append, ".append"),
      (.create, ".create"),
      (.truncate, ".truncate"),
      (.exclusiveCreate, ".exclusiveCreate"),
      (.noFollow, ".noFollow"),
      (.closeOnExec, ".closeOnExec")
    ]
#endif

    return _buildDescription(descriptions)
  }

  /// A textual representation of the open options, suitable for debugging.
  public var debugDescription: String { self.description }
}