/* Built-in callback for POSIX domain. Note that we will pick up localizations from ErrnoErrors.strings in /System/Library/CoreServices/CoreTypes.bundle, if the file happens to be there. */ static CFTypeRef _CFErrorPOSIXCallBack(CFErrorRef err, CFStringRef key) { if (!CFEqual(key, kCFErrorDescriptionKey) && !CFEqual(key, kCFErrorLocalizedFailureReasonKey)) return NULL; const char *errCStr = strerror(CFErrorGetCode(err)); CFStringRef errStr = (errCStr && strlen(errCStr)) ? CFStringCreateWithCString(kCFAllocatorSystemDefault, errCStr, kCFStringEncodingUTF8) : NULL; if (!errStr) return NULL; if (CFEqual(key, kCFErrorDescriptionKey)) return errStr; // If all we wanted was the non-localized description, we're done #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS // We need a kCFErrorLocalizedFailureReasonKey, so look up a possible localization for the error message // Look for the bundle in /System/Library/CoreServices/CoreTypes.bundle CFArrayRef paths = CFCopySearchPathForDirectoriesInDomains(kCFLibraryDirectory, kCFSystemDomainMask, false); if (paths) { if (CFArrayGetCount(paths) > 0) { CFStringRef path = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("%@/CoreServices/CoreTypes.bundle"), CFArrayGetValueAtIndex(paths, 0)); CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path, kCFURLPOSIXPathStyle, false /* not a directory */); if (url) { CFBundleRef bundle = CFBundleCreate(kCFAllocatorSystemDefault, url); if (bundle) { // We only want to return a result if there was a localization CFStringRef localizedErrStr = CFBundleCopyLocalizedString(bundle, errStr, errStr, CFSTR("ErrnoErrors")); if (localizedErrStr == errStr) { CFRelease(localizedErrStr); CFRelease(errStr); errStr = NULL; } else { CFRelease(errStr); errStr = localizedErrStr; } CFRelease(bundle); } CFRelease(url); } CFRelease(path); } CFRelease(paths); } #endif return errStr; }
CFStringRef FSCopyFormatNameForFSType(CFStringRef fsType, int16_t fsSubtype, bool localized, bool encrypted) { CFTypeRef formatName; CFStringRef formatNameTableKey; CFIndex indx; if (NULL == fsType) return NULL; // Create a key for cache localized name table (i.e. "0hfs0") formatNameTableKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d%@%d"), (localized ? 1 : 0), fsType, fsSubtype); // Use OSSpinLock to protect the table accessed from multiple threads OSSpinLockLock(&__FSLocalizedNameTableLock); formatName = (void*)((NULL == __FSLocalizedNameTable) ? NULL : CFDictionaryGetValue(__FSLocalizedNameTable, (const void *)formatNameTableKey)); OSSpinLockUnlock(&__FSLocalizedNameTableLock); if (NULL == formatName) { // not in the cache CFBundleRef bundle = NULL; CFURLRef bundleURL; CFStringRef fsTypeName; static CFArrayRef searchPaths = NULL; /* Construct a bundle path URL from the fsType argument and create a CFBundle. We search (using CFCopySearchPathForDirectoriesInDomains) /Network/Library/Filesystems, /Library/Filesystems, and /System/Library/Filesystems. */ // Create CFURL for /System/Library/Filesystems and cache it if (NULL == searchPaths) { CFArrayRef tmpPaths = CFCopySearchPathForDirectoriesInDomains(kCFLibraryDirectory, kCFSystemDomainMask | kCFNetworkDomainMask | kCFLocalDomainMask, true); CFMutableArrayRef tmpStrings; CFIndex i; if (NULL == tmpPaths) return NULL; // No directories to search?!?! tmpStrings = CFArrayCreateMutable(NULL, CFArrayGetCount(tmpPaths), NULL); if (tmpStrings == NULL) goto done; for (i = 0; i < CFArrayGetCount(tmpPaths); i++) { CFStringRef tStr; CFURLRef tURL; char path[PATH_MAX + 1]; CFTypeRef tobject = CFArrayGetValueAtIndex(tmpPaths, i); if (CFGetTypeID(tobject) == CFURLGetTypeID()) { if (false == CFURLGetFileSystemRepresentation( tobject, false, (UInt8*)path, sizeof(path))) { goto done; } } else if (CFGetTypeID(tobject) == CFStringGetTypeID()) { CFStringGetCString(tobject, path, sizeof(path), kCFStringEncodingUTF8); } else { goto done; } strlcat(path, "/Filesystems", sizeof(path)); tStr = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8); if (tStr == NULL) goto done; tURL = CFURLCreateWithFileSystemPath(NULL, tStr, kCFURLPOSIXPathStyle, true); if (tURL) { CFArrayAppendValue(tmpStrings, tURL); } CFRelease(tStr); } searchPaths = CFArrayCreateCopy(NULL, tmpStrings); done: CFRelease(tmpStrings); CFRelease(tmpPaths); if (searchPaths == NULL) return NULL; } for (indx = 0; indx < CFArrayGetCount(searchPaths); indx++) { CFURLRef libRef = CFArrayGetValueAtIndex(searchPaths, indx); fsTypeName = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@.fs"), fsType); bundleURL = CFURLCreateWithFileSystemPathRelativeToBase(NULL, fsTypeName, kCFURLPOSIXPathStyle, true, libRef); bundle = CFBundleCreate(NULL, bundleURL); CFRelease(fsTypeName); CFRelease(bundleURL); if (NULL != bundle) { break; } } if (NULL != bundle) { // the bundle exists at path CFDictionaryRef localPersonalities = NULL; // Access the Info dictionary in the bundle CFDictionaryRef bundleDict = CFBundleGetInfoDictionary(bundle); // Get localized FSPersonalities only if we want localized name if (localized == true) { localPersonalities = CFBundleGetValueForInfoDictionaryKey(bundle, KEY_FS_PERSONALITIES); //NSLog(CFSTR("localPersonalities = %@\n"), localPersonalities); } /* Get global FSPersonalities. We need to access this since FSSubType exists only * in global FSPersonalities */ CFDictionaryRef globalPersonalities = CFDictionaryGetValue(bundleDict, (const void *) KEY_FS_PERSONALITIES); //NSLog(CFSTR("globalPersonalities = %@\n"), globalPersonalities); CFIndex numPersonalities; if (((NULL != localPersonalities) || (localized == false)) && // localPersonalities or we don't want localizations (NULL != globalPersonalities) && ((numPersonalities = CFDictionaryGetCount(globalPersonalities)) > 0)) { // read all FSPersonalities keys and values CFDictionaryRef valuesBuffer[MAX_FS_SUBTYPES]; CFStringRef keysBuffer[MAX_FS_SUBTYPES]; CFDictionaryRef *values = ((numPersonalities > MAX_FS_SUBTYPES) ? (CFDictionaryRef *)malloc(sizeof(CFDictionaryRef) * numPersonalities) : valuesBuffer); CFStringRef *keys = ((numPersonalities > MAX_FS_SUBTYPES) ? (CFStringRef *)malloc(sizeof(CFStringRef) * numPersonalities) : keysBuffer); CFDictionaryGetKeysAndValues(globalPersonalities, (const void **)keys, (const void **)values); // create CFNumberRef for the FSSubType CFNumberRef subTypeID = CFNumberCreate(NULL, kCFNumberSInt16Type, (const void *)&fsSubtype); CFStringRef FSNameKey = NULL; // search for valid FSSubType - we will use its key from global FSPersonalties to // access FSName from localized FSPersonalities CFIndex index; CFNumberRef readSubTypeID; for (index = 0; index < numPersonalities; index++) { if ((true == CFDictionaryGetValueIfPresent(values[index], (const void *)KEY_FS_SUBTYPE, (const void **)&readSubTypeID)) && (CFNumberCompare(subTypeID, readSubTypeID, NULL) == 0)) { FSNameKey = keys[index]; break; } } CFRelease(subTypeID); /* If a personality hasn't been found, use the last value in the dictionary (note the content of CFDictionary is unordered so choosing the last doesn't produce consistent result) */ if (NULL == FSNameKey) { FSNameKey = keys[numPersonalities - 1]; // is selecting the last entry right ? } // Get FSName from the FSPersonalities entry CFDictionaryRef FSNameDict; if (localized == true) { FSNameDict = CFDictionaryGetValue(localPersonalities, FSNameKey); } else { FSNameDict = CFDictionaryGetValue(globalPersonalities, FSNameKey); } if (NULL != FSNameDict) { CFStringRef tempName = CFDictionaryGetValue(FSNameDict, (const void *)KEY_FS_NAME); CFStringRef encrName = CFDictionaryGetValue(FSNameDict, CFSTR(kFSCoreStorageEncryptNameKey)); if (tempName) { if (encrName) { formatName = (void*)CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (formatName != NULL) { (void)CFDictionarySetValue((void*)formatName, tempName, encrName); } } else { formatName = tempName; } } } if (values != valuesBuffer) free(values); if (keys != keysBuffer) free(keys); } } // If all failed, return "Unknown format (f_fstypename)" as the last resort if (NULL == formatName) { static CFStringRef unknownTypeString = NULL; CFStringRef unknownFSNameString = NULL; // This should use the framework bundle this code resides. CarbonCore ??? */ if (NULL == unknownTypeString) unknownTypeString = CFCopyLocalizedString(UNKNOWN_FS_NAME, "This string is displayed when localized file system name cannot be determined."); unknownFSNameString = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@ (%@)"), unknownTypeString, fsType); formatName = (void*)unknownFSNameString; } // Cache the result OSSpinLockLock(&__FSLocalizedNameTableLock); if (NULL == __FSLocalizedNameTable) __FSLocalizedNameTable = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); // NSLog(CFSTR("Setting value %@ for key %@\n"), formatName, formatNameTableKey); CFDictionarySetValue(__FSLocalizedNameTable, (const void *)formatNameTableKey, (const void *)formatName); OSSpinLockUnlock(&__FSLocalizedNameTableLock); // NSLog(CFSTR("Localized Name Table = %@\n"), __FSLocalizedNameTable); if (NULL != bundle) CFRelease(bundle); // it has to be released here since formatName might be owned by the bundle } CFRelease(formatNameTableKey); if (CFGetTypeID(formatName) == CFStringGetTypeID()) { return CFRetain(formatName); } else if (CFGetTypeID(formatName) == CFDictionaryGetTypeID()) { // Dictionary with the (possibly localized) name as the key, and the encrypted name as the value // If we want the encrypted name, we return the value, else we return the key size_t numEntries = CFDictionaryGetCount((void*)formatName); void *keyNames[numEntries]; void *values[numEntries]; CFDictionaryGetKeysAndValues((void*)formatName, (const void**)&keyNames, (const void**)&values); if (encrypted) return CFRetain(values[0]); else return CFRetain(keyNames[0]); } return CFRetain(formatName); }