Skip to content

FileManager: Implement mountedVolumeURLs(includingResourceValuesForKeys:options:) #1496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,66 @@ open class FileManager : NSObject {
}
}

/* Returns an NSArray of NSURLs locating the mounted volumes available on the computer. The property keys that can be requested are available in NSURL.
*/
/// Returns an array of URLs that identify the mounted volumes available on the device.
open func mountedVolumeURLs(includingResourceValuesForKeys propertyKeys: [URLResourceKey]?, options: VolumeEnumerationOptions = []) -> [URL]? {
NSUnimplemented()
var urls: [URL]

#if os(Linux)
guard let procMounts = try? String(contentsOfFile: "/proc/mounts", encoding: .utf8) else {
return nil
}
urls = []
for line in procMounts.components(separatedBy: "\n") {
let mountPoint = line.components(separatedBy: " ")
if mountPoint.count > 2 {
urls.append(URL(fileURLWithPath: mountPoint[1], isDirectory: true))
}
}
#elseif canImport(Darwin)

func mountPoints(_ statBufs: UnsafePointer<statfs>, _ fsCount: Int) -> [URL] {
var urls: [URL] = []

for fsIndex in 0..<fsCount {
var fs = statBufs.advanced(by: fsIndex).pointee

if options.contains(.skipHiddenVolumes) && fs.f_flags & UInt32(MNT_DONTBROWSE) != 0 {
continue
}

let mountPoint = withUnsafePointer(to: &fs.f_mntonname.0) { (ptr: UnsafePointer<Int8>) -> String in
return string(withFileSystemRepresentation: ptr, length: strlen(ptr))
}
urls.append(URL(fileURLWithPath: mountPoint, isDirectory: true))
}
return urls
}

if #available(OSX 10.13, *) {
var statBufPtr: UnsafeMutablePointer<statfs>?
let fsCount = getmntinfo_r_np(&statBufPtr, MNT_WAIT)
guard let statBuf = statBufPtr, fsCount > 0 else {
return nil
}
urls = mountPoints(statBuf, Int(fsCount))
free(statBufPtr)
} else {
var fsCount = getfsstat(nil, 0, MNT_WAIT)
guard fsCount > 0 else {
return nil
}
let statBuf = UnsafeMutablePointer<statfs>.allocate(capacity: Int(fsCount))
defer { statBuf.deallocate() }
fsCount = getfsstat(statBuf, fsCount * Int32(MemoryLayout<statfs>.stride), MNT_WAIT)
guard fsCount > 0 else {
return nil
}
urls = mountPoints(statBuf, Int(fsCount))
}
#else
#error("Requires a platform-specific implementation")
#endif
return urls
}

/* Returns an NSArray of NSURLs identifying the the directory entries.
Expand Down
19 changes: 19 additions & 0 deletions TestFoundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TestFileManager : XCTestCase {
("test_homedirectoryForUser", test_homedirectoryForUser),
("test_temporaryDirectoryForUser", test_temporaryDirectoryForUser),
("test_creatingDirectoryWithShortIntermediatePath", test_creatingDirectoryWithShortIntermediatePath),
("test_mountedVolumeURLs", test_mountedVolumeURLs)
]
}

Expand Down Expand Up @@ -601,4 +602,22 @@ class TestFileManager : XCTestCase {
XCTFail("Unable to write a file to the temporary directory: \(tmpDir), err: \(error)")
}
}

func test_mountedVolumeURLs() {
guard let volumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys:[], options: []) else {
XCTFail("mountedVolumeURLs returned nil")
return
}
XCTAssertNotEqual(0, volumes.count)
XCTAssertTrue(volumes.contains(URL(fileURLWithPath: "/")))
#if os(macOS)
// On macOS, .skipHiddenVolumes should hide 'nobrowse' volumes of which there should be at least one
guard let visibleVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: [], options: [.skipHiddenVolumes]) else {
XCTFail("mountedVolumeURLs returned nil")
return
}
XCTAssertTrue(visibleVolumes.count > 0)
XCTAssertTrue(visibleVolumes.count < volumes.count)
#endif
}
}