Skip to content

Commit

Permalink
Update collaboration API
Browse files Browse the repository at this point in the history
Makes collaboration more in line with the refactor. Still a lot of work to do.

Namely still need:
- Proper errors
- _with_opts functions
- More ergonomic api
- Better connection procedure
- Updated documentation
- A LOT of unit tests
- An example
- Typed id's for everything (i dont want BnString as the id!!!)
- NEED to refactor the progress callbacks into the new progress api, but we should pull in some of the stuff the collab progress has
- Elimination of apis that are dumb helpers
  • Loading branch information
emesare committed Jan 23, 2025
1 parent 048cf6b commit 652a1d5
Show file tree
Hide file tree
Showing 19 changed files with 1,228 additions and 1,073 deletions.
26 changes: 13 additions & 13 deletions rust/src/collaboration/mod.rs → rust/src/collaboration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod changeset;
mod databasesync;
mod file;
mod folder;
mod group;
Expand All @@ -8,10 +7,10 @@ mod permission;
mod project;
mod remote;
mod snapshot;
mod sync;
mod user;

pub use changeset::*;
pub use databasesync::*;
pub use file::*;
pub use folder::*;
pub use group::*;
Expand All @@ -20,27 +19,28 @@ pub use permission::*;
pub use project::*;
pub use remote::*;
pub use snapshot::*;
pub use sync::*;
pub use user::*;

use core::{ffi, ptr};

use binaryninjacore_sys::*;

use crate::rc::Array;
use crate::rc::{Array, Ref};
use crate::string::{BnStrCompatible, BnString};

// TODO it's unclear where should preventivelly call things like `open`, `pull_files`, `pull_folders`, etc
// and where should let the user do it.

/// Get the single actively connected Remote (for ux simplification), if any
pub fn active_remote() -> Option<Remote> {
pub fn active_remote() -> Option<Ref<Remote>> {
let value = unsafe { BNCollaborationGetActiveRemote() };
ptr::NonNull::new(value).map(|h| unsafe { Remote::from_raw(h) })
ptr::NonNull::new(value).map(|h| unsafe { Remote::ref_from_raw(h) })
}

/// Set the single actively connected Remote
pub fn set_active_remote(remote: Option<&Remote>) {
let remote_ptr = remote.map_or(ptr::null_mut(), |r| unsafe { r.as_raw() } as *mut _);
let remote_ptr = remote.map_or(ptr::null_mut(), |r| r.handle.as_ptr());
unsafe { BNCollaborationSetActiveRemote(remote_ptr) }
}

Expand Down Expand Up @@ -117,32 +117,32 @@ pub fn known_remotes() -> Array<Remote> {
}

/// Get Remote by unique `id`
pub fn get_remote_by_id<S: BnStrCompatible>(id: S) -> Option<Remote> {
pub fn get_remote_by_id<S: BnStrCompatible>(id: S) -> Option<Ref<Remote>> {
let id = id.into_bytes_with_nul();
let value = unsafe { BNCollaborationGetRemoteById(id.as_ref().as_ptr() as *const ffi::c_char) };
ptr::NonNull::new(value).map(|h| unsafe { Remote::from_raw(h) })
ptr::NonNull::new(value).map(|h| unsafe { Remote::ref_from_raw(h) })
}

/// Get Remote by `address`
pub fn get_remote_by_address<S: BnStrCompatible>(address: S) -> Option<Remote> {
pub fn get_remote_by_address<S: BnStrCompatible>(address: S) -> Option<Ref<Remote>> {
let address = address.into_bytes_with_nul();
let value = unsafe {
BNCollaborationGetRemoteByAddress(address.as_ref().as_ptr() as *const ffi::c_char)
};
ptr::NonNull::new(value).map(|h| unsafe { Remote::from_raw(h) })
ptr::NonNull::new(value).map(|h| unsafe { Remote::ref_from_raw(h) })
}

/// Get Remote by `name`
pub fn get_remote_by_name<S: BnStrCompatible>(name: S) -> Option<Remote> {
pub fn get_remote_by_name<S: BnStrCompatible>(name: S) -> Option<Ref<Remote>> {
let name = name.into_bytes_with_nul();
let value =
unsafe { BNCollaborationGetRemoteByName(name.as_ref().as_ptr() as *const ffi::c_char) };
ptr::NonNull::new(value).map(|h| unsafe { Remote::from_raw(h) })
ptr::NonNull::new(value).map(|h| unsafe { Remote::ref_from_raw(h) })
}

/// Remove a Remote from the list of known remotes (saved to Settings)
pub fn remove_known_remote(remote: &Remote) {
unsafe { BNCollaborationRemoveRemote(remote.as_raw()) }
unsafe { BNCollaborationRemoveRemote(remote.handle.as_ptr()) }
}

/// Save the list of known Remotes to local Settings
Expand Down
107 changes: 46 additions & 61 deletions rust/src/collaboration/changeset.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,65 @@
use core::{ffi, mem, ptr};

use binaryninjacore_sys::*;
use core::{ffi, mem, ptr};
use std::ptr::NonNull;

use super::{RemoteFile, User};
use super::{RemoteFile, RemoteUser};

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

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

impl Drop for Changeset {
fn drop(&mut self) {
unsafe { BNFreeCollaborationChangeset(self.as_raw()) }
}
}

impl Clone for Changeset {
fn clone(&self) -> Self {
unsafe {
Self::from_raw(
ptr::NonNull::new(BNNewCollaborationChangesetReference(self.as_raw())).unwrap(),
)
}
}
handle: NonNull<BNCollaborationChangeset>,
}

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

pub(crate) unsafe fn ref_from_raw(handle: &*mut BNCollaborationChangeset) -> &Self {
assert!(!handle.is_null());
mem::transmute(handle)
}

#[allow(clippy::mut_from_ref)]
pub(crate) unsafe fn as_raw(&self) -> &mut BNCollaborationChangeset {
&mut *self.handle.as_ptr()
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.as_raw()) };
let raw = ptr::NonNull::new(result).ok_or(())?;
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<RemoteFile, ()> {
let result = unsafe { BNCollaborationChangesetGetFile(self.as_raw()) };
ptr::NonNull::new(result)
.map(|raw| unsafe { RemoteFile::from_raw(raw) })
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.as_raw(), &mut count) };
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<User, ()> {
let result = unsafe { BNCollaborationChangesetGetAuthor(self.as_raw()) };
ptr::NonNull::new(result)
.map(|raw| unsafe { User::from_raw(raw) })
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.as_raw()) };
let result = unsafe { BNCollaborationChangesetGetName(self.handle.as_ptr()) };
assert!(!result.is_null());
unsafe { BnString::from_raw(result) }
}
Expand All @@ -89,42 +69,47 @@ impl Changeset {
let value = value.into_bytes_with_nul();
unsafe {
BNCollaborationChangesetSetName(
self.as_raw(),
self.handle.as_ptr(),
value.as_ref().as_ptr() as *const ffi::c_char,
)
}
}
}

impl CoreArrayProvider for Changeset {
type Raw = *mut BNCollaborationChangeset;
type Context = ();
type Wrapped<'a> = &'a Self;
impl ToOwned for Changeset {
type Owned = Ref<Self>;

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

unsafe impl CoreArrayProviderInner for Changeset {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeCollaborationChangesetList(raw, count)
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 wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Self::ref_from_raw(raw)
unsafe fn dec_ref(handle: &Self) {
BNFreeCollaborationChangeset(handle.handle.as_ptr());
}
}

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

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

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
*raw
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)
}
}
Loading

0 comments on commit 652a1d5

Please sign in to comment.