linux中怎么获取flash分区大小

linux中怎么获取flash分区大小

linux中怎么获取flash分区大小,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

在嵌入式系统中,由于flash存储空间有限,或者是存储数据,实现数据的循环删除,需要获取到分区的使用情况,可以通过系统下的函数statfs来获取使用情况;实现代码如下:

flashInfo.cpp


#include <stdio.h>                                                                                                                  #include <stdlib.h>#include <unistd.h>#include <sys/vfs.h> typedef unsigned long long u64; //unit: 0-MBytes, 1-KBytes, default MBytesint getPartitionUse(const char *dir, size_t &totleSize, size_t &freeSize, int unit){    struct statfs diskInfo;      int ret = statfs(dir, &diskInfo);    if ( ret != 0 )  {    perror("getPartitionUse statfs error ");    return ret;  }      u64 allBlocks = diskInfo.f_bsize;      u64 tmpTotalSize = allBlocks * diskInfo.f_blocks;      u64 tmpFreeDisk = diskInfo.f_bfree*allBlocks;      totleSize = tmpTotalSize>>20;    freeSize  = tmpFreeDisk>>20;    if(unit == 0)  {      totleSize  = tmpTotalSize>>20;      freeSize  = tmpFreeDisk>>20;    }  else if( unit == 1 )  {    totleSize  = tmpTotalSize>>10;      freeSize  = tmpFreeDisk>>10;  }    return 0;} int main(){  size_t totleSize = 0;  size_t freeSize = 0;      getPartitionUse("/", totleSize,freeSize, 0);    printf ("system  total=%dMB, free=%dMB\n", totleSize, freeSize);      getPartitionUse("/work/data", totleSize,freeSize, 0);    printf ("data  total=%dMB, free=%dMB\n", totleSize, freeSize);        getPartitionUse("/work", totleSize,freeSize, 0);    printf ("config  total=%dMB, free=%dMB\n", totleSize, freeSize);  
}

编译:

mipsel-linux-g++ flashInfo.c -o flashInfo

运行结果如下:

linux中怎么获取flash分区大小

关于linux中怎么获取flash分区大小问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。