Skip to content

Commit eb90a7d

Browse files
committed
Add SCDynamicConfiguration wrapper
1 parent c129f74 commit eb90a7d

File tree

2 files changed

+107
-7
lines changed

2 files changed

+107
-7
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2017 Amagicom AB.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
use core_foundation::base::TCFType;
10+
use core_foundation::boolean::CFBoolean;
11+
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
12+
use core_foundation::string::CFString;
13+
use core_foundation_sys::base::{CFRelease, kCFAllocatorDefault};
14+
use core_foundation_sys::propertylist::CFPropertyListRef;
15+
16+
use system_configuration_sys::dynamic_store::*;
17+
18+
use std::ptr;
19+
20+
/// Access to the key-value pairs in the dynamic store of a running system.
21+
pub struct SCDynamicStore(SCDynamicStoreRef);
22+
23+
impl Drop for SCDynamicStore {
24+
fn drop(&mut self) {
25+
unsafe { CFRelease(self.as_CFTypeRef()) }
26+
}
27+
}
28+
29+
impl_TCFType!(SCDynamicStore, SCDynamicStoreRef, SCDynamicStoreGetTypeID);
30+
31+
impl SCDynamicStore {
32+
/// Creates a new session used to interact with the dynamic store maintained by the System
33+
/// Configuration server.
34+
pub fn create<S: Into<CFString>>(name: S) -> Self {
35+
let cf_name = name.into();
36+
unsafe {
37+
let store = SCDynamicStoreCreate(
38+
kCFAllocatorDefault,
39+
cf_name.as_concrete_TypeRef(),
40+
None,
41+
ptr::null_mut(),
42+
);
43+
SCDynamicStore::wrap_under_create_rule(store)
44+
}
45+
}
46+
47+
/// Creates a new session used to interact with the dynamic store maintained by the System
48+
/// Configuration server. Uses [`SCDynamicStoreCreateWithOptions`] underneath and sets
49+
/// `kSCDynamicStoreUseSessionKeys` to true.
50+
///
51+
/// [`SCDynamicStoreCreateWithOptions`]: https://developer.apple.com/documentation/systemconfiguration/1437818-scdynamicstorecreatewithoptions?language=objc
52+
pub fn create_with_session_keys<S: Into<CFString>>(name: S) -> Self {
53+
let cf_name = name.into();
54+
unsafe {
55+
let store_options = CFDictionary::from_CFType_pairs(&[
56+
(
57+
CFString::wrap_under_create_rule(kSCDynamicStoreUseSessionKeys),
58+
CFBoolean::true_value(),
59+
),
60+
]);
61+
let store = SCDynamicStoreCreateWithOptions(
62+
kCFAllocatorDefault,
63+
cf_name.as_concrete_TypeRef(),
64+
store_options.as_concrete_TypeRef(),
65+
None,
66+
ptr::null_mut(),
67+
);
68+
SCDynamicStore::wrap_under_create_rule(store)
69+
}
70+
}
71+
72+
/// If the given key exists in the store, the associated value is returned.
73+
pub fn get<S: Into<CFString>>(&self, key: S) -> Option<CFDictionary> {
74+
let cf_key = key.into();
75+
unsafe {
76+
let dict_ref =
77+
SCDynamicStoreCopyValue(self.as_concrete_TypeRef(), cf_key.as_concrete_TypeRef());
78+
if dict_ref != ptr::null() {
79+
Some(CFDictionary::wrap_under_create_rule(
80+
dict_ref as *const _ as CFDictionaryRef,
81+
))
82+
} else {
83+
None
84+
}
85+
}
86+
}
87+
88+
/// Sets the value of the given key. Overwrites existing values.
89+
/// Returns `true` on success, false on failure.
90+
pub fn set<S: Into<CFString>>(&self, key: S, value: &CFDictionary) -> bool {
91+
let cf_key = key.into();
92+
let success = unsafe {
93+
SCDynamicStoreSetValue(
94+
self.as_concrete_TypeRef(),
95+
cf_key.as_concrete_TypeRef(),
96+
value.as_concrete_TypeRef() as CFPropertyListRef,
97+
)
98+
};
99+
success != 0
100+
}
101+
}

system-configuration/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
6-
}
7-
}
1+
#[macro_use]
2+
extern crate core_foundation;
3+
extern crate core_foundation_sys;
4+
extern crate system_configuration_sys;
5+
6+
pub mod dynamic_store;

0 commit comments

Comments
 (0)