确定卷是否是代码中的磁盘映像

确定卷是否是代码中的磁盘映像

本文介绍了确定卷是否是代码中的磁盘映像(DMG)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从目标C(或Swift),我需要确定一个安装的卷是否是一个磁盘映像(从一个.dmg文件挂载)。



我到,但他们都没有提供该卷的类型/协议。不过,我可以在下使用终端 diskutil code $ c
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ c $〜$ Temp $ $ diskutil信息/ dev / disk8
设备标识符:disk8
设备节点:/ dev / disk8
整体的一部分:disk8
设备/介质名称:Apple UDIF只读介质

卷名称:不适用(无文件系统)

已装入:不适用(无文件系统)

文件系统:无

内容(IOContent):GUID_partition_scheme
OS Can安装:否
媒体类型:通用
协议:磁盘映像< ===这是我想要什么
SMART状态:不支持

总大小: 5.2 MB(5242880字节)(正好10240 512字节单位)
卷可用空间:不适用(无文件系统)
De副块大小:512字节

只读媒体:是
只读卷:不适用(无文件系统)
可弹出:有

整数:是
内部:否
操作系统9驱动程序:否
低级格式:不支持

编辑:找到,至少用来做到这一点,通过。然而,这是预ARC,我不知道它是否仍然有效。

通过框架。要使用下面的例子,你必须链接到 #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;

$ b $ / code $ / pre

用法:

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

我希望这有助于您。


From Objective C (or Swift), I need to determine if a mounted volume is a Disk Image (mounted from a .dmg file).

Similar questions led me to NSURL Volume Property Keys, but none of them seem to give the type/protocol of the volume.

However, I can see this information with the terminal diskutil function under 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

EDIT: Found some code that at least used to do this, by means of this included category extension to NSWorkspace. However, it is pre-ARC and I'm not sure if it would still work.

Found it via this partial answer on other question..

解决方案

You can obtain this information using the DiskArbitration framework. To use the example below, you must link against and #import it.

#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;

}

Usage:

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

I hope this helps.

这篇关于确定卷是否是代码中的磁盘映像(DMG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:14