diff --git a/cmd/cli.c b/cmd/cli.c index 4d9692d..b520f18 100644 --- a/cmd/cli.c +++ b/cmd/cli.c @@ -1,11 +1,44 @@ #include +#include #include "ipfs/cmd/cli.h" +int cli_is_switch(int argc, char** argv, int index) { + char* to_test = argv[index]; + if (to_test[0] == '-') { + if (strcmp(to_test, "-c") == 0 || strcmp(to_test, "--config") == 0) { + return 2; + } + return 1; + } + return 0; +} + + int cli_get_verb_index(struct CliArguments* args) { + for(int i = 1; i < args->argc; i++) { + int advance_by_more_than_one = cli_is_switch(args->argc, args->argv, i); + if (advance_by_more_than_one == 0) { + // this is the verb + return i; + } else { + if (advance_by_more_than_one == 2) { + // skip the next one + i++; + } + } + } return 0; } char* cli_get_config_dir(struct CliArguments* args) { + for (int i = 1; i < args->argc; i++) { + if (args->argv[i][0] == '-') { + char* param = args->argv[i]; + if ((strcmp(param, "-c") == 0 || strcmp(param, "--config") == 0) && args->argc > i + 1) { + return args->argv[i+1]; + } + } + } return NULL; } diff --git a/core/ipfs_node.c b/core/ipfs_node.c index 390779b..0380e4f 100644 --- a/core/ipfs_node.c +++ b/core/ipfs_node.c @@ -11,6 +11,22 @@ #include "ipfs/exchange/bitswap/bitswap.h" #include "ipfs/journal/journal.h" +struct IpfsNode* ipfs_node_new() { + struct IpfsNode* node = malloc(sizeof(struct IpfsNode)); + if (node != NULL) { + node->blockstore = NULL; + node->exchange = NULL; + node->identity = NULL; + node->mode = MODE_OFFLINE; + node->peerstore = NULL; + node->protocol_handlers = NULL; + node->providerstore = NULL; + node->repo = NULL; + node->routing = NULL; + } + return node; +} + struct Libp2pVector* ipfs_node_online_build_protocol_handlers(struct IpfsNode* node) { struct Libp2pVector* retVal = libp2p_utils_vector_new(1); if (retVal != NULL) { @@ -47,17 +63,11 @@ int ipfs_node_online_protocol_handlers_free(struct Libp2pVector* handlers) { int ipfs_node_online_new(pthread_t *pth_scope, const char* repo_path, struct IpfsNode** node) { struct FSRepo* fs_repo = NULL; - *node = (struct IpfsNode*)malloc(sizeof(struct IpfsNode)); + *node = ipfs_node_new(); if(*node == NULL) return 0; struct IpfsNode* local_node = *node; - local_node->identity = NULL; - local_node->peerstore = NULL; - local_node->providerstore = NULL; - local_node->repo = NULL; - local_node->routing = NULL; - local_node->exchange = NULL; // build the struct if (!ipfs_repo_fsrepo_new(repo_path, NULL, &fs_repo)) { @@ -98,17 +108,11 @@ int ipfs_node_online_new(pthread_t *pth_scope, const char* repo_path, struct Ipf int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node) { struct FSRepo* fs_repo = NULL; - *node = (struct IpfsNode*)malloc(sizeof(struct IpfsNode)); + *node = ipfs_node_new(); if(*node == NULL) return 0; struct IpfsNode* local_node = *node; - local_node->identity = NULL; - local_node->peerstore = NULL; - local_node->providerstore = NULL; - local_node->repo = NULL; - local_node->routing = NULL; - local_node->exchange = NULL; // build the struct if (!ipfs_repo_fsrepo_new(repo_path, NULL, &fs_repo)) { diff --git a/main/main.c b/main/main.c index 81d3505..a08945b 100644 --- a/main/main.c +++ b/main/main.c @@ -38,10 +38,14 @@ #endif // MINGW void stripit(int argc, char** argv) { - char tmp[strlen(argv[argc])]; - strcpy(tmp, &argv[argc][1]); + char* old_arg = argv[argc]; + int full_length = strlen(old_arg); + char *tmp = (char*) malloc(full_length + 1); + char* ptr1 = &old_arg[1]; + strcpy(tmp, ptr1); tmp[strlen(tmp)-1] = 0; - strcpy(argv[argc], tmp); + strcpy(old_arg, tmp); + free(tmp); return; } diff --git a/repo/config/config.c b/repo/config/config.c index a94805e..e3277c2 100644 --- a/repo/config/config.c +++ b/repo/config/config.c @@ -149,8 +149,15 @@ int ipfs_repo_config_init(struct RepoConfig* config, unsigned int num_bits_for_k config->addresses->swarm_head->next = libp2p_utils_linked_list_new(); config->addresses->swarm_head->next->item = malloc(strlen(addr1) + 1); strcpy(config->addresses->swarm_head->next->item, addr1); + + strcpy(addr1, "/ip4/127.0.0.1/tcp/5001"); + config->addresses->api = malloc(strlen(addr1)+1); + strcpy(config->addresses->api, addr1); + strcpy(addr1, "ip4/127.0.0.1/tcp/8080"); + config->addresses->gateway = malloc(strlen(addr1+1)); + strcpy(config->addresses->gateway, addr1); free(addr1); - + config->discovery.mdns.enabled = 1; config->discovery.mdns.interval = 10; diff --git a/repo/fsrepo/lmdb_datastore.c b/repo/fsrepo/lmdb_datastore.c index 12cd604..441a966 100644 --- a/repo/fsrepo/lmdb_datastore.c +++ b/repo/fsrepo/lmdb_datastore.c @@ -341,6 +341,8 @@ int repo_fsrepro_lmdb_open(int argc, char** argv, struct Datastore* datastore) { db_context->db_environment = NULL; return 0; } + mdb_txn_commit(db_context->current_transaction); + db_context->current_transaction = NULL; return 1; } diff --git a/repo/init.c b/repo/init.c index 4f9160e..671ee62 100644 --- a/repo/init.c +++ b/repo/init.c @@ -128,12 +128,7 @@ int ipfs_repo_init(int argc, char** argv) { return 0; } // make the directory -#ifdef __MINGW32__ - if (mkdir(repo_directory) == -1) { -#else - if (mkdir(repo_directory, S_IRWXU) == -1) { -#endif - printf("Unable to create the directory: %s\n", repo_directory); + if (!os_mkdir(repo_directory)) { return 0; } // make the repository