2009-08-07 5 views
0

Je veux connaître la taille de bloc naturel du média IO utilisé dans une application de cacao. J'ai vu une fonction dans IOMedia.cpp appelée "getPreferredBlockSize()" qui me donne soi-disant ma taille de bloc. S'il vous plaît pouvez-vous m'expliquer comment utiliser cette fonction dans mon application ou s'il existe une autre méthode permettant de trouver la taille de bloc naturelle du média IO sous-jacent.trouver la taille de bloc naturel du média utilisé par la poignée de fichier

Merci

Répondre

1

Vous pouvez utiliser la fonction C libc, qui prend un descripteur de fichier et un struct stat et remplit le struct avec les données. Code comme ceci fonctionnerait (avertissement: Je n'ai pas testé moi-même):

#include <sys/stat.h> 

/* ... */ 

struct stat info; 
blksize_t preferredBlockSize; 
int fd = [myFileHandle fileDescriptor]; 
if ((fstat(fd, &info)) == 0) { 
    preferredBlockSize = info.st_blksize; 
} else { 
    NSLog(@"Could not get file stats"); 
} 

/* Do what you want with preferredBlockSize */ 

De la page man fstat():

Le champ st_blksize donne la taille de blocs "préféré" pour le système de fichiers efficace I/O. (Écriture dans un fichier en petits morceaux peut provoquer une inefficacité-rewrite lecture-modification.)

+0

Merci mipadi je cherchais cela depuis un Longtemps. – King

+0

Ceci est _not_ la taille de bloc native. Pour ma HD, c'est environ 13 Mo, mais la taille de bloc native est seulement de 512 Ko. –

+0

Je pense que vous voulez dire fstat (fd, & info) où vous conditionne stat (fd, & info). – Travis

1

Voici le code pour obtenir la taille du bloc natif:

#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 
Questions connexes