views:

328

answers:

3

I want to be able to determine at runtime what the sector size is for a give filesystem. C code is acceptable. for example I format my Data partitions with a 32k sector size that have lots of large video files. I want to be able to get this value at runtime.

+1  A: 
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
        int page_size = getpagesize();
        printf("The page size is %d\n", page_size);
        exit(0);
}
ennuikiller
getpagesize() returns the page size for memory, not the filesystem.
R Samuel Klatchko
+4  A: 

I think you want statvfs (if by pagesize you mean sector size?) which from what I remember works linux and OSX. I think you need to use the f_bsize field but I unfortunately do not have a linux box to test against atm.

For windows you want the GetDiskFreeSpace function.

tyranid
`statvfs()` is indeed one way to do it on Linux (it's a POSIX function). `statfs()` is an alternative on Linux, and as a BSD-derived function it's the one that's available on OSX. In both cases the `f_bsize` field of the returned structure is the one you want.
caf
+1  A: 

On Windows, call the GetSystemInfo() function

void WINAPI GetSystemInfo(
  __out  LPSYSTEM_INFO lpSystemInfo
);

Then access the dwPageSize value in the returned SYSTEM_INFO structure. This size is guaranteed to give file system page aligment. For example, use this size for reading and writing files in unbuffered mode.

If you need the volumen sector size, simply call GetDiskFreeSpace() function then read the lpBytesPerSector value.

Foredecker