How to get free space of internal file system?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
jarda
Posts: 3
Joined: Sun Sep 04, 2016 10:13 pm

How to get free space of internal file system?

Post by jarda » Sun Sep 04, 2016 10:22 pm

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.

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

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

Post by platforma » Mon Sep 05, 2016 11:26 am

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.

mad474
Posts: 60
Joined: Sun Dec 29, 2013 7:48 pm

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

Post by mad474 » Mon Sep 05, 2016 12:25 pm

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

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

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

Post by markxr » Mon Sep 05, 2016 3:54 pm

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.

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

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

Post by platforma » Fri Sep 09, 2016 2:24 pm

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.

Online
User avatar
dhylands
Posts: 3234
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

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

Post by dhylands » Fri Sep 09, 2016 5:04 pm

stmhal has the statvfs function, which should be portable to any other codebase which uses fatfs
https://github.com/micropython/micropyt ... #L296-L324

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

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

Post by platforma » Sat Sep 10, 2016 10:59 pm

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.

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

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

Post by markxr » Sun Sep 11, 2016 10:39 am

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....

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

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

Post by platforma » Sun Sep 11, 2016 6:19 pm

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...

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

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

Post by platforma » Mon Sep 12, 2016 12:26 pm

There's now a PR #2413.

Post Reply