From 42aa1646abfaa31ccc193a49679df216a3a9fb7d Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 4 Oct 2017 09:36:38 -0500 Subject: [PATCH] Misc changes to support binary upload --- core/Makefile | 2 +- core/http_request.c | 11 +++-- importer/Makefile | 2 +- importer/importer.c | 77 +++++++++++++++++++++----------- include/ipfs/core/http_request.h | 5 ++- namesys/name.c | 4 +- routing/offline.c | 2 +- test/scripts/run_tests.sh | 1 + test/scripts/test_7.sh | 49 ++++++++++++++++++++ 9 files changed, 117 insertions(+), 36 deletions(-) create mode 100755 test/scripts/test_7.sh diff --git a/core/Makefile b/core/Makefile index e2ef9a4..3786d50 100644 --- a/core/Makefile +++ b/core/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c99 +CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c11 ifdef DEBUG CFLAGS += -g3 diff --git a/core/http_request.c b/core/http_request.c index e5229d9..5d511bf 100644 --- a/core/http_request.c +++ b/core/http_request.c @@ -471,11 +471,12 @@ int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* * @param local_node the context * @param request the request * @param result the results + * @param result_size the size of the results * @param data the array with post data - * @param size the data length + * @param data_size the data length * @returns true(1) on success, false(0) on error */ -int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* request, char** result, char *data, size_t size) { +int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t* result_size, char *data, size_t data_size) { if (request == NULL || request->command == NULL || data == NULL) return 0; @@ -507,7 +508,7 @@ int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* CURLFORM_PTRCONTENTS, data, CURLFORM_CONTENTTYPE, "application/octet-stream", CURLFORM_FILENAME, "", - CURLFORM_CONTENTSLENGTH, size, + CURLFORM_CONTENTSLENGTH, data_size, CURLFORM_END); @@ -531,8 +532,10 @@ int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (res == CURLE_OK) { - if (strcmp(s.ptr, "404 page not found") != 0) + if (strcmp(s.ptr, "404 page not found") != 0) { *result = s.ptr; + *result_size = s.len; + } else res = -1; } else { diff --git a/importer/Makefile b/importer/Makefile index de55e1f..a749dc7 100644 --- a/importer/Makefile +++ b/importer/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c99 +CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c11 ifdef DEBUG CFLAGS += -g3 diff --git a/importer/importer.c b/importer/importer.c index c14ac33..5e555de 100644 --- a/importer/importer.c +++ b/importer/importer.c @@ -1,3 +1,6 @@ +// these two for strdup +#define _GNU_SOURCE +#define __USE_GNU #include #include #include @@ -8,6 +11,7 @@ #include "libp2p/os/utils.h" #include "ipfs/cmd/cli.h" #include "ipfs/core/ipfs_node.h" +#include "ipfs/core/http_request.h" #include "ipfs/repo/fsrepo/fs_repo.h" #include "ipfs/repo/init.h" #include "ipfs/unixfs/unixfs.h" @@ -386,32 +390,55 @@ int ipfs_import_files(struct CliArguments* args) { } ipfs_node_offline_new(repo_path, &local_node); - - // import the file(s) - current = first; - while (current != NULL) { - if (current->file_name[0] != '-') { // not a switch - os_utils_split_filename(current->file_name, &path, &filename); - size_t bytes_written = 0; - if (!ipfs_import_file(NULL, current->file_name, &directory_entry, local_node, &bytes_written, recursive)) - goto exit; - ipfs_import_print_node_results(directory_entry, filename); - // cleanup - if (path != NULL) { - free(path); - path = NULL; - } - if (filename != NULL) { - free(filename); - filename = NULL; - } - if (directory_entry != NULL) { - ipfs_hashtable_node_free(directory_entry); - directory_entry = NULL; - } + /** disabling for the time being + if (local_node->mode == MODE_API_AVAILABLE) { + // do this through the API + struct HttpRequest* request = ipfs_core_http_request_new(); + request->command = "add"; + struct HttpParam* recursive_param = ipfs_core_http_param_new(); + recursive_param->name = strdup("recursive"); + recursive_param->value = strdup((recursive ? "true" : "false")); + libp2p_utils_vector_add(request->params, recursive_param); + current = first; + while (current != NULL) { + libp2p_utils_vector_add(request->arguments, current->file_name); + current = current->next; } - current = current->next; - } + uint8_t* result = NULL; + size_t result_size = 0; + if (!ipfs_core_http_request_post(local_node, request, &result, &result_size, data, data_size)) { + + } + + } else { + */ + // No daemon is running. Do this without using the API + // import the file(s) + current = first; + while (current != NULL) { + if (current->file_name[0] != '-') { // not a switch + os_utils_split_filename(current->file_name, &path, &filename); + size_t bytes_written = 0; + if (!ipfs_import_file(NULL, current->file_name, &directory_entry, local_node, &bytes_written, recursive)) + goto exit; + ipfs_import_print_node_results(directory_entry, filename); + // cleanup + if (path != NULL) { + free(path); + path = NULL; + } + if (filename != NULL) { + free(filename); + filename = NULL; + } + if (directory_entry != NULL) { + ipfs_hashtable_node_free(directory_entry); + directory_entry = NULL; + } + } + current = current->next; + } + // } uncomment this line when the api is up and running with file transfer retVal = 1; exit: diff --git a/include/ipfs/core/http_request.h b/include/ipfs/core/http_request.h index 6838b27..c9b5b52 100644 --- a/include/ipfs/core/http_request.h +++ b/include/ipfs/core/http_request.h @@ -81,8 +81,9 @@ int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* * @param local_node the context * @param request the request * @param result the results + * @param result_size the size of the results * @param data the array with post data - * @param size the data length + * @param data_size the data length * @returns true(1) on success, false(0) on error */ -int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* request, char** result, char *data, size_t size); +int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t* result_size, char *data, size_t data_size); diff --git a/namesys/name.c b/namesys/name.c index c79bb17..f0f2664 100644 --- a/namesys/name.c +++ b/namesys/name.c @@ -23,7 +23,7 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) { size_t response_size = 0; int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size); if (response != NULL && response_size > 0) { - fprintf(stdout, response); + fwrite(response, 1, response_size, stdout); free(response); } ipfs_core_http_request_free(request); @@ -42,7 +42,7 @@ int ipfs_name_resolve(struct IpfsNode* local_node, char* name) { size_t response_size = 0; int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size); if (response != NULL && response_size > 0) { - fprintf(stdout, response); + fwrite(response, 1, response_size, stdout); free(response); } ipfs_core_http_request_free(request); diff --git a/routing/offline.c b/routing/offline.c index 023ae2d..9355fb5 100644 --- a/routing/offline.c +++ b/routing/offline.c @@ -153,7 +153,7 @@ int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, const unsigned c return 0; } ipfs_core_http_request_free(request); - fprintf(stdout, response); + fwrite(response, 1, response_size, stdout); return 1; } else { libp2p_logger_debug("offline", "Unable to announce that I can provide the hash, as API not available.\n"); diff --git a/test/scripts/run_tests.sh b/test/scripts/run_tests.sh index 631ef69..fd64437 100755 --- a/test/scripts/run_tests.sh +++ b/test/scripts/run_tests.sh @@ -6,3 +6,4 @@ ./run_test.sh test_4.sh ./run_test.sh test_5.sh ./run_test.sh test_6.sh +./run_test.sh test_7.sh diff --git a/test/scripts/test_7.sh b/test/scripts/test_7.sh new file mode 100755 index 0000000..376e6f0 --- /dev/null +++ b/test/scripts/test_7.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +#### +# Attempt to add and retrieve binary file from running daemon +# +#### + +source ./test_helpers.sh + +IPFS="../../main/ipfs --config /tmp/ipfs_1" + +function pre { + rm -Rf /tmp/ipfs_1 + eval "$IPFS" init; + check_failure "pre" $? + cp ../config.test1.wo_journal /tmp/ipfs_1/config +} + +function post { + rm -Rf /tmp/ipfs_1; + rm hello.bin; + rm hello2.bin; +} + +function body { + create_binary_file; + + #start the daemon + eval "../../main/ipfs --config /tmp/ipfs_1 daemon &" + daemon_id=$! + sleep 5 + + # add file + eval "$IPFS" add hello.bin + check_failure "add hello.bin" $? + sleep 5 + + # retrieve file + eval "$IPFS" cat QmX4zpwaE7CSgZZsULgoB3gXYC6hh7RN19bEfWxw7sL8Xx > hello2.bin + check_failure "cat" $? + + # file size should be 256 + actualsize=$(wc -c < hello2.bin) + if [ $actualsize -ne 256 ]; then + echo '*** Failure *** file size incorrect' + fi + + kill -9 $daemon_id +}