I'm doing some fairly low level work with OCaml and need to get a list of all of the block-level files that exist on a given Linux machine. My current attempt involves running the following and then parsing based on sd* from there.
Sys.readdir "/dev/";;
But I am wondering if there is an existing/better way to do this? Since block devices have a ton of different names I'll have to write a ton of regexp. Is there a way that would let me specify that I only want block device files?
Note: Unfortunately I am unable to use Jane Street Core, or any other libraries for this.
The
Unixmodule has a functionUnix.statwhich returns a record of typeUnix.statsdescribing a file. Thest_kindfield is of typeUnix.file_kindwhich has a constructorS_BLKif a device is a block level device.So, you could write something like the following to get a list of block level devices without relying on the filenames themselves to make that determination.
This could be wrapped up into a function to allow easily reading block devices from any directory.