[SWIFT] View file system information for mounted volumes

Overview

--Get a list of mounted volumes and display information for each file system. --File system information in NSWorkspace getFileSystemInfo \ (forPath: isRemovable: isWritable: isUnmountable: description: type: ) To get it. --Also get a list of mounted volumes at FileManager at mountedVolumeURLs \ (includingResourceValuesForKeys: options: ). ..

reference

-[Handling of C pointer for Cocoa Part 3 \ [Swift ] -Cocoa API explanation \ (macOS / iOS )](https://cocoaapi.hatenablog.com/entry/2015/04/16/Cocoa % E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AEC% E3% 83% 9D% E3% 82% A4% E3% 83% B3% E3% 82% BF% E3 % 81% AE% E5% 8F% 96% E3% 82% 8A% E6% 89% B1% E3% 81% 84% E3% 81% 9D% E3% 81% AE3)

result

――I want to see various things, so I'm running it with some devices connected or connected to the server. --It can be confirmed that the root folder / cannot be written from macOS 11.0 due to the following. -About read-only system volumes for macOS Catalina --This volume is read-only," Macintosh HD ": / --Files and data are "Macintosh HD --Data" :/ System / Volumes / Data /

macOS 11.0

********* 
/  /*Root folder*/
is not removal media
is not writable
is not unmountable
apfs apfs
********* 
/Volumes/BOOTCAMP
is not removal media
is writable
is not unmountable
ntfs ntfs
********* 
/Volumes/Untitled 1  /*USB memory*/
is removal media
is writable
is unmountable
msdos msdos
********* 
/Volumes/<Server name>  /*smb connected server*/
is not removal media
is writable
is unmountable
smbfs smbfs
********* 
/Volumes/<DVD name>  /* DVD */
is removal media
is not writable
is unmountable
hfs hfs

macOS 10.15

********* 
/  /*Root folder*/
is not removal media
is writable
is not unmountable
apfs apfs
********* 
/Volumes/<Server name>  /*smb connected server*/
is not removal media
is writable
is unmountable
smbfs smbfs
********* 
/Volumes/Untitled  /*USB memory*/
is removal media
is writable
is unmountable
msdos msdos

Implementation

printVolumesInfo()

private func printVolumesInfo() {
    guard let volumeUrls = FileManager.default.mountedVolumeURLs(
            includingResourceValuesForKeys: nil,
            options: FileManager.VolumeEnumerationOptions.skipHiddenVolumes) else {
        return
    }
    
    for volumeUrl in volumeUrls {
        //Pointer reference in Swift
        // [Handling of C pointers for Cocoa Part 3\[Swift\] \-Cocoa API explanation\(macOS/iOS\)](https://cocoaapi.hatenablog.com/entry/2015/04/16/Cocoa%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AEC%E3%83%9D%E3%82%A4%E3%83%B3%E3%82%BF%E3%81%AE%E5%8F%96%E3%82%8A%E6%89%B1%E3%81%84%E3%81%9D%E3%81%AE3)
        var isRemoval = ObjCBool(false)
        var isWritable = ObjCBool(false)
        var isUnmountable = ObjCBool(false)
        
        // AutoreleasingUnsafePointer<NSString?>Get
        // [Swift: How to get the value from a AutoreleasingUnsafePointer<NSString?> from a NSScanner?](https://stackoverflow.com/questions/24205768/swift-how-to-get-the-value-from-a-autoreleasingunsafepointernsstring-from-a/24206064#24206064)
        var description: NSString? = nil
        var type: NSString? = nil
        
        NSWorkspace.shared.getFileSystemInfo(forPath: volumeUrl.path,
                                             isRemovable: &isRemoval,
                                             isWritable: &isWritable,
                                             isUnmountable: &isUnmountable,
                                             description: &description,
                                             type: &type)
        
        let descriptionForPrint = description as String?
        let typeForPrint = type as String?
        var diskInfo = ""
        if let descriptionForPrint = descriptionForPrint {
            diskInfo = diskInfo.appending(descriptionForPrint)
        }
        if let typeForPrint = typeForPrint {
            diskInfo = diskInfo.appendingFormat("%@%@", diskInfo.isEmpty ? "" : ", " , typeForPrint)
        }
        print(
            """
*******
\(volumeUrl.path)
\(FileManager.default.displayName(atPath: volumeUrl.path))
\(isRemoval.boolValue ? "is removal media" : "is not removal media")
\(isWritable.boolValue ? "is writable" : "is not writable")
\(isUnmountable.boolValue ? "is unmountable" : "is not unmountable")
\(diskInfo)
""")
        
    }
}

Recommended Posts

View file system information for mounted volumes