Page 1 of 1

[SOLVED] Help understanding os.statvfs('/')

Posted: Fri Dec 16, 2016 3:29 am
by jpj
I added a 16G SD card to my esp8266. It's functioning fine, no issues. I wanted to check the free space on the SD and found os.statvfs(). Here is my output:

Code: Select all

>>> os.statvfs('/')
(32768, 32768, 30212096, 472032, 472032, 0, 0, 0, 0, 255)
Looking at the source code here
https://raw.githubusercontent.com/hosak ... /vfs_fat.c
I found this section of code (please scroll down):

Code: Select all

STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
    (void)vfs_in;
    const char *path = mp_obj_str_get_str(path_in);

    FATFS *fatfs;
    DWORD nclst;
    FRESULT res = f_getfree(path, &nclst, &fatfs);
    if (FR_OK != res) {
        nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
          MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
    }

    mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));

    t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * fatfs->ssize); // f_bsize
    t->items[1] = t->items[0]; // f_frsize
    t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2) * fatfs->csize); // f_blocks
    t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
    t->items[4] = t->items[3]; // f_bavail
    t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
    t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
    t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
    t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
    t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax

    return MP_OBJ_FROM_PTR(t);
}
If I understand correctly, my blocksize is 32768 and number of blocks free is 472032. So I should have 14751 megabytes free.
>>> blocksize = 32768
>>> freeblocks = 472032
>>> blocksize * freeblocks
15467544576
>>> megabyte = 1024 * 1024
>>> 15467544576 / megabyte
14751.0
Does that look correct?

Thanks,
J

Re: Help understanding os.statvfs('/')

Posted: Fri Dec 16, 2016 6:32 am
by Roberthh
For me, that looks reasonable. 15467544576 is about 16 * 1,000,000,000 or 16 GB, as they call it in marketing.

Re: Help understanding os.statvfs('/')

Posted: Fri Dec 16, 2016 9:45 am
by platforma
Hi, I'm the author of the commit. You're understanding is correct! Also man statvfs might be useful if you want to read on: https://linux.die.net/man/2/statvfs