Page 1 of 2

How to get free space of internal file system?

Posted: Sun Sep 04, 2016 10:22 pm
by jarda
Is there a way how to get information about free space of the ESP8266 internal file system? I have only found esp.flash_size() (4194304 on my board).

Thanks!

J.

Re: How to get free space of internal file system?

Posted: Mon Sep 05, 2016 11:26 am
by platforma
Good question, I think there isn't a single function that will return the size left on the fs.
I think on 1MB modules, the usable size of the filesystem is 64Kb, minus everything you have written to it already.

Re: How to get free space of internal file system?

Posted: Mon Sep 05, 2016 12:25 pm
by mad474
Maybe this could be a starting point:
https://github.com/micropython/micropyt ... v.py#L5-L7
And I guess you'll also need to walk through directories and count uos.stat('from_all_files')[6] in oder to subtract the result from something.

Please be so kind and publish the script, cos others might need it too :-D

Re: How to get free space of internal file system?

Posted: Mon Sep 05, 2016 3:54 pm
by markxr
As far as I can tell, you'd need to ask the dos fat filesystem how many clusters it has available, and multiply by cluster size.

Micropython doesn't have a function to do this, so you will probably need to add one.

I did investigate this briefly.

The function f_getfree defined in ff.c seems like it will do it. But you'd need to pass in the appropritate FATFS *, and there is no wrapper for this in extmod/vfs_fat_file.c

It's not completely clear how to determine the cluster size.

Re: How to get free space of internal file system?

Posted: Fri Sep 09, 2016 2:24 pm
by platforma
f_getfree might not even be compiled into the ESP port to reduce firmware size, from what I can remember though, ffconf.h doesn't disable it in ESP. You should be able to expose the function from the fatfs library, and perhaps call it from the uos module? Sector size is 4Kb, block sizes are 512 bytes, you can find this in lib/fatfs/ffconf.h and esp8266/mpconfigport.h. I don't have the codebase handy at the moment, but I can take a look later.

Re: How to get free space of internal file system?

Posted: Fri Sep 09, 2016 5:04 pm
by dhylands
stmhal has the statvfs function, which should be portable to any other codebase which uses fatfs
https://github.com/micropython/micropyt ... #L296-L324

Re: How to get free space of internal file system?

Posted: Sat Sep 10, 2016 10:59 pm
by platforma
I've ported the function over to vfs_fat.c, which is then called from os_getfree(path). I don't have an esp module right now, have to dig it out tomorrow and give it a go.

Re: How to get free space of internal file system?

Posted: Sun Sep 11, 2016 10:39 am
by markxr
Obviously this is useful for data-logging applications which use logic like:

* Check free fs space
* If less than some threshold, delete the oldest file
* Write data to new file....

Re: How to get free space of internal file system?

Posted: Sun Sep 11, 2016 6:19 pm
by platforma
Hmmm...I can't get REPL from the current git master, no response at all. Previous alpha binaries flash and work correctly though. Anybody else is having similar issues? Straight after flashing the firmware I see the watchdog reset:

Code: Select all

 ets Jan  8 2013,rst cause:4, boot mode:(1,2)
wdt reset
Edit: Nevermind, it just worked all of a sudden...

Re: How to get free space of internal file system?

Posted: Mon Sep 12, 2016 12:26 pm
by platforma
There's now a PR #2413.