Mount/Unmount events in Foundation tool

  • I have a Foundation tool (daemon) that I would very much like to get volume mount and unmount events. I can't get NSWorkspaceDidMountNotification because I can't use NSWorkspace.

    Is there some other event I can subscribe to, or is there a way of subscribing to Carbon events for the same action from within a Cocoa app? I already checked NSDistributedNotificationCenter, and it doesn't fire any useful events on volume mount/unmount.

    --
    James Bucanek
  • On 25/01/07, James Bucanek <subscriber...> wrote:
    > I have a Foundation tool (daemon) that I would very much like to get volume mount and unmount events. I can't get NSWorkspaceDidMountNotification because I can't use NSWorkspace.
    >
    > Is there some other event I can subscribe to, or is there a way of subscribing to Carbon events for the same action from within a Cocoa app? I already checked NSDistributedNotificationCenter, and it doesn't fire any useful events on volume mount/unmount.

    You might want to look at the DiskArbitration framework.

    -- Finlay
  • Finlay Dobbie wrote on Thursday, January 25, 2007:

    > On 25/01/07, James Bucanek <subscriber...>  wrote:
    >> I have a Foundation tool (daemon) that I would very much like to get volume mount and unmount events. I can't get
    > NSWorkspaceDidMountNotification because I can't use NSWorkspace.
    >>
    >> Is there some other event I can subscribe to, or is there a way of subscribing to Carbon events for the same action from within a Cocoa
    > app? I already checked NSDistributedNotificationCenter, and it doesn't fire any useful events on volume mount/unmount.
    >
    > You might want to look at the DiskArbitration framework.

    I looked at Disk Arbitration, but didn't see any kind of event support. There were functions that looked like they would approve or block volumes from mounting, and functions for getting information about devices, but nothing that looked like a mount/unmount notification. I say "looked like" because the documentation for Disk Arbitration can only politely be described as "minimal."

    --
    James Bucanek
  • On 26/01/2007, at 11:30 AM, James Bucanek wrote:

    > Finlay Dobbie wrote on Thursday, January 25, 2007:
    >
    >> On 25/01/07, James Bucanek <subscriber...> wrote:
    >>> I have a Foundation tool (daemon) that I would very much like to
    >>> get volume mount and unmount events. I can't get
    >> NSWorkspaceDidMountNotification because I can't use NSWorkspace.
    >>>
    >>> Is there some other event I can subscribe to, or is there a way
    >>> of subscribing to Carbon events for the same action from within a
    >>> Cocoa
    >> app? I already checked NSDistributedNotificationCenter, and it
    >> doesn't fire any useful events on volume mount/unmount.
    >>
    >> You might want to look at the DiskArbitration framework.
    >
    > I looked at Disk Arbitration, but didn't see any kind of event
    > support. There were functions that looked like they would approve
    > or block volumes from mounting, and functions for getting
    > information about devices, but nothing that looked like a mount/
    > unmount notification. I say "looked like" because the documentation
    > for Disk Arbitration can only politely be described as "minimal."

    Look at the source code for disktool.

    http://www.opensource.apple.com/darwinsource/10.4.8.ppc/
    DiskArbitration-156/disktool/disktool.c
  • On Jan 25, 2007, at 6:31 PM, James Bucanek wrote:

    > I have a Foundation tool (daemon) that I would very much like to
    > get volume mount and unmount events. I can't get
    > NSWorkspaceDidMountNotification because I can't use NSWorkspace.
    >
    > Is there some other event I can subscribe to, or is there a way of
    > subscribing to Carbon events for the same action from within a
    > Cocoa app? I already checked NSDistributedNotificationCenter, and
    > it doesn't fire any useful events on volume mount/unmount.

    It would appear to be necessary to grovel through the DiskArbitration
    framework headers.

    It looks pretty straightforward, though.  I was able to throw
    together a trivial program in about 15 or 20 minutes from first
    looking at DiskArbitration.h.  Maybe it will help get you going.

    #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);
    }
  • R. Matthew Emerson wrote on Thursday, January 25, 2007:
    > It would appear to be necessary to grovel through the DiskArbitration
    > framework headers.

    It would -- something I don't normally think about.

    The lack of documentation here is somewhat distressing. Are these considered internal or private APIs in any way? Your example uses the DARegisterDiskAppearedCallback() function. On a whim, I searched developer.apple.com for DARegisterDiskAppearedCallback and got 0 hits. So Apple apparently has no documentation or example code that uses this function.

    > It looks pretty straightforward, though.  I was able to throw
    > together a trivial program in about 15 or 20 minutes from first
    > looking at DiskArbitration.h.  Maybe it will help get you going.

    It did indeed, thanks! I now have a registered DADiskAppearedCallback and my Cocoa tool is merrily getting disk mount events. Very nice.

    Chris Suter wrote on Friday, January 26, 2007:

    Actually, this turned out to be more confusing than helpful. disktool never registers to get notifications of disk mount/unmount events (which makes sense, since it's a transient command line tool).

    --
    James Bucanek
  • > Chris Suter wrote on Friday, January 26, 2007:
    >> Look at the source code for disktool.
    >>
    >> http://www.opensource.apple.com/darwinsource/10.4.8.ppc/
    >> DiskArbitration-156/disktool/disktool.c
    >
    > Actually, this turned out to be more confusing than helpful.
    > disktool never registers to get notifications of disk mount/unmount
    > events (which makes sense, since it's a transient command line tool).

    It does. Try typing disktool -y.
  • Am 26.01.2007 um 00:31 schrieb James Bucanek:
    > I have a Foundation tool (daemon) that I would very much like to
    > get volume mount and unmount events. I can't get
    > NSWorkspaceDidMountNotification because I can't use NSWorkspace.
    >
    > Is there some other event I can subscribe to, or is there a way of
    > subscribing to Carbon events for the same action from within a
    > Cocoa app? I already checked NSDistributedNotificationCenter, and
    > it doesn't fire any useful events on volume mount/unmount.

    There are CarbonEvents for that, which can be used from a Cocoa app.
    Though you'd have to try them out to know whether they work in your
    particular command line tool. I think you'd need a run loop of sorts
    for those events to be dispatched, and NSApplication dispatches
    Carbon events during normal event processing. Not sure whether that
    is because it uses NSRunLoop, or what, though.

    The Carbon Events are of class

    kEventClassVolume

    and have the following constants as their names:

    kEventVolumeMounted
    kEventVolumeUnmounted

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
  • On 26/01/07, Uli Kusterer <witness.of.teachtext...> wrote:
    > Am 26.01.2007 um 00:31 schrieb James Bucanek:
    >> I have a Foundation tool (daemon) that I would very much like to
    >> get volume mount and unmount events. I can't get
    >> NSWorkspaceDidMountNotification because I can't use NSWorkspace.
    >>
    >> Is there some other event I can subscribe to, or is there a way of
    >> subscribing to Carbon events for the same action from within a
    >> Cocoa app? I already checked NSDistributedNotificationCenter, and
    >> it doesn't fire any useful events on volume mount/unmount.
    >
    > There are CarbonEvents for that, which can be used from a Cocoa app.

    Carbon is not daemon-safe. Disk Arbitration is.

    -- Finlay
  • On 26/01/07, James Bucanek <subscriber...> wrote:
    > R. Matthew Emerson wrote on Thursday, January 25, 2007:
    >> It would appear to be necessary to grovel through the DiskArbitration
    >> framework headers.
    >
    > It would -- something I don't normally think about.
    >
    > The lack of documentation here is somewhat distressing. Are these considered internal or private APIs in any way? Your example uses the DARegisterDiskAppearedCallback() function. On a whim, I searched developer.apple.com for DARegisterDiskAppearedCallback and got 0 hits. So Apple apparently has no documentation or example code that uses this function.

    If they are in public headers, then they're public API. There is some
    HeaderDoc documentation in the header, and it's probably a bug that
    this has not been pushed out to developer.apple.com - file a bug.

    -- Finlay
previous month january 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today