0

In a Carbon app I need to convert an HFS style MacOS path into a POSIX one that can be used in an fopen() call. For example:

my Vol:myFolder:myFile.jpg

to something like:

/my Vol/myFolder/myFile.jpg

If my Vol is my sytem disk, /myFolder/myFile.jpg works just fine, but if it's on a different volume, it does not work (ie. my Vol/myFolder/myFile.jpg fails.

How to I specify the volume here?

Thanks!

Bill

3
  • I'm not sure if I edited this right, because the example is inconsistent about whether the path separator is a colon or a slash, so I don't know if it's supposed to be an HFS path. Commented Jul 21, 2012 at 2:09
  • @JonathanGrynspan: Yes, and kCFURLHFSPathStyle still refers to a colon-delimited path. Commented Aug 6, 2012 at 16:22
  • Whiiiich is why my answer used it. ;) Commented Aug 6, 2012 at 16:37

2 Answers 2

2

An approach that avoids hard-coding (consider a volume not mounted in /Volumes/, such as a manually mounted one.)

CFStringRef myHFSPath = CFSTR("Macintosh HD:Some Folder:Some Subfolder:Some File");

CFURLRef url = CFURLCreateWithFileSystemPath(NULL, myHFSPath, kCFURLHFSPathStyle, FALSE);
if (url) {
    UInt8 posixPath[PATH_MAX * 2]; /* Extra-large because why not? */
    if (CFURLGetFileSystemRepresentation(url, TRUE, posixPath, sizeof(posixPath)) {
        /*
            posixPath now contains a C string suitable for passing to BSD and
            C functions like fopen().
        */
    }
    CFRelease(url);
}
Sign up to request clarification or add additional context in comments.

Comments

0

For POSIX style paths you need to preface secondary volumes with "/Volumes". So your example would be, /Volumes/myVol/myFolder/myFile.jpg. Note even if myVol is your system disk this works. So prefacing with /Volumes is always safe.

1 Comment

Eeeeh! This is hard-coding and subject to failure in the future or on a non-standard system configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.