systemconfiguration-rs 0.5.0

Safe Rust bindings for Apple's SystemConfiguration framework via a Swift bridge on macOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::{
    ffi::c_void,
    ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign},
    ptr::NonNull,
    sync::{Arc, Mutex},
};

use crate::{bridge, error::Result, ffi, network_services::NetworkService, PropertyList};

struct CallbackState {
    callback: Box<dyn FnMut(PreferencesNotification) + Send>,
}

unsafe extern "C" fn preferences_callback(notification_type: u32, info: *mut c_void) {
    if info.is_null() {
        return;
    }

    let mutex = unsafe { &*info.cast::<Mutex<CallbackState>>() };
    if let Ok(mut state) = mutex.lock() {
        (state.callback)(PreferencesNotification::from_raw(notification_type));
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PreferencesNotification(u32);

impl PreferencesNotification {
    pub const COMMIT: Self = Self(1 << 0);
    pub const APPLY: Self = Self(1 << 1);

    pub const fn from_raw(raw: u32) -> Self {
        Self(raw)
    }

    pub const fn raw_value(self) -> u32 {
        self.0
    }

    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }
}

impl BitOr for PreferencesNotification {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

impl BitOrAssign for PreferencesNotification {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

impl BitAnd for PreferencesNotification {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        Self(self.0 & rhs.0)
    }
}

impl BitAndAssign for PreferencesNotification {
    fn bitand_assign(&mut self, rhs: Self) {
        self.0 &= rhs.0;
    }
}

#[derive(Clone)]
pub struct Preferences {
    raw: bridge::OwnedHandle,
    callback_state: Option<Arc<Mutex<CallbackState>>>,
}

impl std::fmt::Debug for Preferences {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Preferences").finish_non_exhaustive()
    }
}

impl Preferences {
    pub fn type_id() -> u64 {
        unsafe { ffi::preferences::sc_preferences_get_type_id() }
    }

    pub fn new(name: &str, prefs_id: Option<&str>) -> Result<Self> {
        Self::create(name, prefs_id)
    }

    pub fn new_with_authorization(name: &str, prefs_id: Option<&str>) -> Result<Self> {
        unsafe { Self::create_with_authorization(name, prefs_id, None) }
    }

    /// # Safety
    ///
    /// `authorization` must be a valid `AuthorizationRef` obtained from a
    /// compatible Security-framework binding, and it must remain valid for the
    /// lifetime requirements imposed by `SCPreferences`.
    pub unsafe fn new_with_authorization_raw(
        name: &str,
        prefs_id: Option<&str>,
        authorization: Option<NonNull<c_void>>,
    ) -> Result<Self> {
        unsafe { Self::create_with_authorization(name, prefs_id, authorization) }
    }

    pub fn new_with_callback<F>(name: &str, prefs_id: Option<&str>, callback: F) -> Result<Self>
    where
        F: FnMut(PreferencesNotification) + Send + 'static,
    {
        let mut preferences = Self::new(name, prefs_id)?;
        preferences.set_callback(callback)?;
        Ok(preferences)
    }

    pub fn new_with_authorization_and_callback<F>(
        name: &str,
        prefs_id: Option<&str>,
        callback: F,
    ) -> Result<Self>
    where
        F: FnMut(PreferencesNotification) + Send + 'static,
    {
        let mut preferences = Self::new_with_authorization(name, prefs_id)?;
        preferences.set_callback(callback)?;
        Ok(preferences)
    }

    /// # Safety
    ///
    /// `authorization` must be a valid `AuthorizationRef` obtained from a
    /// compatible Security-framework binding, and it must remain valid for the
    /// lifetime requirements imposed by `SCPreferences`.
    pub unsafe fn new_with_authorization_raw_and_callback<F>(
        name: &str,
        prefs_id: Option<&str>,
        authorization: Option<NonNull<c_void>>,
        callback: F,
    ) -> Result<Self>
    where
        F: FnMut(PreferencesNotification) + Send + 'static,
    {
        let mut preferences =
            unsafe { Self::create_with_authorization(name, prefs_id, authorization) }?;
        preferences.set_callback(callback)?;
        Ok(preferences)
    }

    fn create(name: &str, prefs_id: Option<&str>) -> Result<Self> {
        let name = bridge::cstring(name, "sc_preferences_create")?;
        let prefs_id = bridge::optional_cstring(prefs_id, "sc_preferences_create")?;
        let raw = unsafe {
            ffi::preferences::sc_preferences_create(
                name.as_ptr(),
                prefs_id
                    .as_ref()
                    .map_or(std::ptr::null(), |value| value.as_ptr()),
            )
        };
        let raw = bridge::owned_handle_or_last("sc_preferences_create", raw)?;
        Ok(Self {
            raw,
            callback_state: None,
        })
    }

    unsafe fn create_with_authorization(
        name: &str,
        prefs_id: Option<&str>,
        authorization: Option<NonNull<c_void>>,
    ) -> Result<Self> {
        let name = bridge::cstring(name, "sc_preferences_create_with_authorization")?;
        let prefs_id =
            bridge::optional_cstring(prefs_id, "sc_preferences_create_with_authorization")?;
        let raw = unsafe {
            ffi::preferences::sc_preferences_create_with_authorization(
                name.as_ptr(),
                prefs_id
                    .as_ref()
                    .map_or(std::ptr::null(), |value| value.as_ptr()),
                authorization.map_or(std::ptr::null_mut(), NonNull::as_ptr),
            )
        };
        let raw = bridge::owned_handle_or_last("sc_preferences_create_with_authorization", raw)?;
        Ok(Self {
            raw,
            callback_state: None,
        })
    }

    pub fn set_callback<F>(&mut self, callback: F) -> Result<()>
    where
        F: FnMut(PreferencesNotification) + Send + 'static,
    {
        let state = Arc::new(Mutex::new(CallbackState {
            callback: Box::new(callback),
        }));
        self.set_callback_state(Some(state))
    }

    pub fn clear_callback(&mut self) -> Result<()> {
        self.set_callback_state(None)
    }

    fn set_callback_state(&mut self, callback: Option<Arc<Mutex<CallbackState>>>) -> Result<()> {
        let ok = unsafe {
            ffi::preferences::sc_preferences_set_callback(
                self.raw.as_ptr(),
                callback
                    .as_ref()
                    .map(|_| preferences_callback as unsafe extern "C" fn(u32, *mut c_void)),
                callback.as_ref().map_or(std::ptr::null_mut(), |state| {
                    Arc::as_ptr(state).cast_mut().cast::<c_void>()
                }),
            )
        };
        bridge::bool_result("sc_preferences_set_callback", ok)?;
        self.callback_state = callback;
        Ok(())
    }

    pub fn schedule_with_run_loop_current(&self) -> Result<()> {
        let ok = unsafe {
            ffi::preferences::sc_preferences_schedule_with_run_loop_current(self.raw.as_ptr())
        };
        bridge::bool_result("sc_preferences_schedule_with_run_loop_current", ok)
    }

    pub fn unschedule_from_run_loop_current(&self) -> Result<()> {
        let ok = unsafe {
            ffi::preferences::sc_preferences_unschedule_from_run_loop_current(self.raw.as_ptr())
        };
        bridge::bool_result("sc_preferences_unschedule_from_run_loop_current", ok)
    }

    pub fn set_dispatch_queue_global(&self) -> Result<()> {
        let ok = unsafe {
            ffi::preferences::sc_preferences_set_dispatch_queue_global(self.raw.as_ptr())
        };
        bridge::bool_result("sc_preferences_set_dispatch_queue_global", ok)
    }

    pub fn clear_dispatch_queue(&self) -> Result<()> {
        let ok =
            unsafe { ffi::preferences::sc_preferences_clear_dispatch_queue(self.raw.as_ptr()) };
        bridge::bool_result("sc_preferences_clear_dispatch_queue", ok)
    }

    pub fn lock(&self, wait: bool) -> Result<()> {
        let ok =
            unsafe { ffi::preferences::sc_preferences_lock(self.raw.as_ptr(), u8::from(wait)) };
        bridge::bool_result("sc_preferences_lock", ok)
    }

    pub fn commit_changes(&self) -> Result<()> {
        let ok = unsafe { ffi::preferences::sc_preferences_commit_changes(self.raw.as_ptr()) };
        bridge::bool_result("sc_preferences_commit_changes", ok)
    }

    pub fn apply_changes(&self) -> Result<()> {
        let ok = unsafe { ffi::preferences::sc_preferences_apply_changes(self.raw.as_ptr()) };
        bridge::bool_result("sc_preferences_apply_changes", ok)
    }

    pub fn unlock(&self) -> Result<()> {
        let ok = unsafe { ffi::preferences::sc_preferences_unlock(self.raw.as_ptr()) };
        bridge::bool_result("sc_preferences_unlock", ok)
    }

    pub fn synchronize(&self) {
        unsafe { ffi::preferences::sc_preferences_synchronize(self.raw.as_ptr()) };
    }

    pub fn signature(&self) -> Option<String> {
        bridge::take_optional_string(unsafe {
            ffi::preferences::sc_preferences_copy_signature(self.raw.as_ptr())
        })
    }

    pub fn copy_key_list(&self) -> Vec<String> {
        bridge::take_string_array(unsafe {
            ffi::preferences::sc_preferences_copy_key_list(self.raw.as_ptr())
        })
    }

    pub fn get_value(&self, key: &str) -> Result<Option<PropertyList>> {
        let key = bridge::cstring(key, "sc_preferences_get_value")?;
        let raw =
            unsafe { ffi::preferences::sc_preferences_get_value(self.raw.as_ptr(), key.as_ptr()) };
        Ok(unsafe { bridge::OwnedHandle::from_raw(raw) }.map(PropertyList::from_owned_handle))
    }

    pub fn add_value(&self, key: &str, value: &PropertyList) -> Result<()> {
        let key = bridge::cstring(key, "sc_preferences_add_value")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_add_value(
                self.raw.as_ptr(),
                key.as_ptr(),
                value.as_ptr(),
            )
        };
        bridge::bool_result("sc_preferences_add_value", ok)
    }

    pub fn set_value(&self, key: &str, value: &PropertyList) -> Result<()> {
        let key = bridge::cstring(key, "sc_preferences_set_value")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_set_value(
                self.raw.as_ptr(),
                key.as_ptr(),
                value.as_ptr(),
            )
        };
        bridge::bool_result("sc_preferences_set_value", ok)
    }

    pub fn remove_value(&self, key: &str) -> Result<()> {
        let key = bridge::cstring(key, "sc_preferences_remove_value")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_remove_value(self.raw.as_ptr(), key.as_ptr())
        };
        bridge::bool_result("sc_preferences_remove_value", ok)
    }

    pub fn path_create_unique_child(&self, prefix: &str) -> Result<Option<String>> {
        let prefix = bridge::cstring(prefix, "sc_preferences_path_create_unique_child")?;
        Ok(bridge::take_optional_string(unsafe {
            ffi::preferences::sc_preferences_path_create_unique_child(
                self.raw.as_ptr(),
                prefix.as_ptr(),
            )
        }))
    }

    pub fn path_get_value(&self, path: &str) -> Result<Option<PropertyList>> {
        let path = bridge::cstring(path, "sc_preferences_path_get_value")?;
        let raw = unsafe {
            ffi::preferences::sc_preferences_path_get_value(self.raw.as_ptr(), path.as_ptr())
        };
        Ok(unsafe { bridge::OwnedHandle::from_raw(raw) }.map(PropertyList::from_owned_handle))
    }

    pub fn path_get_link(&self, path: &str) -> Result<Option<String>> {
        let path = bridge::cstring(path, "sc_preferences_path_get_link")?;
        Ok(bridge::take_optional_string(unsafe {
            ffi::preferences::sc_preferences_path_get_link(self.raw.as_ptr(), path.as_ptr())
        }))
    }

    pub fn path_set_value(&self, path: &str, value: &PropertyList) -> Result<()> {
        let path = bridge::cstring(path, "sc_preferences_path_set_value")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_path_set_value(
                self.raw.as_ptr(),
                path.as_ptr(),
                value.as_ptr(),
            )
        };
        bridge::bool_result("sc_preferences_path_set_value", ok)
    }

    pub fn path_set_link(&self, path: &str, link: &str) -> Result<()> {
        let path = bridge::cstring(path, "sc_preferences_path_set_link")?;
        let link = bridge::cstring(link, "sc_preferences_path_set_link")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_path_set_link(
                self.raw.as_ptr(),
                path.as_ptr(),
                link.as_ptr(),
            )
        };
        bridge::bool_result("sc_preferences_path_set_link", ok)
    }

    pub fn path_remove_value(&self, path: &str) -> Result<()> {
        let path = bridge::cstring(path, "sc_preferences_path_remove_value")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_path_remove_value(self.raw.as_ptr(), path.as_ptr())
        };
        bridge::bool_result("sc_preferences_path_remove_value", ok)
    }

    pub fn set_computer_name(&self, name: Option<&str>) -> Result<()> {
        let name = bridge::optional_cstring(name, "sc_preferences_set_computer_name")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_set_computer_name(
                self.raw.as_ptr(),
                name.as_ref()
                    .map_or(std::ptr::null(), |value| value.as_ptr()),
            )
        };
        bridge::bool_result("sc_preferences_set_computer_name", ok)
    }

    pub fn set_local_host_name(&self, name: Option<&str>) -> Result<()> {
        let name = bridge::optional_cstring(name, "sc_preferences_set_local_host_name")?;
        let ok = unsafe {
            ffi::preferences::sc_preferences_set_local_host_name(
                self.raw.as_ptr(),
                name.as_ref()
                    .map_or(std::ptr::null(), |value| value.as_ptr()),
            )
        };
        bridge::bool_result("sc_preferences_set_local_host_name", ok)
    }

    pub fn network_services(&self) -> Vec<NetworkService> {
        NetworkService::copy_all(self)
    }

    pub(crate) fn as_ptr(&self) -> bridge::RawHandle {
        self.raw.as_ptr()
    }
}