Skip navigation.
 
mlCreating NSData objects from Resource Fork Data
FROM : Matthew Fahrenbacher
DATE : Sat Nov 09 23:11:01 2002

I was working on a project where I can distribute files at certain
times, but I'm running into a little snag.  It's painfully easy to use
NSData to archive a files data and keep its attributes in an
NSDictionary to be applied later.  The trick is to be able to maintain
the resource fork data as well.  Here's my attempt at doing it, and I
was wondering if anyone could help point out what I'm doing wrong (I'm
not very good with the Carbon APIs).

- (NSData *)loadResourceDataForFile:(NSString *)source_path
{
   //try to load resource data
        Handle handle;
        CFURLRef urlRef;
        SInt16 refNum;
        Str255 str;

        BOOL isDir;
   NSData *resourceData = nil;

        [[NSFileManager defaultManager] fileExistsAtPath:source_path
isDirectory:&isDir];

   //will this work if I don't escape out spaces?  i.e.
@"/Documents/Hello World"
        urlRef = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
(CFStringRef) source_path,
                                                kCFURLPOSIXPathStyle,
isDir);
        if (urlRef != NULL)
        {
            FSRef fsRef;
            if (CFURLGetFSRef(urlRef, &fsRef))
            {
                FSSpec spec;
                if(FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL,
&spec, NULL) == noErr)
                {
                    if((refNum = FSpOpenResFile(&spec,fsRdPerm)) == -1)
                        return nil;

           //load the resource data into memory
           //**
                    SetResLoad(TRUE);
                    LoadResource(handle);

                    if(ResError() != noErr || handle == NULL)
                        return nil;

                    //we've got data!
                    BlockMove(*handle, str, GetHandleSize(handle));
           //is this the right length?
                    resourceData = [[[NSData alloc] initWithBytes:str
length:256] autorelease];

                    //DisposeHandle(handle);
                    CloseResFile(refNum);
           //**
                }
            }
            CFRelease(urlRef);
        }

   return resourceData;
}


Everything is mostly the same for applying the resource fork data
except for between the stars (and the method signature)

- (void)applyResourceData:(NSData *)resourceData forFile:(NSString
*)source_path
//**
           const char *bytes = [resourceData bytes];

                        handle = NewHandle( bytes[0]+1 );
                        if (handle != NULL)
                        {
                            BlockMove( bytes, (* handle), bytes[0]+1 );
                            WriteResource(handle);
                            DisposeHandle(handle);
           }
//**


Thanks a lot,
Matt Fahrenbacher

Related mailsAuthorDate
mlCreating NSData objects from Resource Fork Data Matthew Fahrenbach… Nov 9, 23:11
mlRe: Creating NSData objects from Resource Fork Data Esteban Uribe Nov 10, 01:09
mlRe: Creating NSData objects from Resource Fork Data Esteban Uribe Nov 10, 01:16
mlRe: Creating NSData objects from Resource Fork Data Marcel Weiher Nov 10, 15:35
mlRe: Creating NSData objects from Resource Fork Data Matthew Fahrenbach… Nov 10, 15:40
mlRe: Creating NSData objects from Resource Fork Data Marcel Weiher Nov 10, 16:00
mlRe: Creating NSData objects from Resource Fork Data Nick Zitzmann Nov 10, 16:52
mlRe: Creating NSData objects from Resource Fork Data Matthew Fahrenbach… Nov 10, 17:09
mlRe: Creating NSData objects from Resource Fork Data David Dunham Nov 10, 20:30
mlRe: Creating NSData objects from Resource Fork Data Esteban Uribe Nov 11, 00:24
mlRe: Creating NSData objects from Resource Fork Data Nathan Day Nov 11, 01:06
mlRe: Creating NSData objects from Resource Fork Data Chris Parker Nov 11, 10:49
mlRe: Creating NSData objects from Resource Fork Data Matthew Fahrenbach… Nov 13, 21:00