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 mode 100755 index 0000000..e38263d Binary files /dev/null and b/cmd/ipfs/ipfs differ 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));