FileMode.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift System open source project
  4. //
  5. // Copyright (c) 2025 - 2026 Apple Inc. and the Swift System project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #if !os(Windows)
  12. /// A strongly-typed file mode representing a C `mode_t`.
  13. ///
  14. /// - Note: Only available on Unix-like platforms.
  15. @frozen
  16. @available(System 1.7.0, *)
  17. public struct FileMode: RawRepresentable, Sendable, Hashable, Codable {
  18. /// The raw C mode.
  19. @_alwaysEmitIntoClient
  20. public var rawValue: CInterop.Mode
  21. /// Creates a strongly-typed `FileMode` from the raw C value.
  22. @_alwaysEmitIntoClient
  23. public init(rawValue: CInterop.Mode) { self.rawValue = rawValue }
  24. /// Creates a `FileMode` from the given file type and permissions.
  25. ///
  26. /// - Note: This initializer masks the inputs with their respective bit masks.
  27. @_alwaysEmitIntoClient
  28. public init(type: FileType, permissions: FilePermissions) {
  29. self.rawValue = (type.rawValue & _MODE_FILETYPE_MASK) | (permissions.rawValue & _MODE_PERMISSIONS_MASK)
  30. }
  31. /// The file's type, from the mode's file-type bits.
  32. ///
  33. /// Setting this property will mask the `newValue` with the file-type bit mask `S_IFMT`.
  34. @_alwaysEmitIntoClient
  35. public var type: FileType {
  36. get { FileType(rawValue: rawValue & _MODE_FILETYPE_MASK) }
  37. set { rawValue = (rawValue & ~_MODE_FILETYPE_MASK) | (newValue.rawValue & _MODE_FILETYPE_MASK) }
  38. }
  39. /// The file's permissions, from the mode's permission bits.
  40. ///
  41. /// Setting this property will mask the `newValue` with the permissions bit mask `ALLPERMS`.
  42. @_alwaysEmitIntoClient
  43. public var permissions: FilePermissions {
  44. get { FilePermissions(rawValue: rawValue & _MODE_PERMISSIONS_MASK) }
  45. set { rawValue = (rawValue & ~_MODE_PERMISSIONS_MASK) | (newValue.rawValue & _MODE_PERMISSIONS_MASK) }
  46. }
  47. }
  48. #endif