diff --git a/Makefile b/Makefile index a018366..9a73d52 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,15 @@ all: - cd repo; make all; cd cmd; make all; cd commands; make all; + cd core; make all; + cd os; make all; + cd repo; make all; cd test; make all; clean: - cd repo; make clean; cd cmd; make clean; cd commands; make clean; - cd test; make clean; \ No newline at end of file + cd core; make clean; + cd os; make clean; + cd repo; make clean; + cd test; make clean; diff --git a/cmd/ipfs/Makefile b/cmd/ipfs/Makefile index 1f5306a..1d9b305 100644 --- a/cmd/ipfs/Makefile +++ b/cmd/ipfs/Makefile @@ -10,7 +10,7 @@ OBJS = init.o main.o ../../commands/argument.o ipfs: $(OBJS) $(CC) -o $@ $^ $(LFLAGS) -all: ipfs +all: $(DEPS) clean: rm -f *.o diff --git a/cmd/ipfs/init.c b/cmd/ipfs/init.c index 55aad5f..827ea15 100644 --- a/cmd/ipfs/init.c +++ b/cmd/ipfs/init.c @@ -1,30 +1,117 @@ #include +#include #include "init.h" +#include "../../commands/command_option.h" +#include "../../commands/request.h" +#include "../../core/ipfs_node.h" +#include "../../core/builder.h" +#include "../../repo/config/config.h" +#include "../../repo/fsrepo/fs_repo.h" -int get_init_command(struct command* cmd) { +const int nBitsForKeypairDefault = 2048; + +/*** + * runs before major processing during initialization + * @param request the request + * @returns 0 if a problem, otherwise a 1 + */ +int init_pre_run(struct Request* request) { + //TODO: make sure daemon is not running + return 0; +} + +int initialize_ipns_keyspace(char* repoRoot) { + //TODO: open fs repo + struct FSRepo repo; + int retVal = fs_repo_open(repoRoot, &repo); + //TODO: make a new node, then close it + //TODO: setup offline routing on new node + struct IpfsNode* ipfs_node; + struct Context* ctx; + struct BuildCfg* bld_cfg; + //TODO: see line 185 of init.go, what does core.BldCfg{Repo: r} do? BldCfg is a structure + retVal = core_builder_new_node(ctx, bld_cfg, ipfs_node); + //return namesys_initialize_keyspace(ctx, ipfs_node->DAG, ipfs_node->Namesys, ipfs_node->pinning, ipfs_node->private_key); + return 0; +} + +/** + * called by init_run, to do the heavy lifting + * @param outFile an output stream (stdout) + * @param repoRoot a string + * @param empty true(1) if empty, false(0) if not + * @param nBitsForKeyPair number of bits for key pair + * @param conf the configuration struct + * @returns 0 on error, 1 on success + */ +int do_init(FILE* outFile, char* repoRoot, int empty, int nBitsForKeyPair, struct Config* conf) { + //TODO: verify that it is not already initialized + //TODO: If the conf is null, make one + //TODO: initialize the fs repo + //TODO: add default assets + return initialize_ipns_keyspace(repoRoot); +} + +/*** + * does major processing during initialization + * @param request the request + * @returns 0 if a problem, otherwise a 1 + */ +int init_run(struct Request* request) { + // TODO: make sure offline + // TODO: check parameters for logic errors + // TODO: Initialize + struct Config conf; + // TODO: handle files in request + // do the heavy lifting + int nBitsForKeyPair = request->cmd->options[1]->default_int_val; + do_init(stdout, request->invoc_context->config_root, 1, nBitsForKeyPair, &conf); + + return 0; +} + +/*** + * does the cleanup after major processing during initialization + * @param request the request + * @returns 0 if a problem, otherwise a 1 + */ +int init_post_run(struct Request* request) { + // nothing to do + return 1; +} + +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 cmd->argument_count = 1; + cmd->option_count = 2; + init_command(cmd); // allocate memory for array of pointers - cmd->arguments = malloc(cmd->argument_count * sizeof(struct argument*)); - if (cmd->arguments == NULL) - return 0; - // allocate memory for argument - cmd->arguments[0] = malloc(sizeof(struct argument)); int retVal = init_string_argument(cmd->arguments[0], "default-config", 0, 0, "Initialize with the given configuration"); cmd->arguments[0]->enable_stdin = 1; + // options + cmd->options[0]->name_count = 2; + retVal = init_command_option(cmd->options[0], "Number of bits to use in the generated RSA private key"); + cmd->options[0]->names[0] = "bits"; + cmd->options[0]->names[1] = "b"; + cmd->options[0]->kind = integer; + cmd->options[0]->default_int_val = nBitsForKeypairDefault; + cmd->options[1]->name_count = 2; + retVal = init_command_option(cmd->options[1], "Don't add and pin help files to the local storage"); + cmd->options[1]->default_bool_val = 0; + cmd->options[1]->names[0] = "empty-repo"; + cmd->options[1]->names[1] = "e"; + + // function pointers + cmd->pre_run = init_pre_run; + cmd->run = init_run; + cmd->post_run = init_post_run; + return retVal; } -int uninit_command(struct command* cmd) { - for(int i = 0; i < cmd->argument_count; i++) - uninit_argument(cmd->arguments[i]); - free(cmd->arguments); - return 0; -} + diff --git a/cmd/ipfs/init.h b/cmd/ipfs/init.h index 7f8ef21..68f9354 100644 --- a/cmd/ipfs/init.h +++ b/cmd/ipfs/init.h @@ -11,13 +11,13 @@ * @param command the struct to fill * @returns 0 on failure, otherwise 1 */ -int get_init_command(struct command* command); +int get_init_command(struct Command* command); /*** * Uninitializes all the dynamic memory caused by get_init_command * @param command the struct * @returns 0 on failure, otherwise 1 */ -int uninit_command(struct command* command); +int uninit_command(struct Command* command); #endif diff --git a/commands/Makefile b/commands/Makefile index e884642..dbd1c33 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -1,13 +1,15 @@ CC = gcc CFLAGS = -O0 LFLAGS = -DEPS = argument.h command.h -OBJS = argument.o +DEPS = argument.h command.h request.h command_option.h +OBJS = argument.o command.o command_option.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) all: $(OBJS) + cd cli; make all; clean: - rm -f *.o \ No newline at end of file + rm -f *.o + cd cli; make clean; diff --git a/commands/argument.c b/commands/argument.c index 8035695..ca9bc56 100644 --- a/commands/argument.c +++ b/commands/argument.c @@ -3,11 +3,11 @@ #include "argument.h" -int uninit_argument(struct argument* argument) { +int uninit_argument(struct Argument* argument) { return 1; } -int init_argument(struct argument* argument, char* name, int required, int variadic, char* description) { +int init_argument(struct Argument* argument, char* name, int required, int variadic, char* description) { argument->name = name; argument->required = required; argument->variadic = variadic; @@ -15,14 +15,14 @@ int init_argument(struct argument* argument, char* name, int required, int varia return 1; } -int init_string_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 retVal = init_argument(argument, name, required, variadic, description); if (retVal) argument->type = string; return retVal; } -int init_file_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) { int retVal = init_argument(argument, name, required, variadic, description); if (retVal) argument->type = file; diff --git a/commands/argument.h b/commands/argument.h index cd19956..caddafe 100644 --- a/commands/argument.h +++ b/commands/argument.h @@ -1,11 +1,11 @@ #ifndef __COMMANDS_ARGUMENT_H__ #define __COMMANDS_ARGUMENT_H__ -enum argument_type { string, file }; +enum ArgumentType { string, file }; -struct argument { +struct Argument { char* name; - enum argument_type type; + enum ArgumentType type; int required; int variadic; int supports_stdin; @@ -14,9 +14,9 @@ struct argument { int enable_stdin; }; -int init_argument(struct argument* argument, char* name, int required, int variadic, char* description); -int uninit_argument(struct argument* argument); -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); +int init_argument(struct Argument* argument, char* name, int required, int variadic, char* description); +int uninit_argument(struct Argument* argument); +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/cli/Makefile b/commands/cli/Makefile new file mode 100644 index 0000000..4554582 --- /dev/null +++ b/commands/cli/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -O0 +LFLAGS = +DEPS = parse.h +OBJS = parse.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f *.o diff --git a/commands/cli/parse.c b/commands/cli/parse.c new file mode 100644 index 0000000..5edfa48 --- /dev/null +++ b/commands/cli/parse.c @@ -0,0 +1,5 @@ +#include "parse.h" + +int cli_parse(char** params, FILE* inStream, struct Command* cmd, struct Request* request) { + return 0; +} diff --git a/commands/cli/parse.h b/commands/cli/parse.h new file mode 100644 index 0000000..4da54a4 --- /dev/null +++ b/commands/cli/parse.h @@ -0,0 +1,23 @@ +/*** + * methods to parse the command line parameters +*/ +#ifndef __COMMANDS_CLI_PARSE_H__ +#define __COMMANDS_CLI_PARSE_H__ + +#include + +#include "../command.h" + +/*** + * turns parameters passed in into a Request struct + * @param params the command line parameters + * @param inStream a stream (for piped input) + * @param cmd the command struct, already initialized + * @param request the end result, something that can be passed on that actually does something + * @returns 0 if something bad happens, otherwise 1 + */ +int cli_parse(char** params, FILE* inStream, struct Command* cmd, struct Request* request); + +int cli_parse_opts(char** params, struct Command* cmd, char* path, char** stringVals); + +#endif /* parse_h */ diff --git a/commands/command.c b/commands/command.c new file mode 100644 index 0000000..fb84b1b --- /dev/null +++ b/commands/command.c @@ -0,0 +1,38 @@ +// +// command.c +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// +#include + +#include "command.h" + +int init_command(struct Command* cmd) { + // allocate memory for Argument array + cmd->arguments = malloc(cmd->argument_count * sizeof(struct Argument*)); + if (cmd->arguments == NULL) + return 0; + // allocate memory for each argument + for(int i = 0; i < cmd->argument_count; i++) + cmd->arguments[i] = malloc(sizeof(struct Argument)); + // allocate memory for CommandOption array + cmd->options = malloc(cmd->option_count * sizeof(struct CommandOption*)); + if (cmd->options == NULL) + return 0; + // allocate memory for each CommandOption + for(int i = 0; i < cmd->option_count; i++) + cmd->options[i] = malloc(sizeof(struct CommandOption)); + return 1; +} + +int uninit_command(struct Command* cmd) { + for(int i = 0; i < cmd->argument_count; i++) + uninit_argument(cmd->arguments[i]); + free(cmd->arguments); + for(int i = 0; i < cmd->option_count; i++) + uninit_option(cmd->options[i]); + free(cmd->options); + return 0; +} diff --git a/commands/command.h b/commands/command.h index 70e3220..a55de72 100644 --- a/commands/command.h +++ b/commands/command.h @@ -6,8 +6,10 @@ #define __COMMANDS_COMMAND_H__ #include "argument.h" +#include "request.h" +#include "command_option.h" -struct help_text { +struct HelpText { char* tagline; char* short_description; char** synopsis_options_values; @@ -20,15 +22,16 @@ struct help_text { char* synopsis; }; -struct command { - //struct option* options; - struct argument** arguments; +struct Command { + struct CommandOption** options; + int option_count; + struct Argument** arguments; int argument_count; - //int (*pre_run)(struct request*); - //int (*run)(struct request*); - //int (*post_run)(struct request*); + int (*pre_run)(struct Request*); + int (*run)(struct Request*); + int (*post_run)(struct Request*); //struct marshaller** marshallers; - struct help_text help_text; + struct HelpText help_text; // a boolean to determine if this is an external // binary. @@ -38,4 +41,8 @@ struct command { char** subcommands; }; +// construction/destruction +int init_command(struct Command* cmd); +int uninit_command(struct Command* cmd); + #endif // command.h diff --git a/commands/command_option.c b/commands/command_option.c new file mode 100644 index 0000000..293cf62 --- /dev/null +++ b/commands/command_option.c @@ -0,0 +1,26 @@ +// +// option.c +// c-ipfs +// +// Created by John Jones on 10/26/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#include +#include + +#include "command_option.h" + +int init_command_option(struct CommandOption* option, char* description) { + option->description = description; + // allocate memory for names + option->names = malloc(option->name_count * sizeof(char*)); + if (option->names == NULL) + return 0; + return 1; +} + +int uninit_option(struct CommandOption* option) { + free(option->names); + return 0; +} diff --git a/commands/command_option.h b/commands/command_option.h new file mode 100644 index 0000000..35c7749 --- /dev/null +++ b/commands/command_option.h @@ -0,0 +1,31 @@ +// +// option.h +// c-ipfs +// +// Created by John Jones on 10/26/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#ifndef __COMMANDS_COMMAND_OPTION_H__ +#define __COMMANDS_COMMAND_OPTION_H__ + +enum Kind { invalid, boolean, integer, unsignedInt, decimal, str }; + +struct CommandOption { + char** names; + int name_count; + enum Kind kind; + char* description; + int default_int_val; + int default_bool_val; + uint32_t default_uint_val; + float default_float_val; + char* default_string_val; +}; + +// constructors +int init_command_option(struct CommandOption* option, char* description); +// destructors +int uninit_option(struct CommandOption* option); + +#endif /* option_h */ diff --git a/commands/context.h b/commands/context.h new file mode 100644 index 0000000..cc0e810 --- /dev/null +++ b/commands/context.h @@ -0,0 +1,26 @@ +// +// context.h +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#ifndef __COMMANDS_CONTEXT_H__ +#define __COMMANDS_CONTEXT_H__ + +#include "req_log.h" +#include "../repo/config/config.h" +#include "../core/ipfs_node.h" + +struct Context { + int online; + char* config_root; + struct ReqLog req_log; + struct Config config; + int (*load_config)(char* path); + struct IpfsNode node; + int (*construct_node)(struct IpfsNode* node); +}; + +#endif /* context_h */ diff --git a/commands/req_log.h b/commands/req_log.h new file mode 100644 index 0000000..19fdb49 --- /dev/null +++ b/commands/req_log.h @@ -0,0 +1,31 @@ +// +// req_log.h +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#ifndef __COMMANDS_REQ_LOG_H__ +#define __COMMANDS_REQ_LOG_H__ + +struct ReqLogEntry { + long start_time; + long end_time; + int active; + char* command; + struct Option** options; + struct Argument** args; + int id; + struct Request* req; + struct ReqLog* log; +}; + +struct ReqLog { + struct ReqLogEntry** requests; + int next_id; + //mutex lock; + long keep_duration; +}; + +#endif /* req_log_h */ diff --git a/commands/request.h b/commands/request.h new file mode 100644 index 0000000..928851c --- /dev/null +++ b/commands/request.h @@ -0,0 +1,28 @@ +// +// request.h +// c-ipfs +// +// Created by John Jones on 10/26/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#ifndef __COMMANDS_REQUEST_H__ +#define __COMMANDS_REQUEST_H__ + +#include "context.h" +#include "command.h" + +struct Request { + char* path; + //optmap options; + char* arguments; + //file[] files; + struct Command* cmd; + struct Context* invoc_context; + //context rctx; + //map[string]Option optionDefs; + //map[string]interface{} values; + //ioReader stdin; +}; + +#endif /* request_h */ diff --git a/core/Makefile b/core/Makefile new file mode 100644 index 0000000..6d18de9 --- /dev/null +++ b/core/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -O0 +LFLAGS = +DEPS = builder.h ipfs_node.h +OBJS = builder.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f *.o diff --git a/core/builder.c b/core/builder.c new file mode 100644 index 0000000..099e285 --- /dev/null +++ b/core/builder.c @@ -0,0 +1,14 @@ +// +// builder.c +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#include "builder.h" + +int core_builder_new_node(struct Context* context, struct BuildCfg* build_cfg, struct IpfsNode* buildConfig) { + // TODO: Implement this methods + return 0; +} diff --git a/core/builder.h b/core/builder.h new file mode 100644 index 0000000..0743801 --- /dev/null +++ b/core/builder.h @@ -0,0 +1,29 @@ +// +// builder.h +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#ifndef __CORE_BUILDER_H__ +#define __CORE_BUILDER_H__ + +#include + +#include "../commands/context.h" +#include "../repo/config/config.h" + +struct BuildCfg { + int online; + // ExtraOpts map[string]bool + int permanent; + int nil_repo; + //struct RoutingOption routing; + //struct HostOption host; + //struct Repo repo; +}; + +int core_builder_new_node(struct Context* context, struct BuildCfg* build_cfg, struct IpfsNode* buildConfig); + +#endif /* builder_h */ diff --git a/core/ipfs_node.h b/core/ipfs_node.h new file mode 100644 index 0000000..7dd2906 --- /dev/null +++ b/core/ipfs_node.h @@ -0,0 +1,21 @@ +// +// ipfs_node.h +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#ifndef __CORE_IPFS_NODE_H__ +#define __CORE_IPFS_NODE_H__ + +struct IpfsNode { + //struct PeerId identity; + //struct Repo repo; + //struct Pinner pinning; // an interface + //struct Mount** mounts; + //struct PrivKey* private_key; + // TODO: Add more here +}; + +#endif /* ipfs_node_h */ diff --git a/os/Makefile b/os/Makefile new file mode 100644 index 0000000..7d7fcbe --- /dev/null +++ b/os/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -O0 +LFLAGS = +DEPS = utils.h +OBJS = utils.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f *.o diff --git a/os/utils.c b/os/utils.c new file mode 100644 index 0000000..631f1b6 --- /dev/null +++ b/os/utils.c @@ -0,0 +1,32 @@ +#include "utils.h" + +/** + * get an environment varible from the os + * @param variable the variable to look for + * @returns the results + */ +char* os_utils_getenv(char* variable) { + return getenv(variable); +} + +/** + * join 2 pieces of a filepath, being careful about the slashes + * @param root the root part + * @param extension what should be concatenated + * @param results where to put the results + * @param max_len throw an error if the total is longer than max_len + */ +int os_utils_filepath_join(char* root, char* extension, char* results, int max_len) { + //TODO: implement slash checks + if (strlen(root) + strlen(extension) + 1 > max_len) + return 0; + strncpy(results, root, strlen(root) + 1); + strncat(results, extension, strlen(extension)+1); + return 1; +} + +int os_utils_file_exists(char* file_name) { + if (access(file_name, F_OK) != -1) + return 1; + return 0; +} diff --git a/os/utils.h b/os/utils.h new file mode 100644 index 0000000..bb61a93 --- /dev/null +++ b/os/utils.h @@ -0,0 +1,29 @@ +/** + * OS specific stuff. This is going to get ugly, but at least its in one place + */ +#ifndef __OS_UTILS_H__ +#define __OS_UTILS_H__ + +#include +#include +#include + +/** + * get an environment varible from the os + * @param variable the variable to look for + * @returns the results + */ +char* os_utils_getenv(char* variable); + +/** + * join 2 pieces of a filepath, being careful about the slashes + * @param root the root part + * @param extension what should be concatenated + * @param results where to put the results + * @param max_len throw an error if the total is longer than max_len + */ +int os_utils_filepath_join(char* root, char* extension, char* results, int max_len); + +int os_utils_file_exists(char* file_name); + +#endif /* utils_h */ diff --git a/repo/Makefile b/repo/Makefile index b618e5c..0527ec2 100644 --- a/repo/Makefile +++ b/repo/Makefile @@ -7,6 +7,9 @@ OBJS = repo.o $(CC) -c -o $@ $< $(CFLAGS) all: $(OBJS) - + cd config; make all; + cd fsrepo; make all; clean: - rm -f *.o \ No newline at end of file + rm -f *.o + cd config; make clean; + cd fsrepo; make clean; \ No newline at end of file diff --git a/repo/config/Makefile b/repo/config/Makefile new file mode 100644 index 0000000..d0a5cce --- /dev/null +++ b/repo/config/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -O0 +LFLAGS = +DEPS = config.h datastore.h identity.h +OBJS = config.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f *.o diff --git a/repo/config/config.c b/repo/config/config.c new file mode 100644 index 0000000..80920fe --- /dev/null +++ b/repo/config/config.c @@ -0,0 +1,76 @@ +// +// config.c +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#include +#include +#include + +#include "config.h" +#include "../../os/utils.h" + + +/*** + * public + */ + +/*** + * gets the default path from the environment variable and/or the homedir struct + * NOTE: this allocates memory for result. Clean up after yourself. + * @param result where the result string will reside. + * @returns true(1) on success, or false(0) + */ +int config_get_default_path_root(char* result) { + char* root = os_utils_getenv("IPFS_PATH"); + if (root == NULL) { + root = os_utils_getenv("HOME"); + result = malloc( strlen(root) + 7); + if (result == NULL) + return 0; + strncpy(result, root, strlen(root)+1); + strncat(result, "/.ipfs", 7); + } else { + char* result = malloc(strlen(root)+1); + if (result == NULL) + return 0; + strncpy(result, root, strlen(root)+1); + } + return 1; +} + +/*** + * Returns the path "extension" relative to the configuration root. + * If an empty string is provided for config_root, the default root + * is used. NOTE: be sure to dispose of the memory allocated for result. + * @param config_root the path to the root of the configuration + * @param extension the extension to add to the path + * @param result the result of config_root with extension appended + * @returns true(1) if everything went okay, false(0) otherwise + */ +int config_path(char* config_root, char* extension, char* result, int max_len) { + if (strlen(config_root) == 0) { + char* default_path; + int retVal = config_get_default_path_root(default_path); + if (!retVal) + return retVal; + retVal = os_utils_filepath_join(default_path, extension, result, max_len); + free(default_path); + return retVal; + } + return os_utils_filepath_join(config_root, extension, result, max_len); +} + +/** + * provide the full path of the config file, given the directory. + * NOTE: This allocates memory for result. Make sure to clean up after yourself. + * @param path the path to the config file (without the actual file name) + * @param result the full filename including the path + * @returns true(1) on success, false(0) otherwise + */ +int config_get_file_name(char* path, char* result) { + return 0; +} diff --git a/repo/config/config.h b/repo/config/config.h index 23f3550..be8be38 100644 --- a/repo/config/config.h +++ b/repo/config/config.h @@ -2,10 +2,11 @@ #define __CONFIG_H__ #include "datastore.h" +#include "identity.h" -struct config { - //struct identity identity; - struct datastore datastore; +struct Config { + struct Identity identity; + struct Datastore datastore; //struct address* addresses; //struct mount* mounts; //struct discovery discovery; @@ -19,4 +20,25 @@ struct config { //struct reprovider reprovider; }; +/** + * provide the full path of the config file, given the directory. + * NOTE: This allocates memory for result. Make sure to clean up after yourself. + * @param path the path to the config file (without the actual file name) + * @param result the full filename including the path + * @returns true(1) on success, false(0) otherwise + */ +int config_get_file_name(char* path, char* result); + +/*** + * Returns the path "extension" relative to the configuration root. + * If an empty string is provided for config_root, the default root + * is used. + * @param config_root the path to the root of the configuration + * @param extension the extension to add to the path + * @param result the result of config_root with extension appended + * @param max_len the max length of the result + * @returns true(1) if everything went okay, false(0) otherwise + */ +int config_path(char* config_root, char* extension, char* result, int max_len); + #endif diff --git a/repo/config/datastore.h b/repo/config/datastore.h index 4aa82af..ca3c5ae 100644 --- a/repo/config/datastore.h +++ b/repo/config/datastore.h @@ -5,7 +5,7 @@ //const char* datastore_default_directory = "datastore"; -struct datastore { +struct Datastore { char* type; char* path; char* storage_max; diff --git a/repo/config/identity.h b/repo/config/identity.h new file mode 100644 index 0000000..2323626 --- /dev/null +++ b/repo/config/identity.h @@ -0,0 +1,17 @@ +// +// identity.h +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#ifndef __REPO_CONFIG_IDENTITY_H__ +#define __REPO_CONFIG_IDENTITY_H__ + +struct Identity { + char* peer_id; + char* private_key; +}; + +#endif /* identity_h */ diff --git a/repo/fsrepo/Makefile b/repo/fsrepo/Makefile new file mode 100644 index 0000000..cfa445b --- /dev/null +++ b/repo/fsrepo/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -O0 +LFLAGS = +DEPS = fs_repo.h +OBJS = fs_repo.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f *.o diff --git a/repo/fsrepo/fs_repo.c b/repo/fsrepo/fs_repo.c new file mode 100644 index 0000000..5b21a81 --- /dev/null +++ b/repo/fsrepo/fs_repo.c @@ -0,0 +1,83 @@ +// +// fs_repo.c +// c-ipfs +// +// Created by John Jones on 10/27/16. +// Copyright © 2016 JMJAtlanta. All rights reserved. +// + +#include "fs_repo.h" +#include "../config/config.h" +#include "../../os/utils.h" +/** + * private methods + */ + +/** + * constructs the FSRepo struct. Basically fills in the FSRepo.path + * @param repo_path the path to the repo + * @param repo the struct to fill in + * @returns false(0) if something bad happened, otherwise true(1) + */ +int repo_new_fs_repo(char* repo_path, struct FSRepo* repo) { + //TODO: add the root to the path, based on environment variable HOMEDIR + // but for now, just do this + repo->path = repo_path; + return 1; +} + +/** + * checks to see if the repo is initialized at the given path + * @param full_path the path to the repo + * @returns true(1) if the config file is there, false(0) otherwise + */ +int repo_config_is_initialized(char* full_path) { + char* config_file_full_path; + int retVal = config_get_file_name(full_path, config_file_full_path); + if (!retVal) + return 0; + if (os_utils_file_exists(config_file_full_path)) + retVal = 1; + return retVal; +} + +/*** + * Check to see if the repo is initialized + * @param full_path the path to the repo + * @returns true(1) if it is initialized, false(0) otherwise. + */ +int repo_is_initialized_unsynced(char* full_path) { + return repo_config_is_initialized(full_path); +} + +/** + * checks to see if the repo is initialized + * @param full_path the full path to the repo + * @returns true(1) if it is initialized, otherwise false(0) + */ +int repo_check_initialized(char* full_path) { + // note the old version of this reported an error if the repo was a .go-ipfs repo (by looking at the path) + // this version skips that step + return repo_is_initialized_unsynced(full_path); +} + + +/** + * public methods + */ + +/** + * opens a fsrepo + * @param repo_path the path to the repo + * @param repo where to store the repo info + * @return 0 if there was a problem, otherwise 1 + */ +int fs_repo_open(char* repo_path, struct FSRepo* repo) { + //TODO: lock + int retVal = repo_new_fs_repo(repo_path, repo); + // check if initialized + if (!repo_check_initialized(repo->path)) + return 0; + return 0; +} + diff --git a/repo/fsrepo/fs_repo.h b/repo/fsrepo/fs_repo.h new file mode 100644 index 0000000..368d0eb --- /dev/null +++ b/repo/fsrepo/fs_repo.h @@ -0,0 +1,29 @@ +/*** + * handles a repo on the file system + */ +#ifndef fs_repo_h +#define fs_repo_h + +#include + +/** + * a structure to hold the repo info + */ +struct FSRepo { + int closed; + char* path; + struct IOCloser* lock_file; + struct Config* config; + struct Datastore* data_store; +}; + +/** + * opens a fsrepo + * @param repoPath the path to the repo + * @param repo where to store the repo info + * @return 0 if there was a problem, otherwise 1 + */ +int fs_repo_open(char* repoPath, struct FSRepo* repo); + + +#endif /* fs_repo_h */ diff --git a/repo/repo.c b/repo/repo.c index edaa251..127f44a 100644 --- a/repo/repo.c +++ b/repo/repo.c @@ -1,6 +1,6 @@ #include "repo.h" -int repo_get_config(struct config* config) { +int repo_get_config(struct Config* config) { return 0; } diff --git a/repo/repo.h b/repo/repo.h index cef4d55..59405da 100644 --- a/repo/repo.h +++ b/repo/repo.h @@ -10,17 +10,17 @@ * @param config a place to put the buffer (must have been pre-allocated) * @returns 0 on error */ -int repo_get_config(struct config* config); +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(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_datastore(struct Datastore* datastore); int repo_get_storage_usage(uint64_t* usage); #endif // __REPO_H__ diff --git a/test/Makefile b/test/Makefile index 3cf109a..31be1fa 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,7 +2,9 @@ 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 +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/fs_repo.o ../repo//config/config.o ../os/utils.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/test/cmd/ipfs/test_init.h b/test/cmd/ipfs/test_init.h index 055faa0..d2504df 100644 --- a/test/cmd/ipfs/test_init.h +++ b/test/cmd/ipfs/test_init.h @@ -11,7 +11,7 @@ #include int test_get_init_command() { - struct command cmd = { 0 }; + struct Command cmd = { 0 }; int retVal = 1; // make sure its empty if (cmd.help_text.tagline != NULL) { @@ -36,7 +36,7 @@ int test_get_init_command() { fprintf(stderr, "argument count should be 1"); retVal = 0; } else { - struct argument arg1 = *(cmd.arguments[0]); + struct Argument arg1 = *(cmd.arguments[0]); if (strncmp(arg1.name, "default-config", 14) != 0) { fprintf(stderr, "arg1 wrong name. Expected %s but got %s\n", "default_config", arg1.name); retVal = 0;