diff --git a/Makefile b/Makefile index aca6bdc..9cb12f2 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ all: cd flatfs; make all; cd datastore; make all; cd thirdparty; make all; + cd main; make all; cd test; make all; clean: @@ -32,6 +33,7 @@ clean: cd flatfs; make clean; cd datastore; make clean; cd thirdparty; make clean; + cd main; make clean; cd test; make clean; rebuild: clean all diff --git a/importer/importer.c b/importer/importer.c index a8b0516..24fc431 100644 --- a/importer/importer.c +++ b/importer/importer.c @@ -2,6 +2,7 @@ #include "ipfs/importer/importer.h" #include "ipfs/merkledag/merkledag.h" +#include "ipfs/repo/fsrepo/fs_repo.h" #define MAX_DATA_SIZE 262144 // 1024 * 256; @@ -63,3 +64,45 @@ int ipfs_import_file(const char* fileName, struct Node** node, struct FSRepo* fs return 1; } + +/** + * called from the command line + * @param argc the number of arguments + * @param argv the arguments + */ +int ipfs_import(int argc, char** argv) { + /* + * Param 0: ipfs + * param 1: add + * param 2: filename + */ + struct Node* node = NULL; + struct FSRepo* fs_repo = NULL; + + // open the repo + int retVal = ipfs_repo_fsrepo_new(NULL, NULL, &fs_repo); + if (retVal == 0) { + return 0; + } + retVal = ipfs_repo_fsrepo_open(fs_repo); + + // import the file(s) + retVal = ipfs_import_file(argv[2], &node, fs_repo); + + // give some results to the user + int buffer_len = 100; + unsigned char buffer[buffer_len]; + retVal = ipfs_cid_hash_to_base58(node->hash, node->hash_size, buffer, buffer_len); + if (retVal == 0) { + printf("Unable to generate hash\n"); + return 0; + } + printf("added %s %s\n", buffer, argv[2]); + + if (node != NULL) + ipfs_node_free(node); + if (fs_repo != NULL) + ipfs_repo_fsrepo_free(fs_repo); + + return retVal; +} diff --git a/include/ipfs/importer/importer.h b/include/ipfs/importer/importer.h index 34ab524..5b1941a 100644 --- a/include/ipfs/importer/importer.h +++ b/include/ipfs/importer/importer.h @@ -12,4 +12,11 @@ */ int ipfs_import_file(const char* fileName, struct Node** node, struct FSRepo* fs_repo); +/** + * called from the command line + * @param argc the number of arguments + * @param argv the arguments + */ +int ipfs_import(int argc, char** argv); + #endif /* INCLUDE_IPFS_IMPORTER_IMPORTER_H_ */ diff --git a/include/ipfs/repo/init.h b/include/ipfs/repo/init.h new file mode 100644 index 0000000..c49fdc0 --- /dev/null +++ b/include/ipfs/repo/init.h @@ -0,0 +1,17 @@ +#pragma once + +/*** + * Makes a new ipfs repository + * @param path the path to the repository, should end + * with .ipfs, and the directory should already exist. + * @returns true(1) on success + */ +int make_ipfs_repository(const char* path); + +/** + * Initialize a repository, called from the command line + * @param argc number of arguments + * @param argv arguments + * @returns true(1) on success + */ +int ipfs_repo_init(int argc, char** argv); diff --git a/main/Makefile b/main/Makefile new file mode 100644 index 0000000..7c74a8a --- /dev/null +++ b/main/Makefile @@ -0,0 +1,37 @@ +CC = gcc +CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/ -I../../c-protobuf -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 = main.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 \ + ../importer/importer.o \ + ../importer/exporter.o \ + ../merkledag/merkledag.o ../merkledag/node.o \ + ../multibase/multibase.o \ + ../os/utils.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 \ + ../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 \ + ../unixfs/unixfs.o \ + ../../c-protobuf/protobuf.o ../../c-protobuf/varint.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +ipfs: $(OBJS) + $(CC) -o $@ $^ $(LFLAGS) ../../lmdb/libraries/liblmdb/liblmdb.a + +all: ipfs + +clean: + rm -f *.o + rm -f ipfs \ No newline at end of file diff --git a/main/ipfs b/main/ipfs new file mode 100755 index 0000000..6fce399 Binary files /dev/null and b/main/ipfs differ diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..525e679 --- /dev/null +++ b/main/main.c @@ -0,0 +1,57 @@ +#include +#include + +#include "ipfs/repo/init.h" +#include "ipfs/importer/importer.h" + +void stripit(int argc, char** argv) { + char tmp[strlen(argv[argc])]; + strcpy(tmp, &argv[argc][1]); + tmp[strlen(tmp)-1] = 0; + strcpy(argv[argc], tmp); + return; +} + +void strip_quotes(int argc, char** argv) { + for(int i = 0; i < argc; i++) { + if (argv[i][0] == '\'' && argv[i][strlen(argv[i])-1] == '\'') { + stripit(i, argv); + } + } +} + +#define INIT 1 +#define ADD 2 + +/*** + * Basic parsing of command line arguments to figure out where the user wants to go + */ +int parse_arguments(int argc, char** argv) { + if (argc == 1) { + printf("No parameters passed.\n"); + return 0; + } + if (strcmp("init", argv[1]) == 0) { + return INIT; + } + if (strcmp("add", argv[1]) == 0) { + return ADD; + } + return -1; +} + +/*** + * The beginning + */ +int main(int argc, char** argv) { + strip_quotes(argc, argv); + int retVal = parse_arguments(argc, argv); + switch (retVal) { + case (INIT): + return ipfs_repo_init(argc, argv); + break; + case (ADD): + ipfs_import(argc, argv); + break; + } +} diff --git a/repo/Makefile b/repo/Makefile index 3277e1f..26696af 100644 --- a/repo/Makefile +++ b/repo/Makefile @@ -6,7 +6,7 @@ CFLAGS += -g3 endif DEPS = -OBJS = +OBJS = init.c %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/repo/init.c b/repo/init.c new file mode 100644 index 0000000..dce7587 --- /dev/null +++ b/repo/init.c @@ -0,0 +1,77 @@ +#include +#include +#include + +#include "ipfs/os/utils.h" +#include "ipfs/repo/config/config.h" +#include "ipfs/repo/fsrepo/fs_repo.h" + +int make_ipfs_repository(const char* path) { + int retVal; + char currDirectory[1024]; + struct RepoConfig* repo_config; + + printf("initializing ipfs node at %s\n", path); + // build a default repo config + retVal = ipfs_repo_config_new(&repo_config); + if (retVal == 0) + return 0; + printf("generating 2048-bit RSA keypair..."); + retVal = ipfs_repo_config_init(repo_config, 2048, path); + if (retVal == 0) + return 0; + printf("done\n"); + // now the fs_repo + struct FSRepo* fs_repo; + retVal = ipfs_repo_fsrepo_new(path, repo_config, &fs_repo); + if (retVal == 0) + return 0; + // this builds a new repo + retVal = ipfs_repo_fsrepo_init(fs_repo); + if (retVal == 0) { + ipfs_repo_fsrepo_free(fs_repo); + return 0; + } + + // give some results to the user + printf("peer identity: %s\n", fs_repo->config->identity->peer_id); + + // clean up + ipfs_repo_fsrepo_free(fs_repo); + + // make sure the repository exists + retVal = os_utils_filepath_join(path, "config", currDirectory, 1024); + if (retVal == 0) + return 0; + retVal = os_utils_file_exists(currDirectory); + return retVal; +} + + +/** + * Initialize a repository + */ +int ipfs_repo_init(int argc, char** argv) { + char* home_directory = os_utils_get_homedir(); + //allow user to pass directory on command line + if (argc > 2) { + home_directory = argv[2]; + } + // get the full path + int dir_len = strlen(home_directory) + 7; + char dir[dir_len]; + strcpy(dir, home_directory); + os_utils_filepath_join(home_directory, ".ipfs", dir, dir_len); + // make sure it doesn't already exist + if (os_utils_file_exists(dir)) { + printf("Directory already exists: %s\n", dir); + return 0; + } + // make the directory + if (mkdir(dir, S_IRWXU) == -1) { + printf("Unable to create the directory: %s\n", dir); + return 0; + } + // make the repository + return make_ipfs_repository(dir); +} diff --git a/test/Makefile b/test/Makefile index b725c00..9b09625 100644 --- a/test/Makefile +++ b/test/Makefile @@ -15,6 +15,7 @@ OBJS = testit.o test_helper.o \ ../merkledag/merkledag.o ../merkledag/node.o \ ../multibase/multibase.o \ ../os/utils.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 \ ../repo/config/bootstrap_peers.o ../repo/config/datastore.o ../repo/config/gateway.o \ diff --git a/test/test_helper.c b/test/test_helper.c index 453e59b..387b69b 100644 --- a/test/test_helper.c +++ b/test/test_helper.c @@ -2,6 +2,7 @@ #include #include +#include "ipfs/repo/init.h" #include "ipfs/repo/fsrepo/fs_repo.h" #include "ipfs/os/utils.h" @@ -67,10 +68,9 @@ int remove_directory(const char *path) return r; } -int make_ipfs_repository(const char* path) { - int retVal; - char currDirectory[1024]; - struct RepoConfig* repo_config; +int drop_and_build_repository(const char* path) { + int retVal = 0; + char currDirectory[strlen(path) + 20]; if (os_utils_file_exists(path)) { retVal = os_utils_filepath_join(path, "config", currDirectory, 1024); @@ -89,37 +89,7 @@ int make_ipfs_repository(const char* path) { mkdir(path, S_IRWXU); } - // build a default repo config - retVal = ipfs_repo_config_new(&repo_config); - if (retVal == 0) - return 0; - retVal = ipfs_repo_config_init(repo_config, 2048, path); - if (retVal == 0) - return 0; - // now the fs_repo - struct FSRepo* fs_repo; - retVal = ipfs_repo_fsrepo_new(path, repo_config, &fs_repo); - if (retVal == 0) - return 0; - // this builds a new repo - retVal = ipfs_repo_fsrepo_init(fs_repo); - if (retVal == 0) { - ipfs_repo_fsrepo_free(fs_repo); - return 0; - } - // clean up - ipfs_repo_fsrepo_free(fs_repo); - - // make sure the repository exists - retVal = os_utils_filepath_join(path, "config", currDirectory, 1024); - if (retVal == 0) - return 0; - retVal = os_utils_file_exists(currDirectory); - return retVal; -} - -int drop_and_build_repository(const char* path) { return make_ipfs_repository(path); }