get partition size

get partition size

一直不希望 partclone 用外部程式去抓partition size,也沒有什麼 library 可以用!

但其實也沒有多難

主要就是:

ioctl(fd, BLKGETSIZE, &numblocks);

或是

ioctl(*ret, BLKGETSIZE64, &partition_size)

unsigned long long get_partition_size(int* ret){

unsigned long long dest_size = 0;

unsigned long dest_block;

struct stat stat;

int debug = 1;

if (!fstat(*ret, &stat)) {

if (S_ISFIFO(stat.st_mode)){

dest_size = 0;

} else if (S_ISREG(stat.st_mode)){

dest_size = stat.st_size;

} else {

#ifdef BLKGETSIZE64

if (ioctl(*ret, BLKGETSIZE64, &dest_size) < 0) {

printf("get device size error\n");

}

printf( "get device size %lli by ioctl BLKGETSIZE64,\n", dest_size);

return dest_size;

#endif

#ifdef BLKGETSIZE

if (ioctl(*ret, BLKGETSIZE, &dest_block) >= 0) {

dest_size = (unsigned long long)(dest_block * 512);

}

printf( "get block %li and device size %lli by ioctl BLKGETSIZE,\n", dest_block, dest_size);

return dest_size;

#endif

}

return dest_size;

}

reference:

  1. http://linuxproblem.org/art_20.html

  2. source code of ntfsclone

convert from Thomas blog post id 468 old convert log: ./145604/tag%3E2009%2004%20linux%20cc)

@2009 @04 @linux @cc

Comments