Page 1 of 1
How can I get disk Infos?
Posted: Wed Sep 09, 2020 3:36 pm
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?
Re: How can I get disk Infos?
Posted: Wed Sep 09, 2020 4:49 pm
by wilbert
Did you see this post from Shardik ?
viewtopic.php?p=415435#p415435
Re: How can I get disk Infos?
Posted: Wed Sep 09, 2020 5:35 pm
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.
Re: How can I get disk Infos?
Posted: Thu Sep 10, 2020 12:09 pm
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.
Re: How can I get disk Infos?
Posted: Thu Sep 10, 2020 2:58 pm
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?
Re: How can I get disk Infos?
Posted: Thu Sep 10, 2020 7:29 pm
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.
Re: How can I get disk Infos?
Posted: Thu Sep 10, 2020 8:13 pm
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.
Re: How can I get disk Infos?
Posted: Thu Sep 10, 2020 8:45 pm
by Wolfram
Thank you both