Re: Diskarbitration callback
Re: Diskarbitration callback
- Subject: Re: Diskarbitration callback
- From: Axel Luttgens <email@hidden>
- Date: Thu, 8 May 2008 17:58:24 +0200
Le 8 mai 08 à 10:21, Andy Green a écrit :
I've written some code which uses the diskArbitration framework to
unmount (and eject) a disk.
It works, in that the disk gets unmounted, but my callback isn't being
called, even though the operation is successful. Any chance of
spotting
the mistake in this code fragment? :-
Cheers,
Andy
8<----
struct Nothing
{
bool dummy;
};
static void AndygDiskArbCallback( DADiskRef disk, DADissenterRef
dissenter, void * context)
{
printf("If I ever see this, my problems are over\n");
Nothing * n = (Nothing *)context;
n->dummy = true;
// I want to give a semaphore here to unlock the main thread
}
int main(int argc, char *argv[])
{
const char * cStr = "disk0s2";
Nothing * n = new Nothing;
n->dummy = false;
DASessionRef diskArbSession = DASessionCreate(kCFAllocatorDefault);
assert(diskArbSession);
DADiskRef disk = DADiskCreateFromBSDName( kCFAllocatorDefault,
diskArbSession, cStr );
assert(disk);
printf("confirming %s == %s??\n", DADiskGetBSDName(disk), cStr);
DADiskRef whole = DADiskCopyWholeDisk( disk );
if ( whole )
{
printf("wholedisk == %s\n", DADiskGetBSDName(whole));
DADiskUnmount(whole, kDADiskUnmountOptionWhole |
kDADiskUnmountOptionForce, AndygDiskArbCallback, n);
CFRelease(whole);
}
printf("------Wait...");
sleep(1); // instead of sleep, I want to wait on a semaphore
here,
until the unmount is complete.
// unfortunately the callback isn't being called!
// I'll do this too, once I get the callback from Unmount...
// DADiskEject( disk, kDADiskEjectOptionDefault,
AndygDiskArbCallback, NULL );
CFRelease(disk);
CFRelease(diskArbSession);
printf("AT THE END OF IT ALL, n=%d\n", n->dummy);
delete(n);
}
Hello Andy,
You'll find hereafter an amended version of your code.
For legibility purposes, I kept it very verbose and as as close as
possible to your own version.
But please check and double-check! I wrote it very quickly and am not
very well versed myself into those matters. ;-)
HTH,
Axel
#include <stdio.h>
#include <DiskArbitration/DiskArbitration.h>
void AndygDiskArbCallback( DADiskRef disk, DADissenterRef dissenter,
void * context);
typedef struct
{
int dummy;
} Nothing;
void AndygDiskArbCallback( DADiskRef disk, DADissenterRef dissenter,
void * context)
{
printf("If I ever see this, my problems are over\n");
if (dissenter == NULL)
((Nothing *)context)->dummy = 0;
else
((Nothing *)context)->dummy = DADissenterGetStatus(dissenter) ?
DADissenterGetStatus(dissenter) : -1;
// I want to give a semaphore here to unlock the main thread
CFRunLoopStop(CFRunLoopGetCurrent());
}
int main(int argc, char *argv[])
{
const char * cStr;
static Nothing * n;
DASessionRef diskArbSession;
DADiskRef disk;
DADiskRef whole;
SInt32 rc;
cStr = "disk1s1";
n = (Nothing *) malloc(sizeof(Nothing));
n->dummy = -1;
diskArbSession = NULL;
disk = NULL;
whole = NULL;
rc = -1;
diskArbSession = DASessionCreate(kCFAllocatorDefault);
if (diskArbSession != NULL)
{
DASessionScheduleWithRunLoop(diskArbSession, CFRunLoopGetCurrent(),
CFSTR("drvHostBaseDA"));
disk = DADiskCreateFromBSDName(kCFAllocatorDefault, diskArbSession,
cStr);
if (disk != NULL)
{
printf("Got disk bearing BSD name '%s' starting with string '%s'.
\n", DADiskGetBSDName(disk), cStr);
whole = DADiskCopyWholeDisk(disk);
if (whole != NULL)
{
printf("Going to unmount whole disk with BSD name '%s'.\n",
DADiskGetBSDName(whole));
DADiskUnmount(whole, kDADiskUnmountOptionWhole |
kDADiskUnmountOptionForce, AndygDiskArbCallback, n);
rc = CFRunLoopRunInMode(CFSTR("drvHostBaseDA"), 10.0, TRUE);
if (rc == kCFRunLoopRunHandledSource)
{
printf("Something happened.\n");
if (n->dummy == 0)
{
printf("Unmount has been successful.\n");
// I'll do this too, once I get the callback from Unmount...
// DADiskEject( disk, kDADiskEjectOptionDefault,
AndygDiskArbCallback, NULL );
}
else
{
printf("Unmount failed with code '%d'.\n", n->dummy);
}
}
CFRelease(whole);
}
CFRelease(diskArbSession);
}
DASessionUnscheduleFromRunLoop(diskArbSession,
CFRunLoopGetCurrent(), CFSTR("drvHostBaseDA"));
CFRelease(disk);
}
free(n);
return(0);
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden