More implementation of bitswap

This commit is contained in:
John Jones 2017-07-27 12:05:41 -05:00
parent e1135fef3b
commit 73d7d5daed
27 changed files with 193 additions and 87 deletions

View file

@ -65,7 +65,7 @@ int ipfs_blocks_block_protobuf_decode(const unsigned char* buffer, const size_t
unsigned char* temp_buffer = NULL;
size_t temp_size;
*block = ipfs_blocks_block_new();
*block = ipfs_block_new();
if (*block == NULL)
goto exit;
@ -115,7 +115,7 @@ exit:
* @param block a pointer to the struct Block that will be created
* @returns true(1) on success
*/
struct Block* ipfs_blocks_block_new() {
struct Block* ipfs_block_new() {
// allocate memory for structure
struct Block* block = (struct Block*)malloc(sizeof(struct Block));
@ -172,7 +172,7 @@ int ipfs_block_free(struct Block* block) {
* @returns a new Block that is a copy
*/
struct Block* ipfs_block_copy(struct Block* original) {
struct Block* copy = ipfs_blocks_block_new();
struct Block* copy = ipfs_block_new();
copy->data_length = original->data_length;
copy->data = (unsigned char*) malloc(original->data_length);
memcpy(copy->data, original->data, original->data_length);

View file

@ -113,6 +113,7 @@ char* ipfs_blockstore_path_get(const struct FSRepo* fs_repo, const char* filenam
* @returns true(1) on success
*/
int ipfs_blockstore_get(const struct BlockstoreContext* context, struct Cid* cid, struct Block** block) {
int retVal = 0;
// get datastore key, which is a base32 key of the multihash
unsigned char* key = ipfs_blockstore_hash_to_base32(cid->hash, cid->hash_length);
@ -122,13 +123,19 @@ int ipfs_blockstore_get(const struct BlockstoreContext* context, struct Cid* cid
unsigned char buffer[file_size];
FILE* file = fopen(filename, "rb");
if (file == NULL)
goto exit;
size_t bytes_read = fread(buffer, 1, file_size, file);
fclose(file);
int retVal = ipfs_blocks_block_protobuf_decode(buffer, bytes_read, block);
if (!ipfs_blocks_block_protobuf_decode(buffer, bytes_read, block))
goto exit;
(*block)->cid = ipfs_cid_copy(cid);
retVal = 1;
exit:
free(key);
free(filename);