在获取 bsdName 之前获取磁盘号

发布于 2024-12-16 18:47:23 字数 2238 浏览 1 评论 0原文

我刚刚遇到一个问题,我可以使用下面的代码行获取bsdName,但是如果USB闪存盘中有两个分区,那么我只能获取最后一个bsdName,有人可以告诉我如何从U盘中获取所有bsdName USB 驱动器?实际上我想获取每个分区的卷名称...提前致谢

CFStringRef bsdName = ( CFStringRef ) IORegistryEntrySearchCFProperty ( usbDevice,
                                                                               kIOServicePlane,
                                                                               CFSTR ( kIOBSDNameKey ),
                                                                               kCFAllocatorDefault,
                                                                               kIORegistryIterateRecursively );
        NSLog(@"bsdName=%@",bsdName);
        volumeName = deviceNameWithMountPath((NSString*)bsdName);
        //find_hid_device(usbDevice);
        NSLog(@"volume Name=%@",volumeName);

        NSString *mediaType = FindWholeMedia(usbDevice);
        NSLog(@"media type = %@",mediaType);

        //CFRelease(bsdName);

        // Done with this USB device; release the reference added by IOIteratorNext
        kr = IOObjectRelease(usbDevice);        
    }
}



NSString *deviceNameWithMountPath(NSString *mountPath)
{
    DASessionRef session = NULL;
    DADiskRef disk = NULL;
    CFDictionaryRef dd = NULL;
    CFTypeRef val = nil;

    session = DASessionCreate(kCFAllocatorDefault);
    if (!session) {
        NSLog(@"Can't create DiskArb session");
        return nil;
    }

//    DASessionScheduleWithRunLoop(session,
//                                 CFRunLoopGetCurrent(),
//                                 kCFRunLoopDefaultMode);
//    
//    DARegisterDiskAppearedCallback(session, NULL, DiskAppeared, NULL);


    disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, [mountPath UTF8String]);
    if (!disk) {
        NSLog(@"DADiskCreateFromBSDName(%s) failed", [mountPath UTF8String]);
        return nil;
    }
    dd = DADiskCopyDescription(disk);
    if (!dd) {
        NSLog(@"DADiskCopyDescription(%s) failed", [mountPath UTF8String]);
        return nil;
    }

    CFDictionaryGetValueIfPresent(dd, (kDADiskDescriptionVolumeNameKey), &val);
    DiskAppeared(disk, NULL);
    CFRelease(disk);
    CFRelease(session); 

    return (NSString *)val;
}

I have just encountered one problem, I can get the bsdName with below code lines but if there are two partitions in a USB flash disk, then I can only get the last bsdName, can someone tell me how do I get all the bsdName from the USB drive? actually I would like to get the volume name of each partition... thanks in advance

CFStringRef bsdName = ( CFStringRef ) IORegistryEntrySearchCFProperty ( usbDevice,
                                                                               kIOServicePlane,
                                                                               CFSTR ( kIOBSDNameKey ),
                                                                               kCFAllocatorDefault,
                                                                               kIORegistryIterateRecursively );
        NSLog(@"bsdName=%@",bsdName);
        volumeName = deviceNameWithMountPath((NSString*)bsdName);
        //find_hid_device(usbDevice);
        NSLog(@"volume Name=%@",volumeName);

        NSString *mediaType = FindWholeMedia(usbDevice);
        NSLog(@"media type = %@",mediaType);

        //CFRelease(bsdName);

        // Done with this USB device; release the reference added by IOIteratorNext
        kr = IOObjectRelease(usbDevice);        
    }
}



NSString *deviceNameWithMountPath(NSString *mountPath)
{
    DASessionRef session = NULL;
    DADiskRef disk = NULL;
    CFDictionaryRef dd = NULL;
    CFTypeRef val = nil;

    session = DASessionCreate(kCFAllocatorDefault);
    if (!session) {
        NSLog(@"Can't create DiskArb session");
        return nil;
    }

//    DASessionScheduleWithRunLoop(session,
//                                 CFRunLoopGetCurrent(),
//                                 kCFRunLoopDefaultMode);
//    
//    DARegisterDiskAppearedCallback(session, NULL, DiskAppeared, NULL);


    disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, [mountPath UTF8String]);
    if (!disk) {
        NSLog(@"DADiskCreateFromBSDName(%s) failed", [mountPath UTF8String]);
        return nil;
    }
    dd = DADiskCopyDescription(disk);
    if (!dd) {
        NSLog(@"DADiskCopyDescription(%s) failed", [mountPath UTF8String]);
        return nil;
    }

    CFDictionaryGetValueIfPresent(dd, (kDADiskDescriptionVolumeNameKey), &val);
    DiskAppeared(disk, NULL);
    CFRelease(disk);
    CFRelease(session); 

    return (NSString *)val;
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文