From ef380f2a6915e978bbd9f28fb7a7a1b495c6e94d Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Fri, 30 Dec 2016 01:18:03 -0300 Subject: [PATCH] Initial implementation of pin/pin --- Makefile | 2 ++ include/ipfs/pin/pin.h | 32 ++++++++++++++++++++++++++++++ include/ipfs/util/errs.h | 3 ++- main/Makefile | 2 ++ pin/Makefile | 18 +++++++++++++++++ pin/pin.c | 43 ++++++++++++++++++++++++++++++++++++++++ util/errs.c | 3 ++- 7 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 include/ipfs/pin/pin.h create mode 100644 pin/Makefile create mode 100644 pin/pin.c diff --git a/Makefile b/Makefile index 3281c2e..1d1b767 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ all: cd merkledag; make all; cd multibase; make all; cd os; make all; + cd pin; make all; cd repo; make all; cd flatfs; make all; cd datastore; make all; @@ -30,6 +31,7 @@ clean: cd merkledag; make clean; cd multibase; make clean; cd os; make clean; + cd pin; make clean; cd repo; make clean; cd flatfs; make clean; cd datastore; make clean; diff --git a/include/ipfs/pin/pin.h b/include/ipfs/pin/pin.h new file mode 100644 index 0000000..9594c61 --- /dev/null +++ b/include/ipfs/pin/pin.h @@ -0,0 +1,32 @@ +#ifndef IPFS_PIN_H + #define IPFS_PIN_H + + #include "ipfs/util/errs.h" + + #ifdef IPFS_PIN_C + const char *ipfs_pin_linkmap[] = { + "recursive", + "direct", + "indirect", + "internal", + "not pinned", + "any", + "all" + }; + #else // IPFS_PIN_C + extern const char *ipfs_pin_map[]; + #endif // IPFS_PIN_C + enum { + Recursive = 0, + Direct, + Indirect, + Internal, + NotPinned, + Any, + All + }; + + typedef int PinMode; + + int ipfs_pin_init (); +#endif // IPFS_PIN_H diff --git a/include/ipfs/util/errs.h b/include/ipfs/util/errs.h index a872dc6..5c15308 100644 --- a/include/ipfs/util/errs.h +++ b/include/ipfs/util/errs.h @@ -26,6 +26,7 @@ ErrResolveLimit, ErrInvalidSignature, ErrInvalidSignatureFmt, - ErrNoRecord + ErrNoRecord, + ErrCidDecodeFailed } ErrsIdx; #endif // IPFS_ERRS_H diff --git a/main/Makefile b/main/Makefile index 510c568..3e7a37d 100644 --- a/main/Makefile +++ b/main/Makefile @@ -9,6 +9,7 @@ OBJS = main.o \ ../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.o \ ../core/builder.o \ ../datastore/ds_helper.o \ + ../datastore/key.o \ ../dnslink/dnslink.o \ ../flatfs/flatfs.o \ ../importer/importer.o ../importer/exporter.o ../importer/resolver.o \ @@ -17,6 +18,7 @@ OBJS = main.o \ ../multibase/multibase.o \ ../namesys/isdomain.o \ ../os/utils.o \ + ../pin/pin.o \ ../repo/init.o \ ../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \ ../repo/config/config.o ../repo/config/identity.o \ diff --git a/pin/Makefile b/pin/Makefile new file mode 100644 index 0000000..d0ba437 --- /dev/null +++ b/pin/Makefile @@ -0,0 +1,18 @@ +CC = gcc +CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -Wall + +ifdef DEBUG +CFLAGS += -g3 +endif + +LFLAGS = +DEPS = +OBJS = pin.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f *.o diff --git a/pin/pin.c b/pin/pin.c new file mode 100644 index 0000000..bd3df84 --- /dev/null +++ b/pin/pin.c @@ -0,0 +1,43 @@ +#include +#include + +#define IPFS_PIN_C +#include "ipfs/pin/pin.h" + +#include "ipfs/cid/cid.h" +#include "ipfs/datastore/key.h" +#include "ipfs/util/errs.h" + +// package pin implements structures and methods to keep track of +// which objects a user wants to keep stored locally. + +#define PIN_DATASTOREKEY_SIZE 100 +char *pinDatastoreKey = NULL; +size_t pinDatastoreKeySize = 0; + +struct Cid *emptyKey = NULL; + +int ipfs_pin_init () +{ + int err; + unsigned char *empty_hash = (unsigned char*) "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"; + + if (!pinDatastoreKey) { // initialize just one time. + pinDatastoreKey = malloc(PIN_DATASTOREKEY_SIZE); + if (!pinDatastoreKey) { + return ErrAllocFailed; + } + err = ipfs_datastore_key_new("/local/pins", pinDatastoreKey, PIN_DATASTOREKEY_SIZE, &pinDatastoreKeySize); + if (err) { + free (pinDatastoreKey); + pinDatastoreKey = NULL; + return err; + } + + if (!ipfs_cid_protobuf_decode(empty_hash, strlen ((char*)empty_hash), &emptyKey)) { + return ErrCidDecodeFailed; + } + } + + return 0; +} diff --git a/util/errs.c b/util/errs.c index 6083506..ad4884c 100644 --- a/util/errs.c +++ b/util/errs.c @@ -28,5 +28,6 @@ char *Err[] = { "resolve depth exceeded", NULL, "Invalid value. Not signed by PrivateKey corresponding to %s", - "no usable records in given set" + "no usable records in given set", + "failed to decode empty key constant" };