📄 storage_util.cpp
字号:
struct statfs s; unsigned long long size; if(statfs(e->mnt_dir,&s) < 0) { endmntent(fp); return false; } 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; //entry path if(e->mnt_dir != NULL) strncpy(pinfo->mount_dir, e->mnt_dir, 256); } endmntent(fp); return true;}bool GetDiskPartitionList(disk_info_t *dinfo, list<part_info_t> &plist){ plist.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 (!isdigit(s[-1])) {findadisk: //find a disk sprintf(devname, "/dev/%s", ptname); if(strcmp(dinfo->name, devname)) continue; //find the disk! then get the partition list 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])) { //find a part part_info_t pinfo; memset(&pinfo, 0, sizeof(part_info_t)); //fill the pinfo sprintf(devname, "/dev/%s", ptname); strncpy(pinfo.name, devname, 256); if(_InitPartitionInfo(&pinfo)) { if(pinfo.capacity == 0) pinfo.capacity = size; plist.push_back(pinfo); } } else { goto findadisk; } }//while }//if }//while fclose(procpf); return true;}bool GetInternalFlashList(list<flash_info_t> &flist){ flist.clear(); 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); } flash_info_t finfo; memset(&finfo, 0, sizeof(flash_info_t)); //interface if(!strncmp(dev_name, INTERNAL_PREFIX_I519, 7) || !strncmp(dev_name, INTERNAL_PREFIX_Z, 8)) { finfo.interface = INTERNAL; } else { continue; } strncpy(finfo.name, dev_name, 256); //find the partition in mnt table //fill the pinfo struct //access if(!strncmp(e->mnt_opts, "ro", 2)) finfo.access = RO; else if(!strncmp(e->mnt_opts, "rw", 2)) finfo.access = RW; else finfo.access = UNKNOWN_ACCESS; //file system type if(!strncmp(e->mnt_type, "jffs2", 5)) finfo.fs_type = JJFS; else if(!strncmp(e->mnt_type, "ext2", 4)) finfo.fs_type = EXT2FS; else if(!strncmp(e->mnt_type, "ext3", 4)) finfo.fs_type = EXT3FS; else if(!strncmp(e->mnt_type, "vfat", 4)) finfo.fs_type = VFATFS; else finfo.fs_type = UNKNOWN_FS; //capacity struct statfs s; unsigned long long size; if(statfs(e->mnt_dir,&s) < 0) { endmntent(fp); return -1; } size = ((s.f_blocks * (long long )s.f_bsize) + 1024/2 ) / 1024; finfo.capacity = size; size = ((s.f_bavail * (long long)s.f_bsize) + 1024/2) / 1024; finfo.free = size; //entry path if(e->mnt_dir != NULL) strncpy(finfo.mount_dir, e->mnt_dir, 256); flist.push_back(finfo); } endmntent(fp); return true;}bool GetNetworkPartitionList(list<part_info_t> &plist){ plist.clear(); struct mntent *e; FILE *fp; char dev_name[256]; part_info_t finfo; //init mnt table fp = setmntent(MNT_TABLE_FILE, "r"); if(fp == NULL) return false; while((e = getmntent(fp)) != NULL) { strncpy(dev_name, e->mnt_fsname, 256); //find the real root path if(!strncmp(e->mnt_fsname, "/dev/root", 9)) { FindRealRootDeviceName(dev_name); } //interface string sdev = dev_name; //XXX: have risk? if(sdev.find(":") == string::npos) { continue; } strncpy(finfo.name, dev_name, 256); //find the partition in mnt table //fill the pinfo struct //access if(!strncmp(e->mnt_opts, "ro", 2)) finfo.access = RO; else if(!strncmp(e->mnt_opts, "rw", 2)) finfo.access = RW; else finfo.access = UNKNOWN_ACCESS; //file system type if(!strncmp(e->mnt_type, "jffs2", 5)) finfo.fs_type = JJFS; else if(!strncmp(e->mnt_type, "ext2", 4)) finfo.fs_type = EXT2FS; else if(!strncmp(e->mnt_type, "ext3", 4)) finfo.fs_type = EXT3FS; else if(!strncmp(e->mnt_type, "vfat", 4)) finfo.fs_type = VFATFS; else finfo.fs_type = UNKNOWN_FS; //capacity struct statfs s; unsigned long long size; if(statfs(e->mnt_dir,&s) < 0) { endmntent(fp); return -1; } size = ((s.f_blocks * (long long )s.f_bsize) + 1024/2 ) / 1024; finfo.capacity = size; size = ((s.f_bavail * (long long)s.f_bsize) + 1024/2) / 1024; finfo.free = size; //entry path if(e->mnt_dir != NULL) strncpy(finfo.mount_dir, e->mnt_dir, 256); plist.push_back(finfo); } endmntent(fp); return true;}bool GetDiskInfo(disk_info_t *dinfo){ list<disk_info_t> disklist; GetDiskList(disklist); list<disk_info_t>::iterator it = disklist.begin(); for(; it != disklist.end(); it++) { if(!strcmp((*it).name, dinfo->name)) { dinfo->interface = (*it).interface; dinfo->capacity = (*it).capacity; break; } } if(it == disklist.end()) { //maybe it is a flash flash_info_t finfo; strncpy(finfo.name, dinfo->name, 256); if(GetFlashInfo(&finfo)) { dinfo->interface = finfo.interface; dinfo->capacity = finfo.capacity; } else { return false; } } return true;}bool GetFlashInfo(flash_info_t *finfo){ list<flash_info_t> flashlist; GetInternalFlashList(flashlist); list<flash_info_t>::iterator it = flashlist.begin(); for(; it != flashlist.end(); it++) { if(!strcmp((*it).name, finfo->name)) { strncpy(finfo->mount_dir, (*it).mount_dir, 256); finfo->interface = (*it).interface; finfo->access = (*it).access; finfo->fs_type = (*it).fs_type; finfo->capacity = (*it).capacity; finfo->free = (*it).free; break; } } if(it == flashlist.end()) return false; return true;}bool GetPartitionInfo(part_info_t *pinfo){ list<part_info_t> partlist; GetPartitionList(partlist); list<part_info_t>::iterator it = partlist.begin(); for(; it != partlist.end(); it++) { if(!strcmp((*it).name, pinfo->name)) { strncpy(pinfo->mount_dir, (*it).mount_dir, 256); pinfo->access = (*it).access; pinfo->fs_type = (*it).fs_type; pinfo->capacity = (*it).capacity; pinfo->free = (*it).free; break; } } if(it == partlist.end()) { //maybe it is a flash flash_info_t finfo; strncpy(finfo.name, pinfo->name, 256); if(GetFlashInfo(&finfo)) { strncpy(pinfo->mount_dir, finfo.mount_dir, 256); pinfo->access = finfo.access; pinfo->fs_type = finfo.fs_type; pinfo->capacity = finfo.capacity; pinfo->free = finfo.free; } else { return false; } } return true;}bool GetPartitionList(list<part_info_t> &pinfo_list){ pinfo_list.clear(); list<disk_info_t> disklist; GetDiskList(disklist); list<disk_info_t>::iterator it = disklist.begin(); //collect all disk's part list for(; it != disklist.end(); it++) { disk_info_t dinfo; strncpy(dinfo.name, (*it).name, 256); list<part_info_t> diskpartlist; if(GetDiskPartitionList(&dinfo, diskpartlist)) { pinfo_list.insert(pinfo_list.end(), diskpartlist.begin(), diskpartlist.end()); } } //get net part list list<part_info_t> netpartlist; if(GetNetworkPartitionList(netpartlist)) { pinfo_list.insert(pinfo_list.end(), netpartlist.begin(), netpartlist.end()); } return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -