Skip to content

Commit a515417

Browse files
committed
Riff off the getfsstat implementation for OpenBSD.
The mac OS implementation is close to what is required for OpenBSD but requires tweaking. Instead of trying to unify the two implementations with #if branches, just riff off the mac OS implementation and create a new platform specific implementation.
1 parent 2271829 commit a515417

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Sources/Foundation/FileManager+POSIX.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ extension FileManager {
7070
}
7171
urls = mountPoints(statBuf, Int(fsCount))
7272
}
73+
#elseif os(OpenBSD)
74+
func mountPoints(_ statBufs: UnsafePointer<statfs>, _ fsCount: Int) -> [URL] {
75+
var urls: [URL] = []
76+
77+
for fsIndex in 0..<fsCount {
78+
var fs = statBufs.advanced(by: fsIndex).pointee
79+
80+
let mountPoint = withUnsafePointer(to: &fs.f_mntonname.0) { (ptr: UnsafePointer<Int8>) -> String in
81+
return string(withFileSystemRepresentation: ptr, length: strlen(ptr))
82+
}
83+
urls.append(URL(fileURLWithPath: mountPoint, isDirectory: true))
84+
}
85+
return urls
86+
}
87+
88+
var fsCount = getfsstat(nil, 0, MNT_WAIT)
89+
guard fsCount > 0 else {
90+
return nil
91+
}
92+
let statBuf = UnsafeMutablePointer<statfs>.allocate(capacity: Int(fsCount))
93+
defer { statBuf.deallocate() }
94+
fsCount = getfsstat(statBuf, Int(fsCount) * MemoryLayout<statfs>.stride, MNT_WAIT)
95+
guard fsCount > 0 else {
96+
return nil
97+
}
98+
urls = mountPoints(statBuf, Int(fsCount))
7399
#else
74100
#error("Requires a platform-specific implementation")
75101
#endif

0 commit comments

Comments
 (0)