Search code examples
objective-ccmacoscocoahard-drive

Mac analyzing disk for free sectors


I need to read hard disk sectors. Specifically, I am trying to write a function as follow:

findFreeBlocks - analyze disk structures and find two uninterrupted free blocks of a given length.

int findFreeBlocks(char * drivePath, DWORD sectorsNeeded, u_int64_t *freeSector1, u_int64_t *freeSector2)

Parameters: [IN] drivePath – path to a logical drive [IN] sectorsNeeded – length of needed free block in sectors (sector = 512 bytes) [OUT] freeSector1 – the first sector of the first found free block [OUT] freeSector2 – the first sector of the second found free block

Return: 0 – if success, error code – if error

Can someone point me APIs that will help me do this? I have experience with Objective-c but C/C++ can work too (and I am guessing it will be that since this is very low level). Thanks


Solution

  • As @PlasmaHH points out, an operating system like MacOS isn't going to be offering you an Objective C or C++ API to deal with sectors and tracks on a physical disk. (Nor will it let you alter the speed of the motor inside the drive, or move the drive head to the center or outside of the plates.)

    If you want to write "low level" code, like a defragmenter or similar, you'll have to know which file system you are dealing with. Mac's default HFS+ does not have a lot of open-source tools for working with it. There are some notes on the structures:

    http://developer.apple.com/library/mac/#technotes/tn/tn1150.html

    ...a tool called hfsdebug is out there, which has been deprecated in favor of fileXray:

    Link

    (No source code for those, though. That someone who purports to be an educator yet ships his "Mac OS/X Internals" book with a closed-source tool speaks volumes [pun intended] about the culture of Apple development. Depressing that I could actually make money writing a book called "Mac OS/X Internals Internals".)

    Long story short: if you want to mess with the file system you're going to be stuck with working with raw IO on the block device at /dev/rdisk*. There's no library. It's almost certainly not worth your time, go do something cool with Linux instead. :)