3

I don't find a way to check the free space available in a device using Haxe, Openfl, Lime or another library. I would like to avoid download data that will exceed the size recommended for an app in each device. What do you do to check that?

Alexander L. Hayes
  • 3,892
  • 4
  • 13
  • 34
ccsakuweb
  • 789
  • 5
  • 17
  • Which platforms are you targeting? – Jonas Malaco Sep 27 '15 at 04:20
  • Both iOS and Android have APIs for querying free space, and so do most other devices out there. If a cross platform openfl/lime extension doesn't exist already, the best solution would be to code one yourself. In rare cases you might also be able to call the native APIs directly. – Jonas Malaco Sep 27 '15 at 08:24

1 Answers1

-1

Try creating a file of that size! Then either delete it or reopen and write (not append) over its contents.

I don't know whether all platforms Haxe supports will work fine with this trick, but this algorithm is reported to work in many places and languages (I personally tested it in Ruby and saw the same suggestion for C++/.NET). To check whether X bytes of disk space are available:

  • open a new file for writing
  • seek X-1 bytes from the beginning
  • write a byte of data (whatever you want, 0, 42...)
  • close the file (probably unrelated to the task at hand, but don't forget to do that anyway)

If there's insufficient disk space, you'll likely get an exception at some point in this algorithm. You'll have to find out what errors to expect and process them properly.

Using ihx I've found this is working and requires nothing but Haxe Standard Library:

haxe interactive shell v0.3.4
type "help" for help
>> import sys.io.*; 
>> var f = File.write('loca', true) 
sys.io.FileOutput : { __f => #abstract }
>> f.seek(39999, FileSeek.SeekBegin) 
Void : null
>> f.writeByte(0) 
Void : null
>> f.close() 
Void : null

After these manipulations, I had a file named loca of exactly 40000 bytes in my working directory.

By the way, be careful when doing things like these in ihx since it re-runs the entire session with the last entered line appended each time.

Ongoing experimentation:

However, when there's insufficient disk space, it may not fail with errors. In this case you'll have to check the real size with sys.FileSystem.stat(path).size. And don't forget to delete the file if there's not enough space.

Community
  • 1
  • 1
D-side
  • 9,150
  • 3
  • 28
  • 44
  • If you have a scenario where it's somewhat likely to run out of space, it doesn't seem too rational to create large files just for figuring out how much space you can actually use. At least, why is this better than just saving whatever you need to save and hoping for the best? – Jonas Malaco Sep 27 '15 at 04:24
  • @jonasmalacofilho because "whatever you need to save" is not yet on the device, the process is potentially slow/expensive and silently fails given lack of disk space. It's best to reserve space beforehand to make sure that (a) there is enough (b) other processes elsewhere in the file system don't eat up the necessary space during the download. – D-side Sep 27 '15 at 08:16
  • Fair, but this assumes that your process is more important than all others, up to the point that it should still try to use all available space and make the device unusable. – Jonas Malaco Sep 27 '15 at 08:19
  • @jonasmalacofilho I haven't seen a lot of software that actually cares. Some OSes are kind enough to warn about this overall risky situation, but decision is still up to the user. – D-side Sep 27 '15 at 08:30
  • Yeah, and situations where storage exhaustion needs to be handled gracefully are rare too. However, that's the point of the question, no? Since he's trying to respect some recommended storage size, it seems to me that he wants to decide whether or not to save some non essential data; perhaps it's some kind of cache. – Jonas Malaco Sep 27 '15 at 08:35
  • Actually, I'm guessing about the "non essential" part since he talks about recommended storage limits, not actual ones... – Jonas Malaco Sep 27 '15 at 08:38
  • @jonasmalacofilho actually, I've never heard of any "recommended storage limit" on any platform. So I suggest we wait for the OP. Meanwhile, this can perfectly solve some cases like reserving disk space for a single-file known-size download. – D-side Sep 27 '15 at 08:49
  • 1
    Hi, thank you for the answers. I have to download a set of files before save them. And I want to avoid all the process if there is not space in the device for all the files. If there is not space I want to notify the user to let him decide remove some old content to download the new set. Sometimes the app will need to download and save 400MB. I would need to write in this case a file of 400MB? I think this is not an optimal solution. So I will try the sugestion of @jonasmalacolfilho if there is not any extension already made. – ccsakuweb Sep 27 '15 at 11:45
  • @ccsakuweb it's more like writing one byte after seeking 400MB-1 through nothing, that instructs the OS to reserve the given space. Then you can write the downloaded data right into it (over the reserved space), avoiding the chance of another application taking space while the download is in progress. It's your call. – D-side Sep 27 '15 at 11:51