1

I'm trying to get the size on disk of a file in iOS using Objective C. As of now I've been able to get the actual size of the file and other file information using NSFileManager and then getting the attributes attributesOfItemAtPath:error but not the size on disk. I also tried getting the file size from struct stat but again it doesn't give me size on disk.I tried using NSTask to make a call to du -h but iOS didn't allow me to fork other processes. Any ideas are welcome :)

I know this questions is similar to many others but the difference is that I'm trying to do this in iOS and most of the methods used in other systems don't work here.

Thanks

F2_CMD
  • 123
  • 2
  • 6

1 Answers1

3

EDIT: It's worth reading Nikolai Ruhe's answer to the same question here: https://stackoverflow.com/a/28660040/97337.

See What is the block size of the iphone filesystem? for information on the iOS block size. The size of a file on disk will be it's stat size rounded up to the next block.

Community
  • 1
  • 1
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • So basically I need to find the block size. I was thinking on using `st_blocks * 512` but I don't know if `st_blocks` is always the number of 512 bytes or this varies from OS to OS? Are there any methods to get the block size? – F2_CMD Apr 08 '12 at 00:18
  • getmntinfo(). See the linked question (What is the block size of the iphone filesystem?" The answer is 8k. – Rob Napier Apr 08 '12 at 01:09
  • Thanks for the replies. Obviously, I read the link but I want to know if it's possible using `st_blocks * 512`. This depends on whether stat always reports `st_blocks` in units of 512. It seems to me that this is the case. – F2_CMD Apr 08 '12 at 06:18
  • 1
    Sorry; I misunderstood. Yes, st_blocks is always in 512 byte units, no matter what the filesystem block size is. – Rob Napier Apr 08 '12 at 16:42
  • 1
    A file's allocated size on the volume is even more complex to determine. There's meta data (extended attributes are extensively used on iOS and that takes up space). Also, HFS supports file system level compression that might actually reduce allocated space. Cocoa has support to get most of this information using `NSURL`'s `NSURLTotalFileAllocatedSizeKey` property. There's some more information [here](http://stackoverflow.com/a/28660040/104790). – Nikolai Ruhe Feb 23 '15 at 09:09