[SWIFT] Afficher les informations du système de fichiers pour les volumes montés

Aperçu

référence

résultat

――Je veux voir diverses choses, donc je l'exécute avec certains appareils connectés ou connectés au serveur. --Il peut être confirmé que le dossier racine / ne peut pas être écrit à partir de macOS 11.0 pour les raisons suivantes.

macOS 11.0

********* 
/  /*Dossier racine*/
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  /*Clé USB*/
is removal media
is writable
is unmountable
msdos msdos
********* 
/Volumes/<Nom du serveur>  /*Serveur connecté à smb*/
is not removal media
is writable
is unmountable
smbfs smbfs
********* 
/Volumes/<Nom du DVD>  /* DVD */
is removal media
is not writable
is unmountable
hfs hfs

macOS 10.15

********* 
/  /*Dossier racine*/
is not removal media
is writable
is not unmountable
apfs apfs
********* 
/Volumes/<Nom du serveur>  /*Serveur connecté à smb*/
is not removal media
is writable
is unmountable
smbfs smbfs
********* 
/Volumes/Untitled  /*Clé USB*/
is removal media
is writable
is unmountable
msdos msdos

la mise en oeuvre

printVolumesInfo()

private func printVolumesInfo() {
    guard let volumeUrls = FileManager.default.mountedVolumeURLs(
            includingResourceValuesForKeys: nil,
            options: FileManager.VolumeEnumerationOptions.skipHiddenVolumes) else {
        return
    }
    
    for volumeUrl in volumeUrls {
        //Référence du pointeur dans Swift
        // [Manipulation des pointeurs C pour Cocoa Part 3\[Swift\] \-Explication de l'API Cocoa\(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?>Avoir
        // [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

Afficher les informations du système de fichiers pour les volumes montés