mirror of
https://github.com/apple/swift-system
synced 2026-11-24 04:38:14 +00:00
Add CInterop namespace and PlatformChar/PlatformEncoding typealiases for narrow/wide characters. Change Windows syscalls to use wide-character variants. Add full Windows root parser to error-correct malformed roots and correctly normalize roots and post-root portions of the path. Add platformString APIs as replacements for cString APIs. Add internal SystemString as backing storage type for FilePath. Add Windows path mocking support. This change includes API/ABI additions and deprecations.
178 lines
6.7 KiB
Swift
178 lines
6.7 KiB
Swift
/*
|
|
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: 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) }
|
|
}
|
|
|
|
// @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 }
|
|
}
|