/* * Copyright(c) 2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1(the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include #include #include "SCPPrivate.h" #include #include static CFArrayRef normalizePath(CFStringRef path) { CFArrayRef tmpElements; CFMutableArrayRef elements; CFIndex nElements; CFIndex i; if (!CFStringHasPrefix(path, CFSTR("/"))) { /* if no root separator */ return NULL; } tmpElements = CFStringCreateArrayBySeparatingStrings(NULL, path, CFSTR("/")); elements = CFArrayCreateMutableCopy(NULL, 0, tmpElements); CFRelease(tmpElements); /* remove empty path components */ nElements = CFArrayGetCount(elements); for (i=nElements; i>0; i--) { CFStringRef pathElement; pathElement = CFArrayGetValueAtIndex(elements, i-1); if (CFStringGetLength(pathElement) == 0) { CFArrayRemoveValueAtIndex(elements, i-1); nElements--; } } if (nElements < 1) { CFRelease(elements); return NULL; } return elements; } static SCPStatus getPath(SCPSessionRef session, CFStringRef path, CFMutableDictionaryRef *entity) { CFArrayRef elements; CFIndex i; CFIndex nElements; SCPStatus status; CFMutableDictionaryRef value = NULL; if (session == NULL) { return SCP_NOSESSION; /* you can't do anything with a closed session */ } elements = normalizePath(path); if (elements == NULL) { return SCP_NOKEY; } /* get preferences key */ status = SCPGet(session, CFArrayGetValueAtIndex(elements, 0), (CFPropertyListRef *)&value); if (status != SCP_OK) { goto done; } if (CFGetTypeID(value) != CFDictionaryGetTypeID()) { status = SCP_NOKEY; goto done; } nElements = CFArrayGetCount(elements); for (i=1; i 1) { CFDictionarySetValue(element, CFArrayGetValueAtIndex(elements, nElements-1), value); } status = SCPSet(session, CFArrayGetValueAtIndex(elements, 0), root); if (newRoot) CFRelease(root); CFRelease(elements); return status; } SCPStatus SCPPathSetLink(SCPSessionRef session, CFStringRef path, CFStringRef link) { CFMutableDictionaryRef dict; SCPStatus status; if (session == NULL) { return SCP_NOSESSION; /* you can't do anything with a closed session */ } dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionaryAddValue(dict, kSCResvLink, link); status = SCPPathSetValue(session, path, dict); CFRelease(dict); return status; } SCPStatus SCPPathRemove(SCPSessionRef session, CFStringRef path) { CFMutableDictionaryRef element; CFArrayRef elements = NULL; CFIndex i; CFIndex nElements; CFMutableDictionaryRef root = NULL; SCPStatus status = SCP_NOKEY; if (session == NULL) { return SCP_NOSESSION; /* you can't do anything with a closed session */ } elements = normalizePath(path); if (elements == NULL) { return SCP_NOKEY; } /* get preferences key */ status = SCPGet(session, CFArrayGetValueAtIndex(elements, 0), (CFPropertyListRef *)&root); if (status != SCP_OK) { goto done; } nElements = CFArrayGetCount(elements); if (nElements == 1) { /* if we are removing the data associated with the preference key */ status = SCPRemove(session, CFArrayGetValueAtIndex(elements, 0)); goto done; } element = root; for (i=1; i