binaryninja/collaboration/
changeset.rs

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
use binaryninjacore_sys::*;
use std::ffi::c_char;
use std::ptr::NonNull;

use super::{RemoteFile, RemoteUser};

use crate::database::snapshot::SnapshotId;
use crate::database::Database;
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
use crate::string::{BnStrCompatible, BnString};

/// A collection of snapshots in a local database
#[repr(transparent)]
pub struct Changeset {
    handle: NonNull<BNCollaborationChangeset>,
}

impl Changeset {
    pub(crate) unsafe fn from_raw(handle: NonNull<BNCollaborationChangeset>) -> Self {
        Self { handle }
    }

    #[allow(unused)]
    pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNCollaborationChangeset>) -> Ref<Self> {
        Ref::new(Self { handle })
    }

    /// Owning database for snapshots
    pub fn database(&self) -> Result<Database, ()> {
        let result = unsafe { BNCollaborationChangesetGetDatabase(self.handle.as_ptr()) };
        let raw = NonNull::new(result).ok_or(())?;
        Ok(unsafe { Database::from_raw(raw) })
    }

    /// Relevant remote File object
    pub fn file(&self) -> Result<Ref<RemoteFile>, ()> {
        let result = unsafe { BNCollaborationChangesetGetFile(self.handle.as_ptr()) };
        NonNull::new(result)
            .map(|raw| unsafe { RemoteFile::ref_from_raw(raw) })
            .ok_or(())
    }

    /// List of snapshot ids in the database
    pub fn snapshot_ids(&self) -> Result<Array<SnapshotId>, ()> {
        let mut count = 0;
        let result =
            unsafe { BNCollaborationChangesetGetSnapshotIds(self.handle.as_ptr(), &mut count) };
        (!result.is_null())
            .then(|| unsafe { Array::new(result, count, ()) })
            .ok_or(())
    }

    /// Relevant remote author User
    pub fn author(&self) -> Result<Ref<RemoteUser>, ()> {
        let result = unsafe { BNCollaborationChangesetGetAuthor(self.handle.as_ptr()) };
        NonNull::new(result)
            .map(|raw| unsafe { RemoteUser::ref_from_raw(raw) })
            .ok_or(())
    }

    /// Changeset name
    pub fn name(&self) -> BnString {
        let result = unsafe { BNCollaborationChangesetGetName(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::from_raw(result) }
    }

    /// Set the name of the changeset, e.g. in a name changeset function.
    pub fn set_name<S: BnStrCompatible>(&self, value: S) -> bool {
        let value = value.into_bytes_with_nul();
        unsafe {
            BNCollaborationChangesetSetName(
                self.handle.as_ptr(),
                value.as_ref().as_ptr() as *const c_char,
            )
        }
    }
}

impl ToOwned for Changeset {
    type Owned = Ref<Self>;

    fn to_owned(&self) -> Self::Owned {
        unsafe { RefCountable::inc_ref(self) }
    }
}

unsafe impl RefCountable for Changeset {
    unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
        Ref::new(Self {
            handle: NonNull::new(BNNewCollaborationChangesetReference(handle.handle.as_ptr()))
                .unwrap(),
        })
    }

    unsafe fn dec_ref(handle: &Self) {
        BNFreeCollaborationChangeset(handle.handle.as_ptr());
    }
}

impl CoreArrayProvider for Changeset {
    type Raw = *mut BNCollaborationChangeset;
    type Context = ();
    type Wrapped<'a> = Guard<'a, Self>;
}

unsafe impl CoreArrayProviderInner for Changeset {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeCollaborationChangesetList(raw, count)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
        let raw_ptr = NonNull::new(*raw).unwrap();
        Guard::new(Self::from_raw(raw_ptr), context)
    }
}