From 80036b5bd9a15a25e1e3831563f05ba9103e0fcd Mon Sep 17 00:00:00 2001 From: josch Date: Mon, 29 Dec 2014 13:57:53 +0100 Subject: [PATCH] move loop polling for upload completion into separate function --- fuse/filecache.c | 26 ++++++-------------------- fuse/operations.c | 26 ++++++-------------------- mfapi/apicalls.c | 2 +- mfapi/apicalls.h | 5 +++-- mfapi/mfconn.c | 27 +++++++++++++++++++++++++++ mfapi/mfconn.h | 3 +++ mfshell/commands/put.c | 26 +++++++------------------- utils/http.c | 2 +- 8 files changed, 54 insertions(+), 63 deletions(-) diff --git a/fuse/filecache.c b/fuse/filecache.c index 7e7e1fc..239e041 100644 --- a/fuse/filecache.c +++ b/fuse/filecache.c @@ -69,8 +69,6 @@ int filecache_upload_patch(const char *quickkey, uint64_t local_revision, char *patch_file; int retval; char *upload_key; - int status; - int fileerror; cachefile = strdup_printf("%s/%s_%d", filecache_path, quickkey, local_revision); @@ -151,26 +149,14 @@ int filecache_upload_patch(const char *quickkey, uint64_t local_revision, return -1; } // poll for completion - for (;;) { - // no need to update the secret key after this - retval = mfconn_api_upload_poll_upload(conn, upload_key, - &status, &fileerror); - if (retval != 0) { - fprintf(stderr, "mfconn_api_upload_poll_upload failed\n"); - return -1; - } - fprintf(stderr, "status: %d, filerror: %d\n", status, fileerror); - - // values 98 and 99 are terminal states for a completed upload - if (status == 99 || status == 98) { - fprintf(stderr, "done\n"); - break; - } - sleep(1); - } - + retval = mfconn_upload_poll_for_completion(conn, upload_key); free(upload_key); + if (retval != 0) { + fprintf(stderr, "mfconn_upload_poll_for_completion failed\n"); + return -1; + } + return 0; } diff --git a/fuse/operations.c b/fuse/operations.c index a3f8f81..69b263e 100644 --- a/fuse/operations.c +++ b/fuse/operations.c @@ -450,8 +450,6 @@ int mediafirefs_release(const char *path, struct fuse_file_info *file_info) int retval; struct mediafirefs_context_private *ctx; struct mediafirefs_openfile *openfile; - int status; - int fileerror; ctx = fuse_get_context()->private_data; @@ -511,26 +509,14 @@ int mediafirefs_release(const char *path, struct fuse_file_info *file_info) return -EACCES; } // poll for completion - for (;;) { - // no need to update the secret key after this - retval = mfconn_api_upload_poll_upload(ctx->conn, upload_key, - &status, &fileerror); - if (retval != 0) { - fprintf(stderr, "mfconn_api_upload_poll_upload failed\n"); - return -1; - } - fprintf(stderr, "status: %d, filerror: %d\n", status, fileerror); - - // values 98 and 99 are terminal states for a completed upload - if (status == 99 || status == 98) { - fprintf(stderr, "done\n"); - break; - } - sleep(1); - } - + retval = mfconn_upload_poll_for_completion(ctx->conn, upload_key); free(upload_key); + if (retval != 0) { + fprintf(stderr, "mfconn_upload_poll_for_completion failed\n"); + return -1; + } + folder_tree_update(ctx->tree, ctx->conn, true); return 0; } diff --git a/mfapi/apicalls.c b/mfapi/apicalls.c index c0f4d95..6cbad54 100644 --- a/mfapi/apicalls.c +++ b/mfapi/apicalls.c @@ -22,7 +22,7 @@ #include "../utils/http.h" -const char *mfconn_file_link_types[] = { +const char *mfconn_file_link_types[] = { "normal_download", "direct_download", "view", diff --git a/mfapi/apicalls.h b/mfapi/apicalls.h index 23893df..6ffc1db 100644 --- a/mfapi/apicalls.h +++ b/mfapi/apicalls.h @@ -54,7 +54,7 @@ enum mfconn_file_link_type { MFCONN_FILE_LINK_TYPE_ONE_TIME_DOWNLOAD }; -extern const char *mfconn_file_link_types[]; // declared in apicalls.c +extern const char *mfconn_file_link_types[]; // declared in apicalls.c struct mfconn_device_change { enum mfconn_device_change_type change; @@ -72,7 +72,8 @@ int mfconn_api_file_get_info(mfconn * conn, mffile * file, int mfconn_api_file_get_links(mfconn * conn, mffile * file, const char *quickkey, - enum mfconn_file_link_type link_mask); + enum mfconn_file_link_type + link_mask); int mfconn_api_file_move(mfconn * conn, const char *quickkey, const char *folderkey); diff --git a/mfapi/mfconn.c b/mfapi/mfconn.c index 51fab84..40d13f2 100644 --- a/mfapi/mfconn.c +++ b/mfapi/mfconn.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "../utils/strings.h" #include "apicalls.h" @@ -402,3 +403,29 @@ int mfconn_get_max_num_retries(mfconn * conn) { return conn->max_num_retries; } + +int mfconn_upload_poll_for_completion(mfconn * conn, const char *upload_key) +{ + int status; + int fileerror; + int retval; + + for (;;) { + // no need to update the secret key after this + retval = mfconn_api_upload_poll_upload(conn, upload_key, &status, + &fileerror); + if (retval != 0) { + fprintf(stderr, "mfconn_api_upload_poll_upload failed\n"); + return -1; + } + fprintf(stderr, "status: %d, filerror: %d\n", status, fileerror); + + // values 98 and 99 are terminal states for a completed upload + if (status == 99 || status == 98) { + fprintf(stderr, "done\n"); + break; + } + sleep(1); + } + return 0; +} diff --git a/mfapi/mfconn.h b/mfapi/mfconn.h index 93788f1..21aceaa 100644 --- a/mfapi/mfconn.h +++ b/mfapi/mfconn.h @@ -59,4 +59,7 @@ uint32_t mfconn_get_secret_key(mfconn * conn); int mfconn_get_max_num_retries(mfconn * conn); +int mfconn_upload_poll_for_completion(mfconn * conn, + const char *upload_key); + #endif diff --git a/mfshell/commands/put.c b/mfshell/commands/put.c index ea303ec..5919f78 100644 --- a/mfshell/commands/put.c +++ b/mfshell/commands/put.c @@ -21,10 +21,10 @@ #include #include #include -#include #include #include "../../mfapi/apicalls.h" +#include "../../mfapi/mfconn.h" #include "../mfshell.h" #include "../../mfapi/folder.h" #include "../commands.h" // IWYU pragma: keep @@ -36,8 +36,6 @@ int mfshell_cmd_put(mfshell * mfshell, int argc, char *const argv[]) char *temp; char *file_name; char *upload_key; - int status; - int fileerror; FILE *fh; if (mfshell == NULL) @@ -83,23 +81,13 @@ int mfshell_cmd_put(mfshell * mfshell, int argc, char *const argv[]) fprintf(stderr, "upload_key: %s\n", upload_key); // poll for completion - for (;;) { - // no need to update the secret key after this - retval = mfconn_api_upload_poll_upload(mfshell->conn, upload_key, - &status, &fileerror); - if (retval != 0) { - fprintf(stderr, "mfconn_api_upload_poll_upload failed\n"); - return -1; - } - fprintf(stderr, "status: %d, filerror: %d\n", status, fileerror); - if (status == 99) { - fprintf(stderr, "done\n"); - break; - } - sleep(1); - } - + retval = mfconn_upload_poll_for_completion(mfshell->conn, upload_key); free(upload_key); + if (retval != 0) { + fprintf(stderr, "mfconn_upload_poll_for_completion failed\n"); + return -1; + } + return 0; } diff --git a/utils/http.c b/utils/http.c index e0b3df4..4b02391 100644 --- a/utils/http.c +++ b/utils/http.c @@ -72,7 +72,7 @@ static void http_curl_reset(mfhttp * conn) curl_easy_setopt(conn->curl_handle, CURLOPT_VERBOSE, 0L); // it should never take 5 seconds to establish a connection to the server - curl_easy_setopt(conn->curl_handle, CURLOPT_CONNECTTIMEOUT_MS,5000L); + curl_easy_setopt(conn->curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 5000L); //curl_easy_setopt(conn->curl_handle, CURLOPT_SSL_VERIFYPEER, 0L); }