About DADiskMountApprovalCallback double callback…

Some time ago I made a post on an issue I was having with DiskArbitration. I was registering to DADiskMountApprovalCallback to detect when an disk image was mounted, but each time the callback was called twice, so I did a little hack to ignore the second call.

A few days ago, Aaron Burghardt posted a comment to say that my code contained a possible race condition and that I should instead register to DADiskDescriptionChangedCallback with the kDADiskDescriptionWatchVolumePath parameter.

So I tried what he said, and it works perfectly well, so I want to share the code here.

void description_changed(DADiskRef disk, CFArrayRef keys, void* ctx)
{
	NSDictionary* dic = DADiskCopyDescription(disk);
	// Do stuff
	[dic release];
}
// Code
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DARegisterDiskDescriptionChangedCallback(session, NULL, kDADiskDescriptionWatchVolumePath, description_changed, NULL);
// Code

In the end, it’s a win-win, because it’s less code and it’s safer.

So, a big thank to Aaron Burghardt for suggesting me this method.

DiskArbitration callback issue

I’m currently coding an utility that need the DiskArbitration framework. I use this framework to be notified each time a disk image (.dmg) is mounted on the filesystem by registering to DADiskMountApprovalCallback with the DARegisterDiskMountApprovalCallback() function. If you are interested in how these functions work you can read one of my previous post in which I talk about this.
Continue reading

Coding an Apple compliant Daemon

In a precedent article I was showing you how to prevent a disk from mounting on the file system, at the end of the artcile I said that it would probably be best if this kind of program could run as a daemon, so this is what I’ll show you in this post, to begin let’s take a look at the precedent code :
Continue reading

Prevent a disk from mounting on the filesystem

Today I will show you how to prevent a disk (physical or virtual) to mount on the filesystem.
In order to do that, we will use the DiskArbitration framework. There is very few documentation for this framework, the best is to read the header files, which are very well commented.
Continue reading