Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Return upload state
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Oct 17, 2017
1 parent ef8fc0f commit b9c3c40
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
20 changes: 8 additions & 12 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,26 +414,22 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path)
uv_signal_init(env->loop, sig);
uv_signal_start(sig, upload_signal_handler, SIGINT);

storj_upload_state_t *state = malloc(sizeof(storj_upload_state_t));
if (!state) {
return 1;
}

sig->data = state;

storj_progress_cb progress_cb = (storj_progress_cb)noop;
if (env->log_options->level == 0) {
progress_cb = file_progress;
}

int status = storj_bridge_store_file(env,
state,
&upload_opts,
NULL,
progress_cb,
upload_file_complete);
storj_upload_state_t *state = storj_bridge_store_file(env,
&upload_opts,
NULL,
progress_cb,
upload_file_complete);

return status;
sig->data = state;

return state->error_status;
}

static void download_file_complete(int status, FILE *fd, void *handle)
Expand Down
12 changes: 5 additions & 7 deletions src/storj.h
Original file line number Diff line number Diff line change
Expand Up @@ -947,13 +947,11 @@ STORJ_API int storj_bridge_store_file_cancel(storj_upload_state_t *state);
* @param[in] finished_cb Function called when download finished
* @return A non-zero error value on failure and 0 on success.
*/
STORJ_API int storj_bridge_store_file(storj_env_t *env,
storj_upload_state_t *state,
storj_upload_opts_t *opts,
void *handle,
storj_progress_cb progress_cb,
storj_finished_upload_cb finished_cb);

STORJ_API storj_upload_state_t *storj_bridge_store_file(storj_env_t *env,
storj_upload_opts_t *opts,
void *handle,
storj_progress_cb progress_cb,
storj_finished_upload_cb finished_cb);

/**
* @brief Will cancel a download
Expand Down
19 changes: 14 additions & 5 deletions src/uploader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2641,16 +2641,20 @@ STORJ_API int storj_bridge_store_file_cancel(storj_upload_state_t *state)
return 0;
}

STORJ_API int storj_bridge_store_file(storj_env_t *env,
storj_upload_state_t *state,
STORJ_API storj_upload_state_t *storj_bridge_store_file(storj_env_t *env,
storj_upload_opts_t *opts,
void *handle,
storj_progress_cb progress_cb,
storj_finished_upload_cb finished_cb)
{
if (!opts->fd) {
env->log->error(env->log_options, handle, "Invalid File descriptor");
return 1;
return NULL;
}

storj_upload_state_t *state = malloc(sizeof(storj_upload_state_t));
if (!state) {
return NULL;
}

state->env = env;
Expand Down Expand Up @@ -2722,6 +2726,11 @@ STORJ_API int storj_bridge_store_file(storj_env_t *env,
work->data = state;

state->pending_work_count += 1;
return uv_queue_work(env->loop, (uv_work_t*) work,
prepare_upload_state, begin_work_queue);

int status = uv_queue_work(env->loop, (uv_work_t*) work,
prepare_upload_state, begin_work_queue);
if (status) {
state->error_status = STORJ_QUEUE_ERROR;
}
return state;
}
31 changes: 13 additions & 18 deletions test/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,12 @@ int test_upload()
.rs = true
};

storj_upload_state_t *state = malloc(sizeof(storj_upload_state_t));

int status = storj_bridge_store_file(env,
state,
&upload_opts,
NULL,
check_store_file_progress,
check_store_file);
assert(status == 0);
storj_upload_state_t *state = storj_bridge_store_file(env,
&upload_opts,
NULL,
check_store_file_progress,
check_store_file);
assert(state->error_status == 0);

// run all queued events

Expand Down Expand Up @@ -578,21 +575,19 @@ int test_upload_cancel()
.fd = fopen(file, "r")
};

storj_upload_state_t *state = malloc(sizeof(storj_upload_state_t));

int status = storj_bridge_store_file(env,
state,
&upload_opts,
NULL,
check_store_file_progress,
check_store_file_cancel);
assert(status == 0);
storj_upload_state_t *state = storj_bridge_store_file(env,
&upload_opts,
NULL,
check_store_file_progress,
check_store_file_cancel);
assert(state->error_status == 0);


// process the loop one at a time so that we can do other things while
// the loop is processing, such as cancel the download
int count = 0;
bool more;
int status = 0;
do {
more = uv_run(env->loop, UV_RUN_ONCE);
if (more == false) {
Expand Down

0 comments on commit b9c3c40

Please sign in to comment.