adjusted makefiles, generating config file for new ipfs repository

This commit is contained in:
John Jones 2016-11-10 08:28:51 -05:00
parent 8d82e8235b
commit 32d187faa4
26 changed files with 276 additions and 79 deletions

View file

@ -1,3 +1,7 @@
DEBUG = true
export DEBUG
all:
cd cmd; make all;
cd commands; make all;

View file

@ -1,5 +1,10 @@
CC = gcc
CFLAGS = -O0
CFLAGS = -O0 -I../../include -I../../../c-libp2p/include
ifdef DEBUG
CFLAGS += -g3
endif
LFLAGS =
DEPS =
OBJS = init.o main.o ../../commands/argument.o
@ -10,7 +15,7 @@ OBJS = init.o main.o ../../commands/argument.o
ipfs: $(OBJS)
$(CC) -o $@ $^ $(LFLAGS)
all: $(DEPS)
all: $(OBJS)
clean:
rm -f *.o

View file

@ -2,8 +2,8 @@
#include <stdio.h>
#include "ipfs/cmd/ipfs/init.h"
#include "ipfs/commands/request.h"
#include "ipfs/commands/command_option.h"
//#include "ipfs/commands/request.h"
#include "ipfs/os/utils.h"
#include "ipfs/core/ipfs_node.h"
#include "ipfs/core/builder.h"
@ -19,7 +19,7 @@ const int nBitsForKeypairDefault = 2048;
*/
int init_pre_run(struct Request* request) {
//TODO: make sure daemon is not running
return 0;
return 1;
}
int initialize_ipns_keyspace(char* repo_root) {
@ -35,13 +35,13 @@ int initialize_ipns_keyspace(char* repo_root) {
//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;
return retVal;
}
/**
* called by init_run, to do the heavy lifting
* @param out_file an output stream (stdout)
* @param repo_root a string
* @param repo_root a path that is where the .ipfs directory will be put
* @param empty true(1) if empty, false(0) if not
* @param num_bits_for_keypair number of bits for key pair
* @param conf the configuration struct
@ -52,12 +52,18 @@ int do_init(FILE* out_file, char* repo_root, int empty, int num_bits_for_keypair
if (!os_utils_directory_writeable(repo_root))
return 0;
// verify that it is not already initialized
if (!fs_repo_is_initialized(repo_root))
if (fs_repo_is_initialized(repo_root))
return 0;
//TODO: If the conf is null, make one
if (conf == NULL)
repo_config_init(conf, num_bits_for_keypair, repo_root);
//TODO: initialize the fs repo
if ( conf->identity.peer_id == NULL) {
int retVal = repo_config_init(conf, num_bits_for_keypair, repo_root);
if (retVal == 0)
return 0;
}
// initialize the fs repo
int retVal = fs_repo_init(repo_root, conf);
if (retVal == 0)
return 0;
//TODO: add default assets
return initialize_ipns_keyspace(repo_root);
}
@ -71,13 +77,11 @@ int init_run(struct Request* request) {
// TODO: make sure offline
// TODO: check parameters for logic errors
// TODO: Initialize
struct RepoConfig conf;
struct RepoConfig conf = {0};
// TODO: handle files in request
// do the heavy lifting
int num_bits_for_key_pair = request->cmd->options[1]->default_int_val;
do_init(stdout, request->invoc_context->config_root, 1, num_bits_for_key_pair, &conf);
return 0;
int num_bits_for_key_pair = request->cmd.options[0]->default_int_val;
return do_init(stdout, request->invoc_context->config_root, 1, num_bits_for_key_pair, &conf);
}
/***
@ -90,27 +94,31 @@ int init_post_run(struct Request* request) {
return 1;
}
int get_init_command(struct Command* cmd) {
int ipfs_cmd_ipfs_init_command_new(struct Command* cmd) {
int retVal = 1;
// 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";
cmd->argument_count = 1;
cmd->option_count = 2;
init_command(cmd);
commands_command_init(cmd);
// allocate memory for array of pointers
int retVal = init_string_argument(cmd->arguments[0], "default-config", 0, 0, "Initialize with the given configuration");
retVal = commands_argument_init(cmd->arguments[0], "default-config", 0, 0, "Initialize with the given configuration");
if (retVal == 0)
return 0;
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");
retVal = commands_command_option_init(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");
retVal = commands_command_option_init(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";
@ -123,4 +131,13 @@ int get_init_command(struct Command* cmd) {
return retVal;
}
/***
* Uninitializes all the dynamic memory caused by get_init_command
* @param command the struct
* @returns 0 on failure, otherwise 1
*/
int ipfs_cmd_ipfs_init_command_free(struct Command* command) {
// NOTE: commands_command_free takes care of arguments and command_options
commands_command_free(command);
return 1;
}

View file

@ -1,7 +1,9 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include
LFLAGS =
DEPS =
DEPS = ../include/ipfs/commands/argument.h ../include/ipfs/commands/command_option.h \
../include/ipfs/commands/command.h ../include/ipfs/commands/context.h \
../include/ipfs/commands/req_log.h ../include/ipfs/commands/request.h
OBJS = argument.o command.o command_option.o
%.o: %.c $(DEPS)

View file

@ -3,11 +3,11 @@
#include "ipfs/commands/argument.h"
int uninit_argument(struct Argument* argument) {
int commands_argument_free(struct Argument* argument) {
return 1;
}
int init_argument(struct Argument* argument, char* name, int required, int variadic, char* description) {
int commands_argument_init(struct Argument* argument, char* name, int required, int variadic, char* description) {
argument->name = name;
argument->required = required;
argument->variadic = variadic;
@ -15,15 +15,15 @@ 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 retVal = init_argument(argument, name, required, variadic, description);
int commands_argument_string_init(struct Argument* argument, char* name, int required, int variadic, char* description) {
int retVal = commands_argument_init(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 retVal = init_argument(argument, name, required, variadic, description);
int commands_argument_file_init(struct Argument* argument, char* name, int required, int variadic, char* description) {
int retVal = commands_argument_init(argument, name, required, variadic, description);
if (retVal)
argument->type = file;
return retVal;

View file

@ -9,7 +9,7 @@
#include "ipfs/commands/command.h"
int init_command(struct Command* cmd) {
int commands_command_init(struct Command* cmd) {
// allocate memory for Argument array
cmd->arguments = malloc(cmd->argument_count * sizeof(struct Argument*));
if (cmd->arguments == NULL)
@ -27,12 +27,14 @@ int init_command(struct Command* cmd) {
return 1;
}
int uninit_command(struct Command* cmd) {
int commands_command_free(struct Command* cmd) {
// arguments
for(int i = 0; i < cmd->argument_count; i++)
uninit_argument(cmd->arguments[i]);
commands_argument_free(cmd->arguments[i]);
free(cmd->arguments);
//command options
for(int i = 0; i < cmd->option_count; i++)
uninit_option(cmd->options[i]);
commands_command_option_free(cmd->options[i]);
free(cmd->options);
return 0;
}

View file

@ -11,7 +11,7 @@
#include "ipfs/commands/command_option.h"
int init_command_option(struct CommandOption* option, char* description) {
int commands_command_option_init(struct CommandOption* option, char* description) {
option->description = description;
// allocate memory for names
option->names = malloc(option->name_count * sizeof(char*));
@ -20,7 +20,7 @@ int init_command_option(struct CommandOption* option, char* description) {
return 1;
}
int uninit_option(struct CommandOption* option) {
int commands_command_option_free(struct CommandOption* option) {
free(option->names);
return 0;
}

View file

@ -10,5 +10,5 @@
int core_builder_new_node(struct Context* context, struct BuildCfg* build_cfg, struct IpfsNode* buildConfig) {
// TODO: Implement this methods
return 0;
return 1;
}

View file

@ -11,13 +11,13 @@
* @param command the struct to fill
* @returns 0 on failure, otherwise 1
*/
int get_init_command(struct Command* command);
int ipfs_cmd_ipfs_init_command_new(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 ipfs_cmd_ipfs_init_command_free(struct Command* command);
#endif

View file

@ -14,9 +14,41 @@ 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);
/**
* Initialize an argument structure
* @param argument the structure to initialize
* @param name the name of the argument
* @param required true(1) if the argument is required
* @param variadic true(1) if the argument is variadic
* @param description the description of the argument
* @returns true(1) if all went okay
*/
int commands_argument_init(struct Argument* argument, char* name, int required, int variadic, char* description);
/***
* Free resources caused by init of argument
* @param argument the structure to clean up
* @returns true(1)
*/
int commands_argument_free(struct Argument* argument);
/***
* initializes a string type argument
* @param argument the structure to initialize
* @param name the name of the argument
* @param required true(1) if the argument is required
* @param variadic true(1) if the argument is variadic
* @param description the description of the argument
* @returns true(1) if all went okay
*/
int commands_argument_string_init(struct Argument* argument, char* name, int required, int variadic, char* description);
/***
* initializes a file type argument
* @param argument the structure to initialize
* @param name the name of the argument
* @param required true(1) if the argument is required
* @param variadic true(1) if the argument is variadic
* @param description the description of the argument
* @returns true(1) if all went okay
*/
int commands_argument_file_init(struct Argument* argument, char* name, int required, int variadic, char* description);
#endif

View file

@ -6,9 +6,12 @@
#define __COMMANDS_COMMAND_H__
#include "ipfs/commands/argument.h"
#include "ipfs/commands/request.h"
//#include "ipfs/commands/request.h"
#include "command_option.h"
// forward declaration
struct Request;
struct HelpText {
char* tagline;
char* short_description;
@ -42,7 +45,7 @@ struct Command {
};
// construction/destruction
int init_command(struct Command* cmd);
int uninit_command(struct Command* cmd);
int commands_command_init(struct Command* cmd);
int commands_command_free(struct Command* cmd);
#endif // command.h

View file

@ -25,9 +25,19 @@ struct CommandOption {
char* default_string_val;
};
// constructors
int init_command_option(struct CommandOption* option, char* description);
// destructors
int uninit_option(struct CommandOption* option);
/***
* Allocate the resources needed for a command option
* @param option the CommandOption to initialize
* @param description a description of this CommandOption
* @returns true(1) on success
*/
int commands_command_option_init(struct CommandOption* option, char* description);
/***
* Cleans up the resources of a CommandOption
* @param option the CommandOption to clean up
* @returns true(1)
*/
int commands_command_option_free(struct CommandOption* option);
#endif /* option_h */

View file

@ -17,7 +17,7 @@ struct Request {
//optmap options;
char* arguments;
//file[] files;
struct Command* cmd;
struct Command cmd;
struct Context* invoc_context;
//context rctx;
//map[string]Option optionDefs;

View file

@ -21,4 +21,11 @@ struct Identity {
*/
int repo_config_identity_new(struct Identity* identity, unsigned long num_bits_for_keypair);
/***
* Frees resources held by Identity
* @param identity the identity that we're cleaning up
* @returns true(0)
*/
int repo_config_identity_free(struct Identity* identity);
#endif /* identity_h */

View file

@ -42,5 +42,13 @@ int fs_repo_is_initialized(char* repo_path);
*/
int fs_repo_write_config_file(char* path, struct RepoConfig* config);
/**
* Initializes a new FSRepo at the given path with the provided config
* @param repo_path the path to use
* @param config the information for the config file
* @returns true(1) on success
*/
int fs_repo_init(char* repo_path, struct RepoConfig* config);
#endif /* fs_repo_h */

View file

@ -46,6 +46,6 @@ int os_utils_file_exists(char* file_name) {
}
int os_utils_directory_writeable(char* path) {
int result = access("/root/", W_OK);
int result = access(path, W_OK);
return result == 0;
}

View file

@ -1,5 +1,10 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include
ifdef DEBUG
CFLAGS += -g3
endif
DEPS =
OBJS = repo.o

View file

@ -1,5 +1,10 @@
CC = gcc
CFLAGS = -O0 -I../../include -I../../../c-libp2p/include
ifdef DEBUG
CFLAGS += -g3
endif
LFLAGS =
DEPS = config.h datastore.h identity.h
OBJS = config.o

View file

@ -29,10 +29,12 @@ int repo_config_bootstrap_peers_retrieve(struct BootstrapPeers* list) {
list->num_peers = 9;
// allocate memory for list
list->peers = malloc(sizeof(struct IPFSAddr*) * list->num_peers);
if (list->peers == NULL)
return 0;
for(int i = 0; i < list->num_peers; i++) {
// allocate memory for struct
struct IPFSAddr* currAddr = malloc(sizeof(struct IPFSAddr));
struct IPFSAddr* currAddr = (struct IPFSAddr*)malloc(sizeof(struct IPFSAddr));
if (currAddr == NULL)
return 0;
// allocate memory for string
@ -44,11 +46,12 @@ int repo_config_bootstrap_peers_retrieve(struct BootstrapPeers* list) {
}
int repo_config_bootstrap_peers_free(struct BootstrapPeers* list) {
int array_size = sizeof(list);
for(int i = 0; i < array_size; i++) {
for(int i = 0; i < list->num_peers; i++) {
if (list->peers[i] != NULL) {
ipfsaddr_free(list->peers[i]);
free(list->peers[i]);
}
}
return 1;
}

View file

@ -30,3 +30,9 @@ int repo_config_identity_new(struct Identity* identity, unsigned long num_bits_f
// TODO: Store peer id in identity struct
return 1;
}
int repo_config_identity_free(struct Identity* identity) {
if (identity->private_key.der != NULL)
free(identity->private_key.der);
return 0;
}

View file

@ -1,5 +1,10 @@
CC = gcc
CFLAGS = -O0 -I../../include -I../../../c-libp2p/include
ifdef DEBUG
CFLAGS += -g3
endif
LFLAGS =
DEPS =
OBJS = fs_repo.o

View file

@ -6,6 +6,8 @@
// Copyright © 2016 JMJAtlanta. All rights reserved.
//
#include "libp2p/crypto/encoding/base64.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/os/utils.h"
/**
@ -27,7 +29,14 @@ int repo_config_write_config_file(char* full_filename, struct RepoConfig* config
fprintf(out_file, " \"Identity\": {\n");
fprintf(out_file, " \"PeerID\": \"%s\",\n", config->identity.peer_id);
// TODO: print correct format of private key
//fprintf(out_file, " \"PrivKey\": \"%s\"\n", config->identity.private_key.base64);
// first base 64 it
size_t encoded_size = base64_encode_length(config->identity.private_key.der, config->identity.private_key.der_length);
unsigned char encoded_buffer[encoded_size + 1];
int retVal = base64_encode(config->identity.private_key.der, config->identity.private_key.der_length, encoded_buffer, encoded_size, &encoded_size);
if (retVal == 0)
return 0;
encoded_buffer[encoded_size] = 0;
fprintf(out_file, " \"PrivKey\": \"%s\"\n", encoded_buffer);
fprintf(out_file, " },\n");
fprintf(out_file, " \"Datastore\": {\n");
fprintf(out_file, " \"Type\": \"%s\",\n", config->datastore.type);
@ -98,13 +107,30 @@ int repo_config_write_config_file(char* full_filename, struct RepoConfig* config
* @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) {
int fs_repo_new_fs_repo(char* repo_path, struct FSRepo* repo) {
if (repo_path == NULL) {
// get the user's home directory
char* home_dir = os_utils_get_homedir();
unsigned long newPathLen = strlen(home_dir) + strlen(repo_path) + 2; // 1 for slash and 1 for end
char* default_subdir = "/.ipfs";
unsigned long newPathLen = strlen(home_dir) + strlen(default_subdir) + 2; // 1 for slash and 1 for end
char* newPath = malloc(sizeof(char) * newPathLen);
os_utils_filepath_join(os_utils_get_homedir(), repo_path, newPath, newPathLen);
os_utils_filepath_join(os_utils_get_homedir(), default_subdir, newPath, newPathLen);
repo->path = newPath;
} else {
int len = strlen(repo_path) + 1;
repo->path = (char*)malloc(len);
strncpy(repo->path, repo_path, len);
}
return 1;
}
/**
* Cleans up memory
* @param repo the struct to clean up
* @returns true(1) on success
*/
int fs_repo_free(struct FSRepo* repo) {
free(repo->path);
return 1;
}
@ -156,7 +182,7 @@ int repo_check_initialized(char* full_path) {
int fs_repo_open_config(struct FSRepo* repo) {
//TODO: open config file
//TODO: read into the FSRepo struct
return 0;
return 1;
}
/***
@ -165,7 +191,8 @@ int fs_repo_open_config(struct FSRepo* repo) {
* @returns 0 on failure, otherwise 1
*/
int fs_repo_open_datastore(struct FSRepo* repo) {
return 0;
//TODO: this
return 1;
}
/**
@ -181,23 +208,33 @@ int fs_repo_open_datastore(struct FSRepo* repo) {
int fs_repo_open(char* repo_path, struct FSRepo* repo) {
//TODO: lock
// get the path set in the repo struct
int retVal = repo_new_fs_repo(repo_path, repo);
if (retVal == 0)
int retVal = fs_repo_new_fs_repo(repo_path, repo);
if (retVal == 0) {
fs_repo_free(repo);
return 0;
}
// check if initialized
if (!repo_check_initialized(repo->path))
if (!repo_check_initialized(repo->path)) {
fs_repo_free(repo);
return 0;
}
//TODO: lock the file (remember to unlock)
//TODO: check the version, and make sure it is correct
//TODO: make sure the directory is writable
//TODO: open the config
fs_repo_open_config(repo);
//TODO: open the datastore. Note: the config file has the datastore type
fs_repo_open_datastore(repo);
if (!fs_repo_open_config(repo)) {
fs_repo_free(repo);
return 0;
}
//TODO: open the datastore. Note: the config file has the datastore type
if (!fs_repo_open_datastore(repo)) {
fs_repo_free(repo);
return 0;
}
return 1;
}
/***
* checks to see if the repo is initialized
@ -210,6 +247,12 @@ int fs_repo_is_initialized(char* repo_path) {
return fs_repo_is_initialized_unsynced(repo_path);
}
/**
* Initializes a new FSRepo at the given path with the provided config
* @param path the path to use
* @param config the information for the config file
* @returns true(1) on success
*/
int fs_repo_init(char* path, struct RepoConfig* config) {
// TODO: Do a lock so 2 don't do this at the same time

View file

@ -1,7 +1,7 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -g3
LFLAGS = -L../../c-libp2p -lp2p
DEPS =
DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h
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 ../repo/config/identity.o \
@ -12,11 +12,11 @@ OBJS = testit.o ../cmd/ipfs/init.o ../commands/argument.o ../commands/command_op
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
testit: $(OBJS)
test_ipfs: $(OBJS)
$(CC) -o $@ $^ $(LFLAGS)
all: testit
all: test_ipfs
clean:
rm -f *.o
rm -f testit
rm -f test_ipfs

View file

@ -6,10 +6,49 @@
#include "ipfs/cmd/ipfs/init.h"
#include "ipfs/commands/argument.h"
#include "ipfs/commands/request.h"
#include "ipfs/commands/command.h"
#include <stdio.h>
//#include <string.h>
int test_init_new_installation() {
// do the minimum to get the .ipfs directory structure and config file built
struct Request request;
int retVal = ipfs_cmd_ipfs_init_command_new( &request.cmd );
if (retVal == 0)
return 0;
// build a request so it builds the repository in the /tmp directory
request.invoc_context = (struct Context*)malloc(sizeof(struct Context));
request.invoc_context->config_root = "/tmp/.ipfs";
// run the methods
retVal = request.cmd.pre_run(&request);
if (retVal == 0)
return 0;
retVal = request.cmd.run(&request);
if (retVal == 0)
return 0;
retVal = request.cmd.post_run(&request);
if (retVal == 0)
return 0;
// clean up
ipfs_cmd_ipfs_init_command_free( &request.cmd );
// make sure the repository exists
retVal = os_utils_file_exists("/tmp/.ipfs/config");
return retVal;
}
/***
* This is used for the command line interpreter, which is still in development
*/
int test_get_init_command() {
struct Command cmd = { 0 };
int retVal = 1;
@ -19,7 +58,7 @@ int test_get_init_command() {
return 0;
}
// grab the stuff
retVal = get_init_command(&cmd);
retVal = ipfs_cmd_ipfs_init_command_new(&cmd);
if (!retVal) {
fprintf(stderr, "Function call to get_init_command not successful. Return was %d\n", retVal);
@ -42,7 +81,7 @@ int test_get_init_command() {
retVal = 0;
}
}
uninit_command(&cmd);
ipfs_cmd_ipfs_init_command_free(&cmd);
return retVal;
}

View file

@ -14,6 +14,7 @@ int testit(const char* name, int (*func)(void)) {
}
int main(int argc, char** argv) {
testit("test_init_new_installation", test_init_new_installation);
testit("test_repo_config_init", test_repo_config_init);
testit("test_repo_config_write", test_repo_config_write);
testit("test_repo_config_identity_new", test_repo_config_identity_new);

View file

@ -2,7 +2,7 @@ CC = gcc
CFLAGS = -O0 -I../../include -I../../../c-libp2p/include
LFLAGS =
DEPS =
OBJS = ipfsaddr.o
OBJS = ipfs_addr.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)