How can I get disk Infos?

Mac OSX specific forum
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

How can I get disk Infos?

Post by Wolfram »

At the moment I use diskutil info to get the media name and size of all disks.
Also disk which are not formatted or in a unknown format.

Is there a way to do it with the FileManager or something similar?
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How can I get disk Infos?

Post by wilbert »

Did you see this post from Shardik ?
viewtopic.php?p=415435#p415435
Windows (x64)
Raspberry Pi OS (Arm64)
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: How can I get disk Infos?

Post by Wolfram »

[quote="wilbert"]Did you see this post from Shardik ?

No, I didn't see it, but I tried similar code yesterday and could find a way to list disks that are not in a read able format.

Code: Select all

 CocoaMessage(0, FileManager, "mountedVolumeURLsIncludingResourceValuesForKeys:", KeyArray, "options:", 0)
lists disk which are already read able only, but not unformatted or unknown formats.
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How can I get disk Infos?

Post by wilbert »

Maybe you can use the DiskArbitration framework.

This uses DADiskCreateFromBSDName
https://www.purebasic.fr/german/viewtop ... 43#p328843

It might be possible you need the IOKit framework as well and use DADiskCreateFromIOMedia instead of DADiskCreateFromBSDName but I'm not sure.
Windows (x64)
Raspberry Pi OS (Arm64)
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: How can I get disk Infos?

Post by Wolfram »

This works, but I wasn't able to get other objects for key.

How can I get the size of the device or partitions?
macOS Catalina 10.15.7
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How can I get disk Infos?

Post by Shardik »

Wolfram wrote:This works, but I wasn't able to get other objects for key.

How can I get the size of the device or partitions?
I have just posted this example in the German forum.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How can I get disk Infos?

Post by wilbert »

Nice example Shardik.

This is what I came up with (not as extended as your code).

Code: Select all

ImportC "-framework DiskArbitration"
  DADiskCopyDescription(disk)
  DADiskCreateFromBSDName(allocator, session, name.p-ascii)
  DASessionCreate(allocator)
EndImport

Procedure.s GetStringForKey(Dictionary, Key.s)
  Protected.i Object, UTF8String
  Object = CocoaMessage(0, Dictionary, "objectForKey:$", @Key)
  If Object
    UTF8String = CocoaMessage(0, Object, "UTF8String")
    If UTF8String
      ProcedureReturn PeekS(UTF8String, -1, #PB_UTF8)
    EndIf
  EndIf
  ProcedureReturn #Null$
EndProcedure

Procedure.q GetQuadValueForKey(Dictionary, Key.s)
  Protected.i Value, ValueQ.q
  Value = CocoaMessage(0, Dictionary, "valueForKey:$", @Key)
  If Value
    CocoaMessage(@ValueQ, Value, "longLongValue")
    ProcedureReturn ValueQ
  EndIf
  ProcedureReturn #Null
EndProcedure



Session = DASessionCreate(#Null)
If Session
  DiskNumber = 0
  Repeat
    PartitionNumber = 1
    Repeat
      DiskName.s = "/dev/disk"+DiskNumber+"s"+PartitionNumber
      Disk = DADiskCreateFromBSDName(#Null, Session, DiskName)
      If Disk
        Dictionary = DADiskCopyDescription(Disk)
        If Dictionary
          Debug "BSDName: " + DiskName
          Debug "Device model: " + GetStringForKey(Dictionary, "DADeviceModel")
          Debug "Media name: " + GetStringForKey(Dictionary, "DAMediaName")
          Debug "Media size: " + GetQuadValueForKey(Dictionary, "DAMediaSize")
          Debug "Volume name: " + GetStringForKey(Dictionary, "DAVolumeName")
          Debug "Volume type: " + GetStringForKey(Dictionary, "DAVolumeType")
          Debug LSet("", 50, "-")
          CFRelease_(Dictionary)
          PartitionNumber + 1
        EndIf
        CFRelease_(Disk)
      EndIf
    Until Dictionary = #Null
    Disknumber + 1
  Until PartitionNumber = 1
  CFRelease_(Session)
EndIf
Some partitions return the same very large number for DAMediaSize.
I have no idea why. :?
Windows (x64)
Raspberry Pi OS (Arm64)
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: How can I get disk Infos?

Post by Wolfram »

Thank you both :D
macOS Catalina 10.15.7
Post Reply