On Mac OS X, there is a daemon called diskarbitrationd that can notifiy interested clients of the appearance of disks and filesystems. Users talk to diskarbitrationd via the Disk Arbitration framework.
For some reason, the current developer documentation doesn’t have much to say about this framework. It therefore appears to be necessary to grovel through the headers to figure out how to use it. Fortunately, the headers are well-commented.
I was able to throw together a trivial program in about 15 or 20 minutes from first looking at DiskArbitration.h. Maybe this will help someone out there.
#include <stdio.h>
#include <DiskArbitration/DiskArbitration.h>
void hello_disk(DADiskRef disk, void *context)
{
printf("disk %s appeared\n", DADiskGetBSDName(disk));
}
void goodbye_disk(DADiskRef disk, void *context)
{
printf("disk %s disappeared\n", DADiskGetBSDName(disk));
}
main()
{
DASessionRef session;
session = DASessionCreate(kCFAllocatorDefault);
DARegisterDiskAppearedCallback(session, NULL, hello_disk, NULL);
DARegisterDiskDisappearedCallback(session, NULL, goodbye_disk, NULL);
DASessionScheduleWithRunLoop(session,
CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRunLoopRun();
CFRelease(session);
exit(0);
}
Sorry to ask such a basic question, but I am new to XCode and the Mac OS X environment. I’m trying to build your code snippet above, and am getting:
[Session started at 2007-09-05 21:14:58 -0700.]
ZeroLink: unknown symbol ‘_DASessionCreate’
DiskInsertion has exited due to signal 6 (SIGABRT).
I tried additional includes…
#include
#include
Any tips would be greatly appreciated. Trying to build a simple daemon to perform an action when a particular volume is mounted… and your code is a good example. Unfortunately I am unable to really get started.
Thanks.
rdw
You need to add the DiskArbitration.framework to your Xcode project.
See this part of the Xcode documentation.
If you compile from the command-line, you’d say
cc foo.m -framework DiskArbitration -framework Foundation.you wrote: “Maybe this will help somebody out there”
I would say that this it so perfectly what I was looking for that it is almost scary.
Thank you!