3

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.

hippietrail
  • 14,735
  • 16
  • 96
  • 147
Abdul Naveed
  • 567
  • 2
  • 9
  • possible duplicate of [How to open and read resource forks in Obj-C](http://stackoverflow.com/questions/19400043/how-to-open-and-read-resource-forks-in-obj-c) – Darren Aug 14 '15 at 06:51
  • 2
    @Darren, not a duplicate, testing whether the fork exists is not the same as actually reading the fork. – JWWalker Aug 14 '15 at 16:24

3 Answers3

4

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

Thomas Tempelmann
  • 10,222
  • 7
  • 66
  • 132
geowar
  • 4,294
  • 1
  • 26
  • 23
1

Alternatively

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

That requires

#include <sys/xattr.h>
vadian
  • 253,546
  • 28
  • 306
  • 323
0

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
}
Thomas Tempelmann
  • 10,222
  • 7
  • 66
  • 132