Re: NSFileManager and Resource Forks
Re: NSFileManager and Resource Forks
- Subject: Re: NSFileManager and Resource Forks
- From: email@hidden
- Date: Wed, 27 Oct 2010 12:34:11 -0600
FYI ... this works just fine:
NSString *dataPath = [[NSBundle mainBundle]
pathForResource:@"RSRC" ofType:@"PCSMAC"];
NSString *rsrcPath = [dataPath
stringByAppendingString:@"/..namedfork/rsrc"];
NSData *data = [fm contentsAtPath:rsrcPath];
int len = [data length];
void *buf = malloc(len);
[data getBytes:buf];
FSRef fileRef;
if (CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:nspath],
&fileRef))
{
HFSUniStr255 resourceForkName;
FSGetResourceForkName(&resourceForkName);
if (FSCreateResourceFork(&fileRef, resourceForkName.length,
resourceForkName.unicode, 0) == noErr)
{
ResFileRefNum refNum;
if (FSOpenResourceFile(&fileRef, resourceForkName.length,
resourceForkName.unicode, fsRdWrPerm, &refNum) == noErr)
{
FSWriteFork(refNum,fsFromStart,NULL,(ByteCount)len,buf, NULL);
CloseResFile(refNum);
}
}
}
Thanks to all who responded!
- koko
On Oct 27, 2010, at 12:17 PM, Jean-Daniel Dupas wrote:
Le 27 oct. 2010 à 19:52, email@hidden a écrit :
Here is my code to write a resource fork for a given file.
NSString *dataPath = [[NSBundle mainBundle]
pathForResource:@"RSRC" ofType:@"PCSMAC"];
NSString *rsrcPath = [dataPath
stringByAppendingString:@"/..namedfork/rsrc"];
NSData *data = [fm contentsAtPath:rsrcPath];
NSString *outPath = [nspath
stringByAppendingString:@"/..namedfork/rsrc"];
ok = [fm createFileAtPath:outPath contents:data attributes:nil];
if(!ok)
NSLog(@"Resource fork not written for %@",nspath);
dataPath is the path to a file with a resource fork.
rsrcPath is the path to the resource fork
data does contain the resource fork data ... its size is equal to
the size of the resource fork.
outPath is the path to a file to which I want to add the resource
fork.
ok = [fm createFileAtPath:outPath contents:data attributes:nil];
returns NO.
My question is why is the resource fork not being written for
outpath?
-koko
On Oct 27, 2010, at 9:13 AM, Charles Srstka wrote:
On Oct 26, 2010, at 9:26 PM, email@hidden wrote:
Is there a way to write a resource fork for a file at a path?
If you want to read/write individual resources, you’ll have to use
the Carbon Resource Manager. However, if you want to read/write
the resource map directly to/from the file, you can do that with
NSFileManager by appending “/..namedfork/rsrc” to the end of the
pathname.
Charles
No, you don't need the Carbon rsrc manager, you need the
CoreServices File Manager, which is available to 64 bits app and
AFAIK, the only supported way to save resource forks.
Apple even recommends against using ..namedfork. If you want to
access it at BSD level, that last recommendation i saw was to use
xattr (http://lists.apple.com/archives/unix-porting/2007/Oct/msg00018.html
)
Use FSOpenFork passing the name obtained with FSGetResourceForkName
as argument to open the rsrc stream of the file, and then, use
FSWriteFork() to write your data.
-- Jean-Daniel
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden