Question

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

Was it helpful?

Solution

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);
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top