tags:

views:

311

answers:

1

Hi guys

I have a situation in which my .dmg file will on my removable storage device which contains my application. when i double click on it, it will get mounted on my local machine and inside the mounted volume will be my .app (aplication file). Now I want my application to auto launch once my dmg file is mounted on my local machine. Also now my app needs he info about where the actual dmg file is present like its path on the removable storage device. Is this possible and if so how do i find out the path of the dmg file from which the volume is mounted.

Thanks

+2  A: 
  1. Automatically launching an application is not possible in Mac OS X. There are some safety reasons against it. The only thing that can be automatically launched is a .pkg file and this only through Safari AFAIK.

  2. It is possible to determine the DMG file the application resides on. You have to use IOKit for this. Try playing around with IORegistryExplorer.

Some code that may help you

Those are my first attempts on using IOKit, it's for another purpose but it should help non-the-less.

// hopefully all needed headers
#include <sys/stat.h>

#include <IOKit/IOKitLib.h>
#include <IOKit/IOBSD.h>
#include <CoreFoundation/CoreFoundation.h>

/* First we want to get the major and minor BSD number
 * of the DMG that our app is residing on.
 *
 * char *path is the path of a file that resides on the disk image.
 * It is like this: /Volumes/Partition Name/SomeFile
 * The simplest method to get such a path is to ask
 * NSBundle for the path of the executable.
 */

// look up device number with stat
char *path = "path/to/app";

struct stat stats;
if (stat(path, &stats) != 0) {
    return;
}
int bsd_major = major(stats.st_dev);
int bsd_minor = minor(stats.st_dev);

/* Now that we've got the BSD numbers we have to locate the
 * IOService that has those numbers. IOKit works with
 * CoreFoundation types.
 */

CFTypeRef keys[2] = { CFSTR(kIOBSDMajorKey), CFSTR(kIOBSDMinorKey) };
CFTypeRef values[2];
values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &bsd_major);
values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &bsd_minor);

CFDictionaryRef matchingDictionary;
matchingDictionary = CFDictionaryCreate(kCFAllocatorDefault,
                                        &keys, &values,
                                        sizeof(keys) / sizeof(*keys),
                                        &kCFTypeDictionaryKeyCallBacks,
                                        &kCFTypeDictionaryValueCallBacks);

CFRelease(values[0]);
CFRelease(values[1]);
// IOServiceGetMatchingService uses up one reference to the dictionary
io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
                                                   matchingDictionary);

if (!service) {
    return;
}

/* Now this part is quite different from what I need
 * for my application. I'm not sure how this works
 * because I'm currently not at my Mac and cannot try it.
 * 
 * You need to go up the IOService chain. It looks like this:
  +-o IOHDIXHDDriveOutKernelUserClient
    +-o IODiskImageBlockStorageDeviceOutKernel   <---- You want to get up here
      +-o IOBlockStorageDriver
        +-o Apple UDIF read-only compressed (zlib) Media
          +-o IOMediaBSDClient
          +-o IOApplePartitionScheme
            +-o Apple@1
            | +-o IOMediaBSDClient
            +-o disk image@2               <---- This is the matched IOService!
              +-o IOMediaBSDClient
 *
 * IODiskImage... has a property "Protocol Characteristics" which is a
 * dictionary that has the key "Virtual Interface Location Path" which is  
 * the path to the disk image. There are probably #defines somewhere in
 * IOKit for those keys.
 *
 * This code is NOT tested. It's out of my head and the documentation.
 * This goes up 4 times in the hierarchy. Hopefully there aren't more
 * than 1 parents.
 */

for (int i = 0; i < 4; i++) {
    io_service_t parent;
    IORegistryEntryGetParentEntry(service, kIOServicePlane, &parent);
    IOObjectRelease(service);
    service = parent;
}

 /* Getting the property from the IOService is the last step:
  */

 CFDictionaryRef characteristics;
 characteristics = (CFDictionaryRef)IORegistryEntryCreateCFProperty(service,
                                          CFSTR("Protocol Characteristics"),
                                          kCFAllocatorDefault, 0)

 CFStringRef *dmgPath = CFDictionaryGetValue(characteristics,
                                    CFSTR("Virtual Interface Location Path"));
 // clean up
 IOObjectRelease(service);
 CFRetain(dmgPath);
 CFRelease(characteristics);

 // Use the path

 // later
 CFRelease(dmgPath);

Much of this can be done using the Foundation classes instead of the CoreFoundation classes because of the toll-free bridging support. This makes it a little bit easier and more legible.

Georg
Thanks gs for the reply. Please can you be more specific about where i can get good examples on IOKit like books and stuff. Currently i am going through examples given in /developer/iokit/examples.Thanks
King
Unfortunately there aren't any good examples. I'll try to include some of my own code in the answer.
Georg
This was really helpful, although it turned out not to work for me on Tiger - the call to IORegistryEntryGetParentEntry gets a kIOReturnNoDevice. Great on Snow Leopard, though.
invalidname
@invalidname: Then look the hierarchy up using the *ioreg* or the corresponding GUI tool. (Can't recall the name at the moment, but it's located in the developers tool folder.)
Georg
I actually ended up calling "hdiutil info -plist" from an NSTask and converting the output into an NSDictionary that I could then walk. Works on 10.4-6.
invalidname