在 MBED 板上的 LocalFileSystem 中查询空闲 space
Querying free space in LocalFileSystem on the MBED board
在 MBED 板上的 LocalFileSystem
中是否有任何支持 API 的免费 space?我试过 statvfs
但它似乎不起作用...有什么想法吗?
我想我可以简单地列出所有文件并从总大小中减去总数,但我想知道是否有更好的方法。
这是我试过的:
long GetAvailableSpace(const char* path)
{
struct statvfs stat;
if (statvfs(path, &stat) != 0) {
// error happens, just quits here
return -1;
}
// the available size is f_bsize * f_bavail
return stat.f_bsize * stat.f_bavail;
}
更新:
我最终遍历所有文件并计算它:
#define MAX_STORAGE 512000
int LocalFileSystemFreeSpace(){
char filename[MAX_FILENAME];
DIR *d;
struct dirent *dir;
int total = 0;
d = opendir("/local");
if(d){
while((dir = readdir(d)) != NULL){
sprintf(filename, "/local/%s", dir->d_name);
int size = FileSize(filename);
total += size;
//printf("%s -> %d\r\n",filename,size);
}
closedir(d);
}
// printf("Total files: %d\r\n", total);
// printf("Free: %d\r\n",MAX_STORAGE-total);
return MAX_STORAGE-total;
}
int FileSize(char * filename){
FILE * fp = fopen(filename,"r");
if(fp==NULL){
return 0;
}
int prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz=ftell(fp);
fclose(fp);
return sz;
}
我认为现在不可能。 Semihosting is used for the LocalFileSystem API, and the only commands that are currently implemented are here。没有免费磁盘 space...
在 MBED 板上的 LocalFileSystem
中是否有任何支持 API 的免费 space?我试过 statvfs
但它似乎不起作用...有什么想法吗?
我想我可以简单地列出所有文件并从总大小中减去总数,但我想知道是否有更好的方法。
这是我试过的:
long GetAvailableSpace(const char* path)
{
struct statvfs stat;
if (statvfs(path, &stat) != 0) {
// error happens, just quits here
return -1;
}
// the available size is f_bsize * f_bavail
return stat.f_bsize * stat.f_bavail;
}
更新:
我最终遍历所有文件并计算它:
#define MAX_STORAGE 512000
int LocalFileSystemFreeSpace(){
char filename[MAX_FILENAME];
DIR *d;
struct dirent *dir;
int total = 0;
d = opendir("/local");
if(d){
while((dir = readdir(d)) != NULL){
sprintf(filename, "/local/%s", dir->d_name);
int size = FileSize(filename);
total += size;
//printf("%s -> %d\r\n",filename,size);
}
closedir(d);
}
// printf("Total files: %d\r\n", total);
// printf("Free: %d\r\n",MAX_STORAGE-total);
return MAX_STORAGE-total;
}
int FileSize(char * filename){
FILE * fp = fopen(filename,"r");
if(fp==NULL){
return 0;
}
int prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz=ftell(fp);
fclose(fp);
return sz;
}
我认为现在不可能。 Semihosting is used for the LocalFileSystem API, and the only commands that are currently implemented are here。没有免费磁盘 space...