2016-11-28 21:13:46 +00:00
|
|
|
/**
|
|
|
|
* A basic storage building block of the IPFS system
|
|
|
|
*/
|
|
|
|
|
2016-12-05 15:50:17 +00:00
|
|
|
#include "ipfs/merkledag/merkledag.h"
|
|
|
|
|
2016-11-28 21:13:46 +00:00
|
|
|
/***
|
|
|
|
* Adds a node to the dagService and blockService
|
|
|
|
* @param node the node to add
|
2016-12-05 15:50:17 +00:00
|
|
|
* @param cid the resultant cid that was added
|
2016-11-28 21:13:46 +00:00
|
|
|
* @returns true(1) on success
|
|
|
|
*/
|
2016-12-05 15:50:17 +00:00
|
|
|
int ipfs_merkledag_add(struct Node* node, struct FSRepo* fs_repo) {
|
2016-11-28 21:13:46 +00:00
|
|
|
// taken from merkledag.go line 59
|
|
|
|
|
2016-12-05 15:50:17 +00:00
|
|
|
// turn the node into a block
|
|
|
|
struct Block* block;
|
|
|
|
ipfs_blocks_block_new(node->data, node->data_size, &block);
|
|
|
|
|
2016-12-05 22:23:58 +00:00
|
|
|
int retVal = fs_repo->config->datastore->datastore_put_block(block, fs_repo->config->datastore);
|
2016-12-05 15:50:17 +00:00
|
|
|
if (retVal == 0) {
|
|
|
|
ipfs_blocks_block_free(block);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-07 16:07:36 +00:00
|
|
|
Node_Set_Cached(node, block->cid);
|
2016-12-05 15:50:17 +00:00
|
|
|
ipfs_blocks_block_free(block);
|
|
|
|
|
|
|
|
// TODO: call HasBlock (unsure why as yet)
|
|
|
|
return 1;
|
2016-11-28 21:13:46 +00:00
|
|
|
}
|
2016-12-05 22:23:58 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Retrieves a node from the datastore based on the cid
|
|
|
|
* @param cid the key to look for
|
|
|
|
* @param node the node to be created
|
|
|
|
* @param fs_repo the repository
|
|
|
|
* @returns true(1) on success
|
|
|
|
*/
|
|
|
|
int ipfs_merkledag_get(const struct Cid* cid, struct Node** node, const struct FSRepo* fs_repo) {
|
|
|
|
int retVal = 1;
|
|
|
|
|
|
|
|
// look for the block
|
|
|
|
struct Block* block;
|
|
|
|
retVal = fs_repo->config->datastore->datastore_get_block(cid, &block, fs_repo->config->datastore);
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// we have the block. Fill the node
|
|
|
|
*node = N_Create_From_Data(block->data, block->data_length);
|
2016-12-07 16:07:36 +00:00
|
|
|
Node_Set_Cached(*node, cid);
|
2016-12-05 22:23:58 +00:00
|
|
|
|
2016-12-07 16:53:17 +00:00
|
|
|
ipfs_blocks_block_free(block);
|
|
|
|
|
2016-12-05 22:23:58 +00:00
|
|
|
return retVal;
|
|
|
|
}
|