Skip to content

Commit 4502768

Browse files
committed
Make Database not depend on FileMetadata
1 parent 9160735 commit 4502768

11 files changed

Lines changed: 37 additions & 62 deletions

File tree

binaryninjaapi.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,8 +3357,8 @@ namespace BinaryNinja {
33573357
DataBuffer GetFileContents();
33583358
DataBuffer GetFileContentsHash();
33593359
DataBuffer GetUndoData();
3360-
std::vector<Ref<UndoEntry>> GetUndoEntries();
3361-
std::vector<Ref<UndoEntry>> GetUndoEntries(const ProgressFunction& progress);
3360+
std::vector<Ref<UndoEntry>> GetUndoEntries(Ref<FileMetadata> file);
3361+
std::vector<Ref<UndoEntry>> GetUndoEntries(Ref<FileMetadata> file, const ProgressFunction& progress);
33623362
Ref<KeyValueStore> ReadData();
33633363
Ref<KeyValueStore> ReadData(const ProgressFunction& progress);
33643364
bool StoreData(const Ref<KeyValueStore>& data, const ProgressFunction& progress);
@@ -3393,7 +3393,6 @@ namespace BinaryNinja {
33933393
DataBuffer ReadGlobalData(const std::string& key) const;
33943394
void WriteGlobalData(const std::string& key, const DataBuffer& val);
33953395

3396-
Ref<FileMetadata> GetFile();
33973396
void ReloadConnection();
33983397

33993398
Ref<KeyValueStore> ReadAnalysisCache() const;
@@ -23258,14 +23257,14 @@ namespace BinaryNinja::Collaboration
2325823257

2325923258
/*!
2326023259
Completely sync a database, pushing/pulling/merging/applying changes
23261-
\param database Database to sync
23260+
\param metadata File from database to sync
2326223261
\param file Remote File to sync with
2326323262
\param conflictHandler Function to call to resolve snapshot conflicts
2326423263
\param progress Function to call for progress updates
2326523264
\param nameChangeset Function to call for naming a pushed changeset, if necessary
2326623265
\throws SyncException If there is an error syncing
2326723266
*/
23268-
void SyncDatabase(Ref<Database> database, Ref<RemoteFile> file, AnalysisConflictHandler conflictHandler, ProgressFunction progress = {}, NameChangesetFunction nameChangeset = [](Ref<CollabChangeset>){ return true; });
23267+
void SyncDatabase(Ref<FileMetadata> metadata, Ref<RemoteFile> file, AnalysisConflictHandler conflictHandler, ProgressFunction progress = {}, NameChangesetFunction nameChangeset = [](Ref<CollabChangeset>){ return true; });
2326923268

2327023269
/*!
2327123270
Completely sync a type archive, pushing/pulling/merging/applying changes

binaryninjacore.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4451,7 +4451,6 @@ extern "C"
44514451
BINARYNINJACOREAPI bool BNWriteDatabaseGlobal(BNDatabase* database, const char* key, const char* val);
44524452
BINARYNINJACOREAPI BNDataBuffer* BNReadDatabaseGlobalData(BNDatabase* database, const char* key);
44534453
BINARYNINJACOREAPI bool BNWriteDatabaseGlobalData(BNDatabase* database, const char* key, BNDataBuffer* val);
4454-
BINARYNINJACOREAPI BNFileMetadata* BNGetDatabaseFile(BNDatabase* database);
44554454
BINARYNINJACOREAPI void BNDatabaseReloadConnection(BNDatabase* database);
44564455
BINARYNINJACOREAPI BNKeyValueStore* BNReadDatabaseAnalysisCache(BNDatabase* database);
44574456
BINARYNINJACOREAPI bool BNWriteDatabaseAnalysisCache(BNDatabase* database, BNKeyValueStore* val);
@@ -4477,9 +4476,9 @@ extern "C"
44774476
BINARYNINJACOREAPI BNKeyValueStore* BNReadSnapshotDataWithProgress(
44784477
BNSnapshot* snapshot, void* ctxt, BNProgressFunction progress);
44794478
BINARYNINJACOREAPI BNDataBuffer* BNGetSnapshotUndoData(BNSnapshot* snapshot);
4480-
BINARYNINJACOREAPI BNUndoEntry** BNGetSnapshotUndoEntries(BNSnapshot* snapshot, size_t* count);
4479+
BINARYNINJACOREAPI BNUndoEntry** BNGetSnapshotUndoEntries(BNSnapshot* snapshot, BNFileMetadata* file, size_t* count);
44814480
BINARYNINJACOREAPI BNUndoEntry** BNGetSnapshotUndoEntriesWithProgress(
4482-
BNSnapshot* snapshot, void* ctxt, BNProgressFunction progress, size_t* count);
4481+
BNSnapshot* snapshot, BNFileMetadata* file, void* ctxt, BNProgressFunction progress, size_t* count);
44834482
BINARYNINJACOREAPI bool BNSnapshotHasAncestor(BNSnapshot* snapshot, BNSnapshot* other);
44844483
BINARYNINJACOREAPI bool BNSnapshotStoreData(BNSnapshot* snapshot, BNKeyValueStore* data,
44854484
void* ctxt, BNProgressFunction progress);
@@ -8528,7 +8527,7 @@ extern "C"
85288527
BINARYNINJACOREAPI BNRemote* BNCollaborationCreateRemote(const char* name, const char* address);
85298528
BINARYNINJACOREAPI void BNCollaborationRemoveRemote(BNRemote* remote);
85308529
BINARYNINJACOREAPI void BNCollaborationSaveRemotes();
8531-
BINARYNINJACOREAPI bool BNCollaborationSyncDatabase(BNDatabase* database, BNRemoteFile* file, BNCollaborationAnalysisConflictHandler conflictHandler, void* conflictHandlerCtxt, BNProgressFunction progress, void* progressCtxt, BNCollaborationNameChangesetFunction nameChangeset, void* nameChangesetCtxt);
8530+
BINARYNINJACOREAPI bool BNCollaborationSyncDatabase(BNFileMetadata* metadata, BNRemoteFile* file, BNCollaborationAnalysisConflictHandler conflictHandler, void* conflictHandlerCtxt, BNProgressFunction progress, void* progressCtxt, BNCollaborationNameChangesetFunction nameChangeset, void* nameChangesetCtxt);
85328531
BINARYNINJACOREAPI bool BNCollaborationSyncTypeArchive(BNTypeArchive* archive, BNRemoteFile* file, bool(*conflictHandler)(void*, BNTypeArchiveMergeConflict** conflicts, size_t conflictCount), void* conflictHandlerCtxt, BNProgressFunction progress, void* progressCtxt);
85338532
BINARYNINJACOREAPI bool BNCollaborationPushTypeArchive(BNTypeArchive* archive, BNRemoteFile* file, size_t* count, BNProgressFunction progress, void* progressCtxt);
85348533
BINARYNINJACOREAPI bool BNCollaborationPullTypeArchive(BNTypeArchive* archive, BNRemoteFile* file, size_t* count, bool(*conflictHandler)(void*, BNTypeArchiveMergeConflict** conflicts, size_t conflictCount), void* conflictHandlerCtxt, BNProgressFunction progress, void* progressCtxt);

collaboration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ bool BinaryNinja::Collaboration::NameChangesetCallback(void* ctxt, BNCollaborati
455455
};
456456

457457

458-
void BinaryNinja::Collaboration::SyncDatabase(Ref<Database> database, Ref<RemoteFile> file, std::function<bool(const std::unordered_map<std::string, Ref<AnalysisMergeConflict>>& conflicts)> conflictHandler, ProgressFunction progress, NameChangesetFunction nameChangeset)
458+
void BinaryNinja::Collaboration::SyncDatabase(Ref<FileMetadata> metadata, Ref<RemoteFile> file, std::function<bool(const std::unordered_map<std::string, Ref<AnalysisMergeConflict>>& conflicts)> conflictHandler, ProgressFunction progress, NameChangesetFunction nameChangeset)
459459
{
460460
ProgressContext pctxt;
461461
pctxt.callback = progress;
@@ -466,7 +466,7 @@ void BinaryNinja::Collaboration::SyncDatabase(Ref<Database> database, Ref<Remote
466466
NameChangesetContext ncctxt;
467467
ncctxt.callback = nameChangeset;
468468

469-
BNCollaborationSyncDatabase(database->m_object, file->m_object,
469+
BNCollaborationSyncDatabase(metadata->m_object, file->m_object,
470470
DatabaseConflictHandlerCallback, &chctxt,
471471
ProgressCallback, &pctxt,
472472
NameChangesetCallback, &ncctxt

database.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,19 @@ DataBuffer Snapshot::GetUndoData()
306306
}
307307

308308

309-
vector<Ref<UndoEntry>> Snapshot::GetUndoEntries()
309+
vector<Ref<UndoEntry>> Snapshot::GetUndoEntries(Ref<FileMetadata> file)
310310
{
311-
return GetUndoEntries([](size_t, size_t) { return true; });
311+
return GetUndoEntries(file, [](size_t, size_t) { return true; });
312312
}
313313

314314

315-
vector<Ref<UndoEntry>> Snapshot::GetUndoEntries(const ProgressFunction& progress)
315+
vector<Ref<UndoEntry>> Snapshot::GetUndoEntries(Ref<FileMetadata> file, const ProgressFunction& progress)
316316
{
317317
ProgressContext pctxt;
318318
pctxt.callback = progress;
319319

320320
size_t count;
321-
BNUndoEntry** entries = BNGetSnapshotUndoEntriesWithProgress(m_object, &pctxt, ProgressCallback, &count);
321+
BNUndoEntry** entries = BNGetSnapshotUndoEntriesWithProgress(m_object, file->GetObject(), &pctxt, ProgressCallback, &count);
322322
if (entries == nullptr)
323323
{
324324
throw DatabaseException("BNGetSnapshotUndoEntriesWithProgress");
@@ -536,12 +536,6 @@ void Database::WriteGlobalData(const std::string& key, const DataBuffer& val)
536536
}
537537

538538

539-
Ref<FileMetadata> Database::GetFile()
540-
{
541-
return new FileMetadata(BNGetDatabaseFile(m_object));
542-
}
543-
544-
545539
void Database::ReloadConnection()
546540
{
547541
BNDatabaseReloadConnection(m_object);

python/collaboration/databasesync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,19 @@ def get_local_snapshot_for_remote(snapshot: snapshot.CollabSnapshot, database: D
184184
return Snapshot(handle=ctypes.cast(value, ctypes.POINTER(core.BNSnapshot)))
185185

186186

187-
def sync_database(database: Database, file_: 'file.RemoteFile', conflict_handler: 'util.ConflictHandlerType', progress: 'util.ProgressFuncType' = util.nop, name_changeset: 'util.NameChangesetFuncType' = util.nop):
187+
def sync_database(metadata: FileMetadata, file_: 'file.RemoteFile', conflict_handler: 'util.ConflictHandlerType', progress: 'util.ProgressFuncType' = util.nop, name_changeset: 'util.NameChangesetFuncType' = util.nop):
188188
"""
189189
Completely sync a database, pushing/pulling/merging/applying changes
190190
191-
:param database: Database to sync
191+
:param metadata: File opened from database to sync
192192
:param file_: File to sync with
193193
:param conflict_handler: Function to call to resolve snapshot conflicts
194194
:param progress: Function to call for progress updates
195195
:param name_changeset: Function to call for naming a pushed changeset, if necessary
196196
:raises RuntimeError: If there was an error (or the operation was cancelled)
197197
"""
198198

199-
if not core.BNCollaborationSyncDatabase(ctypes.cast(database.handle, core.BNDatabaseHandle), file_._handle, util.wrap_conflict_handler(conflict_handler), None, util.wrap_progress(progress), None, util.wrap_name_changeset(name_changeset), None):
199+
if not core.BNCollaborationSyncDatabase(ctypes.cast(metadata.handle, core.BNFileMetadataHandle), file_._handle, util.wrap_conflict_handler(conflict_handler), None, util.wrap_progress(progress), None, util.wrap_name_changeset(name_changeset), None):
200200
raise RuntimeError(util._last_error())
201201

202202

python/collaboration/file.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,23 +419,19 @@ def download_to_bndb(self, path: Optional[str] = None, progress: 'util.ProgressF
419419
file.database, lambda conflicts: False, util.split_progress(progress, 1, [0.5, 0.5]))
420420
return file
421421

422-
def sync(self, bv_or_db: Union['BinaryView', 'Database'], conflict_handler: 'util.ConflictHandlerType', progress: 'util.ProgressFuncType' = util.nop, name_changeset: 'util.NameChangesetFuncType' = util.nop):
422+
def sync(self, bv: 'BinaryView', conflict_handler: 'util.ConflictHandlerType', progress: 'util.ProgressFuncType' = util.nop, name_changeset: 'util.NameChangesetFuncType' = util.nop):
423423
"""
424424
Completely sync a file, pushing/pulling/merging/applying changes
425425
426-
:param bv_or_db: Binary view or database to sync with
426+
:param bv: Binary view to sync with
427427
:param conflict_handler: Function to call to resolve snapshot conflicts
428428
:param name_changeset: Function to call for naming a pushed changeset, if necessary
429429
:param progress: Function to call for progress updates
430430
:raises RuntimeError: If there was an error (or the operation was cancelled)
431431
"""
432-
if isinstance(bv_or_db, BinaryView):
433-
if not bv_or_db.file.has_database:
434-
raise RuntimeError("Cannot sync non-database view")
435-
db = bv_or_db.file.database
436-
else:
437-
db = bv_or_db
438-
databasesync.sync_database(db, self, conflict_handler, progress, name_changeset)
432+
if not bv.file.has_database:
433+
raise RuntimeError("Cannot sync non-database view")
434+
databasesync.sync_database(bv.file, self, conflict_handler, progress, name_changeset)
439435

440436
def pull(self, bv_or_db: Union['BinaryView', 'Database'], conflict_handler: 'util.ConflictHandlerType', progress: 'util.ProgressFuncType' = util.nop, name_changeset: 'util.NameChangesetFuncType' = util.nop):
441437
"""

python/database.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,6 @@ def write_global_data(self, key: str, value: databuffer.DataBuffer):
357357
"""Write a binary buffer into a global in the database"""
358358
core.BNWriteDatabaseGlobalData(self.handle, key, value.handle)
359359

360-
@property
361-
def file(self) -> 'filemetadata.FileMetadata':
362-
"""Get the owning FileMetadata (read-only)"""
363-
handle = core.BNGetDatabaseFile(self.handle)
364-
assert handle is not None
365-
return filemetadata.FileMetadata(handle=handle)
366-
367360
@property
368361
def analysis_cache(self) -> KeyValueStore:
369362
"""Get the backing analysis cache kvs (read-only)"""

rust/src/collaboration/file.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,8 @@ impl RemoteFile {
448448
path.as_ref(),
449449
progress.next_subpart().unwrap(),
450450
)?;
451-
let database = file.database().ok_or(())?;
452451
self.sync_with_progress(
453-
&database,
452+
&file,
454453
DatabaseConflictHandlerFail,
455454
NoNameChangeset,
456455
progress.next_subpart().unwrap(),
@@ -465,11 +464,11 @@ impl RemoteFile {
465464
/// * `name_changeset` - Function to call for naming a pushed changeset, if necessary
466465
pub fn sync<C: DatabaseConflictHandler, N: NameChangeset>(
467466
&self,
468-
database: &Database,
467+
metadata: &FileMetadata,
469468
conflict_handler: C,
470469
name_changeset: N,
471470
) -> Result<(), ()> {
472-
sync::sync_database(database, self, conflict_handler, name_changeset)
471+
sync::sync_database(metadata, self, conflict_handler, name_changeset)
473472
}
474473

475474
/// Completely sync a file, pushing/pulling/merging/applying changes
@@ -480,13 +479,13 @@ impl RemoteFile {
480479
/// * `progress` - Function to call for progress updates
481480
pub fn sync_with_progress<C: DatabaseConflictHandler, P: ProgressCallback, N: NameChangeset>(
482481
&self,
483-
database: &Database,
482+
metadata: &FileMetadata,
484483
conflict_handler: C,
485484
name_changeset: N,
486485
progress: P,
487486
) -> Result<(), ()> {
488487
sync::sync_database_with_progress(
489-
database,
488+
metadata,
490489
self,
491490
conflict_handler,
492491
name_changeset,

rust/src/collaboration/sync.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,18 @@ where
243243

244244
/// Completely sync a database, pushing/pulling/merging/applying changes
245245
///
246-
/// * `database` - Database to sync
246+
/// * `metadata` - File openend from database to sync
247247
/// * `file` - File to sync with
248248
/// * `conflict_handler` - Function to call to resolve snapshot conflicts
249249
/// * `name_changeset` - Function to call for naming a pushed changeset, if necessary
250250
pub fn sync_database<C: DatabaseConflictHandler, N: NameChangeset>(
251-
database: &Database,
251+
metadata: &FileMetadata,
252252
file: &RemoteFile,
253253
conflict_handler: C,
254254
name_changeset: N,
255255
) -> Result<(), ()> {
256256
sync_database_with_progress(
257-
database,
257+
metadata,
258258
file,
259259
conflict_handler,
260260
name_changeset,
@@ -264,7 +264,7 @@ pub fn sync_database<C: DatabaseConflictHandler, N: NameChangeset>(
264264

265265
/// Completely sync a database, pushing/pulling/merging/applying changes
266266
///
267-
/// * `database` - Database to sync
267+
/// * `metadata` - File openend from database to sync
268268
/// * `file` - File to sync with
269269
/// * `conflict_handler` - Function to call to resolve snapshot conflicts
270270
/// * `name_changeset` - Function to call for naming a pushed changeset, if necessary
@@ -274,15 +274,15 @@ pub fn sync_database_with_progress<
274274
P: ProgressCallback,
275275
N: NameChangeset,
276276
>(
277-
database: &Database,
277+
metadata: &FileMetadata,
278278
file: &RemoteFile,
279279
mut conflict_handler: C,
280280
mut name_changeset: N,
281281
mut progress: P,
282282
) -> Result<(), ()> {
283283
let success = unsafe {
284284
BNCollaborationSyncDatabase(
285-
database.handle.as_ptr(),
285+
metadata.handle,
286286
file.handle.as_ptr(),
287287
Some(C::cb_handle_conflict),
288288
&mut conflict_handler as *mut C as *mut c_void,

rust/src/database.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::binary_view::BinaryView;
1212
use crate::data_buffer::DataBuffer;
1313
use crate::database::kvs::KeyValueStore;
1414
use crate::database::snapshot::{Snapshot, SnapshotId};
15-
use crate::file_metadata::FileMetadata;
1615
use crate::progress::{NoProgressCallback, ProgressCallback};
1716
use crate::rc::{Array, Ref, RefCountable};
1817
use crate::string::{BnString, IntoCStr};
@@ -181,13 +180,6 @@ impl Database {
181180
unsafe { BNWriteDatabaseGlobalData(self.handle.as_ptr(), key_raw.as_ptr(), value.as_raw()) }
182181
}
183182

184-
/// Get the owning FileMetadata
185-
pub fn file(&self) -> Ref<FileMetadata> {
186-
let result = unsafe { BNGetDatabaseFile(self.handle.as_ptr()) };
187-
assert!(!result.is_null());
188-
FileMetadata::ref_from_raw(result)
189-
}
190-
191183
/// Get the backing analysis cache kvs
192184
pub fn analysis_cache(&self) -> Ref<KeyValueStore> {
193185
let result = unsafe { BNReadDatabaseAnalysisCache(self.handle.as_ptr()) };

0 commit comments

Comments
 (0)