From 81e103f1e01c918a98208250ca2c269c2a114af6 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Wed, 27 Sep 2017 07:02:00 -0500 Subject: [PATCH] Fix for object get when api not running --- importer/exporter.c | 16 ++++++++-------- include/ipfs/routing/routing.h | 1 + main/main.c | 23 ++++++++++++----------- routing/offline.c | 17 +++++++++++++---- routing/online.c | 1 + test/scripts/run_tests.sh | 1 + test/scripts/test_2.sh | 13 ++++++++++++- 7 files changed, 48 insertions(+), 24 deletions(-) diff --git a/importer/exporter.c b/importer/exporter.c index c4b633a..b1f9892 100644 --- a/importer/exporter.c +++ b/importer/exporter.c @@ -31,18 +31,18 @@ int ipfs_exporter_get_node(struct IpfsNode* local_node, const unsigned char* has int retVal = 0; struct KademliaMessage* msg = NULL; - if (local_node->routing->GetValue(local_node->routing, hash, hash_size, (void**)&buffer, &buffer_size)) { - libp2p_logger_debug("exporter", "get_node got a value. Converting it to a HashtableNode\n"); - // unprotobuf - if (!ipfs_hashtable_node_protobuf_decode(buffer, buffer_size, result)) { - libp2p_logger_debug("exporter", "Conversion to HashtableNode not successful\n"); - goto exit; - } - } else { + if (!local_node->routing->GetValue(local_node->routing, hash, hash_size, (void**)&buffer, &buffer_size)) { libp2p_logger_debug("exporter", "get_node got no value. Returning false.\n"); goto exit; } + libp2p_logger_debug("exporter", "get_node got a value. Converting it to a HashtableNode\n"); + // unprotobuf + if (!ipfs_hashtable_node_protobuf_decode(buffer, buffer_size, result)) { + libp2p_logger_debug("exporter", "Conversion to HashtableNode not successful\n"); + goto exit; + } + // copy in the hash (*result)->hash_size = hash_size; (*result)->hash = malloc(hash_size); diff --git a/include/ipfs/routing/routing.h b/include/ipfs/routing/routing.h index fbfc7db..479e6f1 100644 --- a/include/ipfs/routing/routing.h +++ b/include/ipfs/routing/routing.h @@ -30,6 +30,7 @@ struct IpfsRouting { * @param 3 the size of the key * @param 4 a place to store the value * @param 5 the size of the value + * @returns true(1) on success, false(0) otherwise */ int (*GetValue) (struct IpfsRouting*, const unsigned char*, size_t, void**, size_t*); /** diff --git a/main/main.c b/main/main.c index 3c73210..3e416ad 100644 --- a/main/main.c +++ b/main/main.c @@ -150,40 +150,40 @@ int parse_arguments(int argc, char** argv) { * The beginning */ int main(int argc, char** argv) { - + int retVal = 0; strip_quotes(argc, argv); // CliArguments is the new way to do it. Eventually, all will use this structure struct CliArguments* args = cli_arguments_new(argc, argv); if (args != NULL) { // until then, use the old way - int retVal = parse_arguments(argc, argv); - switch (retVal) { + int cmd = parse_arguments(argc, argv); + switch (cmd) { case (INIT): - return ipfs_repo_init(argc, argv); + retVal = ipfs_repo_init(argc, argv); break; case (ADD): - ipfs_import_files(args); + retVal = ipfs_import_files(args); break; case (OBJECT_GET): - ipfs_exporter_object_get(argc, argv); + retVal = ipfs_exporter_object_get(argc, argv); break; case(GET): //ipfs_exporter_get(argc, argv); //break; case (CAT): - ipfs_exporter_object_cat(args); + retVal = ipfs_exporter_object_cat(args); break; case (DNS): - ipfs_dns(argc, argv); + retVal = ipfs_dns(argc, argv); break; case (DAEMON): - ipfs_daemon(argc, argv); + retVal = ipfs_daemon(argc, argv); break; case (PING): - ipfs_ping(argc, argv); + retVal = ipfs_ping(argc, argv); break; case (NAME): - ipfs_name(args); + retVal = ipfs_name(args); break; default: libp2p_logger_error("main", "Invalid command line arguments.\n"); @@ -192,4 +192,5 @@ int main(int argc, char** argv) { cli_arguments_free(args); } libp2p_logger_free(); + exit(retVal == 1 ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/routing/offline.c b/routing/offline.c index e738e35..3ae8ab0 100644 --- a/routing/offline.c +++ b/routing/offline.c @@ -38,11 +38,20 @@ int ipfs_routing_generic_put_value (ipfs_routing* offlineRouting, const unsigned return 0; // success. } +/*** + * Get a value from the merkledag + * @param routing the context + * @param key the key + * @param key_size the size of the key + * @param val where to put the results + * @param vlen the size of the results + * @returns true(1) on success, false(0) otherwise + */ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *key, size_t key_size, void **val, size_t *vlen) { struct HashtableNode* node = NULL; *val = NULL; - int retVal = -1; + int retVal = 0; if (!ipfs_merkledag_get(key, key_size, &node, routing->local_node->repo)) { goto exit; @@ -53,14 +62,14 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char * *val = malloc(protobuf_size); if (ipfs_hashtable_node_protobuf_encode(node, *val, protobuf_size, vlen) == 0) { - goto exit; + goto exit; } - retVal = 0; + retVal = 1; exit: if (node != NULL) ipfs_hashtable_node_free(node); - if (retVal != 0 && *val != NULL) { + if (retVal != 1 && *val != NULL) { free(*val); *val = NULL; } diff --git a/routing/online.c b/routing/online.c index 80cb108..920e138 100644 --- a/routing/online.c +++ b/routing/online.c @@ -363,6 +363,7 @@ int ipfs_routing_online_get_peer_value(ipfs_routing* routing, const struct Libp2 * @param key_size the size of the key * @param buffer where to put the results * @param buffer_size the length of the buffer + * @returns true(1) on success, false(0) otherwise */ int ipfs_routing_online_get_value (ipfs_routing* routing, const unsigned char *key, size_t key_size, void **buffer, size_t *buffer_size) { diff --git a/test/scripts/run_tests.sh b/test/scripts/run_tests.sh index 2e84b8b..16bd111 100755 --- a/test/scripts/run_tests.sh +++ b/test/scripts/run_tests.sh @@ -1,4 +1,5 @@ #!/bin/bash ./run_test.sh test_1.sh +./run_test.sh test_2.sh diff --git a/test/scripts/test_2.sh b/test/scripts/test_2.sh index 67ba9ec..a1d4e6b 100755 --- a/test/scripts/test_2.sh +++ b/test/scripts/test_2.sh @@ -4,8 +4,19 @@ source ./test_helpers.sh IPFS="../../main/ipfs --config /tmp/ipfs_1" +function check_failure() { + FUNC=$1; + RESULT=$2; + if [ $RESULT -eq 0 ]; then + echo ""; + else + echo "Failure in $FUNC. The return value was $RESULT"; + fi +} + function pre { eval "$IPFS" init; + check_failure "pre" $? } function post { @@ -16,7 +27,7 @@ function post { function body { create_hello_world; eval "$IPFS" add hello.txt - eval "$IPFS" object cat Qm123456 + eval "$IPFS" cat QmYAXgX8ARiriupMQsbGXtKdDyGzWry1YV3sycKw1qqmgH }