forked from agorise/c-ipfs
Misc changes to support binary upload
This commit is contained in:
parent
8aa7b7ca77
commit
42aa1646ab
9 changed files with 117 additions and 36 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
// these two for strdup
|
||||
#define _GNU_SOURCE
|
||||
#define __USE_GNU
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -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:
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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
|
||||
|
|
49
test/scripts/test_7.sh
Executable file
49
test/scripts/test_7.sh
Executable file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue