Search code examples
c++macosposixmacos-carbon

How to set volume name when converting MacOSPath to POSIX


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


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