react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

freeSpace from getFSInfo() does not match actual free space on device

Open ivankdev opened this issue 3 years ago • 2 comments

do

const spaceInfo = await getFSInfo();

Real device (iPad) gives me 43GB available - at the same time react-native-fs gives me 38GB available? Any system files is not under calculation or I use it somehow wrongly?

Screen Shot 2022-06-28 at 17 21 21 Screen Shot 2022-06-28 at 17 21 46

ivankdev avatar Jun 28 '22 14:06 ivankdev

Same issue here

Stefuu avatar Feb 05 '24 15:02 Stefuu

I bumped into this same issue. This library uses attributesOfFileSystemForPath and gets the key NSFileSystemFreeSize to get the free storage. Although, Apple suggests another way to get this info.

What seems to happen is that iOS manages some files from the filesystem, deleting or uploading them to cloud to free some space if necessary. So, for example, if an iPhone has X GB's of free storage, and it has 50GBs of "ephemeral" files, the total free space is "X + 50". The iPhone "Settings" app actually reports "X + 50", and this lib reports only "X".

By using Apple's suggested method to get free space (resourceValues(forKeys: [...]), we can use three keys:

  • NSURLVolumeAvailableCapacityKey, which returns the actual free space in the file system without any management from the OS, should return X in our example.
  • NSURLVolumeAvailableCapacityForImportantUsageKey, which returns what is reported by the Settings app, X + 50 in our example. This is probably what you want to use.
  • NSURLVolumeAvailableCapacityForOpportunisticUsageKey, which returns the free space for these "ephemeral" files.

This post also explains the same thing.

I can submit a PR if the maintainers agree.

GGGava avatar Feb 06 '24 14:02 GGGava