Page 1 of 1

how can I determine how many files are open on the SD card?

Posted: Sun Jul 24, 2022 8:02 pm
by RalphD
well the subject is the question. I studied a lot. VFS driver etc. may be somebody knows the solution ?

Re: how can I determine how many files are open on the SD card?

Posted: Mon Jul 25, 2022 3:44 am
by ESP_Sprite
That sounds a bit like an X-Y problem... Why would you need to know that?

Re: how can I determine how many files are open on the SD card?

Posted: Mon Jul 25, 2022 1:32 pm
by abansal22
RalphD wrote:
Sun Jul 24, 2022 8:02 pm
well the subject is the question. I studied a lot. VFS driver etc. may be somebody knows the solution ?
I am not aware of any function, but you can take one variable named index.Whenever you open file just increment the index pointer and when ever you closes the file just decrement the index pointer. Any time you can call the index which let you know the files open in the system.

Thanks
Ankit Bansal

Re: how can I determine how many files are open on the SD card?

Posted: Mon Jul 25, 2022 1:49 pm
by Craige Hales
Thanks for that: https://en.wikipedia.org/wiki/XY_problem
I did not know it had a name.

Re: how can I determine how many files are open on the SD card?

Posted: Mon Jul 25, 2022 4:54 pm
by RalphD
@ESP_sprite your question let me without words .... in multitask applications it may be likely that one task wants to reboot the device but don't you think a reboot with an open file is a good thing? and many more applications in a multitask environment. I was simply hoping to spare not to fiddle through the FATFS driver code.

somebody must have done this before.

Re: how can I determine how many files are open on the SD card?

Posted: Tue Jul 26, 2022 2:59 am
by ESP_Sprite
RalphD wrote:
Mon Jul 25, 2022 4:54 pm
@ESP_sprite your question let me without words .... in multitask applications it may be likely that one task wants to reboot the device but don't you think a reboot with an open file is a good thing? and many more applications in a multitask environment.
There we go; your actual question would be something like 'How do I make sure the SD card filesystem is in a consistent state when I order a reboot, without modifying the code that accesses the files'?

The answer is: I don't think you can, not without running the risk of corruption anyway. The thing is that even if you had a way of finding out the amount of files open, you'd run into a race condition: between the query and you shutting down the ESP chip, a thread may have opened a file, and you'll still get corruption

Note that in embedded devices, a voluntary reboot is actually not that common: it's way more common for the battery to run empty, the user pulls the power plug, a crash happens, whatnot. So in general, you should already develop your application with the capability to recover from e.g. half-written files in mind. If you do that, you get 'free' protection from that happening on a voluntary reboot as well.

Re: how can I determine how many files are open on the SD card?

Posted: Tue Jul 26, 2022 9:31 am
by RalphD
@ESP_sprite absolutely not my question. Leave it, you are too far out of real world applications and requirements.
The info I m looking for is there, in the FATFS driver. The fact that you have to give the number of allowed open files indicates that there is a structure having the space for n open file descriptors. This is the source to look at if I want to know how many files are open.

Re: how can I determine how many files are open on the SD card?

Posted: Wed Jul 27, 2022 12:38 am
by ESP_Sprite
Okidoki, good luck with that then.