gpt4 book ai didi

objective-c - 从代码中确定卷是否为磁盘镜像 (DMG)

转载 作者:太空狗 更新时间:2023-10-30 04:01:56 25 4
gpt4 key购买 nike

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

类似的问题让我找到了 NSURL Volume Property Keys ,但它们似乎都没有提供卷的类型/协议(protocol)。

但是,我可以通过 Protocol 下的终端 diskutil 函数看到这些信息:

~/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 找到它关于其他问题..

最佳答案

您可以使用 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"]];

希望对您有所帮助。

关于objective-c - 从代码中确定卷是否为磁盘镜像 (DMG),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31545381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com