File: FilePermissions.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 (177 lines) | stat: -rw-r--r-- 6,914 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
/*
 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
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
public struct FilePermissions: OptionSet, Hashable, Codable {
  /// The raw C file permissions.
  @_alwaysEmitIntoClient
  public let rawValue: CInterop.Mode

  /// Create a strongly-typed file permission from a raw C value.
  @_alwaysEmitIntoClient
  public init(rawValue: CInterop.Mode) { self.rawValue = rawValue }

  @_alwaysEmitIntoClient
  private init(_ raw: CInterop.Mode) { 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) }
}

/*System 0.0.1, @available(macOS 11.0, 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 }
}