#include "configd.h"
#include "session.h"
SCDStatus
_SCDRemove(SCDSessionRef session, CFStringRef key)
{
SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session;
SCDStatus scd_status = SCD_OK;
boolean_t wasLocked;
CFDictionaryRef dict;
CFMutableDictionaryRef newDict;
CFStringRef sessionKey;
SCDLog(LOG_DEBUG, CFSTR("_SCDRemove:"));
SCDLog(LOG_DEBUG, CFSTR(" key = %@"), key);
if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) {
return SCD_NOSESSION;
}
wasLocked = SCDOptionGet(NULL, kSCDOptionIsLocked);
if (!wasLocked) {
scd_status = _SCDLock(session);
if (scd_status != SCD_OK) {
SCDLog(LOG_DEBUG, CFSTR(" _SCDLock(): %s"), SCDError(scd_status));
return scd_status;
}
}
dict = CFDictionaryGetValue(cacheData, key);
if ((dict == NULL) || (CFDictionaryContainsKey(dict, kSCDData) == FALSE)) {
scd_status = SCD_NOKEY;
goto done;
}
newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
CFSetAddValue(changedKeys, key);
CFSetAddValue(deferredRemovals, key);
sessionKey = CFDictionaryGetValue(newDict, kSCDSession);
if (sessionKey) {
CFStringRef removedKey;
CFDictionaryRemoveValue(newDict, kSCDSession);
removedKey = CFStringCreateWithFormat(NULL, 0, CFSTR("%@:%@"), sessionKey, key);
CFSetAddValue(removedSessionKeys, removedKey);
CFRelease(removedKey);
}
CFDictionaryRemoveValue(newDict, kSCDData);
CFDictionaryRemoveValue(newDict, kSCDInstance);
if (CFDictionaryGetCount(newDict) > 0) {
CFDictionarySetValue(cacheData, key, newDict);
} else {
CFDictionaryRemoveValue(cacheData, key);
}
CFRelease(newDict);
done:
if (!wasLocked)
_SCDUnlock(session);
return scd_status;
}
kern_return_t
_configremove(mach_port_t server,
xmlData_t keyRef,
mach_msg_type_number_t keyLen,
int *scd_status
)
{
kern_return_t status;
serverSessionRef mySession = getSession(server);
CFDataRef xmlKey;
CFStringRef key;
CFStringRef xmlError;
SCDLog(LOG_DEBUG, CFSTR("Remove key from configuration database."));
SCDLog(LOG_DEBUG, CFSTR(" server = %d"), server);
xmlKey = CFDataCreate(NULL, keyRef, keyLen);
status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
if (status != KERN_SUCCESS) {
SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
}
key = CFPropertyListCreateFromXMLData(NULL,
xmlKey,
kCFPropertyListImmutable,
&xmlError);
CFRelease(xmlKey);
if (xmlError) {
SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() key: %s"), xmlError);
*scd_status = SCD_FAILED;
return KERN_SUCCESS;
}
*scd_status = _SCDRemove(mySession->session, key);
CFRelease(key);
return KERN_SUCCESS;
}