How to define extended file attributes and resource forks with their size on Mac OSX?

I wrote a small utility for creating xml for any folder structure and comparing folders through generated xml, which supports both win and Mac platforms. However, on a Mac, recursive calculation of folder size does not stack to the total size. The study revealed that this is due to the extended attributes and resource forks that were present in certain files.

Does anyone know how I can define these extended attributes and resource forks and their size, preferably in python. I am currently using os.path.getsize to determine the file size and add file sizes to determine the size of the folder.

+4
source share
3 answers

You need a hidden stat result element 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 

This file has about 10 MB in the fork resource, which appears as a result of stat , but in the "hidden" attribute. (Bonus points for anyone who knows exactly what file it is.) Note that the man 2 stat states 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 the size of the disk, including overhead from partially used blocks. In this way,

 >>> 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 "Get Information" in Finder, you will see that the file has:

Size: 4 KB on disk (13 bytes)

+2
source

Two options:

You can try using subprocess to invoke the "ls" or "du" command, which should know about extended attributes.

or

You can install the xattr package, which can read the resource plug in addition to the extended attributes (it can be accessed via xattr.XATTR_RESOURCEFORK_NAME . Maybe something like this:

 import xattr x = xattr.xattr("/path/to/my/file") size_ = 0 for attribute in x: size_ += len(x[attribute]) print size_ 

You may need to play around a bit with the extended attribute format, as they are returned as strings, but can be binary (?).

If you provide a minimal, almost working, example code, I could play a little more with it.

+1
source

Just a partial answer ... but to find out the size of the resource rounds, you can simply use the named psuedodirectory file

 os.path.getsize("<path to file of interest>/..namedfork/rsrc") 

It is theoretically possible that other named forks may exist ... but you cannot find the list of available forks.

As for the extended attributes ... what "size" are you interested in? You can use the xattr module to detect their contents and, therefore, the length of the key / value pairs.

But if you are more interested in the size "on disk" ... then it is worth noting that the extended attributes are not stored in any file. They form part of the file’s metadata (that is, just like the name and modified time are metadata) and are stored directly in the B * -tree node, and not in any β€œfile”

+1
source

All Articles