Starting implementation of MerkleDag
This commit is contained in:
parent
5f452969fd
commit
0245aa6549
13 changed files with 615 additions and 265 deletions
4
Makefile
4
Makefile
|
@ -8,7 +8,9 @@ all:
|
|||
cd cmd; make all;
|
||||
cd commands; make all;
|
||||
cd core; make all;
|
||||
cd merkledag; make all;
|
||||
cd multibase; make all;
|
||||
cd node; make all;
|
||||
cd os; make all;
|
||||
cd repo; make all;
|
||||
cd flatfs; make all;
|
||||
|
@ -22,7 +24,9 @@ clean:
|
|||
cd cmd; make clean;
|
||||
cd commands; make clean;
|
||||
cd core; make clean;
|
||||
cd merkledag; make clean;
|
||||
cd multibase; make clean;
|
||||
cd node; make clean;
|
||||
cd os; make clean;
|
||||
cd repo; make clean;
|
||||
cd flatfs; make clean;
|
||||
|
|
|
@ -51,5 +51,5 @@ int ipfs_blockstore_put(struct Block* block, struct FSRepo* fs_repo) {
|
|||
|
||||
// send to Put with key
|
||||
fs_repo->config->datastore->datastore_put(key, key_length, block->data, block->data_length, fs_repo->config->datastore);
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
|
16
include/ipfs/merkledag/merkledag.h
Normal file
16
include/ipfs/merkledag/merkledag.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef __IPFS_MERKLEDAG_H__
|
||||
#define __IPFS_MERKLEDAG_H__
|
||||
|
||||
#include "ipfs/node/node.h"
|
||||
#include "ipfs/repo/fsrepo/fs_repo.h"
|
||||
|
||||
/***
|
||||
* Adds a node to the dagService and blockService
|
||||
* @param node the node to add
|
||||
* @param cid the resultant cid that was added
|
||||
* @returns true(1) on success
|
||||
*/
|
||||
int ipfs_merkledag_add(struct Node* node, struct FSRepo* fs_repo);
|
||||
|
||||
|
||||
#endif
|
|
@ -4,14 +4,10 @@
|
|||
*/
|
||||
#ifndef IPFS_NODE_H
|
||||
#define IPFS_NODE_H
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "inttypes.h"
|
||||
#include "ipfs/cid/cid.h"
|
||||
|
||||
int debug = 0; // Set to 1 to enable debug mode of this program
|
||||
|
||||
/*====================================================================================
|
||||
*
|
||||
* Structures
|
||||
|
@ -24,14 +20,17 @@ struct Link
|
|||
size_t size;
|
||||
struct Cid * Lcid;
|
||||
};
|
||||
|
||||
struct Node
|
||||
{
|
||||
unsigned char * data;
|
||||
size_t data_size;
|
||||
unsigned char * encoded;
|
||||
struct Cid * cached;
|
||||
int link_ammount;
|
||||
struct Link * links[];
|
||||
};
|
||||
|
||||
/*====================================================================================
|
||||
*
|
||||
* Functions
|
||||
|
@ -46,25 +45,13 @@ struct Node
|
|||
* @Param size: Size of the link (size_t)
|
||||
* @Param ahash: An Qmhash
|
||||
*/
|
||||
struct Link * Create_Link(char * name, unsigned char * ahash)
|
||||
{
|
||||
struct Link * mylink;
|
||||
mylink = malloc(sizeof(struct Link));
|
||||
mylink->name = name;
|
||||
int ver = 0;
|
||||
size_t lenhash = strlen(ahash)-1;
|
||||
ipfs_cid_new(ver, ahash, lenhash*2, CID_PROTOBUF, &mylink->Lcid);
|
||||
mylink->size = sizeof(mylink) + mylink->Lcid->hash_length; //Unsure of this
|
||||
return mylink;
|
||||
}
|
||||
struct Link * Create_Link(char * name, unsigned char * ahash);
|
||||
|
||||
/* Free_Link
|
||||
* @param L: Free the link you have allocated.
|
||||
*/
|
||||
void Free_Link(struct Link * L)
|
||||
{
|
||||
ipfs_cid_free(L->Lcid);
|
||||
free(L);
|
||||
}
|
||||
void Free_Link(struct Link * L);
|
||||
|
||||
/*====================================================================================
|
||||
* Node Functions
|
||||
*===================================================================================*/
|
||||
|
@ -72,121 +59,58 @@ void Free_Link(struct Link * L)
|
|||
* Creates an empty node, allocates the required memory
|
||||
* Returns a fresh new node with no data set in it.
|
||||
*/
|
||||
struct Node * Create_Empty_Node()
|
||||
{
|
||||
struct Node * N;
|
||||
N = (struct Node *)malloc(sizeof(struct Node));
|
||||
return N;
|
||||
}
|
||||
struct Node * Create_Empty_Node();
|
||||
|
||||
/*Node_Set_Data
|
||||
* Sets the data of a node
|
||||
* @param Node: The node which you want to set data in.
|
||||
* @param Data, the data you want to assign to the node
|
||||
* returns 1 on success 0 on failure
|
||||
*/
|
||||
int Node_Set_Data(struct Node * N, unsigned char * Data)
|
||||
{
|
||||
if(!N || !Data)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
N->encoded = NULL;
|
||||
N->cached = NULL;
|
||||
N->data = Data;
|
||||
return 1;
|
||||
}
|
||||
int Node_Set_Data(struct Node * N, unsigned char * Data);
|
||||
|
||||
/*Node_Get_Data
|
||||
* Gets data from a node
|
||||
* @param Node: = The node you want to get data from. (unsigned char *)
|
||||
* Returns data of node.
|
||||
*/
|
||||
unsigned char * Node_Get_Data(struct Node * N)
|
||||
{
|
||||
unsigned char * DATA;
|
||||
DATA = N->data;
|
||||
return DATA;
|
||||
}
|
||||
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
|
||||
* @param Node: The node you want to copy (struct CP_Node *)
|
||||
* Returns a copy of the node you wanted to copy.
|
||||
*/
|
||||
struct Node * Node_Copy(struct Node * CP_Node)
|
||||
{
|
||||
struct Node * CN;
|
||||
CN = (struct Node*) malloc(sizeof(struct Node) + sizeof(struct Link) * 2);
|
||||
if(CP_Node->link_ammount != 0)
|
||||
{
|
||||
for(int i=0; i<CP_Node->link_ammount; i++)
|
||||
{
|
||||
CN->links[i] = malloc(sizeof(struct Link));
|
||||
}
|
||||
}
|
||||
memcpy(CN, CP_Node, sizeof(struct Node));
|
||||
memcpy(CN->links[0],CP_Node->links[0], sizeof(struct Link));
|
||||
return CN;
|
||||
}
|
||||
struct Node * Node_Copy(struct Node * CP_Node);
|
||||
|
||||
/*Node_Delete
|
||||
* Once you are finished using a node, always delete it using this.
|
||||
* It will take care of the links inside it.
|
||||
* @param N: the node you want to free. (struct Node *)
|
||||
*/
|
||||
void Node_Delete(struct Node * N)
|
||||
{
|
||||
if(N->link_ammount > 0)
|
||||
{
|
||||
for(int i=0; i<N->link_ammount; i++)
|
||||
{
|
||||
free(N->links[i]);
|
||||
}
|
||||
}
|
||||
free(N);
|
||||
}
|
||||
void Node_Delete(struct Node * N);
|
||||
|
||||
/*Node_Get_Link
|
||||
* Returns a copy of the link with given name
|
||||
* @param Name: (char * name) searches for link with this name
|
||||
* Returns the link struct if it's found otherwise returns NULL
|
||||
*/
|
||||
struct Link * Node_Get_Link(struct Node * N, char * Name)
|
||||
{
|
||||
struct Link * L;
|
||||
for(int i=0;i<N->link_ammount;i++)
|
||||
{
|
||||
if(strcmp(N->links[i]->name,Name) == 0)
|
||||
{
|
||||
L = (struct Link *)malloc(sizeof(struct Link));
|
||||
memcpy(L,N->links[i],sizeof(struct Link));
|
||||
int ver = L->Lcid->version;
|
||||
char * ahash = L->Lcid->hash;
|
||||
size_t lenhash = L->Lcid->hash_length;
|
||||
ipfs_cid_new(ver, ahash, lenhash, CID_PROTOBUF, &L->Lcid);
|
||||
return L;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
struct Link * Node_Get_Link(struct Node * N, char * Name);
|
||||
|
||||
/*Node_Remove_Link
|
||||
* Removes a link from node if found by name.
|
||||
* @param name: Name of link (char * name)
|
||||
* returns 1 on success, 0 on failure.
|
||||
*/
|
||||
int Node_Remove_Link(char * Name, struct Node * mynode)
|
||||
{
|
||||
for(int i=0; i<mynode->link_ammount; i++)
|
||||
{
|
||||
if(mynode->links[i]->name == Name)
|
||||
{
|
||||
for(int x=i;x<mynode->link_ammount && x+1 != mynode->link_ammount;i++)
|
||||
{
|
||||
memcpy(mynode->links[x],mynode->links[x+1],sizeof(struct Link));
|
||||
}
|
||||
free(mynode->links[mynode->link_ammount-1]);
|
||||
mynode->link_ammount--;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Node_Remove_Link(char * Name, struct Node * mynode);
|
||||
|
||||
/* N_Add_Link
|
||||
* Adds a link to your node
|
||||
* @param mynode: &yournode
|
||||
|
@ -194,28 +118,7 @@ int Node_Remove_Link(char * Name, struct Node * mynode)
|
|||
* @param linksz: sizeof(your cid here)
|
||||
* Returns your node with the newly added link
|
||||
*/
|
||||
struct Node * N_Add_Link(struct Node ** mynode, struct Link * mylink, size_t linksz)
|
||||
{
|
||||
struct Node * Nl = *mynode;
|
||||
Nl->link_ammount++;
|
||||
size_t calculatesize = 0;
|
||||
if(Nl->link_ammount != 0)
|
||||
{
|
||||
for(int i=0; i<Nl->link_ammount-1;i++)
|
||||
{
|
||||
calculatesize = calculatesize + sizeof(Nl->links[i]);
|
||||
}
|
||||
calculatesize = calculatesize + linksz;
|
||||
Nl = (struct Node *) realloc(Nl, sizeof(struct Node) + calculatesize);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nl = (struct Node *) malloc(sizeof(struct Node) + linksz);
|
||||
}
|
||||
Nl->links[Nl->link_ammount-1] = malloc(sizeof(struct Link));
|
||||
memcpy(Nl->links[Nl->link_ammount-1],mylink,sizeof(struct Link));
|
||||
return Nl;
|
||||
}
|
||||
struct Node * N_Add_Link(struct Node ** mynode, struct Link * mylink, size_t linksz);
|
||||
|
||||
/*N_Create_From_Link
|
||||
* Create a node from a link
|
||||
|
@ -223,53 +126,21 @@ struct Node * N_Add_Link(struct Node ** mynode, struct Link * mylink, size_t lin
|
|||
* @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.
|
||||
*/
|
||||
struct Node * N_Create_From_Link(struct Link * mylink)
|
||||
{
|
||||
struct Node * mynode;
|
||||
mynode = (struct Node *) malloc(sizeof(struct Node) + sizeof(struct Link));
|
||||
mynode->link_ammount = 0;
|
||||
mynode->link_ammount++;
|
||||
mynode->links[0] = malloc(sizeof(struct Link));
|
||||
memcpy(mynode->links[0], mylink, sizeof(struct Link));
|
||||
return mynode;
|
||||
}
|
||||
struct Node * N_Create_From_Link(struct Link * mylink);
|
||||
|
||||
/*N_Create_From_Data
|
||||
* @param data: bytes buffer you want to create the node from
|
||||
* returns a node with the data you inputted.
|
||||
*/
|
||||
struct Node * N_Create_From_Data(unsigned char * data)
|
||||
{
|
||||
struct Node * mynode;
|
||||
mynode = (struct Node *) malloc(sizeof(struct Node));
|
||||
mynode->data = data;
|
||||
return mynode;
|
||||
}
|
||||
struct Node * N_Create_From_Data(unsigned char * data);
|
||||
|
||||
/*Node_Resolve_Max_Size
|
||||
* !!!This shouldn't concern you!
|
||||
*Gets the ammount of words that will be returned by Node_Resolve
|
||||
*@Param1: The string that will be processed (eg: char * sentence = "foo/bar/bin")
|
||||
*Returns either -1 if something went wrong or the ammount of words that would be processed.
|
||||
*/
|
||||
int Node_Resolve_Max_Size(char * input1)
|
||||
{
|
||||
if(!input1)
|
||||
{
|
||||
return -1; // Input is null, therefor nothing can be processed.
|
||||
}
|
||||
char input[strlen(input1)];
|
||||
bzero(input, strlen(input1));
|
||||
strcpy(input, input1);
|
||||
int num = 0;
|
||||
char * tr;
|
||||
char * end;
|
||||
tr=strtok_r(input,"/",&end);
|
||||
for(int i = 0;tr;i++)
|
||||
{
|
||||
tr=strtok_r(NULL,"/",&end);
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
int Node_Resolve_Max_Size(char * input1);
|
||||
|
||||
/*Node_Resolve Basically stores everything in a pointer array eg: char * bla[Max_Words_]
|
||||
* !!!This shouldn't concern you!!!
|
||||
|
@ -277,31 +148,8 @@ int Node_Resolve_Max_Size(char * input1)
|
|||
*@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.
|
||||
*/
|
||||
int Node_Resolve(char ** result, char * input1);
|
||||
|
||||
int Node_Resolve(char ** result, char * input1)
|
||||
{
|
||||
if(!input1)
|
||||
{
|
||||
return 0; // Input is null, therefor nothing can be processed.
|
||||
}
|
||||
char input[strlen(input1)];
|
||||
bzero(input, strlen(input1));
|
||||
strcpy(input, input1);
|
||||
char * tr;
|
||||
char * end;
|
||||
tr=strtok_r(input,"/",&end);
|
||||
for(int i = 0;tr;i++)
|
||||
{
|
||||
if(debug == 1)
|
||||
{
|
||||
printf("TR: %s\n", tr);
|
||||
}
|
||||
result[i] = (char *) malloc(strlen(tr)+1);
|
||||
strcpy(result[i], tr);
|
||||
tr=strtok_r(NULL,"/",&end);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/**************************************************************************************************************************************
|
||||
*|||||||||||||||||||||||||||||||||||||||| !!!! IMPORTANT !!! ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*
|
||||
**************************************************************************************************************************************
|
||||
|
@ -323,83 +171,25 @@ struct Link_Proc
|
|||
int ammount; //This will store the ammount of links, so you know what to process.
|
||||
struct Link * links[]; // Link array
|
||||
};
|
||||
|
||||
/*Node_Resolve_Links
|
||||
* Processes a path returning all links.
|
||||
* @param N: The node you want to get links from
|
||||
* @param path: The "foo/bar/bin" path
|
||||
*/
|
||||
struct Link_Proc * Node_Resolve_Links(struct Node * N, char * path)
|
||||
{
|
||||
if(!N || !path)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
int expected_link_ammount = Node_Resolve_Max_Size(path);
|
||||
struct Link_Proc * LProc = (struct Link_Proc *) malloc(sizeof(struct Link_Proc) + sizeof(struct Link) * expected_link_ammount);
|
||||
LProc->ammount = 0;
|
||||
char * linknames[expected_link_ammount];
|
||||
Node_Resolve(linknames, path);
|
||||
for(int i=0;i<expected_link_ammount; i++)
|
||||
{
|
||||
struct Link * proclink;
|
||||
proclink = Node_Get_Link(N, linknames[i]);
|
||||
if(proclink)
|
||||
{
|
||||
LProc->links[i] = (struct Link *)malloc(sizeof(struct Link));
|
||||
memcpy(LProc->links[i], proclink, sizeof(struct Link));
|
||||
LProc->ammount++;
|
||||
free(proclink);
|
||||
}
|
||||
}
|
||||
//Freeing pointer array
|
||||
for(int i=0;i<expected_link_ammount; i++)
|
||||
{
|
||||
free(linknames[i]);
|
||||
}
|
||||
return LProc;
|
||||
}
|
||||
struct Link_Proc * Node_Resolve_Links(struct Node * N, char * path);
|
||||
|
||||
/*Free_link_Proc
|
||||
* frees the Link_Proc struct you created.
|
||||
* @param1: Link_Proc struct (struct Link_Proc *)
|
||||
*/
|
||||
void Free_Link_Proc(struct Link_Proc * LPRC)
|
||||
{
|
||||
if(LPRC->ammount != 0)
|
||||
{
|
||||
for(int i=0;i<LPRC->ammount;i++)
|
||||
{
|
||||
Free_Link(LPRC->links[i]);
|
||||
}
|
||||
}
|
||||
free(LPRC);
|
||||
}
|
||||
void Free_Link_Proc(struct Link_Proc * LPRC);
|
||||
|
||||
/*Node_Tree() Basically a unix-like ls
|
||||
*@Param1: Result char * foo[strlen(sentence)]
|
||||
*@Param2: char sentence[] = foo/bar/bin/whatever
|
||||
*Return: 0 if failure, 1 if success
|
||||
*/
|
||||
int Node_Tree(char * result, char * input1) //I don't know where you use this but here it is.
|
||||
{
|
||||
if(!input1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
char input[strlen(input1)];
|
||||
bzero(input, strlen(input1));
|
||||
strcpy(input, input1);
|
||||
for(int i=0; i<strlen(input); i++)
|
||||
{
|
||||
if(input[i] == '/')
|
||||
{
|
||||
input[i] = ' ';
|
||||
}
|
||||
}
|
||||
strcpy(result, input);
|
||||
if(debug == 1)
|
||||
{
|
||||
printf("Node_Tree Internal Result: %s\n",result);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int Node_Tree(char * result, char * input1); //I don't know where you use this but here it is.
|
||||
|
||||
#endif
|
||||
|
|
32
include/ipfs/unixfs/format.h
Normal file
32
include/ipfs/unixfs/format.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
enum UnixFSFormatType { RAW, FILE, DIRECTORY, METADATA, SYMLINK };
|
||||
|
||||
struct UnixFSData {
|
||||
enum UnixFSFormatType type;
|
||||
unsigned char* data;
|
||||
size_t data_length;
|
||||
};
|
||||
|
||||
struct FSNode {
|
||||
unsigned char* data;
|
||||
size_t data_size;
|
||||
size_t* blocksizes;
|
||||
size_t subtotal;
|
||||
enum UnixFSFormatType type;
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy data into a new struct
|
||||
* @param type the type of data
|
||||
* @param data the acutal data
|
||||
* @param data_length the length of the data array
|
||||
* @param result where the struct will be stored
|
||||
* @returns true(1) on success
|
||||
*/
|
||||
int ipfs_unixfs_format_new(enum UnixFSFormatType type, const unsigned char* data, size_t data_length, struct UnixFSData** result);
|
||||
|
||||
/**
|
||||
* Free memory allocated by _new
|
||||
* @param data the struct
|
||||
* @returns true(1) on success
|
||||
*/
|
||||
int ipfs_unixfs_format_free(struct UnixFSData* data);
|
18
merkledag/Makefile
Normal file
18
merkledag/Makefile
Normal file
|
@ -0,0 +1,18 @@
|
|||
CC = gcc
|
||||
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -Wall
|
||||
|
||||
ifdef DEBUG
|
||||
CFLAGS += -g3
|
||||
endif
|
||||
|
||||
LFLAGS =
|
||||
DEPS =
|
||||
OBJS = merkledag.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
all: $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
|
@ -2,14 +2,30 @@
|
|||
* A basic storage building block of the IPFS system
|
||||
*/
|
||||
|
||||
#include "ipfs/merkledag/merkledag.h"
|
||||
|
||||
/***
|
||||
* Adds a node to the dagService and blockService
|
||||
* @param node the node to add
|
||||
* @param cid the resultant cid that was added
|
||||
* @returns true(1) on success
|
||||
*/
|
||||
int ipfs_merkledag_add(struct Node* node) {
|
||||
int ipfs_merkledag_add(struct Node* node, struct FSRepo* fs_repo) {
|
||||
// taken from merkledag.go line 59
|
||||
// TODO: put in blockstore
|
||||
// TODO: call HasBlock (unsure why as yet)
|
||||
|
||||
// turn the node into a block
|
||||
struct Block* block;
|
||||
ipfs_blocks_block_new(node->data, node->data_size, &block);
|
||||
|
||||
int retVal = fs_repo->config->datastore->datastore_put(block->cid->hash, block->cid->hash_length, block->data, block->data_length, fs_repo->config->datastore);
|
||||
if (retVal == 0) {
|
||||
ipfs_blocks_block_free(block);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Node_Set_Cid(node, block->cid);
|
||||
ipfs_blocks_block_free(block);
|
||||
|
||||
// TODO: call HasBlock (unsure why as yet)
|
||||
return 1;
|
||||
}
|
||||
|
|
18
node/Makefile
Normal file
18
node/Makefile
Normal file
|
@ -0,0 +1,18 @@
|
|||
CC = gcc
|
||||
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -Wall
|
||||
|
||||
ifdef DEBUG
|
||||
CFLAGS += -g3
|
||||
endif
|
||||
|
||||
LFLAGS =
|
||||
DEPS =
|
||||
OBJS = node.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
all: $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
362
node/node.c
362
node/node.c
|
@ -2,6 +2,366 @@
|
|||
* An implementation of an IPFS node
|
||||
* Copying the go-ipfs-node project
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "inttypes.h"
|
||||
#include "ipfs/cid/cid.h"
|
||||
#include "ipfs/node/node.h"
|
||||
|
||||
/* Create_Link
|
||||
* @Param name: The name of the link (char *)
|
||||
* @Param size: Size of the link (size_t)
|
||||
* @Param ahash: An Qmhash
|
||||
*/
|
||||
struct Link * Create_Link(char * name, unsigned char * ahash)
|
||||
{
|
||||
struct Link * mylink;
|
||||
mylink = malloc(sizeof(struct Link));
|
||||
mylink->name = name;
|
||||
int ver = 0;
|
||||
size_t lenhash = strlen(ahash)-1;
|
||||
ipfs_cid_new(ver, ahash, lenhash*2, CID_PROTOBUF, &mylink->Lcid);
|
||||
mylink->size = sizeof(mylink) + mylink->Lcid->hash_length; //Unsure of this
|
||||
return mylink;
|
||||
}
|
||||
|
||||
/* Free_Link
|
||||
* @param L: Free the link you have allocated.
|
||||
*/
|
||||
void Free_Link(struct Link * L)
|
||||
{
|
||||
ipfs_cid_free(L->Lcid);
|
||||
free(L);
|
||||
}
|
||||
|
||||
/*====================================================================================
|
||||
* Node Functions
|
||||
*===================================================================================*/
|
||||
/*Create_Empty_Node
|
||||
* Creates an empty node, allocates the required memory
|
||||
* Returns a fresh new node with no data set in it.
|
||||
*/
|
||||
struct Node * Create_Empty_Node()
|
||||
{
|
||||
struct Node * N;
|
||||
N = (struct Node *)malloc(sizeof(struct Node));
|
||||
return N;
|
||||
}
|
||||
|
||||
/*Node_Set_Data
|
||||
* Sets the data of a node
|
||||
* @param Node: The node which you want to set data in.
|
||||
* @param Data, the data you want to assign to the node
|
||||
* returns 1 on success 0 on failure
|
||||
*/
|
||||
int Node_Set_Data(struct Node * N, unsigned char * Data)
|
||||
{
|
||||
if(!N || !Data)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
N->encoded = NULL;
|
||||
N->cached = NULL;
|
||||
N->data = Data;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*Node_Get_Data
|
||||
* Gets data from a node
|
||||
* @param Node: = The node you want to get data from. (unsigned char *)
|
||||
* Returns data of node.
|
||||
*/
|
||||
unsigned char * Node_Get_Data(struct Node * N)
|
||||
{
|
||||
unsigned char * DATA;
|
||||
DATA = N->data;
|
||||
return DATA;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
return ipfs_cid_new(cid->version, cid->hash, cid->hash_length, cid->codec, &N->cached);
|
||||
}
|
||||
|
||||
/*Node_Copy: Returns a copy of the node you input
|
||||
* @param Node: The node you want to copy (struct CP_Node *)
|
||||
* Returns a copy of the node you wanted to copy.
|
||||
*/
|
||||
struct Node * Node_Copy(struct Node * CP_Node)
|
||||
{
|
||||
struct Node * CN;
|
||||
CN = (struct Node*) malloc(sizeof(struct Node) + sizeof(struct Link) * 2);
|
||||
if(CP_Node->link_ammount != 0)
|
||||
{
|
||||
for(int i=0; i<CP_Node->link_ammount; i++)
|
||||
{
|
||||
CN->links[i] = malloc(sizeof(struct Link));
|
||||
}
|
||||
}
|
||||
memcpy(CN, CP_Node, sizeof(struct Node));
|
||||
memcpy(CN->links[0],CP_Node->links[0], sizeof(struct Link));
|
||||
return CN;
|
||||
}
|
||||
|
||||
/*Node_Delete
|
||||
* Once you are finished using a node, always delete it using this.
|
||||
* It will take care of the links inside it.
|
||||
* @param N: the node you want to free. (struct Node *)
|
||||
*/
|
||||
void Node_Delete(struct Node * N)
|
||||
{
|
||||
if(N->link_ammount > 0)
|
||||
{
|
||||
for(int i=0; i<N->link_ammount; i++)
|
||||
{
|
||||
free(N->links[i]);
|
||||
}
|
||||
}
|
||||
if (N->cached != NULL)
|
||||
free(N->cached);
|
||||
free(N);
|
||||
}
|
||||
|
||||
/*Node_Get_Link
|
||||
* Returns a copy of the link with given name
|
||||
* @param Name: (char * name) searches for link with this name
|
||||
* Returns the link struct if it's found otherwise returns NULL
|
||||
*/
|
||||
struct Link * Node_Get_Link(struct Node * N, char * Name)
|
||||
{
|
||||
struct Link * L;
|
||||
for(int i=0;i<N->link_ammount;i++)
|
||||
{
|
||||
if(strcmp(N->links[i]->name,Name) == 0)
|
||||
{
|
||||
L = (struct Link *)malloc(sizeof(struct Link));
|
||||
memcpy(L,N->links[i],sizeof(struct Link));
|
||||
int ver = L->Lcid->version;
|
||||
char * ahash = L->Lcid->hash;
|
||||
size_t lenhash = L->Lcid->hash_length;
|
||||
ipfs_cid_new(ver, ahash, lenhash, CID_PROTOBUF, &L->Lcid);
|
||||
return L;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*Node_Remove_Link
|
||||
* Removes a link from node if found by name.
|
||||
* @param name: Name of link (char * name)
|
||||
* returns 1 on success, 0 on failure.
|
||||
*/
|
||||
int Node_Remove_Link(char * Name, struct Node * mynode)
|
||||
{
|
||||
for(int i=0; i<mynode->link_ammount; i++)
|
||||
{
|
||||
if(mynode->links[i]->name == Name)
|
||||
{
|
||||
for(int x=i;x<mynode->link_ammount && x+1 != mynode->link_ammount;i++)
|
||||
{
|
||||
memcpy(mynode->links[x],mynode->links[x+1],sizeof(struct Link));
|
||||
}
|
||||
free(mynode->links[mynode->link_ammount-1]);
|
||||
mynode->link_ammount--;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* N_Add_Link
|
||||
* Adds a link to your node
|
||||
* @param mynode: &yournode
|
||||
* @param mylink: the CID you want to create a node from
|
||||
* @param linksz: sizeof(your cid here)
|
||||
* Returns your node with the newly added link
|
||||
*/
|
||||
struct Node * N_Add_Link(struct Node ** mynode, struct Link * mylink, size_t linksz)
|
||||
{
|
||||
struct Node * Nl = *mynode;
|
||||
Nl->link_ammount++;
|
||||
size_t calculatesize = 0;
|
||||
if(Nl->link_ammount != 0)
|
||||
{
|
||||
for(int i=0; i<Nl->link_ammount-1;i++)
|
||||
{
|
||||
calculatesize = calculatesize + sizeof(Nl->links[i]);
|
||||
}
|
||||
calculatesize = calculatesize + linksz;
|
||||
Nl = (struct Node *) realloc(Nl, sizeof(struct Node) + calculatesize);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nl = (struct Node *) malloc(sizeof(struct Node) + linksz);
|
||||
}
|
||||
Nl->links[Nl->link_ammount-1] = malloc(sizeof(struct Link));
|
||||
memcpy(Nl->links[Nl->link_ammount-1],mylink,sizeof(struct Link));
|
||||
return Nl;
|
||||
}
|
||||
|
||||
/*N_Create_From_Link
|
||||
* Create a node from a link
|
||||
* @param mylink: the link you want to create it from. (struct Cid *)
|
||||
* @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.
|
||||
*/
|
||||
struct Node * N_Create_From_Link(struct Link * mylink)
|
||||
{
|
||||
struct Node * mynode;
|
||||
mynode = (struct Node *) malloc(sizeof(struct Node) + sizeof(struct Link));
|
||||
mynode->link_ammount = 0;
|
||||
mynode->link_ammount++;
|
||||
mynode->links[0] = malloc(sizeof(struct Link));
|
||||
memcpy(mynode->links[0], mylink, sizeof(struct Link));
|
||||
return mynode;
|
||||
}
|
||||
|
||||
/*N_Create_From_Data
|
||||
* @param data: bytes buffer you want to create the node from
|
||||
* returns a node with the data you inputted.
|
||||
*/
|
||||
struct Node * N_Create_From_Data(unsigned char * data)
|
||||
{
|
||||
struct Node * mynode;
|
||||
mynode = (struct Node *) malloc(sizeof(struct Node));
|
||||
mynode->data = data;
|
||||
mynode->cached = NULL;
|
||||
return mynode;
|
||||
}
|
||||
|
||||
/*Node_Resolve_Max_Size
|
||||
* !!!This shouldn't concern you!
|
||||
*Gets the ammount of words that will be returned by Node_Resolve
|
||||
*@Param1: The string that will be processed (eg: char * sentence = "foo/bar/bin")
|
||||
*Returns either -1 if something went wrong or the ammount of words that would be processed.
|
||||
*/
|
||||
int Node_Resolve_Max_Size(char * input1)
|
||||
{
|
||||
if(!input1)
|
||||
{
|
||||
return -1; // Input is null, therefor nothing can be processed.
|
||||
}
|
||||
char input[strlen(input1)];
|
||||
bzero(input, strlen(input1));
|
||||
strcpy(input, input1);
|
||||
int num = 0;
|
||||
char * tr;
|
||||
char * end;
|
||||
tr=strtok_r(input,"/",&end);
|
||||
for(int i = 0;tr;i++)
|
||||
{
|
||||
tr=strtok_r(NULL,"/",&end);
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/*Node_Resolve Basically stores everything in a pointer array eg: char * bla[Max_Words_]
|
||||
* !!!This shouldn't concern you!!!
|
||||
*@param1: Pointer array(char * foo[x], X=Whatever ammount there is. should be used with the helper function Node_Resolve_Max_Size)
|
||||
*@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.
|
||||
*/
|
||||
int Node_Resolve(char ** result, char * input1)
|
||||
{
|
||||
if(!input1)
|
||||
{
|
||||
return 0; // Input is null, therefor nothing can be processed.
|
||||
}
|
||||
char input[strlen(input1)];
|
||||
bzero(input, strlen(input1));
|
||||
strcpy(input, input1);
|
||||
char * tr;
|
||||
char * end;
|
||||
tr=strtok_r(input,"/",&end);
|
||||
for(int i = 0;tr;i++)
|
||||
{
|
||||
result[i] = (char *) malloc(strlen(tr)+1);
|
||||
strcpy(result[i], tr);
|
||||
tr=strtok_r(NULL,"/",&end);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*Node_Resolve_Links
|
||||
* Processes a path returning all links.
|
||||
* @param N: The node you want to get links from
|
||||
* @param path: The "foo/bar/bin" path
|
||||
*/
|
||||
struct Link_Proc * Node_Resolve_Links(struct Node * N, char * path)
|
||||
{
|
||||
if(!N || !path)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
int expected_link_ammount = Node_Resolve_Max_Size(path);
|
||||
struct Link_Proc * LProc = (struct Link_Proc *) malloc(sizeof(struct Link_Proc) + sizeof(struct Link) * expected_link_ammount);
|
||||
LProc->ammount = 0;
|
||||
char * linknames[expected_link_ammount];
|
||||
Node_Resolve(linknames, path);
|
||||
for(int i=0;i<expected_link_ammount; i++)
|
||||
{
|
||||
struct Link * proclink;
|
||||
proclink = Node_Get_Link(N, linknames[i]);
|
||||
if(proclink)
|
||||
{
|
||||
LProc->links[i] = (struct Link *)malloc(sizeof(struct Link));
|
||||
memcpy(LProc->links[i], proclink, sizeof(struct Link));
|
||||
LProc->ammount++;
|
||||
free(proclink);
|
||||
}
|
||||
}
|
||||
//Freeing pointer array
|
||||
for(int i=0;i<expected_link_ammount; i++)
|
||||
{
|
||||
free(linknames[i]);
|
||||
}
|
||||
return LProc;
|
||||
}
|
||||
|
||||
/*Free_link_Proc
|
||||
* frees the Link_Proc struct you created.
|
||||
* @param1: Link_Proc struct (struct Link_Proc *)
|
||||
*/
|
||||
void Free_Link_Proc(struct Link_Proc * LPRC)
|
||||
{
|
||||
if(LPRC->ammount != 0)
|
||||
{
|
||||
for(int i=0;i<LPRC->ammount;i++)
|
||||
{
|
||||
Free_Link(LPRC->links[i]);
|
||||
}
|
||||
}
|
||||
free(LPRC);
|
||||
}
|
||||
|
||||
/*Node_Tree() Basically a unix-like ls
|
||||
*@Param1: Result char * foo[strlen(sentence)]
|
||||
*@Param2: char sentence[] = foo/bar/bin/whatever
|
||||
*Return: 0 if failure, 1 if success
|
||||
*/
|
||||
int Node_Tree(char * result, char * input1) //I don't know where you use this but here it is.
|
||||
{
|
||||
if(!input1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
char input[strlen(input1)];
|
||||
bzero(input, strlen(input1));
|
||||
strcpy(input, input1);
|
||||
for(int i=0; i<strlen(input); i++)
|
||||
{
|
||||
if(input[i] == '/')
|
||||
{
|
||||
input[i] = ' ';
|
||||
}
|
||||
}
|
||||
strcpy(result, input);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -2,14 +2,23 @@ CC = gcc
|
|||
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/ -g3 -Wall
|
||||
LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread
|
||||
DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h
|
||||
OBJS = testit.o ../cmd/ipfs/init.o ../commands/argument.o ../commands/command_option.o \
|
||||
../commands/command.o ../commands/cli/parse.o ../core/builder.o ../repo/fsrepo/fs_repo.o \
|
||||
../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o ../repo/config/config.o ../os/utils.o ../repo/config/identity.o \
|
||||
OBJS = testit.o \
|
||||
../blocks/block.o ../blocks/blockstore.o \
|
||||
../cid/cid.o \
|
||||
../cmd/ipfs/init.o \
|
||||
../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.o \
|
||||
../core/builder.o \
|
||||
../datastore/ds_helper.o \
|
||||
../flatfs/flatfs.o \
|
||||
../merkledag/merkledag.o \
|
||||
../multibase/multibase.o \
|
||||
../node/node.o \
|
||||
../os/utils.o \
|
||||
../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \
|
||||
../repo/config/config.o ../repo/config/identity.o \
|
||||
../repo/config/bootstrap_peers.o ../repo/config/datastore.o ../repo/config/gateway.o \
|
||||
../repo/config/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \
|
||||
../thirdparty/ipfsaddr/ipfs_addr.o ../cid/cid.o ../multibase/multibase.o \
|
||||
../flatfs/flatfs.o ../blocks/block.o ../blocks/blockstore.o \
|
||||
../datastore/ds_helper.o
|
||||
../thirdparty/ipfsaddr/ipfs_addr.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
|
45
test/merkledag/test_merkledag.h
Normal file
45
test/merkledag/test_merkledag.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include "ipfs/merkledag/merkledag.h"
|
||||
#include "ipfs/node/node.h"
|
||||
|
||||
int test_merkledag_add_data() {
|
||||
int retVal = 0;
|
||||
|
||||
// open the fs repo
|
||||
struct RepoConfig* repo_config;
|
||||
struct FSRepo* fs_repo;
|
||||
const char* path = "/tmp/.ipfs";
|
||||
|
||||
// create the struct
|
||||
retVal = ipfs_repo_fsrepo_new((char*)path, repo_config, &fs_repo);
|
||||
if (retVal == 0)
|
||||
return 0;
|
||||
|
||||
// open the repository and read the config file
|
||||
retVal = ipfs_repo_fsrepo_open(fs_repo);
|
||||
if (retVal == 0) {
|
||||
ipfs_repo_fsrepo_free(fs_repo);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create data for node
|
||||
size_t binary_data_size = 255;
|
||||
unsigned char binary_data[binary_data_size];
|
||||
for(int i = 0; i < binary_data_size; i++) {
|
||||
binary_data[i] = i;
|
||||
}
|
||||
|
||||
// create a node
|
||||
struct Node* node = N_Create_From_Data(binary_data);
|
||||
|
||||
retVal = ipfs_merkledag_add(node, fs_repo);
|
||||
if (retVal == 0)
|
||||
return 0;
|
||||
|
||||
// make sure everything is correct
|
||||
if (node->cached == NULL)
|
||||
return 0;
|
||||
|
||||
Node_Delete(node);
|
||||
|
||||
return 1;
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#include "cid/test_cid.h"
|
||||
#include "cmd/ipfs/test_init.h"
|
||||
#include "flatfs/test_flatfs.h"
|
||||
#include "merkledag/test_merkledag.h"
|
||||
#include "node/test_node.h"
|
||||
#include "repo/test_repo_bootstrap_peers.h"
|
||||
#include "repo/test_repo_config.h"
|
||||
|
@ -39,7 +40,8 @@ const char* names[] = {
|
|||
"test_blocks_new",
|
||||
"test_repo_bootstrap_peers_init",
|
||||
"test_ipfs_datastore_put",
|
||||
"test_node"
|
||||
"test_node",
|
||||
"test_merkledag_add_data"
|
||||
};
|
||||
|
||||
int (*funcs[])(void) = {
|
||||
|
@ -61,7 +63,8 @@ int (*funcs[])(void) = {
|
|||
test_blocks_new,
|
||||
test_repo_bootstrap_peers_init,
|
||||
test_ipfs_datastore_put,
|
||||
test_node
|
||||
test_node,
|
||||
test_merkledag_add_data
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "ipfs/unixfs/format.h"
|
||||
|
||||
/**
|
||||
* Copy data into a new struct
|
||||
* @param type the type of data
|
||||
* @param data the acutal data
|
||||
* @param data_length the length of the data array
|
||||
* @param result where the struct will be stored
|
||||
* @returns true(1) on success
|
||||
*/
|
||||
int ipfs_unixfs_format_new(enum UnixFSFormatType type, const unsigned char* data,
|
||||
size_t data_length, struct UnixFSData** result) {
|
||||
struct UnixFSData* new_struct = malloc(sizeof(struct UnixFSData));
|
||||
if (new_struct == NULL)
|
||||
return 0;
|
||||
|
||||
new_struct->data = (unsigned char*) malloc(sizeof(unsigned char) * data_length);
|
||||
if (new_struct->data == NULL) {
|
||||
free (new_struct);
|
||||
return 0;
|
||||
}
|
||||
memcpy(new_struct->data, data, data_length);
|
||||
new_struct->type = type;
|
||||
new_struct->data_length = data_length;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free memory allocated by _new
|
||||
* @param data the struct
|
||||
* @returns true(1) on success
|
||||
*/
|
||||
int ipfs_unixfs_format_free(struct UnixFSData* unixfs_data) {
|
||||
free(unixfs_data->data);
|
||||
free(unixfs_data);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue