How to read and write resource forks in OS X 64 bit applications

2.6k Views Asked by At

Writing an archival utility, I need to be able to read and write resource forks on Mac OS X file systems.

There used to exist FSOpenFork and related functions, but current documentation both online and included with Xcode (v7.1) does not even mention these functions any more.

Furthermore, functions such as GetEOF are not even available any more to 64 bit apps.

Which APIs are currently available for reading and writing resource forks? If you can, please provide the declarations for functions to open, read, write, close and inquire/set the EOF and current r/w offset.

Note: While I've added my own answer listing the replacement FS... functions, it would be nice if others could add more ways, such as using CFURL APIs or the named fork method (which uses a special file name, but I keep forgetting how that works).

2

There are 2 best solutions below

0
On BEST ANSWER

The header file "Files.h" lists the available FS... functions.

Here are a few replacements:

  • SetFPos -> FSSetForkPosition
  • GetEOF -> FSGetForkSize
  • FSRead -> FSReadFork
0
On

You can open the resource fork as a regular file by appending /..namedfork/rsrc. (This is defined in <sys/paths.h> as _PATH_RSRCFORKSPEC.) Although ugly, it does enable shell activities to access the resource fork even if there not aware resource forks exist. Example:

ls -l myfile/..namedfork/rsrc
cp myfile/..namedfork/rsrc ...

The resource fork is also available as the com.apple.ResourceFork extended attributes. (This is defined as XATTR_RESOURCEFORK_NAME in <sys/xattr.h>.) In particular, f/getxattr() and f/setxattr() can read and write the resource fork. The _PATH_RSRCFORKSPEC hack is actually implemented in terms of extended attributes by the XNU kernel.