Check for resource fork in Objective C

568 Views Asked by At

How do i check whether a file exists with resource fork or not?

Note : With out using the API's of CarbonCore->Resources.h, Since they are deprecated.

I will be able to open the file where there exist resource fork or not. But i need to check whether the resource fork exists or not. This is what exactly i'm looking for.

3

There are 3 best solutions below

1
On BEST ANSWER

Use getattrlist or fgetattrlist passing ATTR_FILE_RSRCLENGTH in the attrList parameter. see the man page for getattrlist.

0
On

For those who want to get the actual resource fork length, here's the code:

#include <sys/attr.h>

struct attrlist attrlist = {0};
attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
attrlist.fileattr    = ATTR_FILE_RSRCLENGTH;
typedef struct __attribute__((__packed__)) {
    uint32_t size;
    off_t forkSize;
} result_t;
result_t result = {0};
if (getattrlist (filepath, &attrlist, &result, sizeof(result), 0) == 0) {
    assert(sizeof(result)==result.size); // makes sure we got the struct right
    // now we find the rsrc fork size in result.forkSize
}
0
On

Alternatively

BOOL hasResourceFork = getxattr("/path/To/file", "com.apple.ResourceFork", NULL, 0, 0, 0) != -1;

That requires

#include <sys/xattr.h>