FileFlags.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // |------------------------|
  12. // | Swift API to C Mapping |
  13. // |------------------------------------------------------------------|
  14. // | FileFlags | Darwin | FreeBSD | OpenBSD |
  15. // |------------------|---------------|---------------|---------------|
  16. // | noDump | UF_NODUMP | UF_NODUMP | UF_NODUMP |
  17. // | userImmutable | UF_IMMUTABLE | UF_IMMUTABLE | UF_IMMUTABLE |
  18. // | userAppend | UF_APPEND | UF_APPEND | UF_APPEND |
  19. // | archived | SF_ARCHIVED | SF_ARCHIVED | SF_ARCHIVED |
  20. // | systemImmutable | SF_IMMUTABLE | SF_IMMUTABLE | SF_IMMUTABLE |
  21. // | systemAppend | SF_APPEND | SF_APPEND | SF_APPEND |
  22. // | opaque | UF_OPAQUE | UF_OPAQUE | N/A |
  23. // | hidden | UF_HIDDEN | UF_HIDDEN | N/A |
  24. // | systemNoUnlink | SF_NOUNLINK | SF_NOUNLINK | N/A |
  25. // | compressed | UF_COMPRESSED | N/A | N/A |
  26. // | tracked | UF_TRACKED | N/A | N/A |
  27. // | dataVault | UF_DATAVAULT | N/A | N/A |
  28. // | restricted | SF_RESTRICTED | N/A | N/A |
  29. // | firmlink | SF_FIRMLINK | N/A | N/A |
  30. // | dataless | SF_DATALESS | N/A | N/A |
  31. // | userNoUnlink | N/A | UF_NOUNLINK | N/A |
  32. // | offline | N/A | UF_OFFLINE | N/A |
  33. // | readOnly | N/A | UF_READONLY | N/A |
  34. // | reparse | N/A | UF_REPARSE | N/A |
  35. // | sparse | N/A | UF_SPARSE | N/A |
  36. // | system | N/A | UF_SYSTEM | N/A |
  37. // | snapshot | N/A | SF_SNAPSHOT | N/A |
  38. // |------------------|---------------|---------------|---------------|
  39. #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD)
  40. /// File-specific flags found in the `st_flags` property of a `stat` struct
  41. /// or used as input to `chflags()`.
  42. ///
  43. /// - Note: Only available on Darwin, FreeBSD, and OpenBSD.
  44. @frozen
  45. @available(System 1.7.0, *)
  46. public struct FileFlags: OptionSet, Sendable, Hashable, Codable {
  47. /// The raw C flags.
  48. @_alwaysEmitIntoClient
  49. public let rawValue: CInterop.FileFlags
  50. /// Creates a strongly-typed `FileFlags` from the raw C value.
  51. @_alwaysEmitIntoClient
  52. public init(rawValue: CInterop.FileFlags) { self.rawValue = rawValue }
  53. // MARK: Flags Available on Darwin, FreeBSD, and OpenBSD
  54. /// Do not dump the file during backups.
  55. ///
  56. /// The corresponding C constant is `UF_NODUMP`.
  57. /// - Note: This flag may be changed by the file owner or superuser.
  58. @_alwaysEmitIntoClient
  59. public static var noDump: FileFlags { FileFlags(rawValue: _UF_NODUMP) }
  60. /// File may not be changed.
  61. ///
  62. /// The corresponding C constant is `UF_IMMUTABLE`.
  63. /// - Note: This flag may be changed by the file owner or superuser.
  64. @_alwaysEmitIntoClient
  65. public static var userImmutable: FileFlags { FileFlags(rawValue: _UF_IMMUTABLE) }
  66. /// Writes to the file may only append.
  67. ///
  68. /// The corresponding C constant is `UF_APPEND`.
  69. /// - Note: This flag may be changed by the file owner or superuser.
  70. @_alwaysEmitIntoClient
  71. public static var userAppend: FileFlags { FileFlags(rawValue: _UF_APPEND) }
  72. /// File has been archived.
  73. ///
  74. /// The corresponding C constant is `SF_ARCHIVED`.
  75. /// - Note: This flag may only be changed by the superuser.
  76. @_alwaysEmitIntoClient
  77. public static var archived: FileFlags { FileFlags(rawValue: _SF_ARCHIVED) }
  78. /// File may not be changed.
  79. ///
  80. /// The corresponding C constant is `SF_IMMUTABLE`.
  81. /// - Note: This flag may only be changed by the superuser.
  82. @_alwaysEmitIntoClient
  83. public static var systemImmutable: FileFlags { FileFlags(rawValue: _SF_IMMUTABLE) }
  84. /// Writes to the file may only append.
  85. ///
  86. /// The corresponding C constant is `SF_APPEND`.
  87. /// - Note: This flag may only be changed by the superuser.
  88. @_alwaysEmitIntoClient
  89. public static var systemAppend: FileFlags { FileFlags(rawValue: _SF_APPEND) }
  90. // MARK: Flags Available on Darwin and FreeBSD
  91. #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
  92. /// Directory is opaque when viewed through a union mount.
  93. ///
  94. /// The corresponding C constant is `UF_OPAQUE`.
  95. /// - Note: This flag may be changed by the file owner or superuser.
  96. @_alwaysEmitIntoClient
  97. public static var opaque: FileFlags { FileFlags(rawValue: _UF_OPAQUE) }
  98. /// File should not be displayed in a GUI.
  99. ///
  100. /// The corresponding C constant is `UF_HIDDEN`.
  101. /// - Note: This flag may be changed by the file owner or superuser.
  102. @_alwaysEmitIntoClient
  103. public static var hidden: FileFlags { FileFlags(rawValue: _UF_HIDDEN) }
  104. /// File may not be removed or renamed.
  105. ///
  106. /// The corresponding C constant is `SF_NOUNLINK`.
  107. /// - Note: This flag may only be changed by the superuser.
  108. @_alwaysEmitIntoClient
  109. public static var systemNoUnlink: FileFlags { FileFlags(rawValue: _SF_NOUNLINK) }
  110. #endif
  111. // MARK: Flags Available on Darwin only
  112. #if SYSTEM_PACKAGE_DARWIN
  113. /// File is compressed at the file system level.
  114. ///
  115. /// The corresponding C constant is `UF_COMPRESSED`.
  116. /// - Note: This flag is read-only. Attempting to change it will result in undefined behavior.
  117. @_alwaysEmitIntoClient
  118. public static var compressed: FileFlags { FileFlags(rawValue: _UF_COMPRESSED) }
  119. /// File is tracked for the purpose of document IDs.
  120. ///
  121. /// The corresponding C constant is `UF_TRACKED`.
  122. /// - Note: This flag may be changed by the file owner or superuser.
  123. @_alwaysEmitIntoClient
  124. public static var tracked: FileFlags { FileFlags(rawValue: _UF_TRACKED) }
  125. /// File requires an entitlement for reading and writing.
  126. ///
  127. /// The corresponding C constant is `UF_DATAVAULT`.
  128. /// - Note: This flag may be changed by the file owner or superuser.
  129. @_alwaysEmitIntoClient
  130. public static var dataVault: FileFlags { FileFlags(rawValue: _UF_DATAVAULT) }
  131. /// File requires an entitlement for writing.
  132. ///
  133. /// The corresponding C constant is `SF_RESTRICTED`.
  134. /// - Note: This flag may only be changed by the superuser.
  135. @_alwaysEmitIntoClient
  136. public static var restricted: FileFlags { FileFlags(rawValue: _SF_RESTRICTED) }
  137. /// File is a firmlink.
  138. ///
  139. /// Firmlinks are used by macOS to create transparent links between
  140. /// the read-only system volume and writable data volume. For example,
  141. /// the `/Applications` folder on the system volume is a firmlink to
  142. /// the `/Applications` folder on the data volume, allowing the user
  143. /// to see both system- and user-installed applications in a single folder.
  144. ///
  145. /// The corresponding C constant is `SF_FIRMLINK`.
  146. /// - Note: This flag may only be changed by the superuser.
  147. @_alwaysEmitIntoClient
  148. public static var firmlink: FileFlags { FileFlags(rawValue: _SF_FIRMLINK) }
  149. /// File is a dataless placeholder (content is stored remotely).
  150. ///
  151. /// The system will attempt to materialize the file when accessed according to
  152. /// the dataless file materialization policy of the accessing thread or process.
  153. /// See `getiopolicy_np(3)`.
  154. ///
  155. /// The corresponding C constant is `SF_DATALESS`.
  156. /// - Note: This flag is read-only. Attempting to change it will result in undefined behavior.
  157. @_alwaysEmitIntoClient
  158. public static var dataless: FileFlags { FileFlags(rawValue: _SF_DATALESS) }
  159. #endif
  160. // MARK: Flags Available on FreeBSD Only
  161. #if os(FreeBSD)
  162. /// File may not be removed or renamed.
  163. ///
  164. /// The corresponding C constant is `UF_NOUNLINK`.
  165. /// - Note: This flag may be changed by the file owner or superuser.
  166. @_alwaysEmitIntoClient
  167. public static var userNoUnlink: FileFlags { FileFlags(rawValue: _UF_NOUNLINK) }
  168. /// File has the Windows offline attribute.
  169. ///
  170. /// File systems may use this flag for compatibility with the Windows `FILE_ATTRIBUTE_OFFLINE` attribute,
  171. /// but otherwise provide no special handling when it's set.
  172. ///
  173. /// The corresponding C constant is `UF_OFFLINE`.
  174. /// - Note: This flag may be changed by the file owner or superuser.
  175. @_alwaysEmitIntoClient
  176. public static var offline: FileFlags { FileFlags(rawValue: _UF_OFFLINE) }
  177. /// File is read-only.
  178. ///
  179. /// File systems may use this flag for compatibility with the Windows `FILE_ATTRIBUTE_READONLY` attribute.
  180. ///
  181. /// The corresponding C constant is `UF_READONLY`.
  182. /// - Note: This flag may be changed by the file owner or superuser.
  183. @_alwaysEmitIntoClient
  184. public static var readOnly: FileFlags { FileFlags(rawValue: _UF_READONLY) }
  185. /// File contains a Windows reparse point.
  186. ///
  187. /// File systems may use this flag for compatibility with the Windows `FILE_ATTRIBUTE_REPARSE_POINT` attribute.
  188. ///
  189. /// The corresponding C constant is `UF_REPARSE`.
  190. /// - Note: This flag may be changed by the file owner or superuser.
  191. @_alwaysEmitIntoClient
  192. public static var reparse: FileFlags { FileFlags(rawValue: _UF_REPARSE) }
  193. /// File is sparse.
  194. ///
  195. /// File systems may use this flag for compatibility with the Windows `FILE_ATTRIBUTE_SPARSE_FILE` attribute,
  196. /// or to indicate a sparse file.
  197. ///
  198. /// The corresponding C constant is `UF_SPARSE`.
  199. /// - Note: This flag may be changed by the file owner or superuser.
  200. @_alwaysEmitIntoClient
  201. public static var sparse: FileFlags { FileFlags(rawValue: _UF_SPARSE) }
  202. /// File has the Windows system attribute.
  203. ///
  204. /// File systems may use this flag for compatibility with the Windows `FILE_ATTRIBUTE_SYSTEM` attribute,
  205. /// but otherwise provide no special handling when it's set.
  206. ///
  207. /// The corresponding C constant is `UF_SYSTEM`.
  208. /// - Note: This flag may be changed by the file owner or superuser.
  209. @_alwaysEmitIntoClient
  210. public static var system: FileFlags { FileFlags(rawValue: _UF_SYSTEM) }
  211. /// File is a snapshot.
  212. ///
  213. /// The corresponding C constant is `SF_SNAPSHOT`.
  214. /// - Note: This flag may only be changed by the superuser.
  215. @_alwaysEmitIntoClient
  216. public static var snapshot: FileFlags { FileFlags(rawValue: _SF_SNAPSHOT) }
  217. #endif
  218. }
  219. #endif