Querying free space in LocalFileSystem on the MBED board

133 views Asked by At

Is there any supported API to get free space in the LocalFileSystem on an MBED board? I've tried statvfs but it doesn't seem to work... Any ideas?

I guess I could simply list all files and subtract the total from the total size, but I was wondering if there's a better way.

This is what I tried:

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;
}

UPDATE:

I ended up iterating over all files and calculating it:

#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;
} 
1

There are 1 answers

1
Jan Jongboom On BEST ANSWER

I don't think it's possible right now. Semihosting is used for the LocalFileSystem API, and the only commands that are currently implemented are here. Nothing for free disk space...