ccsakuweb
ccsakuweb

Reputation: 789

How to check the available free space in Haxe?

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?

Upvotes: 3

Views: 214

Answers (1)

D-side
D-side

Reputation: 9485

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.

Upvotes: -1

Related Questions