diff --git a/codex/storage.go b/codex/storage.go index 256598e..8af1f5b 100644 --- a/codex/storage.go +++ b/codex/storage.go @@ -24,6 +24,10 @@ import ( static int cGoCodexStorageDelete(void* codexCtx, char* cid, void* resp) { return codex_storage_delete(codexCtx, cid, (CodexCallback) callback, resp); } + + static int cGoCodexStorageExists(void* codexCtx, char* cid, void* resp) { + return codex_storage_exists(codexCtx, cid, (CodexCallback) callback, resp); + } */ import "C" @@ -142,3 +146,19 @@ func (node CodexNode) Delete(cid string) error { _, err := bridge.wait() return err } + +// Exists checks if a given cid exists in the local storage. +func (node CodexNode) Exists(cid string) (bool, error) { + bridge := newBridgeCtx() + defer bridge.free() + + var cCid = C.CString(cid) + defer C.free(unsafe.Pointer(cCid)) + + if C.cGoCodexStorageExists(node.ctx, cCid, bridge.resp) != C.RET_OK { + return false, bridge.callError("cGoCodexStorageExists") + } + + result, err := bridge.wait() + return result == "true", err +} diff --git a/codex/storage_test.go b/codex/storage_test.go index bca6381..45d665c 100644 --- a/codex/storage_test.go +++ b/codex/storage_test.go @@ -119,3 +119,30 @@ func TestDelete(t *testing.T) { t.Fatal("expected manifests to be empty after deletion") } } + +func TestExists(t *testing.T) { + codex := newCodexNode(t) + + cid, _ := uploadHelper(t, codex) + + exists, err := codex.Exists(cid) + if err != nil { + t.Fatal(err) + } + if !exists { + t.Fatal("expected cid to exist") + } + + err = codex.Delete(cid) + if err != nil { + t.Fatal(err) + } + + exists, err = codex.Exists(cid) + if err != nil { + t.Fatal(err) + } + if exists { + t.Fatal("expected cid to not exist after deletion") + } +} diff --git a/vendor/nim-codex b/vendor/nim-codex index a86d858..72e9a3f 160000 --- a/vendor/nim-codex +++ b/vendor/nim-codex @@ -1 +1 @@ -Subproject commit a86d8586456d5eb6805b228e80e264ee736a6a90 +Subproject commit 72e9a3f85dd6cf43240bf3e0fbdcf27e704d3ca6