]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/SettingsBundle/BonjourSCStore.m
mDNSResponder-878.250.4.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / SettingsBundle / BonjourSCStore.m
1 /*
2 *
3 * Copyright (c) 2016 Apple Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #import "BonjourSCStore.h"
19 #import <Foundation/Foundation.h>
20 #import <AssertMacros.h>
21
22 @implementation BonjourSCStore
23
24 + (NSArray * _Nullable)objectForKey:(NSString * _Nonnull)key
25 {
26 NSArray * result = nil;
27 SCPreferencesRef store;
28 OSStatus err;
29 NSDictionary * origDict;
30
31 store = SCPreferencesCreateWithAuthorization(kCFAllocatorDefault, SC_DYNDNS_PREFS_KEY, NULL, NULL);
32 require_action(store != NULL, SysConfigErr, err = SCError());
33 require_action(true == SCPreferencesLock(store, true), LockFailed, err = SCError());
34
35 origDict = (__bridge NSDictionary *)SCPreferencesPathGetValue(store, SC_DYNDNS_SYSTEM_KEY);
36 if (origDict) origDict = [NSDictionary dictionaryWithDictionary: origDict];
37
38 result = [origDict objectForKey: key];
39
40 SCPreferencesUnlock(store);
41
42 LockFailed:
43 CFRelease(store);
44 SysConfigErr:
45 return(result);
46 }
47
48 + (void)setObject:(NSArray * _Nullable)value forKey:(NSString * _Nonnull)key
49 {
50 SCPreferencesRef store;
51 OSStatus err;
52 NSMutableDictionary * origDict;
53 Boolean success;
54
55 store = SCPreferencesCreateWithAuthorization(kCFAllocatorDefault, SC_DYNDNS_PREFS_KEY, NULL, NULL);
56 require_action(store != NULL, SysConfigErr, err = SCError());
57 require_action(true == SCPreferencesLock(store, true), LockFailed, err = SCError());
58
59 origDict = (__bridge NSMutableDictionary *)SCPreferencesPathGetValue(store, SC_DYNDNS_SYSTEM_KEY);
60 if (!origDict) origDict = [NSMutableDictionary dictionary];
61 else origDict = [NSMutableDictionary dictionaryWithDictionary: origDict];
62
63 if (value.count) [origDict setObject: value forKey: key];
64 else [origDict removeObjectForKey: key];
65
66 success = SCPreferencesPathSetValue(store, SC_DYNDNS_SYSTEM_KEY, (__bridge CFDictionaryRef)origDict);
67 require_action(success, SCError, err = SCError(););
68
69 success = SCPreferencesCommitChanges(store);
70 require_action(success, SCError, err = SCError());
71 success = SCPreferencesApplyChanges(store);
72 require_action(success, SCError, err = SCError());
73
74 SCError:
75 SCPreferencesUnlock(store);
76 LockFailed:
77 CFRelease(store);
78 SysConfigErr:
79 return;
80 }
81
82
83 @end
84