从代码确定卷是否是磁盘映像(DMG)

时间:2015-07-21 17:07:07

标签: objective-c macos filesystems nsurl diskimage

从Objective C(或Swift),我需要确定挂载的卷是否是磁盘映像(从.dmg文件挂载)。

类似的问题让我感到NSURL Volume Property Keys,但他们似乎都没有给出音量的类型/协议。

但是,我可以使用diskutil下的终端Protocol功能查看此信息:

~/Temp$ diskutil info /dev/disk8
   Device Identifier:        disk8
   Device Node:              /dev/disk8
   Part of Whole:            disk8
   Device / Media Name:      Apple UDIF read-only Media

   Volume Name:              Not applicable (no file system)

   Mounted:                  Not applicable (no file system)

   File System:              None

   Content (IOContent):      GUID_partition_scheme
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 Disk Image <=== THIS IS WHAT I WANT
   SMART Status:             Not Supported

   Total Size:               5.2 MB (5242880 Bytes) (exactly 10240 512-Byte-Units)
   Volume Free Space:        Not applicable (no file system)
   Device Block Size:        512 Bytes

   Read-Only Media:          Yes
   Read-Only Volume:         Not applicable (no file system)
   Ejectable:                Yes

   Whole:                    Yes
   Internal:                 No
   OS 9 Drivers:             No
   Low Level Format:         Not supported

编辑:通过some code找到至少用于此操作的this included category extension to NSWorkspace。但是,它是ARC之前的,我不确定它是否仍然有用。

在其他问题上通过this partial answer找到它。

1 个答案:

答案 0 :(得分:6)

您可以使用DiskArbitration框架获取此信息。要使用下面的示例,您必须链接到#import

#import <DiskArbitration/DiskArbitration.h>

...

- (BOOL)isDMGVolumeAtURL:(NSURL *)url
{

  BOOL isDMG = NO;

  if (url.isFileURL) {

    DASessionRef session = DASessionCreate(kCFAllocatorDefault);
    if (session != nil) {

      DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, (__bridge CFURLRef)url);
      if (disk != nil) {

        NSDictionary * desc = CFBridgingRelease(DADiskCopyDescription(disk));
        NSString * model = desc[(NSString *)kDADiskDescriptionDeviceModelKey];
        isDMG = ([model isEqualToString:@"Disk Image"]);

        CFRelease(disk);

      }

      CFRelease(session);

    }

  }

  return isDMG;

}

用法:

BOOL isDMG = [someObject isDMGVolumeAtURL:[NSURL fileURLWithPath:@"/Volumes/Some Volume"]];

我希望这会有所帮助。