Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine file's extended attributes and resource forks with their size on Mac OSX?

I had written a small utility for creating xml for any folder structure and comparison of folders via generated xml that supports both win and Mac as platforms. However on Mac, recursively calculating folder size don't adds up to total size. On investigation, it came that it is due to extended attributes and resource forks that were present on certain files.

Can anybody know how can I determine these extended attributes and resource forks and their size preferably in python. Currently, I am using os.path.getsize to determine the size of file and adding files size to determine folder size eventually.

like image 766
Gagandeep Singh Avatar asked Sep 27 '11 17:09

Gagandeep Singh


1 Answers

You want the hidden member of a stat result called st_blocks.

>>> s = os.stat('some_file')
>>> s
posix.stat_result(st_mode=33261, st_ino=12583347, st_dev=234881026,
                  st_nlink=1, st_uid=1000, st_gid=20, st_size=9889973,
                  st_atime=1301371810, st_mtime=847731600, st_ctime=1301371422)
>>> s.st_size / 1e6 # size of data fork only, in MB
9.889973
>>> x.st_blocks * 512e-6 # total size on disk, in MB
20.758528

The file in question has about 10 MB in the resource fork, which shows up in the result from stat but in a "hidden" attribute. (Bonus points for anyone who knows exactly which file this is.) Note that it is documented in man 2 stat that the st_blocks attribute always measures increments of 512 bytes.

Note: st_size measures the number of bytes of data, but st_blocks measures size on disk including the overhead from partially used blocks. So,

>>> open('file.txt', 'w').write('Hello, world!')
13
>>> s = os.stat('file.txt')
>>> s.st_size
13
>>> s.st_blocks * 512
4096

Now if you do a "Get Info" in the Finder, you'll see that the file has:

Size: 4 KB on disk (13 bytes)

like image 111
Dietrich Epp Avatar answered Oct 11 '22 15:10

Dietrich Epp