|
| 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; |
| 12 | +use core_foundation::propertylist::{CFPropertyList, CFPropertyListSubClass}; |
| 13 | +use core_foundation::string::CFString; |
| 14 | +use core_foundation_sys::base::{CFRelease, kCFAllocatorDefault}; |
| 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 | + /// |
| 74 | + /// Use `CFPropertyList::downcast` to cast the result into the correct type. |
| 75 | + pub fn get<S: Into<CFString>>(&self, key: S) -> Option<CFPropertyList> { |
| 76 | + let cf_key = key.into(); |
| 77 | + unsafe { |
| 78 | + let dict_ref = |
| 79 | + SCDynamicStoreCopyValue(self.as_concrete_TypeRef(), cf_key.as_concrete_TypeRef()); |
| 80 | + if dict_ref != ptr::null() { |
| 81 | + Some(CFPropertyList::wrap_under_create_rule(dict_ref)) |
| 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>, R, V: CFPropertyListSubClass<R>>( |
| 91 | + &self, |
| 92 | + key: S, |
| 93 | + value: &V, |
| 94 | + ) -> bool { |
| 95 | + self.set_raw(key, &value.to_CFPropertyList()) |
| 96 | + } |
| 97 | + |
| 98 | + /// Sets the value of the given key. Overwrites existing values. |
| 99 | + /// Returns `true` on success, false on failure. |
| 100 | + pub fn set_raw<S: Into<CFString>>(&self, key: S, value: &CFPropertyList) -> bool { |
| 101 | + let cf_key = key.into(); |
| 102 | + let success = unsafe { |
| 103 | + SCDynamicStoreSetValue( |
| 104 | + self.as_concrete_TypeRef(), |
| 105 | + cf_key.as_concrete_TypeRef(), |
| 106 | + value.as_concrete_TypeRef(), |
| 107 | + ) |
| 108 | + }; |
| 109 | + success != 0 |
| 110 | + } |
| 111 | +} |
0 commit comments