1
0

FilePermissions.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. This source file is part of the Swift System open source project
  3. Copyright (c) 2020 Apple Inc. and the Swift System project authors
  4. Licensed under Apache License v2.0 with Runtime Library Exception
  5. See https://swift.org/LICENSE.txt for license information
  6. */
  7. /// The access permissions for a file.
  8. ///
  9. /// The following example
  10. /// creates an instance of the `FilePermissions` structure
  11. /// from a raw octal literal and compares it
  12. /// to a file permission created using named options:
  13. ///
  14. /// let perms = FilePermissions(rawValue: 0o644)
  15. /// perms == [.ownerReadWrite, .groupRead, .otherRead] // true
  16. @frozen
  17. @available(System 0.0.1, *)
  18. public struct FilePermissions: OptionSet, Sendable, Hashable, Codable {
  19. /// The raw C file permissions.
  20. @_alwaysEmitIntoClient
  21. public let rawValue: CModeT
  22. /// Create a strongly-typed file permission from a raw C value.
  23. @_alwaysEmitIntoClient
  24. public init(rawValue: CModeT) { self.rawValue = rawValue }
  25. /// Indicates that other users have read-only permission.
  26. @_alwaysEmitIntoClient
  27. public static var otherRead: FilePermissions { .init(rawValue: 0o4) }
  28. /// Indicates that other users have write-only permission.
  29. @_alwaysEmitIntoClient
  30. public static var otherWrite: FilePermissions { .init(rawValue: 0o2) }
  31. /// Indicates that other users have execute-only permission.
  32. @_alwaysEmitIntoClient
  33. public static var otherExecute: FilePermissions { .init(rawValue: 0o1) }
  34. /// Indicates that other users have read-write permission.
  35. @_alwaysEmitIntoClient
  36. public static var otherReadWrite: FilePermissions { .init(rawValue: 0o6) }
  37. /// Indicates that other users have read-execute permission.
  38. @_alwaysEmitIntoClient
  39. public static var otherReadExecute: FilePermissions { .init(rawValue: 0o5) }
  40. /// Indicates that other users have write-execute permission.
  41. @_alwaysEmitIntoClient
  42. public static var otherWriteExecute: FilePermissions { .init(rawValue: 0o3) }
  43. /// Indicates that other users have read, write, and execute permission.
  44. @_alwaysEmitIntoClient
  45. public static var otherReadWriteExecute: FilePermissions { .init(rawValue: 0o7) }
  46. /// Indicates that the group has read-only permission.
  47. @_alwaysEmitIntoClient
  48. public static var groupRead: FilePermissions { .init(rawValue: 0o40) }
  49. /// Indicates that the group has write-only permission.
  50. @_alwaysEmitIntoClient
  51. public static var groupWrite: FilePermissions { .init(rawValue: 0o20) }
  52. /// Indicates that the group has execute-only permission.
  53. @_alwaysEmitIntoClient
  54. public static var groupExecute: FilePermissions { .init(rawValue: 0o10) }
  55. /// Indicates that the group has read-write permission.
  56. @_alwaysEmitIntoClient
  57. public static var groupReadWrite: FilePermissions { .init(rawValue: 0o60) }
  58. /// Indicates that the group has read-execute permission.
  59. @_alwaysEmitIntoClient
  60. public static var groupReadExecute: FilePermissions { .init(rawValue: 0o50) }
  61. /// Indicates that the group has write-execute permission.
  62. @_alwaysEmitIntoClient
  63. public static var groupWriteExecute: FilePermissions { .init(rawValue: 0o30) }
  64. /// Indicates that the group has read, write, and execute permission.
  65. @_alwaysEmitIntoClient
  66. public static var groupReadWriteExecute: FilePermissions { .init(rawValue: 0o70) }
  67. /// Indicates that the owner has read-only permission.
  68. @_alwaysEmitIntoClient
  69. public static var ownerRead: FilePermissions { .init(rawValue: 0o400) }
  70. /// Indicates that the owner has write-only permission.
  71. @_alwaysEmitIntoClient
  72. public static var ownerWrite: FilePermissions { .init(rawValue: 0o200) }
  73. /// Indicates that the owner has execute-only permission.
  74. @_alwaysEmitIntoClient
  75. public static var ownerExecute: FilePermissions { .init(rawValue: 0o100) }
  76. /// Indicates that the owner has read-write permission.
  77. @_alwaysEmitIntoClient
  78. public static var ownerReadWrite: FilePermissions { .init(rawValue: 0o600) }
  79. /// Indicates that the owner has read-execute permission.
  80. @_alwaysEmitIntoClient
  81. public static var ownerReadExecute: FilePermissions { .init(rawValue: 0o500) }
  82. /// Indicates that the owner has write-execute permission.
  83. @_alwaysEmitIntoClient
  84. public static var ownerWriteExecute: FilePermissions { .init(rawValue: 0o300) }
  85. /// Indicates that the owner has read, write, and execute permission.
  86. @_alwaysEmitIntoClient
  87. public static var ownerReadWriteExecute: FilePermissions { .init(rawValue: 0o700) }
  88. /// Indicates that the file is executed as the owner.
  89. ///
  90. /// For more information, see the `setuid(2)` man page.
  91. @_alwaysEmitIntoClient
  92. public static var setUserID: FilePermissions { .init(rawValue: 0o4000) }
  93. /// Indicates that the file is executed as the group.
  94. ///
  95. /// For more information, see the `setgid(2)` man page.
  96. @_alwaysEmitIntoClient
  97. public static var setGroupID: FilePermissions { .init(rawValue: 0o2000) }
  98. /// Indicates that executable's text segment
  99. /// should be kept in swap space even after it exits.
  100. ///
  101. /// For more information, see the `chmod(2)` man page's
  102. /// discussion of `S_ISVTX` (the sticky bit).
  103. @_alwaysEmitIntoClient
  104. public static var saveText: FilePermissions { .init(rawValue: 0o1000) }
  105. }
  106. @available(System 0.0.1, *)
  107. extension FilePermissions
  108. : CustomStringConvertible, CustomDebugStringConvertible
  109. {
  110. /// A textual representation of the file permissions.
  111. @inline(never)
  112. public var description: String {
  113. let descriptions: [(Element, StaticString)] = [
  114. (.ownerReadWriteExecute, ".ownerReadWriteExecute"),
  115. (.ownerReadWrite, ".ownerReadWrite"),
  116. (.ownerReadExecute, ".ownerReadExecute"),
  117. (.ownerWriteExecute, ".ownerWriteExecute"),
  118. (.ownerRead, ".ownerRead"),
  119. (.ownerWrite, ".ownerWrite"),
  120. (.ownerExecute, ".ownerExecute"),
  121. (.groupReadWriteExecute, ".groupReadWriteExecute"),
  122. (.groupReadWrite, ".groupReadWrite"),
  123. (.groupReadExecute, ".groupReadExecute"),
  124. (.groupWriteExecute, ".groupWriteExecute"),
  125. (.groupRead, ".groupRead"),
  126. (.groupWrite, ".groupWrite"),
  127. (.groupExecute, ".groupExecute"),
  128. (.otherReadWriteExecute, ".otherReadWriteExecute"),
  129. (.otherReadWrite, ".otherReadWrite"),
  130. (.otherReadExecute, ".otherReadExecute"),
  131. (.otherWriteExecute, ".otherWriteExecute"),
  132. (.otherRead, ".otherRead"),
  133. (.otherWrite, ".otherWrite"),
  134. (.otherExecute, ".otherExecute"),
  135. (.setUserID, ".setUserID"),
  136. (.setGroupID, ".setGroupID"),
  137. (.saveText, ".saveText")
  138. ]
  139. return _buildDescription(descriptions)
  140. }
  141. /// A textual representation of the file permissions, suitable for debugging.
  142. public var debugDescription: String { self.description }
  143. }