Apple a pour la première fois procédé à sa présentation au mois de juin. Swift System est une bibliothèque multiplateforme. Elle donne l’accès à un ensemble d’interfaces vers des appels système. Quelques exemples :
Descripteurs de fichiers
Code Swift : | Sélectionner tout |
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 | /* This source file is part of the Swift System open source project Copyright (c) 2020 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 // @available(macOS 10.16, 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 } } // @available(macOS 10.16, 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) } /// 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:System/FileDescriptor/open(_:_:options:permissions:)-10dcs> /// 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 } /// 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:System/FileDescriptor/open(_:_:options:permissions:)-10dcs> /// 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:System/FileDescriptor/open(_:_:options:permissions:)-10dcs> /// 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 os(macOS) || os(iOS) || os(watchOS) || os(tvOS) /// 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 /// Indicates that opening the file doesn't follow symlinks. /// /// If you specify this option /// and the file path you pass to /// <doc:System/FileDescriptor/open(_:_:options:permissions:)-10dcs> /// 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 } #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) /// 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:System/FileDescriptor/open(_:_:options:permissions:)-10dcs> /// 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 /// 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 } } /// 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 os(macOS) || os(iOS) || os(watchOS) || os(tvOS) /// 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 } } // @available(macOS 10.16, 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 } } // @available(macOS 10.16, 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 os(macOS) || os(iOS) || os(watchOS) || os(tvOS) 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 } } // @available(macOS 10.16, 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 os(macOS) || os(iOS) || os(watchOS) || os(tvOS) 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") ] #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 } } |
Opérations sur les fichiers
Code Swift : | Sélectionner tout |
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 | /* This source file is part of the Swift System open source project Copyright (c) 2020 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 */ @_implementationOnly import SystemInternals // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) extension FileDescriptor { /// Opens or creates a file for reading or writing. /// /// - Parameters: /// - path: The location of the file to open. /// - mode: The read and write access to use. /// - options: The behavior for opening the file. /// - permissions: The file permissions to use for created files. /// - retryOnInterrupt: Whether to retry the open operation /// if it throws ``Errno/interrupted``. /// The default is `true`. /// Pass `false` to try only once and throw an error upon interruption. /// - Returns: A file descriptor for the open file /// /// The corresponding C function is `open`. @_alwaysEmitIntoClient public static func open( _ path: FilePath, _ mode: FileDescriptor.AccessMode, options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(), permissions: FilePermissions? = nil, retryOnInterrupt: Bool = true ) throws -> FileDescriptor { try path.withCString { try FileDescriptor.open( $0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt) } } /// Opens or creates a file for reading or writing. /// /// - Parameters: /// - path: The location of the file to open. /// - mode: The read and write access to use. /// - options: The behavior for opening the file. /// - permissions: The file permissions to use for created files. /// - retryOnInterrupt: Whether to retry the open operation /// if it throws ``Errno/interrupted``. /// The default is `true`. /// Pass `false` to try only once and throw an error upon interruption. /// - Returns: A file descriptor for the open file /// /// The corresponding C function is `open`. @_alwaysEmitIntoClient public static func open( _ path: UnsafePointer<CChar>, _ mode: FileDescriptor.AccessMode, options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(), permissions: FilePermissions? = nil, retryOnInterrupt: Bool = true ) throws -> FileDescriptor { try FileDescriptor._open( path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt ).get() } @usableFromInline internal static func _open( _ path: UnsafePointer<CChar>, _ mode: FileDescriptor.AccessMode, options: FileDescriptor.OpenOptions, permissions: FilePermissions?, retryOnInterrupt: Bool = true ) -> Result<FileDescriptor, Errno> { let oFlag = mode.rawValue | options.rawValue let descOrError: Result<CInt, Errno> = valueOrErrno(retryOnInterrupt: retryOnInterrupt) { if let permissions = permissions { return system_open(path, oFlag, permissions.rawValue) } precondition(!options.contains(.create), "Create must be given permissions") return system_open(path, oFlag) } return descOrError.map { FileDescriptor(rawValue: $0) } } /// Deletes a file descriptor. /// /// Deletes the file descriptor from the per-process object reference table. /// If this is the last reference to the underlying object, /// the object will be deactivated. /// /// The corresponding C function is `close`. @_alwaysEmitIntoClient public func close() throws { try _close().get() } @usableFromInline internal func _close() -> Result<(), Errno> { nothingOrErrno(system_close(self.rawValue)) } /// Reposition the offset for the given file descriptor. /// /// - Parameters: /// - offset: The new offset for the file descriptor. /// - whence: The origin of the new offset. /// - Returns: The file's offset location, /// in bytes from the beginning of the file. /// /// The corresponding C function is `lseek`. @_alwaysEmitIntoClient @discardableResult public func seek( offset: Int64, from whence: FileDescriptor.SeekOrigin ) throws -> Int64 { try _seek(offset: offset, from: whence).get() } @usableFromInline internal func _seek( offset: Int64, from whence: FileDescriptor.SeekOrigin ) -> Result<Int64, Errno> { let newOffset = system_lseek(self.rawValue, COffT(offset), whence.rawValue) return valueOrErrno(Int64(newOffset)) } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "seek") public func lseek( offset: Int64, from whence: FileDescriptor.SeekOrigin ) throws -> Int64 { try seek(offset: offset, from: whence) } /// Reads bytes at the current file offset into a buffer. /// /// - Parameters: /// - buffer: The region of memory to read into. /// - retryOnInterrupt: Whether to retry the read operation /// if it throws ``Errno/interrupted``. /// The default is `true`. /// Pass `false` to try only once and throw an error upon interruption. /// - Returns: The number of bytes that were read. /// /// The <doc://com.apple.documentation/documentation/swift/unsafemutablerawbufferpointer/3019191-count> property of `buffer` /// determines the maximum number of bytes that are read into that buffer. /// /// After reading, /// this method increments the file's offset by the number of bytes read. /// To change the file's offset, /// call the ``seek(offset:from:)`` method. /// /// The corresponding C function is `read`. @_alwaysEmitIntoClient public func read( into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool = true ) throws -> Int { try _read(into: buffer, retryOnInterrupt: retryOnInterrupt).get() } @usableFromInline internal func _read( into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool = true ) throws -> Result<Int, Errno> { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { system_read(self.rawValue, buffer.baseAddress, buffer.count) } } /// Reads bytes at the specified offset into a buffer. /// /// - Parameters: /// - offset: The file offset where reading begins. /// - buffer: The region of memory to read into. /// - retryOnInterrupt: Whether to retry the read operation /// if it throws ``Errno/interrupted``. /// The default is `true`. /// Pass `false` to try only once and throw an error upon interruption. /// - Returns: The number of bytes that were read. /// /// The <doc://com.apple.documentation/documentation/swift/unsafemutablerawbufferpointer/3019191-count> property of `buffer` /// determines the maximum number of bytes that are read into that buffer. /// /// Unlike <doc:System/FileDescriptor/read(into:retryOnInterrupt:)>, /// this method leaves the file's existing offset unchanged. /// /// The corresponding C function is `pread`. @_alwaysEmitIntoClient public func read( fromAbsoluteOffset offset: Int64, into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool = true ) throws -> Int { try _read( fromAbsoluteOffset: offset, into: buffer, retryOnInterrupt: retryOnInterrupt ).get() } @usableFromInline internal func _read( fromAbsoluteOffset offset: Int64, into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool ) -> Result<Int, Errno> { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { system_pread(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset)) } } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "read") public func pread( fromAbsoluteOffset offset: Int64, into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool = true ) throws -> Int { try read( fromAbsoluteOffset: offset, into: buffer, retryOnInterrupt: retryOnInterrupt) } /// Writes the contents of a buffer at the current file offset. /// /// - Parameters: /// - buffer: The region of memory that contains the data being written. /// - retryOnInterrupt: Whether to retry the write operation /// if it throws ``Errno/interrupted``. /// The default is `true`. /// Pass `false` to try only once and throw an error upon interruption. /// - Returns: The number of bytes that were written. /// /// After writing, /// this method increments the file's offset by the number of bytes written. /// To change the file's offset, /// call the ``seek(offset:from:)`` method. /// /// The corresponding C function is `write`. @_alwaysEmitIntoClient public func write( _ buffer: UnsafeRawBufferPointer, retryOnInterrupt: Bool = true ) throws -> Int { try _write(buffer, retryOnInterrupt: retryOnInterrupt).get() } @usableFromInline internal func _write( _ buffer: UnsafeRawBufferPointer, retryOnInterrupt: Bool ) -> Result<Int, Errno> { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { system_write(self.rawValue, buffer.baseAddress, buffer.count) } } /// Writes the contents of a buffer at the specified offset. /// /// - Parameters: /// - offset: The file offset where writing begins. /// - buffer: The region of memory that contains the data being written. /// - retryOnInterrupt: Whether to retry the write operation /// if it throws ``Errno/interrupted``. /// The default is `true`. /// Pass `false` to try only once and throw an error upon interruption. /// - Returns: The number of bytes that were written. /// /// Unlike ``write(_:retryOnInterrupt:)``, /// this method leaves the file's existing offset unchanged. /// /// The corresponding C function is `pwrite`. @_alwaysEmitIntoClient public func write( toAbsoluteOffset offset: Int64, _ buffer: UnsafeRawBufferPointer, retryOnInterrupt: Bool = true ) throws -> Int { try _write(toAbsoluteOffset: offset, buffer, retryOnInterrupt: retryOnInterrupt).get() } @usableFromInline internal func _write( toAbsoluteOffset offset: Int64, _ buffer: UnsafeRawBufferPointer, retryOnInterrupt: Bool ) -> Result<Int, Errno> { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { system_pwrite(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset)) } } @_alwaysEmitIntoClient @available(*, unavailable, renamed: "write") public func pwrite( toAbsoluteOffset offset: Int64, into buffer: UnsafeRawBufferPointer, retryOnInterrupt: Bool = true ) throws -> Int { try write( toAbsoluteOffset: offset, buffer, retryOnInterrupt: retryOnInterrupt) } } |
Permissions sur les fichiers
Code : | Sélectionner tout |
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 | /* This source file is part of the Swift System open source project Copyright (c) 2020 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 */ /// The access permissions for a file. /// /// The following example /// creates an instance of the `FilePermissions` structure /// from a raw octal literal and compares it /// to a file permission created using named options: /// /// let perms = FilePermissions(rawValue: 0o644) /// perms == [.ownerReadWrite, .groupRead, .otherRead] // true @frozen // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) public struct FilePermissions: OptionSet, Hashable, Codable { /// The raw C file permissions. @_alwaysEmitIntoClient public let rawValue: CModeT /// Create a strongly-typed file permission from a raw C value. @_alwaysEmitIntoClient public init(rawValue: CModeT) { self.rawValue = rawValue } @_alwaysEmitIntoClient private init(_ raw: CModeT) { self.init(rawValue: raw) } /// Indicates that other users have read-only permission. @_alwaysEmitIntoClient public static var otherRead: FilePermissions { FilePermissions(0o4) } /// Indicates that other users have write-only permission. @_alwaysEmitIntoClient public static var otherWrite: FilePermissions { FilePermissions(0o2) } /// Indicates that other users have execute-only permission. @_alwaysEmitIntoClient public static var otherExecute: FilePermissions { FilePermissions(0o1) } /// Indicates that other users have read-write permission. @_alwaysEmitIntoClient public static var otherReadWrite: FilePermissions { FilePermissions(0o6) } /// Indicates that other users have read-execute permission. @_alwaysEmitIntoClient public static var otherReadExecute: FilePermissions { FilePermissions(0o5) } /// Indicates that other users have write-execute permission. @_alwaysEmitIntoClient public static var otherWriteExecute: FilePermissions { FilePermissions(0o3) } /// Indicates that other users have read, write, and execute permission. @_alwaysEmitIntoClient public static var otherReadWriteExecute: FilePermissions { FilePermissions(0o7) } /// Indicates that the group has read-only permission. @_alwaysEmitIntoClient public static var groupRead: FilePermissions { FilePermissions(0o40) } /// Indicates that the group has write-only permission. @_alwaysEmitIntoClient public static var groupWrite: FilePermissions { FilePermissions(0o20) } /// Indicates that the group has execute-only permission. @_alwaysEmitIntoClient public static var groupExecute: FilePermissions { FilePermissions(0o10) } /// Indicates that the group has read-write permission. @_alwaysEmitIntoClient public static var groupReadWrite: FilePermissions { FilePermissions(0o60) } /// Indicates that the group has read-execute permission. @_alwaysEmitIntoClient public static var groupReadExecute: FilePermissions { FilePermissions(0o50) } /// Indicates that the group has write-execute permission. @_alwaysEmitIntoClient public static var groupWriteExecute: FilePermissions { FilePermissions(0o30) } /// Indicates that the group has read, write, and execute permission. @_alwaysEmitIntoClient public static var groupReadWriteExecute: FilePermissions { FilePermissions(0o70) } /// Indicates that the owner has read-only permission. @_alwaysEmitIntoClient public static var ownerRead: FilePermissions { FilePermissions(0o400) } /// Indicates that the owner has write-only permission. @_alwaysEmitIntoClient public static var ownerWrite: FilePermissions { FilePermissions(0o200) } /// Indicates that the owner has execute-only permission. @_alwaysEmitIntoClient public static var ownerExecute: FilePermissions { FilePermissions(0o100) } /// Indicates that the owner has read-write permission. @_alwaysEmitIntoClient public static var ownerReadWrite: FilePermissions { FilePermissions(0o600) } /// Indicates that the owner has read-execute permission. @_alwaysEmitIntoClient public static var ownerReadExecute: FilePermissions { FilePermissions(0o500) } /// Indicates that the owner has write-execute permission. @_alwaysEmitIntoClient public static var ownerWriteExecute: FilePermissions { FilePermissions(0o300) } /// Indicates that the owner has read, write, and execute permission. @_alwaysEmitIntoClient public static var ownerReadWriteExecute: FilePermissions { FilePermissions(0o700) } /// Indicates that the file is executed as the owner. /// /// For more information, see the `setuid(2)` man page. @_alwaysEmitIntoClient public static var setUserID: FilePermissions { FilePermissions(0o4000) } /// Indicates that the file is executed as the group. /// /// For more information, see the `setgid(2)` man page. @_alwaysEmitIntoClient public static var setGroupID: FilePermissions { FilePermissions(0o2000) } /// Indicates that executable's text segment /// should be kept in swap space even after it exits. /// /// For more information, see the `chmod(2)` man page's /// discussion of `S_ISVTX` (the sticky bit). @_alwaysEmitIntoClient public static var saveText: FilePermissions { FilePermissions(0o1000) } } // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) extension FilePermissions : CustomStringConvertible, CustomDebugStringConvertible { /// A textual representation of the file permissions. @inline(never) public var description: String { let descriptions: [(Element, StaticString)] = [ (.ownerReadWriteExecute, ".ownerReadWriteExecute"), (.ownerReadWrite, ".ownerReadWrite"), (.ownerReadExecute, ".ownerReadExecute"), (.ownerWriteExecute, ".ownerWriteExecute"), (.ownerRead, ".ownerRead"), (.ownerWrite, ".ownerWrite"), (.ownerExecute, ".ownerExecute"), (.groupReadWriteExecute, ".groupReadWriteExecute"), (.groupReadWrite, ".groupReadWrite"), (.groupReadExecute, ".groupReadExecute"), (.groupWriteExecute, ".groupWriteExecute"), (.groupRead, ".groupRead"), (.groupWrite, ".groupWrite"), (.groupExecute, ".groupExecute"), (.otherReadWriteExecute, ".otherReadWriteExecute"), (.otherReadWrite, ".otherReadWrite"), (.otherReadExecute, ".otherReadExecute"), (.otherWriteExecute, ".otherWriteExecute"), (.otherRead, ".otherRead"), (.otherWrite, ".otherWrite"), (.otherExecute, ".otherExecute"), (.setUserID, ".setUserID"), (.setGroupID, ".setGroupID"), (.saveText, ".saveText") ] return _buildDescription(descriptions) } /// A textual representation of the file permissions, suitable for debugging. public var debugDescription: String { self.description } } |
« Le résultat c’est du code qui se lit et se comporte comme du Swift idiomatique », commente Apple.
« System en est encore au stade l’enfance », précise Apple. Lors de la publication de la feuille de route de la version 5.3 du langage Swift, Apple avait annoncé que cette version allait apporter de profondes améliorations de performances ainsi que la prise en charge sur Windows. Beaucoup de chemin reste à parcourir et l’un des chantiers est justement la prise en charge de Swift System pour le développement sous Windows après le cas Linux.
Source : Apple
Et vous ?
Qu’en pensez-vous ?
Voir aussi :
Swift 5.3 est disponible avec de meilleures performances et axée sur une meilleure productivité, et améliore l'ergonomie de l'écriture du code Swift
En route vers Swift 6 : l'équipe en charge du développement de Swift dévoile la nouvelle feuille de route du projet
Swift 5.2 est disponible avec une nouvelle architecture de diagnostic, qui vise à améliorer la qualité et la précision des messages d'erreur
Swift 5.3 sera officiellement disponible et pris en charge sur Windows, ainsi que sur d'autres distributions Linux