mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
v4: Add FileSystemTypeName wrapper
This commit is contained in:
2 files changed
+97
-7
No files matched your search
@@ -237,6 +237,67 @@ public struct FileSystemSubtype: RawRepresentable, Sendable, Hashable, Codable {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD)
|
||||
// MARK: - FileSystemTypeName
|
||||
|
||||
/// A Swift wrapper of the C `f_fstypename` file system type name found in a
|
||||
/// `statfs` struct.
|
||||
///
|
||||
/// - Note: Only available on Darwin and BSD.
|
||||
@frozen
|
||||
@available(System 199, *)
|
||||
public struct FileSystemTypeName
|
||||
: RawRepresentable, Sendable, Hashable, Codable, ExpressibleByStringLiteral
|
||||
{
|
||||
|
||||
/// The raw file system type name.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: String
|
||||
|
||||
/// Creates a strongly-typed `FileSystemTypeName` from the raw name.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(rawValue: String) { self.rawValue = rawValue }
|
||||
|
||||
/// Creates a strongly-typed `FileSystemTypeName` from the raw name.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(_ rawValue: String) { self.rawValue = rawValue }
|
||||
|
||||
/// Creates a `FileSystemTypeName` from a string literal, such as `"apfs"`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init(stringLiteral value: String) { self.rawValue = value }
|
||||
|
||||
// MARK: Type Names Available on Darwin Only
|
||||
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
/// The Apple File System (APFS).
|
||||
///
|
||||
/// - Note: Only available on Darwin.
|
||||
///
|
||||
/// The corresponding `f_fstypename` value is `"apfs"`.
|
||||
@_alwaysEmitIntoClient
|
||||
public static var apfs: FileSystemTypeName { FileSystemTypeName(rawValue: "apfs") }
|
||||
|
||||
/// The Mac OS Extended file system (HFS Plus).
|
||||
///
|
||||
/// - Note: Only available on Darwin.
|
||||
///
|
||||
/// The corresponding `f_fstypename` value is `"hfs"`.
|
||||
@_alwaysEmitIntoClient
|
||||
public static var hfs: FileSystemTypeName { FileSystemTypeName(rawValue: "hfs") }
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(System 199, *)
|
||||
extension FileSystemTypeName: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
/// The file system type name, such as `apfs`.
|
||||
@inline(never)
|
||||
public var description: String { rawValue }
|
||||
|
||||
/// The file system type name as a quoted string, such as `"apfs"`.
|
||||
public var debugDescription: String { description.debugDescription }
|
||||
}
|
||||
#endif // SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD)
|
||||
|
||||
// MARK: - StatFS
|
||||
|
||||
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD)
|
||||
@@ -618,16 +679,16 @@ public struct StatFS: RawRepresentable, Sendable, Hashable {
|
||||
set { rawValue.f_owner = newValue.rawValue }
|
||||
}
|
||||
|
||||
/// File system type name.
|
||||
/// File system type name, such as "apfs".
|
||||
///
|
||||
/// - Note: Only available on Darwin and BSD.
|
||||
///
|
||||
/// The corresponding C property is `f_fstypename`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var typeName: String {
|
||||
withUnsafeBytes(of: rawValue.f_fstypename) {
|
||||
public var typeName: FileSystemTypeName {
|
||||
FileSystemTypeName(rawValue: withUnsafeBytes(of: rawValue.f_fstypename) {
|
||||
String(_nullTerminatedBytes: $0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Directory where the file system is mounted, such as "/System/Volumes/Data".
|
||||
|
||||
@@ -86,7 +86,7 @@ private struct StatFSTests {
|
||||
try withTemporaryFilePath(basename: "StatFS_darwinBSD") { tempDir in
|
||||
let statfs = try StatFS(tempDir)
|
||||
#expect(statfs.preferredIOBlockSize > 0)
|
||||
#expect(!statfs.typeName.isEmpty)
|
||||
#expect(!statfs.typeName.rawValue.isEmpty)
|
||||
#expect(statfs.mountPoint.isAbsolute)
|
||||
#expect(!statfs.mountSource.string.isEmpty)
|
||||
|
||||
@@ -135,7 +135,7 @@ private struct StatFSTests {
|
||||
#expect(s.preferredIOBlockSize == Int(raw.f_iosize))
|
||||
#expect(s.owner.rawValue == raw.f_owner)
|
||||
#expect(s.mountFlags.rawValue == raw.f_flags)
|
||||
#expect(s.typeName == withUnsafeBytes(of: raw.f_fstypename) {
|
||||
#expect(s.typeName.rawValue == withUnsafeBytes(of: raw.f_fstypename) {
|
||||
$0.withMemoryRebound(to: CChar.self) { String(cString: $0.baseAddress!) }
|
||||
})
|
||||
#expect(s.mountPoint.string == withUnsafeBytes(of: raw.f_mntonname) {
|
||||
@@ -391,6 +391,35 @@ private struct StatFSTests {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD)
|
||||
@available(System 199, *)
|
||||
@Test func fileSystemTypeNameConformances() throws {
|
||||
let name: FileSystemTypeName = "apfs"
|
||||
#expect(name == FileSystemTypeName(rawValue: "apfs"))
|
||||
#expect(name == FileSystemTypeName("apfs"))
|
||||
#expect(name != "hfs")
|
||||
#expect(name.hashValue == FileSystemTypeName("apfs").hashValue)
|
||||
#expect(name.description == "apfs")
|
||||
#expect(name.debugDescription == #""apfs""#)
|
||||
|
||||
#if canImport(Foundation)
|
||||
let data = try JSONEncoder().encode(name)
|
||||
#expect(String(decoding: data, as: UTF8.self) == #""apfs""#)
|
||||
#expect(try JSONDecoder().decode(FileSystemTypeName.self, from: data) == name)
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
@available(System 199, *)
|
||||
@Test func fileSystemTypeNameConstants() throws {
|
||||
#expect(FileSystemTypeName.apfs.rawValue == "apfs")
|
||||
#expect(FileSystemTypeName.hfs.rawValue == "hfs")
|
||||
// The root volume is APFS on every supported Apple platform.
|
||||
#expect(try StatFS("/").typeName == .apfs)
|
||||
}
|
||||
#endif
|
||||
|
||||
@available(System 199, *)
|
||||
@Test func descriptions() throws {
|
||||
#expect(MountFlags.readOnly.description == "[.readOnly]")
|
||||
@@ -417,7 +446,7 @@ private struct StatFSTests {
|
||||
let s = StatFS(rawValue: raw)
|
||||
let typeNameLength = MemoryLayout.size(ofValue: raw.f_fstypename)
|
||||
let mountPointLength = MemoryLayout.size(ofValue: raw.f_mntonname)
|
||||
#expect(s.typeName == String(repeating: "a", count: typeNameLength))
|
||||
#expect(s.typeName.rawValue == String(repeating: "a", count: typeNameLength))
|
||||
#expect(s.mountPoint.string == String(repeating: "b", count: mountPointLength))
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user