zoukankan      html  css  js  c++  java
  • 嵌入式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 MBytes
    int 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
    ", totleSize, freeSize);  
    
        getPartitionUse("/work/data", totleSize,freeSize, 0);
        printf ("data  total=%dMB, free=%dMB
    ", totleSize, freeSize);  
    	
        getPartitionUse("/work", totleSize,freeSize, 0);
        printf ("config  total=%dMB, free=%dMB
    ", totleSize, freeSize);  
    }

    编译:

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

    运行结果如下:

    这样可以在界面或者内置web上用进度条的形式显示出来,比较直观。

    个人微信订阅号:

  • 相关阅读:
    服务器图片等资源在8080端口保存
    thinkphp 3.2.1 URL 大小写问题 下面有具体说明
    linux samba smb 在客户端无法连接使用
    php连接redis服务
    服务器死机 导致 mongo 挂掉
    同一个页面引用不同版本jquery库
    CSS3阴影 box-shadow的使用和技巧总结
    php 中使用正则
    Hbase-1.1.1-java API
    hive1.2.1问题集锦
  • 原文地址:https://www.cnblogs.com/fensnote/p/13436480.html
Copyright © 2011-2022 走看看