FSRW "space remaining" function..?
CarlosFandango
Posts: 67
Hi All, I am working on an app that (amongst other things) writes files to an SD card over the serial link using FSRW. A function that returns the amount of remaining free space on the card would be extremely useful; although I can see a way that this could be done, I'd like to ask if anyone has already implemented this first. I'm not keen on introducing any bugs at this stage - if there is code available that someone is be willing to share, I would be very grateful.
Thanks!
-CF
Thanks!
-CF
Comments
http://forums.parallax.com/forums/default.aspx?f=25&m=201345
Where this is requested. Rokicki, if you're reading this, did you ever implement this function at all?
-CF
that shouldn't be too bad.
-tom
Hi Tom... well, as I said, it would be very useful to me. The device I'm working on offers the ability to upload files to the SD, but doing this blindly without having any idea of how much free space there is is probably not a good idea. Ideally I would query the device via comms, and find out if there is enough room for the file I want to upload, and if not, report an error.
So far I've used your code with only a few minor modifications (block level access mainly) but these have been very simple and didn't require your work to be totally understood. I think I could modify it further to get the free space, but it would take me I don't know how long and would certainly result in multiple breakages... so I guess I'm asking if you feel like adding this?!
-CF
There's probably a simple clever way to do it much like the directory reading code, where you just
adjust the block pointer to be pointing at the FAT and then scan linearly, looking for free FAT entries.
That sounds great! Better than being in London anyway, it's bl**dy freezing here... mind you at least it's not raining (not until tomorrow...!)
Anyway, no worries - I just asked on the offchance really. I'll add it, I'm sure it won't take too much effort. As of right now though I don't understand that much about the SD card structure at all, thanks to your efforts I haven't needed to(!) - if you can give me some tips about the FAT structure then that would help me a lot right now.
-CF
You should insert this (public) method into FSRW somewhere, and it will do what you asked!!!
I needed to add it for something I was doing....
[noparse][[/noparse]code]
Pub PdriveStats(Type):Space|Ctr
{{
'·· Returns drive capacity information, in bytes
'
'·· If Type is True...
'·· Returns amount of free space left on the drive
'
'·· If Type is False...
'·· Returns amount of total space allocated for files (regardless of whether used or not)
}}
· If Type 'Calculate free space, on drive, in clusters
·
··· Repeat Ctr from 2 to totclusters-1· 'Count through the FAT...
····· If byte[noparse][[/noparse]readfat(Ctr)]==0 'If the fat for this cluster = 0 (unused)
······· Space++ 'Increment counter
·······
· else 'Total drive space = totclusters
·
··· Space := totclusters
· 'Convert clusters to bytes
· Space <<= (clustershift+sectorshift)
[noparse][[/noparse]/code]
Regards M.R.B.