我想找出可可应用程序中使用的IO媒体的自然块大小。我在IOMedia.cpp中看到了一个名为“ getPreferredBlockSize()”的函数,该函数应该可以给我我的块大小。请大家解释一下我如何在我的应用程序中使用此功能,或者是否有其他方法可以用来查找基础IO介质的自然块大小。

谢谢

最佳答案

这是获取本机块大小的代码:

#include <sys/stat.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOBSD.h>
#include <IOKit/storage/IOMedia.h>
#include <CoreFoundation/CoreFoundation.h>

// look up device number with stat
struct stat stats;
if (stat(path, &stats) != 0) {
    return;
}
// use st_rdev instead of st_dev if
// the path is a device (/dev/disk0)
int bsd_major = major(stats.st_dev);
int bsd_minor = minor(stats.st_dev);

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;
}
CFNumberRef blockSizeProperty;
blockSizeProperty = (CFNumberRef)IORegistryEntryCreateCFProperty(service,
                                        CFSTR(kIOMediaPreferredBlockSizeKey),
                                        kCFAllocatorDefault, 0);
if (!blockSizeProperty) {
    return;
}

int blockSize;
CFNumberGetValue(blockSizeProperty, kCFNumberIntType, &blockSize);
CFRelease(blockSizeProperty);

// blockSize is the native block size of the device

关于cocoa - 查找文件句柄使用的媒体的自然块大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1246315/

10-10 03:57