Merged in changes to node.h and node.c

yamux
jmjatlanta 2016-12-07 11:07:36 -05:00
parent a180a63160
commit 4a6b88871a
4 changed files with 116 additions and 63 deletions

View File

@ -5,7 +5,6 @@
#ifndef IPFS_NODE_H #ifndef IPFS_NODE_H
#define IPFS_NODE_H #define IPFS_NODE_H
#include "inttypes.h"
#include "ipfs/cid/cid.h" #include "ipfs/cid/cid.h"
/*==================================================================================== /*====================================================================================
@ -28,7 +27,7 @@ struct Node
unsigned char * encoded; unsigned char * encoded;
struct Cid * cached; struct Cid * cached;
int link_ammount; int link_ammount;
struct Link* links[]; struct Link * links[];
}; };
/*==================================================================================== /*====================================================================================
@ -40,6 +39,7 @@ struct Node
/*==================================================================================== /*====================================================================================
* Link Functions * Link Functions
*===================================================================================*/ *===================================================================================*/
/* Create_Link /* Create_Link
* @Param name: The name of the link (char *) * @Param name: The name of the link (char *)
* @Param size: Size of the link (size_t) * @Param size: Size of the link (size_t)
@ -55,20 +55,32 @@ void Free_Link(struct Link * L);
/*==================================================================================== /*====================================================================================
* Node Functions * Node Functions
*===================================================================================*/ *===================================================================================*/
/*Create_Empty_Node /*Create_Empty_Node
* Creates an empty node, allocates the required memory * Creates an empty node, allocates the required memory
* Returns a fresh new node with no data set in it. * Returns a fresh new node with no data set in it.
*/ */
struct Node * Create_Empty_Node(); struct Node * Create_Empty_Node();
//Node_Set_Cached
int Node_Set_Cached(struct Node * N, struct Cid * TheCid);
/*Node_Set_Data /*Node_Set_Data
* Sets the data of a node * Sets the data of a node
* @param Node: The node which you want to set data in. * @param Node: The node which you want to set data in.
* @param Data, the data you want to assign to the node * @param Data, the data you want to assign to the node
* Sets pointers of encoded & cached to NULL /following go method
* returns 1 on success 0 on failure * returns 1 on success 0 on failure
*/ */
int Node_Set_Data(struct Node * N, unsigned char * Data); int Node_Set_Data(struct Node * N, unsigned char * Data);
/*Node_Set_Encoded
* @param NODE: the node you wish to alter (struct Node *)
* @param Data: The data you wish to set in encoded.(unsigned char *)
* returns 1 on success 0 on failure
*/
int Node_Set_Encoded(struct Node * N, unsigned char * Data);
/*Node_Get_Data /*Node_Get_Data
* Gets data from a node * Gets data from a node
* @param Node: = The node you want to get data from. (unsigned char *) * @param Node: = The node you want to get data from. (unsigned char *)
@ -76,14 +88,6 @@ int Node_Set_Data(struct Node * N, unsigned char * Data);
*/ */
unsigned char * Node_Get_Data(struct Node * N); unsigned char * Node_Get_Data(struct Node * N);
/**
* set Cid on node
* @param node the node
* @param the Cid to use
* @returns true(1) on success
*/
int Node_Set_Cid(struct Node* N, struct Cid* cid);
/*Node_Copy: Returns a copy of the node you input /*Node_Copy: Returns a copy of the node you input
* @param Node: The node you want to copy (struct CP_Node *) * @param Node: The node you want to copy (struct CP_Node *)
* Returns a copy of the node you wanted to copy. * Returns a copy of the node you wanted to copy.
@ -118,7 +122,7 @@ int Node_Remove_Link(char * Name, struct Node * mynode);
* @param linksz: sizeof(your cid here) * @param linksz: sizeof(your cid here)
* Returns your node with the newly added link * Returns your node with the newly added link
*/ */
struct Node* N_Add_Link(struct Node** mynode, struct Link* mylink, size_t linksz); struct Node * N_Add_Link(struct Node ** mynode, struct Link * mylink, size_t linksz);
/*N_Create_From_Link /*N_Create_From_Link
* Create a node from a link * Create a node from a link
@ -126,15 +130,20 @@ struct Node* N_Add_Link(struct Node** mynode, struct Link* mylink, size_t linksz
* @param linksize: sizeof(the link in mylink) (size_T) * @param linksize: sizeof(the link in mylink) (size_T)
* Returns a fresh new node with the link you specified. Has to be freed with Node_Free preferably. * Returns a fresh new node with the link you specified. Has to be freed with Node_Free preferably.
*/ */
struct Node * N_Create_From_Link(struct Link * mylink); struct Node * N_Create_From_Link(struct Link * mylink) ;
/*N_Create_From_Data /*N_Create_From_Data
* @param data: bytes buffer you want to create the node from * @param data: bytes buffer you want to create the node from
* @param data_size length of buffer
* returns a node with the data you inputted. * returns a node with the data you inputted.
*/ */
struct Node * N_Create_From_Data(unsigned char * data, size_t data_size); struct Node * N_Create_From_Data(unsigned char * data, size_t data_size);
/*N_Create_From_Encoded
* @param data: encoded bytes buffer you want to create the node from
* returns a node with the encoded data you inputted.
*/
struct Node * N_Create_From_Encoded(unsigned char * data);
/*Node_Resolve_Max_Size /*Node_Resolve_Max_Size
* !!!This shouldn't concern you! * !!!This shouldn't concern you!
*Gets the ammount of words that will be returned by Node_Resolve *Gets the ammount of words that will be returned by Node_Resolve

View File

@ -23,7 +23,7 @@ int ipfs_merkledag_add(struct Node* node, struct FSRepo* fs_repo) {
return 0; return 0;
} }
Node_Set_Cid(node, block->cid); Node_Set_Cached(node, block->cid);
ipfs_blocks_block_free(block); ipfs_blocks_block_free(block);
// TODO: call HasBlock (unsure why as yet) // TODO: call HasBlock (unsure why as yet)
@ -48,7 +48,7 @@ int ipfs_merkledag_get(const struct Cid* cid, struct Node** node, const struct F
// we have the block. Fill the node // we have the block. Fill the node
*node = N_Create_From_Data(block->data, block->data_length); *node = N_Create_From_Data(block->data, block->data_length);
Node_Set_Cid(*node, cid); Node_Set_Cached(*node, cid);
return retVal; return retVal;
} }

View File

@ -7,8 +7,13 @@
#include <string.h> #include <string.h>
#include "inttypes.h" #include "inttypes.h"
#include "ipfs/cid/cid.h" #include "ipfs/cid/cid.h"
#include "ipfs/node/node.h" #include "ipfs/node/node.h"
/*====================================================================================
* Link Functions
*===================================================================================*/
/* Create_Link /* Create_Link
* @Param name: The name of the link (char *) * @Param name: The name of the link (char *)
* @Param size: Size of the link (size_t) * @Param size: Size of the link (size_t)
@ -20,7 +25,7 @@ struct Link * Create_Link(char * name, unsigned char * ahash)
mylink = malloc(sizeof(struct Link)); mylink = malloc(sizeof(struct Link));
mylink->name = name; mylink->name = name;
int ver = 0; int ver = 0;
size_t lenhash = strlen(ahash)-1; size_t lenhash = strlen((char*)ahash)-1;
ipfs_cid_new(ver, ahash, lenhash*2, CID_PROTOBUF, &mylink->Lcid); ipfs_cid_new(ver, ahash, lenhash*2, CID_PROTOBUF, &mylink->Lcid);
mylink->size = sizeof(mylink) + mylink->Lcid->hash_length; //Unsure of this mylink->size = sizeof(mylink) + mylink->Lcid->hash_length; //Unsure of this
return mylink; return mylink;
@ -34,7 +39,6 @@ void Free_Link(struct Link * L)
ipfs_cid_free(L->Lcid); ipfs_cid_free(L->Lcid);
free(L); free(L);
} }
/*==================================================================================== /*====================================================================================
* Node Functions * Node Functions
*===================================================================================*/ *===================================================================================*/
@ -46,7 +50,6 @@ struct Node * Create_Empty_Node()
{ {
struct Node * N; struct Node * N;
N = (struct Node *)malloc(sizeof(struct Node)); N = (struct Node *)malloc(sizeof(struct Node));
N->data_size = 0;
N->cached = NULL; N->cached = NULL;
N->data = NULL; N->data = NULL;
N->encoded = NULL; N->encoded = NULL;
@ -54,10 +57,24 @@ struct Node * Create_Empty_Node()
return N; return N;
} }
/**
* Set the cached struct element
* @param N the node to be modified
* @param TheCid the Cid to be copied into the Node->cached element
* @returns true(1) on success
*/
int Node_Set_Cached(struct Node * N, struct Cid * TheCid)
{
if (N->cached != NULL)
ipfs_cid_free(N->cached);
return ipfs_cid_new(TheCid->version, TheCid->hash, TheCid->hash_length, TheCid->codec, &(N->cached));
}
/*Node_Set_Data /*Node_Set_Data
* Sets the data of a node * Sets the data of a node
* @param Node: The node which you want to set data in. * @param Node: The node which you want to set data in.
* @param Data, the data you want to assign to the node * @param Data, the data you want to assign to the node
* Sets pointers of encoded & cached to NULL /following go method
* returns 1 on success 0 on failure * returns 1 on success 0 on failure
*/ */
int Node_Set_Data(struct Node * N, unsigned char * Data) int Node_Set_Data(struct Node * N, unsigned char * Data)
@ -72,31 +89,40 @@ int Node_Set_Data(struct Node * N, unsigned char * Data)
return 1; return 1;
} }
/*** /*Node_Set_Encoded
* Gets data from a node * @param NODE: the node you wish to alter (struct Node *)
* @param Node The node you want to get data from. (unsigned char *) * @param Data: The data you wish to set in encoded.(unsigned char *)
* @returns data of node. * returns 1 on success 0 on failure
*/ */
unsigned char* Node_Get_Data(struct Node* N) int Node_Set_Encoded(struct Node * N, unsigned char * Data)
{ {
return N->data; if(!N || !Data)
{
return 0;
}
N->encoded = Data;
//I don't know if these will be needed, enable them if you need them.
//N->cached = NULL;
//N->data = NULL;
return 1;
} }
/*Node_Get_Data
/** * Gets data from a node
* set Cid on node * @param Node: = The node you want to get data from. (unsigned char *)
* @param node the node * Returns data of node.
* @param the Cid to use
* @returns true(1) on success
*/ */
int Node_Set_Cid(struct Node* N, struct Cid* cid) { unsigned char * Node_Get_Data(struct Node * N)
return ipfs_cid_new(cid->version, cid->hash, cid->hash_length, cid->codec, &N->cached); {
unsigned char * DATA;
DATA = N->data;
return DATA;
} }
/*Node_Copy: Returns a copy of the node you input /*Node_Copy: Returns a copy of the node you input
* @param Node: The node you want to copy (struct CP_Node *) * @param Node: The node you want to copy (struct CP_Node *)
* Returns a copy of the node you wanted to copy. * Returns a copy of the node you wanted to copy.
*/ */
struct Node* Node_Copy(struct Node * CP_Node) struct Node * Node_Copy(struct Node * CP_Node)
{ {
struct Node * CN; struct Node * CN;
CN = (struct Node*) malloc(sizeof(struct Node) + sizeof(struct Link) * 2); CN = (struct Node*) malloc(sizeof(struct Node) + sizeof(struct Link) * 2);
@ -111,7 +137,6 @@ struct Node* Node_Copy(struct Node * CP_Node)
memcpy(CN->links[0],CP_Node->links[0], sizeof(struct Link)); memcpy(CN->links[0],CP_Node->links[0], sizeof(struct Link));
return CN; return CN;
} }
/*Node_Delete /*Node_Delete
* Once you are finished using a node, always delete it using this. * Once you are finished using a node, always delete it using this.
* It will take care of the links inside it. * It will take care of the links inside it.
@ -119,24 +144,28 @@ struct Node* Node_Copy(struct Node * CP_Node)
*/ */
void Node_Delete(struct Node * N) void Node_Delete(struct Node * N)
{ {
if(N->link_ammount > 0) if(N)
{ {
for(int i=0; i<N->link_ammount; i++) if(N->link_ammount > 0)
{ {
free(N->links[i]); for(int i=0; i<N->link_ammount; i++)
{
free(N->links[i]);
}
}
if(N->cached)
{
free(N->cached);
} }
}
if (N->cached != NULL)
free(N->cached);
free(N); free(N);
}
} }
/*Node_Get_Link /*Node_Get_Link
* Returns a copy of the link with given name * Returns a copy of the link with given name
* @param Name: (char * name) searches for link with this name * @param Name: (char * name) searches for link with this name
* Returns the link struct if it's found otherwise returns NULL * Returns the link struct if it's found otherwise returns NULL
*/ */
struct Link* Node_Get_Link(struct Node * N, char * Name) struct Link * Node_Get_Link(struct Node * N, char * Name)
{ {
struct Link * L; struct Link * L;
for(int i=0;i<N->link_ammount;i++) for(int i=0;i<N->link_ammount;i++)
@ -146,7 +175,7 @@ struct Link* Node_Get_Link(struct Node * N, char * Name)
L = (struct Link *)malloc(sizeof(struct Link)); L = (struct Link *)malloc(sizeof(struct Link));
memcpy(L,N->links[i],sizeof(struct Link)); memcpy(L,N->links[i],sizeof(struct Link));
int ver = L->Lcid->version; int ver = L->Lcid->version;
char * ahash = L->Lcid->hash; unsigned char * ahash = L->Lcid->hash;
size_t lenhash = L->Lcid->hash_length; size_t lenhash = L->Lcid->hash_length;
ipfs_cid_new(ver, ahash, lenhash, CID_PROTOBUF, &L->Lcid); ipfs_cid_new(ver, ahash, lenhash, CID_PROTOBUF, &L->Lcid);
return L; return L;
@ -154,7 +183,6 @@ struct Link* Node_Get_Link(struct Node * N, char * Name)
} }
return NULL; return NULL;
} }
/*Node_Remove_Link /*Node_Remove_Link
* Removes a link from node if found by name. * Removes a link from node if found by name.
* @param name: Name of link (char * name) * @param name: Name of link (char * name)
@ -177,7 +205,6 @@ int Node_Remove_Link(char * Name, struct Node * mynode)
} }
return 0; return 0;
} }
/* N_Add_Link /* N_Add_Link
* Adds a link to your node * Adds a link to your node
* @param mynode: &yournode * @param mynode: &yournode
@ -221,29 +248,49 @@ struct Node * N_Create_From_Link(struct Link * mylink)
mynode->link_ammount = 0; mynode->link_ammount = 0;
mynode->link_ammount++; mynode->link_ammount++;
mynode->links[0] = malloc(sizeof(struct Link)); mynode->links[0] = malloc(sizeof(struct Link));
mynode->data_size = 0;
mynode->data = NULL;
mynode->cached = NULL;
memcpy(mynode->links[0], mylink, sizeof(struct Link)); memcpy(mynode->links[0], mylink, sizeof(struct Link));
mynode->cached = NULL;
mynode->data = NULL;
mynode->encoded = NULL;
return mynode; return mynode;
} }
/*N_Create_From_Data /*N_Create_From_Data
* @param data bytes buffer you want to create the node from * @param data: bytes buffer you want to create the node from
* @param data_size the length of the buffer
* returns a node with the data you inputted. * returns a node with the data you inputted.
*/ */
struct Node * N_Create_From_Data(unsigned char * data, size_t data_size) struct Node * N_Create_From_Data(unsigned char * data, size_t data_size)
{ {
struct Node * mynode; if(data)
mynode = (struct Node *) malloc(sizeof(struct Node)); {
mynode->data = data; struct Node * mynode;
mynode->data_size = data_size; mynode = (struct Node *) malloc(sizeof(struct Node));
mynode->cached = NULL; mynode->data = data;
mynode->link_ammount = 0; mynode->data_size = data_size;
return mynode; mynode->link_ammount=0;
mynode->encoded = NULL;
mynode->cached = NULL;
return mynode;
}
return NULL;
}
/*N_Create_From_Encoded
* @param data: encoded bytes buffer you want to create the node from
* returns a node with the encoded data you inputted.
*/
struct Node * N_Create_From_Encoded(unsigned char * data)
{
if(data)
{
struct Node * mynode;
mynode = (struct Node *) malloc(sizeof(struct Node));
mynode->encoded = data;
mynode->link_ammount = 0;
mynode->data = NULL;
mynode->cached = NULL;
return mynode;
}
return NULL;
} }
/*Node_Resolve_Max_Size /*Node_Resolve_Max_Size
* !!!This shouldn't concern you! * !!!This shouldn't concern you!
*Gets the ammount of words that will be returned by Node_Resolve *Gets the ammount of words that will be returned by Node_Resolve
@ -277,6 +324,7 @@ int Node_Resolve_Max_Size(char * input1)
*@param2: Sentence to gather words/paths from (Eg: char * meh = "foo/bar/bin") *@param2: Sentence to gather words/paths from (Eg: char * meh = "foo/bar/bin")
*@Returns 1 or 0, 0 if something went wrong, 1 if everything went smoothly. *@Returns 1 or 0, 0 if something went wrong, 1 if everything went smoothly.
*/ */
int Node_Resolve(char ** result, char * input1) int Node_Resolve(char ** result, char * input1)
{ {
if(!input1) if(!input1)
@ -333,7 +381,6 @@ struct Link_Proc * Node_Resolve_Links(struct Node * N, char * path)
} }
return LProc; return LProc;
} }
/*Free_link_Proc /*Free_link_Proc
* frees the Link_Proc struct you created. * frees the Link_Proc struct you created.
* @param1: Link_Proc struct (struct Link_Proc *) * @param1: Link_Proc struct (struct Link_Proc *)

View File

@ -27,9 +27,6 @@ int test_merkledag_get_data() {
return 0; return 0;
} }
// get the size of the database
int start_file_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
// create data for node // create data for node
size_t binary_data_size = 256; size_t binary_data_size = 256;
unsigned char binary_data[binary_data_size]; unsigned char binary_data[binary_data_size];