#include "MDSPrefs.h"
#include <CoreFoundation/CFPreferences.h>
#include <stdlib.h>
MDSPrefs::MDSPrefs()
: mPluginFolders(NULL)
{
if (!readPathFromPrefs() && !readPathFromEnv())
useDefaultPath();
}
MDSPrefs::~MDSPrefs()
{
if (mPluginFolders)
CFRelease(mPluginFolders);
}
bool
MDSPrefs::readPathFromPrefs()
{
static const CFStringRef kPrefsSuite = CFSTR("com.apple.mds");
static const CFStringRef kPluginPathKey = CFSTR("securityPluginPath");
bool result = true;
CFPreferencesAddSuitePreferencesToApp(kCFPreferencesCurrentApplication, kPrefsSuite);
CFPropertyListRef value;
value = CFPreferencesCopyAppValue(kPluginPathKey, kCFPreferencesCurrentApplication);
if (CFGetTypeID(value) != CFArrayGetTypeID())
result = false;
else {
CFArrayRef array = (CFArrayRef) value;
int numItems = CFArrayGetCount(array);
for (int i = 0; i < numItems; i++)
if (CFGetTypeID(CFArrayGetValueAtIndex(array, i)) != CFStringGetTypeID()) {
result = false;
break;
}
}
if (result)
mPluginFolders = (CFArrayRef) value;
else
CFRelease(value);
return result;
}
bool
MDSPrefs::readPathFromEnv()
{
static const char *kPluginPathEnv = "MDSPATH";
static const CFStringRef kSeparator = CFSTR(":");
char *envValue = getenv(kPluginPathEnv);
if (envValue) {
CFStringRef path = CFStringCreateWithCString(NULL, envValue, kCFStringEncodingUTF8);
mPluginFolders = CFStringCreateArrayBySeparatingStrings(NULL, path, kSeparator);
CFRelease(path);
return true;
}
return false;
}
void
MDSPrefs::useDefaultPath()
{
static const CFStringRef kDefaultPluginPath = CFSTR("/System/Library/Security");
mPluginFolders = CFArrayCreate(NULL, (const void **) &kDefaultPluginPath, 1, &kCFTypeArrayCallBacks);
}
int
MDSPrefs::getNumberOfPluginFolders() const
{
if (mPluginFolders)
return CFArrayGetCount(mPluginFolders);
return 0;
}
const char *
MDSPrefs::getPluginFolder(int index)
{
if (mPluginFolders) {
int numValues = CFArrayGetCount(mPluginFolders);
if (index >= 0 && index < numValues) {
CFStringRef value = (CFStringRef) CFArrayGetValueAtIndex(mPluginFolders, index);
if (value) {
if (CFStringGetCString(value, mTempBuffer, kTempBufferSize, kCFStringEncodingUTF8))
return mTempBuffer;
}
}
}
return NULL;
}