可可 + dmg 文件 +追溯它的路径

发布于 2024-08-12 18:06:26 字数 219 浏览 2 评论 0原文

我遇到的情况是,我的 .dmg 文件将位于包含我的应用程序的可移动存储设备上。当我双击它时,它将安装在我的本地计算机上,并且安装的卷内将是我的 .app (应用程序文件)。现在,我希望我的应用程序在我的 dmg 文件安装到本地计算机上后自动启动。现在我的应用程序还需要有关实际 dmg 文件所在位置的信息,例如其在可移动存储设备上的路径。这是否可能,如果可以,我如何找到挂载该卷的 dmg 文件的路径。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-08-19 18:06:26
  1. 在 Mac OS X 中无法自动启动应用程序。存在一些安全原因。唯一可以自动启动的是 .pkg 文件,而且只能通过 Safari AFAIK。

  2. 可以确定应用程序所在的 DMG 文件。为此,您必须使用 IOKit。尝试使用 IORegistryExplorer。

一些可能对您有帮助的代码

这些是我第一次尝试使用 IOKit,它是出于另一个目的,但它应该有帮助。

// 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);

由于免费桥接支持,大部分工作都可以使用 Foundation 类而不是 CoreFoundation 类来完成。这使得它更容易、更易读。


如果 IOBlockStorageDriver 的父 IOService 是 IODiskImageBlockStorageDeviceOutKernel,则上面的示例代码可以正常工作。如果父 IOService 的名称是“AppleDiskImageDevice”,则 IOService 链看起来有点不同:

  +-o IOHDIXHDDriveOutKernelUserClient
    +-o AppleDiskImageDevice        <---- You want to get up here
      +-o IOBlockStorageDriver
        +-o Apple Disk Image Media  <---- This is different
          +-o IOMediaBSDClient
          +-o IOApplePartitionScheme
            +-o Apple@1
            | +-o IOMediaBSDClient
            +-o disk image@2        <---- This is the matched IOService!
              +-o IOMediaBSDClient

您可以在上面的 for 循环之后获取图像文件路径 URL 字符串,如下所示:

CFMutableDictionaryRef properties = nil;
IORegistryEntryCreateCFProperties(service, &properties, kCFAllocatorDefault, kNilOptions);
if (properties) {
    CFStringRef url = CFDictionaryGetValue(properties, CFSTR("DiskImageURL"));
    CFRelease(properties);
}
IOObjectRelease(service);
  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.


The sample code above is working if the parent IOService of IOBlockStorageDriver is IODiskImageBlockStorageDeviceOutKernel. If the name of the parent IOService is "AppleDiskImageDevice", the IOService chain looks a bit different:

  +-o IOHDIXHDDriveOutKernelUserClient
    +-o AppleDiskImageDevice        <---- You want to get up here
      +-o IOBlockStorageDriver
        +-o Apple Disk Image Media  <---- This is different
          +-o IOMediaBSDClient
          +-o IOApplePartitionScheme
            +-o Apple@1
            | +-o IOMediaBSDClient
            +-o disk image@2        <---- This is the matched IOService!
              +-o IOMediaBSDClient

You can obtain the image file path URL string like bellow after the for-loop above:

CFMutableDictionaryRef properties = nil;
IORegistryEntryCreateCFProperties(service, &properties, kCFAllocatorDefault, kNilOptions);
if (properties) {
    CFStringRef url = CFDictionaryGetValue(properties, CFSTR("DiskImageURL"));
    CFRelease(properties);
}
IOObjectRelease(service);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文