📄 storage_util.cpp
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#include <sys/vfs.h>#include <mntent.h>#include <sys/param.h>#include <sys/mount.h>#include <stdio.h> #include <errno.h>#include <string.h>#include <dirent.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdlib.h>#include <string>using namespace std;#include "storage_util.h"int FindRealRootDeviceName(char *dev_name){ struct dirent *ent; struct stat stat_buf, root_stat; char *file_name = NULL; dev_t dev; DIR *directory; if (stat("/", &root_stat) != 0) goto finddone; else { /* This check is here in case they pass in /dev name */ if ((root_stat.st_mode & S_IFMT) == S_IFBLK) dev = root_stat.st_rdev; else dev = root_stat.st_dev; directory = opendir("/dev"); if (!directory) goto finddone; else { while((ent = readdir(directory)) != NULL) { const char *my_name = ent->d_name; if (my_name[0] == '.' && my_name[1] == '.' && !my_name[2]) continue; if (strcmp(my_name, "root")==0) continue; //file_name = strcat("/dev/", my_name); file_name = malloc(256); sprintf(file_name, "%s%s", "/dev/", my_name); if (stat(file_name, &stat_buf) == 0 && S_ISBLK(stat_buf.st_mode)!=0 && stat_buf.st_rdev == dev) break; free(file_name); file_name=NULL; } closedir(directory); } }finddone: if(file_name==NULL) { file_name = strdup("/dev/root"); strcpy(dev_name, file_name); free(file_name); return -1; } else { strcpy(dev_name, file_name); free(file_name); return 0; }}int init_store_info(store_info_t* pinfo, struct mntent *e){ struct statfs s; char dev_name[256]; unsigned long long size; memset(pinfo, 0 , sizeof(store_info_t)); strncpy(pinfo->mount_dir, e->mnt_dir, 256); strcpy(dev_name, e->mnt_fsname); //find the real device if(!strncmp(e->mnt_fsname, "/dev/root", 9)) { FindRealRootDeviceName(dev_name); } //interface if(!strncmp(dev_name, INTERNAL_PREFIX_I519, 7) || !strncmp(dev_name, INTERNAL_PREFIX_Z, 8)) { pinfo->interface = INTERNAL; pinfo->valid = 1; } else if(!strncmp(dev_name, SD_PREFIX, 8)) { pinfo->interface = SD; pinfo->valid = 1; } else if(!strncmp(dev_name, CF_PREFIX, 7)) { pinfo->interface = CF; pinfo->valid = 1; } else if(!strncmp(dev_name, HD_PREFIX, 7)) { pinfo->interface = HD; pinfo->valid = 1; } else { pinfo->interface = UNKNOWN_INTERFACE; pinfo->valid = 0; } strncpy(pinfo->name, dev_name, 256); //access if(!strncmp(e->mnt_opts, "ro", 2)) pinfo->access = RO; else if(!strncmp(e->mnt_opts, "rw", 2)) pinfo->access = RW; else pinfo->access = UNKNOWN_ACCESS; //file system type if(!strncmp(e->mnt_type, "jffs2", 5)) pinfo->fs_type = JJFS; else if(!strncmp(e->mnt_type, "ext2", 4)) pinfo->fs_type = EXT2FS; else if(!strncmp(e->mnt_type, "ext3", 4)) pinfo->fs_type = EXT3FS; else if(!strncmp(e->mnt_type, "vfat", 4)) pinfo->fs_type = VFATFS; else pinfo->fs_type = UNKNOWN_FS; //capacity if(statfs(e->mnt_dir,&s) < 0) return -1; size = ((s.f_blocks * (long long )s.f_bsize) + 1024/2 ) / 1024; pinfo->capacity = size; size = ((s.f_bavail * (long long)s.f_bsize) + 1024/2) / 1024; pinfo->free = size; return 0;}FILE *fp;int FindStoreInit(){ //init mnt table fp = setmntent(MNT_TABLE_FILE, "r"); if(fp == NULL) return -1; return 0;}int FindStoreFini(){ endmntent(fp);}int FindFirstStore(store_info_t* pinfo){ struct mntent *e; //get first entry if(fp == NULL) return -1; e = getmntent(fp); if(e == NULL) return -1; //fill the info struct init_store_info(pinfo, e); //done return 0;}int FindNextStore(store_info_t* pinfo){ struct mntent *e; if(fp == NULL) return -1; e = getmntent(fp); if(e == NULL) return -1; init_store_info(pinfo, e); return 0;}int GetStore(store_info_t* pinfo, char* name){ struct mntent *e; FILE *fp; //init mnt table fp = setmntent(MNT_TABLE_FILE, "r"); if(fp == NULL) return -1; //erase pinfo memset(pinfo, 0, sizeof(store_info_t)); while(1) { //get first entry e = getmntent(fp); if(e == NULL) goto done; init_store_info(pinfo, e); if(!strcmp(pinfo->name, name)) { goto done; } memset(pinfo, 0, sizeof(store_info_t)); }done: endmntent(fp); if(pinfo->valid != 0) return 0; else return -1;}int IsDisk(store_info_t* pinfo){ return pinfo->valid;}bool GetDiskList(list<disk_info_t> &dlist){ //clear the list first dlist.clear(); FILE *procpf; char line[256], ptname[256], devname[256], *s; int major, minor; unsigned long long size; procpf = fopen(PROC_PARTITIONS, "r"); if(procpf == NULL) { return false; } while (fgets(line, sizeof(line), procpf)) { if (sscanf (line, " %d %d %llu %[^\n ]", &major, &minor, &size, ptname) != 4) continue; for (s = ptname; *s; s++); //if the end character is digit, it is a partition if (isdigit(s[-1])) continue; //this is is disk sprintf(devname, "/dev/%s", ptname); //fill disk_info_t struct disk_info_t dinfo; strncpy(dinfo.name, devname, 256); if(!strncmp(devname, INTERNAL_PREFIX_I519, 7) || !strncmp(devname, INTERNAL_PREFIX_Z, 8)) { dinfo.interface = INTERNAL; } else if(!strncmp(devname, SD_PREFIX, 8)) { dinfo.interface = SD; } else if(!strncmp(devname, CF_PREFIX, 7)) { dinfo.interface = CF; } else if(!strncmp(devname, HD_PREFIX, 7)) { dinfo.interface = HD; } else { dinfo.interface = UNKNOWN_INTERFACE; } dinfo.capacity = size; //nhu: this is low performance dlist.push_back(dinfo); } fclose(procpf); return true;}bool _InitPartitionInfo(part_info_t *pinfo){ struct mntent *e; FILE *fp; char dev_name[256]; //init mnt table fp = setmntent(MNT_TABLE_FILE, "r"); if(fp == NULL) return false; while((e = getmntent(fp)) != NULL) { //find the real root path strncpy(dev_name, e->mnt_fsname, 256); if(!strncmp(e->mnt_fsname, "/dev/root", 9)) { FindRealRootDeviceName(dev_name); } if(strcmp(dev_name, pinfo->name)) continue; //find the partition in mnt table //fill the pinfo struct //access if(!strncmp(e->mnt_opts, "ro", 2)) pinfo->access = RO; else if(!strncmp(e->mnt_opts, "rw", 2)) pinfo->access = RW; else pinfo->access = UNKNOWN_ACCESS; //file system type if(!strncmp(e->mnt_type, "jffs2", 5)) pinfo->fs_type = JJFS; else if(!strncmp(e->mnt_type, "ext2", 4)) pinfo->fs_type = EXT2FS; else if(!strncmp(e->mnt_type, "ext3", 4)) pinfo->fs_type = EXT3FS; else if(!strncmp(e->mnt_type, "vfat", 4)) pinfo->fs_type = VFATFS; else pinfo->fs_type = UNKNOWN_FS; //capacity
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -