statvfs : Informations système Linux
Bonjour.
Je souhaite obtenir des informations sur mes partitions Linux. Des informations telles que :
- Espace total
- Espace libre
- Espace utilisé
- ...
Pour ceci, j'utilise ce code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
void disk_space(char *path)
{
struct statvfs fiData;
if((statvfs(path,&fiData)) < 0 )
{
printf("Failed to stat %s:\n", path);
}
else
{
printf("Disk %s: \n", path);
printf("block size: %u\n", fiData.f_bsize);
printf("total no blocks: %i\n", fiData.f_blocks);
printf("free blocks: %i\n", fiData.f_bfree);
printf("free blocks (for non-root): %i\n", fiData.f_bavail);
float totalspace=((float) fiData.f_bsize)*((float) fiData.f_blocks);
float freespace=((float) fiData.f_bsize)*((float) fiData.f_bavail);
printf("Free space:%.2fMB, %.2fGB,\n",freespace/(1024.0*1024.0),freespace/(1024.0*1024.0*1024.0));
printf("Total space: %.2fMB, %.2fGB,\n", totalspace/(1024.0*1024.0),totalspace/(1024.0*1024.0*1024.0));
}
}
int main(int argc, char *argv[])
{
disk_space("/dev/sda1");
return 0,;
} |
Mon problème, c'est que j'obtiens toujours le même résultat, quelque soit la partition analysée :
Citation:
Starting /home/pierre/Documents/C++/infos_hdd/infos_hdd...
Disk /dev/sda1:
block size: 4096
total no blocks: 128996
free blocks: 128311
free blocks (for non-root): 128311
Free space:501,21MB, 0,49GB,
Total space: 503,89MB, 0,49GB,
Sachant que mon /dev/sda1 équivaut a mon /home, si je fait :
Code:
1 2 3 4 5 6 7
| int main(int argc, char *argv[])
{
disk_space("/home");
return 0,;
} |
'obtiens des résultats valides :
Citation:
Starting /home/pierre/Documents/C++/infos_hdd/infos_hdd...
Disk /home:
block size: 4096
total no blocks: 8410362
free blocks: 7999085
free blocks (for non-root): 7571857
Free space:29577,57MB, 28,88GB,
Total space: 32852,98MB, 32,08GB,
Je ne vois pas d'ou vient le problème ? Comment obtenir ces informations depuis le nom de mes partitions (/dev/sdaX), et non pas depuis leur point de montage ?
Cordialement,
PKO
Edit :
Pour information, j'ai fait un second test, cette fois avec le methode fopen :
Ne fonctionne pas :
Code:
1 2 3 4 5 6 7 8 9
| FILE* fp = NULL;
fp = fopen("/dev/sda1","rb" );
if( fp != NULL )
cout<<"OK" <<endl;
else
cout<<"KO" <<endl;
fclose( fp ); |
Fonctionne :
Code:
1 2 3 4 5 6 7 8 9
| FILE* fp = NULL;
fp = fopen("/home","rb" );
if( fp != NULL )
cout<<"OK" <<endl;
else
cout<<"KO" <<endl;
fclose( fp ); |
A défaut de me servir de /dev/sda, y'a t'il un moyen d'obtenir la relation partition -> point de montage ?
Merci.