Hello,
in my app I need the current amount of free space on a certain volume.
I use the following code to the this information:
if let mountedVolumeURLs = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil)
{
for volumeUrl in mountedVolumeURLs
{
guard let values = try? self.url.resourceValues(forKeys: [.volumeTotalCapacityKey, .volumeAvailableCapacityKey]) else { continue }
let totalBytes = values.allValues[.volumeTotalCapacityKey] as? UInt64 ?? 0
let freeBytes = values.allValues[.volumeAvailableCapacityKey] as? UInt64 ?? 0 // This is not always correct.
}
}
When writing to a volume and then calling the code above, a wrong amount is returned. I suspect that there is still some cacheing going on.
Is there any function that I can call to get this information from the file system without any cacheing? I guess an alternative would be to get a list of all files, get their sizes, add them up and subtract that number from the total bytes of the volume. But maybe there is a more elegant solution.
I also tried something like this:
let handle = try FileDescriptor.open(volumeUrl.path, .writeOnly, options: .create, permissions: .ownerReadWrite, retryOnInterrupt: true)
fcntl(handle.rawValue, F_FULLFSYNC)
But this did not help. I would appreciate any help.
Regards