From 689408ffe999949a7e3802f3b67b39e3c6ac408f Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Wed, 26 Oct 2016 20:14:07 -0500 Subject: [PATCH] First stab at ipfs init --- Makefile | 9 +++++++++ cmd/Makefile | 5 +++++ cmd/ipfs/Makefile | 16 +++++++++++++++ cmd/ipfs/init.c | 15 ++++++++++++++ cmd/ipfs/init.h | 11 +++++++++++ cmd/ipfs/ipfs | Bin 0 -> 8936 bytes cmd/ipfs/main.c | 32 ++++++++++++++++++++++++++++++ commands/argument.c | 21 ++++++++++++++++++++ commands/argument.h | 21 ++++++++++++++++++++ commands/command.h | 40 ++++++++++++++++++++++++++++++++++++++ repo/Makefile | 12 ++++++++++++ repo/config/config.h | 22 +++++++++++++++++++++ repo/config/datastore.h | 21 ++++++++++++++++++++ repo/repo.c | 6 ++++++ repo/repo.h | 26 +++++++++++++++++++++++++ test/Makefile | 16 +++++++++++++++ test/cmd/ipfs/test_init.h | 36 ++++++++++++++++++++++++++++++++++ test/repo/test_repo.h | 13 +++++++++++++ test/testit.c | 19 ++++++++++++++++++ test/testit.h | 1 + 20 files changed, 342 insertions(+) create mode 100644 Makefile create mode 100644 cmd/Makefile create mode 100644 cmd/ipfs/Makefile create mode 100644 cmd/ipfs/init.c create mode 100644 cmd/ipfs/init.h create mode 100755 cmd/ipfs/ipfs create mode 100644 cmd/ipfs/main.c create mode 100644 commands/argument.c create mode 100644 commands/argument.h create mode 100644 commands/command.h create mode 100644 repo/Makefile create mode 100644 repo/config/config.h create mode 100644 repo/config/datastore.h create mode 100644 repo/repo.c create mode 100644 repo/repo.h create mode 100644 test/Makefile create mode 100644 test/cmd/ipfs/test_init.h create mode 100644 test/repo/test_repo.h create mode 100644 test/testit.c create mode 100644 test/testit.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5ed0fe5 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: + cd repo; make all; + cd cmd; make all; + cd test; make all; + +clean: + cd repo; make clean; + cd cmd; make clean; + cd test; make clean; \ No newline at end of file diff --git a/cmd/Makefile b/cmd/Makefile new file mode 100644 index 0000000..dd8c1d8 --- /dev/null +++ b/cmd/Makefile @@ -0,0 +1,5 @@ +all: + cd ipfs; make all; + +clean: + cd ipfs; make clean; \ No newline at end of file diff --git a/cmd/ipfs/Makefile b/cmd/ipfs/Makefile new file mode 100644 index 0000000..3952be3 --- /dev/null +++ b/cmd/ipfs/Makefile @@ -0,0 +1,16 @@ +CC = gcc +CFLAGS = -O0 +LFLAGS = +DEPS = init.h +OBJS = init.o main.o ../../commands/argument.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +ipfs: $(OBJS) + $(CC) -o $@ $^ $(LFLAGS) + +all: ipfs + +clean: + rm -f *.o \ No newline at end of file diff --git a/cmd/ipfs/init.c b/cmd/ipfs/init.c new file mode 100644 index 0000000..99dc8a9 --- /dev/null +++ b/cmd/ipfs/init.c @@ -0,0 +1,15 @@ +#include "init.h" + +int get_init_command(struct command* cmd) { + // help text + cmd->help_text.tagline = "Initializes IPFS config file."; + cmd->help_text.short_description = "\nInitializes IPFS configuration files and generates a new keypair.\n\nipfs uses a repository in the local file system. By default, the repo is\nlocated at ~/.ipfs. To change the repo location, set the $IPFS_PATH\nenvironment variable.:\n\n export IPFS_PATH=/path/to/ipfsrepo"; + // arguments + // initialize an array of arguments with size of 1 + struct argument argument; + init_string_argument(&argument, "default-config", 0, 0, "Initialize with the given configuration"); + argument.enable_stdin = 1; + struct argument* array[1] = { &argument }; + cmd->arguments = array; + return 0; +} diff --git a/cmd/ipfs/init.h b/cmd/ipfs/init.h new file mode 100644 index 0000000..335f211 --- /dev/null +++ b/cmd/ipfs/init.h @@ -0,0 +1,11 @@ +/** + * Initialize an IPFS repository + */ +#ifndef __CMD_IPFS_INIT_H__ +#define __CMD_IPFS_INIT_H__ + +#include "../../commands/command.h" + +int get_init_command(struct command* command); + +#endif diff --git a/cmd/ipfs/ipfs b/cmd/ipfs/ipfs new file m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;XC$=PRHpn3;Z_x*+5wH)2>&Tjz8(QDP6+f?hM z+7GDpS*By^dGl?f%==QO26(z?jhI|_vMH8d%NOt(}m|>R@81uFF4X~Tm2NKj+Kl#o3A~Y5HUL~PA9v{4qNAR9YdqK)t z#dkF8-Rk>tztUYwH}?zekb#hakb#hakb#hakb#hakb#hakb#hakb#hahnIovd-(uG zx4lPD>RL~aiga#NWGlLzVXxb)=J_+lic^9?89bRW7sE&*Bg0)&Q?q#!!ia9>(P|%n z-?(HtRPRC^*=#Fa0G1jU5y-Q)h(Zp%jC92@kUQ52`Bc_$kfk;qkuDYrI4NSUm5>N` zWNw|EE$F7j24y>Yi%vZ@v$vpz6$2IKzh?hbaYo6H%ol>paAL9QH zwWnzyUFLA;IJfJgNc&tQl~Q5S9oiz4A@t5(o|LwC)QUsQ^VI#_I)Zp>9rgV9`(%W8Z8*9TB#mpG{ E7eh6!c>n+a literal 0 HcmV?d00001 diff --git a/cmd/ipfs/main.c b/cmd/ipfs/main.c new file mode 100644 index 0000000..0456ade --- /dev/null +++ b/cmd/ipfs/main.c @@ -0,0 +1,32 @@ +#include +#include +#include + +void print_help(int longHelp, FILE* outStream) { + +} + +/** + * command line interface to IPFS. + * Steps: + * 1) parse the command line + * 2) if user requests help, display then exit + * 3) run the command + * 4) output the response + * 5) if anything fails, print the error, maybe with help + */ +int main(int argc, char** argv) { + + if (argc == 2) { + if (strncmp(argv[1], "help", 4) == 0) { + print_help(1, stdout); + exit(0); + } else if (strncmp(argv[1], "--version", 9) == 0) { + argv[1] = "version"; + } + } // end of help + + // parse command line into an invocation + // exit + exit(0); +} diff --git a/commands/argument.c b/commands/argument.c new file mode 100644 index 0000000..38506be --- /dev/null +++ b/commands/argument.c @@ -0,0 +1,21 @@ +#include "argument.h" + +int init_argument(struct argument* argument, char* name, int required, int variadic, char* description) { + argument->name = name; + argument->required = required; + argument->variadic = variadic; + argument->description = description; + return 0; +} + +int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description) { + init_argument(argument, name, required, variadic, description); + argument->type = string; + return 0; +} + +int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description) { + init_argument(argument, name, required, variadic, description); + argument->type = file; + return 0; +} diff --git a/commands/argument.h b/commands/argument.h new file mode 100644 index 0000000..13b4611 --- /dev/null +++ b/commands/argument.h @@ -0,0 +1,21 @@ +#ifndef __COMMANDS_ARGUMENT_H__ +#define __COMMANDS_ARGUMENT_H__ + +enum argument_type { string, file }; + +struct argument { + char* name; + enum argument_type type; + int required; + int variadic; + int supports_stdin; + int recursive; + char* description; + int enable_stdin; +}; + +int init_argument(struct argument* argument, char* name, int required, int variadic, char* description); +int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description); +int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description); + +#endif diff --git a/commands/command.h b/commands/command.h new file mode 100644 index 0000000..1e354de --- /dev/null +++ b/commands/command.h @@ -0,0 +1,40 @@ +/*** + * The structures to commands + */ + +#ifndef __COMMANDS_COMMAND_H__ +#define __COMMANDS_COMMAND_H__ + +#include "argument.h" + +struct help_text { + char* tagline; + char* short_description; + char** synopsis_options_values; + + // optional + char* usage; + char* long_description; + char* options; + char* subcommands; + char* synopsis; +}; + +struct command { + //struct option* options; + struct argument** arguments; + //int (*pre_run)(struct request*); + //int (*run)(struct request*); + //int (*post_run)(struct request*); + //struct marshaller** marshallers; + struct help_text help_text; + + // a boolean to determine if this is an external + // binary. + int external; + + //struct type return_type; + char** subcommands; +}; + +#endif // command.h diff --git a/repo/Makefile b/repo/Makefile new file mode 100644 index 0000000..b618e5c --- /dev/null +++ b/repo/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS = -O0 +DEPS = repo.h +OBJS = repo.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f *.o \ No newline at end of file diff --git a/repo/config/config.h b/repo/config/config.h new file mode 100644 index 0000000..23f3550 --- /dev/null +++ b/repo/config/config.h @@ -0,0 +1,22 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +#include "datastore.h" + +struct config { + //struct identity identity; + struct datastore datastore; + //struct address* addresses; + //struct mount* mounts; + //struct discovery discovery; + //struct ipns ipns; + //struct bootstrap* peer_addresses; + //struct tour tour; + //struct gateway gateway; + //struct supernode_routing supernode_client_config; + //struct api api; + //struct swarm swarm; + //struct reprovider reprovider; +}; + +#endif diff --git a/repo/config/datastore.h b/repo/config/datastore.h new file mode 100644 index 0000000..5a2305e --- /dev/null +++ b/repo/config/datastore.h @@ -0,0 +1,21 @@ +#ifndef __DATASTORE_H__ +#define __DATASTORE_H__ + +#include + +const char* datastore_default_directory = "datastore"; + +struct datastore { + char* type; + char* path; + char* storage_max; + int64_t storage_gc_watermark; + char* gc_period; + + char* params; + int no_sync; + int hash_on_read; + int bloom_filter_size; +}; + +#endif diff --git a/repo/repo.c b/repo/repo.c new file mode 100644 index 0000000..edaa251 --- /dev/null +++ b/repo/repo.c @@ -0,0 +1,6 @@ +#include "repo.h" + +int repo_get_config(struct config* config) { + return 0; +} + diff --git a/repo/repo.h b/repo/repo.h new file mode 100644 index 0000000..cef4d55 --- /dev/null +++ b/repo/repo.h @@ -0,0 +1,26 @@ +#ifndef __REPO_H__ +#define __REPO_H__ + +#include + +#include "config/config.h" + +/** + * Get the config + * @param config a place to put the buffer (must have been pre-allocated) + * @returns 0 on error + */ +int repo_get_config(struct config* config); + +/** + * Retrieves the config + * @param config a place to get the information + * @returns 0 on error + */ +int repo_set_config(struct config* config); +int repo_set_config_key(char* key, void* value); +int repo_get_config_key(char* key, void* value); +int repo_get_datastore(struct datastore* datastore); +int repo_get_storage_usage(uint64_t* usage); + +#endif // __REPO_H__ diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..a39e929 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,16 @@ +CC = gcc +CFLAGS = -O0 +LFLAGS = +DEPS = testit.h repo/test_repo.h cmd/ipfs/test_init.h +OBJS = testit.o ../cmd/ipfs/init.o ../commands/argument.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +testit: $(OBJS) + $(CC) -o $@ $^ $(LFLAGS) + +all: testit + +clean: + rm -f *.o \ No newline at end of file diff --git a/test/cmd/ipfs/test_init.h b/test/cmd/ipfs/test_init.h new file mode 100644 index 0000000..e7cc339 --- /dev/null +++ b/test/cmd/ipfs/test_init.h @@ -0,0 +1,36 @@ +/** + * Testing of cmd/ipfs/init + */ +#ifndef __TEST_INIT_H__ +#define __TEST_INIT_H__ + +#include "../../../cmd/ipfs/init.h" + +#include +#include + +int test_get_init_command() { + struct command cmd = { 0 }; + + // make sure its empty + if (cmd.help_text.tagline != NULL) { + fprintf(stderr, "short description should be null\n"); + return 0; + } + // grab the stuff + get_init_command(&cmd); + + // make sure its right + if (cmd.help_text.tagline == NULL) { + fprintf(stderr, "short description is null\n"); + return 0; + } + if (strcmp(cmd.help_text.tagline, "Initializes IPFS config file.") != 0) { + fprintf(stderr, "short description is not null\n"); + return 0; + } + return 1; +} + +#endif + diff --git a/test/repo/test_repo.h b/test/repo/test_repo.h new file mode 100644 index 0000000..8f1d108 --- /dev/null +++ b/test/repo/test_repo.h @@ -0,0 +1,13 @@ +#ifndef __TEST_REPO_H__ +#define __TEST_REPO_H__ + +#include "../../repo/repo.h" + +#include + +int test_config_repo() { + return 0; +} + +#endif + diff --git a/test/testit.c b/test/testit.c new file mode 100644 index 0000000..fe00d5f --- /dev/null +++ b/test/testit.c @@ -0,0 +1,19 @@ +#include "testit.h" +#include "repo/test_repo.h" +#include "cmd/ipfs/test_init.h" + +int testit(const char* name, int (*func)(void)) { + printf("Testing %s...\n", name); + int retVal = func(); + if (retVal) + printf("%s success!\n", name); + else + printf("** Uh oh! %s failed.**\n", name); + return retVal; +} + +int main(int argc, char** argv) { + testit("config_repo", test_config_repo); + testit("get_init_command", test_get_init_command); + return 1; +} diff --git a/test/testit.h b/test/testit.h new file mode 100644 index 0000000..5463ca5 --- /dev/null +++ b/test/testit.h @@ -0,0 +1 @@ +int testit(const char* name, int (*func)(void));