Read resource fork using NSFileHandle

I use the old code shown below to read a resource fork file, it works fine but contains many deprecated API (under 10.8) so I want to convert it.

The documentation for FSReadFork says


Deprecation Statement

To read the data fork at the Foundation layer, use NSFileHandle APIs instead.


But using NSFileHandle doesn't work, someone can point me in the right direction?


+ (NSData*)old_readResForkFile:(NSString*)path {
    FSRef theFsRef;

    OSStatus status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &theFsRef, NULL);
    if (status != noErr) {
        return nil;
    }
    ResFileRefNum resFileRef = FSOpenResFile(&theFsRef, fsRdPerm);
    if (resFileRef < 0) {
        return nil;
    }

    NSMutableData* theData = nil;
    ByteCount theByteCount;
    SInt64 theForkSize;

    if (FSGetForkSize(resFileRef, &theForkSize) == noErr && theForkSize <= UINT_MAX) {
        theData = [NSMutableData dataWithLength:theForkSize];
        if (FSReadFork(resFileRef, fsFromStart, 0, theForkSize, [theData mutableBytes], &theByteCount) != noErr || (SInt64)theByteCount != theForkSize ) {
            theData = nil;
        }
    }
    CloseResFile(resFileRef);
    return theData;
}

Accepted Reply

You're really just reading the resource fork as unstructured data? That is, you're not using it for traditional resource records?


You really should transition to something else, perhaps application-specific extended attributes. See the setxattr() and getxattr() functions.


In the meantime, if you append "/..namedfork/rsrc" to a file's path, it's a path to the resource fork of the file. So, you can use such a path with NSFileHandle to access the resource fork.

Replies

You're really just reading the resource fork as unstructured data? That is, you're not using it for traditional resource records?


You really should transition to something else, perhaps application-specific extended attributes. See the setxattr() and getxattr() functions.


In the meantime, if you append "/..namedfork/rsrc" to a file's path, it's a path to the resource fork of the file. So, you can use such a path with NSFileHandle to access the resource fork.

Oh oh appending the string "/..namedfork/rsrc" seems to work fine, in the past I've found posts suggesting to add "/rsrc" but it wasn't the correct path component.


I read the res fork as unstructured data, I use that to compare the birary content


I can't simply use something else because these files are used by my app users and I can't control them


BTW with the correct componente path seems to work fine and finally I can remove the deprecated API.


Thanks a lot

hi


can you help me? what language is it?what development app do I use to do that?


thank you