From c0cf331505b60f3d8d093c328604b39a26b75baf Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 19 Dec 2017 11:19:53 -0500 Subject: [PATCH 01/68] Modified the "storj list-buckets" command to include optional bucket name to be entered as part of the existing command. The command "storj list-buckets [bucket-name]" is used to list the bucket ID of corresponding bucket name, if found in the buckets list. Example: If no bucket name specified, then the command lists all available buckets ./storj list-buckets ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2017-12-15T17:23:25.832Z Name: testbucket ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 ID: b20824d412f7a8845379769e Decrypted: true Created: 2017-12-18T18:15:05.270Z Name: testbucket4 Example: If bucket name is specified, then the command lists details specific to the entered bucket name ./storj list-buckets testbucket ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2017-12-15T17:23:25.832Z Name: testbucket Example: If bucket name is specified, but doesnt exists, then the command return error ./storj list-buckets testbucketXYZ Invalid bucket name. --- src/cli.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/cli.c b/src/cli.c index 0e14903..4e6b4da 100644 --- a/src/cli.c +++ b/src/cli.c @@ -900,11 +900,35 @@ static void get_buckets_callback(uv_work_t *work_req, int status) printf("No buckets.\n"); } - for (int i = 0; i < req->total_buckets; i++) { + for (int i = 0; i < req->total_buckets; i++) + { storj_bucket_meta_t *bucket = &req->buckets[i]; - printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", - bucket->id, bucket->decrypted ? "true" : "false", - bucket->created, bucket->name); + //printf("KSA:[%s] req->handle = %s & bucket->name = %s \n",__FUNCTION__, req->handle, bucket->name); + + if (req->handle != NULL) + { + int ret = strcmp(req->handle, bucket->name); + if (ret == 0x00) + { + printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", + bucket->id, bucket->decrypted ? "true" : "false", + bucket->created, bucket->name); + break; + } + else + { + if (i >= (req->total_buckets -1)) + { + printf("Invalid bucket name. \n"); + } + } + } + else + { + printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", + bucket->id, bucket->decrypted ? "true" : "false", + bucket->created, bucket->name); + } } storj_free_get_buckets_request(req); @@ -1439,9 +1463,24 @@ int main(int argc, char **argv) storj_bridge_delete_file(env, bucket_id, file_id, NULL, delete_file_callback); - } else if (strcmp(command, "list-buckets") == 0) { - storj_bridge_get_buckets(env, NULL, get_buckets_callback); - } else if (strcmp(command, "list-mirrors") == 0) { + } + else if (strcmp(command, "list-buckets") == 0) + { + char *bucket_name = argv[command_index + 1]; + char *handle = NULL; + if (bucket_name != NULL) + { + handle = (void *)bucket_name; + //printf("KSA:: # of argc = %d , arg[0] = %s, arg[] = %s, command index = %d\n", argc, argv[0], argv[command_index + 1], command_index); + } + else + { + handle = NULL; + //printf("KSA:: listing all bucket "); + } + storj_bridge_get_buckets(env, handle, get_buckets_callback); + } + else if (strcmp(command, "list-mirrors") == 0) { char *bucket_id = argv[command_index + 1]; char *file_id = argv[command_index + 2]; From dea86f4b1741eade6ef7242275a52f3c56dffd6f Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 20 Dec 2017 11:59:58 -0500 Subject: [PATCH 02/68] Added internal command "get-bucket-id", similar to list-buckets [bucket_name] --- src/cli.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 136 insertions(+), 16 deletions(-) diff --git a/src/cli.c b/src/cli.c index 4e6b4da..54a84eb 100644 --- a/src/cli.c +++ b/src/cli.c @@ -26,6 +26,17 @@ typedef struct { char *key; } user_options_t; +typedef struct { + storj_env_t *env; + char *bucket_name; + char *bucket_id; + char *file_name; + char *file_id; + char *cmd_req; /**< cli command requested */ + bool cmd_resp; /**< cli command response 0->fail; 1->success */ + void *handle; +}cli_state_t; + #ifndef errno extern int errno; #endif @@ -68,7 +79,7 @@ static inline void noop() {}; " STORJ_ENCRYPTION_KEY file encryption key\n\n" -#define CLI_VERSION "libstorj-2.0.0-beta" +#define CLI_VERSION "libstorj-2.0.1-beta" static void json_logger(const char *message, int level, void *handle) { @@ -900,24 +911,75 @@ static void get_buckets_callback(uv_work_t *work_req, int status) printf("No buckets.\n"); } - for (int i = 0; i < req->total_buckets; i++) + for (int i = 0; i < req->total_buckets; i++) { storj_bucket_meta_t *bucket = &req->buckets[i]; + cli_state_t *cli_state = req->handle; //printf("KSA:[%s] req->handle = %s & bucket->name = %s \n",__FUNCTION__, req->handle, bucket->name); - if (req->handle != NULL) + if (cli_state->bucket_name != NULL) { - int ret = strcmp(req->handle, bucket->name); + int ret = strcmp(cli_state->bucket_name, bucket->name); if (ret == 0x00) { printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", bucket->id, bucket->decrypted ? "true" : "false", bucket->created, bucket->name); + cli_state->bucket_id = bucket->id; break; } else { - if (i >= (req->total_buckets -1)) + if (i >= (req->total_buckets -1)) + { + printf("Invalid bucket name. \n"); + } + } + } + else + { + printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", + bucket->id, bucket->decrypted ? "true" : "false", + bucket->created, bucket->name); + } + } + + storj_free_get_buckets_request(req); + free(work_req); +} + +static void get_bucket_id_callback(uv_work_t *work_req, int status) +{ + assert(status == 0); + get_buckets_request_t *req = work_req->data; + + if (req->status_code == 401) { + printf("Invalid user credentials.\n"); + } else if (req->status_code != 200 && req->status_code != 304) { + printf("Request failed with status code: %i\n", req->status_code); + } else if (req->total_buckets == 0) { + printf("No buckets.\n"); + } + + for (int i = 0; i < req->total_buckets; i++) + { + storj_bucket_meta_t *bucket = &req->buckets[i]; + cli_state_t *cli_state = req->handle; + + if (cli_state->bucket_name != NULL) + { + int ret = strcmp(cli_state->bucket_name, bucket->name); + if (ret == 0x00) + { + printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", + bucket->id, bucket->decrypted ? "true" : "false", + bucket->created, bucket->name); + cli_state->bucket_id = bucket->id; + break; + } + else + { + if (i >= (req->total_buckets -1)) { printf("Invalid bucket name. \n"); } @@ -1388,6 +1450,15 @@ int main(int argc, char **argv) goto end_program; } + cli_state_t *cli_state = malloc(sizeof(cli_state_t)); + if (!cli_state) { + status = 1; + goto end_program; + } + memset(cli_state, 0x00, sizeof(cli_state_t)); + + cli_state->env = env; + if (strcmp(command, "download-file") == 0) { char *bucket_id = argv[command_index + 1]; char *file_id = argv[command_index + 2]; @@ -1417,16 +1488,47 @@ int main(int argc, char **argv) status = 1; goto end_program; } - } else if (strcmp(command, "list-files") == 0) { - char *bucket_id = argv[command_index + 1]; + } + else if (strcmp(command, "list-files") == 0) + { + char *bucket_id = NULL; - if (!bucket_id) { + /* get the corresponding bucket id from the bucket name */ + char *bucket_name = argv[command_index + 1]; + + printf("KSA:[%s]: # of argc = %d , arg[0] = %s, arg[] = %s, command index = %d\n", __FUNCTION__, argc, argv[0], argv[command_index + 1], command_index); + if (!bucket_name) + { printf("Missing first argument: \n"); status = 1; goto end_program; } + else + { + cli_state->bucket_name = bucket_name; + printf("KSA:[%s] cli_state->bucket-name = %s\n", __FUNCTION__, cli_state->bucket_name); + printf("KSA:[%s] cli_state->bucket_id = %s\n", __FUNCTION__, cli_state->bucket_id); + if(!cli_state->bucket_id) + { + printf("KSA:[%s] current cli_state->bucket_id = %s ... Getting bucket id \n", __FUNCTION__, cli_state->bucket_id); + //storj_bridge_get_buckets(env, cli_state->bucket_name, get_bucket_name_callback); + + if (!cli_state->bucket_id) + { - storj_bridge_list_files(env, bucket_id, NULL, list_files_callback); + } + } + } + #if 0 + else + { + /* pass the bucket id to the below functio */ + storj_bridge_get_buckets(env, bucket_name, get_bucket_name_callback); + } + printf("KSA:[%s] bucket_id = %s\n", g_cli_info.p_bucket_id); + //storj_bridge_list_files(env, g_cli_info.p_bucket_id, NULL, list_files_callback); + //free(g_cli_info.p_bucket_id); + #endif } else if (strcmp(command, "add-bucket") == 0) { char *bucket_name = argv[command_index + 1]; @@ -1463,22 +1565,40 @@ int main(int argc, char **argv) storj_bridge_delete_file(env, bucket_id, file_id, NULL, delete_file_callback); - } - else if (strcmp(command, "list-buckets") == 0) + } + else if (strcmp(command, "list-buckets") == 0) { char *bucket_name = argv[command_index + 1]; - char *handle = NULL; - if (bucket_name != NULL) + if (bucket_name != NULL) { - handle = (void *)bucket_name; + cli_state->bucket_name = (void *)bucket_name; //printf("KSA:: # of argc = %d , arg[0] = %s, arg[] = %s, command index = %d\n", argc, argv[0], argv[command_index + 1], command_index); } else { - handle = NULL; + cli_state->bucket_name = NULL; //printf("KSA:: listing all bucket "); } - storj_bridge_get_buckets(env, handle, get_buckets_callback); + /* when callback returns, we store the bucket id of bucket name else null */ + storj_bridge_get_buckets(env, cli_state, get_buckets_callback); + } + else if (strcmp(command, "get-bucket-id") == 0) + { + char *bucket_name = argv[command_index + 1]; + if (!bucket_name) + { + printf("Missing first argument: \n"); + status = 1; + goto end_program; + } + else + { + cli_state->bucket_name = (void *)bucket_name; + cli_state->cmd_req = command; + printf("cli_cmd=%s\n", cli_state->cmd_req); + } + /* when callback returns, we store the bucket id of bucket name else null */ + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } else if (strcmp(command, "list-mirrors") == 0) { char *bucket_id = argv[command_index + 1]; From 6cbd1968b9b70b689b888fd6841d3d632d331384 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 20 Dec 2017 13:48:14 -0500 Subject: [PATCH 03/68] list-files with debug checkin --- src/cli.c | 67 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/cli.c b/src/cli.c index 54a84eb..4ca0450 100644 --- a/src/cli.c +++ b/src/cli.c @@ -32,7 +32,8 @@ typedef struct { char *bucket_id; char *file_name; char *file_id; - char *cmd_req; /**< cli command requested */ + char *curr_cmd_req; /**< cli curr command requested */ + char *next_cmd_req; /**< cli next command requested */ bool cmd_resp; /**< cli command response 0->fail; 1->success */ void *handle; }cli_state_t; @@ -41,6 +42,7 @@ typedef struct { extern int errno; #endif +static void queue_next_cli_cmd(cli_state_t *cli_state); static inline void noop() {}; #define HELP_TEXT "usage: storj [] []\n\n" \ @@ -917,15 +919,15 @@ static void get_buckets_callback(uv_work_t *work_req, int status) cli_state_t *cli_state = req->handle; //printf("KSA:[%s] req->handle = %s & bucket->name = %s \n",__FUNCTION__, req->handle, bucket->name); - if (cli_state->bucket_name != NULL) + if (cli_state->bucket_name != NULL) { - int ret = strcmp(cli_state->bucket_name, bucket->name); + int ret = strcmp(cli_state->bucket_name, bucket->name); if (ret == 0x00) { printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", bucket->id, bucket->decrypted ? "true" : "false", bucket->created, bucket->name); - cli_state->bucket_id = bucket->id; + cli_state->bucket_id = (char *)bucket->id; break; } else @@ -952,6 +954,7 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) { assert(status == 0); get_buckets_request_t *req = work_req->data; + cli_state_t *cli_state = req->handle; if (req->status_code == 401) { printf("Invalid user credentials.\n"); @@ -964,18 +967,19 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) for (int i = 0; i < req->total_buckets; i++) { storj_bucket_meta_t *bucket = &req->buckets[i]; - cli_state_t *cli_state = req->handle; + cli_state->next_cmd_req = NULL; - if (cli_state->bucket_name != NULL) + if (cli_state->bucket_name != NULL) { - int ret = strcmp(cli_state->bucket_name, bucket->name); + int ret = strcmp(cli_state->bucket_name, bucket->name); if (ret == 0x00) { printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", bucket->id, bucket->decrypted ? "true" : "false", bucket->created, bucket->name); - cli_state->bucket_id = bucket->id; - break; + cli_state->bucket_id = (char *)bucket->id; + cli_state->next_cmd_req = "list-files-1"; + break; } else { @@ -993,6 +997,12 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) } } + /* set the next command to execute, handled in queue_next_cli_cmd() */ + if ((cli_state->next_cmd_req != NULL) && (strcmp(cli_state->curr_cmd_req, "list-files") == 0x00)) + { + queue_next_cli_cmd(cli_state); + } + storj_free_get_buckets_request(req); free(work_req); } @@ -1505,30 +1515,16 @@ int main(int argc, char **argv) } else { + cli_state->curr_cmd_req = command; cli_state->bucket_name = bucket_name; printf("KSA:[%s] cli_state->bucket-name = %s\n", __FUNCTION__, cli_state->bucket_name); printf("KSA:[%s] cli_state->bucket_id = %s\n", __FUNCTION__, cli_state->bucket_id); if(!cli_state->bucket_id) { printf("KSA:[%s] current cli_state->bucket_id = %s ... Getting bucket id \n", __FUNCTION__, cli_state->bucket_id); - //storj_bridge_get_buckets(env, cli_state->bucket_name, get_bucket_name_callback); - - if (!cli_state->bucket_id) - { - - } + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } } - #if 0 - else - { - /* pass the bucket id to the below functio */ - storj_bridge_get_buckets(env, bucket_name, get_bucket_name_callback); - } - printf("KSA:[%s] bucket_id = %s\n", g_cli_info.p_bucket_id); - //storj_bridge_list_files(env, g_cli_info.p_bucket_id, NULL, list_files_callback); - //free(g_cli_info.p_bucket_id); - #endif } else if (strcmp(command, "add-bucket") == 0) { char *bucket_name = argv[command_index + 1]; @@ -1579,7 +1575,7 @@ int main(int argc, char **argv) cli_state->bucket_name = NULL; //printf("KSA:: listing all bucket "); } - /* when callback returns, we store the bucket id of bucket name else null */ + /* when callback returns, we store the bucket id of bucket name else null */ storj_bridge_get_buckets(env, cli_state, get_buckets_callback); } else if (strcmp(command, "get-bucket-id") == 0) @@ -1594,10 +1590,9 @@ int main(int argc, char **argv) else { cli_state->bucket_name = (void *)bucket_name; - cli_state->cmd_req = command; - printf("cli_cmd=%s\n", cli_state->cmd_req); + cli_state->curr_cmd_req = command; } - /* when callback returns, we store the bucket id of bucket name else null */ + /* when callback returns, we store the bucket id of bucket name else null */ storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } else if (strcmp(command, "list-mirrors") == 0) { @@ -1617,7 +1612,6 @@ int main(int argc, char **argv) status = 1; goto end_program; } - } // run all queued events @@ -1646,3 +1640,16 @@ int main(int argc, char **argv) } return status; } + +/* cli cmd queue processing function */ +static void queue_next_cli_cmd(cli_state_t *cli_state) +{ + void *handle = cli_state->handle; + + if ((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00) && + (strcmp("list-files-1", cli_state->next_cmd_req) == 0x00)) + { + printf("KSA->[%s]<- cli_state->curr_cmd_req = %s; cli_state->next_cmd_req = %s \n",__FUNCTION__, cli_state->curr_cmd_req, cli_state->next_cmd_req); + storj_bridge_list_files(cli_state->env, cli_state->bucket_id, NULL, list_files_callback); + } +} From 67d2181a3ff6c181fe2fdb008b2540c61f5600d1 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 20 Dec 2017 13:58:36 -0500 Subject: [PATCH 04/68] Checking to support "storj list-files [bucket_name]" Test Case #1: No bucket name given kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files Missing first argument: Test Case #2: Complete command with valid bucket name with 2 files in it kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files testbucket ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2017-12-15T17:23:25.832Z Name: testbucket ID: 27607c99600540cc4c57953c Size: 9 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-15T17:27:25.461Z Name: testfile.txt ID: ee2fcc89dfe71c932cf44fcc Size: 10 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-15T17:29:38.994Z Name: testfile2.txt Test Case #3: Complete command with valid bucket name but no files in it kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files testbucket2 ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 No files for bucket. Test Case #4: Compete command with invalid bucket name kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files testbucket2233432 Invalid bucket name. --- src/cli.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/cli.c b/src/cli.c index 4ca0450..a0c0e69 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1506,7 +1506,6 @@ int main(int argc, char **argv) /* get the corresponding bucket id from the bucket name */ char *bucket_name = argv[command_index + 1]; - printf("KSA:[%s]: # of argc = %d , arg[0] = %s, arg[] = %s, command index = %d\n", __FUNCTION__, argc, argv[0], argv[command_index + 1], command_index); if (!bucket_name) { printf("Missing first argument: \n"); @@ -1517,11 +1516,8 @@ int main(int argc, char **argv) { cli_state->curr_cmd_req = command; cli_state->bucket_name = bucket_name; - printf("KSA:[%s] cli_state->bucket-name = %s\n", __FUNCTION__, cli_state->bucket_name); - printf("KSA:[%s] cli_state->bucket_id = %s\n", __FUNCTION__, cli_state->bucket_id); if(!cli_state->bucket_id) { - printf("KSA:[%s] current cli_state->bucket_id = %s ... Getting bucket id \n", __FUNCTION__, cli_state->bucket_id); storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } } @@ -1568,12 +1564,10 @@ int main(int argc, char **argv) if (bucket_name != NULL) { cli_state->bucket_name = (void *)bucket_name; - //printf("KSA:: # of argc = %d , arg[0] = %s, arg[] = %s, command index = %d\n", argc, argv[0], argv[command_index + 1], command_index); } else { cli_state->bucket_name = NULL; - //printf("KSA:: listing all bucket "); } /* when callback returns, we store the bucket id of bucket name else null */ storj_bridge_get_buckets(env, cli_state, get_buckets_callback); @@ -1649,7 +1643,6 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) if ((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00) && (strcmp("list-files-1", cli_state->next_cmd_req) == 0x00)) { - printf("KSA->[%s]<- cli_state->curr_cmd_req = %s; cli_state->next_cmd_req = %s \n",__FUNCTION__, cli_state->curr_cmd_req, cli_state->next_cmd_req); storj_bridge_list_files(cli_state->env, cli_state->bucket_id, NULL, list_files_callback); } } From 1765a2ab4c7bb271d27b7f8a43daa08bc3b68cbd Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 20 Dec 2017 14:46:39 -0500 Subject: [PATCH 05/68] Indentation changes and Code clean up Checking in supports "storj list-files [bucket_name]" Test Case #1: No bucket name given kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files Missing first argument: Test Case #2: Complete command with valid bucket name with 2 files in it kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files testbucket ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2017-12-15T17:23:25.832Z Name: testbucket ID: 27607c99600540cc4c57953c Size: 9 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-15T17:27:25.461Z Name: testfile.txt ID: ee2fcc89dfe71c932cf44fcc Size: 10 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-15T17:29:38.994Z Name: testfile2.txt Test Case #3: Complete command with valid bucket name but no files in it kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files testbucket2 ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 No files for bucket. Test Case #4: Compete command with invalid bucket name kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj -d list-files testbucket2233432 Invalid bucket name. on branches master branches master --- src/cli.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cli.c b/src/cli.c index a0c0e69..0334331 100644 --- a/src/cli.c +++ b/src/cli.c @@ -917,7 +917,6 @@ static void get_buckets_callback(uv_work_t *work_req, int status) { storj_bucket_meta_t *bucket = &req->buckets[i]; cli_state_t *cli_state = req->handle; - //printf("KSA:[%s] req->handle = %s & bucket->name = %s \n",__FUNCTION__, req->handle, bucket->name); if (cli_state->bucket_name != NULL) { @@ -1643,6 +1642,6 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) if ((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00) && (strcmp("list-files-1", cli_state->next_cmd_req) == 0x00)) { - storj_bridge_list_files(cli_state->env, cli_state->bucket_id, NULL, list_files_callback); + storj_bridge_list_files(cli_state->env, cli_state->bucket_id, NULL, list_files_callback); } } From 22dd1b18e15a347f73fb1b71895854c0029a459e Mon Sep 17 00:00:00 2001 From: Kishore Date: Fri, 22 Dec 2017 09:13:19 -0500 Subject: [PATCH 06/68] Added the ./stroj upload-file ========================================================================================================== Testcase #1: No aruguments entered kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj upload-file Missing arguments: ======================================================================================================== LIST ALLTHE VALID BUCKETS NAMES kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj list-buckets ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2017-12-15T17:23:25.832Z Name: testbucket ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 ID: b20824d412f7a8845379769e Decrypted: true Created: 2017-12-18T18:15:05.270Z Name: testbucket4 ========================================================================================================= PICK A BUCKET AND LIST ALL THE FILES IN THAT BUCKET kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj list-files testbucket2 ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 ID: 4022eed501eb8d008ce61b84 Size: 22 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-21T23:39:17.352Z Name: testfileA.txt ========================================================================================================== TEST CASE#2: Execute the complete upload-file command kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj upload-file testbucket2 testjson.json ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 [======================================================================] 100.00% Upload Success! File ID: 037431e2b0489b41db7a363d ========================================================================================================== LIST THE FILES IN THE ABOVE UPLOADED BUCKET TO VERIFY THAT THE NEW FILES IS LISTED AS WELL kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj list-files testbucket2 ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 ID: 4022eed501eb8d008ce61b84 Size: 22 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-21T23:39:17.352Z Name: testfileA.txt ID: 037431e2b0489b41db7a363d Size: 1448 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-22T13:56:16.628Z Name: testjson.json ========================================================================================================== TEST CASE#3: VALID BUCKET NAME AND INVALID FILE-NAME kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj upload-file testbucket2 testjson.jsondsf ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 Invalid file path: testjson.jsondsf ========================================================================================================== TEST CASE#4: INVALID BUCKET NAME AND INVALID FILE-NAME kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj upload-file testbucket2sfs testjson.jsondsf Invalid bucket name. ========================================================================================================== TEST CASE#4: INVALID BUCKET NAME AND VALID FILE-NAME kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj upload-file testbucket3sfs testjson.json Invalid bucket name. ========================================================================================================== TEST CASE#5: VALID BUCKET NAME AND VALID FILE-NAME kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj upload-file testbucket3 testjson.json ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 [======================================================================] 100.00% Upload Success! File ID: f10d046334eea2e7a6ba9723 ========================================================================================================== TEST CASE#6: TRY AGAIN TO UPLOAD THE SAME FILE WITH VALID BUCKET NAME AND VALID FILE-NAME kishore@kishore-Inspiron-13-7353:~/libstorj/src$ ./storj upload-file testbucket3 testjson.json ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 Preparing File... Upload failure: File already exists ========================================================================================================== --- src/cli.c | 65 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/src/cli.c b/src/cli.c index 0334331..92ba62c 100644 --- a/src/cli.c +++ b/src/cli.c @@ -31,6 +31,7 @@ typedef struct { char *bucket_name; char *bucket_id; char *file_name; + char *file_path; char *file_id; char *curr_cmd_req; /**< cli curr command requested */ char *next_cmd_req; /**< cli next command requested */ @@ -396,6 +397,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path) if (!fd) { printf("Invalid file path: %s\n", file_path); + return 1; } const char *file_name = get_filename_separator(file_path); @@ -977,8 +979,23 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) bucket->id, bucket->decrypted ? "true" : "false", bucket->created, bucket->name); cli_state->bucket_id = (char *)bucket->id; - cli_state->next_cmd_req = "list-files-1"; - break; + + if((ret = strcmp(cli_state->curr_cmd_req, "list-files")) == 0x00) + { + cli_state->next_cmd_req = "list-files-1"; + status = 1; + } + else if((ret = strcmp(cli_state->curr_cmd_req, "upload-file")) == 0x00) + { + cli_state->next_cmd_req = "upload-file-1"; + status = 1; + } + else + { + printf("Invalid curr cmd req = %s\n", cli_state->curr_cmd_req); + status = 0; + } + break; } else { @@ -996,8 +1013,7 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) } } - /* set the next command to execute, handled in queue_next_cli_cmd() */ - if ((cli_state->next_cmd_req != NULL) && (strcmp(cli_state->curr_cmd_req, "list-files") == 0x00)) + if(0x01 == status) { queue_next_cli_cmd(cli_state); } @@ -1262,6 +1278,7 @@ int main(int argc, char **argv) char *user = NULL; char *pass = NULL; char *mnemonic = NULL; + cli_state_t *cli_state = NULL; if (strcmp(command, "get-info") == 0) { printf("Storj bridge: %s\n\n", storj_bridge); @@ -1459,7 +1476,7 @@ int main(int argc, char **argv) goto end_program; } - cli_state_t *cli_state = malloc(sizeof(cli_state_t)); + cli_state = malloc(sizeof(cli_state_t)); if (!cli_state) { status = 1; goto end_program; @@ -1483,19 +1500,30 @@ int main(int argc, char **argv) status = 1; goto end_program; } - } else if (strcmp(command, "upload-file") == 0) { - char *bucket_id = argv[command_index + 1]; + } + else if (strcmp(command, "upload-file") == 0) + { + char *bucket_id = NULL; + + /* get the corresponding bucket id from the bucket name */ + char *bucket_name = argv[command_index + 1]; char *path = argv[command_index + 2]; - if (!bucket_id || !path) { - printf("Missing arguments: \n"); + if (!bucket_name || !path) + { + printf("Missing arguments: \n"); status = 1; goto end_program; } - - if (upload_file(env, bucket_id, path)) { - status = 1; - goto end_program; + else + { + cli_state->curr_cmd_req = command; + cli_state->bucket_name = bucket_name; + cli_state->file_path = path; + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } } } else if (strcmp(command, "list-files") == 0) @@ -1631,6 +1659,10 @@ int main(int argc, char **argv) if (mnemonic) { free(mnemonic); } + if(cli_state){ + free(cli_state); + } + return status; } @@ -1639,9 +1671,14 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) { void *handle = cli_state->handle; - if ((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00) && + if ((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00) && (strcmp("list-files-1", cli_state->next_cmd_req) == 0x00)) { storj_bridge_list_files(cli_state->env, cli_state->bucket_id, NULL, list_files_callback); } + else if ((strcmp("upload-file" , cli_state->curr_cmd_req) == 0x00) && + (strcmp("upload-file-1", cli_state->next_cmd_req) == 0x00)) + { + upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path); + } } From 055323a3cc682c51b2da9e86f93e277a64654ebd Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 26 Dec 2017 09:15:03 -0500 Subject: [PATCH 07/68] Indentation Changes & Implemented list-files, upload-file and download-file cli command using symbolic name --- src/cli.c | 99 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 30 deletions(-) diff --git a/src/cli.c b/src/cli.c index 92ba62c..05cb85f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -827,6 +827,7 @@ static void list_files_callback(uv_work_t *work_req, int status) int ret_status = 0; assert(status == 0); list_files_request_t *req = work_req->data; + cli_state_t *cli_state = req->handle; if (req->status_code == 404) { printf("Bucket id [%s] does not exist\n", req->bucket_id); @@ -845,23 +846,40 @@ static void list_files_callback(uv_work_t *work_req, int status) printf("No files for bucket.\n"); } - for (int i = 0; i < req->total_files; i++) { - + for (int i = 0; i < req->total_files; i++) + { storj_file_meta_t *file = &req->files[i]; - printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", - file->id, - file->size, - file->decrypted ? "true" : "false", - file->mimetype, - file->created, - file->filename); + /* get the file id of the given file name */ + if(cli_state->file_name != NULL) + { + if(strcmp(cli_state->file_name, file->filename) == 0x00) + { + cli_state->file_id = (char *)file->id; + cli_state->next_cmd_req = "download-file-1"; + break; + } + } + else + { + printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", + file->id, + file->size, + file->decrypted ? "true" : "false", + file->mimetype, + file->created, + file->filename); + } + } + + if(strcmp(cli_state->curr_cmd_req, "download-file" ) == 0x00) + { + queue_next_cli_cmd(cli_state); } cleanup: storj_free_list_files_request(req); free(work_req); - exit(ret_status); } static void delete_file_callback(uv_work_t *work_req, int status) @@ -953,6 +971,7 @@ static void get_buckets_callback(uv_work_t *work_req, int status) static void get_bucket_id_callback(uv_work_t *work_req, int status) { + int ret_status = 0x00; assert(status == 0); get_buckets_request_t *req = work_req->data; cli_state_t *cli_state = req->handle; @@ -972,28 +991,28 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) if (cli_state->bucket_name != NULL) { - int ret = strcmp(cli_state->bucket_name, bucket->name); - if (ret == 0x00) + if(strcmp(cli_state->bucket_name, bucket->name) == 0x00) { printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", bucket->id, bucket->decrypted ? "true" : "false", bucket->created, bucket->name); cli_state->bucket_id = (char *)bucket->id; - if((ret = strcmp(cli_state->curr_cmd_req, "list-files")) == 0x00) + if(((strcmp(cli_state->curr_cmd_req, "list-files")) == 0x00) || + ((strcmp(cli_state->curr_cmd_req, "download-file")) == 0x00)) { cli_state->next_cmd_req = "list-files-1"; - status = 1; + ret_status = 1; } - else if((ret = strcmp(cli_state->curr_cmd_req, "upload-file")) == 0x00) + else if((strcmp(cli_state->curr_cmd_req, "upload-file")) == 0x00) { cli_state->next_cmd_req = "upload-file-1"; - status = 1; + ret_status = 1; } else { printf("Invalid curr cmd req = %s\n", cli_state->curr_cmd_req); - status = 0; + ret_status = 0; } break; } @@ -1013,7 +1032,7 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) } } - if(0x01 == status) + if(0x01 == ret_status) { queue_next_cli_cmd(cli_state); } @@ -1485,20 +1504,28 @@ int main(int argc, char **argv) cli_state->env = env; - if (strcmp(command, "download-file") == 0) { - char *bucket_id = argv[command_index + 1]; - char *file_id = argv[command_index + 2]; + if (strcmp(command, "download-file") == 0) + { + char *bucket_name = argv[command_index + 1]; + char *file_name = argv[command_index + 2]; char *path = argv[command_index + 3]; - if (!bucket_id || !file_id || !path) { - printf("Missing arguments: \n"); + if (!bucket_name || !file_name || !path) + { + printf("Missing arguments: \n"); status = 1; goto end_program; } - - if (download_file(env, bucket_id, file_id, path)) { - status = 1; - goto end_program; + else + { + cli_state->curr_cmd_req = command; + cli_state->bucket_name = bucket_name; + cli_state->file_name = file_name; + cli_state->file_path = path; + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } } } else if (strcmp(command, "upload-file") == 0) @@ -1543,6 +1570,8 @@ int main(int argc, char **argv) { cli_state->curr_cmd_req = command; cli_state->bucket_name = bucket_name; + cli_state->file_name = NULL; + cli_state->file_id = NULL; if(!cli_state->bucket_id) { storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); @@ -1671,10 +1700,20 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) { void *handle = cli_state->handle; - if ((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00) && - (strcmp("list-files-1", cli_state->next_cmd_req) == 0x00)) + if (((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00)|| + ((strcmp("download-file" , cli_state->curr_cmd_req) == 0x00))) && + ((strcmp("list-files-1", cli_state->next_cmd_req) == 0x00)|| + (strcmp("download-file-1", cli_state->next_cmd_req)==0x00))) { - storj_bridge_list_files(cli_state->env, cli_state->bucket_id, NULL, list_files_callback); + if(strcmp("list-files-1" , cli_state->next_cmd_req) == 0x00) + { + storj_bridge_list_files(cli_state->env, cli_state->bucket_id, cli_state, list_files_callback); + } + + if(strcmp("download-file-1" , cli_state->next_cmd_req) == 0x00) + { + download_file(cli_state->env, cli_state->bucket_id, cli_state->file_id, cli_state->file_path); + } } else if ((strcmp("upload-file" , cli_state->curr_cmd_req) == 0x00) && (strcmp("upload-file-1", cli_state->next_cmd_req) == 0x00)) From 802b30fcdcd7dea91c0a6d6939868b2a6af440da Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 27 Dec 2017 14:52:34 -0500 Subject: [PATCH 08/68] Initial implementation of upload-file alias cp using ./storj cp storj:///[filename][.][] --- src/cli.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 1 deletion(-) diff --git a/src/cli.c b/src/cli.c index 05cb85f..8019478 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -44,6 +44,7 @@ extern int errno; #endif static void queue_next_cli_cmd(cli_state_t *cli_state); +static const char *get_filename_separator(const char *file_path); static inline void noop() {}; #define HELP_TEXT "usage: storj [] []\n\n" \ @@ -84,6 +85,104 @@ static inline void noop() {}; #define CLI_VERSION "libstorj-2.0.1-beta" + +static void print_error(char *this, char *filename1, char *filename2) +{ + fprintf(stderr, "%s cannot move %s to %s\n%s\n", + this, filename1, filename2, strerror(errno)); + + exit(EXIT_FAILURE); +} + +static void print_upload_usage(char *this) +{ + fprintf(stderr,"SYNTAX ERROR:\nUsage %s [old_filename] [new_filename]",this); + + exit(EXIT_FAILURE); +} + +static int file_exists(char *file_path) +{ + FILE *fd = fopen(file_path, "r"); + + if (!fd) { + printf("Invalid file path: %s\n", file_path); + return 1; + } + + const char *file_name = get_filename_separator(file_path); + + if (!file_name) + { + file_name = file_path; + } + return 0; +} + +static int strpos(char *str, char *sub_str) +{ + /* find first occurance of substring in string */ + char *sub_str_pos=strstr(str,sub_str); + + /* if null return -1 , otherwise return substring address - base address */ + return sub_str_pos == NULL ? -1 : (sub_str_pos - str ); +} + +static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) +{ + char sub_str[] = "storj://"; + int i = 0x00; /* num of tokens */ + + int ret = strpos(cmd_str,sub_str); + ret == -1 ? printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret) : printf("%d",ret); + + if(ret == 0x00) + { + /* start tokenizing */ + str_token[0] = strtok(cmd_str, "/"); + while(str_token[i] != NULL) + { + i++; + str_token[i] = strtok(NULL, "/"); + } + } + else + { + i = ret; + } + + return i; +} + +void printdir(char *dir, int depth) +{ + DIR *dp; + struct dirent *entry; + struct stat statbuf; + int spaces = depth*4; + + if((dp = opendir(dir)) == NULL) { + fprintf(stderr,"cannot open directory: %s\n", dir); + return; + } + chdir(dir); + while((entry = readdir(dp)) != NULL) { + lstat(entry->d_name,&statbuf); + if(S_ISDIR(statbuf.st_mode)) { + /* Found a directory, but ignore . and .. */ + if(strcmp(".",entry->d_name) == 0 || + strcmp("..",entry->d_name) == 0) + continue; + printf("%*s%s/\n",spaces,"",entry->d_name); + /* Recurse at a new indent level */ + printdir(entry->d_name,depth+1); + } + else printf("%*s%s\n",spaces,"",entry->d_name); + } + chdir(".."); + closedir(dp); +} + static void json_logger(const char *message, int level, void *handle) { printf("{\"message\": \"%s\", \"level\": %i, \"timestamp\": %" PRIu64 "}\n", @@ -1528,6 +1627,52 @@ int main(int argc, char **argv) } } } + else if (strcmp(command, "cp") == 0) + { + int num_of_tokens = 0x00; + char *token[10]; /* Max 9 directories supported */ + char *path = argv[command_index + 1]; + char *bucket_name = argv[command_index + 2]; + + memset(token, 0x00, sizeof(token)); + printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); + printf("KSA:[%s] bucket_name = %s \n", __FUNCTION__, bucket_name); + + /* check if file exists */ + if(file_exists(path) == 0x00) + { + const char *file_name = get_filename_separator(path); + printf("KSA:[%s] file name = %s\n", __FUNCTION__, file_name); + + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } + + if(num_of_tokens < 0x02) /* no file-name entered */ + { + printf("[%s] Invalid command ... \n", __FUNCTION__); + goto end_program; + } + else + { + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = path; + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } + } + } + else + { + printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); + printf("Invalid filename \n"); + } + } else if (strcmp(command, "upload-file") == 0) { char *bucket_id = NULL; From d53fa412e1bef02aa2b89334aca0d122b97b85e5 Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 28 Dec 2017 08:58:04 -0500 Subject: [PATCH 09/68] SUPPORT -->./storj -d cp storj:/// --> ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/* --> Invalid target filename --> ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/. --> uploads correctly --> ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/teset2 --> Invalid target filename --> ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/teset1 --> uploads correctly --- src/cli.c | 64 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/src/cli.c b/src/cli.c index 8019478..f6a13ee 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1642,7 +1642,7 @@ int main(int argc, char **argv) if(file_exists(path) == 0x00) { const char *file_name = get_filename_separator(path); - printf("KSA:[%s] file name = %s\n", __FUNCTION__, file_name); + printf("KSA:[%s][%d] file name = %s\n", __FUNCTION__, __LINE__,file_name); num_of_tokens = validate_cmd_tokenize(bucket_name, token); printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); @@ -1651,20 +1651,56 @@ int main(int argc, char **argv) printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); } - if(num_of_tokens < 0x02) /* no file-name entered */ + switch (num_of_tokens ) { - printf("[%s] Invalid command ... \n", __FUNCTION__); - goto end_program; - } - else - { - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = path; - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } + case 0x03: /* local filename and upload filename are valid names */ + if ((strcmp(file_name, token[2]) == 0x00) || + (strcmp(token[2], ".") == 0x00)) + { + printf("[%d] am here file_path = %s\n", __LINE__, path); + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = path; + printf("[%d] am here file_path = %s\n", __LINE__, path); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } + } + else + { + #if 0 + /* rename the source file same as target file and upload */ + int ret = 0x00; + ret = rename(path, token[2]); + printf("[%d]rename = %d \n", __LINE__, ret); + #endif + + printf("KSA:[%s] Invalid upload target filename \n", + __FUNCTION__, file_name, token[2]); + } + break; + + case 0x02: /* missing uploadfilename */ + if (token[2] == NULL) + { + printf("[%d] am here file_path = %s\n", __LINE__, path); + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = path; + printf("[%d] am here file_path = %s\n", __LINE__, path); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } + } + break; + case 0x01: + case 0x00: + default: + printf("[%s] Invalid command ... token[2]=%s \n", __FUNCTION__, token[2]); + goto end_program; + break; } } else From 4e1c7f5efb680ae1384dec2161e2c68fa35f1ad8 Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 28 Dec 2017 11:33:54 -0500 Subject: [PATCH 10/68] Incremental checking Modified the file_exists() to verify the local filename entered is either a directory or a file. Also if it is a directory will print out all the files recursively( from sub directories) --- src/cli.c | 105 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 23 deletions(-) diff --git a/src/cli.c b/src/cli.c index f6a13ee..7b0fec4 100644 --- a/src/cli.c +++ b/src/cli.c @@ -103,38 +103,88 @@ static void print_upload_usage(char *this) static int file_exists(char *file_path) { - FILE *fd = fopen(file_path, "r"); + struct stat sb; + gid_t st_grpid; - if (!fd) { - printf("Invalid file path: %s\n", file_path); + if (stat(file_path, &sb) == -1) + { + perror("stat"); + printf("\n\n returning 1\n\n"); return 1; } - const char *file_name = get_filename_separator(file_path); - - if (!file_name) + switch (sb.st_mode & S_IFMT) { - file_name = file_path; - } - return 0; + case S_IFBLK: + printf("block device\n"); + break; + case S_IFCHR: + printf("character device\n"); + break; + case S_IFDIR: + printf("file_path = %s\n\n", file_path); + printf("directory\n"); + printdir(file_path, 0); + break; + case S_IFIFO: + printf("FIFO/pipe\n"); + break; + case S_IFLNK: + printf("symlink\n"); + break; + case S_IFREG: + printf("regular file\n"); + return 0; + break; + case S_IFSOCK: + printf("socket\n"); + break; + default: + printf("unknown?\n"); + break; + } + + #if ENABLE_FILE_DETAILS + printf("I-node number: %ld\n", (long)sb.st_ino); + + printf("Mode: %lo (octal)\n", + (unsigned long)sb.st_mode); + + printf("Link count: %ld\n", (long)sb.st_nlink); + printf("Ownership: UID=%ld GID=%ld\n", + (long)sb.st_uid, (long)sb.st_gid); + + printf("Preferred I/O block size: %ld bytes\n", + (long)sb.st_blksize); + printf("File size: %lld bytes\n", + (long long)sb.st_size); + printf("Blocks allocated: %lld\n", + (long long)sb.st_blocks); + + printf("Last status change: %s", ctime(&sb.st_ctime)); + printf("Last file access: %s", ctime(&sb.st_atime)); + printf("Last file modification: %s", ctime(&sb.st_mtime)); + #endif + + return 1; } static int strpos(char *str, char *sub_str) { /* find first occurance of substring in string */ - char *sub_str_pos=strstr(str,sub_str); + char *sub_str_pos=strstr(str,sub_str); /* if null return -1 , otherwise return substring address - base address */ - return sub_str_pos == NULL ? -1 : (sub_str_pos - str ); + return sub_str_pos == NULL ? -1 : (sub_str_pos - str ); } static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) { - char sub_str[] = "storj://"; + char sub_str[] = "storj://"; int i = 0x00; /* num of tokens */ - int ret = strpos(cmd_str,sub_str); - ret == -1 ? printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret) : printf("%d",ret); + int ret = strpos(cmd_str,sub_str); + ret == -1 ? printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret) : printf("%d",ret); if(ret == 0x00) { @@ -151,7 +201,7 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) i = ret; } - return i; + return i; } void printdir(char *dir, int depth) @@ -159,7 +209,7 @@ void printdir(char *dir, int depth) DIR *dp; struct dirent *entry; struct stat statbuf; - int spaces = depth*4; + int spaces = depth*4; if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); @@ -1279,6 +1329,7 @@ int main(int argc, char **argv) {"log", required_argument, 0, 'l'}, {"debug", no_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, + {"recursive", required_argument, 0, 'r'}, {0, 0, 0, 0} }; @@ -1297,10 +1348,11 @@ int main(int argc, char **argv) char *storj_bridge = getenv("STORJ_BRIDGE"); int c; int log_level = 0; + char *local_file_path = NULL; char *proxy = getenv("STORJ_PROXY"); - while ((c = getopt_long_only(argc, argv, "hdl:p:vVu:", + while ((c = getopt_long_only(argc, argv, "hdl:p:vVu:r:R:", cmd_options, &index)) != -1) { switch (c) { case 'u': @@ -1318,7 +1370,14 @@ int main(int argc, char **argv) case 'V': case 'v': printf(CLI_VERSION "\n\n"); - exit(0); + //exit(0); + break; + case 'R': + case 'r': + printf( "Recursive Coping \n\n"); + local_file_path = optarg; + printf("local_file_path = %s, %s\n\n", local_file_path, optarg); + //exit(0); break; case 'h': printf(HELP_TEXT); @@ -1635,8 +1694,8 @@ int main(int argc, char **argv) char *bucket_name = argv[command_index + 2]; memset(token, 0x00, sizeof(token)); - printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); - printf("KSA:[%s] bucket_name = %s \n", __FUNCTION__, bucket_name); + printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); + printf("KSA:[%s][%d] upload path = %s\n", __FUNCTION__, __LINE__, bucket_name); /* check if file exists */ if(file_exists(path) == 0x00) @@ -1651,7 +1710,7 @@ int main(int argc, char **argv) printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); } - switch (num_of_tokens ) + switch (num_of_tokens) { case 0x03: /* local filename and upload filename are valid names */ if ((strcmp(file_name, token[2]) == 0x00) || @@ -1676,8 +1735,8 @@ int main(int argc, char **argv) printf("[%d]rename = %d \n", __LINE__, ret); #endif - printf("KSA:[%s] Invalid upload target filename \n", - __FUNCTION__, file_name, token[2]); + printf("KSA:[%s][%d] Invalid upload target filename \n", + __FUNCTION__, __LINE__); } break; From 76244d2370fd6920b79870e7009a06762c3197a1 Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 28 Dec 2017 14:33:46 -0500 Subject: [PATCH 11/68] Modified printdir() to printout all the files with absolutely recursively. Commented the printf statements. Uncommenting them, you can see on the stdio --- src/cli.c | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/cli.c b/src/cli.c index 7b0fec4..9cdcafa 100644 --- a/src/cli.c +++ b/src/cli.c @@ -105,6 +105,7 @@ static int file_exists(char *file_path) { struct stat sb; gid_t st_grpid; + FILE *out_fd; if (stat(file_path, &sb) == -1) { @@ -124,7 +125,13 @@ static int file_exists(char *file_path) case S_IFDIR: printf("file_path = %s\n\n", file_path); printf("directory\n"); - printdir(file_path, 0); + + if((out_fd = fopen("output.txt", "a+")) == NULL) + { + return; + } + printdir(file_path, 0,out_fd); + fclose(out_fd); break; case S_IFIFO: printf("FIFO/pipe\n"); @@ -204,33 +211,58 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) return i; } -void printdir(char *dir, int depth) +void printdir(char *dir, int depth, FILE *fd) { DIR *dp; struct dirent *entry; struct stat statbuf; - int spaces = depth*4; + int spaces = depth*4; + char tmp_dir[80]; + char *full_path = NULL; + + memset(tmp_dir, 0x00, sizeof(tmp_dir)); - if((dp = opendir(dir)) == NULL) { + if((dp = opendir(dir)) == NULL) + { fprintf(stderr,"cannot open directory: %s\n", dir); return; } + chdir(dir); - while((entry = readdir(dp)) != NULL) { + while((entry = readdir(dp)) != NULL) + { lstat(entry->d_name,&statbuf); - if(S_ISDIR(statbuf.st_mode)) { + if(S_ISDIR(statbuf.st_mode)) + { /* Found a directory, but ignore . and .. */ if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue; - printf("%*s%s/\n",spaces,"",entry->d_name); + + //fprintf(out_fd, "%*s%s\n",spaces,"",entry->d_name); + //printf("%*s%s/\n",spaces,"",entry->d_name); + + //strcpy(tmp_dir,entry->d_name); + //printf("%*s%s/\n",spaces,"",tmp_dir); + + //strcat(tmp_dir,"__"); + //printf("%*s%s/\n",spaces,"",tmp_dir); + /* Recurse at a new indent level */ - printdir(entry->d_name,depth+1); + printdir(entry->d_name,depth+1,fd); + } + else + { + //fprintf(out_fd,"%s%s%s\n","",tmp_dir,entry->d_name); + //printf("%s%s%s\n","",tmp_dir,entry->d_name); + full_path = realpath(entry->d_name, NULL); + fprintf(fd,"%s%s\n","",full_path); + printf("%s%s\n","",full_path); } - else printf("%*s%s\n",spaces,"",entry->d_name); } chdir(".."); closedir(dp); + free(full_path); } static void json_logger(const char *message, int level, void *handle) From 0d0c2860374f5e59336ab43e7bdcfeec9f718763 Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 28 Dec 2017 14:35:33 -0500 Subject: [PATCH 12/68] Cleaned the code. Removed the commented code out --- src/cli.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/cli.c b/src/cli.c index 9cdcafa..57f970d 100644 --- a/src/cli.c +++ b/src/cli.c @@ -239,22 +239,11 @@ void printdir(char *dir, int depth, FILE *fd) strcmp("..",entry->d_name) == 0) continue; - //fprintf(out_fd, "%*s%s\n",spaces,"",entry->d_name); - //printf("%*s%s/\n",spaces,"",entry->d_name); - - //strcpy(tmp_dir,entry->d_name); - //printf("%*s%s/\n",spaces,"",tmp_dir); - - //strcat(tmp_dir,"__"); - //printf("%*s%s/\n",spaces,"",tmp_dir); - /* Recurse at a new indent level */ printdir(entry->d_name,depth+1,fd); } else { - //fprintf(out_fd,"%s%s%s\n","",tmp_dir,entry->d_name); - //printf("%s%s%s\n","",tmp_dir,entry->d_name); full_path = realpath(entry->d_name, NULL); fprintf(fd,"%s%s\n","",full_path); printf("%s%s\n","",full_path); From 8991b868933eee898f27581923c4694a17837989 Mon Sep 17 00:00:00 2001 From: Kishore Date: Fri, 29 Dec 2017 16:27:56 -0500 Subject: [PATCH 13/68] Incremental checkin Added the functionality to test ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/teset1 ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/. ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/ ./storj cp -r[R] /home/kishore/libstorj/src/teset1 storj://testbucket/teset1 ./storj cp -r[R] /home/kishore/libstorj/src/teset1 storj://testbucket/. ./storj cp -r[R] /home/kishore/libstorj/src/teset1 storj://testbucket/ Working on uploading the file recursively. --- src/cli.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cli.c b/src/cli.c index 57f970d..6f0e29f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1715,6 +1715,7 @@ int main(int argc, char **argv) char *bucket_name = argv[command_index + 2]; memset(token, 0x00, sizeof(token)); + printf("KSA:[%s][%d] local_file_path = %s\n", __FUNCTION__, __LINE__, local_file_path); printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); printf("KSA:[%s][%d] upload path = %s\n", __FUNCTION__, __LINE__, bucket_name); From f8ecced5c635a8e3f92d5aa621f395c6215a21a0 Mon Sep 17 00:00:00 2001 From: Kishore Date: Fri, 29 Dec 2017 16:29:22 -0500 Subject: [PATCH 14/68] Incremental Checking Working on recursive uploads Current implementation ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/teset1 ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/. ./storj cp /home/kishore/libstorj/src/teset1 storj://testbucket/ ./storj cp -r[R] /home/kishore/libstorj/src/teset1 storj://testbucket/teset1 ./storj cp -r[R] /home/kishore/libstorj/src/teset1 storj://testbucket/. ./storj cp -r[R] /home/kishore/libstorj/src/teset1 storj://testbucket/ --- src/cli.c | 204 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 127 insertions(+), 77 deletions(-) diff --git a/src/cli.c b/src/cli.c index 6f0e29f..eaa8d47 100644 --- a/src/cli.c +++ b/src/cli.c @@ -18,6 +18,12 @@ #define STORJ_THREADPOOL_SIZE "64" +#define CLI_NO_SUCH_FILE_OR_DIR 0x00 +#define CLI_VALID_REGULAR_FILE 0x01 +#define CLI_VALID_DIR 0x02 +#define CLI_UNKNOWN_FILE_ATTR 0x03 +#define CLI_UPLOAD_FILE_LOG_ERR 0x04 + typedef struct { char *user; char *pass; @@ -107,14 +113,14 @@ static int file_exists(char *file_path) gid_t st_grpid; FILE *out_fd; - if (stat(file_path, &sb) == -1) + if (stat(file_path, &sb) == -1) { perror("stat"); printf("\n\n returning 1\n\n"); - return 1; + return CLI_NO_SUCH_FILE_OR_DIR; } - switch (sb.st_mode & S_IFMT) + switch (sb.st_mode & S_IFMT) { case S_IFBLK: printf("block device\n"); @@ -126,12 +132,14 @@ static int file_exists(char *file_path) printf("file_path = %s\n\n", file_path); printf("directory\n"); - if((out_fd = fopen("output.txt", "a+")) == NULL) + if((out_fd = fopen("output.txt", "w")) == NULL) { - return; + return CLI_UPLOAD_FILE_LOG_ERR; } - printdir(file_path, 0,out_fd); + printdir(file_path, 0, out_fd); fclose(out_fd); + printf("KSA[%s][%d] regular dir\n", __FUNCTION__, __LINE__); + return CLI_VALID_DIR; break; case S_IFIFO: printf("FIFO/pipe\n"); @@ -140,8 +148,8 @@ static int file_exists(char *file_path) printf("symlink\n"); break; case S_IFREG: - printf("regular file\n"); - return 0; + printf("KSA[%s][%d] regular file\n", __FUNCTION__, __LINE__); + return CLI_VALID_REGULAR_FILE; break; case S_IFSOCK: printf("socket\n"); @@ -173,7 +181,7 @@ static int file_exists(char *file_path) printf("Last file modification: %s", ctime(&sb.st_mtime)); #endif - return 1; + return CLI_UNKNOWN_FILE_ATTR; } static int strpos(char *str, char *sub_str) @@ -222,17 +230,17 @@ void printdir(char *dir, int depth, FILE *fd) memset(tmp_dir, 0x00, sizeof(tmp_dir)); - if((dp = opendir(dir)) == NULL) + if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); return; } chdir(dir); - while((entry = readdir(dp)) != NULL) + while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); - if(S_ISDIR(statbuf.st_mode)) + if(S_ISDIR(statbuf.st_mode)) { /* Found a directory, but ignore . and .. */ if(strcmp(".",entry->d_name) == 0 || @@ -242,7 +250,7 @@ void printdir(char *dir, int depth, FILE *fd) /* Recurse at a new indent level */ printdir(entry->d_name,depth+1,fd); } - else + else { full_path = realpath(entry->d_name, NULL); fprintf(fd,"%s%s\n","",full_path); @@ -1397,7 +1405,8 @@ int main(int argc, char **argv) case 'r': printf( "Recursive Coping \n\n"); local_file_path = optarg; - printf("local_file_path = %s, %s\n\n", local_file_path, optarg); + printf("local_file_path = %s\n optarg = %s\n\n", + local_file_path, optarg); //exit(0); break; case 'h': @@ -1711,84 +1720,125 @@ int main(int argc, char **argv) { int num_of_tokens = 0x00; char *token[10]; /* Max 9 directories supported */ - char *path = argv[command_index + 1]; - char *bucket_name = argv[command_index + 2]; + char *path = NULL; + char *bucket_name = NULL; + + if(local_file_path == NULL) + { + path = argv[command_index + 1]; + bucket_name = argv[command_index + 2]; + } + else + { + path = local_file_path; + bucket_name = argv[command_index + 1]; + } memset(token, 0x00, sizeof(token)); - printf("KSA:[%s][%d] local_file_path = %s\n", __FUNCTION__, __LINE__, local_file_path); + printf("KSA:[%s][%d] cp -r[R] arg = %s\n", __FUNCTION__, __LINE__, local_file_path); printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); printf("KSA:[%s][%d] upload path = %s\n", __FUNCTION__, __LINE__, bucket_name); - /* check if file exists */ - if(file_exists(path) == 0x00) + int file_exist_status = file_exists(path); + printf("KSA[%s][%d] file_exist_status = %d\n", + __FUNCTION__, __LINE__, file_exist_status); + const char *file_name = NULL; + char cwd[1024] = "/home/kishore/libstorj/src"; + switch(file_exist_status) { - const char *file_name = get_filename_separator(path); - printf("KSA:[%s][%d] file name = %s\n", __FUNCTION__, __LINE__,file_name); - - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } + case CLI_UNKNOWN_FILE_ATTR: + case CLI_NO_SUCH_FILE_OR_DIR: + printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); + printf("Invalid filename \n"); + break; - switch (num_of_tokens) - { - case 0x03: /* local filename and upload filename are valid names */ - if ((strcmp(file_name, token[2]) == 0x00) || - (strcmp(token[2], ".") == 0x00)) - { - printf("[%d] am here file_path = %s\n", __LINE__, path); - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = path; - printf("[%d] am here file_path = %s\n", __LINE__, path); - if(!cli_state->bucket_id) + case CLI_VALID_REGULAR_FILE: + file_name = get_filename_separator(path); + printf("KSA:[%s][%d] file name = %s\n", __FUNCTION__, __LINE__,file_name); + + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } + + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + switch (num_of_tokens) + { + case 0x03: /* local filename and upload filename are valid names */ + if ((strcmp(file_name, token[2]) == 0x00) || + (strcmp(token[2], ".") == 0x00)) { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = path; + printf("[%d] target file name = %s\n", __LINE__, file_name); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } } - } - else - { - #if 0 - /* rename the source file same as target file and upload */ - int ret = 0x00; - ret = rename(path, token[2]); - printf("[%d]rename = %d \n", __LINE__, ret); - #endif - - printf("KSA:[%s][%d] Invalid upload target filename \n", - __FUNCTION__, __LINE__); - } - break; + else + { + printf("KSA:[%s][%d] Invalid upload target filename - ", + __FUNCTION__, __LINE__); + printf("Use same filename as source or '.' or blank \n"); + goto end_program; + } + break; - case 0x02: /* missing uploadfilename */ - if (token[2] == NULL) - { - printf("[%d] am here file_path = %s\n", __LINE__, path); - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = path; - printf("[%d] am here file_path = %s\n", __LINE__, path); - if(!cli_state->bucket_id) + case 0x02: /* missing upload filename */ + if (token[2] == NULL) { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = path; + printf("[%d] target file name = %s\n", __LINE__, file_name); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } } - } + break; + case 0x01: + case 0x00: + default: + printf("[%s] Invalid command ... token[2]=%s \n", __FUNCTION__, token[2]); + goto end_program; break; - case 0x01: - case 0x00: - default: - printf("[%s] Invalid command ... token[2]=%s \n", __FUNCTION__, token[2]); + } + break; + + case CLI_VALID_DIR: + #if 0 //NEED_TO_FIX_GETCWD_FUNC_ + if (getcwd(cwd, sizeof(cwd)) != NULL) + { + fprintf(stdout, "Current working dir: %s\n", cwd); + strcat(cwd, "/output.txt"); + fprintf(stdout, "Current working dir: %s\n", cwd); + if(file_exists(cwd) == CLI_VALID_REGULAR_FILE) + { + printf("YAHOO>>>>>>\n\n\n"); + } + } + else + { + perror("getcwd() error"); goto end_program; - break; + } + #endif + strcat(cwd, "/output.txt"); + fprintf(stdout, "Current working dir: %s\n", cwd); + if(file_exists(cwd) == CLI_VALID_REGULAR_FILE) + { + printf("YAHOO>>>>>>\n\n\n"); } - } - else - { - printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); - printf("Invalid filename \n"); - } + break; + + default: + break; + } } else if (strcmp(command, "upload-file") == 0) { From 6c2cdfd03500af48cc705e6b2d14df8ee28e11aa Mon Sep 17 00:00:00 2001 From: Kishore Date: Fri, 29 Dec 2017 19:17:46 -0500 Subject: [PATCH 15/68] Reading from a file the list of files to be uploaded is coded. Only one file is currently uploadable --- src/cli.c | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/cli.c b/src/cli.c index eaa8d47..b2846a7 100644 --- a/src/cli.c +++ b/src/cli.c @@ -216,7 +216,7 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) i = ret; } - return i; + return i; } void printdir(char *dir, int depth, FILE *fd) @@ -1744,6 +1744,7 @@ int main(int argc, char **argv) __FUNCTION__, __LINE__, file_exist_status); const char *file_name = NULL; char cwd[1024] = "/home/kishore/libstorj/src"; + char *upload_file = cwd; switch(file_exist_status) { case CLI_UNKNOWN_FILE_ATTR: @@ -1828,11 +1829,46 @@ int main(int argc, char **argv) goto end_program; } #endif - strcat(cwd, "/output.txt"); - fprintf(stdout, "Current working dir: %s\n", cwd); - if(file_exists(cwd) == CLI_VALID_REGULAR_FILE) + strcat(upload_file, "/output.txt"); + printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_file); + if(file_exists(upload_file) == CLI_VALID_REGULAR_FILE) { - printf("YAHOO>>>>>>\n\n\n"); + /* start reading one file at a time and upload the files */ + upload_file = "/home/kishore/libstorj/src/output1.txt"; + FILE *file = fopen ( upload_file, "r" ); + + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } + + if (file != NULL) + { + char line [1000]; + char *temp; + memset(line, 0x00, sizeof(line)); + while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ + { + fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. + temp = strrchr(line, '\n'); + if(temp) *temp = '\0'; + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = line; + printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } + } + fclose(file); + } + else + { + perror(upload_file); //print the error message on stderr. + } } break; From e2cde598e8bddef0dd8b174cce355a6f2957e424 Mon Sep 17 00:00:00 2001 From: Kishore Date: Sun, 31 Dec 2017 19:12:44 -0500 Subject: [PATCH 16/68] fixed the broken list-files command --- src/cli.c | 53 +++++++++-------------------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/src/cli.c b/src/cli.c index b2846a7..0fb927a 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -42,6 +42,7 @@ typedef struct { char *curr_cmd_req; /**< cli curr command requested */ char *next_cmd_req; /**< cli next command requested */ bool cmd_resp; /**< cli command response 0->fail; 1->success */ + bool file_xfer_stat; /**< false -> inprogress; true -> done */ void *handle; }cli_state_t; @@ -575,7 +576,6 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path) if (!fd) { printf("Invalid file path: %s\n", file_path); - return 1; } const char *file_name = get_filename_separator(file_path); @@ -1111,36 +1111,11 @@ static void get_buckets_callback(uv_work_t *work_req, int status) printf("No buckets.\n"); } - for (int i = 0; i < req->total_buckets; i++) - { + for (int i = 0; i < req->total_buckets; i++) { storj_bucket_meta_t *bucket = &req->buckets[i]; - cli_state_t *cli_state = req->handle; - - if (cli_state->bucket_name != NULL) - { - int ret = strcmp(cli_state->bucket_name, bucket->name); - if (ret == 0x00) - { - printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", - bucket->id, bucket->decrypted ? "true" : "false", - bucket->created, bucket->name); - cli_state->bucket_id = (char *)bucket->id; - break; - } - else - { - if (i >= (req->total_buckets -1)) - { - printf("Invalid bucket name. \n"); - } - } - } - else - { - printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", - bucket->id, bucket->decrypted ? "true" : "false", - bucket->created, bucket->name); - } + printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", + bucket->id, bucket->decrypted ? "true" : "false", + bucket->created, bucket->name); } storj_free_get_buckets_request(req); @@ -1399,7 +1374,7 @@ int main(int argc, char **argv) case 'V': case 'v': printf(CLI_VERSION "\n\n"); - //exit(0); + exit(0); break; case 'R': case 'r': @@ -1851,9 +1826,9 @@ int main(int argc, char **argv) memset(line, 0x00, sizeof(line)); while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ { - fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. temp = strrchr(line, '\n'); if(temp) *temp = '\0'; + fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. cli_state->curr_cmd_req = "upload-file"; cli_state->bucket_name = token[1]; cli_state->file_path = line; @@ -1964,17 +1939,7 @@ int main(int argc, char **argv) } else if (strcmp(command, "list-buckets") == 0) { - char *bucket_name = argv[command_index + 1]; - if (bucket_name != NULL) - { - cli_state->bucket_name = (void *)bucket_name; - } - else - { - cli_state->bucket_name = NULL; - } - /* when callback returns, we store the bucket id of bucket name else null */ - storj_bridge_get_buckets(env, cli_state, get_buckets_callback); + storj_bridge_get_buckets(env, NULL, get_buckets_callback); } else if (strcmp(command, "get-bucket-id") == 0) { From 25fa8b4143308efdd8f7e3ef11fd747c255a8b3f Mon Sep 17 00:00:00 2001 From: Kishore Date: Mon, 1 Jan 2018 11:57:25 -0500 Subject: [PATCH 17/68] uploading a list of files partially working --- src/cli.c | 123 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 34 deletions(-) diff --git a/src/cli.c b/src/cli.c index 0fb927a..9cc17c4 100644 --- a/src/cli.c +++ b/src/cli.c @@ -36,9 +36,12 @@ typedef struct { storj_env_t *env; char *bucket_name; char *bucket_id; - char *file_name; - char *file_path; - char *file_id; + char *file_name; /**< next file ready to upload */ + char *file_path; /**< next file ready to upload */ + char *file_id; /**< file id of from the bridge */ + FILE *file_fd; /**< upload file list fd */ + int total_files; /**< total files to upload */ + int curr_up_file; /**< current file number in uploadinng */ char *curr_cmd_req; /**< cli curr command requested */ char *next_cmd_req; /**< cli next command requested */ bool cmd_resp; /**< cli command response 0->fail; 1->success */ @@ -550,14 +553,15 @@ static void upload_file_complete(int status, char *file_id, void *handle) printf("\n"); if (status != 0) { printf("Upload failure: %s\n", storj_strerror(status)); - exit(status); + //exit(status); } printf("Upload Success! File ID: %s\n", file_id); free(file_id); - exit(0); + queue_next_cli_cmd(handle); + //exit(0); } void upload_signal_handler(uv_signal_t *req, int signum) @@ -570,7 +574,7 @@ void upload_signal_handler(uv_signal_t *req, int signum) uv_close((uv_handle_t *)req, close_signal); } -static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path) +static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) { FILE *fd = fopen(file_path, "r"); @@ -616,7 +620,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path) storj_upload_state_t *state = storj_bridge_store_file(env, &upload_opts, - NULL, + handle, progress_cb, upload_file_complete); @@ -1719,7 +1723,7 @@ int main(int argc, char **argv) __FUNCTION__, __LINE__, file_exist_status); const char *file_name = NULL; char cwd[1024] = "/home/kishore/libstorj/src"; - char *upload_file = cwd; + char *upload_list = cwd; switch(file_exist_status) { case CLI_UNKNOWN_FILE_ATTR: @@ -1804,47 +1808,63 @@ int main(int argc, char **argv) goto end_program; } #endif - strcat(upload_file, "/output.txt"); - printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_file); - if(file_exists(upload_file) == CLI_VALID_REGULAR_FILE) + strcat(upload_list, "/output1.txt"); + printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); + if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) { /* start reading one file at a time and upload the files */ - upload_file = "/home/kishore/libstorj/src/output1.txt"; - FILE *file = fopen ( upload_file, "r" ); - - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } + FILE *file = fopen ( upload_list, "r" ); if (file != NULL) { - char line [1000]; + char line [256][256]; char *temp; + int i = 0x00; memset(line, 0x00, sizeof(line)); - while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ + cli_state->file_name = upload_list; + printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); + printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); + /* read a line from a file */ + while(fgets(line[i],sizeof(line), file)!= NULL) { - temp = strrchr(line, '\n'); - if(temp) *temp = '\0'; fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = line; - printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line); - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } + i++; + printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line[i-1]); + } + cli_state->total_files = i; + if(cli_state->total_files > 0x00) + { + cli_state->curr_up_file = 0x01; + } + else + { + cli_state->curr_up_file = 0x00; } - fclose(file); + printf("[%s][%d] total upload files = %d\n", __FUNCTION__, __LINE__, cli_state->total_files); + printf("[%s][%d] upload cur up file# = %d\n", __FUNCTION__, __LINE__, cli_state->curr_up_file); + close(file); } else { perror(upload_file); //print the error message on stderr. } } + + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } + + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + printf("[%s][%d] bucket id = %s\n", __FUNCTION__, __LINE__, cli_state->bucket_id); + if(!cli_state->bucket_id) + { + printf("AM I HERE \n\n"); + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } break; default: @@ -2031,6 +2051,41 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) else if ((strcmp("upload-file" , cli_state->curr_cmd_req) == 0x00) && (strcmp("upload-file-1", cli_state->next_cmd_req) == 0x00)) { - upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path); + + FILE *file = fopen(cli_state->file_name, "r"); + printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); + if (file != NULL) + { + char line[256][256]; + char *temp; + int i = 0x00; + memset(line, 0x00, sizeof(line)); + while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ + { + temp = strrchr(line[i], '\n'); + if(temp) *temp = '\0'; + cli_state->file_path = line[i]; + i++; + printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line[i-1]); + if(i > cli_state->curr_up_file) + break; + } + if(cli_state->curr_up_file < cli_state->total_files) + { + fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. + upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path, cli_state); + cli_state->curr_up_file++; + } + else + { + fprintf(stdout,"***** done uploading files *****\n"); + fclose(file); + exit(0); + } + } + else + { + /* handle single file upload from the command line */ + } } } From f2487a4c420c19edce45734da76d48b23ce70ec5 Mon Sep 17 00:00:00 2001 From: Kishore Date: Mon, 1 Jan 2018 12:59:57 -0500 Subject: [PATCH 18/68] Recursive uploading functionality ./storj cp -r /home/kishore/libstorj/src/ storj://testbucket/ --- src/cli.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/cli.c b/src/cli.c index 9cc17c4..b806de9 100644 --- a/src/cli.c +++ b/src/cli.c @@ -257,8 +257,8 @@ void printdir(char *dir, int depth, FILE *fd) else { full_path = realpath(entry->d_name, NULL); - fprintf(fd,"%s%s\n","",full_path); - printf("%s%s\n","",full_path); + //fprintf(fd,"%s%s\n","",full_path); + //printf("%s%s\n","",full_path); } } chdir(".."); @@ -551,15 +551,17 @@ static void file_progress(double progress, static void upload_file_complete(int status, char *file_id, void *handle) { printf("\n"); - if (status != 0) { + if (status != 0) + { printf("Upload failure: %s\n", storj_strerror(status)); //exit(status); } - - printf("Upload Success! File ID: %s\n", file_id); + else + { + printf("Upload Success! File ID: %s\n", file_id); + } free(file_id); - queue_next_cli_cmd(handle); //exit(0); } @@ -1827,9 +1829,9 @@ int main(int argc, char **argv) /* read a line from a file */ while(fgets(line[i],sizeof(line), file)!= NULL) { - fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. + //fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. i++; - printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line[i-1]); + //printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line[i-1]); } cli_state->total_files = i; if(cli_state->total_files > 0x00) @@ -2066,15 +2068,16 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) if(temp) *temp = '\0'; cli_state->file_path = line[i]; i++; - printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line[i-1]); - if(i > cli_state->curr_up_file) + printf("[%s][%d] [index = %d] target file name = %s\n", __FUNCTION__, __LINE__, i, line[i-1]); + if(i >= cli_state->curr_up_file) break; } - if(cli_state->curr_up_file < cli_state->total_files) + if(cli_state->curr_up_file <= cli_state->total_files) { - fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. + fprintf(stdout,"*****uploading file: %s *****\n",line[i-1]); //print the file contents on stdout. upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path, cli_state); cli_state->curr_up_file++; + //queue_next_cli_cmd(cli_state); } else { From 0c8d13473aee6111d64d4b67f101793ec43dc8fa Mon Sep 17 00:00:00 2001 From: Kishore Date: Mon, 1 Jan 2018 13:20:18 -0500 Subject: [PATCH 19/68] development tested recursive cp (upload) functionality --- src/cli.c | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/cli.c b/src/cli.c index b806de9..ee11980 100644 --- a/src/cli.c +++ b/src/cli.c @@ -550,6 +550,7 @@ static void file_progress(double progress, static void upload_file_complete(int status, char *file_id, void *handle) { + cli_state_t *cli_state = handle; printf("\n"); if (status != 0) { @@ -562,8 +563,12 @@ static void upload_file_complete(int status, char *file_id, void *handle) } free(file_id); + if((cli_state->total_files == 0x00) && + (cli_state->curr_up_file == 0x00)) + { + exit(0); + } queue_next_cli_cmd(handle); - //exit(0); } void upload_signal_handler(uv_signal_t *req, int signum) @@ -1738,6 +1743,7 @@ int main(int argc, char **argv) file_name = get_filename_separator(path); printf("KSA:[%s][%d] file name = %s\n", __FUNCTION__, __LINE__,file_name); + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ num_of_tokens = validate_cmd_tokenize(bucket_name, token); printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); for(int j = 0x00; j < num_of_tokens; j++) @@ -1745,7 +1751,8 @@ int main(int argc, char **argv) printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); } - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + cli_state->total_files = 0x00; + cli_state->curr_up_file = 0x00; switch (num_of_tokens) { case 0x03: /* local filename and upload filename are valid names */ @@ -1793,23 +1800,6 @@ int main(int argc, char **argv) break; case CLI_VALID_DIR: - #if 0 //NEED_TO_FIX_GETCWD_FUNC_ - if (getcwd(cwd, sizeof(cwd)) != NULL) - { - fprintf(stdout, "Current working dir: %s\n", cwd); - strcat(cwd, "/output.txt"); - fprintf(stdout, "Current working dir: %s\n", cwd); - if(file_exists(cwd) == CLI_VALID_REGULAR_FILE) - { - printf("YAHOO>>>>>>\n\n\n"); - } - } - else - { - perror("getcwd() error"); - goto end_program; - } - #endif strcat(upload_list, "/output1.txt"); printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) @@ -1829,9 +1819,7 @@ int main(int argc, char **argv) /* read a line from a file */ while(fgets(line[i],sizeof(line), file)!= NULL) { - //fprintf(stdout,"*****uploading file: %s *****\n",line); //print the file contents on stdout. i++; - //printf("[%s][%d] target file name = %s\n", __FUNCTION__, __LINE__, line[i-1]); } cli_state->total_files = i; if(cli_state->total_files > 0x00) @@ -1848,7 +1836,8 @@ int main(int argc, char **argv) } else { - perror(upload_file); //print the error message on stderr. + /* print the error message on stderr. */ + perror(upload_file); } } @@ -1864,7 +1853,6 @@ int main(int argc, char **argv) printf("[%s][%d] bucket id = %s\n", __FUNCTION__, __LINE__, cli_state->bucket_id); if(!cli_state->bucket_id) { - printf("AM I HERE \n\n"); storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } break; @@ -2077,7 +2065,6 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) fprintf(stdout,"*****uploading file: %s *****\n",line[i-1]); //print the file contents on stdout. upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path, cli_state); cli_state->curr_up_file++; - //queue_next_cli_cmd(cli_state); } else { @@ -2089,6 +2076,8 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) else { /* handle single file upload from the command line */ + printf("handle it dudue\n\n\n"); + upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path, cli_state); } } } From 84919afc0f74008e14f1ea242145eb844f4934ad Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 2 Jan 2018 12:14:38 -0500 Subject: [PATCH 20/68] SUPPORT OF RECURSIVE UPLOADING CLI COMMAND --- src/cli.c | 122 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 50 deletions(-) diff --git a/src/cli.c b/src/cli.c index ee11980..58e6349 100644 --- a/src/cli.c +++ b/src/cli.c @@ -53,6 +53,7 @@ typedef struct { extern int errno; #endif +static void printdir(char *dir, int depth, FILE *fd); static void queue_next_cli_cmd(cli_state_t *cli_state); static const char *get_filename_separator(const char *file_path); static inline void noop() {}; @@ -223,7 +224,7 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) return i; } -void printdir(char *dir, int depth, FILE *fd) +static void printdir(char *dir, int depth, FILE *fd) { DIR *dp; struct dirent *entry; @@ -257,7 +258,7 @@ void printdir(char *dir, int depth, FILE *fd) else { full_path = realpath(entry->d_name, NULL); - //fprintf(fd,"%s%s\n","",full_path); + fprintf(fd,"%s%s\n","",full_path); //printf("%s%s\n","",full_path); } } @@ -1729,8 +1730,10 @@ int main(int argc, char **argv) printf("KSA[%s][%d] file_exist_status = %d\n", __FUNCTION__, __LINE__, file_exist_status); const char *file_name = NULL; - char cwd[1024] = "/home/kishore/libstorj/src"; + //char cwd[1024] = "/home/kishore/libstorj/src"; + char cwd[1024]; char *upload_list = cwd; + memset(upload_list, 0x00, sizeof(cwd)); switch(file_exist_status) { case CLI_UNKNOWN_FILE_ATTR: @@ -1800,66 +1803,85 @@ int main(int argc, char **argv) break; case CLI_VALID_DIR: - strcat(upload_list, "/output1.txt"); - printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); - if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) - { - /* start reading one file at a time and upload the files */ - FILE *file = fopen ( upload_list, "r" ); - - if (file != NULL) + //printf("\tWorkdir: %s\n", getenv("PWD")); + if ((upload_list = getenv("PWD")) != NULL) { - char line [256][256]; - char *temp; - int i = 0x00; - memset(line, 0x00, sizeof(line)); - cli_state->file_name = upload_list; - printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); - printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); - /* read a line from a file */ - while(fgets(line[i],sizeof(line), file)!= NULL) + fprintf(stdout, "Current working dir: %s\n", upload_list); + strcat(upload_list, "/output.txt"); + fprintf(stdout, "Current working dir: %s\n", upload_list); + if(file_exists(cwd) == CLI_VALID_REGULAR_FILE) { - i++; + printf("YAHOO>>>>>>\n\n\n"); } - cli_state->total_files = i; - if(cli_state->total_files > 0x00) + } + else + { + perror("getcwd() error"); + goto end_program; + } + + #if 1 + //strcat(upload_list, "/output1.txt"); + printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); + if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) + { + /* start reading one file at a time and upload the files */ + FILE *file = fopen ( upload_list, "r" ); + + if (file != NULL) { - cli_state->curr_up_file = 0x01; + char line [256][256]; + char *temp; + int i = 0x00; + memset(line, 0x00, sizeof(line)); + cli_state->file_name = upload_list; + printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); + printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); + /* read a line from a file */ + while(fgets(line[i],sizeof(line), file)!= NULL) + { + i++; + } + cli_state->total_files = i; + if(cli_state->total_files > 0x00) + { + cli_state->curr_up_file = 0x01; + } + else + { + cli_state->curr_up_file = 0x00; + } + printf("[%s][%d] total upload files = %d\n", __FUNCTION__, __LINE__, cli_state->total_files); + printf("[%s][%d] upload cur up file# = %d\n", __FUNCTION__, __LINE__, cli_state->curr_up_file); + fclose(file); } else { - cli_state->curr_up_file = 0x00; + /* print the error message on stderr. */ + perror(upload_list); } - printf("[%s][%d] total upload files = %d\n", __FUNCTION__, __LINE__, cli_state->total_files); - printf("[%s][%d] upload cur up file# = %d\n", __FUNCTION__, __LINE__, cli_state->curr_up_file); - close(file); } - else + + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) { - /* print the error message on stderr. */ - perror(upload_file); + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); } - } - - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - printf("[%s][%d] bucket id = %s\n", __FUNCTION__, __LINE__, cli_state->bucket_id); - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } - break; + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + printf("[%s][%d] bucket id = %s\n", __FUNCTION__, __LINE__, cli_state->bucket_id); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + } + #endif + break; - default: - break; - } + default: + break; + }/* switch - case */ } else if (strcmp(command, "upload-file") == 0) { From ffe7ab1c01cc8d93940e41acc24d654e7f139a81 Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 2 Jan 2018 15:12:37 -0500 Subject: [PATCH 21/68] Refactored the upload-file code and added the support for download-file command using storj cp [-rR]storj:/// --- src/cli.c | 412 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 237 insertions(+), 175 deletions(-) diff --git a/src/cli.c b/src/cli.c index 58e6349..4a3d92f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -55,6 +55,8 @@ extern int errno; static void printdir(char *dir, int depth, FILE *fd); static void queue_next_cli_cmd(cli_state_t *cli_state); +static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state); +static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_state); static const char *get_filename_separator(const char *file_path); static inline void noop() {}; @@ -200,28 +202,28 @@ static int strpos(char *str, char *sub_str) static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) { - char sub_str[] = "storj://"; - int i = 0x00; /* num of tokens */ - - int ret = strpos(cmd_str,sub_str); - ret == -1 ? printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret) : printf("%d",ret); - - if(ret == 0x00) - { - /* start tokenizing */ - str_token[0] = strtok(cmd_str, "/"); - while(str_token[i] != NULL) - { - i++; - str_token[i] = strtok(NULL, "/"); - } - } - else - { - i = ret; - } - - return i; + char sub_str[] = "storj://"; + int i = 0x00; /* num of tokens */ + + int ret = strpos(cmd_str, sub_str); + ret == -1 ? printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret) : printf("sub string pos = %d\n", ret); + + if (ret == 0x00) + { + /* start tokenizing */ + str_token[0] = strtok(cmd_str, "/"); + while (str_token[i] != NULL) + { + i++; + str_token[i] = strtok(NULL, "/"); + } + } + else + { + i = ret; + } + + return i; } static void printdir(char *dir, int depth, FILE *fd) @@ -1705,11 +1707,14 @@ int main(int argc, char **argv) } else if (strcmp(command, "cp") == 0) { + int ret = 0x00; int num_of_tokens = 0x00; char *token[10]; /* Max 9 directories supported */ char *path = NULL; char *bucket_name = NULL; + char *file_name = NULL; + /* cp command wrt to upload-file */ if(local_file_path == NULL) { path = argv[command_index + 1]; @@ -1721,167 +1726,53 @@ int main(int argc, char **argv) bucket_name = argv[command_index + 1]; } - memset(token, 0x00, sizeof(token)); - printf("KSA:[%s][%d] cp -r[R] arg = %s\n", __FUNCTION__, __LINE__, local_file_path); - printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); - printf("KSA:[%s][%d] upload path = %s\n", __FUNCTION__, __LINE__, bucket_name); - - int file_exist_status = file_exists(path); - printf("KSA[%s][%d] file_exist_status = %d\n", - __FUNCTION__, __LINE__, file_exist_status); - const char *file_name = NULL; - //char cwd[1024] = "/home/kishore/libstorj/src"; - char cwd[1024]; - char *upload_list = cwd; - memset(upload_list, 0x00, sizeof(cwd)); - switch(file_exist_status) + if (strpos(bucket_name,"storj://") < 0x00) { - case CLI_UNKNOWN_FILE_ATTR: - case CLI_NO_SUCH_FILE_OR_DIR: - printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); - printf("Invalid filename \n"); - break; - - case CLI_VALID_REGULAR_FILE: - file_name = get_filename_separator(path); - printf("KSA:[%s][%d] file name = %s\n", __FUNCTION__, __LINE__,file_name); - - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } - - cli_state->total_files = 0x00; - cli_state->curr_up_file = 0x00; - switch (num_of_tokens) - { - case 0x03: /* local filename and upload filename are valid names */ - if ((strcmp(file_name, token[2]) == 0x00) || - (strcmp(token[2], ".") == 0x00)) - { - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = path; - printf("[%d] target file name = %s\n", __LINE__, file_name); - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } - } - else - { - printf("KSA:[%s][%d] Invalid upload target filename - ", - __FUNCTION__, __LINE__); - printf("Use same filename as source or '.' or blank \n"); - goto end_program; - } - break; - - case 0x02: /* missing upload filename */ - if (token[2] == NULL) - { - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = path; - printf("[%d] target file name = %s\n", __LINE__, file_name); - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } - } - break; - case 0x01: - case 0x00: - default: - printf("[%s] Invalid command ... token[2]=%s \n", __FUNCTION__, token[2]); - goto end_program; - break; - } - break; + /* download-file command */ - case CLI_VALID_DIR: - //printf("\tWorkdir: %s\n", getenv("PWD")); - if ((upload_list = getenv("PWD")) != NULL) - { - fprintf(stdout, "Current working dir: %s\n", upload_list); - strcat(upload_list, "/output.txt"); - fprintf(stdout, "Current working dir: %s\n", upload_list); - if(file_exists(cwd) == CLI_VALID_REGULAR_FILE) - { - printf("YAHOO>>>>>>\n\n\n"); - } - } - else - { - perror("getcwd() error"); - goto end_program; - } - - #if 1 - //strcat(upload_list, "/output1.txt"); - printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); - if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) - { - /* start reading one file at a time and upload the files */ - FILE *file = fopen ( upload_list, "r" ); - - if (file != NULL) - { - char line [256][256]; - char *temp; - int i = 0x00; - memset(line, 0x00, sizeof(line)); - cli_state->file_name = upload_list; - printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); - printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); - /* read a line from a file */ - while(fgets(line[i],sizeof(line), file)!= NULL) - { - i++; - } - cli_state->total_files = i; - if(cli_state->total_files > 0x00) - { - cli_state->curr_up_file = 0x01; - } - else - { - cli_state->curr_up_file = 0x00; - } - printf("[%s][%d] total upload files = %d\n", __FUNCTION__, __LINE__, cli_state->total_files); - printf("[%s][%d] upload cur up file# = %d\n", __FUNCTION__, __LINE__, cli_state->curr_up_file); - fclose(file); - } - else - { - /* print the error message on stderr. */ - perror(upload_list); - } - } + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + num_of_tokens = validate_cmd_tokenize(path, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } + printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); + path = bucket_name; + bucket_name = token[1]; + file_name = token[2]; - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - printf("[%s][%d] bucket id = %s\n", __FUNCTION__, __LINE__, cli_state->bucket_id); + printf("KSA[%s][%d] file path = %s\n",__FUNCTION__,__LINE__, path); + if (!bucket_name || !file_name || !path) + { + printf("Missing arguments: storj cp [-rR]storj:/// \n"); + status = 1; + goto end_program; + } + else + { + cli_state->curr_cmd_req = "download-file"; + cli_state->bucket_name = bucket_name; + cli_state->file_name = file_name; + cli_state->file_path = path; + printf("KSA[%s][%d] cmd = %s, bucket name = %s, file_name = %s, file path = %s\n", + __FUNCTION__, __LINE__,cli_state->curr_cmd_req, cli_state->bucket_name, cli_state->file_name, cli_state->file_path ); if(!cli_state->bucket_id) { storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } - #endif - break; - - default: - break; - }/* switch - case */ + } + } + else + { + /* upload-file command */ + printf("KSA[%s][%d] upload file command \n\n", __FUNCTION__, __LINE__ ); + if(cli_upload_file(path,bucket_name,cli_state) < 0x00) + { + goto end_program; + } + } } else if (strcmp(command, "upload-file") == 0) { @@ -2103,3 +1994,174 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) } } } + +static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state) +{ + int num_of_tokens = 0x00; + char *token[10]; /* Max 9 directories supported */ + + memset(token, 0x00, sizeof(token)); + printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); + printf("KSA:[%s][%d] upload path = %s\n", __FUNCTION__, __LINE__, bucket_name); + + int file_exist_status = file_exists(path); + printf("KSA[%s][%d] file_exist_status = %d\n", + __FUNCTION__, __LINE__, file_exist_status); + const char *file_name = NULL; + char cwd[1024]; + char *upload_list = cwd; + memset(upload_list, 0x00, sizeof(cwd)); + switch(file_exist_status) + { + case CLI_UNKNOWN_FILE_ATTR: + case CLI_NO_SUCH_FILE_OR_DIR: + printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); + printf("Invalid filename \n"); + break; + + case CLI_VALID_REGULAR_FILE: + file_name = get_filename_separator(path); + printf("KSA:[%s][%d] file name = %s\n", __FUNCTION__, __LINE__,file_name); + + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } + + cli_state->total_files = 0x00; + cli_state->curr_up_file = 0x00; + switch (num_of_tokens) + { + case 0x03: /* local filename and upload filename are valid names */ + if ((strcmp(file_name, token[2]) == 0x00) || + (strcmp(token[2], ".") == 0x00)) + { + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = path; + printf("[%d] target file name = %s\n", __LINE__, file_name); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); + } + } + else + { + printf("KSA:[%s][%d] Invalid upload target filename - ", + __FUNCTION__, __LINE__); + printf("Use same filename as source or '.' or blank \n"); + return -1; + } + break; + + case 0x02: /* missing upload filename */ + if (token[2] == NULL) + { + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + cli_state->file_path = path; + printf("[%d] target file name = %s\n", __LINE__, file_name); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); + } + } + break; + case 0x01: + case 0x00: + default: + printf("[%s] Invalid command ... token[2]=%s \n", __FUNCTION__, token[2]); + return -1; + break; + } + break; + + case CLI_VALID_DIR: + if ((upload_list = getenv("PWD")) != NULL) + { + fprintf(stdout, "Current working dir: %s\n", upload_list); + strcat(upload_list, "/output.txt"); + fprintf(stdout, "Current working dir: %s\n", upload_list); + if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) + { + printf("KSA:[%s][%d] Upload file list exists \n", __FUNCTION__, __LINE__); + } + } + else + { + perror("getenv() error"); + return -1; + } + + printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); + if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) + { + /* start reading one file at a time and upload the files */ + FILE *file = fopen ( upload_list, "r" ); + + if (file != NULL) + { + char line [256][256]; + char *temp; + int i = 0x00; + memset(line, 0x00, sizeof(line)); + cli_state->file_name = upload_list; + printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); + printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); + /* read a line from a file */ + while(fgets(line[i],sizeof(line), file)!= NULL) + { + i++; + } + cli_state->total_files = i; + if(cli_state->total_files > 0x00) + { + cli_state->curr_up_file = 0x01; + } + else + { + cli_state->curr_up_file = 0x00; + } + printf("[%s][%d] total upload files = %d\n", __FUNCTION__, __LINE__, cli_state->total_files); + printf("[%s][%d] upload cur up file# = %d\n", __FUNCTION__, __LINE__, cli_state->curr_up_file); + fclose(file); + } + else + { + /* print the error message on stderr. */ + perror(upload_list); + } + } + + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } + + cli_state->curr_cmd_req = "upload-file"; + cli_state->bucket_name = token[1]; + printf("[%s][%d] bucket id = %s\n", __FUNCTION__, __LINE__, cli_state->bucket_id); + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); + } + break; + + default: + break; + }/* switch - case */ + + return 0; +} + +static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_state) +{ + printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); + + return 0; +} From 25baaa0abc46ef774e2de060e17ea4e585d23449 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 3 Jan 2018 09:43:39 -0500 Subject: [PATCH 22/68] new dowload-file command supported --- src/cli.c | 88 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/src/cli.c b/src/cli.c index 4a3d92f..cc052b5 100644 --- a/src/cli.c +++ b/src/cli.c @@ -208,17 +208,17 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) int ret = strpos(cmd_str, sub_str); ret == -1 ? printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret) : printf("sub string pos = %d\n", ret); - if (ret == 0x00) + if (ret == 0x00) { /* start tokenizing */ str_token[0] = strtok(cmd_str, "/"); - while (str_token[i] != NULL) + while (str_token[i] != NULL) { i++; str_token[i] = strtok(NULL, "/"); } - } - else + } + else { i = ret; } @@ -1708,11 +1708,8 @@ int main(int argc, char **argv) else if (strcmp(command, "cp") == 0) { int ret = 0x00; - int num_of_tokens = 0x00; - char *token[10]; /* Max 9 directories supported */ char *path = NULL; char *bucket_name = NULL; - char *file_name = NULL; /* cp command wrt to upload-file */ if(local_file_path == NULL) @@ -1726,43 +1723,13 @@ int main(int argc, char **argv) bucket_name = argv[command_index + 1]; } - if (strpos(bucket_name,"storj://") < 0x00) + if (strpos(bucket_name,"storj://") < 0x00) { /* download-file command */ - - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - num_of_tokens = validate_cmd_tokenize(path, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } - - printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); - path = bucket_name; - bucket_name = token[1]; - file_name = token[2]; - - printf("KSA[%s][%d] file path = %s\n",__FUNCTION__,__LINE__, path); - if (!bucket_name || !file_name || !path) + if(cli_download_file(bucket_name, path, cli_state) < 0x00) { - printf("Missing arguments: storj cp [-rR]storj:/// \n"); - status = 1; goto end_program; } - else - { - cli_state->curr_cmd_req = "download-file"; - cli_state->bucket_name = bucket_name; - cli_state->file_name = file_name; - cli_state->file_path = path; - printf("KSA[%s][%d] cmd = %s, bucket name = %s, file_name = %s, file path = %s\n", - __FUNCTION__, __LINE__,cli_state->curr_cmd_req, cli_state->bucket_name, cli_state->file_name, cli_state->file_path ); - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } - } } else { @@ -1998,7 +1965,7 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state) { int num_of_tokens = 0x00; - char *token[10]; /* Max 9 directories supported */ + char *token[10]; memset(token, 0x00, sizeof(token)); printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); @@ -2053,7 +2020,7 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state printf("KSA:[%s][%d] Invalid upload target filename - ", __FUNCTION__, __LINE__); printf("Use same filename as source or '.' or blank \n"); - return -1; + return -1; } break; @@ -2074,7 +2041,7 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state case 0x00: default: printf("[%s] Invalid command ... token[2]=%s \n", __FUNCTION__, token[2]); - return -1; + return -1; break; } break; @@ -2093,7 +2060,7 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state else { perror("getenv() error"); - return -1; + return -1; } printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); @@ -2161,7 +2128,42 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_state) { + /* download-file command */ + char *file_name = NULL; + int num_of_tokens = 0x00; + char *token[10]; + + memset(token, 0x00, sizeof(token)); + printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); + printf("KSA:[%s][%d] download path = %s\n", __FUNCTION__, __LINE__, bucket_name); printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); + for(int j = 0x00; j < num_of_tokens; j++) + { + printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); + } + + printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); + cli_state->curr_cmd_req = "download-file"; + cli_state->bucket_name = token[1]; + cli_state->file_name = token[2]; + cli_state->file_path = path; + printf("KSA[%s][%d] cmd = %s, bucket name = %s, file_name = %s, file path = %s\n", + __FUNCTION__, __LINE__,cli_state->curr_cmd_req, cli_state->bucket_name, cli_state->file_name, cli_state->file_path ); + if (!cli_state->bucket_name || !cli_state->file_name || !cli_state->file_path) + { + printf("Missing arguments: storj cp [-rR] storj:/// \n"); + return -1; + } + else + { + if(!cli_state->bucket_id) + { + storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); + } + } return 0; } From 48589daab89157c5fb15ff32bdff439acf276f95 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 3 Jan 2018 16:40:44 -0500 Subject: [PATCH 23/68] support of downloading files using cp with debug info --- src/cli.c | 281 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 245 insertions(+), 36 deletions(-) diff --git a/src/cli.c b/src/cli.c index cc052b5..8cc59eb 100644 --- a/src/cli.c +++ b/src/cli.c @@ -114,6 +114,72 @@ static void print_upload_usage(char *this) exit(EXIT_FAILURE); } +static int check_file_path(char *file_path) +{ + struct stat sb; + + if (stat(file_path, &sb) == -1) + { + perror("stat"); + printf("\n\n returning 1\n\n"); + return CLI_NO_SUCH_FILE_OR_DIR; + } + + switch (sb.st_mode & S_IFMT) + { + case S_IFBLK: + printf("block device\n"); + break; + case S_IFCHR: + printf("character device\n"); + break; + case S_IFDIR: + printf("file_path = %s\n\n", file_path); + printf("directory\n"); + return CLI_VALID_DIR; + break; + case S_IFIFO: + printf("FIFO/pipe\n"); + break; + case S_IFLNK: + printf("symlink\n"); + break; + case S_IFREG: + return CLI_VALID_REGULAR_FILE; + break; + case S_IFSOCK: + printf("socket\n"); + break; + default: + printf("unknown?\n"); + break; + } + + #if ENABLE_FILE_DETAILS + printf("I-node number: %ld\n", (long)sb.st_ino); + + printf("Mode: %lo (octal)\n", + (unsigned long)sb.st_mode); + + printf("Link count: %ld\n", (long)sb.st_nlink); + printf("Ownership: UID=%ld GID=%ld\n", + (long)sb.st_uid, (long)sb.st_gid); + + printf("Preferred I/O block size: %ld bytes\n", + (long)sb.st_blksize); + printf("File size: %lld bytes\n", + (long long)sb.st_size); + printf("Blocks allocated: %lld\n", + (long long)sb.st_blocks); + + printf("Last status change: %s", ctime(&sb.st_ctime)); + printf("Last file access: %s", ctime(&sb.st_atime)); + printf("Last file modification: %s", ctime(&sb.st_mtime)); + #endif + + return CLI_UNKNOWN_FILE_ATTR; +} + static int file_exists(char *file_path) { struct stat sb; @@ -645,9 +711,11 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, static void download_file_complete(int status, FILE *fd, void *handle) { + cli_state_t *cli_state = handle; printf("\n"); fclose(fd); - if (status) { + if (status) + { // TODO send to stderr switch(status) { case STORJ_FILE_DECRYPTION_ERROR: @@ -659,10 +727,18 @@ static void download_file_complete(int status, FILE *fd, void *handle) printf("Download failure: %s\n", storj_strerror(status)); } - exit(status); + //exit(status); } - printf("Download Success!\n"); - exit(0); + else + { + printf("Download Success!\n"); + } + + if(cli_state->total_files == 0x00) + { + exit(0); + } + queue_next_cli_cmd(handle); } void download_signal_handler(uv_signal_t *req, int signum) @@ -676,7 +752,7 @@ void download_signal_handler(uv_signal_t *req, int signum) } static int download_file(storj_env_t *env, char *bucket_id, - char *file_id, char *path) + char *file_id, char *path, void *handle) { FILE *fd = NULL; @@ -722,7 +798,7 @@ static int download_file(storj_env_t *env, char *bucket_id, } storj_download_state_t *state = storj_bridge_resolve_file(env, bucket_id, - file_id, fd, NULL, + file_id, fd, handle, progress_cb, download_file_complete); if (!state) { @@ -1038,38 +1114,96 @@ static void list_files_callback(uv_work_t *work_req, int status) printf("No files for bucket.\n"); } + #if 0 + char cwd[1024]; + memset(cwd, 0x00, sizeof(cwd)); + char *dwnld_list_ = cwd; + + if ((dwnld_list = getenv("PWD")) != NULL) + { + fprintf(stdout, "Current working dir: %s\n", dwnld_list); + strcat(dwnld_list, "/download_list.txt"); + fprintf(stdout, "Current working dir: %s\n", dwnld_list); + if(file_exists(dwnl_list) == CLI_VALID_REGULAR_FILE) + { + printf("KSA:[%s][%d] Upload file list exists \n", __FUNCTION__, __LINE__); + } + } + else + { + perror("getenv() error"); + return -1; + } + #endif + + FILE *dwnld_list_fd = stdout; + cli_state->file_id = NULL; + + if (strcmp(cli_state->file_name,"*") == 0x00) + { + if ((dwnld_list_fd = fopen("dwnld_list.txt", "w")) == NULL) + { + printf("[%s][%d] Unable to create download list file \n", + __FUNCTION__, __LINE__); + goto cleanup; + } + /* total number of files available in that bucket */ + cli_state->total_files = req->total_files; + } + for (int i = 0; i < req->total_files; i++) { storj_file_meta_t *file = &req->files[i]; + /* print to screen */ + fprintf(stdout, "ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", + file->id, + file->size, + file->decrypted ? "true" : "false", + file->mimetype, + file->created, + file->filename); + + + /* print to file */ + if (dwnld_list_fd != stdout) + { + fprintf(dwnld_list_fd, "%s:%s\n", + file->id, + file->filename); + } + /* get the file id of the given file name */ if(cli_state->file_name != NULL) { - if(strcmp(cli_state->file_name, file->filename) == 0x00) + //cli_state->next_cmd_req = "download-file-1"; + if (strcmp(cli_state->file_name, file->filename) == 0x00) { cli_state->file_id = (char *)file->id; cli_state->next_cmd_req = "download-file-1"; - break; + cli_state->total_files = 0x00; } } - else - { - printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", - file->id, - file->size, - file->decrypted ? "true" : "false", - file->mimetype, - file->created, - file->filename); - } } - if(strcmp(cli_state->curr_cmd_req, "download-file" ) == 0x00) + if (dwnld_list_fd != stdout) + { + fclose(dwnld_list_fd); + } + + if ((cli_state->file_id != NULL) && + (strcmp(cli_state->next_cmd_req, "download-file-1" ) == 0x00)) { + cli_state->curr_up_file = 0x01; queue_next_cli_cmd(cli_state); } + else + { + printf("cp: cannot stat '%s' : No such file or directory\n",cli_state->file_name); + } cleanup: + storj_free_list_files_request(req); free(work_req); } @@ -1723,22 +1857,32 @@ int main(int argc, char **argv) bucket_name = argv[command_index + 1]; } - if (strpos(bucket_name,"storj://") < 0x00) + printf("bucket-name = %s \n\n", bucket_name); + printf("path = %s \n\n", path); + if (bucket_name != NULL) { - /* download-file command */ - if(cli_download_file(bucket_name, path, cli_state) < 0x00) + if (strpos(bucket_name, "storj://") < 0x00) { - goto end_program; + /* download-file command */ + if(cli_download_file(bucket_name, path, cli_state) < 0x00) + { + goto end_program; + } + } + else + { + /* upload-file command */ + printf("KSA[%s][%d] upload file command \n\n", __FUNCTION__, __LINE__ ); + if(cli_upload_file(path,bucket_name,cli_state) < 0x00) + { + goto end_program; + } } } else { - /* upload-file command */ - printf("KSA[%s][%d] upload file command \n\n", __FUNCTION__, __LINE__ ); - if(cli_upload_file(path,bucket_name,cli_state) < 0x00) - { - goto end_program; - } + printf("Invalid command \n"); + goto end_program; } } else if (strcmp(command, "upload-file") == 0) @@ -1915,7 +2059,60 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) if(strcmp("download-file-1" , cli_state->next_cmd_req) == 0x00) { - download_file(cli_state->env, cli_state->bucket_id, cli_state->file_id, cli_state->file_path); + FILE *file = fopen("/home/kishore/libstorj/src/dwnld_list.txt", "r"); + if (file != NULL) + { + char line[256][256]; + char *temp; + char temp_path[1024]; + int i = 0x00; + char *token[10]; + int tk_idx= 0x00; + memset(token, 0x00, sizeof(token)); + memset(temp_path, 0x00, sizeof(temp_path)); + memset(line, 0x00, sizeof(line)); + while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ + { + temp = strrchr(line[i], '\n'); + if(temp) *temp = '\0'; + temp = line[i]; + i++; + if (i >= cli_state->curr_up_file) + { + break; + } + } + + /* start tokenizing */ + token[0] = strtok(temp, ":"); + while (token[tk_idx] != NULL) + { + tk_idx++; + token[tk_idx] = strtok(NULL, ":"); + } + + if(cli_state->curr_up_file <= cli_state->total_files) + { + cli_state->file_id = token[0]; + strcpy(temp_path, cli_state->file_path); + strcat(temp_path, token[1]); + fprintf(stdout,"*****[%d:%d] downloading file: %s *****\n", + cli_state->curr_up_file, cli_state->total_files, temp_path); + cli_state->curr_up_file++; + download_file(cli_state->env, cli_state->bucket_id, cli_state->file_id, temp_path, cli_state); + } + else + { + fprintf(stdout,"***** done downloading files *****\n"); + fclose(file); + exit(0); + } + } + else + { + download_file(cli_state->env, cli_state->bucket_id, cli_state->file_id, cli_state->file_path,cli_state); + } + } } else if ((strcmp("upload-file" , cli_state->curr_cmd_req) == 0x00) && @@ -1956,7 +2153,6 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) else { /* handle single file upload from the command line */ - printf("handle it dudue\n\n\n"); upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path, cli_state); } } @@ -2132,11 +2328,12 @@ static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_sta char *file_name = NULL; int num_of_tokens = 0x00; char *token[10]; + int ret_status = 0x00; + printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); memset(token, 0x00, sizeof(token)); printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); printf("KSA:[%s][%d] download path = %s\n", __FUNCTION__, __LINE__, bucket_name); - printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ num_of_tokens = validate_cmd_tokenize(bucket_name, token); @@ -2156,14 +2353,26 @@ static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_sta if (!cli_state->bucket_name || !cli_state->file_name || !cli_state->file_path) { printf("Missing arguments: storj cp [-rR] storj:/// \n"); - return -1; + ret_status = -1; } else { - if(!cli_state->bucket_id) + if (strcmp(cli_state->file_name, "*") == 0x00) { - storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); + if (check_file_path(cli_state->file_path) == CLI_VALID_DIR) + { + ret_status = storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); + } + else + { + printf("cp target '%s' is not a directory\n", cli_state->file_path); + ret_status = -1; + } + } + else + { + ret_status = storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); } } - return 0; + return ret_status; } From 31fba52f50c24699245dc6b307e2a33d69efc3e6 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 3 Jan 2018 17:57:42 -0500 Subject: [PATCH 24/68] support of single file and all files download and list-files command works too --- src/cli.c | 89 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/src/cli.c b/src/cli.c index 8cc59eb..cf1be54 100644 --- a/src/cli.c +++ b/src/cli.c @@ -121,7 +121,6 @@ static int check_file_path(char *file_path) if (stat(file_path, &sb) == -1) { perror("stat"); - printf("\n\n returning 1\n\n"); return CLI_NO_SUCH_FILE_OR_DIR; } @@ -134,8 +133,6 @@ static int check_file_path(char *file_path) printf("character device\n"); break; case S_IFDIR: - printf("file_path = %s\n\n", file_path); - printf("directory\n"); return CLI_VALID_DIR; break; case S_IFIFO: @@ -189,7 +186,6 @@ static int file_exists(char *file_path) if (stat(file_path, &sb) == -1) { perror("stat"); - printf("\n\n returning 1\n\n"); return CLI_NO_SUCH_FILE_OR_DIR; } @@ -272,7 +268,10 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) int i = 0x00; /* num of tokens */ int ret = strpos(cmd_str, sub_str); - ret == -1 ? printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret) : printf("sub string pos = %d\n", ret); + if( ret == -1) + { + printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret); + } if (ret == 0x00) { @@ -1139,7 +1138,7 @@ static void list_files_callback(uv_work_t *work_req, int status) FILE *dwnld_list_fd = stdout; cli_state->file_id = NULL; - if (strcmp(cli_state->file_name,"*") == 0x00) + if ((cli_state->file_name !=NULL) && (strcmp(cli_state->file_name,"*") == 0x00)) { if ((dwnld_list_fd = fopen("dwnld_list.txt", "w")) == NULL) { @@ -1155,15 +1154,17 @@ static void list_files_callback(uv_work_t *work_req, int status) { storj_file_meta_t *file = &req->files[i]; - /* print to screen */ - fprintf(stdout, "ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", - file->id, - file->size, - file->decrypted ? "true" : "false", - file->mimetype, - file->created, - file->filename); - + if (strcmp(cli_state->curr_cmd_req,"list-files") == 0x00) + { + /* print to screen */ + fprintf(stdout, "ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", + file->id, + file->size, + file->decrypted ? "true" : "false", + file->mimetype, + file->created, + file->filename); + } /* print to file */ if (dwnld_list_fd != stdout) @@ -1176,9 +1177,15 @@ static void list_files_callback(uv_work_t *work_req, int status) /* get the file id of the given file name */ if(cli_state->file_name != NULL) { - //cli_state->next_cmd_req = "download-file-1"; if (strcmp(cli_state->file_name, file->filename) == 0x00) { + if(check_file_path("dwnld_list.txt") == CLI_VALID_REGULAR_FILE) + { + if (remove("dwnld_list.txt") == 0x00) + { + printf("%s file deleted \n", "dwnld_list.txt"); + } + } cli_state->file_id = (char *)file->id; cli_state->next_cmd_req = "download-file-1"; cli_state->total_files = 0x00; @@ -1190,16 +1197,22 @@ static void list_files_callback(uv_work_t *work_req, int status) { fclose(dwnld_list_fd); } - - if ((cli_state->file_id != NULL) && - (strcmp(cli_state->next_cmd_req, "download-file-1" ) == 0x00)) + + if (strcmp(cli_state->curr_cmd_req, "download-file" ) == 0x00) { cli_state->curr_up_file = 0x01; + cli_state->next_cmd_req = "download-file-1"; queue_next_cli_cmd(cli_state); } else { - printf("cp: cannot stat '%s' : No such file or directory\n",cli_state->file_name); + if(check_file_path("dwnld_list.txt") == CLI_VALID_REGULAR_FILE) + { + if (remove("dwnld_list.txt") == 0x00) + { + printf("file deleted \n\n"); + } + } } cleanup: @@ -1299,20 +1312,25 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) bucket->created, bucket->name); cli_state->bucket_id = (char *)bucket->id; - if(((strcmp(cli_state->curr_cmd_req, "list-files")) == 0x00) || - ((strcmp(cli_state->curr_cmd_req, "download-file")) == 0x00)) + if(strcmp(cli_state->curr_cmd_req, "list-files") == 0x00) + { + cli_state->next_cmd_req = "list-files-1"; + ret_status = 1; + } + else if(strcmp(cli_state->curr_cmd_req, "download-file") == 0x00) { cli_state->next_cmd_req = "list-files-1"; ret_status = 1; } - else if((strcmp(cli_state->curr_cmd_req, "upload-file")) == 0x00) + else if(strcmp(cli_state->curr_cmd_req, "upload-file") == 0x00) { cli_state->next_cmd_req = "upload-file-1"; ret_status = 1; } else { - printf("Invalid curr cmd req = %s\n", cli_state->curr_cmd_req); + printf("[%s][%d]Invalid curr cmd req = %s\n", + __FUNCTION__, __LINE__, cli_state->curr_cmd_req); ret_status = 0; } break; @@ -1526,11 +1544,7 @@ int main(int argc, char **argv) break; case 'R': case 'r': - printf( "Recursive Coping \n\n"); local_file_path = optarg; - printf("local_file_path = %s\n optarg = %s\n\n", - local_file_path, optarg); - //exit(0); break; case 'h': printf(HELP_TEXT); @@ -1857,8 +1871,6 @@ int main(int argc, char **argv) bucket_name = argv[command_index + 1]; } - printf("bucket-name = %s \n\n", bucket_name); - printf("path = %s \n\n", path); if (bucket_name != NULL) { if (strpos(bucket_name, "storj://") < 0x00) @@ -1872,7 +1884,6 @@ int main(int argc, char **argv) else { /* upload-file command */ - printf("KSA[%s][%d] upload file command \n\n", __FUNCTION__, __LINE__ ); if(cli_upload_file(path,bucket_name,cli_state) < 0x00) { goto end_program; @@ -2059,7 +2070,8 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) if(strcmp("download-file-1" , cli_state->next_cmd_req) == 0x00) { - FILE *file = fopen("/home/kishore/libstorj/src/dwnld_list.txt", "r"); + //FILE *file = fopen("/home/kishore/libstorj/src/dwnld_list.txt", "r"); + FILE *file = fopen("dwnld_list.txt", "r"); if (file != NULL) { char line[256][256]; @@ -2330,26 +2342,15 @@ static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_sta char *token[10]; int ret_status = 0x00; - printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); memset(token, 0x00, sizeof(token)); - printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); - printf("KSA:[%s][%d] download path = %s\n", __FUNCTION__, __LINE__, bucket_name); /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } - printf("KSA[%s][%d] download file command \n\n", __FUNCTION__, __LINE__ ); cli_state->curr_cmd_req = "download-file"; cli_state->bucket_name = token[1]; cli_state->file_name = token[2]; cli_state->file_path = path; - printf("KSA[%s][%d] cmd = %s, bucket name = %s, file_name = %s, file path = %s\n", - __FUNCTION__, __LINE__,cli_state->curr_cmd_req, cli_state->bucket_name, cli_state->file_name, cli_state->file_path ); if (!cli_state->bucket_name || !cli_state->file_name || !cli_state->file_path) { printf("Missing arguments: storj cp [-rR] storj:/// \n"); @@ -2365,7 +2366,7 @@ static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_sta } else { - printf("cp target '%s' is not a directory\n", cli_state->file_path); + printf("storj;// cp target '%s' is not a directory\n", cli_state->file_path); ret_status = -1; } } From a4b63d60a567cf062325456adb14e1d54a47d545 Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 9 Jan 2018 11:03:15 -0500 Subject: [PATCH 25/68] Cleaned up the 'get-bucket-id' command --- src/cli.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cli.c b/src/cli.c index cf1be54..b108117 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1327,7 +1327,10 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) cli_state->next_cmd_req = "upload-file-1"; ret_status = 1; } - else + else if(strcmp(cli_state->curr_cmd_req, "get-bucket-id") == 0x00) + { + ret_status = 0; + }else { printf("[%s][%d]Invalid curr cmd req = %s\n", __FUNCTION__, __LINE__, cli_state->curr_cmd_req); From cda97c61f0c56159e0a73329a57a39308adc5339 Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 9 Jan 2018 14:22:04 -0500 Subject: [PATCH 26/68] Release Notes 01/09/2018 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Supported CLI commands ./storj get-bucket-id Example: ./storj get-bucket-id testbucket ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket ./storj upload-file Example: ./storj upload-file testbucket3 uploader.lo ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 [======================================================================] 100.00% Upload Success! File ID: b8c4543226e9c65f61aa0141 ./storj download-file Example: Local file name not given ./storj download-file testbucket3 uploader.o /tmp/. ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 stat: No such file or directory Warning: File already exists at path [/tmp/.]. Would you like to overwrite [/tmp/.]: [y/n] y Unable to open /tmp/.: Is a directory Example: Local file name given ./storj download-file testbucket3 uploader.o /tmp/uploader.ks ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 [======================================================================] 100.00% Download Success! Example: Downloading with a filename that already exists locally ./storj download-file testbucket3 uploader.o /tmp/uploader.ks ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 Warning: File already exists at path [/tmp/uploader.ks]. Would you like to overwrite [/tmp/uploader.ks]: [y/n] y [======================================================================] 100.00% Download Success! ./storj list-files Example: List files in a bucket using bucket name ./storj list-files testbucket3 ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 ID: f10d046334eea2e7a6ba9723 Size: 1448 bytes Decrypted: true Type: application/octet-stream Created: 2017-12-22T13:57:52.415Z Name: testjson.json ID: 0f58c36885553516ccb63141 Size: 9200 bytes Decrypted: true Type: application/octet-stream Created: 2018-01-09T16:53:18.496Z Name: teset1 ID: 190dc667ba33a72b00e3dbb7 Size: 16528 bytes Decrypted: true Type: application/octet-stream Created: 2018-01-09T17:30:28.090Z Name: rs.o ID: f25ae0dde4fb7b3559e8f089 Size: 94808 bytes Decrypted: true Type: application/octet-stream Created: 2018-01-09T17:31:03.138Z Name: uploader.o ID: b8c4543226e9c65f61aa0141 Size: 279 bytes Decrypted: true Type: application/octet-stream Created: 2018-01-09T17:32:02.076Z Name: uploader.lo ./storj cp storj:/// ./storj cp storj:///. ./storj cp storj:/// Example: uploading a file using ‘cp’ command. The target file name should be same as the upload file name, or ‘.’ or blank(no target file name) Example: Using uploaded file name same as uploading same file ./storj cp /tmp/uploader.ksa storj://testbucket/uploader.ksa ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket [======================================================================] 100.00% Upload Success! File ID: 3092473ebc48a2d469a4a59d Example: Using uploaded file name same as uploading same file, trying to upload a file already uploaded ./storj cp uploader.o storj://testbucket3/uploader.o ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 Preparing File... Upload failure: File already exists Example: Uploading a file to a different bucket ./storj cp uploader.o storj://testbucket2/uploader.o ID: 725131c74690150e7b0cc747 Decrypted: true Created: 2017-12-18T16:41:41.583Z Name: testbucket2 [======================================================================] 100.00% Upload Success! File ID: 6b82f6030fc688336190d768 Example: Using uploaded file name is entered as ‘.’ ./storj cp http.o storj://testbucket3/. ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 [======================================================================] 100.00% Upload Success! File ID: 65ac6ea61a175d06bcfa7567 Example: Missing uploaded file name (no file name entered or is blank) ./storj cp rs.lo storj://testbucket3/ [2212] target file name = rs.lo ID: 965efd4b5d65fd82643f5727 Decrypted: true Created: 2017-12-18T18:14:05.098Z Name: testbucket3 [======================================================================] 100.00% Upload Success! File ID: fcd43a40f4543366d09ddb5c ./storj cp storj:/// Example: Downloaded file (teset1) same as the downloading file(teset1) ./storj cp storj://testbucket/teset1 /tmp/teset1 ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket [======================================================================] 100.00% Download Success! Example: Downloaded file(teset2) different from downloading file(teset1) ./storj cp storj://testbucket/teset1 /tmp/teset2 ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket [======================================================================] 100.00% Download Success! Example: Downloading same as above. The command prompts to overwrite? ./storj cp storj://testbucket/teset1 /tmp/teset2 ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket Warning: File already exists at path [/tmp/teset2]. Would you like to overwrite [/tmp/teset2]: [y/n] y [======================================================================] 100.00% Download Success! Example: Downloaded file name not given ./storj cp storj://testbucket/teset1 /tmp/ ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket Warning: File already exists at path [/tmp/]. Would you like to overwrite [/tmp/]: [y/n] y Unable to open /tmp/: Is a directory Example: Downloaded file name is ‘.’ ./storj cp storj://testbucket/teset1 /tmp/. ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket Warning: File already exists at path [/tmp/.]. Would you like to overwrite [/tmp/.]: [y/n] y Unable to open /tmp/.: Is a directory Example: Downloaded file name is ‘*’-- DONT USE THIS COMMAND. It will overwrite the first file in the directory and renames it to the downloading file name. ./storj cp storj://testbucket/http.o /tmp/* ID: 6cb2014a988adca6ed712577 Decrypted: true Created: 2018-01-04T16:55:29.832Z Name: testbucket Warning: File already exists at path [/tmp/Aac.o]. Would you like to overwrite [/tmp/Aac.o]: [y/n] y [======================================================================] 100.00% Download Success! --- src/cli.c | 90 +++++++++++++++++++------------------------------------ 1 file changed, 30 insertions(+), 60 deletions(-) diff --git a/src/cli.c b/src/cli.c index b108117..616c634 100644 --- a/src/cli.c +++ b/src/cli.c @@ -69,14 +69,17 @@ static inline void noop() {}; "encryption keys\n\n" \ "working with buckets and files\n" \ " list-buckets\n" \ - " list-files \n" \ + " get-bucket-id \n" \ + " list-files \n" \ " remove-file \n" \ " add-bucket \n" \ " remove-bucket \n" \ " list-mirrors \n\n" \ "downloading and uploading files\n" \ - " upload-file \n" \ - " download-file \n" \ + " upload-file \n" \ + " cp storj:///" \ + " download-file \n" \ + " cp storj:/// " \ "bridge api information\n" \ " get-info\n\n" \ "options:\n" \ @@ -207,7 +210,6 @@ static int file_exists(char *file_path) } printdir(file_path, 0, out_fd); fclose(out_fd); - printf("KSA[%s][%d] regular dir\n", __FUNCTION__, __LINE__); return CLI_VALID_DIR; break; case S_IFIFO: @@ -217,7 +219,6 @@ static int file_exists(char *file_path) printf("symlink\n"); break; case S_IFREG: - printf("KSA[%s][%d] regular file\n", __FUNCTION__, __LINE__); return CLI_VALID_REGULAR_FILE; break; case S_IFSOCK: @@ -270,7 +271,7 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) int ret = strpos(cmd_str, sub_str); if( ret == -1) { - printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret); + printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret); } if (ret == 0x00) @@ -713,7 +714,7 @@ static void download_file_complete(int status, FILE *fd, void *handle) cli_state_t *cli_state = handle; printf("\n"); fclose(fd); - if (status) + if (status) { // TODO send to stderr switch(status) { @@ -1113,28 +1114,6 @@ static void list_files_callback(uv_work_t *work_req, int status) printf("No files for bucket.\n"); } - #if 0 - char cwd[1024]; - memset(cwd, 0x00, sizeof(cwd)); - char *dwnld_list_ = cwd; - - if ((dwnld_list = getenv("PWD")) != NULL) - { - fprintf(stdout, "Current working dir: %s\n", dwnld_list); - strcat(dwnld_list, "/download_list.txt"); - fprintf(stdout, "Current working dir: %s\n", dwnld_list); - if(file_exists(dwnl_list) == CLI_VALID_REGULAR_FILE) - { - printf("KSA:[%s][%d] Upload file list exists \n", __FUNCTION__, __LINE__); - } - } - else - { - perror("getenv() error"); - return -1; - } - #endif - FILE *dwnld_list_fd = stdout; cli_state->file_id = NULL; @@ -1149,7 +1128,7 @@ static void list_files_callback(uv_work_t *work_req, int status) /* total number of files available in that bucket */ cli_state->total_files = req->total_files; } - + for (int i = 0; i < req->total_files; i++) { storj_file_meta_t *file = &req->files[i]; @@ -1167,7 +1146,7 @@ static void list_files_callback(uv_work_t *work_req, int status) } /* print to file */ - if (dwnld_list_fd != stdout) + if (dwnld_list_fd != stdout) { fprintf(dwnld_list_fd, "%s:%s\n", file->id, @@ -1179,9 +1158,10 @@ static void list_files_callback(uv_work_t *work_req, int status) { if (strcmp(cli_state->file_name, file->filename) == 0x00) { - if(check_file_path("dwnld_list.txt") == CLI_VALID_REGULAR_FILE) + if((dwnld_list_fd != stdout) && + (check_file_path("dwnld_list.txt") == CLI_VALID_REGULAR_FILE)) { - if (remove("dwnld_list.txt") == 0x00) + if (remove("dwnld_list.txt") == 0x00) { printf("%s file deleted \n", "dwnld_list.txt"); } @@ -1193,11 +1173,11 @@ static void list_files_callback(uv_work_t *work_req, int status) } } - if (dwnld_list_fd != stdout) + if (dwnld_list_fd != stdout) { fclose(dwnld_list_fd); } - + if (strcmp(cli_state->curr_cmd_req, "download-file" ) == 0x00) { cli_state->curr_up_file = 0x01; @@ -1208,7 +1188,7 @@ static void list_files_callback(uv_work_t *work_req, int status) { if(check_file_path("dwnld_list.txt") == CLI_VALID_REGULAR_FILE) { - if (remove("dwnld_list.txt") == 0x00) + if (remove("dwnld_list.txt") == 0x00) { printf("file deleted \n\n"); } @@ -1332,7 +1312,7 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) ret_status = 0; }else { - printf("[%s][%d]Invalid curr cmd req = %s\n", + printf("[%s][%d]Invalid curr cmd req = %s\n", __FUNCTION__, __LINE__, cli_state->curr_cmd_req); ret_status = 0; } @@ -1874,7 +1854,7 @@ int main(int argc, char **argv) bucket_name = argv[command_index + 1]; } - if (bucket_name != NULL) + if (bucket_name != NULL) { if (strpos(bucket_name, "storj://") < 0x00) { @@ -1917,6 +1897,11 @@ int main(int argc, char **argv) { cli_state->curr_cmd_req = command; cli_state->bucket_name = bucket_name; + if(check_file_path(path) != CLI_VALID_REGULAR_FILE) + { + status = 1; + goto end_program; + } cli_state->file_path = path; if(!cli_state->bucket_id) { @@ -2075,7 +2060,7 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) { //FILE *file = fopen("/home/kishore/libstorj/src/dwnld_list.txt", "r"); FILE *file = fopen("dwnld_list.txt", "r"); - if (file != NULL) + if (file != NULL) { char line[256][256]; char *temp; @@ -2092,7 +2077,7 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) if(temp) *temp = '\0'; temp = line[i]; i++; - if (i >= cli_state->curr_up_file) + if (i >= cli_state->curr_up_file) { break; } @@ -2111,7 +2096,7 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) cli_state->file_id = token[0]; strcpy(temp_path, cli_state->file_path); strcat(temp_path, token[1]); - fprintf(stdout,"*****[%d:%d] downloading file: %s *****\n", + fprintf(stdout,"*****[%d:%d] downloading file: %s *****\n", cli_state->curr_up_file, cli_state->total_files, temp_path); cli_state->curr_up_file++; download_file(cli_state->env, cli_state->bucket_id, cli_state->file_id, temp_path, cli_state); @@ -2133,9 +2118,7 @@ static void queue_next_cli_cmd(cli_state_t *cli_state) else if ((strcmp("upload-file" , cli_state->curr_cmd_req) == 0x00) && (strcmp("upload-file-1", cli_state->next_cmd_req) == 0x00)) { - FILE *file = fopen(cli_state->file_name, "r"); - printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); if (file != NULL) { char line[256][256]; @@ -2179,12 +2162,8 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state char *token[10]; memset(token, 0x00, sizeof(token)); - printf("KSA:[%s][%d] local file path = %s\n", __FUNCTION__, __LINE__, path); - printf("KSA:[%s][%d] upload path = %s\n", __FUNCTION__, __LINE__, bucket_name); int file_exist_status = file_exists(path); - printf("KSA[%s][%d] file_exist_status = %d\n", - __FUNCTION__, __LINE__, file_exist_status); const char *file_name = NULL; char cwd[1024]; char *upload_list = cwd; @@ -2193,22 +2172,15 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state { case CLI_UNKNOWN_FILE_ATTR: case CLI_NO_SUCH_FILE_OR_DIR: - printf("KSA:[%s] file path = %s\n", __FUNCTION__, path); + printf("[%s][%d] file path = %s\n", __FUNCTION__, __LINE__, path); printf("Invalid filename \n"); break; case CLI_VALID_REGULAR_FILE: file_name = get_filename_separator(path); - printf("KSA:[%s][%d] file name = %s\n", __FUNCTION__, __LINE__,file_name); /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } - cli_state->total_files = 0x00; cli_state->curr_up_file = 0x00; switch (num_of_tokens) @@ -2220,7 +2192,6 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state cli_state->curr_cmd_req = "upload-file"; cli_state->bucket_name = token[1]; cli_state->file_path = path; - printf("[%d] target file name = %s\n", __LINE__, file_name); if(!cli_state->bucket_id) { storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); @@ -2228,8 +2199,7 @@ static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state } else { - printf("KSA:[%s][%d] Invalid upload target filename - ", - __FUNCTION__, __LINE__); + printf("Invalid upload target filename - "); printf("Use same filename as source or '.' or blank \n"); return -1; } @@ -2361,9 +2331,9 @@ static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_sta } else { - if (strcmp(cli_state->file_name, "*") == 0x00) + if (strcmp(cli_state->file_name, "*") == 0x00) { - if (check_file_path(cli_state->file_path) == CLI_VALID_DIR) + if (check_file_path(cli_state->file_path) == CLI_VALID_DIR) { ret_status = storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); } From da4c0b8db3eb8bd337c85c11afb3700d75b6a4e7 Mon Sep 17 00:00:00 2001 From: Kishore Date: Fri, 12 Jan 2018 12:53:39 -0500 Subject: [PATCH 27/68] Revert "Added unity" This reverts commit f0cfe08ba703d5f5563d20e34801e3c34cdfe120. From 8a22b65d9cc43d406710f3249d7d501a6db9711b Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 16 Jan 2018 20:45:02 -0500 Subject: [PATCH 28/68] added storjapi callback function calls --- src/Makefile.am | 2 +- src/cli.c | 52 ++++++++++++++++--------------------------------- src/storj.c | 44 +++++++++++++++++++++++++++++++++++++++++ src/storj.h | 38 ++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index e18a4cc..0ec354e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,7 @@ endif include_HEADERS = storj.h bin_PROGRAMS = storj -storj_SOURCES = cli.c storj.h +storj_SOURCES = cli.c storj.h storjapi_callback.c storjapi_callback.h storj_LDADD = libstorj.la if BUILD_STORJ_DLL storj_LDFLAGS = -Wall diff --git a/src/cli.c b/src/cli.c index 616c634..55685b9 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1606,6 +1606,7 @@ int main(int argc, char **argv) char *pass = NULL; char *mnemonic = NULL; cli_state_t *cli_state = NULL; + storj_api_t *storj_api = NULL; if (strcmp(command, "get-info") == 0) { printf("Storj bridge: %s\n\n", storj_bridge); @@ -1804,6 +1805,7 @@ int main(int argc, char **argv) } cli_state = malloc(sizeof(cli_state_t)); + if (!cli_state) { status = 1; goto end_program; @@ -1812,6 +1814,17 @@ int main(int argc, char **argv) cli_state->env = env; + storj_api = malloc(sizeof(storj_api_t)); + + if (!cli_state) { + status = 1; + goto end_program; + } + memset(storj_api, 0x00, sizeof(storj_api)); + + storj_api->env = env; + + if (strcmp(command, "download-file") == 0) { char *bucket_name = argv[command_index + 1]; @@ -1911,28 +1924,9 @@ int main(int argc, char **argv) } else if (strcmp(command, "list-files") == 0) { - char *bucket_id = NULL; - /* get the corresponding bucket id from the bucket name */ - char *bucket_name = argv[command_index + 1]; - - if (!bucket_name) - { - printf("Missing first argument: \n"); - status = 1; - goto end_program; - } - else - { - cli_state->curr_cmd_req = command; - cli_state->bucket_name = bucket_name; - cli_state->file_name = NULL; - cli_state->file_id = NULL; - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } - } + storj_api->bucket_name = argv[command_index + 1]; + storj_get_bucket_files(storj_api); } else if (strcmp(command, "add-bucket") == 0) { char *bucket_name = argv[command_index + 1]; @@ -1976,20 +1970,8 @@ int main(int argc, char **argv) } else if (strcmp(command, "get-bucket-id") == 0) { - char *bucket_name = argv[command_index + 1]; - if (!bucket_name) - { - printf("Missing first argument: \n"); - status = 1; - goto end_program; - } - else - { - cli_state->bucket_name = (void *)bucket_name; - cli_state->curr_cmd_req = command; - } - /* when callback returns, we store the bucket id of bucket name else null */ - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); + storj_api->bucket_name = argv[command_index + 1]; + storj_get_bucket_id(storj_api); } else if (strcmp(command, "list-mirrors") == 0) { char *bucket_id = argv[command_index + 1]; diff --git a/src/storj.c b/src/storj.c index 0c0470f..620b2a9 100644 --- a/src/storj.c +++ b/src/storj.c @@ -2,6 +2,7 @@ #include "http.h" #include "utils.h" #include "crypto.h" +#include "storjapi_callback.h" static inline void noop() {}; @@ -1535,3 +1536,46 @@ STORJ_API int storj_bridge_register(storj_env_t *env, } return uv_queue_work(env->loop, (uv_work_t*) work, json_request_worker, cb); } + +/** + * @brief Function gets the bucket id for a given bucket name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) +{ + char *bucket_name = storj_api->bucket_name; + if (!bucket_name) + { + printf("Missing : \n"); + return STORJAPI_BUCKET_NAME_MISSING_ERROR; + } + + storj_api->last_cmd_req = NULL; + storj_api->next_cmd_req = NULL; + storj_api->curr_cmd_req = "get-bucket-id-req"; + storj_api->excp_cmd_resp = "get-bucket-id-resp"; + + /* when callback returns, we store the bucket id of bucket name else null */ + storj_bridge_get_buckets(storj_api->env, storj_api, get_bucket_id_callback); +} + +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_get_bucket_files(storj_api_t *storj_api) +{ + storj_get_bucket_id(storj_api); + storj_api->next_cmd_req = "list-files-req"; +} diff --git a/src/storj.h b/src/storj.h index abf189d..6aee13b 100755 --- a/src/storj.h +++ b/src/storj.h @@ -101,6 +101,9 @@ extern "C" { // Miscellaneous errors #define STORJ_HEX_DECODE_ERROR 7000 +// Storj API related errors +#define STORJAPI_BUCKET_NAME_MISSING_ERROR 8000 + // Exchange report codes #define STORJ_REPORT_SUCCESS 1000 #define STORJ_REPORT_FAILURE 1100 @@ -542,6 +545,29 @@ typedef struct { int pending_work_count; } storj_upload_state_t; +/** + * @brief A Structure for passing the User's Application info to + * Storj API. + */ +typedef struct storj_api { + storj_env_t *env; + char *bucket_name; + char *bucket_id; + char *file_name; + char *file_id; + char *src_info; /**< next file ready to upload */ + char *dst_file; /**< next file ready to upload */ + int total_files; /**< total files to upload */ + char *curr_cmd_req; /**< cli curr command requested */ + char *next_cmd_req; /**< cli curr command requested */ + char *excp_cmd_resp; /**< expected cmd response */ + char *rcvd_cmd_resp; /**< received cmd response */ + char *last_cmd_req; /**< last command requested */ + int error_status; /**< command response/error status */ + storj_log_levels_t *log; + void *handle; +} storj_api_t; + /** * @brief Initialize a Storj environment * @@ -998,6 +1024,18 @@ STORJ_API int storj_bridge_register(storj_env_t *env, void *handle, uv_after_work_cb cb); +/** + * @brief Get bucket id by bucket name + * + * @param[in] env The storj environment struct + * @param[in] email the user's email + * @param[in] password the user's password + * @param[in] handle A pointer that will be available in the callback + * @param[in] cb A function called with response when complete + * @return A non-zero error value on failure and 0 on success. + */ +STORJ_API int storj_get_bucket_id(storj_api_t *storj_api); + static inline char separator() { #ifdef _WIN32 From 0f90173c373d6fee3edd3c0dd6a43e833243f965 Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 16 Jan 2018 20:45:19 -0500 Subject: [PATCH 29/68] added storjapi callback function calls --- src/storjapi_callback.c | 231 ++++++++++++++++++++++++++++++++++++++++ src/storjapi_callback.h | 61 +++++++++++ 2 files changed, 292 insertions(+) create mode 100644 src/storjapi_callback.c create mode 100755 src/storjapi_callback.h diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c new file mode 100644 index 0000000..5f8763d --- /dev/null +++ b/src/storjapi_callback.c @@ -0,0 +1,231 @@ +#include "storjapi_callback.h" + + +void get_bucket_id_callback(uv_work_t *work_req, int status) +{ + int ret_status = 0x00; + assert(status == 0); + get_buckets_request_t *req = work_req->data; + storj_api_t *storj_api = req->handle; + + storj_api->last_cmd_req = storj_api->curr_cmd_req; + storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + + if (req->status_code == 401) { + printf("Invalid user credentials.\n"); + } else if (req->status_code != 200 && req->status_code != 304) { + printf("Request failed with status code: %i\n", req->status_code); + } else if (req->total_buckets == 0) { + printf("No buckets.\n"); + } + + for (int i = 0; i < req->total_buckets; i++) + { + storj_bucket_meta_t *bucket = &req->buckets[i]; + + if(strcmp(storj_api->bucket_name, bucket->name) == 0x00) + { + printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", + bucket->id, bucket->decrypted ? "true" : "false", + bucket->created, bucket->name); + + /* store the bucket id */ + storj_api->bucket_id = (char *)bucket->id; + + break; + } + else + { + if (i >= (req->total_buckets -1)) + { + printf("Invalid bucket name. \n"); + } + } + } + + queue_next_cmd_req(storj_api); + + storj_free_get_buckets_request(req); + free(work_req); +} + + +void list_files_callback(uv_work_t *work_req, int status) +{ + int ret_status = 0; + assert(status == 0); + list_files_request_t *req = work_req->data; + storj_api_t *storj_api = req->handle; + + storj_api->last_cmd_req = storj_api->curr_cmd_req; + storj_api->rcvd_cmd_resp = "list-files-resp"; + + if (req->status_code == 404) { + printf("Bucket id [%s] does not exist\n", req->bucket_id); + goto cleanup; + } else if (req->status_code == 400) { + printf("Bucket id [%s] is invalid\n", req->bucket_id); + goto cleanup; + } else if (req->status_code == 401) { + printf("Invalid user credentials.\n"); + goto cleanup; + } else if (req->status_code != 200) { + printf("Request failed with status code: %i\n", req->status_code); + } + + if (req->total_files == 0) { + printf("No files for bucket.\n"); + goto cleanup; + } + + for (int i = 0; i < req->total_files; i++) + { + storj_file_meta_t *file = &req->files[i]; + + printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", + file->id, + file->size, + file->decrypted ? "true" : "false", + file->mimetype, + file->created, + file->filename); + } + + queue_next_cmd_req(storj_api); + +cleanup: + + storj_free_list_files_request(req); + free(work_req); +} + +void queue_next_cmd_req(storj_api_t *storj_api) +{ + void *handle = storj_api->handle; + + if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) + { + printf("expt resp = %s; rcvd resp = %s \n",storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); + printf("last cmd = %s; cur cmd = %s; next cmd = %s\n", + storj_api->last_cmd_req, storj_api->curr_cmd_req, storj_api->next_cmd_req); + if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) + { + storj_api->last_cmd_req = storj_api->curr_cmd_req; + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = NULL; + storj_api->excp_cmd_resp = "list-files-resp"; + storj_bridge_list_files(storj_api->env, storj_api->bucket_id, storj_api, list_files_callback); + } + } + +#if 0 + if (((strcmp("list-files" , storj_api->curr_cmd_req) == 0x00)|| + ((strcmp("download-file" , storj_api->curr_cmd_req) == 0x00))) && + ((strcmp("list-files-1", storj_api->next_cmd_req) == 0x00)|| + (strcmp("download-file-1", storj_api->next_cmd_req)==0x00))) + { + if(strcmp("list-files-1" , storj_api->next_cmd_req) == 0x00) + { + storj_bridge_list_files(storj_api->env, storj_api->bucket_id, storj_api, list_files_callback); + } + + if(strcmp("download-file-1" , storj_api->next_cmd_req) == 0x00) + { + //FILE *file = fopen("/home/kishore/libstorj/src/dwnld_list.txt", "r"); + FILE *file = fopen("dwnld_list.txt", "r"); + if (file != NULL) + { + char line[256][256]; + char *temp; + char temp_path[1024]; + int i = 0x00; + char *token[10]; + int tk_idx= 0x00; + memset(token, 0x00, sizeof(token)); + memset(temp_path, 0x00, sizeof(temp_path)); + memset(line, 0x00, sizeof(line)); + while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ + { + temp = strrchr(line[i], '\n'); + if(temp) *temp = '\0'; + temp = line[i]; + i++; + if (i >= storj_api->curr_up_file) + { + break; + } + } + + /* start tokenizing */ + token[0] = strtok(temp, ":"); + while (token[tk_idx] != NULL) + { + tk_idx++; + token[tk_idx] = strtok(NULL, ":"); + } + + if(storj_api->curr_up_file <= storj_api->total_files) + { + storj_api->file_id = token[0]; + strcpy(temp_path, storj_api->file_path); + strcat(temp_path, token[1]); + fprintf(stdout,"*****[%d:%d] downloading file: %s *****\n", + storj_api->curr_up_file, storj_api->total_files, temp_path); + storj_api->curr_up_file++; + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); + } + else + { + fprintf(stdout,"***** done downloading files *****\n"); + fclose(file); + exit(0); + } + } + else + { + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, storj_api->file_path,storj_api); + } + + } + } + else if ((strcmp("upload-file" , storj_api->curr_cmd_req) == 0x00) && + (strcmp("upload-file-1", storj_api->next_cmd_req) == 0x00)) + { + FILE *file = fopen(storj_api->file_name, "r"); + if (file != NULL) + { + char line[256][256]; + char *temp; + int i = 0x00; + memset(line, 0x00, sizeof(line)); + while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ + { + temp = strrchr(line[i], '\n'); + if(temp) *temp = '\0'; + storj_api->file_path = line[i]; + i++; + printf("[%s][%d] [index = %d] target file name = %s\n", __FUNCTION__, __LINE__, i, line[i-1]); + if(i >= storj_api->curr_up_file) + break; + } + if(storj_api->curr_up_file <= storj_api->total_files) + { + fprintf(stdout,"*****uploading file: %s *****\n",line[i-1]); //print the file contents on stdout. + upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_path, storj_api); + storj_api->curr_up_file++; + } + else + { + fprintf(stdout,"***** done uploading files *****\n"); + fclose(file); + exit(0); + } + } + else + { + /* handle single file upload from the command line */ + upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_path, storj_api); + } + } + #endif +} diff --git a/src/storjapi_callback.h b/src/storjapi_callback.h new file mode 100755 index 0000000..b373818 --- /dev/null +++ b/src/storjapi_callback.h @@ -0,0 +1,61 @@ +/** + * @file storjapi_callback.h + * @brief Storj callback library. + * + * Implements callback functionality that can be customised for + * end user's application + */ + +#ifndef STORJAPI_CALLBACK_H +#define STORJAPI_CALLBACK_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "storj.h" + +/** + * @brief Get bucket id by bucket name + * + * @param[in] env The storj environment struct + * @param[in] email the user's email + * @param[in] password the user's password + * @param[in] handle A pointer that will be available in the callback + * @param[in] cb A function called with response when complete + * @return A non-zero error value on failure and 0 on success. + */ +void get_bucket_id_callback(uv_work_t *work_req, int status); + +/** + * @brief Get bucket id by bucket name + * + * @param[in] env The storj environment struct + * @param[in] email the user's email + * @param[in] password the user's password + * @param[in] handle A pointer that will be available in the callback + * @param[in] cb A function called with response when complete + * @return A non-zero error value on failure and 0 on success. + */ +void list_files_callback(uv_work_t *work_req, int status); + +/** + * @brief Get bucket id by bucket name + * + * @param[in] env The storj environment struct + * @param[in] email the user's email + * @param[in] password the user's password + * @param[in] handle A pointer that will be available in the callback + * @param[in] cb A function called with response when complete + * @return A non-zero error value on failure and 0 on success. + */ +void queue_next_cmd_req(storj_api_t *storj_api); + + + + +#ifdef __cplusplus +} +#endif + +#endif /* STORJ_H */ From 1b5496143c418ce3518fd350dc19c26f4770ed9c Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 16 Jan 2018 21:20:32 -0500 Subject: [PATCH 30/68] added list-files, remove-buket storj api calls --- src/cli.c | 5 ++++ src/storj.c | 17 +++++++++++++ src/storj.h | 24 ++++++++++++++++++ src/storjapi_callback.c | 56 +++++++++++++++++++++++++++++++++++------ 4 files changed, 95 insertions(+), 7 deletions(-) diff --git a/src/cli.c b/src/cli.c index 55685b9..14077c3 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1940,6 +1940,10 @@ int main(int argc, char **argv) NULL, create_bucket_callback); } else if (strcmp(command, "remove-bucket") == 0) { + + storj_api->bucket_name = argv[command_index + 1]; + storj_remove_bucket(storj_api); + #if 0 char *bucket_id = argv[command_index + 1]; if (!bucket_id) { @@ -1950,6 +1954,7 @@ int main(int argc, char **argv) storj_bridge_delete_bucket(env, bucket_id, NULL, delete_bucket_callback); + #endif } else if (strcmp(command, "remove-file") == 0) { char *bucket_id = argv[command_index + 1]; diff --git a/src/storj.c b/src/storj.c index 620b2a9..8012a02 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1579,3 +1579,20 @@ STORJ_API int storj_get_bucket_files(storj_api_t *storj_api) storj_get_bucket_id(storj_api); storj_api->next_cmd_req = "list-files-req"; } + + +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_remove_bucket(storj_api_t *storj_api) +{ + storj_get_bucket_id(storj_api); + storj_api->next_cmd_req = "remove-bucket-req"; +} diff --git a/src/storj.h b/src/storj.h index 6aee13b..4c9a046 100755 --- a/src/storj.h +++ b/src/storj.h @@ -1036,6 +1036,30 @@ STORJ_API int storj_bridge_register(storj_env_t *env, */ STORJ_API int storj_get_bucket_id(storj_api_t *storj_api); +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_get_bucket_files(storj_api_t *storj_api); + +/** + * @brief Get bucket id by bucket name + * + * @param[in] env The storj environment struct + * @param[in] email the user's email + * @param[in] password the user's password + * @param[in] handle A pointer that will be available in the callback + * @param[in] cb A function called with response when complete + * @return A non-zero error value on failure and 0 on success. + */ +STORJ_API int storj_remove_bucket(storj_api_t *storj_api); + static inline char separator() { #ifdef _WIN32 diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 5f8763d..07e3a1a 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1,6 +1,29 @@ #include "storjapi_callback.h" +static void delete_bucket_callback(uv_work_t *work_req, int status) +{ + assert(status == 0); + json_request_t *req = work_req->data; + storj_api_t *storj_api = req->handle; + + storj_api->last_cmd_req = storj_api->curr_cmd_req; + storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + + if (req->status_code == 200 || req->status_code == 204) { + printf("Bucket was successfully removed.\n"); + } else if (req->status_code == 401) { + printf("Invalid user credentials.\n"); + } else { + printf("Failed to destroy bucket. (%i)\n", req->status_code); + } + + json_object_put(req->response); + free(req->path); + free(req); + free(work_req); +} + void get_bucket_id_callback(uv_work_t *work_req, int status) { int ret_status = 0x00; @@ -13,10 +36,13 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) if (req->status_code == 401) { printf("Invalid user credentials.\n"); + goto cleanup; } else if (req->status_code != 200 && req->status_code != 304) { printf("Request failed with status code: %i\n", req->status_code); + goto cleanup; } else if (req->total_buckets == 0) { printf("No buckets.\n"); + goto cleanup; } for (int i = 0; i < req->total_buckets; i++) @@ -39,12 +65,14 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) if (i >= (req->total_buckets -1)) { printf("Invalid bucket name. \n"); + goto cleanup; } } } queue_next_cmd_req(storj_api); +cleanup: storj_free_get_buckets_request(req); free(work_req); } @@ -75,7 +103,7 @@ void list_files_callback(uv_work_t *work_req, int status) if (req->total_files == 0) { printf("No files for bucket.\n"); - goto cleanup; + goto cleanup; } for (int i = 0; i < req->total_files; i++) @@ -105,16 +133,30 @@ void queue_next_cmd_req(storj_api_t *storj_api) if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) { - printf("expt resp = %s; rcvd resp = %s \n",storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); - printf("last cmd = %s; cur cmd = %s; next cmd = %s\n", - storj_api->last_cmd_req, storj_api->curr_cmd_req, storj_api->next_cmd_req); - if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) + printf("[%s][%d]expt resp = %s; rcvd resp = %s \n", + __FUNCTION__, __LINE__, + storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); + printf("[%s][%d]last cmd = %s; cur cmd = %s; next cmd = %s\n", + __FUNCTION__, __LINE__, storj_api->last_cmd_req, + storj_api->curr_cmd_req, storj_api->next_cmd_req); + + if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) { - storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->next_cmd_req = NULL; storj_api->excp_cmd_resp = "list-files-resp"; - storj_bridge_list_files(storj_api->env, storj_api->bucket_id, storj_api, list_files_callback); + storj_bridge_list_files(storj_api->env, storj_api->bucket_id, + storj_api, list_files_callback); + } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "remove-bucket-req") == 0x00)) + { + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = NULL; + storj_api->excp_cmd_resp = "remove-bucekt-resp"; + storj_bridge_delete_bucket(storj_api->env, storj_api->bucket_id, + storj_api, delete_bucket_callback); } } From 0d08d56eb7b8b8872735426e74dde010ad8cc8dc Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 17 Jan 2018 11:35:22 -0500 Subject: [PATCH 31/68] added remove bucket, remove file commands --- src/cli.c | 17 ++++++++++- src/storj.c | 23 +++++++++++++-- src/storj.h | 15 +++++++++- src/storjapi_callback.c | 62 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 110 insertions(+), 7 deletions(-) diff --git a/src/cli.c b/src/cli.c index 14077c3..c850a34 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1956,7 +1956,21 @@ int main(int argc, char **argv) delete_bucket_callback); #endif - } else if (strcmp(command, "remove-file") == 0) { + } + else if (strcmp(command, "remove-file") == 0) + { + storj_api->bucket_name = argv[command_index + 1]; + storj_api->file_name = argv[command_index + 2]; + + if (!storj_api->bucket_name|| !storj_api->file_name) + { + printf("Missing arguments, expected: \n"); + status = 1; + goto end_program; + } + + storj_remove_file(storj_api); + #if 0 char *bucket_id = argv[command_index + 1]; char *file_id = argv[command_index + 2]; @@ -1967,6 +1981,7 @@ int main(int argc, char **argv) } storj_bridge_delete_file(env, bucket_id, file_id, NULL, delete_file_callback); + #endif } else if (strcmp(command, "list-buckets") == 0) diff --git a/src/storj.c b/src/storj.c index 8012a02..26dd23c 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1556,8 +1556,9 @@ STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) } storj_api->last_cmd_req = NULL; - storj_api->next_cmd_req = NULL; storj_api->curr_cmd_req = "get-bucket-id-req"; + storj_api->next_cmd_req = NULL; + storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "get-bucket-id-resp"; /* when callback returns, we store the bucket id of bucket name else null */ @@ -1578,9 +1579,9 @@ STORJ_API int storj_get_bucket_files(storj_api_t *storj_api) { storj_get_bucket_id(storj_api); storj_api->next_cmd_req = "list-files-req"; + storj_api->final_cmd_req = NULL; } - /** * @brief Function gets the list of files for a given bucket * name @@ -1595,4 +1596,22 @@ STORJ_API int storj_remove_bucket(storj_api_t *storj_api) { storj_get_bucket_id(storj_api); storj_api->next_cmd_req = "remove-bucket-req"; + storj_api->final_cmd_req = NULL; } + +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_remove_file(storj_api_t *storj_api) +{ + storj_get_bucket_files(storj_api); + storj_api->final_cmd_req = "remove-file-req"; +} + diff --git a/src/storj.h b/src/storj.h index 4c9a046..a92077c 100755 --- a/src/storj.h +++ b/src/storj.h @@ -558,11 +558,12 @@ typedef struct storj_api { char *src_info; /**< next file ready to upload */ char *dst_file; /**< next file ready to upload */ int total_files; /**< total files to upload */ + char *last_cmd_req; /**< last command requested */ char *curr_cmd_req; /**< cli curr command requested */ char *next_cmd_req; /**< cli curr command requested */ + char *final_cmd_req; /**< final command in the seq */ char *excp_cmd_resp; /**< expected cmd response */ char *rcvd_cmd_resp; /**< received cmd response */ - char *last_cmd_req; /**< last command requested */ int error_status; /**< command response/error status */ storj_log_levels_t *log; void *handle; @@ -1060,6 +1061,18 @@ STORJ_API int storj_get_bucket_files(storj_api_t *storj_api); */ STORJ_API int storj_remove_bucket(storj_api_t *storj_api); +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_remove_file(storj_api_t *storj_api); + static inline char separator() { #ifdef _WIN32 diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 07e3a1a..e4b9edc 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1,12 +1,39 @@ #include "storjapi_callback.h" +static void delete_file_callback(uv_work_t *work_req, int status) +{ + assert(status == 0); + json_request_t *req = work_req->data; + + storj_api_t *storj_api = req->handle; + storj_api->last_cmd_req = storj_api->curr_cmd_req; + storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + + if (req->status_code == 200 || req->status_code == 204) { + printf("File was successfully removed from bucket.\n"); + } else if (req->status_code == 401) { + printf("Invalid user credentials.\n"); + goto cleanup; + } else { + printf("Failed to remove file from bucket. (%i)\n", req->status_code); + goto cleanup; + } + + json_object_put(req->response); + + queue_next_cmd_req(storj_api); +cleanup: + free(req->path); + free(req); + free(work_req); +} static void delete_bucket_callback(uv_work_t *work_req, int status) { assert(status == 0); json_request_t *req = work_req->data; - storj_api_t *storj_api = req->handle; + storj_api_t *storj_api = req->handle; storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; @@ -14,11 +41,16 @@ static void delete_bucket_callback(uv_work_t *work_req, int status) printf("Bucket was successfully removed.\n"); } else if (req->status_code == 401) { printf("Invalid user credentials.\n"); + goto cleanup; } else { printf("Failed to destroy bucket. (%i)\n", req->status_code); + goto cleanup; } json_object_put(req->response); + + queue_next_cmd_req(storj_api); +cleanup: free(req->path); free(req); free(work_req); @@ -110,6 +142,13 @@ void list_files_callback(uv_work_t *work_req, int status) { storj_file_meta_t *file = &req->files[i]; + if ((storj_api->file_name != NULL) && + (strcmp(storj_api->file_name,file->filename)) == 0x00) + { + /* store the file id */ + storj_api->file_id = (char *)file->id; + } + printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", file->id, file->size, @@ -144,7 +183,8 @@ void queue_next_cmd_req(storj_api_t *storj_api) (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = NULL; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "list-files-resp"; storj_bridge_list_files(storj_api->env, storj_api->bucket_id, storj_api, list_files_callback); @@ -153,11 +193,27 @@ void queue_next_cmd_req(storj_api_t *storj_api) (strcmp(storj_api->next_cmd_req, "remove-bucket-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = NULL; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "remove-bucekt-resp"; storj_bridge_delete_bucket(storj_api->env, storj_api->bucket_id, storj_api, delete_bucket_callback); } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "remove-file-req") == 0x00)) + { + printf("I am here\n"); + printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", + __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, + storj_api->bucket_name); + + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "remove-file-resp"; + storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api, delete_file_callback); + } } #if 0 From 5f43f89ea600a5b23f0791404971440f4d15b7bd Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 17 Jan 2018 11:53:07 -0500 Subject: [PATCH 32/68] added list-mirrors api command --- src/cli.c | 20 +++++++++- src/storj.c | 15 ++++++++ src/storj.h | 12 ++++++ src/storjapi_callback.c | 83 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/cli.c b/src/cli.c index c850a34..9ec1e7f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1993,7 +1993,21 @@ int main(int argc, char **argv) storj_api->bucket_name = argv[command_index + 1]; storj_get_bucket_id(storj_api); } - else if (strcmp(command, "list-mirrors") == 0) { + else if (strcmp(command, "list-mirrors") == 0) + { + storj_api->bucket_name = argv[command_index + 1]; + storj_api->file_name = argv[command_index + 2]; + + if (!storj_api->bucket_name|| !storj_api->file_name) + { + printf("Missing arguments, expected: \n"); + status = 1; + goto end_program; + } + + storj_list_mirrors(storj_api); + + #if 0 char *bucket_id = argv[command_index + 1]; char *file_id = argv[command_index + 2]; @@ -2004,7 +2018,9 @@ int main(int argc, char **argv) } storj_bridge_list_mirrors(env, bucket_id, file_id, NULL, list_mirrors_callback); - } else { + #endif + } + else { printf("'%s' is not a storj command. See 'storj --help'\n\n", command); status = 1; diff --git a/src/storj.c b/src/storj.c index 26dd23c..a9b4dec 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1615,3 +1615,18 @@ STORJ_API int storj_remove_file(storj_api_t *storj_api) storj_api->final_cmd_req = "remove-file-req"; } +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_list_mirrors(storj_api_t *storj_api) +{ + storj_get_bucket_files(storj_api); + storj_api->final_cmd_req = "list-mirrors-req"; +} diff --git a/src/storj.h b/src/storj.h index a92077c..74cadce 100755 --- a/src/storj.h +++ b/src/storj.h @@ -1073,6 +1073,18 @@ STORJ_API int storj_remove_bucket(storj_api_t *storj_api); */ STORJ_API int storj_remove_file(storj_api_t *storj_api); +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_list_mirrors(storj_api_t *storj_api); + static inline char separator() { #ifdef _WIN32 diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index e4b9edc..3e7f7da 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1,5 +1,66 @@ #include "storjapi_callback.h" +static void list_mirrors_callback(uv_work_t *work_req, int status) +{ + assert(status == 0); + json_request_t *req = work_req->data; + + storj_api_t *storj_api = req->handle; + storj_api->last_cmd_req = storj_api->curr_cmd_req; + storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + + if (req->status_code != 200) { + printf("Request failed with status code: %i\n", + req->status_code); + } + + if (req->response == NULL) { + free(req); + free(work_req); + printf("Failed to list mirrors.\n"); + exit(1); + } + + int num_mirrors = json_object_array_length(req->response); + + struct json_object *shard; + struct json_object *established; + struct json_object *available; + struct json_object *item; + struct json_object *hash; + struct json_object *contract; + struct json_object *address; + struct json_object *port; + struct json_object *node_id; + + for (int i = 0; i < num_mirrors; i++) { + shard = json_object_array_get_idx(req->response, i); + json_object_object_get_ex(shard, "established", + &established); + int num_established = + json_object_array_length(established); + for (int j = 0; j < num_established; j++) { + item = json_object_array_get_idx(established, j); + if (j == 0) { + json_object_object_get_ex(item, "shardHash", + &hash); + printf("Shard %i: %s\n", i, json_object_get_string(hash)); + } + json_object_object_get_ex(item, "contract", &contract); + json_object_object_get_ex(contract, "farmer_id", &node_id); + + const char *node_id_str = json_object_get_string(node_id); + printf("\tnodeID: %s\n", node_id_str); + } + printf("\n\n"); + } + + json_object_put(req->response); + free(req->path); + free(req); + free(work_req); +} + static void delete_file_callback(uv_work_t *work_req, int status) { assert(status == 0); @@ -214,7 +275,29 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, storj_api, delete_file_callback); } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "list-mirrors-req") == 0x00)) + { + printf("I am here\n"); + printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", + __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, + storj_api->bucket_name); + + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "list-mirrors-resp"; + storj_bridge_list_mirrors(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api, list_mirrors_callback); + } } + else + { + printf("[%s][%d]Oops !!!! expt resp = %s; rcvd resp = %s \n", + __FUNCTION__, __LINE__, + storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); + } + #if 0 if (((strcmp("list-files" , storj_api->curr_cmd_req) == 0x00)|| From 4797f777fbe7fb12bad949ddad38551b76665528 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 17 Jan 2018 12:26:26 -0500 Subject: [PATCH 33/68] cleanup the queue_next_cmd_req() statemachine function --- src/storjapi_callback.c | 86 ++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 3e7f7da..656f760 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -7,18 +7,19 @@ static void list_mirrors_callback(uv_work_t *work_req, int status) storj_api_t *storj_api = req->handle; storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + storj_api->rcvd_cmd_resp = "list-mirrors-resp"; if (req->status_code != 200) { printf("Request failed with status code: %i\n", req->status_code); + goto cleanup; } if (req->response == NULL) { free(req); free(work_req); printf("Failed to list mirrors.\n"); - exit(1); + goto cleanup; } int num_mirrors = json_object_array_length(req->response); @@ -56,6 +57,9 @@ static void list_mirrors_callback(uv_work_t *work_req, int status) } json_object_put(req->response); + + queue_next_cmd_req(storj_api); +cleanup: free(req->path); free(req); free(work_req); @@ -68,7 +72,7 @@ static void delete_file_callback(uv_work_t *work_req, int status) storj_api_t *storj_api = req->handle; storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + storj_api->rcvd_cmd_resp = "remove-file-resp"; if (req->status_code == 200 || req->status_code == 204) { printf("File was successfully removed from bucket.\n"); @@ -96,7 +100,7 @@ static void delete_bucket_callback(uv_work_t *work_req, int status) storj_api_t *storj_api = req->handle; storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + storj_api->rcvd_cmd_resp = "remove-bucket-resp"; if (req->status_code == 200 || req->status_code == 204) { printf("Bucket was successfully removed.\n"); @@ -176,8 +180,8 @@ void list_files_callback(uv_work_t *work_req, int status) int ret_status = 0; assert(status == 0); list_files_request_t *req = work_req->data; - storj_api_t *storj_api = req->handle; + storj_api_t *storj_api = req->handle; storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->rcvd_cmd_resp = "list-files-resp"; @@ -199,6 +203,7 @@ void list_files_callback(uv_work_t *work_req, int status) goto cleanup; } + storj_api->file_id = NULL; for (int i = 0; i < req->total_files; i++) { storj_file_meta_t *file = &req->files[i]; @@ -247,6 +252,7 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "list-files-resp"; + storj_bridge_list_files(storj_api->env, storj_api->bucket_id, storj_api, list_files_callback); } @@ -256,39 +262,62 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "remove-bucekt-resp"; + storj_api->excp_cmd_resp = "remove-bucket-resp"; + storj_bridge_delete_bucket(storj_api->env, storj_api->bucket_id, storj_api, delete_bucket_callback); } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "remove-file-req") == 0x00)) { - printf("I am here\n"); - printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", - __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, - storj_api->bucket_name); + if (storj_api->file_id != NULL) + { + printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", + __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, + storj_api->bucket_name); - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "remove-file-resp"; - storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api, delete_file_callback); + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "remove-file-resp"; + + storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api, delete_file_callback); + } + else + { + printf("\'%s\' file doesn't exists in \'%s\' bucket\n", + storj_api->file_name, storj_api->bucket_name); + } } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "list-mirrors-req") == 0x00)) { - printf("I am here\n"); - printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", - __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, - storj_api->bucket_name); - - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "list-mirrors-resp"; - storj_bridge_list_mirrors(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api, list_mirrors_callback); + if (storj_api->file_id != NULL) + { + printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", + __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, + storj_api->bucket_name); + + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "list-mirrors-resp"; + + storj_bridge_list_mirrors(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api, list_mirrors_callback); + } + else + { + printf("\'%s\' file doesn't exists in \'%s\' bucket\n", + storj_api->file_name, storj_api->bucket_name); + } + } + else + { + + printf("[%s][%d] **** ALL CLEAN & DONE *****\n", + __FUNCTION__, __LINE__); } } else @@ -296,6 +325,9 @@ void queue_next_cmd_req(storj_api_t *storj_api) printf("[%s][%d]Oops !!!! expt resp = %s; rcvd resp = %s \n", __FUNCTION__, __LINE__, storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); + printf("[%s][%d]last cmd = %s; cur cmd = %s; next cmd = %s\n", + __FUNCTION__, __LINE__, storj_api->last_cmd_req, + storj_api->curr_cmd_req, storj_api->next_cmd_req); } From e07cac2ad6ae7e1e1b6a200a14f445c77c1fecf3 Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 18 Jan 2018 13:00:28 -0500 Subject: [PATCH 34/68] upload-file storj api command --- src/cli.c | 26 +++++- src/storj.c | 17 ++++ src/storjapi_callback.c | 176 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+), 2 deletions(-) diff --git a/src/cli.c b/src/cli.c index 9ec1e7f..1ab3daf 100644 --- a/src/cli.c +++ b/src/cli.c @@ -583,7 +583,7 @@ static int get_password_verify(char *prompt, char *password, int count) } } -void close_signal(uv_handle_t *handle) +static void close_signal(uv_handle_t *handle) { ((void)0); } @@ -640,7 +640,7 @@ static void upload_file_complete(int status, char *file_id, void *handle) queue_next_cli_cmd(handle); } -void upload_signal_handler(uv_signal_t *req, int signum) +static void upload_signal_handler(uv_signal_t *req, int signum) { storj_upload_state_t *state = req->data; storj_bridge_store_file_cancel(state); @@ -1894,6 +1894,20 @@ int main(int argc, char **argv) } else if (strcmp(command, "upload-file") == 0) { + /* get the corresponding bucket id from the bucket name */ + storj_api->bucket_name = argv[command_index + 1]; + storj_api->file_name = argv[command_index + 2]; + + if (!storj_api->bucket_name || !storj_api->file_name) + { + printf("Missing arguments: \n"); + status = 1; + goto end_program; + } + + storj_upload_file(storj_api); + + #if 0 char *bucket_id = NULL; /* get the corresponding bucket id from the bucket name */ @@ -1921,11 +1935,19 @@ int main(int argc, char **argv) storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } } + #endif } else if (strcmp(command, "list-files") == 0) { /* get the corresponding bucket id from the bucket name */ storj_api->bucket_name = argv[command_index + 1]; + + if (!storj_api->bucket_name) + { + printf("Missing argument: \n"); + status = 1; + goto end_program; + } storj_get_bucket_files(storj_api); } else if (strcmp(command, "add-bucket") == 0) { char *bucket_name = argv[command_index + 1]; diff --git a/src/storj.c b/src/storj.c index a9b4dec..28e8105 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1630,3 +1630,20 @@ STORJ_API int storj_list_mirrors(storj_api_t *storj_api) storj_get_bucket_files(storj_api); storj_api->final_cmd_req = "list-mirrors-req"; } + +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_upload_file(storj_api_t *storj_api) +{ + storj_get_bucket_id(storj_api); + storj_api->next_cmd_req = "upload-file-req"; + storj_api->final_cmd_req = NULL; +} diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 656f760..cc8456a 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1,5 +1,170 @@ #include "storjapi_callback.h" +static inline void noop() {}; + +static const char *get_filename_separator(const char *file_path) +{ + const char *file_name = NULL; +#ifdef _WIN32 + file_name = strrchr(file_path, '\\'); + if (!file_name) { + file_name = strrchr(file_path, '/'); + } + if (!file_name && file_path) { + file_name = file_path; + } + if (!file_name) { + return NULL; + } + if (file_name[0] == '\\' || file_name[0] == '/') { + file_name++; + } +#else + file_name = strrchr(file_path, '/'); + if (!file_name && file_path) { + file_name = file_path; + } + if (!file_name) { + return NULL; + } + if (file_name[0] == '/') { + file_name++; + } +#endif + return file_name; +} + +static void close_signal(uv_handle_t *handle) +{ + ((void)0); +} + +static void upload_signal_handler(uv_signal_t *req, int signum) +{ + storj_upload_state_t *state = req->data; + storj_bridge_store_file_cancel(state); + if (uv_signal_stop(req)) { + printf("Unable to stop signal\n"); + } + uv_close((uv_handle_t *)req, close_signal); +} + +static void file_progress(double progress, + uint64_t downloaded_bytes, + uint64_t total_bytes, + void *handle) +{ + int bar_width = 70; + + if (progress == 0 && downloaded_bytes == 0) { + printf("Preparing File..."); + fflush(stdout); + return; + } + + printf("\r["); + int pos = bar_width * progress; + for (int i = 0; i < bar_width; ++i) { + if (i < pos) { + printf("="); + } else if (i == pos) { + printf(">"); + } else { + printf(" "); + } + } + printf("] %.*f%%", 2, progress * 100); + + fflush(stdout); +} + +static void upload_file_complete(int status, char *file_id, void *handle) +{ + storj_api_t *storj_api = handle; + storj_api->rcvd_cmd_resp = "upload-file-resp"; + + printf("\n"); + if (status != 0) + { + printf("Upload failure: %s\n", storj_strerror(status)); + //exit(status); + } + else + { + printf("Upload Success! File ID: %s\n", file_id); + } + + free(file_id); + + queue_next_cmd_req(storj_api); +} + +static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) +{ + FILE *fd = fopen(file_path, "r"); + + if (!fd) + { + printf("Invalid file path: %s\n", file_path); + exit(-1); + } + + const char *file_name = get_filename_separator(file_path); + + if (!file_name) + { + file_name = file_path; + } + + // Upload opts env variables: + char *prepare_frame_limit = getenv("STORJ_PREPARE_FRAME_LIMIT"); + char *push_frame_limit = getenv("STORJ_PUSH_FRAME_LIMIT"); + char *push_shard_limit = getenv("STORJ_PUSH_SHARD_LIMIT"); + char *rs = getenv("STORJ_REED_SOLOMON"); + + storj_upload_opts_t upload_opts = + { + .prepare_frame_limit = (prepare_frame_limit) ? atoi(prepare_frame_limit) : 1, + .push_frame_limit = (push_frame_limit) ? atoi(push_frame_limit) : 64, + .push_shard_limit = (push_shard_limit) ? atoi(push_shard_limit) : 64, + .rs = (!rs) ? true : (strcmp(rs, "false") == 0) ? false : true, + .bucket_id = bucket_id, + .file_name = file_name, + .fd = fd + }; + + uv_signal_t *sig = malloc(sizeof(uv_signal_t)); + if (!sig) + { + return 1; + } + uv_signal_init(env->loop, sig); + uv_signal_start(sig, upload_signal_handler, SIGINT); + + + + storj_progress_cb progress_cb = (storj_progress_cb)noop; + if (env->log_options->level == 0) + { + progress_cb = file_progress; + } + + storj_upload_state_t *state = storj_bridge_store_file(env, + &upload_opts, + handle, + progress_cb, + upload_file_complete); + + if (!state) + { + return 1; + } + + sig->data = state; + + return state->error_status; +} + static void list_mirrors_callback(uv_work_t *work_req, int status) { assert(status == 0); @@ -313,11 +478,22 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->file_name, storj_api->bucket_name); } } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "upload-file-req") == 0x00)) + { + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "upload-file-resp"; + + upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_name, storj_api); + } else { printf("[%s][%d] **** ALL CLEAN & DONE *****\n", __FUNCTION__, __LINE__); + exit(0); } } else From b13c1c42bddfc87052c184547fbc2cfe7cab4591 Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 18 Jan 2018 16:31:02 -0500 Subject: [PATCH 35/68] code clean up and download-file storj api command added --- src/cli.c | 19 ++++- src/storj.c | 17 ++++ src/storj.h | 32 +++++++- src/storjapi_callback.c | 176 +++++++++++++++++++++++++++++++++------- 4 files changed, 211 insertions(+), 33 deletions(-) diff --git a/src/cli.c b/src/cli.c index 1ab3daf..cc2cdee 100644 --- a/src/cli.c +++ b/src/cli.c @@ -741,7 +741,7 @@ static void download_file_complete(int status, FILE *fd, void *handle) queue_next_cli_cmd(handle); } -void download_signal_handler(uv_signal_t *req, int signum) +static void download_signal_handler(uv_signal_t *req, int signum) { storj_download_state_t *state = req->data; storj_bridge_resolve_file_cancel(state); @@ -1827,6 +1827,22 @@ int main(int argc, char **argv) if (strcmp(command, "download-file") == 0) { + + /* get the corresponding bucket id from the bucket name */ + storj_api->bucket_name = argv[command_index + 1]; + storj_api->file_name = argv[command_index + 2]; + storj_api->dst_file = argv[command_index + 3]; + + if (!storj_api->bucket_name || !storj_api->file_name) + { + printf("Missing arguments: \n"); + status = 1; + goto end_program; + } + + storj_download_file(storj_api); + + #if 0 char *bucket_name = argv[command_index + 1]; char *file_name = argv[command_index + 2]; char *path = argv[command_index + 3]; @@ -1848,6 +1864,7 @@ int main(int argc, char **argv) storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); } } + #endif } else if (strcmp(command, "cp") == 0) { diff --git a/src/storj.c b/src/storj.c index 28e8105..b1b6259 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1647,3 +1647,20 @@ STORJ_API int storj_upload_file(storj_api_t *storj_api) storj_api->next_cmd_req = "upload-file-req"; storj_api->final_cmd_req = NULL; } + +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_download_file(storj_api_t *storj_api) +{ + storj_get_bucket_files(storj_api); + storj_api->final_cmd_req = "download-file-req"; +} + diff --git a/src/storj.h b/src/storj.h index 74cadce..1dd8cff 100755 --- a/src/storj.h +++ b/src/storj.h @@ -555,7 +555,7 @@ typedef struct storj_api { char *bucket_id; char *file_name; char *file_id; - char *src_info; /**< next file ready to upload */ + char *src_file; /**< next file ready to upload */ char *dst_file; /**< next file ready to upload */ int total_files; /**< total files to upload */ char *last_cmd_req; /**< last command requested */ @@ -979,6 +979,12 @@ STORJ_API storj_upload_state_t *storj_bridge_store_file(storj_env_t *env, void *handle, storj_progress_cb progress_cb, storj_finished_upload_cb finished_cb); +/** + * @brief Will free the file info struct passed to the upload finished callback + * + * @param[in] file - The storj_file_meta_t struct from storj_finished_upload_cb callback + */ +STORJ_API void storj_free_uploaded_file_info(storj_file_meta_t *file); /** * @brief Will cancel a download @@ -1085,6 +1091,30 @@ STORJ_API int storj_remove_file(storj_api_t *storj_api); */ STORJ_API int storj_list_mirrors(storj_api_t *storj_api); +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_upload_file(storj_api_t *storj_api); + +/** + * @brief Function gets the list of files for a given bucket + * name + * + * @author kishore (1/16/2018) + * + * @param storj_api + * + * @return STORJ_API int + */ +STORJ_API int storj_download_file(storj_api_t *storj_api); + static inline char separator() { #ifdef _WIN32 diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index cc8456a..c849163 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -2,6 +2,25 @@ static inline void noop() {}; +static void get_input(char *line) +{ + if (fgets(line, BUFSIZ, stdin) == NULL) { + line[0] = '\0'; + } else { + int len = strlen(line); + if (len > 0) { + char *last = strrchr(line, '\n'); + if (last) { + last[0] = '\0'; + } + last = strrchr(line, '\r'); + if (last) { + last[0] = '\0'; + } + } + } +} + static const char *get_filename_separator(const char *file_path) { const char *file_name = NULL; @@ -39,16 +58,6 @@ static void close_signal(uv_handle_t *handle) ((void)0); } -static void upload_signal_handler(uv_signal_t *req, int signum) -{ - storj_upload_state_t *state = req->data; - storj_bridge_store_file_cancel(state); - if (uv_signal_stop(req)) { - printf("Unable to stop signal\n"); - } - uv_close((uv_handle_t *)req, close_signal); -} - static void file_progress(double progress, uint64_t downloaded_bytes, uint64_t total_bytes, @@ -84,35 +93,37 @@ static void upload_file_complete(int status, char *file_id, void *handle) storj_api->rcvd_cmd_resp = "upload-file-resp"; printf("\n"); - if (status != 0) - { + if (status != 0) { printf("Upload failure: %s\n", storj_strerror(status)); - //exit(status); - } - else - { - printf("Upload Success! File ID: %s\n", file_id); + exit(status); } - free(file_id); + printf("Upload Success! File ID: %s\n", file_id); queue_next_cmd_req(storj_api); } +void upload_signal_handler(uv_signal_t *req, int signum) +{ + storj_upload_state_t *state = req->data; + storj_bridge_store_file_cancel(state); + if (uv_signal_stop(req)) { + printf("Unable to stop signal\n"); + } + uv_close((uv_handle_t *)req, close_signal); +} + static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) { FILE *fd = fopen(file_path, "r"); - if (!fd) - { + if (!fd) { printf("Invalid file path: %s\n", file_path); - exit(-1); } const char *file_name = get_filename_separator(file_path); - if (!file_name) - { + if (!file_name) { file_name = file_path; } @@ -122,8 +133,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, char *push_shard_limit = getenv("STORJ_PUSH_SHARD_LIMIT"); char *rs = getenv("STORJ_REED_SOLOMON"); - storj_upload_opts_t upload_opts = - { + storj_upload_opts_t upload_opts = { .prepare_frame_limit = (prepare_frame_limit) ? atoi(prepare_frame_limit) : 1, .push_frame_limit = (push_frame_limit) ? atoi(push_frame_limit) : 64, .push_shard_limit = (push_shard_limit) ? atoi(push_shard_limit) : 64, @@ -134,8 +144,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, }; uv_signal_t *sig = malloc(sizeof(uv_signal_t)); - if (!sig) - { + if (!sig) { return 1; } uv_signal_init(env->loop, sig); @@ -144,8 +153,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, storj_progress_cb progress_cb = (storj_progress_cb)noop; - if (env->log_options->level == 0) - { + if (env->log_options->level == 0) { progress_cb = file_progress; } @@ -155,11 +163,104 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, progress_cb, upload_file_complete); - if (!state) - { + if (!state) { + return 1; + } + + sig->data = state; + + return state->error_status; +} + +static void download_file_complete(int status, FILE *fd, void *handle) +{ + storj_api_t *storj_api = handle; + storj_api->rcvd_cmd_resp = "download-file-resp"; + + printf("\n"); + fclose(fd); + if (status) { + // TODO send to stderr + switch(status) { + case STORJ_FILE_DECRYPTION_ERROR: + printf("Unable to properly decrypt file, please check " \ + "that the correct encryption key was " \ + "imported correctly.\n\n"); + break; + default: + printf("Download failure: %s\n", storj_strerror(status)); + } + + exit(status); + } + printf("Download Success!\n"); + + queue_next_cmd_req(storj_api); +} + +static void download_signal_handler(uv_signal_t *req, int signum) +{ + storj_download_state_t *state = req->data; + storj_bridge_resolve_file_cancel(state); + if (uv_signal_stop(req)) { + printf("Unable to stop signal\n"); + } + uv_close((uv_handle_t *)req, close_signal); +} + +static int download_file(storj_env_t *env, char *bucket_id, + char *file_id, char *path, void *handle) +{ + FILE *fd = NULL; + + if (path) { + char user_input[BUFSIZ]; + memset(user_input, '\0', BUFSIZ); + + if(access(path, F_OK) != -1 ) { + printf("Warning: File already exists at path [%s].\n", path); + while (strcmp(user_input, "y") != 0 && strcmp(user_input, "n") != 0) + { + memset(user_input, '\0', BUFSIZ); + printf("Would you like to overwrite [%s]: [y/n] ", path); + get_input(user_input); + } + + if (strcmp(user_input, "n") == 0) { + printf("\nCanceled overwriting of [%s].\n", path); + return 1; + } + + unlink(path); + } + + fd = fopen(path, "w+"); + } else { + fd = stdout; + } + + if (fd == NULL) { + // TODO send to stderr + printf("Unable to open %s: %s\n", path, strerror(errno)); return 1; } + uv_signal_t *sig = malloc(sizeof(uv_signal_t)); + uv_signal_init(env->loop, sig); + uv_signal_start(sig, download_signal_handler, SIGINT); + + storj_progress_cb progress_cb = (storj_progress_cb)noop; + if (path && env->log_options->level == 0) { + progress_cb = file_progress; + } + + storj_download_state_t *state = storj_bridge_resolve_file(env, bucket_id, + file_id, fd, handle, + progress_cb, + download_file_complete); + if (!state) { + return 1; + } sig->data = state; return state->error_status; @@ -488,6 +589,19 @@ void queue_next_cmd_req(storj_api_t *storj_api) upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_name, storj_api); } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "download-file-req") == 0x00)) + { + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "download-file-resp"; + printf("\n\nAM here ....\n\n"); + + printf("dst_file= %s\n", storj_api->dst_file); + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api->dst_file, storj_api); + } else { From c6aecc95a0c06f66f93a0f256a520314e34fb4fd Mon Sep 17 00:00:00 2001 From: Kishore Date: Sat, 20 Jan 2018 19:11:43 -0500 Subject: [PATCH 36/68] Indentation changes and Code clean up --- src/cli.c | 2 +- src/storj.c | 103 +++++-------------- src/storj.h | 82 ++++++--------- src/storjapi_callback.c | 215 +++++++++++++--------------------------- src/storjapi_callback.h | 27 +---- 5 files changed, 130 insertions(+), 299 deletions(-) diff --git a/src/cli.c b/src/cli.c index cc2cdee..31c8250 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1965,7 +1965,7 @@ int main(int argc, char **argv) status = 1; goto end_program; } - storj_get_bucket_files(storj_api); + storj_list_files(storj_api); } else if (strcmp(command, "add-bucket") == 0) { char *bucket_name = argv[command_index + 1]; diff --git a/src/storj.c b/src/storj.c index b1b6259..28e8530 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1537,15 +1537,6 @@ STORJ_API int storj_bridge_register(storj_env_t *env, return uv_queue_work(env->loop, (uv_work_t*) work, json_request_worker, cb); } -/** - * @brief Function gets the bucket id for a given bucket name - * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int - */ STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) { char *bucket_name = storj_api->bucket_name; @@ -1562,105 +1553,63 @@ STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) storj_api->excp_cmd_resp = "get-bucket-id-resp"; /* when callback returns, we store the bucket id of bucket name else null */ - storj_bridge_get_buckets(storj_api->env, storj_api, get_bucket_id_callback); + return storj_bridge_get_buckets(storj_api->env, storj_api, get_bucket_id_callback); } -/** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int - */ -STORJ_API int storj_get_bucket_files(storj_api_t *storj_api) +STORJ_API int storj_list_files(storj_api_t *storj_api) { - storj_get_bucket_id(storj_api); + int ret = 0x00; + ret = storj_get_bucket_id(storj_api); storj_api->next_cmd_req = "list-files-req"; storj_api->final_cmd_req = NULL; + + return ret; } -/** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int - */ STORJ_API int storj_remove_bucket(storj_api_t *storj_api) { - storj_get_bucket_id(storj_api); + int ret = 0x00; + ret = storj_get_bucket_id(storj_api); storj_api->next_cmd_req = "remove-bucket-req"; storj_api->final_cmd_req = NULL; + + return ret; } -/** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int - */ STORJ_API int storj_remove_file(storj_api_t *storj_api) { - storj_get_bucket_files(storj_api); + int ret = 0x00; + ret = storj_list_files(storj_api); storj_api->final_cmd_req = "remove-file-req"; + + return ret; } -/** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int - */ STORJ_API int storj_list_mirrors(storj_api_t *storj_api) { - storj_get_bucket_files(storj_api); + int ret = 0x00; + ret = storj_list_files(storj_api); storj_api->final_cmd_req = "list-mirrors-req"; + + return ret; } -/** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int - */ STORJ_API int storj_upload_file(storj_api_t *storj_api) { - storj_get_bucket_id(storj_api); + int ret = 0x00; + ret = storj_get_bucket_id(storj_api); storj_api->next_cmd_req = "upload-file-req"; storj_api->final_cmd_req = NULL; + + return ret; } -/** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int - */ STORJ_API int storj_download_file(storj_api_t *storj_api) { - storj_get_bucket_files(storj_api); + int ret = 0x00; + ret = storj_list_files(storj_api); storj_api->final_cmd_req = "download-file-req"; + + return ret; } diff --git a/src/storj.h b/src/storj.h index 1dd8cff..0585711 100755 --- a/src/storj.h +++ b/src/storj.h @@ -1030,88 +1030,70 @@ STORJ_API int storj_bridge_register(storj_env_t *env, const char *password, void *handle, uv_after_work_cb cb); - /** - * @brief Get bucket id by bucket name - * - * @param[in] env The storj environment struct - * @param[in] email the user's email - * @param[in] password the user's password - * @param[in] handle A pointer that will be available in the callback - * @param[in] cb A function called with response when complete + * @brief Function returns the corresponding bucket's id for a + * given bucket name + * + * @param[in] storj_api_t structure that passes user's input + * info * @return A non-zero error value on failure and 0 on success. */ STORJ_API int storj_get_bucket_id(storj_api_t *storj_api); /** - * @brief Function gets the list of files for a given bucket - * name + * @brief Function to list files in a given bucket name * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. */ -STORJ_API int storj_get_bucket_files(storj_api_t *storj_api); +STORJ_API int storj_list_files(storj_api_t *storj_api); /** - * @brief Get bucket id by bucket name - * - * @param[in] env The storj environment struct - * @param[in] email the user's email - * @param[in] password the user's password - * @param[in] handle A pointer that will be available in the callback - * @param[in] cb A function called with response when complete + * @brief Function to remove a given bucket name + * + * @param[in] storj_api_t structure that passes user's input + * info * @return A non-zero error value on failure and 0 on success. */ STORJ_API int storj_remove_bucket(storj_api_t *storj_api); /** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api + * @brief Function to remove a file from a given bucket name * - * @return STORJ_API int + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. */ STORJ_API int storj_remove_file(storj_api_t *storj_api); /** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) - * - * @param storj_api + * @brief Function to return the node IDs for a given file for a + * given bucket name * - * @return STORJ_API int + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. */ STORJ_API int storj_list_mirrors(storj_api_t *storj_api); /** - * @brief Function gets the list of files for a given bucket + * @brief Function to upload a local file into a given bucket * name * - * @author kishore (1/16/2018) - * - * @param storj_api - * - * @return STORJ_API int + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. */ STORJ_API int storj_upload_file(storj_api_t *storj_api); /** - * @brief Function gets the list of files for a given bucket - * name - * - * @author kishore (1/16/2018) + * @brief Function to download a file from a given bucket to a + * local folder * - * @param storj_api - * - * @return STORJ_API int + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. */ STORJ_API int storj_download_file(storj_api_t *storj_api); diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index c849163..b2d27d4 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -65,7 +65,8 @@ static void file_progress(double progress, { int bar_width = 70; - if (progress == 0 && downloaded_bytes == 0) { + if (progress == 0 && downloaded_bytes == 0) + { printf("Preparing File..."); fflush(stdout); return; @@ -73,12 +74,18 @@ static void file_progress(double progress, printf("\r["); int pos = bar_width * progress; - for (int i = 0; i < bar_width; ++i) { - if (i < pos) { + for (int i = 0; i < bar_width; ++i) + { + if (i < pos) + { printf("="); - } else if (i == pos) { + } + else if (i == pos) + { printf(">"); - } else { + } + else + { printf(" "); } } @@ -93,7 +100,8 @@ static void upload_file_complete(int status, char *file_id, void *handle) storj_api->rcvd_cmd_resp = "upload-file-resp"; printf("\n"); - if (status != 0) { + if (status != 0) + { printf("Upload failure: %s\n", storj_strerror(status)); exit(status); } @@ -103,11 +111,12 @@ static void upload_file_complete(int status, char *file_id, void *handle) queue_next_cmd_req(storj_api); } -void upload_signal_handler(uv_signal_t *req, int signum) +static void upload_signal_handler(uv_signal_t *req, int signum) { storj_upload_state_t *state = req->data; storj_bridge_store_file_cancel(state); - if (uv_signal_stop(req)) { + if (uv_signal_stop(req)) + { printf("Unable to stop signal\n"); } uv_close((uv_handle_t *)req, close_signal); @@ -117,13 +126,15 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, { FILE *fd = fopen(file_path, "r"); - if (!fd) { + if (!fd) + { printf("Invalid file path: %s\n", file_path); } const char *file_name = get_filename_separator(file_path); - if (!file_name) { + if (!file_name) + { file_name = file_path; } @@ -133,7 +144,8 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, char *push_shard_limit = getenv("STORJ_PUSH_SHARD_LIMIT"); char *rs = getenv("STORJ_REED_SOLOMON"); - storj_upload_opts_t upload_opts = { + storj_upload_opts_t upload_opts = + { .prepare_frame_limit = (prepare_frame_limit) ? atoi(prepare_frame_limit) : 1, .push_frame_limit = (push_frame_limit) ? atoi(push_frame_limit) : 64, .push_shard_limit = (push_shard_limit) ? atoi(push_shard_limit) : 64, @@ -144,7 +156,8 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, }; uv_signal_t *sig = malloc(sizeof(uv_signal_t)); - if (!sig) { + if (!sig) + { return 1; } uv_signal_init(env->loop, sig); @@ -153,7 +166,8 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, storj_progress_cb progress_cb = (storj_progress_cb)noop; - if (env->log_options->level == 0) { + if (env->log_options->level == 0) + { progress_cb = file_progress; } @@ -368,12 +382,17 @@ static void delete_bucket_callback(uv_work_t *work_req, int status) storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->rcvd_cmd_resp = "remove-bucket-resp"; - if (req->status_code == 200 || req->status_code == 204) { + if (req->status_code == 200 || req->status_code == 204) + { printf("Bucket was successfully removed.\n"); - } else if (req->status_code == 401) { + } + else if (req->status_code == 401) + { printf("Invalid user credentials.\n"); goto cleanup; - } else { + } + else + { printf("Failed to destroy bucket. (%i)\n", req->status_code); goto cleanup; } @@ -397,13 +416,18 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; - if (req->status_code == 401) { - printf("Invalid user credentials.\n"); - goto cleanup; - } else if (req->status_code != 200 && req->status_code != 304) { + if (req->status_code == 401) + { + printf("Invalid user credentials.\n"); + goto cleanup; + } + else if (req->status_code != 200 && req->status_code != 304) + { printf("Request failed with status code: %i\n", req->status_code); goto cleanup; - } else if (req->total_buckets == 0) { + } + else if (req->total_buckets == 0) + { printf("No buckets.\n"); goto cleanup; } @@ -412,20 +436,20 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) { storj_bucket_meta_t *bucket = &req->buckets[i]; - if(strcmp(storj_api->bucket_name, bucket->name) == 0x00) + if (strcmp(storj_api->bucket_name, bucket->name) == 0x00) { printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", bucket->id, bucket->decrypted ? "true" : "false", bucket->created, bucket->name); - + /* store the bucket id */ storj_api->bucket_id = (char *)bucket->id; break; - } + } else { - if (i >= (req->total_buckets -1)) + if (i >= (req->total_buckets - 1)) { printf("Invalid bucket name. \n"); goto cleanup; @@ -451,36 +475,44 @@ void list_files_callback(uv_work_t *work_req, int status) storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->rcvd_cmd_resp = "list-files-resp"; - if (req->status_code == 404) { + if (req->status_code == 404) + { printf("Bucket id [%s] does not exist\n", req->bucket_id); goto cleanup; - } else if (req->status_code == 400) { + } + else if (req->status_code == 400) + { printf("Bucket id [%s] is invalid\n", req->bucket_id); goto cleanup; - } else if (req->status_code == 401) { + } + else if (req->status_code == 401) + { printf("Invalid user credentials.\n"); goto cleanup; - } else if (req->status_code != 200) { + } + else if (req->status_code != 200) + { printf("Request failed with status code: %i\n", req->status_code); } - if (req->total_files == 0) { + if (req->total_files == 0) + { printf("No files for bucket.\n"); goto cleanup; } storj_api->file_id = NULL; - for (int i = 0; i < req->total_files; i++) + for (int i = 0; i < req->total_files; i++) { storj_file_meta_t *file = &req->files[i]; - if ((storj_api->file_name != NULL) && - (strcmp(storj_api->file_name,file->filename)) == 0x00) + if ((storj_api->file_name != NULL) && + (strcmp(storj_api->file_name, file->filename)) == 0x00) { /* store the file id */ storj_api->file_id = (char *)file->id; } - + printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", file->id, file->size, @@ -492,7 +524,7 @@ void list_files_callback(uv_work_t *work_req, int status) queue_next_cmd_req(storj_api); -cleanup: + cleanup: storj_free_list_files_request(req); free(work_req); @@ -604,7 +636,6 @@ void queue_next_cmd_req(storj_api_t *storj_api) } else { - printf("[%s][%d] **** ALL CLEAN & DONE *****\n", __FUNCTION__, __LINE__); exit(0); @@ -619,116 +650,4 @@ void queue_next_cmd_req(storj_api_t *storj_api) __FUNCTION__, __LINE__, storj_api->last_cmd_req, storj_api->curr_cmd_req, storj_api->next_cmd_req); } - - -#if 0 - if (((strcmp("list-files" , storj_api->curr_cmd_req) == 0x00)|| - ((strcmp("download-file" , storj_api->curr_cmd_req) == 0x00))) && - ((strcmp("list-files-1", storj_api->next_cmd_req) == 0x00)|| - (strcmp("download-file-1", storj_api->next_cmd_req)==0x00))) - { - if(strcmp("list-files-1" , storj_api->next_cmd_req) == 0x00) - { - storj_bridge_list_files(storj_api->env, storj_api->bucket_id, storj_api, list_files_callback); - } - - if(strcmp("download-file-1" , storj_api->next_cmd_req) == 0x00) - { - //FILE *file = fopen("/home/kishore/libstorj/src/dwnld_list.txt", "r"); - FILE *file = fopen("dwnld_list.txt", "r"); - if (file != NULL) - { - char line[256][256]; - char *temp; - char temp_path[1024]; - int i = 0x00; - char *token[10]; - int tk_idx= 0x00; - memset(token, 0x00, sizeof(token)); - memset(temp_path, 0x00, sizeof(temp_path)); - memset(line, 0x00, sizeof(line)); - while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ - { - temp = strrchr(line[i], '\n'); - if(temp) *temp = '\0'; - temp = line[i]; - i++; - if (i >= storj_api->curr_up_file) - { - break; - } - } - - /* start tokenizing */ - token[0] = strtok(temp, ":"); - while (token[tk_idx] != NULL) - { - tk_idx++; - token[tk_idx] = strtok(NULL, ":"); - } - - if(storj_api->curr_up_file <= storj_api->total_files) - { - storj_api->file_id = token[0]; - strcpy(temp_path, storj_api->file_path); - strcat(temp_path, token[1]); - fprintf(stdout,"*****[%d:%d] downloading file: %s *****\n", - storj_api->curr_up_file, storj_api->total_files, temp_path); - storj_api->curr_up_file++; - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); - } - else - { - fprintf(stdout,"***** done downloading files *****\n"); - fclose(file); - exit(0); - } - } - else - { - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, storj_api->file_path,storj_api); - } - - } - } - else if ((strcmp("upload-file" , storj_api->curr_cmd_req) == 0x00) && - (strcmp("upload-file-1", storj_api->next_cmd_req) == 0x00)) - { - FILE *file = fopen(storj_api->file_name, "r"); - if (file != NULL) - { - char line[256][256]; - char *temp; - int i = 0x00; - memset(line, 0x00, sizeof(line)); - while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ - { - temp = strrchr(line[i], '\n'); - if(temp) *temp = '\0'; - storj_api->file_path = line[i]; - i++; - printf("[%s][%d] [index = %d] target file name = %s\n", __FUNCTION__, __LINE__, i, line[i-1]); - if(i >= storj_api->curr_up_file) - break; - } - if(storj_api->curr_up_file <= storj_api->total_files) - { - fprintf(stdout,"*****uploading file: %s *****\n",line[i-1]); //print the file contents on stdout. - upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_path, storj_api); - storj_api->curr_up_file++; - } - else - { - fprintf(stdout,"***** done uploading files *****\n"); - fclose(file); - exit(0); - } - } - else - { - /* handle single file upload from the command line */ - upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_path, storj_api); - } - } - #endif } diff --git a/src/storjapi_callback.h b/src/storjapi_callback.h index b373818..ff04123 100755 --- a/src/storjapi_callback.h +++ b/src/storjapi_callback.h @@ -16,38 +16,19 @@ extern "C" { #include "storj.h" /** - * @brief Get bucket id by bucket name - * - * @param[in] env The storj environment struct - * @param[in] email the user's email - * @param[in] password the user's password - * @param[in] handle A pointer that will be available in the callback - * @param[in] cb A function called with response when complete - * @return A non-zero error value on failure and 0 on success. + * @brief Callback function returning the bucket id for a given + * bucket name */ void get_bucket_id_callback(uv_work_t *work_req, int status); /** - * @brief Get bucket id by bucket name + * @brief Callback function listing the files in a given bucket * - * @param[in] env The storj environment struct - * @param[in] email the user's email - * @param[in] password the user's password - * @param[in] handle A pointer that will be available in the callback - * @param[in] cb A function called with response when complete - * @return A non-zero error value on failure and 0 on success. */ void list_files_callback(uv_work_t *work_req, int status); /** - * @brief Get bucket id by bucket name - * - * @param[in] env The storj environment struct - * @param[in] email the user's email - * @param[in] password the user's password - * @param[in] handle A pointer that will be available in the callback - * @param[in] cb A function called with response when complete - * @return A non-zero error value on failure and 0 on success. + * @brief Storj api state machine function */ void queue_next_cmd_req(storj_api_t *storj_api); From 50e0b33135b1a673852dcc33afc7fbadb4b096b4 Mon Sep 17 00:00:00 2001 From: Kishore Date: Mon, 22 Jan 2018 09:45:52 -0500 Subject: [PATCH 37/68] Merged the changes from MASTER libstorj to my local master branch off of forked libstroj(remote repo) --- src/bip39.c | 5 ++ src/cli.c | 11 ++- src/downloader.c | 1 + src/storj.c | 157 ++++++++++++++++++++++++++++++++++++++-- src/storj.h | 31 +++++++- src/storjapi_callback.c | 28 +++---- src/uploader.c | 53 ++++++++++---- 7 files changed, 239 insertions(+), 47 deletions(-) diff --git a/src/bip39.c b/src/bip39.c index ced31ef..7d4864b 100644 --- a/src/bip39.c +++ b/src/bip39.c @@ -190,6 +190,11 @@ bool mnemonic_check(const char *mnemonic) int mnemonic_to_seed(const char *mnemonic, const char *passphrase, char **buffer) { + // If mnemonic is NULL, assume empty string + if (!mnemonic) { + mnemonic = ""; + } + int passphraselen = strlen(passphrase); // We can't exceed a password of 256 bytes diff --git a/src/cli.c b/src/cli.c index 31c8250..eb6d412 100644 --- a/src/cli.c +++ b/src/cli.c @@ -617,7 +617,7 @@ static void file_progress(double progress, fflush(stdout); } -static void upload_file_complete(int status, char *file_id, void *handle) +static void upload_file_complete(int status, storj_file_meta_t *file, void *handle) { cli_state_t *cli_state = handle; printf("\n"); @@ -626,12 +626,11 @@ static void upload_file_complete(int status, char *file_id, void *handle) printf("Upload failure: %s\n", storj_strerror(status)); //exit(status); } - else - { - printf("Upload Success! File ID: %s\n", file_id); - } - free(file_id); + printf("Upload Success! File ID: %s\n", file->id); + + storj_free_uploaded_file_info(file); + if((cli_state->total_files == 0x00) && (cli_state->curr_up_file == 0x00)) { diff --git a/src/downloader.c b/src/downloader.c index de40876..eba0fd3 100644 --- a/src/downloader.c +++ b/src/downloader.c @@ -1240,6 +1240,7 @@ static void request_info(uv_work_t *work) req->info->size = 0; req->info->hmac = NULL; req->info->id = NULL; + req->info->bucket_id = NULL; req->info->decrypted = false; req->info->index = NULL; diff --git a/src/storj.c b/src/storj.c index 28e8530..8605c0a 100644 --- a/src/storj.c +++ b/src/storj.c @@ -293,6 +293,7 @@ static void list_files_request_worker(uv_work_t *work) struct json_object *mimetype; struct json_object *size; struct json_object *id; + struct json_object *bucket_id; struct json_object *created; struct json_object *hmac; struct json_object *hmac_value; @@ -304,6 +305,7 @@ static void list_files_request_worker(uv_work_t *work) json_object_object_get_ex(file, "mimetype", &mimetype); json_object_object_get_ex(file, "size", &size); json_object_object_get_ex(file, "id", &id); + json_object_object_get_ex(file, "bucket", &bucket_id); json_object_object_get_ex(file, "created", &created); json_object_object_get_ex(file, "hmac", &hmac); json_object_object_get_ex(hmac, "value", &hmac_value); @@ -317,6 +319,7 @@ static void list_files_request_worker(uv_work_t *work) file->index = NULL; file->hmac = json_object_get_string(hmac_value); file->id = json_object_get_string(id); + file->bucket_id = json_object_get_string(bucket_id); file->decrypted = false; file->filename = NULL; @@ -341,6 +344,90 @@ static void list_files_request_worker(uv_work_t *work) } } +static void get_file_info_request_worker(uv_work_t *work) +{ + get_file_info_request_t *req = work->data; + int status_code = 0; + + req->error_code = fetch_json(req->http_options, + req->options, req->method, req->path, req->body, + req->auth, &req->response, &status_code); + + req->status_code = status_code; + + // Get the bucket key to encrypt the filename from bucket id + char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); + generate_bucket_key(req->encrypt_options->mnemonic, + req->bucket_id, + &bucket_key_as_str); + + uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); + if (!bucket_key) { + req->error_code = STORJ_MEMORY_ERROR; + return; + } + + free(bucket_key_as_str); + + // Get file name encryption key with first half of hmac w/ magic + struct hmac_sha512_ctx ctx1; + hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); + hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); + uint8_t key[SHA256_DIGEST_SIZE]; + hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); + + free(bucket_key); + + struct json_object *file; + struct json_object *filename; + struct json_object *mimetype; + struct json_object *size; + struct json_object *id; + struct json_object *bucket_id; + struct json_object *created; + struct json_object *hmac; + struct json_object *hmac_value; + + json_object_object_get_ex(req->response, "filename", &filename); + json_object_object_get_ex(req->response, "mimetype", &mimetype); + json_object_object_get_ex(req->response, "size", &size); + json_object_object_get_ex(req->response, "id", &id); + json_object_object_get_ex(req->response, "bucket", &bucket_id); + json_object_object_get_ex(req->response, "created", &created); + json_object_object_get_ex(req->response, "hmac", &hmac); + json_object_object_get_ex(hmac, "value", &hmac_value); + + req->file = malloc(sizeof(storj_file_meta_t)); + req->file->created = json_object_get_string(created); + req->file->mimetype = json_object_get_string(mimetype); + req->file->size = json_object_get_int64(size); + req->file->erasure = NULL; + req->file->index = NULL; + req->file->hmac = json_object_get_string(hmac_value); + req->file->id = json_object_get_string(id); + req->file->bucket_id = json_object_get_string(bucket_id); + req->file->decrypted = false; + req->file->filename = NULL; + + // Attempt to decrypt the filename, otherwise + // we will default the filename to the encrypted text. + // The decrypted flag will be set to indicate the status + // of decryption for alternative display. + const char *encrypted_file_name = json_object_get_string(filename); + if (encrypted_file_name) { + char *decrypted_file_name; + int error_status = decrypt_meta(encrypted_file_name, key, + &decrypted_file_name); + if (!error_status) { + req->file->decrypted = true; + req->file->filename = decrypted_file_name; + } else { + req->file->decrypted = false; + req->file->filename = strdup(encrypted_file_name); + } + } +} + static uv_work_t *uv_work_new() { uv_work_t *work = malloc(sizeof(uv_work_t)); @@ -410,6 +497,39 @@ static list_files_request_t *list_files_request_new( return req; } +static get_file_info_request_t *get_file_info_request_new( + storj_http_options_t *http_options, + storj_bridge_options_t *options, + storj_encrypt_options_t *encrypt_options, + const char *bucket_id, + char *method, + char *path, + struct json_object *request_body, + bool auth, + void *handle) +{ + get_file_info_request_t *req = malloc(sizeof(get_file_info_request_t)); + if (!req) { + return NULL; + } + + req->http_options = http_options; + req->options = options; + req->encrypt_options = encrypt_options; + req->bucket_id = bucket_id; + req->method = method; + req->path = path; + req->auth = auth; + req->body = request_body; + req->response = NULL; + req->file = NULL; + req->error_code = 0; + req->status_code = 0; + req->handle = handle; + + return req; +} + static create_bucket_request_t *create_bucket_request_new( storj_http_options_t *http_options, storj_bridge_options_t *bridge_options, @@ -1467,10 +1587,10 @@ STORJ_API int storj_bridge_delete_frame(storj_env_t *env, } STORJ_API int storj_bridge_get_file_info(storj_env_t *env, - const char *bucket_id, - const char *file_id, - void *handle, - uv_after_work_cb cb) + const char *bucket_id, + const char *file_id, + void *handle, + uv_after_work_cb cb) { char *path = str_concat_many(5, "/buckets/", bucket_id, "/files/", file_id, "/info"); @@ -1478,13 +1598,36 @@ STORJ_API int storj_bridge_get_file_info(storj_env_t *env, return STORJ_MEMORY_ERROR; } - uv_work_t *work = json_request_work_new(env, "GET", path, NULL, - true, handle); + uv_work_t *work = uv_work_new(); if (!work) { return STORJ_MEMORY_ERROR; } - return uv_queue_work(env->loop, (uv_work_t*) work, json_request_worker, cb); + work->data = get_file_info_request_new(env->http_options, + env->bridge_options, + env->encrypt_options, + bucket_id, "GET", path, + NULL, true, handle); + + if (!work->data) { + return STORJ_MEMORY_ERROR; + } + + return uv_queue_work(env->loop, (uv_work_t*) work, + get_file_info_request_worker, cb); +} + +STORJ_API void storj_free_get_file_info_request(get_file_info_request_t *req) +{ + if (req->response) { + json_object_put(req->response); + } + free(req->path); + if (req->file) { + free((char *)req->file->filename); + } + free(req->file); + free(req); } STORJ_API int storj_bridge_list_mirrors(storj_env_t *env, diff --git a/src/storj.h b/src/storj.h index 0585711..cd5eedb 100755 --- a/src/storj.h +++ b/src/storj.h @@ -297,6 +297,7 @@ typedef struct { uint64_t size; const char *hmac; const char *id; + const char *bucket_id; bool decrypted; const char *index; } storj_file_meta_t; @@ -320,6 +321,24 @@ typedef struct { void *handle; } list_files_request_t; +/** @brief A structure for queueing get file info request work + */ +typedef struct { + storj_http_options_t *http_options; + storj_encrypt_options_t *encrypt_options; + storj_bridge_options_t *options; + const char *bucket_id; + char *method; + char *path; + bool auth; + struct json_object *body; + struct json_object *response; + storj_file_meta_t *file; + int error_code; + int status_code; + void *handle; +} get_file_info_request_t; + typedef enum { BUCKET_PUSH, BUCKET_PULL @@ -359,7 +378,7 @@ typedef void (*storj_finished_download_cb)(int status, FILE *fd, void *handle); /** @brief A function signature for an upload complete callback */ -typedef void (*storj_finished_upload_cb)(int error_status, char *file_id, void *handle); +typedef void (*storj_finished_upload_cb)(int error_status, storj_file_meta_t *file, void *handle); /** @brief A structure that represents a pointer to a shard * @@ -487,7 +506,7 @@ typedef struct { uint32_t shard_concurrency; const char *index; const char *file_name; - char *file_id; + storj_file_meta_t *info; const char *encrypted_file_name; FILE *original_file; uint64_t file_size; @@ -939,6 +958,13 @@ STORJ_API int storj_bridge_get_file_info(storj_env_t *env, void *handle, uv_after_work_cb cb); +/** + * @brief Will free all structs for get file info request + * + * @param[in] req - The work request from storj_bridge_get_file_info callback + */ +STORJ_API void storj_free_get_file_info_request(get_file_info_request_t *req); + /** * @brief Get mirror data for a file * @@ -979,6 +1005,7 @@ STORJ_API storj_upload_state_t *storj_bridge_store_file(storj_env_t *env, void *handle, storj_progress_cb progress_cb, storj_finished_upload_cb finished_cb); + /** * @brief Will free the file info struct passed to the upload finished callback * diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index b2d27d4..1471d48 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -94,19 +94,20 @@ static void file_progress(double progress, fflush(stdout); } -static void upload_file_complete(int status, char *file_id, void *handle) +static void upload_file_complete(int status, storj_file_meta_t *file, void *handle) { storj_api_t *storj_api = handle; storj_api->rcvd_cmd_resp = "upload-file-resp"; printf("\n"); - if (status != 0) - { + if (status != 0) { printf("Upload failure: %s\n", storj_strerror(status)); exit(status); } - printf("Upload Success! File ID: %s\n", file_id); + printf("Upload Success! File ID: %s\n", file->id); + + storj_free_uploaded_file_info(file); queue_next_cmd_req(storj_api); } @@ -115,8 +116,7 @@ static void upload_signal_handler(uv_signal_t *req, int signum) { storj_upload_state_t *state = req->data; storj_bridge_store_file_cancel(state); - if (uv_signal_stop(req)) - { + if (uv_signal_stop(req)) { printf("Unable to stop signal\n"); } uv_close((uv_handle_t *)req, close_signal); @@ -126,15 +126,13 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, { FILE *fd = fopen(file_path, "r"); - if (!fd) - { + if (!fd) { printf("Invalid file path: %s\n", file_path); } const char *file_name = get_filename_separator(file_path); - if (!file_name) - { + if (!file_name) { file_name = file_path; } @@ -144,8 +142,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, char *push_shard_limit = getenv("STORJ_PUSH_SHARD_LIMIT"); char *rs = getenv("STORJ_REED_SOLOMON"); - storj_upload_opts_t upload_opts = - { + storj_upload_opts_t upload_opts = { .prepare_frame_limit = (prepare_frame_limit) ? atoi(prepare_frame_limit) : 1, .push_frame_limit = (push_frame_limit) ? atoi(push_frame_limit) : 64, .push_shard_limit = (push_shard_limit) ? atoi(push_shard_limit) : 64, @@ -156,8 +153,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, }; uv_signal_t *sig = malloc(sizeof(uv_signal_t)); - if (!sig) - { + if (!sig) { return 1; } uv_signal_init(env->loop, sig); @@ -166,8 +162,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, storj_progress_cb progress_cb = (storj_progress_cb)noop; - if (env->log_options->level == 0) - { + if (env->log_options->level == 0) { progress_cb = file_progress; } @@ -628,7 +623,6 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "download-file-resp"; - printf("\n\nAM here ....\n\n"); printf("dst_file= %s\n", storj_api->dst_file); download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, diff --git a/src/uploader.c b/src/uploader.c index a671dbb..34d2f51 100644 --- a/src/uploader.c +++ b/src/uploader.c @@ -248,10 +248,6 @@ static void cleanup_state(storj_upload_state_t *state) free(state->frame_id); } - if (state->hmac_id) { - free(state->hmac_id); - } - if (state->encrypted_file_name) { free((char *)state->encrypted_file_name); } @@ -286,10 +282,6 @@ static void cleanup_state(storj_upload_state_t *state) free(state->encrypted_file_path); } - if (state->index) { - free((char *)state->index); - } - if (state->shard) { for (int i = 0; i < state->total_shards; i++ ) { @@ -309,7 +301,7 @@ static void cleanup_state(storj_upload_state_t *state) free(state->shard); } - state->finished_cb(state->error_status, state->file_id, state->handle); + state->finished_cb(state->error_status, state->info, state->handle); free(state); } @@ -358,17 +350,36 @@ static void after_create_bucket_entry(uv_work_t *work, int status) req->log->info(state->env->log_options, state->handle, "Successfully Added bucket entry"); + req->log->debug(state->env->log_options, state->handle, + "fn[after_create_bucket_entry] - JSON Response: %s", json_object_to_json_string(req->response)); + state->add_bucket_entry_count = 0; state->completed_upload = true; + state->info = malloc(sizeof(storj_file_meta_t)); + state->info->created = NULL; + state->info->filename = state->file_name; + state->info->mimetype = NULL; + state->info->erasure = NULL; + state->info->size = state->file_size; + state->info->hmac = state->hmac_id; + state->info->id = NULL; + state->info->bucket_id = state->bucket_id; + state->info->decrypted = true; + state->info->index = state->index; + struct json_object *file_id_value = NULL; - char *file_id = NULL; + struct json_object *created_value = NULL; + struct json_object *mimetype_value = NULL; + if (json_object_object_get_ex(req->response, "id", &file_id_value)) { - file_id = (char *)json_object_get_string(file_id_value); + state->info->id = strdup((char *)json_object_get_string(file_id_value)); } - - if (file_id) { - state->file_id = strdup(file_id); + if (json_object_object_get_ex(req->response, "created", &created_value)) { + state->info->created = strdup((char *)json_object_get_string(created_value)); + } + if (json_object_object_get_ex(req->response, "mimetype", &mimetype_value)) { + state->info->mimetype = strdup((char *)json_object_get_string(mimetype_value)); } } else if (state->add_bucket_entry_count == 6) { @@ -2662,7 +2673,7 @@ STORJ_API storj_upload_state_t *storj_bridge_store_file(storj_env_t *env, } else { state->index = NULL; } - state->file_id = NULL; + state->info = NULL; state->file_name = opts->file_name; state->encrypted_file_name = NULL; state->original_file = opts->fd; @@ -2733,3 +2744,15 @@ STORJ_API storj_upload_state_t *storj_bridge_store_file(storj_env_t *env, } return state; } + +STORJ_API void storj_free_uploaded_file_info(storj_file_meta_t *file) +{ + if (file) { + free((char *)file->id); + free((char *)file->created); + free((char *)file->mimetype); + free((char *)file->hmac); + free((char *)file->index); + } + free(file); +} From 7241f8d6618aaf39b104e3b16a238c1e17ab544b Mon Sep 17 00:00:00 2001 From: Kishore Date: Mon, 22 Jan 2018 19:59:07 -0500 Subject: [PATCH 38/68] Recursive upload-file with hardcoding of paths --- src/cli.c | 38 ++++ src/storj.c | 10 ++ src/storj.h | 15 ++ src/storjapi_callback.c | 377 ++++++++++++++++++++++++++++++++++++++++ src/storjapi_callback.h | 6 + 5 files changed, 446 insertions(+) diff --git a/src/cli.c b/src/cli.c index eb6d412..69bf205 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1953,6 +1953,44 @@ int main(int argc, char **argv) } #endif } + + else if (strcmp(command, "upload-files") == 0) + { + /* get the corresponding bucket id from the bucket name */ + storj_api->bucket_name = argv[command_index + 1]; + storj_api->file_path = argv[command_index + 2]; + + + storj_api->src_list = "/home/kishore/libstorj/src/src_list.txt"; + storj_api->dst_list = "/home/kishore/libstorj/src/dst_list.txt"; + + //printf("src_file=%s; dst_file=%s\n", storj_api->src_file, storj_api->dst_file); + + if (!storj_api->bucket_name || !storj_api->file_name) + { + printf("Missing arguments: \n"); + status = 1; + goto end_program; + } + + #if 0 + storj_api->src_fd = fopen(storj_api->src_file, "r"); + + if (!storj_api->src_fd) { + printf("Invalid file path: %s\n", storj_api->src_file); + exit(0); + } + + storj_api->dst_fd = fopen(storj_api->dst_file, "r"); + + if (!storj_api->dst_fd) { + printf("Invalid file path: %s\n", storj_api->dst_file); + exit(0); + } + #endif + + storj_upload_files(storj_api); + } else if (strcmp(command, "list-files") == 0) { /* get the corresponding bucket id from the bucket name */ diff --git a/src/storj.c b/src/storj.c index 8605c0a..8e43b3a 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1747,6 +1747,16 @@ STORJ_API int storj_upload_file(storj_api_t *storj_api) return ret; } +STORJ_API int storj_upload_files(storj_api_t *storj_api) +{ + int ret = 0x00; + ret = storj_get_bucket_id(storj_api); + storj_api->next_cmd_req = "verify-upload-files-req"; + storj_api->final_cmd_req = "upload-files-req"; + + return ret; +} + STORJ_API int storj_download_file(storj_api_t *storj_api) { int ret = 0x00; diff --git a/src/storj.h b/src/storj.h index cd5eedb..b69f054 100755 --- a/src/storj.h +++ b/src/storj.h @@ -574,7 +574,12 @@ typedef struct storj_api { char *bucket_id; char *file_name; char *file_id; + char *file_path; /**< local upload files directory path */ + FILE *src_fd; + char *src_list; /**< file list ready to upload */ char *src_file; /**< next file ready to upload */ + FILE *dst_fd; + char *dst_list; /**< file list ready to upload */ char *dst_file; /**< next file ready to upload */ int total_files; /**< total files to upload */ char *last_cmd_req; /**< last command requested */ @@ -1114,6 +1119,16 @@ STORJ_API int storj_list_mirrors(storj_api_t *storj_api); */ STORJ_API int storj_upload_file(storj_api_t *storj_api); +/** + * @brief Function to upload local files into a given bucket + * name + * + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +STORJ_API int storj_upload_files(storj_api_t *storj_api); + /** * @brief Function to download a file from a given bucket to a * local folder diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 1471d48..7af41e2 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -21,6 +21,152 @@ static void get_input(char *line) } } +// inserts into subject[] at position pos +void append(char subject[], const char insert[], int pos) +{ + char buf[100] = {}; // 100 so that it's big enough. fill with 0 + // or you could use malloc() to allocate sufficient space + + strncpy(buf, subject, pos); // copy at most first pos characters + int len = strlen(buf); + strcpy(buf+len, insert); // copy all of insert[] at the end + len += strlen(insert); // increase the length by length of insert[] + strcpy(buf+len, subject+pos); // copy the rest + + strcpy(subject, buf); // copy it back to subject + // deallocate buf[] here, if used malloc() +} + +char* replace_char(char* str, char find, char replace) +{ + char *current_pos = strchr(str,find); + while (current_pos) + { + *current_pos= replace; + append(current_pos,"_",0); + current_pos = strchr(current_pos,find); + } + return str; +} + +int no_of_files = 0x00; +static void printdir(char *dir, int depth, FILE *src_fd, FILE *dst_fd, void *handle) +{ + DIR *dp; + struct dirent *entry; + struct stat statbuf; + int spaces = depth*4; + char tmp_dir[800] = {}; + char *full_path = NULL; + char *s; + char * start; + storj_api_t *storj_api = handle; + + if((dp = opendir(dir)) == NULL) + { + fprintf(stderr,"cannot open directory: %s\n", dir); + return; + } + + chdir(dir); + while((entry = readdir(dp)) != NULL) + { + lstat(entry->d_name, &statbuf); + if (S_ISDIR(statbuf.st_mode)) + { + /* Found a directory, but ignore . and .. */ + if (strcmp(".", entry->d_name) == 0 || + strcmp("..", entry->d_name) == 0) continue; + + /* Recurse at a new indent level */ + printdir(entry->d_name, depth + 1, src_fd, dst_fd, handle); + } + else + { + no_of_files++; + full_path = realpath(entry->d_name, NULL); + /* write to src file */ + fprintf(src_fd, "%s%s\n", "", full_path); + printf("[%d]%s%s\n", no_of_files, "", full_path); + + /* replace the dir with __ */ + s = strstr(full_path, storj_api->file_path); + start = s + strlen(storj_api->file_path); + start = replace_char(start, '/', '_'); + memset(tmp_dir, 0x00, sizeof(tmp_dir)); + strcat(tmp_dir, storj_api->file_path); + strcat(tmp_dir, start); + + /* write to dst file */ + fprintf(dst_fd, "%s%s\n", "", tmp_dir); + printf("[%d]%s%s\n", no_of_files, "", tmp_dir); + } + } + chdir(".."); + closedir(dp); + free(full_path); +} + +static int file_exists(void *handle) +{ + struct stat sb; + gid_t st_grpid; + storj_api_t *storj_api = handle; + + FILE *src_fd, *dst_fd; + + if (stat(storj_api->file_path, &sb) == -1) + { + perror("stat"); + return CLI_NO_SUCH_FILE_OR_DIR; + } + + switch (sb.st_mode & S_IFMT) + { + case S_IFBLK: + printf("block device\n"); + break; + case S_IFCHR: + printf("character device\n"); + break; + case S_IFDIR: + printf("file_path = %s\n\n", storj_api->file_path); + printf("directory\n"); + + if((src_fd = fopen("src_list.txt", "w")) == NULL) + { + return CLI_UPLOAD_FILE_LOG_ERR; + } + + if((dst_fd = fopen("dst_list.txt", "w")) == NULL) + { + return CLI_UPLOAD_FILE_LOG_ERR; + } + printdir(storj_api->file_path, 0, src_fd, dst_fd, handle); + fclose(src_fd); + fclose(dst_fd); + return CLI_VALID_DIR; + break; + case S_IFIFO: + printf("FIFO/pipe\n"); + break; + case S_IFLNK: + printf("symlink\n"); + break; + case S_IFREG: + return CLI_VALID_REGULAR_FILE; + break; + case S_IFSOCK: + printf("socket\n"); + break; + default: + printf("unknown?\n"); + break; + } + + return CLI_UNKNOWN_FILE_ATTR; +} + static const char *get_filename_separator(const char *file_path) { const char *file_name = NULL; @@ -128,6 +274,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, if (!fd) { printf("Invalid file path: %s\n", file_path); + exit(0); } const char *file_name = get_filename_separator(file_path); @@ -181,6 +328,182 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, return state->error_status; } +static void upload_files_complete(int status, storj_file_meta_t *file, void *handle) +{ + storj_api_t *storj_api = handle; + storj_api->rcvd_cmd_resp = "upload-files-resp"; + + printf("\n"); + if (status != 0) + { + printf("[%s][%d]Upload failure: %s\n", + __FUNCTION__, __LINE__, storj_strerror(status)); + } + else + { + printf("Upload Success! File ID: %s\n", file->id); + storj_free_uploaded_file_info(file); + } + + queue_next_cmd_req(storj_api); +} + +static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) +{ + storj_api_t *storj_api = handle; + + FILE *fd = fopen(file_path, "r"); + + if (!fd) + { + printf("[%s][%d]Invalid file : %s\n", __FUNCTION__, __LINE__, file_path); + exit(0); + } + + printf("uploading src file = %s as ", file_path); + + /* replace the dir with __ */ + char *s = strstr(storj_api->src_file, storj_api->file_path); + char *start = s + strlen(storj_api->file_path); + char tmp_dir[256]; + start = replace_char(start, '/', '_'); + memset(tmp_dir, 0x00, sizeof(tmp_dir)); + strcat(tmp_dir, storj_api->file_path); + strcat(tmp_dir, start); + storj_api->dst_file = tmp_dir; + + const char *file_name = get_filename_separator(storj_api->dst_file); + + if (!file_name) + { + file_name = file_path; + } + printf(" %s\n", file_name); + + // Upload opts env variables: + char *prepare_frame_limit = getenv("STORJ_PREPARE_FRAME_LIMIT"); + char *push_frame_limit = getenv("STORJ_PUSH_FRAME_LIMIT"); + char *push_shard_limit = getenv("STORJ_PUSH_SHARD_LIMIT"); + char *rs = getenv("STORJ_REED_SOLOMON"); + + storj_upload_opts_t upload_opts = { + .prepare_frame_limit = (prepare_frame_limit) ? atoi(prepare_frame_limit) : 1, + .push_frame_limit = (push_frame_limit) ? atoi(push_frame_limit) : 64, + .push_shard_limit = (push_shard_limit) ? atoi(push_shard_limit) : 64, + .rs = (!rs) ? true : (strcmp(rs, "false") == 0) ? false : true, + .bucket_id = bucket_id, + .file_name = file_name, + .fd = fd + }; + + uv_signal_t *sig = malloc(sizeof(uv_signal_t)); + if (!sig) { + return 1; + } + uv_signal_init(env->loop, sig); + uv_signal_start(sig, upload_signal_handler, SIGINT); + + + + storj_progress_cb progress_cb = (storj_progress_cb)noop; + if (env->log_options->level == 0) { + progress_cb = file_progress; + } + + storj_upload_state_t *state = storj_bridge_store_file(env, + &upload_opts, + handle, + progress_cb, + upload_files_complete); + + if (!state) { + return 1; + } + + sig->data = state; + + return state->error_status; +} + +static void verify_upload_files(void *handle) +{ + storj_api_t *storj_api = handle; + char cwd[1024]; + int total_src_files = 0x00; + int total_dst_files = 0x00; + int ret = 0x00; + + storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; + int file_attr = file_exists(handle); + + memset(cwd, 0x00, sizeof(cwd)); + strcpy(cwd, storj_api->file_path); + strcat(cwd, "/src_list.txt"); + + storj_api->src_fd = fopen(cwd, "r"); + + if (!storj_api->src_fd) + { + printf("Invalid file path: %s\n", "src_list.txt"); + exit(0); + } + else + { + /* count total src_list files */ + char line[256][256]; + char *temp; + int i = 0x00; + + memset(line, 0x00, sizeof(line)); + + /* read a line from a file */ + while (fgets(line[i], sizeof(line), storj_api->src_fd) != NULL) + { + i++; + } + + total_src_files = i; + printf("total_src_files = %d\n", total_src_files); + } + + memset(cwd, 0x00, sizeof(cwd)); + strcpy(cwd, storj_api->file_path); + strcat(cwd, "/dst_list.txt"); + + storj_api->dst_fd = fopen(cwd, "r"); + + if (!storj_api->dst_fd) + { + printf("Invalid file path: %s\n", "dst_list.txt"); + exit(0); + } + else + { + /* count total src_list files */ + char line[256][256]; + char *temp; + int i = 0x00; + + memset(line, 0x00, sizeof(line)); + + /* read a line from a file */ + while (fgets(line[i], sizeof(line), storj_api->dst_fd) != NULL) + { + i++; + } + + total_dst_files = i; + printf("total_dst_files = %d\n", total_dst_files); + } + + + if (total_dst_files == total_src_files) + { + storj_api->total_files = total_src_files; + } + queue_next_cmd_req(storj_api); +} + static void download_file_complete(int status, FILE *fd, void *handle) { storj_api_t *storj_api = handle; @@ -616,6 +939,60 @@ void queue_next_cmd_req(storj_api_t *storj_api) upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_name, storj_api); } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "verify-upload-files-req") == 0x00)) + { + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "verify-upload-files-resp"; + + verify_upload_files(storj_api); + } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "upload-files-req") == 0x00)) + { + storj_api->curr_cmd_req = storj_api->next_cmd_req; + //storj_api->next_cmd_req = storj_api->final_cmd_req; + //storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "upload-files-resp"; + static int curr_upload_file = 0x01; + + FILE *file = fopen(storj_api->src_list, "r"); + + char line[256][256]; + char *temp; + int i = 0x00; + memset(line, 0x00, sizeof(line)); + + if (file != NULL) + { + while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ + { + temp = strrchr(line[i], '\n'); + if(temp) *temp = '\0'; + storj_api->src_file = line[i]; + i++; + if(i >= curr_upload_file) + { + break; + } + } + } + fclose(file); + + if (curr_upload_file <= storj_api->total_files) + { + curr_upload_file++; + } + else + { + exit(0); + } + + printf("about to upload = %s \n\n", storj_api->src_file); + upload_files(storj_api->env, storj_api->bucket_id, storj_api->src_file, storj_api); + } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "download-file-req") == 0x00)) { diff --git a/src/storjapi_callback.h b/src/storjapi_callback.h index ff04123..5881519 100755 --- a/src/storjapi_callback.h +++ b/src/storjapi_callback.h @@ -15,6 +15,12 @@ extern "C" { #include "storj.h" +#define CLI_NO_SUCH_FILE_OR_DIR 0x00 +#define CLI_VALID_REGULAR_FILE 0x01 +#define CLI_VALID_DIR 0x02 +#define CLI_UNKNOWN_FILE_ATTR 0x03 +#define CLI_UPLOAD_FILE_LOG_ERR 0x04 + /** * @brief Callback function returning the bucket id for a given * bucket name From df05565594aabfc16e1b7238485bd2c1ef0988af Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 23 Jan 2018 09:10:53 -0500 Subject: [PATCH 39/68] Clean up of upload-files --- src/cli.c | 22 ------- src/storj.h | 2 +- src/storjapi_callback.c | 132 ++++++++++++---------------------------- 3 files changed, 39 insertions(+), 117 deletions(-) diff --git a/src/cli.c b/src/cli.c index 69bf205..024044e 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1960,12 +1960,6 @@ int main(int argc, char **argv) storj_api->bucket_name = argv[command_index + 1]; storj_api->file_path = argv[command_index + 2]; - - storj_api->src_list = "/home/kishore/libstorj/src/src_list.txt"; - storj_api->dst_list = "/home/kishore/libstorj/src/dst_list.txt"; - - //printf("src_file=%s; dst_file=%s\n", storj_api->src_file, storj_api->dst_file); - if (!storj_api->bucket_name || !storj_api->file_name) { printf("Missing arguments: \n"); @@ -1973,22 +1967,6 @@ int main(int argc, char **argv) goto end_program; } - #if 0 - storj_api->src_fd = fopen(storj_api->src_file, "r"); - - if (!storj_api->src_fd) { - printf("Invalid file path: %s\n", storj_api->src_file); - exit(0); - } - - storj_api->dst_fd = fopen(storj_api->dst_file, "r"); - - if (!storj_api->dst_fd) { - printf("Invalid file path: %s\n", storj_api->dst_file); - exit(0); - } - #endif - storj_upload_files(storj_api); } else if (strcmp(command, "list-files") == 0) diff --git a/src/storj.h b/src/storj.h index b69f054..b857e45 100755 --- a/src/storj.h +++ b/src/storj.h @@ -576,7 +576,7 @@ typedef struct storj_api { char *file_id; char *file_path; /**< local upload files directory path */ FILE *src_fd; - char *src_list; /**< file list ready to upload */ + char src_list[256]; /**< file list ready to upload */ char *src_file; /**< next file ready to upload */ FILE *dst_fd; char *dst_list; /**< file list ready to upload */ diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 7af41e2..cb7f1c6 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -21,36 +21,41 @@ static void get_input(char *line) } } -// inserts into subject[] at position pos +/* inserts into subject[] at position pos */ void append(char subject[], const char insert[], int pos) { - char buf[100] = {}; // 100 so that it's big enough. fill with 0 - // or you could use malloc() to allocate sufficient space + char buf[256] = { }; - strncpy(buf, subject, pos); // copy at most first pos characters + /* copy at most first pos characters */ + strncpy(buf, subject, pos); int len = strlen(buf); - strcpy(buf+len, insert); // copy all of insert[] at the end - len += strlen(insert); // increase the length by length of insert[] - strcpy(buf+len, subject+pos); // copy the rest - strcpy(subject, buf); // copy it back to subject - // deallocate buf[] here, if used malloc() + /* copy all of insert[] at the end */ + strcpy(buf + len, insert); + + /* increase the length by length of insert[] */ + len += strlen(insert); + + /* copy the rest */ + strcpy(buf + len, subject + pos); + + /* copy it back to subject */ + strcpy(subject, buf); } char* replace_char(char* str, char find, char replace) { - char *current_pos = strchr(str,find); + char *current_pos = strchr(str, find); while (current_pos) { - *current_pos= replace; - append(current_pos,"_",0); - current_pos = strchr(current_pos,find); + *current_pos = replace; + append(current_pos, "_", 0); + current_pos = strchr(current_pos, find); } - return str; + return (str); } -int no_of_files = 0x00; -static void printdir(char *dir, int depth, FILE *src_fd, FILE *dst_fd, void *handle) +static void printdir(char *dir, int depth, FILE *src_fd, void *handle) { DIR *dp; struct dirent *entry; @@ -79,27 +84,13 @@ static void printdir(char *dir, int depth, FILE *src_fd, FILE *dst_fd, void *han strcmp("..", entry->d_name) == 0) continue; /* Recurse at a new indent level */ - printdir(entry->d_name, depth + 1, src_fd, dst_fd, handle); + printdir(entry->d_name, depth + 1, src_fd, handle); } else { - no_of_files++; full_path = realpath(entry->d_name, NULL); /* write to src file */ fprintf(src_fd, "%s%s\n", "", full_path); - printf("[%d]%s%s\n", no_of_files, "", full_path); - - /* replace the dir with __ */ - s = strstr(full_path, storj_api->file_path); - start = s + strlen(storj_api->file_path); - start = replace_char(start, '/', '_'); - memset(tmp_dir, 0x00, sizeof(tmp_dir)); - strcat(tmp_dir, storj_api->file_path); - strcat(tmp_dir, start); - - /* write to dst file */ - fprintf(dst_fd, "%s%s\n", "", tmp_dir); - printf("[%d]%s%s\n", no_of_files, "", tmp_dir); } } chdir(".."); @@ -130,21 +121,12 @@ static int file_exists(void *handle) printf("character device\n"); break; case S_IFDIR: - printf("file_path = %s\n\n", storj_api->file_path); - printf("directory\n"); - - if((src_fd = fopen("src_list.txt", "w")) == NULL) - { - return CLI_UPLOAD_FILE_LOG_ERR; - } - - if((dst_fd = fopen("dst_list.txt", "w")) == NULL) + if((src_fd = fopen(storj_api->src_list, "w")) == NULL) { return CLI_UPLOAD_FILE_LOG_ERR; } - printdir(storj_api->file_path, 0, src_fd, dst_fd, handle); + printdir(storj_api->file_path, 0, src_fd, handle); fclose(src_fd); - fclose(dst_fd); return CLI_VALID_DIR; break; case S_IFIFO: @@ -428,23 +410,27 @@ static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path static void verify_upload_files(void *handle) { storj_api_t *storj_api = handle; - char cwd[1024]; + char cwd[256]; int total_src_files = 0x00; int total_dst_files = 0x00; int ret = 0x00; - storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; - int file_attr = file_exists(handle); - + /* create a upload list file src_list.txt */ memset(cwd, 0x00, sizeof(cwd)); strcpy(cwd, storj_api->file_path); - strcat(cwd, "/src_list.txt"); + strcat(cwd, "src_list.txt"); + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + strcpy(storj_api->src_list, cwd); + + storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; + int file_attr = file_exists(handle); storj_api->src_fd = fopen(cwd, "r"); if (!storj_api->src_fd) { - printf("Invalid file path: %s\n", "src_list.txt"); + printf("[%s][%d]Invalid file path: %s\n", + __FUNCTION__, __LINE__, storj_api->src_list); exit(0); } else @@ -463,44 +449,10 @@ static void verify_upload_files(void *handle) } total_src_files = i; - printf("total_src_files = %d\n", total_src_files); - } - - memset(cwd, 0x00, sizeof(cwd)); - strcpy(cwd, storj_api->file_path); - strcat(cwd, "/dst_list.txt"); - - storj_api->dst_fd = fopen(cwd, "r"); - - if (!storj_api->dst_fd) - { - printf("Invalid file path: %s\n", "dst_list.txt"); - exit(0); - } - else - { - /* count total src_list files */ - char line[256][256]; - char *temp; - int i = 0x00; - - memset(line, 0x00, sizeof(line)); - - /* read a line from a file */ - while (fgets(line[i], sizeof(line), storj_api->dst_fd) != NULL) - { - i++; - } - - total_dst_files = i; - printf("total_dst_files = %d\n", total_dst_files); } + storj_api->total_files = total_src_files; - if (total_dst_files == total_src_files) - { - storj_api->total_files = total_src_files; - } queue_next_cmd_req(storj_api); } @@ -854,13 +806,6 @@ void queue_next_cmd_req(storj_api_t *storj_api) if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) { - printf("[%s][%d]expt resp = %s; rcvd resp = %s \n", - __FUNCTION__, __LINE__, - storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); - printf("[%s][%d]last cmd = %s; cur cmd = %s; next cmd = %s\n", - __FUNCTION__, __LINE__, storj_api->last_cmd_req, - storj_api->curr_cmd_req, storj_api->next_cmd_req); - if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) { @@ -953,8 +898,6 @@ void queue_next_cmd_req(storj_api_t *storj_api) (strcmp(storj_api->next_cmd_req, "upload-files-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; - //storj_api->next_cmd_req = storj_api->final_cmd_req; - //storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "upload-files-resp"; static int curr_upload_file = 0x01; @@ -987,10 +930,11 @@ void queue_next_cmd_req(storj_api_t *storj_api) } else { - exit(0); + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + //exit(0); } - printf("about to upload = %s \n\n", storj_api->src_file); upload_files(storj_api->env, storj_api->bucket_id, storj_api->src_file, storj_api); } else if ((storj_api->next_cmd_req != NULL) && From 977e8cfc52f55956a9685e618c7cdc3be0c7fa16 Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 23 Jan 2018 10:28:25 -0500 Subject: [PATCH 40/68] Added upload file metrics to upload-files command --- src/storj.h | 14 +++++++++++++- src/storjapi_callback.c | 28 ++++++++++++++++------------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/storj.h b/src/storj.h index b857e45..be80290 100755 --- a/src/storj.h +++ b/src/storj.h @@ -564,6 +564,18 @@ typedef struct { int pending_work_count; } storj_upload_state_t; +/** + * @brief A Structure for management information base (MIB) for + * Storj API. + */ +typedef struct storj_api_mib { + int xfer_count; /**< # of files xferred (up/down) */ + int total_files; /**< total files to xfer */ + int success_xfer_count; /**< files xferred successfully */ + int fail_xfer_count; /**< files xferred successfully */ + void *handle; +} storj_api_mib_t; + /** * @brief A Structure for passing the User's Application info to * Storj API. @@ -579,7 +591,7 @@ typedef struct storj_api { char src_list[256]; /**< file list ready to upload */ char *src_file; /**< next file ready to upload */ FILE *dst_fd; - char *dst_list; /**< file list ready to upload */ + int xfer_count; /**< # of files xferred (up/down) */ char *dst_file; /**< next file ready to upload */ int total_files; /**< total files to upload */ char *last_cmd_req; /**< last command requested */ diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index cb7f1c6..75f599b 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -342,7 +342,8 @@ static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path exit(0); } - printf("uploading src file = %s as ", file_path); + printf("Uploading[%d]of[%d] src file = %s as ", + storj_api->xfer_count, storj_api->total_files, file_path); /* replace the dir with __ */ char *s = strstr(storj_api->src_file, storj_api->file_path); @@ -385,8 +386,6 @@ static int upload_files(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_progress_cb progress_cb = (storj_progress_cb)noop; if (env->log_options->level == 0) { progress_cb = file_progress; @@ -452,6 +451,7 @@ static void verify_upload_files(void *handle) } storj_api->total_files = total_src_files; + storj_api->xfer_count = 0x01; queue_next_cmd_req(storj_api); } @@ -899,7 +899,6 @@ void queue_next_cmd_req(storj_api_t *storj_api) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->excp_cmd_resp = "upload-files-resp"; - static int curr_upload_file = 0x01; FILE *file = fopen(storj_api->src_list, "r"); @@ -916,7 +915,7 @@ void queue_next_cmd_req(storj_api_t *storj_api) if(temp) *temp = '\0'; storj_api->src_file = line[i]; i++; - if(i >= curr_upload_file) + if(i >= storj_api->xfer_count) { break; } @@ -924,18 +923,23 @@ void queue_next_cmd_req(storj_api_t *storj_api) } fclose(file); - if (curr_upload_file <= storj_api->total_files) + if (storj_api->xfer_count <= storj_api->total_files) { - curr_upload_file++; + /* is it the last file ? */ + if(storj_api->xfer_count == storj_api->total_files) + { + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + } + + upload_files(storj_api->env, storj_api->bucket_id, storj_api->src_file, storj_api); + storj_api->xfer_count++; } else { - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - //exit(0); + printf("[%s][%d] Invalid xfer counts\n", __FUNCTION__, __LINE__); + exit(0); } - - upload_files(storj_api->env, storj_api->bucket_id, storj_api->src_file, storj_api); } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "download-file-req") == 0x00)) From 37097612d41b8eae6dffb6a7e279bad5c5a3238f Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 24 Jan 2018 11:36:34 -0500 Subject: [PATCH 41/68] Recursive download-files storj api command added --- src/cli.c | 46 +++++++++++++++++--- src/storj.c | 9 ++++ src/storjapi_callback.c | 95 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 138 insertions(+), 12 deletions(-) diff --git a/src/cli.c b/src/cli.c index 024044e..c89aba6 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1953,7 +1953,6 @@ int main(int argc, char **argv) } #endif } - else if (strcmp(command, "upload-files") == 0) { /* get the corresponding bucket id from the bucket name */ @@ -1969,6 +1968,21 @@ int main(int argc, char **argv) storj_upload_files(storj_api); } + else if (strcmp(command, "download-files") == 0) + { + /* get the corresponding bucket id from the bucket name */ + storj_api->bucket_name = argv[command_index + 1]; + storj_api->file_path = argv[command_index + 2]; + + if (!storj_api->bucket_name || !storj_api->file_name) + { + printf("Missing arguments: \n"); + status = 1; + goto end_program; + } + + storj_download_files(storj_api); + } else if (strcmp(command, "list-files") == 0) { /* get the corresponding bucket id from the bucket name */ @@ -1992,7 +2006,6 @@ int main(int argc, char **argv) storj_bridge_create_bucket(env, bucket_name, NULL, create_bucket_callback); - } else if (strcmp(command, "remove-bucket") == 0) { storj_api->bucket_name = argv[command_index + 1]; @@ -2009,8 +2022,7 @@ int main(int argc, char **argv) storj_bridge_delete_bucket(env, bucket_id, NULL, delete_bucket_callback); #endif - - } + } else if (strcmp(command, "remove-file") == 0) { storj_api->bucket_name = argv[command_index + 1]; @@ -2073,8 +2085,30 @@ int main(int argc, char **argv) storj_bridge_list_mirrors(env, bucket_id, file_id, NULL, list_mirrors_callback); #endif - } - else { + } + else if (strcmp(command, "test-cli") == 0) + { + time_t rawtime; + char buffer [255]; + + time (&rawtime); + sprintf(buffer,"/tmp/STORJ_%s" ,ctime(&rawtime) ); + + // Lets convert space to _ in + char *p = buffer; + for (; *p; ++p) + { + if (*p == ' ') + *p = '_'; + else if (*p == '\n') + *p = '\0'; + } + + printf("%s\n",buffer); + //fopen(buffer,"w"); + } + else + { printf("'%s' is not a storj command. See 'storj --help'\n\n", command); status = 1; diff --git a/src/storj.c b/src/storj.c index 8e43b3a..5063eb6 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1766,3 +1766,12 @@ STORJ_API int storj_download_file(storj_api_t *storj_api) return ret; } +STORJ_API int storj_download_files(storj_api_t *storj_api) +{ + int ret = 0x00; + ret = storj_list_files(storj_api); + storj_api->final_cmd_req = "download-files-req"; + + return ret; +} + diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 75f599b..b732d50 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -472,12 +472,14 @@ static void download_file_complete(int status, FILE *fd, void *handle) "imported correctly.\n\n"); break; default: - printf("Download failure: %s\n", storj_strerror(status)); + printf("[%d][%d]Download failure: %s\n", + __FUNCTION__, __LINE__, storj_strerror(status)); } - - exit(status); } - printf("Download Success!\n"); + else + { + printf("Download Success!\n"); + } queue_next_cmd_req(storj_api); } @@ -771,6 +773,15 @@ void list_files_callback(uv_work_t *work_req, int status) goto cleanup; } + + FILE *dwnld_list_fd = stdout; + if ((dwnld_list_fd = fopen("/tmp/dwnld_list.txt", "w")) == NULL) + { + printf("[%s][%d] Unable to create download list file\n", + __FUNCTION__, __LINE__); + goto cleanup; + } + storj_api->file_id = NULL; for (int i = 0; i < req->total_files; i++) { @@ -790,8 +801,13 @@ void list_files_callback(uv_work_t *work_req, int status) file->mimetype, file->created, file->filename); + + fprintf(dwnld_list_fd, "%s:%s\n",file->id, file->filename); } + storj_api->total_files = req->total_files; + storj_api->xfer_count = 0x01; + fclose(dwnld_list_fd); queue_next_cmd_req(storj_api); cleanup: @@ -804,6 +820,12 @@ void queue_next_cmd_req(storj_api_t *storj_api) { void *handle = storj_api->handle; + printf("[%s][%d]start !!!! expt resp = %s; rcvd resp = %s \n", + __FUNCTION__, __LINE__, + storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); + printf("[%s][%d]last cmd = %s; cur cmd = %s; next cmd = %s\n", + __FUNCTION__, __LINE__, storj_api->last_cmd_req, + storj_api->curr_cmd_req, storj_api->next_cmd_req); if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) { if ((storj_api->next_cmd_req != NULL) && @@ -953,10 +975,71 @@ void queue_next_cmd_req(storj_api_t *storj_api) download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, storj_api->dst_file, storj_api); } + else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "download-files-req") == 0x00)) + { + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->excp_cmd_resp = "download-file-resp"; + + FILE *file = stdout; + + if ((file = fopen("/tmp/dwnld_list.txt", "r")) != NULL) + { + char line[256][256]; + char *temp; + char temp_path[1024]; + int i = 0x00; + char *token[10]; + int tk_idx= 0x00; + memset(token, 0x00, sizeof(token)); + memset(temp_path, 0x00, sizeof(temp_path)); + memset(line, 0x00, sizeof(line)); + while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ + { + temp = strrchr(line[i], '\n'); + if(temp) *temp = '\0'; + i++; + if (i >= storj_api->xfer_count) + { + break; + } + } + fclose(file); + + /* start tokenizing */ + token[0] = strtok(line[i-1], ":"); + while (token[tk_idx] != NULL) + { + tk_idx++; + token[tk_idx] = strtok(NULL, ":"); + } + + if(storj_api->xfer_count <= storj_api->total_files) + { + /* is it the last file ? */ + if(storj_api->xfer_count == storj_api->total_files) + { + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + } + + storj_api->file_id = token[0]; + strcpy(temp_path, storj_api->file_path); + strcat(temp_path, token[1]); + fprintf(stdout,"*****[%d:%d] downloading file to: %s *****\n", storj_api->xfer_count, storj_api->total_files, temp_path); + storj_api->xfer_count++; + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); + } + else + { + printf("[%s][%d] Invalid xfer counts\n", __FUNCTION__, __LINE__); + exit(0); + } + } + } else { - printf("[%s][%d] **** ALL CLEAN & DONE *****\n", - __FUNCTION__, __LINE__); + printf("[%s][%d] **** ALL CLEAN & DONE *****\n", __FUNCTION__, __LINE__); exit(0); } } From f8081cdcbc5e0de45f1c00a6cce2fdb6cddbc8e9 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 24 Jan 2018 12:54:34 -0500 Subject: [PATCH 42/68] To list buckets, the following commands are supported storj ls storj list-buckets To list bucket id and the corresponding files in that bucket-name, use storj ls To list mirrors for a specific file in a bucket, use storj lm storj list-mirrors To remove a file from a bucket, use storj rm storj remove-file --- src/cli.c | 89 ++++++++++++++----------------------------------------- 1 file changed, 22 insertions(+), 67 deletions(-) diff --git a/src/cli.c b/src/cli.c index c89aba6..2262083 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1824,6 +1824,12 @@ int main(int argc, char **argv) storj_api->env = env; + printf("command = %s; command_index = %d\n", command, command_index); + printf("local_file_path (req arg_ = %s\n", local_file_path); + printf("argv[command_index+1] = %s\n", argv[command_index+1]); + printf("argv[command_index+2] = %s\n", argv[command_index+2]); + printf("argv[command_index+3] = %s\n", argv[command_index+3]); + if (strcmp(command, "download-file") == 0) { @@ -1922,36 +1928,6 @@ int main(int argc, char **argv) } storj_upload_file(storj_api); - - #if 0 - char *bucket_id = NULL; - - /* get the corresponding bucket id from the bucket name */ - char *bucket_name = argv[command_index + 1]; - char *path = argv[command_index + 2]; - - if (!bucket_name || !path) - { - printf("Missing arguments: \n"); - status = 1; - goto end_program; - } - else - { - cli_state->curr_cmd_req = command; - cli_state->bucket_name = bucket_name; - if(check_file_path(path) != CLI_VALID_REGULAR_FILE) - { - status = 1; - goto end_program; - } - cli_state->file_path = path; - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } - } - #endif } else if (strcmp(command, "upload-files") == 0) { @@ -2007,10 +1983,6 @@ int main(int argc, char **argv) storj_bridge_create_bucket(env, bucket_name, NULL, create_bucket_callback); } else if (strcmp(command, "remove-bucket") == 0) { - - storj_api->bucket_name = argv[command_index + 1]; - storj_remove_bucket(storj_api); - #if 0 char *bucket_id = argv[command_index + 1]; if (!bucket_id) { @@ -2019,11 +1991,10 @@ int main(int argc, char **argv) goto end_program; } - storj_bridge_delete_bucket(env, bucket_id, NULL, - delete_bucket_callback); - #endif + storj_api->bucket_name = argv[command_index + 1]; + storj_remove_bucket(storj_api); } - else if (strcmp(command, "remove-file") == 0) + else if ((strcmp(command, "remove-file") == 0) || (strcmp(command, "rm") == 0)) { storj_api->bucket_name = argv[command_index + 1]; storj_api->file_name = argv[command_index + 2]; @@ -2036,30 +2007,27 @@ int main(int argc, char **argv) } storj_remove_file(storj_api); - #if 0 - char *bucket_id = argv[command_index + 1]; - char *file_id = argv[command_index + 2]; - - if (!bucket_id || !file_id) { - printf("Missing arguments, expected: \n"); - status = 1; - goto end_program; - } - storj_bridge_delete_file(env, bucket_id, file_id, - NULL, delete_file_callback); - #endif - } - else if (strcmp(command, "list-buckets") == 0) + else if ((strcmp(command, "list-buckets") == 0) || (strcmp(command, "ls") == 0x00)) { - storj_bridge_get_buckets(env, NULL, get_buckets_callback); + if (argv[command_index + 1] != NULL) + { + /* bucket-name , used to list files */ + storj_api->bucket_name = argv[command_index + 1]; + + storj_list_files(storj_api); + } + else + { + storj_bridge_get_buckets(env, NULL, get_buckets_callback); + } } else if (strcmp(command, "get-bucket-id") == 0) { storj_api->bucket_name = argv[command_index + 1]; storj_get_bucket_id(storj_api); } - else if (strcmp(command, "list-mirrors") == 0) + else if ((strcmp(command, "list-mirrors") == 0) || (strcmp(command, "lm") == 0)) { storj_api->bucket_name = argv[command_index + 1]; storj_api->file_name = argv[command_index + 2]; @@ -2072,19 +2040,6 @@ int main(int argc, char **argv) } storj_list_mirrors(storj_api); - - #if 0 - char *bucket_id = argv[command_index + 1]; - char *file_id = argv[command_index + 2]; - - if (!bucket_id || !file_id) { - printf("Missing arguments, expected: \n"); - status = 1; - goto end_program; - } - storj_bridge_list_mirrors(env, bucket_id, file_id, - NULL, list_mirrors_callback); - #endif } else if (strcmp(command, "test-cli") == 0) { From dbb03bc9e42fc08fb165f4f80704639670dce407 Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 25 Jan 2018 13:59:05 -0500 Subject: [PATCH 43/68] adding the linux style 'cp' type upload file command ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x --- src/cli.c | 187 +++++++++++++++++++++++++++++++++++++--- src/storj.h | 12 ++- src/storjapi_callback.c | 16 +++- 3 files changed, 201 insertions(+), 14 deletions(-) diff --git a/src/cli.c b/src/cli.c index 2262083..472e0b8 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1737,7 +1737,6 @@ int main(int argc, char **argv) } else if (file_mnemonic) { free(file_mnemonic); } - } // Third, ask for authentication @@ -1826,9 +1825,15 @@ int main(int argc, char **argv) printf("command = %s; command_index = %d\n", command, command_index); printf("local_file_path (req arg_ = %s\n", local_file_path); - printf("argv[command_index+1] = %s\n", argv[command_index+1]); - printf("argv[command_index+2] = %s\n", argv[command_index+2]); - printf("argv[command_index+3] = %s\n", argv[command_index+3]); + for (int i = 0x00; i < argc; i++) + { + printf("argc = %d; argv[%d] = %s\n", argc, i, argv[i]); + } + + for (int i = 0x00; i < (argc - command_index); i++) + { + printf("argc = %d; argv[command_index+%d] = %s\n", argc, i, argv[command_index + i]); + } if (strcmp(command, "download-file") == 0) { @@ -1879,16 +1884,176 @@ int main(int argc, char **argv) /* cp command wrt to upload-file */ if(local_file_path == NULL) - { - path = argv[command_index + 1]; - bucket_name = argv[command_index + 2]; + { + /* without -r[R] */ + path = argv[command_index + 0x01]; + bucket_name = argv[command_index + 0x02]; } - else + else /* with -r[R] */ { - path = local_file_path; - bucket_name = argv[command_index + 1]; + /* handling single file copy with -r[R]: ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x */ + if (argc == 0x05) + { + /* Handle the src argument */ + /* single file to copy, make sure the files exits */ + if (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE) + { + storj_api->file_name = local_file_path; + + /* Handle the dst argument (storj:/// */ + bucket_name = argv[argc - 0x01]; + printf("bucket-name = %s\n", bucket_name); + + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + + if (num_of_tokens == 0x03) + { + for (int j = 0x00; j < num_of_tokens; j++) + { + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } + + char *dst_file_name = NULL; + + storj_api->bucket_name = token[1]; + dst_file_name = (char *)get_filename_separator(local_file_path); + + if ((strcmp(dst_file_name, token[2]) == 0x00) || + (strcmp(token[2], ".") == 0x00)) + { + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + strcpy(storj_api->src_list, dst_file_name); + storj_api->dst_file = storj_api->src_list; + printf("file uploaded as same %s \n", storj_api->dst_file); + } + else + { + storj_api->dst_file = token[2]; + printf("file uploaded as %s\n", storj_api->dst_file); + } + printf("******* EXECUTE UPLOAD CMD HERE \n"); + storj_upload_file(storj_api); + } + else + { + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } + else + { + if(check_file_path(local_file_path) == CLI_VALID_DIR) + { + char pwd_path[256]= {}; + memset(pwd_path, 0x00, sizeof(pwd_path)); + + char *upload_list_file = pwd_path; + + if ((upload_list_file = getenv("TMPDIR")) != NULL) + { + if (upload_list_file[strlen(upload_list_file)] == '/') + { + strcat(upload_list_file, "STORJ_output_list.txt"); + } + else + { + strcat(upload_list_file, "/STORJ_output_list.txt"); + } + + /* check the directory and create the upload list */ + storj_api->dst_file = upload_list_file; + fprintf(stdout, "upload files list : %s\n", storj_api->dst_file); + FILE *dst_file_fd = NULL; + if ((dst_file_fd = fopen(storj_api->dst_file, "w")) != NULL) + { + printdir(local_file_path, 0x00, dst_file_fd); + } + + /* Handle the dst argument (storj:/// */ + bucket_name = argv[argc - 0x01]; + printf("bucket-name = %s\n", bucket_name); + + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + memset(token, 0x00, sizeof(token)); + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("num of tokes = %d; \n", num_of_tokens); + + if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) + { + for (int j = 0x00; j < num_of_tokens; j++) + { + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } + + char *dst_file_name = NULL; + + storj_api->bucket_name = token[1]; + + if ((token[2] == NULL) || + (strcmp(token[2], ".") == 0x00)) + { + printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); + //storj_upload_file(storj_api); + } + else + { + printf("[%s][%d] Something terrible wrong. Shouldn't be here !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } + else + { + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + + } + } + + printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); + goto end_program; + } + } + + #if 0 + storj_api->file_path = "/tmp/upload_list.txt"; + FILE *file = NULL; + + if ((file = fopen(storj_api->file_path, "w")) != NULL) + { + fprintf(file, "%s\n", path); + //printf("path = %s \n", path); + for (int i = (command_index + 1); i < (argc -1); i++) + { + fprintf(file, "%s\n", argv[i]); + //printf("-->[%d+ %d] %s\n", command_index, i, argv[i]); + } + } + fclose(file); + + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + int *token[0x03]; + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + + for (int j= 0x00; j < num_of_tokens; j++) + { + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } + + if ((num_of_tokens >= 0x02) && (num_of_tokens <= 0x03)) + { + + } + #endif + } + + + #if 0 if (bucket_name != NULL) { if (strpos(bucket_name, "storj://") < 0x00) @@ -1913,12 +2078,14 @@ int main(int argc, char **argv) printf("Invalid command \n"); goto end_program; } + #endif } else if (strcmp(command, "upload-file") == 0) { /* get the corresponding bucket id from the bucket name */ storj_api->bucket_name = argv[command_index + 1]; storj_api->file_name = argv[command_index + 2]; + storj_api->dst_file = NULL; if (!storj_api->bucket_name || !storj_api->file_name) { diff --git a/src/storj.h b/src/storj.h index be80290..e19c073 100755 --- a/src/storj.h +++ b/src/storj.h @@ -591,8 +591,8 @@ typedef struct storj_api { char src_list[256]; /**< file list ready to upload */ char *src_file; /**< next file ready to upload */ FILE *dst_fd; - int xfer_count; /**< # of files xferred (up/down) */ char *dst_file; /**< next file ready to upload */ + int xfer_count; /**< # of files xferred (up/down) */ int total_files; /**< total files to upload */ char *last_cmd_req; /**< last command requested */ char *curr_cmd_req; /**< cli curr command requested */ @@ -1151,6 +1151,16 @@ STORJ_API int storj_upload_files(storj_api_t *storj_api); */ STORJ_API int storj_download_file(storj_api_t *storj_api); +/** + * @brief Function to download files from a given bucket to a + * local folder + * + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +STORJ_API int storj_download_files(storj_api_t *storj_api); + static inline char separator() { #ifdef _WIN32 diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index b732d50..0cbdc79 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -252,6 +252,8 @@ static void upload_signal_handler(uv_signal_t *req, int signum) static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) { + storj_api_t *storj_api = handle; + FILE *fd = fopen(file_path, "r"); if (!fd) { @@ -261,8 +263,16 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, const char *file_name = get_filename_separator(file_path); - if (!file_name) { - file_name = file_path; + if (storj_api->dst_file == NULL) + { + if (!file_name) + { + file_name = file_path; + } + } + else + { + file_name = storj_api->dst_file; } // Upload opts env variables: @@ -472,7 +482,7 @@ static void download_file_complete(int status, FILE *fd, void *handle) "imported correctly.\n\n"); break; default: - printf("[%d][%d]Download failure: %s\n", + printf("[%s][%d]Download failure: %s\n", __FUNCTION__, __LINE__, storj_strerror(status)); } } From c747064412dba40c1f6183127fc5302d99af611c Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 25 Jan 2018 15:18:24 -0500 Subject: [PATCH 44/68] adding the linux style 'cp' type upload file command Commands supported: ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x ./storj cp -r /home/kishore/libstorj/src/cli.o storj://newbucket/. ./storj cp -r /home/kishore/libstorj/src storj://newbucket ./storj upload-files newbucket /home/kishore/libstorj/src ./storj upload-file newbucket /home/kishore/libstorj/src/cli.o --- src/cli.c | 28 +++++++++++++++------------- src/storjapi_callback.c | 36 +++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/cli.c b/src/cli.c index 472e0b8..e0e866b 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1948,9 +1948,12 @@ int main(int argc, char **argv) { char pwd_path[256]= {}; memset(pwd_path, 0x00, sizeof(pwd_path)); - char *upload_list_file = pwd_path; + /* save the src file path */ + storj_api->file_path = local_file_path; + + /* create upload files list based on the file path */ if ((upload_list_file = getenv("TMPDIR")) != NULL) { if (upload_list_file[strlen(upload_list_file)] == '/') @@ -1962,14 +1965,10 @@ int main(int argc, char **argv) strcat(upload_list_file, "/STORJ_output_list.txt"); } - /* check the directory and create the upload list */ - storj_api->dst_file = upload_list_file; - fprintf(stdout, "upload files list : %s\n", storj_api->dst_file); - FILE *dst_file_fd = NULL; - if ((dst_file_fd = fopen(storj_api->dst_file, "w")) != NULL) - { - printdir(local_file_path, 0x00, dst_file_fd); - } + /* check the directory and create the path to upload list file */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); + storj_api->dst_file = storj_api->src_list; /* Handle the dst argument (storj:/// */ bucket_name = argv[argc - 0x01]; @@ -1996,7 +1995,7 @@ int main(int argc, char **argv) (strcmp(token[2], ".") == 0x00)) { printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); - //storj_upload_file(storj_api); + storj_upload_files(storj_api); } else { @@ -2012,9 +2011,11 @@ int main(int argc, char **argv) } } - - printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); - goto end_program; + else + { + printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); + goto end_program; + } } } @@ -2101,6 +2102,7 @@ int main(int argc, char **argv) /* get the corresponding bucket id from the bucket name */ storj_api->bucket_name = argv[command_index + 1]; storj_api->file_path = argv[command_index + 2]; + storj_api->dst_file = NULL; if (!storj_api->bucket_name || !storj_api->file_name) { diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 0cbdc79..5287c3c 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -419,22 +419,44 @@ static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path static void verify_upload_files(void *handle) { storj_api_t *storj_api = handle; - char cwd[256]; int total_src_files = 0x00; int total_dst_files = 0x00; int ret = 0x00; + /* upload list file previously not created? */ + if (storj_api->dst_file == NULL) + { + char pwd_path[256]= {}; + memset(pwd_path, 0x00, sizeof(pwd_path)); + char *upload_list_file = pwd_path; + + printf("am here \n"); + /* create upload files list based on the file path */ + if ((upload_list_file = getenv("TMPDIR")) != NULL) + { + if (upload_list_file[strlen(upload_list_file)] == '/') + { + strcat(upload_list_file, "STORJ_output_list.txt"); + } + else + { + strcat(upload_list_file, "/STORJ_output_list.txt"); + } + + /* check the directory and create the path to upload list file */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); + storj_api->dst_file = storj_api->src_list; + } + } + /* create a upload list file src_list.txt */ - memset(cwd, 0x00, sizeof(cwd)); - strcpy(cwd, storj_api->file_path); - strcat(cwd, "src_list.txt"); - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - strcpy(storj_api->src_list, cwd); + printf("inside verify src list = %s\n",storj_api->src_list); storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; int file_attr = file_exists(handle); - storj_api->src_fd = fopen(cwd, "r"); + storj_api->src_fd = fopen(storj_api->src_list, "r"); if (!storj_api->src_fd) { From 42d642fb43406c30161489a1f53cada25367e8a2 Mon Sep 17 00:00:00 2001 From: Kishore Date: Fri, 26 Jan 2018 11:55:59 -0500 Subject: [PATCH 45/68] storj ls storj list-buckets storj ls storj lm storj list-mirrors storj rm storj remove-file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit upload commands that work: ./storj upload-file testbucket2 /home/kishore/libstorj/src/cli.o ./storj upload-files testbucket2 /home/kishore/libstorj/src/ ./storj upload-files testbucket2 /home/kishore/libstorj/src ./storj cp -r /home/kishore/libstorj/src/cli.c storj://testbucket2/. ./storj cp -r /home/kishore/libstorj/src/cli.c storj://testbucket2/clitest.c ./storj cp -r /home/kishore/libstorj/src storj://testbucket2 ./storj cp -r /home/kishore/libstorj/src storj://testbucket2/ ./storj cp -r /home/kishore/libstorj/src/ storj://testbucket2 ./storj cp -r /home/kishore/libstorj/src/ storj://testbucket2/ ./storj cp -r /home/kishore/libstorj/src/ storj://testbucket2/. ./storj cp -r /home/kishore/libstorj/src/ storj://testbucket2/* → [main][2045] storj://; storj:/// storj:///. !!!!! ./storj cp -r /home/kishore/libstorj/src/* storj://testbucket2/ ./storj cp -r /home/kishore/libstorj/src/* storj://testbucket2/. ./storj cp -r /home/kishore/libstorj/src/* storj://testbucket2/* → [main][2045] storj://; storj:/// storj:///. !!!!! ./storj cp -r /home/kishore/libstorj/src/ strj://testbucket2/ → [main][2051] Valid dst filename missing !!!!! ./storj cp -r /home/kishore/libstorj/src/* storj:///testbucket2/ → extra slashes are ignored ./storj cp -r /home/kishore/libstorj/src/* storj:////////testbucket2/ → extra slashes are ignored ./storj cp -r /home/kishore/libstorj/src/* storj:// → Missing : ./storj cp -r storj:////////testbucket2/ → [main][2051] Valid dst filename missing !!!!! ./storj cp -r /home/kishore/libstorj/src/* → [main][2051] Valid dst filename missing !!!!! ./storj cp -r storj://testbucket2/ /home/kishore/libstorj/src/ → [main][2051] Valid dst filename missing !!!!! ./storj cp -r /home/kishore/libstorj/src/*.c storj:///testbucket2/ → selective files using wild characters ./storj cp -r /home/kishore/libstorj/src/sto*.c storj:///testbucket2/→ selective files using wild characters Download commands that work: ./storj download-file testbucket2 clitest.c /tmp/tst.c ./storj download-files testbucket2 /tmp/ --- src/cli.c | 282 ++++++++++++++++++---------------------- src/storjapi_callback.c | 15 ++- 2 files changed, 137 insertions(+), 160 deletions(-) diff --git a/src/cli.c b/src/cli.c index e0e866b..147452a 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1473,6 +1473,7 @@ static int export_keys(char *host) int main(int argc, char **argv) { int status = 0; + char temp_buff[256] = {}; static struct option cmd_options[] = { {"url", required_argument, 0, 'u'}, @@ -1892,123 +1893,112 @@ int main(int argc, char **argv) else /* with -r[R] */ { /* handling single file copy with -r[R]: ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x */ - if (argc == 0x05) + /* Handle the src argument */ + /* single file to copy, make sure the files exits */ + if ((argc == 0x05) && (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE)) { - /* Handle the src argument */ - /* single file to copy, make sure the files exits */ - if (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE) - { - storj_api->file_name = local_file_path; + storj_api->file_name = local_file_path; - /* Handle the dst argument (storj:/// */ - bucket_name = argv[argc - 0x01]; - printf("bucket-name = %s\n", bucket_name); + /* Handle the dst argument (storj:/// */ + bucket_name = argv[argc - 0x01]; + printf("bucket-name = %s\n", bucket_name); - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - char *token[0x03]; - int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - if (num_of_tokens == 0x03) + if (num_of_tokens == 0x03) + { + for (int j = 0x00; j < num_of_tokens; j++) { - for (int j = 0x00; j < num_of_tokens; j++) - { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } - char *dst_file_name = NULL; + char *dst_file_name = NULL; - storj_api->bucket_name = token[1]; - dst_file_name = (char *)get_filename_separator(local_file_path); + storj_api->bucket_name = token[1]; + dst_file_name = (char *)get_filename_separator(local_file_path); - if ((strcmp(dst_file_name, token[2]) == 0x00) || - (strcmp(token[2], ".") == 0x00)) - { - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - strcpy(storj_api->src_list, dst_file_name); - storj_api->dst_file = storj_api->src_list; - printf("file uploaded as same %s \n", storj_api->dst_file); - } - else - { - storj_api->dst_file = token[2]; - printf("file uploaded as %s\n", storj_api->dst_file); - } - printf("******* EXECUTE UPLOAD CMD HERE \n"); - storj_upload_file(storj_api); + if ((strcmp(dst_file_name, token[2]) == 0x00) || + (strcmp(token[2], ".") == 0x00)) + { + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + strcpy(storj_api->src_list, dst_file_name); + storj_api->dst_file = storj_api->src_list; + printf("file uploaded as same %s \n", storj_api->dst_file); } else { - printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); - goto end_program; + storj_api->dst_file = token[2]; + printf("file uploaded as %s\n", storj_api->dst_file); } + printf("******* EXECUTE UPLOAD CMD HERE \n"); + storj_upload_file(storj_api); } else { - if(check_file_path(local_file_path) == CLI_VALID_DIR) + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } + else + { + /* save the src file path */ + storj_api->file_path = local_file_path; + + char pwd_path[256]= {}; + memset(pwd_path, 0x00, sizeof(pwd_path)); + char *upload_list_file = pwd_path; + + /* create upload files list based on the file path */ + if ((upload_list_file = getenv("TMPDIR")) != NULL) + { + printf("uploadlistfile = %s\n", upload_list_file); + printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); + if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') + { + strcat(upload_list_file, "STORJ_output_list.txt"); + } + else { - char pwd_path[256]= {}; - memset(pwd_path, 0x00, sizeof(pwd_path)); - char *upload_list_file = pwd_path; + strcat(upload_list_file, "/STORJ_output_list.txt"); + } + + /* check the directory and create the path to upload list file */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); + storj_api->dst_file = storj_api->src_list; + printf("**dst_file = %s\n", storj_api->dst_file); + } + else + { + printf("[%s][%d] Upload list file generation error!!! \n", + __FUNCTION__, __LINE__); + goto end_program; + } - /* save the src file path */ - storj_api->file_path = local_file_path; + /* SRC PATH IS A DIRECTORY */ + if(check_file_path(local_file_path) != CLI_VALID_DIR) + { + /* if local file path is a file, then just get the directory + from that */ + char *ret = NULL; + ret = strrchr(local_file_path, '/'); + memset(temp_buff, 0x00, sizeof(temp_buff)); + memcpy(temp_buff, local_file_path, (ret-local_file_path)); - /* create upload files list based on the file path */ - if ((upload_list_file = getenv("TMPDIR")) != NULL) - { - if (upload_list_file[strlen(upload_list_file)] == '/') - { - strcat(upload_list_file, "STORJ_output_list.txt"); - } - else - { - strcat(upload_list_file, "/STORJ_output_list.txt"); - } - - /* check the directory and create the path to upload list file */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); - storj_api->dst_file = storj_api->src_list; - - /* Handle the dst argument (storj:/// */ - bucket_name = argv[argc - 0x01]; - printf("bucket-name = %s\n", bucket_name); - - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - char *token[0x03]; - memset(token, 0x00, sizeof(token)); - int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("num of tokes = %d; \n", num_of_tokens); - - if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) - { - for (int j = 0x00; j < num_of_tokens; j++) - { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } - - char *dst_file_name = NULL; - - storj_api->bucket_name = token[1]; - - if ((token[2] == NULL) || - (strcmp(token[2], ".") == 0x00)) - { - printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); - storj_upload_files(storj_api); - } - else - { - printf("[%s][%d] Something terrible wrong. Shouldn't be here !!!!!\n", __FUNCTION__, __LINE__); - goto end_program; - } - } - else - { - printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); - goto end_program; - } + printf("ret = %s diff = %d temp = %s\n", ret, ret -local_file_path, temp_buff); + FILE *file= NULL; + if ((file = fopen(storj_api->src_list, "w")) != NULL) + { + fprintf(file, "%s\n", local_file_path); + fprintf(stdout, "%s\n", local_file_path); + + for (int i = 0x01; i < ((argc - command_index) - 1); i++) + { + fprintf(file, "%s\n", argv[command_index + i]); + fprintf(stdout, "%s\n", argv[command_index + i]); } } else @@ -2016,70 +2006,54 @@ int main(int argc, char **argv) printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); goto end_program; } - } - } - - #if 0 - storj_api->file_path = "/tmp/upload_list.txt"; - FILE *file = NULL; - if ((file = fopen(storj_api->file_path, "w")) != NULL) - { - fprintf(file, "%s\n", path); - //printf("path = %s \n", path); - for (int i = (command_index + 1); i < (argc -1); i++) - { - fprintf(file, "%s\n", argv[i]); - //printf("-->[%d+ %d] %s\n", command_index, i, argv[i]); - } - } - fclose(file); - - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - int *token[0x03]; - int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + fclose(file); - for (int j= 0x00; j < num_of_tokens; j++) - { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } + storj_api->file_path = temp_buff; + printf("src file path = %s\n", storj_api->file_path); + } - if ((num_of_tokens >= 0x02) && (num_of_tokens <= 0x03)) - { + /* Handle the dst argument (storj:/// */ + bucket_name = argv[argc - 0x01]; + printf("bucket-name = %s\n", bucket_name); - } - #endif - - } + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + memset(token, 0x00, sizeof(token)); + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("num of tokes = %d; \n", num_of_tokens); - - - #if 0 - if (bucket_name != NULL) - { - if (strpos(bucket_name, "storj://") < 0x00) - { - /* download-file command */ - if(cli_download_file(bucket_name, path, cli_state) < 0x00) + if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) { - goto end_program; - } - } - else - { - /* upload-file command */ - if(cli_upload_file(path,bucket_name,cli_state) < 0x00) + for (int j = 0x00; j < num_of_tokens; j++) + { + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } + + char *dst_file_name = NULL; + + storj_api->bucket_name = token[1]; + + if ((token[2] == NULL) || + (strcmp(token[2], ".") == 0x00)) + { + printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); + storj_upload_files(storj_api); + } + else + { + printf("[%s][%d] storj://; storj:/// storj:///. !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } + else { + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); goto end_program; } - } - } - else - { - printf("Invalid command \n"); - goto end_program; + + } } - #endif } else if (strcmp(command, "upload-file") == 0) { @@ -2110,8 +2084,8 @@ int main(int argc, char **argv) status = 1; goto end_program; } - - storj_upload_files(storj_api); + + storj_upload_files(storj_api); } else if (strcmp(command, "download-files") == 0) { diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 5287c3c..3275a70 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -434,7 +434,7 @@ static void verify_upload_files(void *handle) /* create upload files list based on the file path */ if ((upload_list_file = getenv("TMPDIR")) != NULL) { - if (upload_list_file[strlen(upload_list_file)] == '/') + if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') { strcat(upload_list_file, "STORJ_output_list.txt"); } @@ -448,14 +448,15 @@ static void verify_upload_files(void *handle) memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); storj_api->dst_file = storj_api->src_list; } - } - /* create a upload list file src_list.txt */ - printf("inside verify src list = %s\n",storj_api->src_list); + /* create a upload list file src_list.txt */ + printf("inside verify src list = %s\n",storj_api->src_list); - storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; - int file_attr = file_exists(handle); + int file_attr = file_exists(handle); + } + /* create a upload list file src_list.txt */ + printf("inside verify src list = %s\n",storj_api->src_list); storj_api->src_fd = fopen(storj_api->src_list, "r"); if (!storj_api->src_fd) @@ -484,7 +485,9 @@ static void verify_upload_files(void *handle) storj_api->total_files = total_src_files; storj_api->xfer_count = 0x01; + printf("[][] total files = %d, xfer_count = %d\n", storj_api->total_files, storj_api->xfer_count); + storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; queue_next_cmd_req(storj_api); } From 07ea11ad58b28cafd73e3f88283c1d73da6adce3 Mon Sep 17 00:00:00 2001 From: Kishore Date: Fri, 26 Jan 2018 16:16:04 -0500 Subject: [PATCH 46/68] =?UTF-8?q?Supported=20=E2=86=92=20./storj=20cp=20-r?= =?UTF-8?q?=20/home/kishore/libstorj/src/cli.c=20storj://testbucket2/=20?= =?UTF-8?q?=E2=86=92=20./storj=20cp=20-r=20/home/kishore/libstorj/src/cli.?= =?UTF-8?q?c=20storj://testbucket2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cli.c b/src/cli.c index 147452a..031e343 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1905,9 +1905,10 @@ int main(int argc, char **argv) /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ char *token[0x03]; + memset(token,0x00, sizeof(token)); int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - if (num_of_tokens == 0x03) + if ((num_of_tokens == 0x02) || (num_of_tokens == 0x03)) { for (int j = 0x00; j < num_of_tokens; j++) { @@ -1919,7 +1920,7 @@ int main(int argc, char **argv) storj_api->bucket_name = token[1]; dst_file_name = (char *)get_filename_separator(local_file_path); - if ((strcmp(dst_file_name, token[2]) == 0x00) || + if ((token[2] == NULL) || (strcmp(dst_file_name, token[2]) == 0x00) || (strcmp(token[2], ".") == 0x00)) { memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); From 92770fb8f8005420776d246b646d9d968b6a505d Mon Sep 17 00:00:00 2001 From: Kishore Date: Sat, 27 Jan 2018 19:32:37 -0500 Subject: [PATCH 47/68] Added recursive and non recursive ie cp -r[R] option of upload files command --- src/cli.c | 338 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 210 insertions(+), 128 deletions(-) diff --git a/src/cli.c b/src/cli.c index 031e343..b29ec33 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1879,181 +1879,263 @@ int main(int argc, char **argv) } else if (strcmp(command, "cp") == 0) { + #define UPLOAD_CMD 0x00 + #define DOWNLOAD_CMD 0x01 + #define RECURSIVE_CMD 0x02 + #define NON_RECURSIVE_CMD 0x03 + int ret = 0x00; - char *path = NULL; + char *src_path = NULL; /* holds the local path */ + char *dst_path = NULL; /* holds the storj:// path */ char *bucket_name = NULL; + int cmd_type = 0x00; /* 0-> upload and 1 -> download */ /* cp command wrt to upload-file */ - if(local_file_path == NULL) + if(local_file_path == NULL)/* without -r[R] */ { - /* without -r[R] */ - path = argv[command_index + 0x01]; - bucket_name = argv[command_index + 0x02]; + /* hold the local path */ + src_path = argv[command_index + 0x01]; + + /* Handle the dst argument (storj:/// */ + dst_path = argv[argc - 0x01]; + printf("bucket-name = %s\n", bucket_name); + + cmd_type = NON_RECURSIVE_CMD; } - else /* with -r[R] */ + else /* with -r[R] */ { - /* handling single file copy with -r[R]: ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x */ - /* Handle the src argument */ - /* single file to copy, make sure the files exits */ - if ((argc == 0x05) && (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE)) + src_path = local_file_path; + + /* Handle the dst argument (storj:/// */ + dst_path = argv[argc - 0x01]; + + cmd_type = RECURSIVE_CMD; + } + + char sub_str[] = "storj://"; + + /* check for upload or download command */ + ret = strpos(dst_path, sub_str); + if( ret == 0x00) + { + if (cmd_type == NON_RECURSIVE_CMD) { - storj_api->file_name = local_file_path; + if (check_file_path(src_path) != CLI_VALID_DIR) + { + local_file_path = src_path; + printf("[main.c][1906] local_file_path = %s\n", local_file_path); - /* Handle the dst argument (storj:/// */ - bucket_name = argv[argc - 0x01]; - printf("bucket-name = %s\n", bucket_name); + bucket_name = dst_path; + printf("[main.c][1912] bucket-name = %s\n", bucket_name); + } + else + { + printf("[%s][%d] Invalid command entry\n", + __FUNCTION__, __LINE__); + goto end_program; + } + } + else if (cmd_type == RECURSIVE_CMD) + { + local_file_path = src_path; + printf("[main.c][1906] local_file_path = %s\n", local_file_path); - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - char *token[0x03]; - memset(token,0x00, sizeof(token)); - int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + bucket_name = dst_path; + printf("[main.c][1912] bucket-name = %s\n", bucket_name); + } + else + { + printf("[%s][%d] Invalid command entry \n", __FUNCTION__, __LINE__); + goto end_program; + } - if ((num_of_tokens == 0x02) || (num_of_tokens == 0x03)) + printf("[main.c][1921] upload command\n"); + cmd_type = UPLOAD_CMD; + } + else if (ret == -1) + { + ret = strpos(src_path, sub_str); + + if (ret == 0x00) + { + printf("[main.c][1941] download command\n"); + cmd_type = DOWNLOAD_CMD; + + local_file_path = dst_path; + printf("[main.c][1906] local_file_path = %s\n", local_file_path); + + bucket_name = src_path; + printf("[main.c][1912] bucket-name = %s\n", bucket_name); + } + else + { + printf("[%s][%d]Invalid Command Entry (%d), \ntry ... stroj:///\n", + __FUNCTION__, __LINE__, ret); + goto end_program; + } + } + else + { + printf("[%s][%d]Invalid Command Entry (%d), \ntry ... stroj:///\n", + __FUNCTION__, __LINE__, ret); + goto end_program; + } + + /* handling single file copy with -r[R]: ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x */ + /* Handle the src argument */ + /* single file to copy, make sure the files exits */ + if ((argc == 0x05) && (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE)) + { + storj_api->file_name = local_file_path; + + /* Handle the dst argument (storj:/// */ + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + memset(token,0x00, sizeof(token)); + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + + if ((num_of_tokens == 0x02) || (num_of_tokens == 0x03)) + { + for (int j = 0x00; j < num_of_tokens; j++) { - for (int j = 0x00; j < num_of_tokens; j++) - { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } - char *dst_file_name = NULL; + char *dst_file_name = NULL; - storj_api->bucket_name = token[1]; - dst_file_name = (char *)get_filename_separator(local_file_path); + storj_api->bucket_name = token[1]; + dst_file_name = (char *)get_filename_separator(local_file_path); - if ((token[2] == NULL) || (strcmp(dst_file_name, token[2]) == 0x00) || - (strcmp(token[2], ".") == 0x00)) - { - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - strcpy(storj_api->src_list, dst_file_name); - storj_api->dst_file = storj_api->src_list; - printf("file uploaded as same %s \n", storj_api->dst_file); - } - else - { - storj_api->dst_file = token[2]; - printf("file uploaded as %s\n", storj_api->dst_file); - } - printf("******* EXECUTE UPLOAD CMD HERE \n"); - storj_upload_file(storj_api); + if ((token[2] == NULL) || (strcmp(dst_file_name, token[2]) == 0x00) || + (strcmp(token[2], ".") == 0x00)) + { + /* use the src list buff as temp memory to hold the dst filename */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + strcpy(storj_api->src_list, dst_file_name); + storj_api->dst_file = storj_api->src_list; + printf("file uploaded as same %s \n", storj_api->dst_file); } else { - printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); - goto end_program; + storj_api->dst_file = token[2]; + printf("file uploaded as %s\n", storj_api->dst_file); } + printf("******* EXECUTE UPLOAD CMD HERE \n"); + storj_upload_file(storj_api); } else { - /* save the src file path */ - storj_api->file_path = local_file_path; + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } + else + { + /* directory is being used, store it in file_path */ + storj_api->file_path = local_file_path; - char pwd_path[256]= {}; - memset(pwd_path, 0x00, sizeof(pwd_path)); - char *upload_list_file = pwd_path; + char pwd_path[256]= {}; + memset(pwd_path, 0x00, sizeof(pwd_path)); + char *upload_list_file = pwd_path; - /* create upload files list based on the file path */ - if ((upload_list_file = getenv("TMPDIR")) != NULL) + /* create "/tmp/STORJ_upload_list_file.txt" upload files list based on the file path */ + if ((upload_list_file = getenv("TMPDIR")) != NULL) + { + printf("uploadlistfile = %s\n", upload_list_file); + printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); + if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') { - printf("uploadlistfile = %s\n", upload_list_file); - printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); - if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') - { - strcat(upload_list_file, "STORJ_output_list.txt"); - } - else - { - strcat(upload_list_file, "/STORJ_output_list.txt"); - } - - /* check the directory and create the path to upload list file */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); - storj_api->dst_file = storj_api->src_list; - printf("**dst_file = %s\n", storj_api->dst_file); + strcat(upload_list_file, "STORJ_output_list.txt"); } else { - printf("[%s][%d] Upload list file generation error!!! \n", - __FUNCTION__, __LINE__); - goto end_program; + strcat(upload_list_file, "/STORJ_output_list.txt"); } - /* SRC PATH IS A DIRECTORY */ - if(check_file_path(local_file_path) != CLI_VALID_DIR) - { - /* if local file path is a file, then just get the directory - from that */ - char *ret = NULL; - ret = strrchr(local_file_path, '/'); - memset(temp_buff, 0x00, sizeof(temp_buff)); - memcpy(temp_buff, local_file_path, (ret-local_file_path)); + /* check the directory and create the path to upload list file */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); + storj_api->dst_file = storj_api->src_list; + printf("**dst_file = %s\n", storj_api->dst_file); + } + else + { + printf("[%s][%d] Upload list file generation error!!! \n", + __FUNCTION__, __LINE__); + goto end_program; + } - printf("ret = %s diff = %d temp = %s\n", ret, ret -local_file_path, temp_buff); + /* Handle wild character options for files selection */ + if(check_file_path(local_file_path) != CLI_VALID_DIR) + { + /* if local file path is a file, then just get the directory + from that */ + char *ret = NULL; + ret = strrchr(local_file_path, '/'); + memset(temp_buff, 0x00, sizeof(temp_buff)); + memcpy(temp_buff, local_file_path, (ret-local_file_path)); + + printf("ret = %s diff = %d temp = %s\n", ret, ret -local_file_path, temp_buff); + + FILE *file= NULL; + /* create the file and add the list of files to be uploaded */ + if ((file = fopen(storj_api->src_list, "w")) != NULL) + { + fprintf(file, "%s\n", local_file_path); + fprintf(stdout, "%s\n", local_file_path); - FILE *file= NULL; - if ((file = fopen(storj_api->src_list, "w")) != NULL) - { - fprintf(file, "%s\n", local_file_path); - fprintf(stdout, "%s\n", local_file_path); - - for (int i = 0x01; i < ((argc - command_index) - 1); i++) - { - fprintf(file, "%s\n", argv[command_index + i]); - fprintf(stdout, "%s\n", argv[command_index + i]); - } - } - else + for (int i = 0x01; i < ((argc - command_index) - 1); i++) { - printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); - goto end_program; + fprintf(file, "%s\n", argv[command_index + i]); + fprintf(stdout, "%s\n", argv[command_index + i]); } - - fclose(file); - - storj_api->file_path = temp_buff; - printf("src file path = %s\n", storj_api->file_path); } + else + { + printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); + goto end_program; + } + fclose(file); - /* Handle the dst argument (storj:/// */ - bucket_name = argv[argc - 0x01]; - printf("bucket-name = %s\n", bucket_name); + storj_api->file_path = temp_buff; + printf("src file path = %s\n", storj_api->file_path); + } - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - char *token[0x03]; - memset(token, 0x00, sizeof(token)); - int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("num of tokes = %d; \n", num_of_tokens); + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + memset(token, 0x00, sizeof(token)); + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("num of tokes = %d; \n", num_of_tokens); - if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) + if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) + { + for (int j = 0x00; j < num_of_tokens; j++) { - for (int j = 0x00; j < num_of_tokens; j++) - { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } - char *dst_file_name = NULL; + char *dst_file_name = NULL; - storj_api->bucket_name = token[1]; + storj_api->bucket_name = token[1]; - if ((token[2] == NULL) || - (strcmp(token[2], ".") == 0x00)) - { - printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); - storj_upload_files(storj_api); - } - else - { - printf("[%s][%d] storj://; storj:/// storj:///. !!!!!\n", __FUNCTION__, __LINE__); - goto end_program; - } - } + if ((token[2] == NULL) || + (strcmp(token[2], ".") == 0x00)) + { + printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); + storj_upload_files(storj_api); + } else { - printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + printf("[%s][%d] storj://; storj:/// storj:///. !!!!!\n", __FUNCTION__, __LINE__); goto end_program; } - } + else + { + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } } else if (strcmp(command, "upload-file") == 0) From d84721a0b8495a0f8dd7e57f14d7990e470cd3d3 Mon Sep 17 00:00:00 2001 From: Kishore Date: Sun, 28 Jan 2018 13:00:27 -0500 Subject: [PATCH 48/68] Handles different cp valid and invalid options --- src/cli.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/cli.c b/src/cli.c index b29ec33..af45ce1 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1533,6 +1533,10 @@ int main(int argc, char **argv) printf(HELP_TEXT); exit(0); break; + default: + exit(0); + break; + } } @@ -1904,29 +1908,40 @@ int main(int argc, char **argv) } else /* with -r[R] */ { - src_path = local_file_path; + if ((strcmp(argv[1],"-r") == 0x00) || (strcmp(argv[1],"-R") == 0x00)) + { + src_path = local_file_path; - /* Handle the dst argument (storj:/// */ - dst_path = argv[argc - 0x01]; + /* Handle the dst argument (storj:/// */ + dst_path = argv[argc - 0x01]; - cmd_type = RECURSIVE_CMD; + cmd_type = RECURSIVE_CMD; + } + else + { + printf("[%s][%d] Invalid command option '%s'\n", + __FUNCTION__, __LINE__, argv[1]); + goto end_program; + } } char sub_str[] = "storj://"; /* check for upload or download command */ ret = strpos(dst_path, sub_str); - if( ret == 0x00) + if( ret == 0x00) /* Handle upload command*/ { if (cmd_type == NON_RECURSIVE_CMD) { if (check_file_path(src_path) != CLI_VALID_DIR) { local_file_path = src_path; - printf("[main.c][1906] local_file_path = %s\n", local_file_path); + printf("[%s][%d] local_file_path = %s\n", + __FUNCTION__, __LINE__, local_file_path); bucket_name = dst_path; - printf("[main.c][1912] bucket-name = %s\n", bucket_name); + printf("[%s][%d] bucket-name = %s\n", + __FUNCTION__, __LINE__, bucket_name); } else { @@ -1938,10 +1953,12 @@ int main(int argc, char **argv) else if (cmd_type == RECURSIVE_CMD) { local_file_path = src_path; - printf("[main.c][1906] local_file_path = %s\n", local_file_path); + printf("[%s][%d] local_file_path = %s\n", + __FUNCTION__, __LINE__, local_file_path); bucket_name = dst_path; - printf("[main.c][1912] bucket-name = %s\n", bucket_name); + printf("[%s][%d] bucket-name = %s\n", + __FUNCTION__, __LINE__, bucket_name); } else { @@ -1952,7 +1969,7 @@ int main(int argc, char **argv) printf("[main.c][1921] upload command\n"); cmd_type = UPLOAD_CMD; } - else if (ret == -1) + else if (ret == -1) /* Handle download command*/ { ret = strpos(src_path, sub_str); From c2259946084391a4a20a4e5bd1660ac20fdecc5b Mon Sep 17 00:00:00 2001 From: Kishore Date: Mon, 29 Jan 2018 12:37:50 -0500 Subject: [PATCH 49/68] added support for recursive and non-recursive 'cp' style download file command --- src/cli.c | 364 +++++++++++++++++++++++++++------------- src/storjapi_callback.c | 4 + 2 files changed, 255 insertions(+), 113 deletions(-) diff --git a/src/cli.c b/src/cli.c index af45ce1..a1ed417 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1925,10 +1925,10 @@ int main(int argc, char **argv) } } - char sub_str[] = "storj://"; - /* check for upload or download command */ + char sub_str[] = "storj://"; ret = strpos(dst_path, sub_str); + if( ret == 0x00) /* Handle upload command*/ { if (cmd_type == NON_RECURSIVE_CMD) @@ -1975,148 +1975,270 @@ int main(int argc, char **argv) if (ret == 0x00) { - printf("[main.c][1941] download command\n"); - cmd_type = DOWNLOAD_CMD; + if ((cmd_type == NON_RECURSIVE_CMD) || (cmd_type == RECURSIVE_CMD)) + { + local_file_path = dst_path; + bucket_name = src_path; + + char *dst_file_name = NULL; + dst_file_name = get_filename_separator(local_file_path); + printf("[%s][%d] local_file_path = %s, dst_file_name = %s and leng = %d\n", + __FUNCTION__, __LINE__, local_file_path, dst_file_name, strlen(dst_file_name)); - local_file_path = dst_path; - printf("[main.c][1906] local_file_path = %s\n", local_file_path); + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + memset(token, 0x00, sizeof(token)); + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("num of tokes = %d; \n", num_of_tokens); - bucket_name = src_path; - printf("[main.c][1912] bucket-name = %s\n", bucket_name); + /* set the bucket name from which file(s) to be downloaded */ + storj_api->bucket_name = token[1]; + + /* initialize the local folder to copy the downloaded file into */ + storj_api->file_path = local_file_path; + + /* initialize the filename to be downloaded as */ + storj_api->dst_file = dst_file_name; + + if ((argc == 0x04) || (argc == 0x05)) /* handle non recursive operations */ + { + if (strcmp(token[2], "*") == 0x00) + { + if (check_file_path(local_file_path) == CLI_VALID_DIR) + { + storj_download_files(storj_api); + } + else + { + printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", + __FUNCTION__, __LINE__, local_file_path); + goto end_program; + } + } + else if (token[2] != NULL) + { + /* set the file to be downloaded */ + storj_api->file_name = token[2]; + + if ((check_file_path(local_file_path) == CLI_VALID_DIR) || (strcmp(dst_file_name, ".") == 0x00)) + { + memset(temp_buff, 0x00, sizeof(temp_buff)); + strcat(temp_buff, local_file_path); + strcat(temp_buff, "/"); + strcat(temp_buff, token[2]); + storj_api->dst_file = temp_buff; + } + else if (strlen(dst_file_name) > 0x00) + { + storj_api->dst_file = local_file_path; + } + else + { + printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", + __FUNCTION__, __LINE__, local_file_path); + goto end_program; + } + printf("[%s][%d]file %s downloaded from bucketname = %s as file %s\n", + __FUNCTION__, __LINE__, storj_api->file_name, storj_api->bucket_name, storj_api->dst_file); + storj_download_file(storj_api); + } + else + { + printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", + __FUNCTION__, __LINE__, local_file_path); + goto end_program; + } + } + else + { + /* more args then needed wrong command */ + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } + else + { + printf("[%s][%d] Invalid command entry \n", __FUNCTION__, __LINE__); + goto end_program; + } } else { - printf("[%s][%d]Invalid Command Entry (%d), \ntry ... stroj:///\n", + printf("[%s][%d]Invalid Command Entry (%d) \n", __FUNCTION__, __LINE__, ret); goto end_program; } } else { - printf("[%s][%d]Invalid Command Entry (%d), \ntry ... stroj:///\n", + printf("[%s][%d]Invalid Command Entry (%d) \n", __FUNCTION__, __LINE__, ret); goto end_program; } - /* handling single file copy with -r[R]: ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x */ - /* Handle the src argument */ - /* single file to copy, make sure the files exits */ - if ((argc == 0x05) && (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE)) + if (cmd_type == UPLOAD_CMD) { - storj_api->file_name = local_file_path; + /* handling single file copy with -r[R]: ./storj cp -r /home/kishore/libstorj/src/xxx.y storj://testbucket/yyy.x */ + /* Handle the src argument */ + /* single file to copy, make sure the files exits */ + if ((argc == 0x05) && (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE)) + { + storj_api->file_name = local_file_path; - /* Handle the dst argument (storj:/// */ - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - char *token[0x03]; - memset(token,0x00, sizeof(token)); - int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + /* Handle the dst argument (storj:/// */ + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + memset(token,0x00, sizeof(token)); + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - if ((num_of_tokens == 0x02) || (num_of_tokens == 0x03)) - { - for (int j = 0x00; j < num_of_tokens; j++) + if ((num_of_tokens == 0x02) || (num_of_tokens == 0x03)) { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } + for (int j = 0x00; j < num_of_tokens; j++) + { + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } - char *dst_file_name = NULL; + char *dst_file_name = NULL; - storj_api->bucket_name = token[1]; - dst_file_name = (char *)get_filename_separator(local_file_path); + storj_api->bucket_name = token[1]; + dst_file_name = (char *)get_filename_separator(local_file_path); - if ((token[2] == NULL) || (strcmp(dst_file_name, token[2]) == 0x00) || - (strcmp(token[2], ".") == 0x00)) - { - /* use the src list buff as temp memory to hold the dst filename */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - strcpy(storj_api->src_list, dst_file_name); - storj_api->dst_file = storj_api->src_list; - printf("file uploaded as same %s \n", storj_api->dst_file); + if ((token[2] == NULL) || (strcmp(dst_file_name, token[2]) == 0x00) || + (strcmp(token[2], ".") == 0x00)) + { + /* use the src list buff as temp memory to hold the dst filename */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + strcpy(storj_api->src_list, dst_file_name); + storj_api->dst_file = storj_api->src_list; + printf("file uploaded as same %s \n", storj_api->dst_file); + } + else + { + storj_api->dst_file = token[2]; + printf("file uploaded as %s\n", storj_api->dst_file); + } + printf("******* EXECUTE UPLOAD CMD HERE \n"); + storj_upload_file(storj_api); } else { - storj_api->dst_file = token[2]; - printf("file uploaded as %s\n", storj_api->dst_file); + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; } - printf("******* EXECUTE UPLOAD CMD HERE \n"); - storj_upload_file(storj_api); } else { - printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); - goto end_program; - } - } - else - { - /* directory is being used, store it in file_path */ - storj_api->file_path = local_file_path; + /* directory is being used, store it in file_path */ + storj_api->file_path = local_file_path; - char pwd_path[256]= {}; - memset(pwd_path, 0x00, sizeof(pwd_path)); - char *upload_list_file = pwd_path; + char pwd_path[256]= {}; + memset(pwd_path, 0x00, sizeof(pwd_path)); + char *upload_list_file = pwd_path; - /* create "/tmp/STORJ_upload_list_file.txt" upload files list based on the file path */ - if ((upload_list_file = getenv("TMPDIR")) != NULL) - { - printf("uploadlistfile = %s\n", upload_list_file); - printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); - if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') + /* create "/tmp/STORJ_upload_list_file.txt" upload files list based on the file path */ + if ((upload_list_file = getenv("TMPDIR")) != NULL) { - strcat(upload_list_file, "STORJ_output_list.txt"); + printf("uploadlistfile = %s\n", upload_list_file); + printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); + if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') + { + strcat(upload_list_file, "STORJ_output_list.txt"); + } + else + { + strcat(upload_list_file, "/STORJ_output_list.txt"); + } + + /* check the directory and create the path to upload list file */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); + storj_api->dst_file = storj_api->src_list; + printf("**dst_file = %s\n", storj_api->dst_file); } else { - strcat(upload_list_file, "/STORJ_output_list.txt"); + printf("[%s][%d] Upload list file generation error!!! \n", + __FUNCTION__, __LINE__); + goto end_program; } - /* check the directory and create the path to upload list file */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); - storj_api->dst_file = storj_api->src_list; - printf("**dst_file = %s\n", storj_api->dst_file); - } - else - { - printf("[%s][%d] Upload list file generation error!!! \n", - __FUNCTION__, __LINE__); - goto end_program; - } + /* Handle wild character options for files selection */ + if(check_file_path(local_file_path) != CLI_VALID_DIR) + { + /* if local file path is a file, then just get the directory + from that */ + char *ret = NULL; + ret = strrchr(local_file_path, '/'); + memset(temp_buff, 0x00, sizeof(temp_buff)); + memcpy(temp_buff, local_file_path, (ret-local_file_path)); + + printf("ret = %s diff = %d temp = %s\n", ret, ret -local_file_path, temp_buff); + + FILE *file= NULL; + /* create the file and add the list of files to be uploaded */ + if ((file = fopen(storj_api->src_list, "w")) != NULL) + { + fprintf(file, "%s\n", local_file_path); + fprintf(stdout, "%s\n", local_file_path); + + for (int i = 0x01; i < ((argc - command_index) - 1); i++) + { + fprintf(file, "%s\n", argv[command_index + i]); + fprintf(stdout, "%s\n", argv[command_index + i]); + } + } + else + { + printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); + goto end_program; + } + fclose(file); - /* Handle wild character options for files selection */ - if(check_file_path(local_file_path) != CLI_VALID_DIR) - { - /* if local file path is a file, then just get the directory - from that */ - char *ret = NULL; - ret = strrchr(local_file_path, '/'); - memset(temp_buff, 0x00, sizeof(temp_buff)); - memcpy(temp_buff, local_file_path, (ret-local_file_path)); - - printf("ret = %s diff = %d temp = %s\n", ret, ret -local_file_path, temp_buff); - - FILE *file= NULL; - /* create the file and add the list of files to be uploaded */ - if ((file = fopen(storj_api->src_list, "w")) != NULL) + storj_api->file_path = temp_buff; + printf("src file path = %s\n", storj_api->file_path); + } + + /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ + char *token[0x03]; + memset(token, 0x00, sizeof(token)); + int num_of_tokens = validate_cmd_tokenize(bucket_name, token); + printf("num of tokes = %d; \n", num_of_tokens); + + if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) { - fprintf(file, "%s\n", local_file_path); - fprintf(stdout, "%s\n", local_file_path); + for (int j = 0x00; j < num_of_tokens; j++) + { + printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + } - for (int i = 0x01; i < ((argc - command_index) - 1); i++) + char *dst_file_name = NULL; + + storj_api->bucket_name = token[1]; + + if ((token[2] == NULL) || + (strcmp(token[2], ".") == 0x00)) { - fprintf(file, "%s\n", argv[command_index + i]); - fprintf(stdout, "%s\n", argv[command_index + i]); + printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); + storj_upload_files(storj_api); } - } + else + { + printf("[%s][%d] storj://; storj:/// storj:///. !!!!!\n", __FUNCTION__, __LINE__); + goto end_program; + } + } else { - printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); + printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); goto end_program; } - fclose(file); - - storj_api->file_path = temp_buff; - printf("src file path = %s\n", storj_api->file_path); } + } + #if 0 + else if (cmd_type == DOWNLOAD_CMD) + { + storj_api->file_name = local_file_path; + char *dst_file_name = get_filename_separator(local_file_path); /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ char *token[0x03]; @@ -2124,36 +2246,52 @@ int main(int argc, char **argv) int num_of_tokens = validate_cmd_tokenize(bucket_name, token); printf("num of tokes = %d; \n", num_of_tokens); - if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) + storj_api->bucket_name = token[1]; + + if (argc == 0x04) /* handle non recursive operations */ { - for (int j = 0x00; j < num_of_tokens; j++) + if (strcmp(token[2], "*") == 0x00) { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); + if (check_file_path(local_file_path) != CLI_VALID_DIR) + { + printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", + __FUNCTION__, __LINE__, local_file_path); + goto end_program; + } } - - char *dst_file_name = NULL; - - storj_api->bucket_name = token[1]; - - if ((token[2] == NULL) || - (strcmp(token[2], ".") == 0x00)) + else if (token[2] != NULL) { - printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); - storj_upload_files(storj_api); + if ((dst_file_name != NULL) || check_file_path(local_file_path) == CLI_VALID_DIR) + { + /* call single file download command */ + printf("****** single file download command function \n"); + } + else + { + printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", + __FUNCTION__, __LINE__, local_file_path); + goto end_program; + } } else { - printf("[%s][%d] storj://; storj:/// storj:///. !!!!!\n", __FUNCTION__, __LINE__); + printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", + __FUNCTION__, __LINE__, local_file_path); goto end_program; } - } + } + else if(argc == 0x05) /* handle recursive operations */ + { + + } else { + /* more args then needed wrong command */ printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); goto end_program; } - } + #endif } else if (strcmp(command, "upload-file") == 0) { @@ -2193,7 +2331,7 @@ int main(int argc, char **argv) storj_api->bucket_name = argv[command_index + 1]; storj_api->file_path = argv[command_index + 2]; - if (!storj_api->bucket_name || !storj_api->file_name) + if (!storj_api->bucket_name || !storj_api->file_path) { printf("Missing arguments: \n"); status = 1; diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 3275a70..cb463a8 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1060,6 +1060,10 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->file_id = token[0]; strcpy(temp_path, storj_api->file_path); + if (storj_api->file_path[(strlen(storj_api->file_path)-1)] != '/') + { + strcat(temp_path, "/"); + } strcat(temp_path, token[1]); fprintf(stdout,"*****[%d:%d] downloading file to: %s *****\n", storj_api->xfer_count, storj_api->total_files, temp_path); storj_api->xfer_count++; From 4f5489890fffdf737d2b7085783a3a4bf015ad9e Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 30 Jan 2018 08:20:23 -0500 Subject: [PATCH 50/68] ** cp style upload-files with and without recursive [-rR] support ** --- src/cli.c | 68 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/src/cli.c b/src/cli.c index a1ed417..28e4a61 100644 --- a/src/cli.c +++ b/src/cli.c @@ -2131,50 +2131,50 @@ int main(int argc, char **argv) /* directory is being used, store it in file_path */ storj_api->file_path = local_file_path; - char pwd_path[256]= {}; - memset(pwd_path, 0x00, sizeof(pwd_path)); - char *upload_list_file = pwd_path; - - /* create "/tmp/STORJ_upload_list_file.txt" upload files list based on the file path */ - if ((upload_list_file = getenv("TMPDIR")) != NULL) + /* Handle wild character options for files selection */ + if(check_file_path(local_file_path) != CLI_VALID_DIR) { - printf("uploadlistfile = %s\n", upload_list_file); - printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); - if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') + char pwd_path[256] = { }; + memset(pwd_path, 0x00, sizeof(pwd_path)); + char *upload_list_file = pwd_path; + + /* create "/tmp/STORJ_upload_list_file.txt" upload files list based on the file path */ + if ((upload_list_file = getenv("TMPDIR")) != NULL) { - strcat(upload_list_file, "STORJ_output_list.txt"); - } + printf("uploadlistfile = %s\n", upload_list_file); + printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); + if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') + { + strcat(upload_list_file, "STORJ_output_list.txt"); + } + else + { + strcat(upload_list_file, "/STORJ_output_list.txt"); + } + + /* check the directory and create the path to upload list file */ + memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); + memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); + storj_api->dst_file = storj_api->src_list; + printf("**dst_file = %s\n", storj_api->dst_file); + } else { - strcat(upload_list_file, "/STORJ_output_list.txt"); + printf("[%s][%d] Upload list file generation error!!! \n", + __FUNCTION__, __LINE__); + goto end_program; } - /* check the directory and create the path to upload list file */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); - storj_api->dst_file = storj_api->src_list; - printf("**dst_file = %s\n", storj_api->dst_file); - } - else - { - printf("[%s][%d] Upload list file generation error!!! \n", - __FUNCTION__, __LINE__); - goto end_program; - } - - /* Handle wild character options for files selection */ - if(check_file_path(local_file_path) != CLI_VALID_DIR) - { /* if local file path is a file, then just get the directory from that */ char *ret = NULL; ret = strrchr(local_file_path, '/'); memset(temp_buff, 0x00, sizeof(temp_buff)); - memcpy(temp_buff, local_file_path, (ret-local_file_path)); + memcpy(temp_buff, local_file_path, (ret - local_file_path)); printf("ret = %s diff = %d temp = %s\n", ret, ret -local_file_path, temp_buff); - FILE *file= NULL; + FILE *file = NULL; /* create the file and add the list of files to be uploaded */ if ((file = fopen(storj_api->src_list, "w")) != NULL) { @@ -2186,7 +2186,7 @@ int main(int argc, char **argv) fprintf(file, "%s\n", argv[command_index + i]); fprintf(stdout, "%s\n", argv[command_index + i]); } - } + } else { printf("[%s][%d] Invalid upload src path entered\n", __FUNCTION__, __LINE__); @@ -2197,6 +2197,12 @@ int main(int argc, char **argv) storj_api->file_path = temp_buff; printf("src file path = %s\n", storj_api->file_path); } + else + { + /* its a valid directory so let the list of files to be uploaded be handled in + verify upload file () */ + storj_api->dst_file = NULL; + } /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ char *token[0x03]; From fd35bd7fa7da58521fc2c2532e58e69682ebf8b6 Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 30 Jan 2018 10:42:46 -0500 Subject: [PATCH 51/68] *** cp style supports download and upload of files *** --- src/cli.c | 11 ++++++++++- src/storjapi_callback.c | 22 +++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/cli.c b/src/cli.c index 28e4a61..47db6e2 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1925,6 +1925,15 @@ int main(int argc, char **argv) } } + if ((strcmp(src_path, argv[command_index]) == 0x00) || + (strcmp(dst_path, argv[command_index]) == 0x00) || + (strcmp(dst_path, src_path) == 0x00)) + { + printf("[%s][%d] Invalid command option '%s'\n", + __FUNCTION__, __LINE__, argv[1]); + goto end_program; + } + /* check for upload or download command */ char sub_str[] = "storj://"; ret = strpos(dst_path, sub_str); @@ -2002,7 +2011,7 @@ int main(int argc, char **argv) if ((argc == 0x04) || (argc == 0x05)) /* handle non recursive operations */ { - if (strcmp(token[2], "*") == 0x00) + if ((token[2] == NULL) || (strcmp(token[2], "*") == 0x00)) { if (check_file_path(local_file_path) == CLI_VALID_DIR) { diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index cb463a8..3c635e4 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1007,8 +1007,16 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->excp_cmd_resp = "download-file-resp"; printf("dst_file= %s\n", storj_api->dst_file); - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api->dst_file, storj_api); + if (storj_api->file_id != NULL) + { + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api->dst_file, storj_api); + } + else + { + printf("[%s][%d] File not found!!!\n", __FUNCTION__, __LINE__); + exit(0); + } } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "download-files-req") == 0x00)) @@ -1067,7 +1075,15 @@ void queue_next_cmd_req(storj_api_t *storj_api) strcat(temp_path, token[1]); fprintf(stdout,"*****[%d:%d] downloading file to: %s *****\n", storj_api->xfer_count, storj_api->total_files, temp_path); storj_api->xfer_count++; - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); + if (storj_api->file_id != NULL) + { + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); + } + else + { + printf("[%s][%d] File not found!!!\n", __FUNCTION__, __LINE__); + exit(0); + } } else { From 0cb92cc4ca6690ee8a2033fa1784c68cd763a88a Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 30 Jan 2018 13:38:53 -0500 Subject: [PATCH 52/68] Added the upload file limit to 256 as a configuration parameter Added the 'rm' for remove bucket or remove file in a bucket. ./storj rm --> removes the bucket ./storj rm --> removes the file from the bucket --- src/cli.c | 7 +++++++ src/storjapi_callback.c | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/cli.c b/src/cli.c index 47db6e2..e4a7f35 100644 --- a/src/cli.c +++ b/src/cli.c @@ -2395,6 +2395,8 @@ int main(int argc, char **argv) storj_api->bucket_name = argv[command_index + 1]; storj_api->file_name = argv[command_index + 2]; + if (storj_api->file_name != NULL) + { if (!storj_api->bucket_name|| !storj_api->file_name) { printf("Missing arguments, expected: \n"); @@ -2403,6 +2405,11 @@ int main(int argc, char **argv) } storj_remove_file(storj_api); + } + else + { + storj_remove_bucket(storj_api); + } } else if ((strcmp(command, "list-buckets") == 0) || (strcmp(command, "ls") == 0x00)) { diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 3c635e4..5a5e1fb 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1,5 +1,7 @@ #include "storjapi_callback.h" +#define MAX_UPLOAD_FILES 256 + static inline void noop() {}; static void get_input(char *line) @@ -91,6 +93,7 @@ static void printdir(char *dir, int depth, FILE *src_fd, void *handle) full_path = realpath(entry->d_name, NULL); /* write to src file */ fprintf(src_fd, "%s%s\n", "", full_path); + fprintf(stdout, "%s%s\n", "", full_path); } } chdir(".."); @@ -453,6 +456,7 @@ static void verify_upload_files(void *handle) printf("inside verify src list = %s\n",storj_api->src_list); int file_attr = file_exists(handle); + printf("file _attr = %d\n", file_attr); } /* create a upload list file src_list.txt */ @@ -468,7 +472,7 @@ static void verify_upload_files(void *handle) else { /* count total src_list files */ - char line[256][256]; + char line[MAX_UPLOAD_FILES][256]; char *temp; int i = 0x00; @@ -477,7 +481,17 @@ static void verify_upload_files(void *handle) /* read a line from a file */ while (fgets(line[i], sizeof(line), storj_api->src_fd) != NULL) { - i++; + if (i <= MAX_UPLOAD_FILES) + { + i++; + } + else + { + i = (i - 1); + printf("[%s][%d] Upload files limit set to %d \n", + __FUNCTION__, __LINE__, (MAX_UPLOAD_FILES)); + break; + } } total_src_files = i; From 5864e6576df730abd204502aa820a5cf630bb87a Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 30 Jan 2018 13:44:37 -0500 Subject: [PATCH 53/68] added 'mkbkt' command to add bucket --- src/cli.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/cli.c b/src/cli.c index e4a7f35..92c5bdd 100644 --- a/src/cli.c +++ b/src/cli.c @@ -2367,7 +2367,9 @@ int main(int argc, char **argv) goto end_program; } storj_list_files(storj_api); - } else if (strcmp(command, "add-bucket") == 0) { + } + else if ((strcmp(command, "add-bucket") == 0) || (strcmp(command, "mkbkt") == 0x00)) + { char *bucket_name = argv[command_index + 1]; if (!bucket_name) { @@ -2397,14 +2399,14 @@ int main(int argc, char **argv) if (storj_api->file_name != NULL) { - if (!storj_api->bucket_name|| !storj_api->file_name) - { - printf("Missing arguments, expected: \n"); - status = 1; - goto end_program; - } + if (!storj_api->bucket_name|| !storj_api->file_name) + { + printf("Missing arguments, expected: \n"); + status = 1; + goto end_program; + } - storj_remove_file(storj_api); + storj_remove_file(storj_api); } else { From 1102cf44390792b5bc2bc93d197c3c10ae1a7527 Mon Sep 17 00:00:00 2001 From: Kishore Date: Tue, 30 Jan 2018 14:14:35 -0500 Subject: [PATCH 54/68] *** Removed the development printf debug info ***. Use the previous to this version if you want all the development printf debug info. --- src/cli.c | 125 ++-------------------------------------- src/storjapi_callback.c | 16 ++--- 2 files changed, 13 insertions(+), 128 deletions(-) diff --git a/src/cli.c b/src/cli.c index 92c5bdd..6a7560e 100644 --- a/src/cli.c +++ b/src/cli.c @@ -16,6 +16,8 @@ #include "storj.h" +//#define debug_enable + #define STORJ_THREADPOOL_SIZE "64" #define CLI_NO_SUCH_FILE_OR_DIR 0x00 @@ -1827,7 +1829,7 @@ int main(int argc, char **argv) storj_api->env = env; - + #ifdef debug_enable printf("command = %s; command_index = %d\n", command, command_index); printf("local_file_path (req arg_ = %s\n", local_file_path); for (int i = 0x00; i < argc; i++) @@ -1839,6 +1841,7 @@ int main(int argc, char **argv) { printf("argc = %d; argv[command_index+%d] = %s\n", argc, i, argv[command_index + i]); } + #endif if (strcmp(command, "download-file") == 0) { @@ -1856,30 +1859,6 @@ int main(int argc, char **argv) } storj_download_file(storj_api); - - #if 0 - char *bucket_name = argv[command_index + 1]; - char *file_name = argv[command_index + 2]; - char *path = argv[command_index + 3]; - - if (!bucket_name || !file_name || !path) - { - printf("Missing arguments: \n"); - status = 1; - goto end_program; - } - else - { - cli_state->curr_cmd_req = command; - cli_state->bucket_name = bucket_name; - cli_state->file_name = file_name; - cli_state->file_path = path; - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(env, cli_state, get_bucket_id_callback); - } - } - #endif } else if (strcmp(command, "cp") == 0) { @@ -1902,7 +1881,6 @@ int main(int argc, char **argv) /* Handle the dst argument (storj:/// */ dst_path = argv[argc - 0x01]; - printf("bucket-name = %s\n", bucket_name); cmd_type = NON_RECURSIVE_CMD; } @@ -1945,12 +1923,8 @@ int main(int argc, char **argv) if (check_file_path(src_path) != CLI_VALID_DIR) { local_file_path = src_path; - printf("[%s][%d] local_file_path = %s\n", - __FUNCTION__, __LINE__, local_file_path); bucket_name = dst_path; - printf("[%s][%d] bucket-name = %s\n", - __FUNCTION__, __LINE__, bucket_name); } else { @@ -1962,12 +1936,8 @@ int main(int argc, char **argv) else if (cmd_type == RECURSIVE_CMD) { local_file_path = src_path; - printf("[%s][%d] local_file_path = %s\n", - __FUNCTION__, __LINE__, local_file_path); bucket_name = dst_path; - printf("[%s][%d] bucket-name = %s\n", - __FUNCTION__, __LINE__, bucket_name); } else { @@ -1975,7 +1945,6 @@ int main(int argc, char **argv) goto end_program; } - printf("[main.c][1921] upload command\n"); cmd_type = UPLOAD_CMD; } else if (ret == -1) /* Handle download command*/ @@ -1990,15 +1959,12 @@ int main(int argc, char **argv) bucket_name = src_path; char *dst_file_name = NULL; - dst_file_name = get_filename_separator(local_file_path); - printf("[%s][%d] local_file_path = %s, dst_file_name = %s and leng = %d\n", - __FUNCTION__, __LINE__, local_file_path, dst_file_name, strlen(dst_file_name)); + dst_file_name = (char *)get_filename_separator(local_file_path); /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ char *token[0x03]; memset(token, 0x00, sizeof(token)); int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("num of tokes = %d; \n", num_of_tokens); /* set the bucket name from which file(s) to be downloaded */ storj_api->bucket_name = token[1]; @@ -2102,11 +2068,6 @@ int main(int argc, char **argv) if ((num_of_tokens == 0x02) || (num_of_tokens == 0x03)) { - for (int j = 0x00; j < num_of_tokens; j++) - { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } - char *dst_file_name = NULL; storj_api->bucket_name = token[1]; @@ -2119,14 +2080,11 @@ int main(int argc, char **argv) memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); strcpy(storj_api->src_list, dst_file_name); storj_api->dst_file = storj_api->src_list; - printf("file uploaded as same %s \n", storj_api->dst_file); } else { storj_api->dst_file = token[2]; - printf("file uploaded as %s\n", storj_api->dst_file); } - printf("******* EXECUTE UPLOAD CMD HERE \n"); storj_upload_file(storj_api); } else @@ -2150,8 +2108,6 @@ int main(int argc, char **argv) /* create "/tmp/STORJ_upload_list_file.txt" upload files list based on the file path */ if ((upload_list_file = getenv("TMPDIR")) != NULL) { - printf("uploadlistfile = %s\n", upload_list_file); - printf("upload_list_file[strlen(upload_list_file)] = %d\n", strlen(upload_list_file)); if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') { strcat(upload_list_file, "STORJ_output_list.txt"); @@ -2165,7 +2121,6 @@ int main(int argc, char **argv) memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); storj_api->dst_file = storj_api->src_list; - printf("**dst_file = %s\n", storj_api->dst_file); } else { @@ -2181,19 +2136,15 @@ int main(int argc, char **argv) memset(temp_buff, 0x00, sizeof(temp_buff)); memcpy(temp_buff, local_file_path, (ret - local_file_path)); - printf("ret = %s diff = %d temp = %s\n", ret, ret -local_file_path, temp_buff); - FILE *file = NULL; /* create the file and add the list of files to be uploaded */ if ((file = fopen(storj_api->src_list, "w")) != NULL) { fprintf(file, "%s\n", local_file_path); - fprintf(stdout, "%s\n", local_file_path); for (int i = 0x01; i < ((argc - command_index) - 1); i++) { fprintf(file, "%s\n", argv[command_index + i]); - fprintf(stdout, "%s\n", argv[command_index + i]); } } else @@ -2204,7 +2155,6 @@ int main(int argc, char **argv) fclose(file); storj_api->file_path = temp_buff; - printf("src file path = %s\n", storj_api->file_path); } else { @@ -2217,15 +2167,9 @@ int main(int argc, char **argv) char *token[0x03]; memset(token, 0x00, sizeof(token)); int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("num of tokes = %d; \n", num_of_tokens); if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) { - for (int j = 0x00; j < num_of_tokens; j++) - { - printf("num of tokes = %d; token[%d] = %s\n", num_of_tokens, j, token[j]); - } - char *dst_file_name = NULL; storj_api->bucket_name = token[1]; @@ -2233,7 +2177,6 @@ int main(int argc, char **argv) if ((token[2] == NULL) || (strcmp(token[2], ".") == 0x00)) { - printf("******* EXECUTE UPLOAD**S** CMD HERE \n"); storj_upload_files(storj_api); } else @@ -2249,64 +2192,6 @@ int main(int argc, char **argv) } } } - #if 0 - else if (cmd_type == DOWNLOAD_CMD) - { - storj_api->file_name = local_file_path; - char *dst_file_name = get_filename_separator(local_file_path); - - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - char *token[0x03]; - memset(token, 0x00, sizeof(token)); - int num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("num of tokes = %d; \n", num_of_tokens); - - storj_api->bucket_name = token[1]; - - if (argc == 0x04) /* handle non recursive operations */ - { - if (strcmp(token[2], "*") == 0x00) - { - if (check_file_path(local_file_path) != CLI_VALID_DIR) - { - printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", - __FUNCTION__, __LINE__, local_file_path); - goto end_program; - } - } - else if (token[2] != NULL) - { - if ((dst_file_name != NULL) || check_file_path(local_file_path) == CLI_VALID_DIR) - { - /* call single file download command */ - printf("****** single file download command function \n"); - } - else - { - printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", - __FUNCTION__, __LINE__, local_file_path); - goto end_program; - } - } - else - { - printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", - __FUNCTION__, __LINE__, local_file_path); - goto end_program; - } - } - else if(argc == 0x05) /* handle recursive operations */ - { - - } - else - { - /* more args then needed wrong command */ - printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); - goto end_program; - } - } - #endif } else if (strcmp(command, "upload-file") == 0) { diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 5a5e1fb..1eea7d7 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -1,5 +1,7 @@ #include "storjapi_callback.h" +//#define debug_enable + #define MAX_UPLOAD_FILES 256 static inline void noop() {}; @@ -93,7 +95,6 @@ static void printdir(char *dir, int depth, FILE *src_fd, void *handle) full_path = realpath(entry->d_name, NULL); /* write to src file */ fprintf(src_fd, "%s%s\n", "", full_path); - fprintf(stdout, "%s%s\n", "", full_path); } } chdir(".."); @@ -433,7 +434,6 @@ static void verify_upload_files(void *handle) memset(pwd_path, 0x00, sizeof(pwd_path)); char *upload_list_file = pwd_path; - printf("am here \n"); /* create upload files list based on the file path */ if ((upload_list_file = getenv("TMPDIR")) != NULL) { @@ -453,14 +453,10 @@ static void verify_upload_files(void *handle) } /* create a upload list file src_list.txt */ - printf("inside verify src list = %s\n",storj_api->src_list); - int file_attr = file_exists(handle); - printf("file _attr = %d\n", file_attr); } /* create a upload list file src_list.txt */ - printf("inside verify src list = %s\n",storj_api->src_list); storj_api->src_fd = fopen(storj_api->src_list, "r"); if (!storj_api->src_fd) @@ -499,7 +495,6 @@ static void verify_upload_files(void *handle) storj_api->total_files = total_src_files; storj_api->xfer_count = 0x01; - printf("[][] total files = %d, xfer_count = %d\n", storj_api->total_files, storj_api->xfer_count); storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; queue_next_cmd_req(storj_api); @@ -869,12 +864,15 @@ void queue_next_cmd_req(storj_api_t *storj_api) { void *handle = storj_api->handle; + #ifdef debug_enable printf("[%s][%d]start !!!! expt resp = %s; rcvd resp = %s \n", __FUNCTION__, __LINE__, storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); printf("[%s][%d]last cmd = %s; cur cmd = %s; next cmd = %s\n", __FUNCTION__, __LINE__, storj_api->last_cmd_req, storj_api->curr_cmd_req, storj_api->next_cmd_req); + #endif + if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) { if ((storj_api->next_cmd_req != NULL) && @@ -1020,7 +1018,6 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "download-file-resp"; - printf("dst_file= %s\n", storj_api->dst_file); if (storj_api->file_id != NULL) { download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, @@ -1108,7 +1105,10 @@ void queue_next_cmd_req(storj_api_t *storj_api) } else { + #ifdef debug_enable printf("[%s][%d] **** ALL CLEAN & DONE *****\n", __FUNCTION__, __LINE__); + #endif + exit(0); } } From 51361962dcb59e17b5b6c216817cd5aa01ddb966 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 31 Jan 2018 10:18:31 -0500 Subject: [PATCH 55/68] Unnecessary code commented --- src/cli.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/cli.c b/src/cli.c index 6a7560e..8d44226 100644 --- a/src/cli.c +++ b/src/cli.c @@ -56,9 +56,9 @@ extern int errno; #endif static void printdir(char *dir, int depth, FILE *fd); -static void queue_next_cli_cmd(cli_state_t *cli_state); -static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state); -static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_state); +//static void queue_next_cli_cmd(cli_state_t *cli_state); +//static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state); +//static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_state); static const char *get_filename_separator(const char *file_path); static inline void noop() {}; @@ -103,22 +103,6 @@ static inline void noop() {}; #define CLI_VERSION "libstorj-2.0.1-beta" - -static void print_error(char *this, char *filename1, char *filename2) -{ - fprintf(stderr, "%s cannot move %s to %s\n%s\n", - this, filename1, filename2, strerror(errno)); - - exit(EXIT_FAILURE); -} - -static void print_upload_usage(char *this) -{ - fprintf(stderr,"SYNTAX ERROR:\nUsage %s [old_filename] [new_filename]",this); - - exit(EXIT_FAILURE); -} - static int check_file_path(char *file_path) { struct stat sb; @@ -590,6 +574,7 @@ static void close_signal(uv_handle_t *handle) ((void)0); } +#if 0 static void file_progress(double progress, uint64_t downloaded_bytes, uint64_t total_bytes, @@ -667,7 +652,7 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, // Upload opts env variables: char *prepare_frame_limit = getenv("STORJ_PREPARE_FRAME_LIMIT"); - char *push_frame_limit = getenv("STORJ_PUSH_FRAME_LIMIT"); + char *push_frame_limit = getenv("STORJ_PU:SH_FRAME_LIMIT"); char *push_shard_limit = getenv("STORJ_PUSH_SHARD_LIMIT"); char *rs = getenv("STORJ_REED_SOLOMON"); @@ -866,6 +851,7 @@ static void list_mirrors_callback(uv_work_t *work_req, int status) free(req); free(work_req); } +#endif static int import_keys(user_options_t *options) { @@ -1091,6 +1077,7 @@ static void register_callback(uv_work_t *work_req, int status) free(work_req); } +#if 0 static void list_files_callback(uv_work_t *work_req, int status) { int ret_status = 0; @@ -1239,6 +1226,7 @@ static void delete_bucket_callback(uv_work_t *work_req, int status) free(req); free(work_req); } +#endif static void get_buckets_callback(uv_work_t *work_req, int status) { @@ -1264,6 +1252,7 @@ static void get_buckets_callback(uv_work_t *work_req, int status) free(work_req); } +#if 0 static void get_bucket_id_callback(uv_work_t *work_req, int status) { int ret_status = 0x00; @@ -1343,6 +1332,7 @@ static void get_bucket_id_callback(uv_work_t *work_req, int status) storj_free_get_buckets_request(req); free(work_req); } +#endif static void create_bucket_callback(uv_work_t *work_req, int status) { @@ -2392,6 +2382,7 @@ int main(int argc, char **argv) return status; } +#if 0 /* cli cmd queue processing function */ static void queue_next_cli_cmd(cli_state_t *cli_state) { @@ -2701,3 +2692,5 @@ static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_sta } return ret_status; } +#endif + From b58f83559a9f95fa1f76b6c603119a7a3177ccd1 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 31 Jan 2018 11:40:35 -0500 Subject: [PATCH 56/68] Removed the commented code (code cleaned) Fixed the uploaded file's naming issue. --- src/cli.c | 837 +----------------------------------------------------- 1 file changed, 11 insertions(+), 826 deletions(-) diff --git a/src/cli.c b/src/cli.c index 8d44226..f3d15ed 100644 --- a/src/cli.c +++ b/src/cli.c @@ -574,285 +574,6 @@ static void close_signal(uv_handle_t *handle) ((void)0); } -#if 0 -static void file_progress(double progress, - uint64_t downloaded_bytes, - uint64_t total_bytes, - void *handle) -{ - int bar_width = 70; - - if (progress == 0 && downloaded_bytes == 0) { - printf("Preparing File..."); - fflush(stdout); - return; - } - - printf("\r["); - int pos = bar_width * progress; - for (int i = 0; i < bar_width; ++i) { - if (i < pos) { - printf("="); - } else if (i == pos) { - printf(">"); - } else { - printf(" "); - } - } - printf("] %.*f%%", 2, progress * 100); - - fflush(stdout); -} - -static void upload_file_complete(int status, storj_file_meta_t *file, void *handle) -{ - cli_state_t *cli_state = handle; - printf("\n"); - if (status != 0) - { - printf("Upload failure: %s\n", storj_strerror(status)); - //exit(status); - } - - printf("Upload Success! File ID: %s\n", file->id); - - storj_free_uploaded_file_info(file); - - if((cli_state->total_files == 0x00) && - (cli_state->curr_up_file == 0x00)) - { - exit(0); - } - queue_next_cli_cmd(handle); -} - -static void upload_signal_handler(uv_signal_t *req, int signum) -{ - storj_upload_state_t *state = req->data; - storj_bridge_store_file_cancel(state); - if (uv_signal_stop(req)) { - printf("Unable to stop signal\n"); - } - uv_close((uv_handle_t *)req, close_signal); -} - -static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) -{ - FILE *fd = fopen(file_path, "r"); - - if (!fd) { - printf("Invalid file path: %s\n", file_path); - } - - const char *file_name = get_filename_separator(file_path); - - if (!file_name) { - file_name = file_path; - } - - // Upload opts env variables: - char *prepare_frame_limit = getenv("STORJ_PREPARE_FRAME_LIMIT"); - char *push_frame_limit = getenv("STORJ_PU:SH_FRAME_LIMIT"); - char *push_shard_limit = getenv("STORJ_PUSH_SHARD_LIMIT"); - char *rs = getenv("STORJ_REED_SOLOMON"); - - storj_upload_opts_t upload_opts = { - .prepare_frame_limit = (prepare_frame_limit) ? atoi(prepare_frame_limit) : 1, - .push_frame_limit = (push_frame_limit) ? atoi(push_frame_limit) : 64, - .push_shard_limit = (push_shard_limit) ? atoi(push_shard_limit) : 64, - .rs = (!rs) ? true : (strcmp(rs, "false") == 0) ? false : true, - .bucket_id = bucket_id, - .file_name = file_name, - .fd = fd - }; - - uv_signal_t *sig = malloc(sizeof(uv_signal_t)); - if (!sig) { - return 1; - } - uv_signal_init(env->loop, sig); - uv_signal_start(sig, upload_signal_handler, SIGINT); - - - - storj_progress_cb progress_cb = (storj_progress_cb)noop; - if (env->log_options->level == 0) { - progress_cb = file_progress; - } - - storj_upload_state_t *state = storj_bridge_store_file(env, - &upload_opts, - handle, - progress_cb, - upload_file_complete); - - if (!state) { - return 1; - } - - sig->data = state; - - return state->error_status; -} - -static void download_file_complete(int status, FILE *fd, void *handle) -{ - cli_state_t *cli_state = handle; - printf("\n"); - fclose(fd); - if (status) - { - // TODO send to stderr - switch(status) { - case STORJ_FILE_DECRYPTION_ERROR: - printf("Unable to properly decrypt file, please check " \ - "that the correct encryption key was " \ - "imported correctly.\n\n"); - break; - default: - printf("Download failure: %s\n", storj_strerror(status)); - } - - //exit(status); - } - else - { - printf("Download Success!\n"); - } - - if(cli_state->total_files == 0x00) - { - exit(0); - } - queue_next_cli_cmd(handle); -} - -static void download_signal_handler(uv_signal_t *req, int signum) -{ - storj_download_state_t *state = req->data; - storj_bridge_resolve_file_cancel(state); - if (uv_signal_stop(req)) { - printf("Unable to stop signal\n"); - } - uv_close((uv_handle_t *)req, close_signal); -} - -static int download_file(storj_env_t *env, char *bucket_id, - char *file_id, char *path, void *handle) -{ - FILE *fd = NULL; - - if (path) { - char user_input[BUFSIZ]; - memset(user_input, '\0', BUFSIZ); - - if(access(path, F_OK) != -1 ) { - printf("Warning: File already exists at path [%s].\n", path); - while (strcmp(user_input, "y") != 0 && strcmp(user_input, "n") != 0) - { - memset(user_input, '\0', BUFSIZ); - printf("Would you like to overwrite [%s]: [y/n] ", path); - get_input(user_input); - } - - if (strcmp(user_input, "n") == 0) { - printf("\nCanceled overwriting of [%s].\n", path); - return 1; - } - - unlink(path); - } - - fd = fopen(path, "w+"); - } else { - fd = stdout; - } - - if (fd == NULL) { - // TODO send to stderr - printf("Unable to open %s: %s\n", path, strerror(errno)); - return 1; - } - - uv_signal_t *sig = malloc(sizeof(uv_signal_t)); - uv_signal_init(env->loop, sig); - uv_signal_start(sig, download_signal_handler, SIGINT); - - storj_progress_cb progress_cb = (storj_progress_cb)noop; - if (path && env->log_options->level == 0) { - progress_cb = file_progress; - } - - storj_download_state_t *state = storj_bridge_resolve_file(env, bucket_id, - file_id, fd, handle, - progress_cb, - download_file_complete); - if (!state) { - return 1; - } - sig->data = state; - - return state->error_status; -} - -static void list_mirrors_callback(uv_work_t *work_req, int status) -{ - assert(status == 0); - json_request_t *req = work_req->data; - - if (req->status_code != 200) { - printf("Request failed with status code: %i\n", - req->status_code); - } - - if (req->response == NULL) { - free(req); - free(work_req); - printf("Failed to list mirrors.\n"); - exit(1); - } - - int num_mirrors = json_object_array_length(req->response); - - struct json_object *shard; - struct json_object *established; - struct json_object *available; - struct json_object *item; - struct json_object *hash; - struct json_object *contract; - struct json_object *address; - struct json_object *port; - struct json_object *node_id; - - for (int i = 0; i < num_mirrors; i++) { - shard = json_object_array_get_idx(req->response, i); - json_object_object_get_ex(shard, "established", - &established); - int num_established = - json_object_array_length(established); - for (int j = 0; j < num_established; j++) { - item = json_object_array_get_idx(established, j); - if (j == 0) { - json_object_object_get_ex(item, "shardHash", - &hash); - printf("Shard %i: %s\n", i, json_object_get_string(hash)); - } - json_object_object_get_ex(item, "contract", &contract); - json_object_object_get_ex(contract, "farmer_id", &node_id); - - const char *node_id_str = json_object_get_string(node_id); - printf("\tnodeID: %s\n", node_id_str); - } - printf("\n\n"); - } - - json_object_put(req->response); - free(req->path); - free(req); - free(work_req); -} -#endif - static int import_keys(user_options_t *options) { int status = 0; @@ -1077,157 +798,6 @@ static void register_callback(uv_work_t *work_req, int status) free(work_req); } -#if 0 -static void list_files_callback(uv_work_t *work_req, int status) -{ - int ret_status = 0; - assert(status == 0); - list_files_request_t *req = work_req->data; - cli_state_t *cli_state = req->handle; - - if (req->status_code == 404) { - printf("Bucket id [%s] does not exist\n", req->bucket_id); - goto cleanup; - } else if (req->status_code == 400) { - printf("Bucket id [%s] is invalid\n", req->bucket_id); - goto cleanup; - } else if (req->status_code == 401) { - printf("Invalid user credentials.\n"); - goto cleanup; - } else if (req->status_code != 200) { - printf("Request failed with status code: %i\n", req->status_code); - } - - if (req->total_files == 0) { - printf("No files for bucket.\n"); - } - - FILE *dwnld_list_fd = stdout; - cli_state->file_id = NULL; - - if ((cli_state->file_name !=NULL) && (strcmp(cli_state->file_name,"*") == 0x00)) - { - if ((dwnld_list_fd = fopen("dwnld_list.txt", "w")) == NULL) - { - printf("[%s][%d] Unable to create download list file \n", - __FUNCTION__, __LINE__); - goto cleanup; - } - /* total number of files available in that bucket */ - cli_state->total_files = req->total_files; - } - - for (int i = 0; i < req->total_files; i++) - { - storj_file_meta_t *file = &req->files[i]; - - if (strcmp(cli_state->curr_cmd_req,"list-files") == 0x00) - { - /* print to screen */ - fprintf(stdout, "ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", - file->id, - file->size, - file->decrypted ? "true" : "false", - file->mimetype, - file->created, - file->filename); - } - - /* print to file */ - if (dwnld_list_fd != stdout) - { - fprintf(dwnld_list_fd, "%s:%s\n", - file->id, - file->filename); - } - - /* get the file id of the given file name */ - if(cli_state->file_name != NULL) - { - if (strcmp(cli_state->file_name, file->filename) == 0x00) - { - if((dwnld_list_fd != stdout) && - (check_file_path("dwnld_list.txt") == CLI_VALID_REGULAR_FILE)) - { - if (remove("dwnld_list.txt") == 0x00) - { - printf("%s file deleted \n", "dwnld_list.txt"); - } - } - cli_state->file_id = (char *)file->id; - cli_state->next_cmd_req = "download-file-1"; - cli_state->total_files = 0x00; - } - } - } - - if (dwnld_list_fd != stdout) - { - fclose(dwnld_list_fd); - } - - if (strcmp(cli_state->curr_cmd_req, "download-file" ) == 0x00) - { - cli_state->curr_up_file = 0x01; - cli_state->next_cmd_req = "download-file-1"; - queue_next_cli_cmd(cli_state); - } - else - { - if(check_file_path("dwnld_list.txt") == CLI_VALID_REGULAR_FILE) - { - if (remove("dwnld_list.txt") == 0x00) - { - printf("file deleted \n\n"); - } - } - } - -cleanup: - - storj_free_list_files_request(req); - free(work_req); -} - -static void delete_file_callback(uv_work_t *work_req, int status) -{ - assert(status == 0); - json_request_t *req = work_req->data; - - if (req->status_code == 200 || req->status_code == 204) { - printf("File was successfully removed from bucket.\n"); - } else if (req->status_code == 401) { - printf("Invalid user credentials.\n"); - } else { - printf("Failed to remove file from bucket. (%i)\n", req->status_code); - } - - json_object_put(req->response); - free(req->path); - free(req); - free(work_req); -} - -static void delete_bucket_callback(uv_work_t *work_req, int status) -{ - assert(status == 0); - json_request_t *req = work_req->data; - - if (req->status_code == 200 || req->status_code == 204) { - printf("Bucket was successfully removed.\n"); - } else if (req->status_code == 401) { - printf("Invalid user credentials.\n"); - } else { - printf("Failed to destroy bucket. (%i)\n", req->status_code); - } - - json_object_put(req->response); - free(req->path); - free(req); - free(work_req); -} -#endif - static void get_buckets_callback(uv_work_t *work_req, int status) { assert(status == 0); @@ -1252,88 +822,6 @@ static void get_buckets_callback(uv_work_t *work_req, int status) free(work_req); } -#if 0 -static void get_bucket_id_callback(uv_work_t *work_req, int status) -{ - int ret_status = 0x00; - assert(status == 0); - get_buckets_request_t *req = work_req->data; - cli_state_t *cli_state = req->handle; - - if (req->status_code == 401) { - printf("Invalid user credentials.\n"); - } else if (req->status_code != 200 && req->status_code != 304) { - printf("Request failed with status code: %i\n", req->status_code); - } else if (req->total_buckets == 0) { - printf("No buckets.\n"); - } - - for (int i = 0; i < req->total_buckets; i++) - { - storj_bucket_meta_t *bucket = &req->buckets[i]; - cli_state->next_cmd_req = NULL; - - if (cli_state->bucket_name != NULL) - { - if(strcmp(cli_state->bucket_name, bucket->name) == 0x00) - { - printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", - bucket->id, bucket->decrypted ? "true" : "false", - bucket->created, bucket->name); - cli_state->bucket_id = (char *)bucket->id; - - if(strcmp(cli_state->curr_cmd_req, "list-files") == 0x00) - { - cli_state->next_cmd_req = "list-files-1"; - ret_status = 1; - } - else if(strcmp(cli_state->curr_cmd_req, "download-file") == 0x00) - { - cli_state->next_cmd_req = "list-files-1"; - ret_status = 1; - } - else if(strcmp(cli_state->curr_cmd_req, "upload-file") == 0x00) - { - cli_state->next_cmd_req = "upload-file-1"; - ret_status = 1; - } - else if(strcmp(cli_state->curr_cmd_req, "get-bucket-id") == 0x00) - { - ret_status = 0; - }else - { - printf("[%s][%d]Invalid curr cmd req = %s\n", - __FUNCTION__, __LINE__, cli_state->curr_cmd_req); - ret_status = 0; - } - break; - } - else - { - if (i >= (req->total_buckets -1)) - { - printf("Invalid bucket name. \n"); - } - } - } - else - { - printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", - bucket->id, bucket->decrypted ? "true" : "false", - bucket->created, bucket->name); - } - } - - if(0x01 == ret_status) - { - queue_next_cli_cmd(cli_state); - } - - storj_free_get_buckets_request(req); - free(work_req); -} -#endif - static void create_bucket_callback(uv_work_t *work_req, int status) { assert(status == 0); @@ -1862,6 +1350,9 @@ int main(int argc, char **argv) char *dst_path = NULL; /* holds the storj:// path */ char *bucket_name = NULL; int cmd_type = 0x00; /* 0-> upload and 1 -> download */ + char modified_src_path[256] = {}; /* use this buffer to store the loca-file-path, if modified */ + memset(modified_src_path, 0x00, sizeof(modified_src_path)); + char *upload_file_path = modified_src_path; /* cp command wrt to upload-file */ if(local_file_path == NULL)/* without -r[R] */ @@ -2124,7 +1615,7 @@ int main(int argc, char **argv) char *ret = NULL; ret = strrchr(local_file_path, '/'); memset(temp_buff, 0x00, sizeof(temp_buff)); - memcpy(temp_buff, local_file_path, (ret - local_file_path)); + memcpy(temp_buff, local_file_path, ((ret - local_file_path)+1)); FILE *file = NULL; /* create the file and add the list of files to be uploaded */ @@ -2151,6 +1642,13 @@ int main(int argc, char **argv) /* its a valid directory so let the list of files to be uploaded be handled in verify upload file () */ storj_api->dst_file = NULL; + + memcpy(upload_file_path, storj_api->file_path, strlen(storj_api->file_path)); + if (upload_file_path[(strlen(upload_file_path) - 1)] != '/') + { + strcat(upload_file_path, "/"); + storj_api->file_path = upload_file_path; + } } /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ @@ -2381,316 +1879,3 @@ int main(int argc, char **argv) return status; } - -#if 0 -/* cli cmd queue processing function */ -static void queue_next_cli_cmd(cli_state_t *cli_state) -{ - void *handle = cli_state->handle; - - if (((strcmp("list-files" , cli_state->curr_cmd_req) == 0x00)|| - ((strcmp("download-file" , cli_state->curr_cmd_req) == 0x00))) && - ((strcmp("list-files-1", cli_state->next_cmd_req) == 0x00)|| - (strcmp("download-file-1", cli_state->next_cmd_req)==0x00))) - { - if(strcmp("list-files-1" , cli_state->next_cmd_req) == 0x00) - { - storj_bridge_list_files(cli_state->env, cli_state->bucket_id, cli_state, list_files_callback); - } - - if(strcmp("download-file-1" , cli_state->next_cmd_req) == 0x00) - { - //FILE *file = fopen("/home/kishore/libstorj/src/dwnld_list.txt", "r"); - FILE *file = fopen("dwnld_list.txt", "r"); - if (file != NULL) - { - char line[256][256]; - char *temp; - char temp_path[1024]; - int i = 0x00; - char *token[10]; - int tk_idx= 0x00; - memset(token, 0x00, sizeof(token)); - memset(temp_path, 0x00, sizeof(temp_path)); - memset(line, 0x00, sizeof(line)); - while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ - { - temp = strrchr(line[i], '\n'); - if(temp) *temp = '\0'; - temp = line[i]; - i++; - if (i >= cli_state->curr_up_file) - { - break; - } - } - - /* start tokenizing */ - token[0] = strtok(temp, ":"); - while (token[tk_idx] != NULL) - { - tk_idx++; - token[tk_idx] = strtok(NULL, ":"); - } - - if(cli_state->curr_up_file <= cli_state->total_files) - { - cli_state->file_id = token[0]; - strcpy(temp_path, cli_state->file_path); - strcat(temp_path, token[1]); - fprintf(stdout,"*****[%d:%d] downloading file: %s *****\n", - cli_state->curr_up_file, cli_state->total_files, temp_path); - cli_state->curr_up_file++; - download_file(cli_state->env, cli_state->bucket_id, cli_state->file_id, temp_path, cli_state); - } - else - { - fprintf(stdout,"***** done downloading files *****\n"); - fclose(file); - exit(0); - } - } - else - { - download_file(cli_state->env, cli_state->bucket_id, cli_state->file_id, cli_state->file_path,cli_state); - } - - } - } - else if ((strcmp("upload-file" , cli_state->curr_cmd_req) == 0x00) && - (strcmp("upload-file-1", cli_state->next_cmd_req) == 0x00)) - { - FILE *file = fopen(cli_state->file_name, "r"); - if (file != NULL) - { - char line[256][256]; - char *temp; - int i = 0x00; - memset(line, 0x00, sizeof(line)); - while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ - { - temp = strrchr(line[i], '\n'); - if(temp) *temp = '\0'; - cli_state->file_path = line[i]; - i++; - printf("[%s][%d] [index = %d] target file name = %s\n", __FUNCTION__, __LINE__, i, line[i-1]); - if(i >= cli_state->curr_up_file) - break; - } - if(cli_state->curr_up_file <= cli_state->total_files) - { - fprintf(stdout,"*****uploading file: %s *****\n",line[i-1]); //print the file contents on stdout. - upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path, cli_state); - cli_state->curr_up_file++; - } - else - { - fprintf(stdout,"***** done uploading files *****\n"); - fclose(file); - exit(0); - } - } - else - { - /* handle single file upload from the command line */ - upload_file(cli_state->env, cli_state->bucket_id, cli_state->file_path, cli_state); - } - } -} - -static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state) -{ - int num_of_tokens = 0x00; - char *token[10]; - - memset(token, 0x00, sizeof(token)); - - int file_exist_status = file_exists(path); - const char *file_name = NULL; - char cwd[1024]; - char *upload_list = cwd; - memset(upload_list, 0x00, sizeof(cwd)); - switch(file_exist_status) - { - case CLI_UNKNOWN_FILE_ATTR: - case CLI_NO_SUCH_FILE_OR_DIR: - printf("[%s][%d] file path = %s\n", __FUNCTION__, __LINE__, path); - printf("Invalid filename \n"); - break; - - case CLI_VALID_REGULAR_FILE: - file_name = get_filename_separator(path); - - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - cli_state->total_files = 0x00; - cli_state->curr_up_file = 0x00; - switch (num_of_tokens) - { - case 0x03: /* local filename and upload filename are valid names */ - if ((strcmp(file_name, token[2]) == 0x00) || - (strcmp(token[2], ".") == 0x00)) - { - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = path; - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); - } - } - else - { - printf("Invalid upload target filename - "); - printf("Use same filename as source or '.' or blank \n"); - return -1; - } - break; - - case 0x02: /* missing upload filename */ - if (token[2] == NULL) - { - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - cli_state->file_path = path; - printf("[%d] target file name = %s\n", __LINE__, file_name); - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); - } - } - break; - case 0x01: - case 0x00: - default: - printf("[%s] Invalid command ... token[2]=%s \n", __FUNCTION__, token[2]); - return -1; - break; - } - break; - - case CLI_VALID_DIR: - if ((upload_list = getenv("PWD")) != NULL) - { - fprintf(stdout, "Current working dir: %s\n", upload_list); - strcat(upload_list, "/output.txt"); - fprintf(stdout, "Current working dir: %s\n", upload_list); - if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) - { - printf("KSA:[%s][%d] Upload file list exists \n", __FUNCTION__, __LINE__); - } - } - else - { - perror("getenv() error"); - return -1; - } - - printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); - if(file_exists(upload_list) == CLI_VALID_REGULAR_FILE) - { - /* start reading one file at a time and upload the files */ - FILE *file = fopen ( upload_list, "r" ); - - if (file != NULL) - { - char line [256][256]; - char *temp; - int i = 0x00; - memset(line, 0x00, sizeof(line)); - cli_state->file_name = upload_list; - printf("KSA[%s][%d] upload file : %s\n", __FUNCTION__, __LINE__, upload_list); - printf("[%s][%d] upload file name = %s\n", __FUNCTION__, __LINE__, cli_state->file_name); - /* read a line from a file */ - while(fgets(line[i],sizeof(line), file)!= NULL) - { - i++; - } - cli_state->total_files = i; - if(cli_state->total_files > 0x00) - { - cli_state->curr_up_file = 0x01; - } - else - { - cli_state->curr_up_file = 0x00; - } - printf("[%s][%d] total upload files = %d\n", __FUNCTION__, __LINE__, cli_state->total_files); - printf("[%s][%d] upload cur up file# = %d\n", __FUNCTION__, __LINE__, cli_state->curr_up_file); - fclose(file); - } - else - { - /* print the error message on stderr. */ - perror(upload_list); - } - } - - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - printf("KSA:[%s] num of tokens = %d \n", __FUNCTION__, num_of_tokens); - for(int j = 0x00; j < num_of_tokens; j++) - { - printf("KSA:[%s] token[%d] = %s\n", __FUNCTION__, j,token[j]); - } - - cli_state->curr_cmd_req = "upload-file"; - cli_state->bucket_name = token[1]; - printf("[%s][%d] bucket id = %s\n", __FUNCTION__, __LINE__, cli_state->bucket_id); - if(!cli_state->bucket_id) - { - storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); - } - break; - - default: - break; - }/* switch - case */ - - return 0; -} - -static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_state) -{ - /* download-file command */ - char *file_name = NULL; - int num_of_tokens = 0x00; - char *token[10]; - int ret_status = 0x00; - - memset(token, 0x00, sizeof(token)); - - /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ - num_of_tokens = validate_cmd_tokenize(bucket_name, token); - - cli_state->curr_cmd_req = "download-file"; - cli_state->bucket_name = token[1]; - cli_state->file_name = token[2]; - cli_state->file_path = path; - if (!cli_state->bucket_name || !cli_state->file_name || !cli_state->file_path) - { - printf("Missing arguments: storj cp [-rR] storj:/// \n"); - ret_status = -1; - } - else - { - if (strcmp(cli_state->file_name, "*") == 0x00) - { - if (check_file_path(cli_state->file_path) == CLI_VALID_DIR) - { - ret_status = storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); - } - else - { - printf("storj;// cp target '%s' is not a directory\n", cli_state->file_path); - ret_status = -1; - } - } - else - { - ret_status = storj_bridge_get_buckets(cli_state->env, cli_state, get_bucket_id_callback); - } - } - return ret_status; -} -#endif - From b01528e6e6700ac960901e9840897355a085d1ad Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 31 Jan 2018 12:00:41 -0500 Subject: [PATCH 57/68] **NEW CLI COMMANDS DEVELOPED** --- src/cli.c | 26 +------------------------- src/storj.c | 20 ++++++++++++++++++++ src/storj.h | 10 ++++++++++ src/storjapi_callback.c | 24 ++++++++++++++++++++++++ src/storjapi_callback.h | 5 +++++ 5 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/cli.c b/src/cli.c index f3d15ed..53d919b 100644 --- a/src/cli.c +++ b/src/cli.c @@ -798,30 +798,6 @@ static void register_callback(uv_work_t *work_req, int status) free(work_req); } -static void get_buckets_callback(uv_work_t *work_req, int status) -{ - assert(status == 0); - get_buckets_request_t *req = work_req->data; - - if (req->status_code == 401) { - printf("Invalid user credentials.\n"); - } else if (req->status_code != 200 && req->status_code != 304) { - printf("Request failed with status code: %i\n", req->status_code); - } else if (req->total_buckets == 0) { - printf("No buckets.\n"); - } - - for (int i = 0; i < req->total_buckets; i++) { - storj_bucket_meta_t *bucket = &req->buckets[i]; - printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", - bucket->id, bucket->decrypted ? "true" : "false", - bucket->created, bucket->name); - } - - storj_free_get_buckets_request(req); - free(work_req); -} - static void create_bucket_callback(uv_work_t *work_req, int status) { assert(status == 0); @@ -1797,7 +1773,7 @@ int main(int argc, char **argv) } else { - storj_bridge_get_buckets(env, NULL, get_buckets_callback); + storj_list_buckets(storj_api); } } else if (strcmp(command, "get-bucket-id") == 0) diff --git a/src/storj.c b/src/storj.c index 5063eb6..636e01f 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1680,6 +1680,26 @@ STORJ_API int storj_bridge_register(storj_env_t *env, return uv_queue_work(env->loop, (uv_work_t*) work, json_request_worker, cb); } + +STORJ_API int storj_list_buckets(storj_api_t *storj_api) +{ + char *bucket_name = storj_api->bucket_name; + if (!bucket_name) + { + printf("Missing : \n"); + return STORJAPI_BUCKET_NAME_MISSING_ERROR; + } + + storj_api->last_cmd_req = NULL; + storj_api->curr_cmd_req = "get-bucket-id-req"; + storj_api->next_cmd_req = NULL; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "get-bucket-id-resp"; + + /* when callback returns, we store the bucket id of bucket name else null */ + return storj_bridge_get_buckets(storj_api->env, storj_api, get_buckets_callback); +} + STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) { char *bucket_name = storj_api->bucket_name; diff --git a/src/storj.h b/src/storj.h index e19c073..3c662f3 100755 --- a/src/storj.h +++ b/src/storj.h @@ -1074,6 +1074,16 @@ STORJ_API int storj_bridge_register(storj_env_t *env, const char *password, void *handle, uv_after_work_cb cb); + +/** + * @brief Function lists the bucket names & IDs + * + * @param[in] storj_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +STORJ_API int storj_list_buckets(storj_api_t *storj_api); + /** * @brief Function returns the corresponding bucket's id for a * given bucket name diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 1eea7d7..64ebc83 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -722,6 +722,30 @@ static void delete_bucket_callback(uv_work_t *work_req, int status) free(work_req); } +void get_buckets_callback(uv_work_t *work_req, int status) +{ + assert(status == 0); + get_buckets_request_t *req = work_req->data; + + if (req->status_code == 401) { + printf("Invalid user credentials.\n"); + } else if (req->status_code != 200 && req->status_code != 304) { + printf("Request failed with status code: %i\n", req->status_code); + } else if (req->total_buckets == 0) { + printf("No buckets.\n"); + } + + for (int i = 0; i < req->total_buckets; i++) { + storj_bucket_meta_t *bucket = &req->buckets[i]; + printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", + bucket->id, bucket->decrypted ? "true" : "false", + bucket->created, bucket->name); + } + + storj_free_get_buckets_request(req); + free(work_req); +} + void get_bucket_id_callback(uv_work_t *work_req, int status) { int ret_status = 0x00; diff --git a/src/storjapi_callback.h b/src/storjapi_callback.h index 5881519..6ddad6d 100755 --- a/src/storjapi_callback.h +++ b/src/storjapi_callback.h @@ -21,6 +21,11 @@ extern "C" { #define CLI_UNKNOWN_FILE_ATTR 0x03 #define CLI_UPLOAD_FILE_LOG_ERR 0x04 +/** + * @brief Callback function listing bucket names & IDs + */ +void get_buckets_callback(uv_work_t *work_req, int status); + /** * @brief Callback function returning the bucket id for a given * bucket name From 226a1b3bb039ad880f00193524dbea1ddc31d197 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 31 Jan 2018 13:17:54 -0500 Subject: [PATCH 58/68] ***READY TO BE STRESS TESTED AND MERGED TO MAIN*** Fixed compilation errors and Warnings. --- src/Makefile.am | 4 ++-- src/storjapi_callback.c | 4 ++-- src/storjapi_callback.h | 9 ------- test/tests.c | 53 ++++++++++++++++++++++++++++------------- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 0ec354e..4301314 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ lib_LTLIBRARIES = libstorj.la -libstorj_la_SOURCES = storj.c utils.c utils.h http.c http.h uploader.c uploader.h downloader.c downloader.h bip39.c bip39.h bip39_english.h crypto.c crypto.h rs.c rs.h +libstorj_la_SOURCES = storj.c utils.c utils.h http.c http.h uploader.c uploader.h downloader.c downloader.h bip39.c bip39.h bip39_english.h crypto.c crypto.h rs.c rs.h storjapi_callback.c storjapi_callback.h libstorj_la_LIBADD = -lcurl -lnettle -ljson-c -luv -lm # The rules of thumb, when dealing with these values are: # - Always increase the revision value. @@ -14,7 +14,7 @@ endif include_HEADERS = storj.h bin_PROGRAMS = storj -storj_SOURCES = cli.c storj.h storjapi_callback.c storjapi_callback.h +storj_SOURCES = cli.c storj.h storj_LDADD = libstorj.la if BUILD_STORJ_DLL storj_LDFLAGS = -Wall diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 64ebc83..97c87da 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -77,7 +77,7 @@ static void printdir(char *dir, int depth, FILE *src_fd, void *handle) return; } - chdir(dir); + int ret = chdir(dir); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); @@ -97,7 +97,7 @@ static void printdir(char *dir, int depth, FILE *src_fd, void *handle) fprintf(src_fd, "%s%s\n", "", full_path); } } - chdir(".."); + ret = chdir(".."); closedir(dp); free(full_path); } diff --git a/src/storjapi_callback.h b/src/storjapi_callback.h index 6ddad6d..a577a33 100755 --- a/src/storjapi_callback.h +++ b/src/storjapi_callback.h @@ -32,20 +32,11 @@ void get_buckets_callback(uv_work_t *work_req, int status); */ void get_bucket_id_callback(uv_work_t *work_req, int status); -/** - * @brief Callback function listing the files in a given bucket - * - */ -void list_files_callback(uv_work_t *work_req, int status); - /** * @brief Storj api state machine function */ void queue_next_cmd_req(storj_api_t *storj_api); - - - #ifdef __cplusplus } #endif diff --git a/test/tests.c b/test/tests.c index 6a0609e..dd90c53 100644 --- a/test/tests.c +++ b/test/tests.c @@ -283,11 +283,11 @@ void check_store_file_progress(double progress, } } -void check_store_file(int error_code, char *file_id, void *handle) +void check_store_file(int error_code, storj_file_meta_t *file, void *handle) { assert(handle == NULL); if (error_code == 0) { - if (strcmp(file_id, "85fb0ed00de1196dc22e0f6d") == 0 ) { + if (file && strcmp(file->id, "85fb0ed00de1196dc22e0f6d") == 0 ) { pass("storj_bridge_store_file"); } else { fail("storj_bridge_store_file(0)"); @@ -297,10 +297,10 @@ void check_store_file(int error_code, char *file_id, void *handle) printf("\t\tERROR: %s\n", storj_strerror(error_code)); } - free(file_id); + storj_free_uploaded_file_info(file); } -void check_store_file_cancel(int error_code, char *file_id, void *handle) +void check_store_file_cancel(int error_code, storj_file_meta_t *file, void *handle) { assert(handle == NULL); if (error_code == STORJ_TRANSFER_CANCELED) { @@ -310,7 +310,7 @@ void check_store_file_cancel(int error_code, char *file_id, void *handle) printf("\t\tERROR: %s\n", storj_strerror(error_code)); } - free(file_id); + storj_free_uploaded_file_info(file); } void check_delete_file(uv_work_t *work_req, int status) @@ -412,22 +412,15 @@ void check_delete_frame(uv_work_t *work_req, int status) void check_file_info(uv_work_t *work_req, int status) { assert(status == 0); - json_request_t *req = work_req->data; + get_file_info_request_t *req = work_req->data; assert(req->handle == NULL); + assert(req->file); + assert(strcmp(req->file->filename, "storj-test-download.data") == 0); + assert(strcmp(req->file->mimetype, "video/ogg") == 0); - struct json_object *value; - int success = json_object_object_get_ex(req->response, "mimetype", &value); - assert(success == 1); - assert(json_object_is_type(value, json_type_string) == 1); - - const char* mimetype = json_object_get_string(value); - - assert(strcmp(mimetype, "video/ogg") == 0); pass("storj_bridge_get_file_info"); - json_object_put(req->response); - free(req->path); - free(req); + storj_free_get_file_info_request(req); free(work_req); } @@ -1106,6 +1099,31 @@ int test_generate_seed_256_trezor() return 0; } +int test_generate_seed_null_mnemonic() +{ + char *mnemonic = NULL; + char *seed = calloc(128 + 1, sizeof(char)); + char *expected_seed = "4ed8d4b17698ddeaa1f1559f152f87b5d472f725ca86d341bd0276f1b61197e21dd5a391f9f5ed7340ff4d4513aab9cce44f9497a5e7ed85fd818876b6eb402e"; + + mnemonic_to_seed(mnemonic, "", &seed); + seed[128] = '\0'; + + int check = memcmp(seed, expected_seed, 128); + if (check != 0) { + fail("test_generate_seed"); + printf("\t\texpected seed: %s\n", expected_seed); + printf("\t\tactual seed: %s\n", seed); + + free(seed); + return 1; + } + + free(seed); + pass("test_generate_seed_null_mnemonic"); + + return 0; +} + int test_generate_bucket_key() { char *mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; @@ -1580,6 +1598,7 @@ int main(void) test_generate_seed(); test_generate_seed_256(); test_generate_seed_256_trezor(); + test_generate_seed_null_mnemonic(); printf("\n"); printf("Test Suite: Crypto\n"); From 94515db866a6e2f6375f72aeadde2b5f21b2070a Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 31 Jan 2018 15:42:13 -0500 Subject: [PATCH 59/68] Updated the help section --- src/cli.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/cli.c b/src/cli.c index 53d919b..faaf657 100644 --- a/src/cli.c +++ b/src/cli.c @@ -64,24 +64,39 @@ static inline void noop() {}; #define HELP_TEXT "usage: storj [] []\n\n" \ "These are common Storj commands for various situations:\n\n" \ - "setting up users profiles\n" \ + "setting up users profiles:\n" \ + "=========================\n" \ " register setup a new storj bridge user\n" \ " import-keys import existing user\n" \ " export-keys export bridge user, password and " \ "encryption keys\n\n" \ - "working with buckets and files\n" \ + "working with buckets and files:\n" \ + "==============================\n" \ " list-buckets\n" \ - " get-bucket-id \n" \ + " ls (lists the available buckets)\n" \ " list-files \n" \ - " remove-file \n" \ + " ls (lists the files in a bucket)\n" \ + " get-bucket-id \n" \ + " remove-file \n" \ + " rm (to remove a file from a bucket)\n" \ + " remove-bucket \n" \ + " rm (to remove a bucket)\n" \ " add-bucket \n" \ - " remove-bucket \n" \ - " list-mirrors \n\n" \ - "downloading and uploading files\n" \ + " mkbkt \n" \ + " list-mirrors \n" \ + " lm \n\n" \ + "uploading files:\n" \ + "===============\n" \ " upload-file \n" \ - " cp storj:///" \ + " cp [-rR] " \ + "storj:///\n" \ + " (e.g. storj cp -[rR] /some-dir/* storj://bucketname/.)\n\n" \ + "downloading files:\n" \ + "=================\n" \ " download-file \n" \ - " cp storj:/// " \ + " cp [-rR] storj:/// " \ + "\n" \ + " (e.g. storj cp -[rR] storj://bucketname/ /some-dir/.)\n\n" \ "bridge api information\n" \ " get-info\n\n" \ "options:\n" \ From d5de6791e0c2439a55679814073e06b2e520da04 Mon Sep 17 00:00:00 2001 From: kishore Date: Sun, 4 Feb 2018 12:31:59 -0500 Subject: [PATCH 60/68] Has pulled in changes from the main libstorj and a fixes a dangling pointer --- src/crypto.c | 102 +++++++++++- src/crypto.h | 51 ++++++ src/storj.c | 360 ++++++++++++++++++++++++---------------- src/storj.h | 63 ++++++- src/storjapi_callback.c | 11 +- src/uploader.c | 40 +---- src/utils.c | 48 ++++++ src/utils.h | 12 ++ test/mockbridge.c | 12 +- test/mockbridge.json | 3 + test/tests.c | 66 ++++++++ 11 files changed, 577 insertions(+), 191 deletions(-) diff --git a/src/crypto.c b/src/crypto.c index 316f830..7811c73 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -141,6 +141,98 @@ int generate_file_key(const char *mnemonic, const char *bucket_id, return status; } +int decrypt_bucket_name(const char *mnemonic, const char *encrypted_name, char **decrypted_name) { + return decrypt_file_name(mnemonic, BUCKET_NAME_MAGIC, encrypted_name, decrypted_name); +} + +int decrypt_file_name(const char *mnemonic, const char* bucket_id, + const char *encrypted_name, char **decrypted_name) { + int status = 0; + + // Derive a key based on the bucket id + char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); + generate_bucket_key(mnemonic, bucket_id, &bucket_key_as_str); + + uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); + if (!bucket_key) { + status = 1; + goto cleanup; + } + + // Get encryption key with first half of hmac w/ magic + struct hmac_sha512_ctx ctx1; + hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); + hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); + uint8_t key[SHA256_DIGEST_SIZE]; + hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); + + status = decrypt_meta(encrypted_name, key, decrypted_name); + +cleanup: + + if (bucket_key) { + memset_zero(bucket_key, BASE16_DECODE_LENGTH(strlen(bucket_key_as_str)) + 1); + free(bucket_key); + } + if (bucket_key_as_str) { + memset_zero(bucket_key_as_str, DETERMINISTIC_KEY_SIZE + 1); + free(bucket_key_as_str); + } + + return status; +} + +int encrypt_bucket_name(const char *mnemonic, const char *bucket_name, char **encrypted_name) { + return encrypt_file_name(mnemonic, BUCKET_NAME_MAGIC, bucket_name, encrypted_name); +} + +int encrypt_file_name(const char *mnemonic, const char* bucket_id, + const char *file_name, char **encrypted_name) { + int status = 0; + + // Derive a key based on the bucket id + char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); + generate_bucket_key(mnemonic, bucket_id, &bucket_key_as_str); + + uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); + if (!bucket_key) { + status = 1; + goto cleanup; + } + + // Get encryption key with first half of hmac w/ magic + struct hmac_sha512_ctx ctx1; + hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); + hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); + uint8_t key[SHA256_DIGEST_SIZE]; + hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); + + // Generate the synthetic iv with first half of hmac w/ bucket and filename + struct hmac_sha512_ctx ctx2; + hmac_sha512_set_key(&ctx2, SHA256_DIGEST_SIZE, bucket_key); + if (strcmp(bucket_id, BUCKET_NAME_MAGIC)) { + hmac_sha512_update(&ctx2, strlen(bucket_id), (uint8_t *) bucket_id); + } + hmac_sha512_update(&ctx2, strlen(file_name), (uint8_t *)file_name); + uint8_t iv[SHA256_DIGEST_SIZE]; + hmac_sha512_digest(&ctx2, SHA256_DIGEST_SIZE, iv); + + status = encrypt_meta(file_name, key, iv, encrypted_name); + +cleanup: + + if (bucket_key) { + memset_zero(bucket_key, BASE16_DECODE_LENGTH(strlen(bucket_key_as_str)) + 1); + free(bucket_key); + } + if (bucket_key_as_str) { + memset_zero(bucket_key_as_str, DETERMINISTIC_KEY_SIZE + 1); + free(bucket_key_as_str); + } + + return status; +} + int get_deterministic_key(const char *key, int key_len, const char *id, char **buffer) { @@ -462,18 +554,20 @@ int decrypt_meta(const char *buffer_base64, if (!base64_decode_update(&ctx3, &decode_len, buffer, strlen(buffer_base64), (uint8_t *)buffer_base64)) { free(buffer); - return 1; + //STORJ_META_DECRYPTION_ERROR + return 6001; } if (!base64_decode_final(&ctx3)) { free(buffer); - return 1; + //STORJ_META_DECRYPTION_ERROR + return 6001; } if (GCM_DIGEST_SIZE + SHA256_DIGEST_SIZE + 1 > decode_len) { free(buffer); //STORJ_META_DECRYPTION_ERROR - return 1; + return 6001; } size_t length = decode_len - GCM_DIGEST_SIZE - SHA256_DIGEST_SIZE; @@ -511,7 +605,7 @@ int decrypt_meta(const char *buffer_base64, int digest_match = memcmp(actual_digest, digest, GCM_DIGEST_SIZE); if (digest_match != 0) { //STORJ_META_DECRYPTION_ERROR - return 1; + return 6001; } *filemeta = calloc(length + 1, sizeof(char)); diff --git a/src/crypto.h b/src/crypto.h index 3f1eb49..f83eb9b 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -71,6 +71,57 @@ int generate_file_key(const char *mnemonic, const char *index, char **file_key); +/** + * @brief Decrypt a bucket name + * + * @param[in] mnemonic Character array of the mnemonic + * @param[in] encrypted_name Character array of the encrypted name + * @param[out] decrypted_name Character array of the decrypted name + * @return A non-zero error value on failure and 0 on success. + */ +int decrypt_bucket_name(const char *mnemonic, + const char *encrypted_name, + char **decrypted_name); + +/** + * @brief Decrypt a file name + * + * @param[in] mnemonic Character array of the mnemonic + * @param[in] bucket_id Character array of bucket id + * @param[in] encrypted_name Character array of the encrypted name + * @param[out] decrypted_name Character array of the decrypted name + * @return A non-zero error value on failure and 0 on success. + */ +int decrypt_file_name(const char *mnemonic, + const char *bucket_id, + const char *encrypted_name, + char **decrypted_name); +/** + * @brief Encrypt a bucket name + * + * @param[in] mnemonic Character array of the mnemonic + * @param[in] bucket_name Character array of the bucket name + * @param[out] encrypted_name Character array of the encrypted name + * @return A non-zero error value on failure and 0 on success. + */ +int encrypt_bucket_name(const char *mnemonic, + const char *bucket_name, + char **encrypted_name); + +/** + * @brief Encrypt a file name + * + * @param[in] mnemonic Character array of the mnemonic + * @param[in] bucket_id Character array of bucket id + * @param[in] file_name Character array of the file name + * @param[out] encrypted_name Character array of the encrypted name + * @return A non-zero error value on failure and 0 on success. + */ +int encrypt_file_name(const char *mnemonic, + const char *bucket_id, + const char *file_name, + char **encrypted_name); + /** * @brief Calculate deterministic key by getting sha512 of key + id * diff --git a/src/storj.c b/src/storj.c index 636e01f..8b530c6 100644 --- a/src/storj.c +++ b/src/storj.c @@ -23,42 +23,14 @@ static void create_bucket_request_worker(uv_work_t *work) create_bucket_request_t *req = work->data; int status_code = 0; - // Derive a key based on the master seed and bucket name magic number - char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); - generate_bucket_key(req->encrypt_options->mnemonic, - BUCKET_NAME_MAGIC, - &bucket_key_as_str); - - uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); - if (!bucket_key) { + // Encrypt the bucket name + if (encrypt_bucket_name(req->encrypt_options->mnemonic, + req->bucket_name, + (char **)&req->encrypted_bucket_name)) { req->error_code = STORJ_MEMORY_ERROR; return; } - free(bucket_key_as_str); - - // Get bucket name encryption key with first half of hmac w/ magic - struct hmac_sha512_ctx ctx1; - hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); - uint8_t key[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); - - // Generate the synthetic iv with first half of hmac w/ name - struct hmac_sha512_ctx ctx2; - hmac_sha512_set_key(&ctx2, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx2, strlen(req->bucket_name), - (uint8_t *)req->bucket_name); - uint8_t bucketname_iv[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx2, SHA256_DIGEST_SIZE, bucketname_iv); - - free(bucket_key); - - // Encrypt the bucket name - char *encrypted_bucket_name; - encrypt_meta(req->bucket_name, key, bucketname_iv, &encrypted_bucket_name); - req->encrypted_bucket_name = encrypted_bucket_name; - struct json_object *body = json_object_new_object(); json_object *name = json_object_new_string(req->encrypted_bucket_name); json_object_object_add(body, "name", name); @@ -109,29 +81,6 @@ static void get_buckets_request_worker(uv_work_t *work) req->total_buckets = num_buckets; } - // Derive a key based on the master seed - char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); - generate_bucket_key(req->encrypt_options->mnemonic, - BUCKET_NAME_MAGIC, - &bucket_key_as_str); - - uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); - if (!bucket_key) { - req->error_code = STORJ_MEMORY_ERROR; - return; - } - - free(bucket_key_as_str); - - // Get bucket name encryption key with first half of hmac w/ magic - struct hmac_sha512_ctx ctx1; - hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); - uint8_t key[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); - - free(bucket_key); - struct json_object *bucket_item; struct json_object *name; struct json_object *created; @@ -159,14 +108,17 @@ static void get_buckets_request_worker(uv_work_t *work) continue; } char *decrypted_name; - int error_status = decrypt_meta(encrypted_name, key, - &decrypted_name); + int error_status = decrypt_bucket_name(req->encrypt_options->mnemonic, + encrypted_name, + &decrypted_name); if (!error_status) { bucket->decrypted = true; bucket->name = decrypted_name; - } else { + } else if (error_status == STORJ_META_DECRYPTION_ERROR){ bucket->decrypted = false; bucket->name = strdup(encrypted_name); + } else { + req->error_code = STORJ_MEMORY_ERROR; } } } @@ -187,29 +139,6 @@ static void get_bucket_request_worker(uv_work_t *work) return; } - // Derive a key based on the master seed - char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); - generate_bucket_key(req->encrypt_options->mnemonic, - BUCKET_NAME_MAGIC, - &bucket_key_as_str); - - uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); - if (!bucket_key) { - req->error_code = STORJ_MEMORY_ERROR; - return; - } - - free(bucket_key_as_str); - - // Get bucket name encryption key with first half of hmac w/ magic - struct hmac_sha512_ctx ctx1; - hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); - uint8_t key[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); - - free(bucket_key); - struct json_object *name; struct json_object *created; struct json_object *id; @@ -231,18 +160,66 @@ static void get_bucket_request_worker(uv_work_t *work) const char *encrypted_name = json_object_get_string(name); if (encrypted_name) { char *decrypted_name; - int error_status = decrypt_meta(encrypted_name, key, - &decrypted_name); + int error_status = decrypt_bucket_name(req->encrypt_options->mnemonic, + encrypted_name, + &decrypted_name); if (!error_status) { req->bucket->decrypted = true; req->bucket->name = decrypted_name; - } else { + } else if (error_status == STORJ_META_DECRYPTION_ERROR){ req->bucket->decrypted = false; req->bucket->name = strdup(encrypted_name); + } else { + req->error_code = STORJ_MEMORY_ERROR; } } } +static void get_bucket_id_request_worker(uv_work_t *work) +{ + get_bucket_id_request_t *req = work->data; + int status_code = 0; + + // Encrypt the bucket name + char *encrypted_bucket_name; + if (encrypt_bucket_name(req->encrypt_options->mnemonic, + req->bucket_name, + &encrypted_bucket_name)) { + req->error_code = STORJ_MEMORY_ERROR; + goto cleanup; + } + + char *escaped_encrypted_bucket_name = str_replace("/", "%2F", encrypted_bucket_name); + if (!escaped_encrypted_bucket_name) { + req->error_code = STORJ_MEMORY_ERROR; + goto cleanup; + } + + char *path = str_concat_many(2, "/bucket-ids/", escaped_encrypted_bucket_name); + if (!path) { + req->error_code = STORJ_MEMORY_ERROR; + goto cleanup; + } + + req->error_code = fetch_json(req->http_options, + req->options, "GET", path, NULL, + true, &req->response, &status_code); + + if (req->response != NULL) { + struct json_object *id; + json_object_object_get_ex(req->response, "id", &id); + req->bucket_id = json_object_get_string(id); + } + + req->status_code = status_code; + +cleanup: + + free(encrypted_bucket_name); + free(escaped_encrypted_bucket_name); + free(path); +} + static void list_files_request_worker(uv_work_t *work) { list_files_request_t *req = work->data; @@ -265,29 +242,6 @@ static void list_files_request_worker(uv_work_t *work) req->total_files = num_files; } - // Get the bucket key to encrypt the filename from bucket id - char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); - generate_bucket_key(req->encrypt_options->mnemonic, - req->bucket_id, - &bucket_key_as_str); - - uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); - if (!bucket_key) { - req->error_code = STORJ_MEMORY_ERROR; - return; - } - - free(bucket_key_as_str); - - // Get file name encryption key with first half of hmac w/ magic - struct hmac_sha512_ctx ctx1; - hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); - uint8_t key[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); - - free(bucket_key); - struct json_object *file; struct json_object *filename; struct json_object *mimetype; @@ -332,14 +286,18 @@ static void list_files_request_worker(uv_work_t *work) continue; } char *decrypted_file_name; - int error_status = decrypt_meta(encrypted_file_name, key, - &decrypted_file_name); + int error_status = decrypt_file_name(req->encrypt_options->mnemonic, + req->bucket_id, + encrypted_file_name, + &decrypted_file_name); if (!error_status) { file->decrypted = true; file->filename = decrypted_file_name; - } else { + } else if (error_status == STORJ_META_DECRYPTION_ERROR) { file->decrypted = false; file->filename = strdup(encrypted_file_name); + } else { + req->error_code = STORJ_MEMORY_ERROR; } } } @@ -355,30 +313,6 @@ static void get_file_info_request_worker(uv_work_t *work) req->status_code = status_code; - // Get the bucket key to encrypt the filename from bucket id - char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); - generate_bucket_key(req->encrypt_options->mnemonic, - req->bucket_id, - &bucket_key_as_str); - - uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); - if (!bucket_key) { - req->error_code = STORJ_MEMORY_ERROR; - return; - } - - free(bucket_key_as_str); - - // Get file name encryption key with first half of hmac w/ magic - struct hmac_sha512_ctx ctx1; - hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); - uint8_t key[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); - - free(bucket_key); - - struct json_object *file; struct json_object *filename; struct json_object *mimetype; struct json_object *size; @@ -416,18 +350,68 @@ static void get_file_info_request_worker(uv_work_t *work) const char *encrypted_file_name = json_object_get_string(filename); if (encrypted_file_name) { char *decrypted_file_name; - int error_status = decrypt_meta(encrypted_file_name, key, - &decrypted_file_name); + int error_status = decrypt_file_name(req->encrypt_options->mnemonic, + req->bucket_id, + encrypted_file_name, + &decrypted_file_name); if (!error_status) { req->file->decrypted = true; req->file->filename = decrypted_file_name; - } else { + } else if (error_status == STORJ_META_DECRYPTION_ERROR) { req->file->decrypted = false; req->file->filename = strdup(encrypted_file_name); + } else { + req->error_code = STORJ_MEMORY_ERROR; } } } +static void get_file_id_request_worker(uv_work_t *work) +{ + get_file_id_request_t *req = work->data; + int status_code = 0; + + char *encrypted_file_name; + if (encrypt_file_name(req->encrypt_options->mnemonic, + req->bucket_id, + req->file_name, + &encrypted_file_name)) { + req->error_code = STORJ_MEMORY_ERROR; + goto cleanup; + } + + char *escaped_encrypted_file_name = str_replace("/", "%2F", encrypted_file_name); + if (!escaped_encrypted_file_name) { + req->error_code = STORJ_MEMORY_ERROR; + goto cleanup; + } + + char *path = str_concat_many(4, "/buckets/", req->bucket_id, + "/file-ids/", escaped_encrypted_file_name); + if (!path) { + req->error_code = STORJ_MEMORY_ERROR; + goto cleanup; + } + + req->error_code = fetch_json(req->http_options, + req->options, "GET", path, NULL, + true, &req->response, &status_code); + + if (req->response != NULL) { + struct json_object *id; + json_object_object_get_ex(req->response, "id", &id); + req->file_id = json_object_get_string(id); + } + + req->status_code = status_code; + +cleanup: + + free(encrypted_file_name); + free(escaped_encrypted_file_name); + free(path); +} + static uv_work_t *uv_work_new() { uv_work_t *work = malloc(sizeof(uv_work_t)); @@ -619,6 +603,58 @@ static get_bucket_request_t *get_bucket_request_new( return req; } +static get_bucket_id_request_t *get_bucket_id_request_new( + storj_http_options_t *http_options, + storj_bridge_options_t *options, + storj_encrypt_options_t *encrypt_options, + const char *bucket_name, + void *handle) +{ + get_bucket_id_request_t *req = malloc(sizeof(get_bucket_id_request_t)); + if (!req) { + return NULL; + } + + req->http_options = http_options; + req->options = options; + req->encrypt_options = encrypt_options; + req->bucket_name = bucket_name; + req->response = NULL; + req->bucket_id = NULL; + req->error_code = 0; + req->status_code = 0; + req->handle = handle; + + return req; +} + +static get_file_id_request_t *get_file_id_request_new( + storj_http_options_t *http_options, + storj_bridge_options_t *options, + storj_encrypt_options_t *encrypt_options, + const char *bucket_id, + const char *file_name, + void *handle) +{ + get_file_id_request_t *req = malloc(sizeof(get_file_id_request_t)); + if (!req) { + return NULL; + } + + req->http_options = http_options; + req->options = options; + req->encrypt_options = encrypt_options; + req->bucket_id = bucket_id; + req->file_name = file_name; + req->response = NULL; + req->file_id = NULL; + req->error_code = 0; + req->status_code = 0; + req->handle = handle; + + return req; +} + static uv_work_t *json_request_work_new( storj_env_t *env, char *method, @@ -1413,6 +1449,27 @@ STORJ_API void storj_free_get_bucket_request(get_bucket_request_t *req) free(req); } +STORJ_API int storj_bridge_get_bucket_id(storj_env_t *env, + const char *name, + void *handle, + uv_after_work_cb cb) +{ + uv_work_t *work = uv_work_new(); + if (!work) { + return STORJ_MEMORY_ERROR; + } + + work->data = get_bucket_id_request_new(env->http_options, + env->bridge_options, + env->encrypt_options, + name, handle); + if (!work->data) { + return STORJ_MEMORY_ERROR; + } + + return uv_queue_work(env->loop, (uv_work_t*) work, get_bucket_id_request_worker, cb); +} + STORJ_API int storj_bridge_list_files(storj_env_t *env, const char *id, void *handle, @@ -1617,6 +1674,28 @@ STORJ_API int storj_bridge_get_file_info(storj_env_t *env, get_file_info_request_worker, cb); } +STORJ_API int storj_bridge_get_file_id(storj_env_t *env, + const char *bucket_id, + const char *file_name, + void *handle, + uv_after_work_cb cb) +{ + uv_work_t *work = uv_work_new(); + if (!work) { + return STORJ_MEMORY_ERROR; + } + + work->data = get_file_id_request_new(env->http_options, + env->bridge_options, + env->encrypt_options, + bucket_id, file_name, handle); + if (!work->data) { + return STORJ_MEMORY_ERROR; + } + + return uv_queue_work(env->loop, (uv_work_t*) work, get_file_id_request_worker, cb); +} + STORJ_API void storj_free_get_file_info_request(get_file_info_request_t *req) { if (req->response) { @@ -1683,13 +1762,6 @@ STORJ_API int storj_bridge_register(storj_env_t *env, STORJ_API int storj_list_buckets(storj_api_t *storj_api) { - char *bucket_name = storj_api->bucket_name; - if (!bucket_name) - { - printf("Missing : \n"); - return STORJAPI_BUCKET_NAME_MISSING_ERROR; - } - storj_api->last_cmd_req = NULL; storj_api->curr_cmd_req = "get-bucket-id-req"; storj_api->next_cmd_req = NULL; diff --git a/src/storj.h b/src/storj.h index 3c662f3..9d48787 100755 --- a/src/storj.h +++ b/src/storj.h @@ -287,6 +287,20 @@ typedef struct { void *handle; } get_bucket_request_t; +/** @brief A structure for queueing get bucket id request work + */ +typedef struct { + storj_http_options_t *http_options; + storj_encrypt_options_t *encrypt_options; + storj_bridge_options_t *options; + const char *bucket_name; + struct json_object *response; + const char *bucket_id; + int error_code; + int status_code; + void *handle; +} get_bucket_id_request_t; + /** @brief A structure for that describes a bucket entry/file */ typedef struct { @@ -339,6 +353,21 @@ typedef struct { void *handle; } get_file_info_request_t; +/** @brief A structure for queueing get file id request work + */ +typedef struct { + storj_http_options_t *http_options; + storj_encrypt_options_t *encrypt_options; + storj_bridge_options_t *options; + const char *bucket_id; + const char *file_name; + struct json_object *response; + const char *file_id; + int error_code; + int status_code; + void *handle; +} get_file_id_request_t; + typedef enum { BUCKET_PUSH, BUCKET_PULL @@ -583,9 +612,9 @@ typedef struct storj_api_mib { typedef struct storj_api { storj_env_t *env; char *bucket_name; - char *bucket_id; + char bucket_id[256]; char *file_name; - char *file_id; + char file_id[256]; char *file_path; /**< local upload files directory path */ FILE *src_fd; char src_list[256]; /**< file list ready to upload */ @@ -838,6 +867,20 @@ STORJ_API int storj_bridge_get_bucket(storj_env_t *env, */ STORJ_API void storj_free_get_bucket_request(get_bucket_request_t *req); +/** + * @brief Get the bucket id by name. + * + * @param[in] env The storj environment struct + * @param[in] name The bucket name + * @param[in] handle A pointer that will be available in the callback + * @param[in] cb A function called with response when complete + * @return A non-zero error value on failure and 0 on success. + */ +STORJ_API int storj_bridge_get_bucket_id(storj_env_t *env, + const char *name, + void *handle, + uv_after_work_cb cb); + /** * @brief Get a list of all files in a bucket. * @@ -982,6 +1025,22 @@ STORJ_API int storj_bridge_get_file_info(storj_env_t *env, */ STORJ_API void storj_free_get_file_info_request(get_file_info_request_t *req); +/** + * @brief Get the file id by name. + * + * @param[in] env The storj environment struct + * @param[in] bucket_id The bucket id + * @param[in] file_name The file name + * @param[in] handle A pointer that will be available in the callback + * @param[in] cb A function called with response when complete + * @return A non-zero error value on failure and 0 on success. + */ +STORJ_API int storj_bridge_get_file_id(storj_env_t *env, + const char *bucket_id, + const char *file_name, + void *handle, + uv_after_work_cb cb); + /** * @brief Get mirror data for a file * diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 97c87da..3f50810 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -783,8 +783,8 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) bucket->created, bucket->name); /* store the bucket id */ - storj_api->bucket_id = (char *)bucket->id; - + memset(storj_api->bucket_id, 0x00, sizeof(storj_api->bucket_id)); + strcpy(storj_api->bucket_id, (char *)bucket->id); break; } else @@ -850,7 +850,6 @@ void list_files_callback(uv_work_t *work_req, int status) goto cleanup; } - storj_api->file_id = NULL; for (int i = 0; i < req->total_files; i++) { storj_file_meta_t *file = &req->files[i]; @@ -859,7 +858,8 @@ void list_files_callback(uv_work_t *work_req, int status) (strcmp(storj_api->file_name, file->filename)) == 0x00) { /* store the file id */ - storj_api->file_id = (char *)file->id; + memset(storj_api->file_id, 0x00, sizeof(storj_api->file_id)); + strcpy(storj_api->file_id, (char *)file->id); } printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", @@ -1101,7 +1101,8 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->final_cmd_req = NULL; } - storj_api->file_id = token[0]; + memset(storj_api->file_id, 0x00, sizeof(storj_api->file_id)); + strcpy(storj_api->file_id, token[0]); strcpy(temp_path, storj_api->file_path); if (storj_api->file_path[(strlen(storj_api->file_path)-1)] != '/') { diff --git a/src/uploader.c b/src/uploader.c index 34d2f51..e4d0b13 100644 --- a/src/uploader.c +++ b/src/uploader.c @@ -371,7 +371,7 @@ static void after_create_bucket_entry(uv_work_t *work, int status) struct json_object *file_id_value = NULL; struct json_object *created_value = NULL; struct json_object *mimetype_value = NULL; - + if (json_object_object_get_ex(req->response, "id", &file_id_value)) { state->info->id = strdup((char *)json_object_get_string(file_id_value)); } @@ -2470,44 +2470,14 @@ static void prepare_upload_state(uv_work_t *work) state->shard[i].work = NULL; } - // Get the bucket key to encrypt the filename - char *bucket_key_as_str = calloc(DETERMINISTIC_KEY_SIZE + 1, sizeof(char)); - generate_bucket_key(state->env->encrypt_options->mnemonic, - state->bucket_id, - &bucket_key_as_str); - - uint8_t *bucket_key = str2hex(strlen(bucket_key_as_str), bucket_key_as_str); - if (!bucket_key) { + if (encrypt_file_name(state->env->encrypt_options->mnemonic, + state->bucket_id, + state->file_name, + (char **)&state->encrypted_file_name)) { state->error_status = STORJ_MEMORY_ERROR; return; } - free(bucket_key_as_str); - - // Get file name encryption key with first half of hmac w/ magic - struct hmac_sha512_ctx ctx1; - hmac_sha512_set_key(&ctx1, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx1, SHA256_DIGEST_SIZE, BUCKET_META_MAGIC); - uint8_t key[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx1, SHA256_DIGEST_SIZE, key); - - // Generate the synthetic iv with first half of hmac w/ bucket and filename - struct hmac_sha512_ctx ctx2; - hmac_sha512_set_key(&ctx2, SHA256_DIGEST_SIZE, bucket_key); - hmac_sha512_update(&ctx2, strlen(state->bucket_id), - (uint8_t *)state->bucket_id); - hmac_sha512_update(&ctx2, strlen(state->file_name), - (uint8_t *)state->file_name); - uint8_t filename_iv[SHA256_DIGEST_SIZE]; - hmac_sha512_digest(&ctx2, SHA256_DIGEST_SIZE, filename_iv); - - free(bucket_key); - - char *encrypted_file_name; - encrypt_meta(state->file_name, key, filename_iv, &encrypted_file_name); - - state->encrypted_file_name = encrypted_file_name; - uint8_t *index = NULL; char *key_as_str = NULL; diff --git a/src/utils.c b/src/utils.c index 6b3d389..f06887d 100755 --- a/src/utils.c +++ b/src/utils.c @@ -80,6 +80,54 @@ char *str_concat_many(int count, ...) return combined; } +char *str_replace(char *search, char *replace, char *subject) { + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + size_t len_search; // length of search (the string to remove) + size_t len_replace; // length of replace (the string to replace search with) + size_t len_front; // distance between search and end of last replace + int count; // number of replacements + + // sanity checks and initialization + if (!subject || !search) + return NULL; + len_search = strlen(search); + if (len_search == 0) + return NULL; // empty search causes infinite loop during count + if (!replace) + replace = ""; + len_replace = strlen(replace); + + // count the number of replacements needed + ins = subject; + tmp = strstr(ins, search); + for (count = 0; tmp != NULL; ++count) { + ins = tmp + len_search; + tmp = strstr(ins, search); + } + + tmp = result = malloc(strlen(subject) + (len_replace - len_search) * count + 1); + + if (!result) + return NULL; + + // first time through the loop, all the variable are set correctly + // from here on, + // tmp points to the end of the result string + // ins points to the next occurrence of search in subject + // orig points to the remainder of subject after "end of search" + while (count--) { + ins = strstr(subject, search); + len_front = ins - subject; + tmp = strncpy(tmp, subject, len_front) + len_front; + tmp = strcpy(tmp, replace) + len_replace; + subject += len_front + len_search; // move to next "end of search" + } + strcpy(tmp, subject); + return result; +} + void random_buffer(uint8_t *buf, size_t len) { static FILE *frand = NULL; diff --git a/src/utils.h b/src/utils.h index da1b2f6..a132393 100644 --- a/src/utils.h +++ b/src/utils.h @@ -48,6 +48,18 @@ uint8_t *str2hex(size_t length, char *data); char *str_concat_many(int count, ...); +/** + * @brief Replace all occurrences of the search string with the replacement string + * + * The result string from this function must be freed. + * + * @param[in] search The value being searched for, otherwise known as the needle + * @param[in] replace The replacement value that replaces found search values + * @param[in] subject The string being searched and replaced on, otherwise known as the haystack. + * @return A null value on error, otherwise a string with replaced values. + */ +char *str_replace(char *search, char *replace, char *subject); + void random_buffer(uint8_t *buf, size_t len); uint64_t shard_size(int hops); diff --git a/test/mockbridge.c b/test/mockbridge.c index 6f0631b..9a58319 100644 --- a/test/mockbridge.c +++ b/test/mockbridge.c @@ -90,7 +90,12 @@ int mock_bridge_server(void *cls, page = get_response_string(responses, "getbucket"); status_code = MHD_HTTP_OK; } - } else if (0 == strcmp(url, "/buckets/368be0816766b28fd5f43af5/files")) { + } else if (0 == strcmp(url, "/bucket-ids/g9qacwq2AE1+5nzL/HYyYdY9WoIr+1ueOuVEx6/IzzZKK9sULoKDDdYvhOpavHH2P3xQNw==")) { + if (check_auth(user, pass, &status_code, page)) { + page = get_response_string(responses, "getbucketid"); + status_code = MHD_HTTP_OK; + } + } else if (0 == strcmp(url, "/buckets/368be0816766b28fd5f43af5/files")) { if (check_auth(user, pass, &status_code, page)) { page = get_response_string(responses, "listfiles"); status_code = MHD_HTTP_OK; @@ -100,6 +105,11 @@ int mock_bridge_server(void *cls, page = get_response_string(responses, "getfileinfo"); status_code = MHD_HTTP_OK; } + } else if (0 == strcmp(url, "/buckets/368be0816766b28fd5f43af5/file-ids/QR6/qHizNFm+t+4vSvr575xVp2R3/rC/qFlmOH10HsdGQtofpUUqwEdaZUJI0jPIImYMZFu6cZDrMIvJtcK3pHAb+kkPv0Y5")) { + if (check_auth(user, pass, &status_code, page)) { + page = get_response_string(responses, "getfileid"); + status_code = MHD_HTTP_OK; + } } else if (0 == strcmp(url, "/buckets/368be0816766b28fd5f43af5/file-ids/hTY5wsqYyLJQppCMiFQI7v2n/IZZiKb0ES1RCrUqK7Fe5m0/+fYwh+E/vp8M3FCEECle63BhlWlHi/Hj/Yg5y/bIjy8SxQ==")) { if (check_auth(user, pass, &status_code, page)) { status_code = MHD_HTTP_NOT_FOUND; diff --git a/test/mockbridge.json b/test/mockbridge.json index d8db72b..bfdd916 100644 --- a/test/mockbridge.json +++ b/test/mockbridge.json @@ -23,6 +23,9 @@ "name": "g9qacwq2AE1+5nzL/HYyYdY9WoIr+1ueOuVEx6/IzzZKK9sULoKDDdYvhOpavHH2P3xQNw==", "id": "368be0816766b28fd5f43af5" }, + "getbucketid": { + "id": "368be0816766b28fd5f43af5" + }, "putbuckets": { "user": "user@sample.com", "encryptionKey": "", diff --git a/test/tests.c b/test/tests.c index dd90c53..d171780 100644 --- a/test/tests.c +++ b/test/tests.c @@ -99,6 +99,20 @@ void check_get_bucket(uv_work_t *work_req, int status) free(work_req); } +void check_get_bucket_id(uv_work_t *work_req, int status) +{ + assert(status == 0); + get_bucket_id_request_t *req = work_req->data; + assert(req->handle == NULL); + assert(strcmp(req->bucket_id, "368be0816766b28fd5f43af5") == 0); + + pass("storj_bridge_get_bucket_id"); + + json_object_put(req->response); + free(req); + free(work_req); +} + void check_get_buckets_badauth(uv_work_t *work_req, int status) { assert(status == 0); @@ -188,6 +202,20 @@ void check_list_files_badauth(uv_work_t *work_req, int status) free(work_req); } +void check_get_file_id(uv_work_t *work_req, int status) +{ + assert(status == 0); + get_file_id_request_t *req = work_req->data; + assert(req->handle == NULL); + assert(strcmp(req->file_id, "998960317b6725a3f8080c2b") == 0); + + pass("storj_bridge_get_file_id"); + + json_object_put(req->response); + free(req); + free(work_req); +} + void check_bucket_tokens(uv_work_t *work_req, int status) { assert(status == 0); @@ -768,6 +796,10 @@ int test_api() status = storj_bridge_get_bucket(env, bucket_id, NULL, check_get_bucket); assert(status == 0); + // get bucket id + status = storj_bridge_get_bucket_id(env, "test", NULL, check_get_bucket_id); + assert(status == 0); + // create a new bucket with a name status = storj_bridge_create_bucket(env, "backups", NULL, check_create_bucket); @@ -784,6 +816,11 @@ int test_api() check_list_files); assert(status == 0); + // get file id + status = storj_bridge_get_file_id(env, bucket_id, "storj-test-download.data", + NULL, check_get_file_id); + assert(status == 0); + // create bucket tokens status = storj_bridge_create_bucket_token(env, bucket_id, @@ -1542,6 +1579,34 @@ int test_memory_mapping() return 0; } +int test_str_replace() +{ + char *subject = "g9qacwq2AE1+5nzL/HYyYdY9WoIr+1ueOuVEx6/IzzZKK9sULoKDDdYvhOpavHH2P3xQNw=="; + + char *result = str_replace("/", "%2F", subject); + if (!result) { + fail("test_str_replace"); + return 0; + } + + char *expected = "g9qacwq2AE1+5nzL%2FHYyYdY9WoIr+1ueOuVEx6%2FIzzZKK9sULoKDDdYvhOpavHH2P3xQNw=="; + + int failed = 0; + if (strcmp(expected, result) != 0) { + failed = 1; + } + + if (failed) { + fail("test_str_replace"); + } else { + pass("test_str_replace"); + } + + free(result); + + return 0; +} + // Test Bridge Server struct MHD_Daemon *start_test_server() { @@ -1615,6 +1680,7 @@ int main(void) test_get_time_milliseconds(); test_determine_shard_size(); test_memory_mapping(); + test_str_replace(); int num_failed = tests_ran - test_status; printf(KGRN "\nPASSED: %i" RESET, test_status); From 51b3b6324e2c40dcf15bebad875f9fcb697941f6 Mon Sep 17 00:00:00 2001 From: kishore Date: Mon, 5 Feb 2018 11:23:59 -0500 Subject: [PATCH 61/68] fixes warnings and clean up of unused code in cli.c --- src/cli.c | 40 +++------------------ src/storjapi_callback.c | 79 +++++++++++++---------------------------- 2 files changed, 28 insertions(+), 91 deletions(-) diff --git a/src/cli.c b/src/cli.c index faaf657..59f20aa 100644 --- a/src/cli.c +++ b/src/cli.c @@ -34,31 +34,11 @@ typedef struct { char *key; } user_options_t; -typedef struct { - storj_env_t *env; - char *bucket_name; - char *bucket_id; - char *file_name; /**< next file ready to upload */ - char *file_path; /**< next file ready to upload */ - char *file_id; /**< file id of from the bridge */ - FILE *file_fd; /**< upload file list fd */ - int total_files; /**< total files to upload */ - int curr_up_file; /**< current file number in uploadinng */ - char *curr_cmd_req; /**< cli curr command requested */ - char *next_cmd_req; /**< cli next command requested */ - bool cmd_resp; /**< cli command response 0->fail; 1->success */ - bool file_xfer_stat; /**< false -> inprogress; true -> done */ - void *handle; -}cli_state_t; - #ifndef errno extern int errno; #endif static void printdir(char *dir, int depth, FILE *fd); -//static void queue_next_cli_cmd(cli_state_t *cli_state); -//static int cli_upload_file(char *path, char *bucket_name, cli_state_t *cli_state); -//static int cli_download_file(char *path, char *bucket_name, cli_state_t *cli_state); static const char *get_filename_separator(const char *file_path); static inline void noop() {}; @@ -1080,7 +1060,6 @@ int main(int argc, char **argv) char *user = NULL; char *pass = NULL; char *mnemonic = NULL; - cli_state_t *cli_state = NULL; storj_api_t *storj_api = NULL; if (strcmp(command, "get-info") == 0) { @@ -1278,23 +1257,13 @@ int main(int argc, char **argv) goto end_program; } - cli_state = malloc(sizeof(cli_state_t)); - - if (!cli_state) { - status = 1; - goto end_program; - } - memset(cli_state, 0x00, sizeof(cli_state_t)); - - cli_state->env = env; - storj_api = malloc(sizeof(storj_api_t)); - if (!cli_state) { + if (!storj_api) { status = 1; goto end_program; } - memset(storj_api, 0x00, sizeof(storj_api)); + memset(storj_api, 0x00, sizeof(*storj_api)); storj_api->env = env; @@ -1314,7 +1283,6 @@ int main(int argc, char **argv) if (strcmp(command, "download-file") == 0) { - /* get the corresponding bucket id from the bucket name */ storj_api->bucket_name = argv[command_index + 1]; storj_api->file_name = argv[command_index + 2]; @@ -1864,8 +1832,8 @@ int main(int argc, char **argv) if (mnemonic) { free(mnemonic); } - if(cli_state){ - free(cli_state); + if(storj_api){ + free(storj_api); } return status; diff --git a/src/storjapi_callback.c b/src/storjapi_callback.c index 3f50810..8aa650a 100644 --- a/src/storjapi_callback.c +++ b/src/storjapi_callback.c @@ -924,48 +924,32 @@ void queue_next_cmd_req(storj_api_t *storj_api) else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "remove-file-req") == 0x00)) { - if (storj_api->file_id != NULL) - { - printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", - __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, - storj_api->bucket_name); + printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", + __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, + storj_api->bucket_name); - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "remove-file-resp"; + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "remove-file-resp"; - storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api, delete_file_callback); - } - else - { - printf("\'%s\' file doesn't exists in \'%s\' bucket\n", - storj_api->file_name, storj_api->bucket_name); - } + storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api, delete_file_callback); } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "list-mirrors-req") == 0x00)) { - if (storj_api->file_id != NULL) - { - printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", - __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, - storj_api->bucket_name); - - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "list-mirrors-resp"; - - storj_bridge_list_mirrors(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api, list_mirrors_callback); - } - else - { - printf("\'%s\' file doesn't exists in \'%s\' bucket\n", - storj_api->file_name, storj_api->bucket_name); - } + printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", + __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, + storj_api->bucket_name); + + storj_api->curr_cmd_req = storj_api->next_cmd_req; + storj_api->next_cmd_req = storj_api->final_cmd_req; + storj_api->final_cmd_req = NULL; + storj_api->excp_cmd_resp = "list-mirrors-resp"; + + storj_bridge_list_mirrors(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api, list_mirrors_callback); } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "upload-file-req") == 0x00)) @@ -1042,16 +1026,8 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "download-file-resp"; - if (storj_api->file_id != NULL) - { - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api->dst_file, storj_api); - } - else - { - printf("[%s][%d] File not found!!!\n", __FUNCTION__, __LINE__); - exit(0); - } + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, + storj_api->dst_file, storj_api); } else if ((storj_api->next_cmd_req != NULL) && (strcmp(storj_api->next_cmd_req, "download-files-req") == 0x00)) @@ -1111,15 +1087,8 @@ void queue_next_cmd_req(storj_api_t *storj_api) strcat(temp_path, token[1]); fprintf(stdout,"*****[%d:%d] downloading file to: %s *****\n", storj_api->xfer_count, storj_api->total_files, temp_path); storj_api->xfer_count++; - if (storj_api->file_id != NULL) - { - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); - } - else - { - printf("[%s][%d] File not found!!!\n", __FUNCTION__, __LINE__); - exit(0); - } + + download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); } else { From 173b5da9d7039accac379e741a23d11a5bea4b82 Mon Sep 17 00:00:00 2001 From: kishore Date: Tue, 6 Feb 2018 13:21:53 -0500 Subject: [PATCH 62/68] Change merges from main libstorj --- README.md | 6 +++++- configure.ac | 2 +- src/Makefile.am | 4 ++-- src/cli.c | 2 +- src/storj.c | 3 ++- src/storj.h | 19 ++----------------- src/uploader.c | 5 ++++- 7 files changed, 17 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a49fbb0..e9c8064 100755 --- a/README.md +++ b/README.md @@ -66,7 +66,11 @@ Dependencies: brew install curl nettle json-c libuv ``` -### Cross Compiling Dependencies from Ubuntu 16.04 +------ + +## Cross Compiling Dependencies from Ubuntu 16.04 + +These notes are for cross compiling for various systems from a Debian based linux distribution, specifically Ubuntu 16.04. The mingw toolchain is used to build for Windows and clang and cctools for macOS builds. **Windows** diff --git a/configure.ac b/configure.ac index cd65768..cadf84d 100755 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([libstorj],[2.0.0-beta]) +AC_INIT([libstorj],[2.0.0-beta2]) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_MACRO_DIR([build-aux/m4]) AM_INIT_AUTOMAKE([foreign]) diff --git a/src/Makefile.am b/src/Makefile.am index 4301314..4c2694c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,14 +7,14 @@ libstorj_la_LIBADD = -lcurl -lnettle -ljson-c -luv -lm # - Increase the age value only if the changes made to the ABI are backward compatible. # https://autotools.io/libtool/version.html # The order for version-info is :: -libstorj_la_LDFLAGS = -Wall -version-info 1:1:0 +libstorj_la_LDFLAGS = -Wall -version-info 2:2:0 if BUILD_STORJ_DLL libstorj_la_LDFLAGS += -no-undefined endif include_HEADERS = storj.h bin_PROGRAMS = storj -storj_SOURCES = cli.c storj.h +storj_SOURCES = cli.c storj.h storj_LDADD = libstorj.la if BUILD_STORJ_DLL storj_LDFLAGS = -Wall diff --git a/src/cli.c b/src/cli.c index 59f20aa..939f650 100644 --- a/src/cli.c +++ b/src/cli.c @@ -96,7 +96,7 @@ static inline void noop() {}; " STORJ_ENCRYPTION_KEY file encryption key\n\n" -#define CLI_VERSION "libstorj-2.0.1-beta" +#define CLI_VERSION "libstorj-2.0.0-beta2" static int check_file_path(char *file_path) { diff --git a/src/storj.c b/src/storj.c index 8b530c6..77464a1 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1774,11 +1774,12 @@ STORJ_API int storj_list_buckets(storj_api_t *storj_api) STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) { + int ret = -1; char *bucket_name = storj_api->bucket_name; if (!bucket_name) { printf("Missing : \n"); - return STORJAPI_BUCKET_NAME_MISSING_ERROR; + return ret; } storj_api->last_cmd_req = NULL; diff --git a/src/storj.h b/src/storj.h index 9d48787..375d542 100755 --- a/src/storj.h +++ b/src/storj.h @@ -101,9 +101,6 @@ extern "C" { // Miscellaneous errors #define STORJ_HEX_DECODE_ERROR 7000 -// Storj API related errors -#define STORJAPI_BUCKET_NAME_MISSING_ERROR 8000 - // Exchange report codes #define STORJ_REPORT_SUCCESS 1000 #define STORJ_REPORT_FAILURE 1100 @@ -593,18 +590,6 @@ typedef struct { int pending_work_count; } storj_upload_state_t; -/** - * @brief A Structure for management information base (MIB) for - * Storj API. - */ -typedef struct storj_api_mib { - int xfer_count; /**< # of files xferred (up/down) */ - int total_files; /**< total files to xfer */ - int success_xfer_count; /**< files xferred successfully */ - int fail_xfer_count; /**< files xferred successfully */ - void *handle; -} storj_api_mib_t; - /** * @brief A Structure for passing the User's Application info to * Storj API. @@ -850,7 +835,7 @@ STORJ_API int storj_bridge_delete_bucket(storj_env_t *env, * @brief Get a info of specific bucket. * * @param[in] env The storj environment struct - * @param[in] bucket_id The bucket id + * @param[in] id The bucket id * @param[in] handle A pointer that will be available in the callback * @param[in] cb A function called with response when complete * @return A non-zero error value on failure and 0 on success. @@ -1007,7 +992,7 @@ STORJ_API int storj_bridge_delete_frame(storj_env_t *env, * * @param[in] env The storj environment struct * @param[in] bucket_id The bucket id - * @param[in] file_id The bucket id + * @param[in] file_id The file id * @param[in] handle A pointer that will be available in the callback * @param[in] cb A function called with response when complete * @return A non-zero error value on failure and 0 on success. diff --git a/src/uploader.c b/src/uploader.c index e4d0b13..218a748 100644 --- a/src/uploader.c +++ b/src/uploader.c @@ -256,6 +256,10 @@ static void cleanup_state(storj_upload_state_t *state) free(state->exclude); } + if (state->index) { + free((char *)state->index); + } + if (state->encryption_ctr) { free(state->encryption_ctr); } @@ -2722,7 +2726,6 @@ STORJ_API void storj_free_uploaded_file_info(storj_file_meta_t *file) free((char *)file->created); free((char *)file->mimetype); free((char *)file->hmac); - free((char *)file->index); } free(file); } From 833cb41356fad9a09eb4a39b91c8061a6f0f6071 Mon Sep 17 00:00:00 2001 From: kishore Date: Tue, 6 Feb 2018 13:53:10 -0500 Subject: [PATCH 63/68] fixes the single file upload without the -r option causes it to upload same file twice --- src/cli.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cli.c b/src/cli.c index 939f650..eca6826 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1580,7 +1580,10 @@ int main(int argc, char **argv) /* create the file and add the list of files to be uploaded */ if ((file = fopen(storj_api->src_list, "w")) != NULL) { - fprintf(file, "%s\n", local_file_path); + if ((strcmp(argv[1],"-r") == 0x00) || (strcmp(argv[1],"-R") == 0x00)) + { + fprintf(file, "%s\n", local_file_path); + } for (int i = 0x01; i < ((argc - command_index) - 1); i++) { From d511a3898b3bc29d6c00fe3004b3762f49973c85 Mon Sep 17 00:00:00 2001 From: kishore Date: Thu, 8 Feb 2018 11:57:29 -0500 Subject: [PATCH 64/68] renamed the file from storjapi_callback.c/h to cli_callback.c/h --- src/Makefile.am | 2 +- src/cli.c | 4 ++-- src/{storjapi_callback.c => cli_callback.c} | 2 +- src/{storjapi_callback.h => cli_callback.h} | 6 +++--- src/storj.c | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) rename src/{storjapi_callback.c => cli_callback.c} (99%) rename src/{storjapi_callback.h => cli_callback.h} (91%) diff --git a/src/Makefile.am b/src/Makefile.am index 4c2694c..886e72e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ lib_LTLIBRARIES = libstorj.la -libstorj_la_SOURCES = storj.c utils.c utils.h http.c http.h uploader.c uploader.h downloader.c downloader.h bip39.c bip39.h bip39_english.h crypto.c crypto.h rs.c rs.h storjapi_callback.c storjapi_callback.h +libstorj_la_SOURCES = storj.c utils.c utils.h http.c http.h uploader.c uploader.h downloader.c downloader.h bip39.c bip39.h bip39_english.h crypto.c crypto.h rs.c rs.h cli_callback.c cli_callback.h libstorj_la_LIBADD = -lcurl -lnettle -ljson-c -luv -lm # The rules of thumb, when dealing with these values are: # - Always increase the revision value. diff --git a/src/cli.c b/src/cli.c index 5cd97f2..c19a9be 100644 --- a/src/cli.c +++ b/src/cli.c @@ -15,7 +15,7 @@ #endif #include "storj.h" -#include "storjapi_callback.c" +#include "cli_callback.c" //#define debug_enable @@ -177,7 +177,7 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) int ret = strpos(cmd_str, sub_str); if( ret == -1) { - printf("Invalid Command Entry (%d), \ntry ... stroj:///\n", ret); + printf("Invalid Command Entry (%d), \ntry ... storj:///\n", ret); } if (ret == 0x00) diff --git a/src/storjapi_callback.c b/src/cli_callback.c similarity index 99% rename from src/storjapi_callback.c rename to src/cli_callback.c index 950549d..a9c52b4 100644 --- a/src/storjapi_callback.c +++ b/src/cli_callback.c @@ -1,4 +1,4 @@ -#include "storjapi_callback.h" +#include "cli_callback.h" //#define debug_enable diff --git a/src/storjapi_callback.h b/src/cli_callback.h similarity index 91% rename from src/storjapi_callback.h rename to src/cli_callback.h index a577a33..ca02577 100755 --- a/src/storjapi_callback.h +++ b/src/cli_callback.h @@ -6,8 +6,8 @@ * end user's application */ -#ifndef STORJAPI_CALLBACK_H -#define STORJAPI_CALLBACK_H +#ifndef CLI_CALLBACK_H +#define CLI_CALLBACK_H #ifdef __cplusplus extern "C" { @@ -41,4 +41,4 @@ void queue_next_cmd_req(storj_api_t *storj_api); } #endif -#endif /* STORJ_H */ +#endif /* CLI_CALLBACK_H */ diff --git a/src/storj.c b/src/storj.c index 77464a1..f31808f 100644 --- a/src/storj.c +++ b/src/storj.c @@ -2,7 +2,7 @@ #include "http.h" #include "utils.h" #include "crypto.h" -#include "storjapi_callback.h" +#include "cli_callback.h" static inline void noop() {}; From cc9c1de1bfb91cbcc0956219dbeff3cb95f0b79d Mon Sep 17 00:00:00 2001 From: kishore Date: Thu, 8 Feb 2018 12:53:19 -0500 Subject: [PATCH 65/68] Has changes per the review comments --- src/cli.c | 8 +- src/cli_callback.c | 248 ++++++++++++++------------------------------- src/storj.c | 10 +- src/storj.h | 2 +- 4 files changed, 85 insertions(+), 183 deletions(-) diff --git a/src/cli.c b/src/cli.c index c19a9be..dda29e0 100644 --- a/src/cli.c +++ b/src/cli.c @@ -21,12 +21,6 @@ #define STORJ_THREADPOOL_SIZE "64" -#define CLI_NO_SUCH_FILE_OR_DIR 0x00 -#define CLI_VALID_REGULAR_FILE 0x01 -#define CLI_VALID_DIR 0x02 -#define CLI_UNKNOWN_FILE_ATTR 0x03 -#define CLI_UPLOAD_FILE_LOG_ERR 0x04 - typedef struct { char *user; char *pass; @@ -1547,7 +1541,7 @@ int main(int argc, char **argv) } else if (strcmp(command, "get-bucket-id") == 0) { storj_api->bucket_name = argv[command_index + 1]; - storj_get_bucket_id(storj_api); + cli_get_bucket_id(storj_api); } else if (strcmp(command, "lm") == 0) { storj_api->bucket_name = argv[command_index + 1]; diff --git a/src/cli_callback.c b/src/cli_callback.c index a9c52b4..4ae13df 100644 --- a/src/cli_callback.c +++ b/src/cli_callback.c @@ -71,8 +71,7 @@ static void printdir(char *dir, int depth, FILE *src_fd, void *handle) char * start; storj_api_t *storj_api = handle; - if((dp = opendir(dir)) == NULL) - { + if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); return; } @@ -81,17 +80,14 @@ static void printdir(char *dir, int depth, FILE *src_fd, void *handle) while((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); - if (S_ISDIR(statbuf.st_mode)) - { + if (S_ISDIR(statbuf.st_mode)) { /* Found a directory, but ignore . and .. */ if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) continue; /* Recurse at a new indent level */ printdir(entry->d_name, depth + 1, src_fd, handle); - } - else - { + } else { full_path = realpath(entry->d_name, NULL); /* write to src file */ fprintf(src_fd, "%s%s\n", "", full_path); @@ -110,8 +106,7 @@ static int file_exists(void *handle) FILE *src_fd, *dst_fd; - if (stat(storj_api->file_path, &sb) == -1) - { + if (stat(storj_api->file_path, &sb) == -1) { perror("stat"); return CLI_NO_SUCH_FILE_OR_DIR; } @@ -125,8 +120,7 @@ static int file_exists(void *handle) printf("character device\n"); break; case S_IFDIR: - if((src_fd = fopen(storj_api->src_list, "w")) == NULL) - { + if((src_fd = fopen(storj_api->src_list, "w")) == NULL) { return CLI_UPLOAD_FILE_LOG_ERR; } printdir(storj_api->file_path, 0, src_fd, handle); @@ -197,8 +191,7 @@ static void file_progress(double progress, { int bar_width = 70; - if (progress == 0 && downloaded_bytes == 0) - { + if (progress == 0 && downloaded_bytes == 0) { printf("Preparing File..."); fflush(stdout); return; @@ -208,16 +201,12 @@ static void file_progress(double progress, int pos = bar_width * progress; for (int i = 0; i < bar_width; ++i) { - if (i < pos) - { + if (i < pos) { printf("="); } - else if (i == pos) - { + else if (i == pos) { printf(">"); - } - else - { + } else { printf(" "); } } @@ -267,15 +256,11 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, const char *file_name = get_filename_separator(file_path); - if (storj_api->dst_file == NULL) - { - if (!file_name) - { + if (storj_api->dst_file == NULL) { + if (!file_name) { file_name = file_path; } - } - else - { + } else { file_name = storj_api->dst_file; } @@ -330,13 +315,10 @@ static void upload_files_complete(int status, storj_file_meta_t *file, void *han storj_api->rcvd_cmd_resp = "upload-files-resp"; printf("\n"); - if (status != 0) - { + if (status != 0) { printf("[%s][%d]Upload failure: %s\n", __FUNCTION__, __LINE__, storj_strerror(status)); - } - else - { + } else { printf("Upload Success! File ID: %s\n", file->id); storj_free_uploaded_file_info(file); } @@ -350,8 +332,7 @@ static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path FILE *fd = fopen(file_path, "r"); - if (!fd) - { + if (!fd) { printf("[%s][%d]Invalid file : %s\n", __FUNCTION__, __LINE__, file_path); exit(0); } @@ -371,8 +352,7 @@ static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path const char *file_name = get_filename_separator(storj_api->dst_file); - if (!file_name) - { + if (!file_name) { file_name = file_path; } printf(" %s\n", file_name); @@ -435,14 +415,10 @@ static void verify_upload_files(void *handle) char *upload_list_file = pwd_path; /* create upload files list based on the file path */ - if ((upload_list_file = getenv("TMPDIR")) != NULL) - { - if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') - { + if ((upload_list_file = getenv("TMPDIR")) != NULL) { + if (upload_list_file[(strlen(upload_list_file) - 1)] == '/') { strcat(upload_list_file, "STORJ_output_list.txt"); - } - else - { + } else { strcat(upload_list_file, "/STORJ_output_list.txt"); } @@ -459,14 +435,11 @@ static void verify_upload_files(void *handle) /* create a upload list file src_list.txt */ storj_api->src_fd = fopen(storj_api->src_list, "r"); - if (!storj_api->src_fd) - { + if (!storj_api->src_fd) { printf("[%s][%d]Invalid file path: %s\n", __FUNCTION__, __LINE__, storj_api->src_list); exit(0); - } - else - { + } else { /* count total src_list files */ char line[MAX_UPLOAD_FILES][256]; char *temp; @@ -477,12 +450,9 @@ static void verify_upload_files(void *handle) /* read a line from a file */ while (fgets(line[i], sizeof(line), storj_api->src_fd) != NULL) { - if (i <= MAX_UPLOAD_FILES) - { + if (i <= MAX_UPLOAD_FILES) { i++; - } - else - { + } else { i = (i - 1); printf("[%s][%d] Upload files limit set to %d \n", __FUNCTION__, __LINE__, (MAX_UPLOAD_FILES)); @@ -519,9 +489,7 @@ static void download_file_complete(int status, FILE *fd, void *handle) printf("[%s][%d]Download failure: %s\n", __FUNCTION__, __LINE__, storj_strerror(status)); } - } - else - { + } else { printf("Download Success!\n"); } @@ -558,6 +526,9 @@ static int download_file(storj_env_t *env, char *bucket_id, if (strcmp(user_input, "n") == 0) { printf("\nCanceled overwriting of [%s].\n", path); + storj_api_t *storj_api = handle; + storj_api->rcvd_cmd_resp = "download-file-resp"; + queue_next_cmd_req(storj_api); return 1; } @@ -756,28 +727,20 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; - if (req->status_code == 401) - { + if (req->status_code == 401) { printf("Invalid user credentials.\n"); goto cleanup; - } - else if (req->status_code != 200 && req->status_code != 304) - { + } else if (req->status_code != 200 && req->status_code != 304) { printf("Request failed with status code: %i\n", req->status_code); goto cleanup; - } - else if (req->total_buckets == 0) - { + } else if (req->total_buckets == 0) { printf("No buckets.\n"); goto cleanup; } - for (int i = 0; i < req->total_buckets; i++) - { + for (int i = 0; i < req->total_buckets; i++) { storj_bucket_meta_t *bucket = &req->buckets[i]; - - if (strcmp(storj_api->bucket_name, bucket->name) == 0x00) - { + if (strcmp(storj_api->bucket_name, bucket->name) == 0x00) { printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", bucket->id, bucket->decrypted ? "true" : "false", bucket->created, bucket->name); @@ -786,11 +749,8 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) memset(storj_api->bucket_id, 0x00, sizeof(storj_api->bucket_id)); strcpy(storj_api->bucket_id, (char *)bucket->id); break; - } - else - { - if (i >= (req->total_buckets - 1)) - { + } else { + if (i >= (req->total_buckets - 1)) { printf("Invalid bucket name. \n"); goto cleanup; } @@ -815,48 +775,37 @@ void list_files_callback(uv_work_t *work_req, int status) storj_api->last_cmd_req = storj_api->curr_cmd_req; storj_api->rcvd_cmd_resp = "list-files-resp"; - if (req->status_code == 404) - { + if (req->status_code == 404) { printf("Bucket id [%s] does not exist\n", req->bucket_id); goto cleanup; - } - else if (req->status_code == 400) - { + } else if (req->status_code == 400) { printf("Bucket id [%s] is invalid\n", req->bucket_id); goto cleanup; - } - else if (req->status_code == 401) - { + } else if (req->status_code == 401) { printf("Invalid user credentials.\n"); goto cleanup; - } - else if (req->status_code != 200) - { + } else if (req->status_code != 200) { printf("Request failed with status code: %i\n", req->status_code); } - if (req->total_files == 0) - { + if (req->total_files == 0) { printf("No files for bucket.\n"); goto cleanup; } FILE *dwnld_list_fd = stdout; - if ((dwnld_list_fd = fopen("/tmp/dwnld_list.txt", "w")) == NULL) - { + if ((dwnld_list_fd = fopen("/tmp/dwnld_list.txt", "w")) == NULL) { printf("[%s][%d] Unable to create download list file\n", __FUNCTION__, __LINE__); goto cleanup; } - for (int i = 0; i < req->total_files; i++) - { + for (int i = 0; i < req->total_files; i++) { storj_file_meta_t *file = &req->files[i]; if ((storj_api->file_name != NULL) && - (strcmp(storj_api->file_name, file->filename)) == 0x00) - { + (strcmp(storj_api->file_name, file->filename)) == 0x00) { /* store the file id */ memset(storj_api->file_id, 0x00, sizeof(storj_api->file_id)); strcpy(storj_api->file_id, (char *)file->id); @@ -897,13 +846,10 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->curr_cmd_req, storj_api->next_cmd_req); #endif - if(storj_api->excp_cmd_resp != NULL) - { - if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) - { + if(storj_api->excp_cmd_resp != NULL) { + if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) { if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) - { + (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; @@ -911,10 +857,8 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_bridge_list_files(storj_api->env, storj_api->bucket_id, storj_api, list_files_callback); - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "remove-bucket-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "remove-bucket-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; @@ -922,10 +866,8 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_bridge_delete_bucket(storj_api->env, storj_api->bucket_id, storj_api, delete_bucket_callback); - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "remove-file-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "remove-file-req") == 0x00)) { printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, storj_api->bucket_name); @@ -937,10 +879,8 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, storj_api, delete_file_callback); - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "list-mirrors-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "list-mirrors-req") == 0x00)) { printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, storj_api->bucket_name); @@ -952,30 +892,24 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_bridge_list_mirrors(storj_api->env, storj_api->bucket_id, storj_api->file_id, storj_api, list_mirrors_callback); - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "upload-file-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "upload-file-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "upload-file-resp"; upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_name, storj_api); - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "verify-upload-files-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "verify-upload-files-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; storj_api->excp_cmd_resp = "verify-upload-files-resp"; verify_upload_files(storj_api); - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "upload-files-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "upload-files-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->excp_cmd_resp = "upload-files-resp"; @@ -986,43 +920,34 @@ void queue_next_cmd_req(storj_api_t *storj_api) int i = 0x00; memset(line, 0x00, sizeof(line)); - if (file != NULL) - { - while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ - { + if (file != NULL) { + while((fgets(line[i],sizeof(line), file)!= NULL)) {/* read a line from a file */ temp = strrchr(line[i], '\n'); if(temp) *temp = '\0'; storj_api->src_file = line[i]; i++; - if(i >= storj_api->xfer_count) - { + if(i >= storj_api->xfer_count) { break; } } } fclose(file); - if (storj_api->xfer_count <= storj_api->total_files) - { + if (storj_api->xfer_count <= storj_api->total_files) { /* is it the last file ? */ - if(storj_api->xfer_count == storj_api->total_files) - { + if(storj_api->xfer_count == storj_api->total_files) { storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; } upload_files(storj_api->env, storj_api->bucket_id, storj_api->src_file, storj_api); storj_api->xfer_count++; - } - else - { + } else { printf("[%s][%d] Invalid xfer counts\n", __FUNCTION__, __LINE__); exit(0); } - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "download-file-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "download-file-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; @@ -1030,17 +955,14 @@ void queue_next_cmd_req(storj_api_t *storj_api) download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, storj_api->dst_file, storj_api); - } - else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "download-files-req") == 0x00)) - { + } else if ((storj_api->next_cmd_req != NULL) && + (strcmp(storj_api->next_cmd_req, "download-files-req") == 0x00)) { storj_api->curr_cmd_req = storj_api->next_cmd_req; storj_api->excp_cmd_resp = "download-file-resp"; FILE *file = stdout; - if ((file = fopen("/tmp/dwnld_list.txt", "r")) != NULL) - { + if ((file = fopen("/tmp/dwnld_list.txt", "r")) != NULL) { char line[256][256]; char *temp; char temp_path[1024]; @@ -1050,13 +972,11 @@ void queue_next_cmd_req(storj_api_t *storj_api) memset(token, 0x00, sizeof(token)); memset(temp_path, 0x00, sizeof(temp_path)); memset(line, 0x00, sizeof(line)); - while((fgets(line[i],sizeof(line), file)!= NULL)) /* read a line from a file */ - { + while((fgets(line[i],sizeof(line), file)!= NULL)) {/* read a line from a file */ temp = strrchr(line[i], '\n'); if(temp) *temp = '\0'; i++; - if (i >= storj_api->xfer_count) - { + if (i >= storj_api->xfer_count) { break; } } @@ -1064,17 +984,14 @@ void queue_next_cmd_req(storj_api_t *storj_api) /* start tokenizing */ token[0] = strtok(line[i-1], ":"); - while (token[tk_idx] != NULL) - { + while (token[tk_idx] != NULL) { tk_idx++; token[tk_idx] = strtok(NULL, ":"); } - if(storj_api->xfer_count <= storj_api->total_files) - { + if(storj_api->xfer_count <= storj_api->total_files) { /* is it the last file ? */ - if(storj_api->xfer_count == storj_api->total_files) - { + if(storj_api->xfer_count == storj_api->total_files) { storj_api->next_cmd_req = storj_api->final_cmd_req; storj_api->final_cmd_req = NULL; } @@ -1082,8 +999,7 @@ void queue_next_cmd_req(storj_api_t *storj_api) memset(storj_api->file_id, 0x00, sizeof(storj_api->file_id)); strcpy(storj_api->file_id, token[0]); strcpy(temp_path, storj_api->file_path); - if (storj_api->file_path[(strlen(storj_api->file_path)-1)] != '/') - { + if (storj_api->file_path[(strlen(storj_api->file_path)-1)] != '/') { strcat(temp_path, "/"); } strcat(temp_path, token[1]); @@ -1091,25 +1007,19 @@ void queue_next_cmd_req(storj_api_t *storj_api) storj_api->xfer_count++; download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); - } - else - { + } else { printf("[%s][%d] Invalid xfer counts\n", __FUNCTION__, __LINE__); exit(0); } } - } - else - { + } else { #ifdef debug_enable printf("[%s][%d] **** ALL CLEAN & DONE *****\n", __FUNCTION__, __LINE__); #endif exit(0); } - } - else - { + } else { printf("[%s][%d]Oops !!!! expt resp = %s; rcvd resp = %s \n", __FUNCTION__, __LINE__, storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); @@ -1117,9 +1027,7 @@ void queue_next_cmd_req(storj_api_t *storj_api) __FUNCTION__, __LINE__, storj_api->last_cmd_req, storj_api->curr_cmd_req, storj_api->next_cmd_req); } - } - else - { + } else { /* calling straight storj calls without going thru the state machine */ exit(0); } diff --git a/src/storj.c b/src/storj.c index f31808f..3d7c273 100644 --- a/src/storj.c +++ b/src/storj.c @@ -1772,7 +1772,7 @@ STORJ_API int storj_list_buckets(storj_api_t *storj_api) return storj_bridge_get_buckets(storj_api->env, storj_api, get_buckets_callback); } -STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) +STORJ_API int cli_get_bucket_id(storj_api_t *storj_api) { int ret = -1; char *bucket_name = storj_api->bucket_name; @@ -1795,7 +1795,7 @@ STORJ_API int storj_get_bucket_id(storj_api_t *storj_api) STORJ_API int storj_list_files(storj_api_t *storj_api) { int ret = 0x00; - ret = storj_get_bucket_id(storj_api); + ret = cli_get_bucket_id(storj_api); storj_api->next_cmd_req = "list-files-req"; storj_api->final_cmd_req = NULL; @@ -1805,7 +1805,7 @@ STORJ_API int storj_list_files(storj_api_t *storj_api) STORJ_API int storj_remove_bucket(storj_api_t *storj_api) { int ret = 0x00; - ret = storj_get_bucket_id(storj_api); + ret = cli_get_bucket_id(storj_api); storj_api->next_cmd_req = "remove-bucket-req"; storj_api->final_cmd_req = NULL; @@ -1833,7 +1833,7 @@ STORJ_API int storj_list_mirrors(storj_api_t *storj_api) STORJ_API int storj_upload_file(storj_api_t *storj_api) { int ret = 0x00; - ret = storj_get_bucket_id(storj_api); + ret = cli_get_bucket_id(storj_api); storj_api->next_cmd_req = "upload-file-req"; storj_api->final_cmd_req = NULL; @@ -1843,7 +1843,7 @@ STORJ_API int storj_upload_file(storj_api_t *storj_api) STORJ_API int storj_upload_files(storj_api_t *storj_api) { int ret = 0x00; - ret = storj_get_bucket_id(storj_api); + ret = cli_get_bucket_id(storj_api); storj_api->next_cmd_req = "verify-upload-files-req"; storj_api->final_cmd_req = "upload-files-req"; diff --git a/src/storj.h b/src/storj.h index 375d542..642f996 100755 --- a/src/storj.h +++ b/src/storj.h @@ -1136,7 +1136,7 @@ STORJ_API int storj_list_buckets(storj_api_t *storj_api); * info * @return A non-zero error value on failure and 0 on success. */ -STORJ_API int storj_get_bucket_id(storj_api_t *storj_api); +STORJ_API int cli_get_bucket_id(storj_api_t *storj_api); /** * @brief Function to list files in a given bucket name From 7654a8f8e7f7328befb170698378e33edd98a285 Mon Sep 17 00:00:00 2001 From: kishore Date: Mon, 12 Feb 2018 12:09:08 -0500 Subject: [PATCH 66/68] 1. moved the cli api files to callback.c; 2. Used the get bucket id storj api call from storj.c instead of reading bucket list and doing a string compare; 3. moved the cli_api_t structure to cli_callback.h and renamed the structure to cli_api_t --- src/cli.c | 138 ++++++------- src/cli_callback.c | 479 ++++++++++++++++++++++++++------------------- src/cli_callback.h | 137 ++++++++++++- src/storj.c | 113 +---------- src/storj.h | 125 ------------ 5 files changed, 489 insertions(+), 503 deletions(-) diff --git a/src/cli.c b/src/cli.c index dda29e0..39171eb 100644 --- a/src/cli.c +++ b/src/cli.c @@ -883,7 +883,7 @@ int main(int argc, char **argv) char *user = NULL; char *pass = NULL; char *mnemonic = NULL; - storj_api_t *storj_api = NULL; + cli_api_t *cli_api = NULL; if (strcmp(command, "get-info") == 0) { printf("Storj bridge: %s\n\n", storj_bridge); @@ -1080,15 +1080,15 @@ int main(int argc, char **argv) goto end_program; } - storj_api = malloc(sizeof(storj_api_t)); + cli_api = malloc(sizeof(cli_api_t)); - if (!storj_api) { + if (!cli_api) { status = 1; goto end_program; } - memset(storj_api, 0x00, sizeof(*storj_api)); + memset(cli_api, 0x00, sizeof(*cli_api)); - storj_api->env = env; + cli_api->env = env; #ifdef debug_enable printf("command = %s; command_index = %d\n", command, command_index); @@ -1115,11 +1115,11 @@ int main(int argc, char **argv) goto end_program; } - memcpy(storj_api->bucket_id, bucket_id, strlen(bucket_id)); - memcpy(storj_api->file_id, file_id, strlen(file_id)); - storj_api->dst_file = path; + memcpy(cli_api->bucket_id, bucket_id, strlen(bucket_id)); + memcpy(cli_api->file_id, file_id, strlen(file_id)); + cli_api->dst_file = path; - if (download_file(env, bucket_id, file_id, path, storj_api)) { + if (download_file(env, bucket_id, file_id, path, cli_api)) { status = 1; goto end_program; } @@ -1133,10 +1133,10 @@ int main(int argc, char **argv) goto end_program; } - memcpy(storj_api->bucket_id, bucket_id, strlen(bucket_id)); - storj_api->dst_file = path; + memcpy(cli_api->bucket_id, bucket_id, strlen(bucket_id)); + cli_api->dst_file = path; - if (upload_file(env, bucket_id, path, storj_api)) { + if (upload_file(env, bucket_id, path, cli_api)) { status = 1; goto end_program; } @@ -1149,7 +1149,7 @@ int main(int argc, char **argv) goto end_program; } - storj_bridge_list_files(env, bucket_id, storj_api, list_files_callback); + storj_bridge_list_files(env, bucket_id, cli_api, list_files_callback); } else if ((strcmp(command, "add-bucket") == 0) || (strcmp(command, "mkbkt") == 0x00)) { char *bucket_name = argv[command_index + 1]; @@ -1171,7 +1171,7 @@ int main(int argc, char **argv) goto end_program; } - storj_bridge_delete_bucket(env, bucket_id, storj_api, + storj_bridge_delete_bucket(env, bucket_id, cli_api, delete_bucket_callback); } else if (strcmp(command, "remove-file") == 0) { @@ -1184,7 +1184,7 @@ int main(int argc, char **argv) goto end_program; } storj_bridge_delete_file(env, bucket_id, file_id, - storj_api, delete_file_callback); + cli_api, delete_file_callback); } else if (strcmp(command, "list-buckets") == 0) { storj_bridge_get_buckets(env, NULL, get_buckets_callback); @@ -1198,7 +1198,7 @@ int main(int argc, char **argv) goto end_program; } storj_bridge_list_mirrors(env, bucket_id, file_id, - storj_api, list_mirrors_callback); + cli_api, list_mirrors_callback); } else if (strcmp(command, "cp") == 0) { #define UPLOAD_CMD 0x00 #define DOWNLOAD_CMD 0x01 @@ -1289,18 +1289,18 @@ int main(int argc, char **argv) int num_of_tokens = validate_cmd_tokenize(bucket_name, token); /* set the bucket name from which file(s) to be downloaded */ - storj_api->bucket_name = token[1]; + cli_api->bucket_name = token[1]; /* initialize the local folder to copy the downloaded file into */ - storj_api->file_path = local_file_path; + cli_api->file_path = local_file_path; /* initialize the filename to be downloaded as */ - storj_api->dst_file = dst_file_name; + cli_api->dst_file = dst_file_name; if ((argc == 0x04) || (argc == 0x05)) { /* handle non recursive operations */ if ((token[2] == NULL) || (strcmp(token[2], "*") == 0x00)) { if (check_file_path(local_file_path) == CLI_VALID_DIR) { - storj_download_files(storj_api); + cli_download_files(cli_api); } else { printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", __FUNCTION__, __LINE__, local_file_path); @@ -1308,24 +1308,24 @@ int main(int argc, char **argv) } } else if (token[2] != NULL) { /* set the file to be downloaded */ - storj_api->file_name = token[2]; + cli_api->file_name = token[2]; if ((check_file_path(local_file_path) == CLI_VALID_DIR) || (strcmp(dst_file_name, ".") == 0x00)) { memset(temp_buff, 0x00, sizeof(temp_buff)); strcat(temp_buff, local_file_path); strcat(temp_buff, "/"); strcat(temp_buff, token[2]); - storj_api->dst_file = temp_buff; + cli_api->dst_file = temp_buff; } else if (strlen(dst_file_name) > 0x00) { - storj_api->dst_file = local_file_path; + cli_api->dst_file = local_file_path; } else { printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", __FUNCTION__, __LINE__, local_file_path); goto end_program; } printf("[%s][%d]file %s downloaded from bucketname = %s as file %s\n", - __FUNCTION__, __LINE__, storj_api->file_name, storj_api->bucket_name, storj_api->dst_file); - storj_download_file(storj_api); + __FUNCTION__, __LINE__, cli_api->file_name, cli_api->bucket_name, cli_api->dst_file); + cli_download_file(cli_api); } else { printf("[%s][%d] Invalid '%s' dst directory !!!!!\n", __FUNCTION__, __LINE__, local_file_path); @@ -1356,7 +1356,7 @@ int main(int argc, char **argv) /* Handle the src argument */ /* single file to copy, make sure the files exits */ if ((argc == 0x05) && (check_file_path(local_file_path) == CLI_VALID_REGULAR_FILE)) { - storj_api->file_name = local_file_path; + cli_api->file_name = local_file_path; /* Handle the dst argument (storj:/// */ /* token[0]-> storj:; token[1]->bucket_name; token[2]->upload_file_name */ @@ -1367,19 +1367,19 @@ int main(int argc, char **argv) if ((num_of_tokens == 0x02) || (num_of_tokens == 0x03)) { char *dst_file_name = NULL; - storj_api->bucket_name = token[1]; + cli_api->bucket_name = token[1]; dst_file_name = (char *)get_filename_separator(local_file_path); if ((token[2] == NULL) || (strcmp(dst_file_name, token[2]) == 0x00) || (strcmp(token[2], ".") == 0x00)) { /* use the src list buff as temp memory to hold the dst filename */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - strcpy(storj_api->src_list, dst_file_name); - storj_api->dst_file = storj_api->src_list; + memset(cli_api->src_list, 0x00, sizeof(cli_api->src_list)); + strcpy(cli_api->src_list, dst_file_name); + cli_api->dst_file = cli_api->src_list; } else { - storj_api->dst_file = token[2]; + cli_api->dst_file = token[2]; } - storj_upload_file(storj_api); + cli_upload_file(cli_api); } else { printf("[%s][%d] Valid dst filename missing !!!!!\n", __FUNCTION__, __LINE__); goto end_program; @@ -1387,7 +1387,7 @@ int main(int argc, char **argv) } else { /* directory is being used, store it in file_path */ - storj_api->file_path = local_file_path; + cli_api->file_path = local_file_path; /* Handle wild character options for files selection */ if(check_file_path(local_file_path) != CLI_VALID_DIR) { @@ -1404,9 +1404,9 @@ int main(int argc, char **argv) } /* check the directory and create the path to upload list file */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); - storj_api->dst_file = storj_api->src_list; + memset(cli_api->src_list, 0x00, sizeof(cli_api->src_list)); + memcpy(cli_api->src_list, upload_list_file, sizeof(pwd_path)); + cli_api->dst_file = cli_api->src_list; } else { printf("[%s][%d] Upload list file generation error!!! \n", __FUNCTION__, __LINE__); @@ -1422,7 +1422,7 @@ int main(int argc, char **argv) FILE *file = NULL; /* create the file and add the list of files to be uploaded */ - if ((file = fopen(storj_api->src_list, "w")) != NULL) { + if ((file = fopen(cli_api->src_list, "w")) != NULL) { if ((strcmp(argv[1],"-r") == 0x00) || (strcmp(argv[1],"-R") == 0x00)) { fprintf(file, "%s\n", local_file_path); } @@ -1436,16 +1436,16 @@ int main(int argc, char **argv) } fclose(file); - storj_api->file_path = temp_buff; + cli_api->file_path = temp_buff; } else { /* its a valid directory so let the list of files to be uploaded be handled in verify upload file () */ - storj_api->dst_file = NULL; + cli_api->dst_file = NULL; - memcpy(upload_file_path, storj_api->file_path, strlen(storj_api->file_path)); + memcpy(upload_file_path, cli_api->file_path, strlen(cli_api->file_path)); if (upload_file_path[(strlen(upload_file_path) - 1)] != '/') { strcat(upload_file_path, "/"); - storj_api->file_path = upload_file_path; + cli_api->file_path = upload_file_path; } } @@ -1457,11 +1457,11 @@ int main(int argc, char **argv) if ((num_of_tokens > 0x00) && ((num_of_tokens >= 0x02) || (num_of_tokens <= 0x03))) { char *dst_file_name = NULL; - storj_api->bucket_name = token[1]; + cli_api->bucket_name = token[1]; if ((token[2] == NULL) || (strcmp(token[2], ".") == 0x00)) { - storj_upload_files(storj_api); + cli_upload_files(cli_api); } else { printf("[%s][%d] storj://; storj:/// storj:///. !!!!!\n", __FUNCTION__, __LINE__); goto end_program; @@ -1475,30 +1475,30 @@ int main(int argc, char **argv) } else if (strcmp(command, "upload-files") == 0) { /* get the corresponding bucket id from the bucket name */ - storj_api->bucket_name = argv[command_index + 1]; - storj_api->file_path = argv[command_index + 2]; - storj_api->dst_file = NULL; + cli_api->bucket_name = argv[command_index + 1]; + cli_api->file_path = argv[command_index + 2]; + cli_api->dst_file = NULL; - if (!storj_api->bucket_name || !storj_api->file_name) { + if (!cli_api->bucket_name || !cli_api->file_name) { printf("Missing arguments: \n"); status = 1; goto end_program; } - storj_upload_files(storj_api); + cli_upload_files(cli_api); } else if (strcmp(command, "download-files") == 0) { /* get the corresponding bucket id from the bucket name */ - storj_api->bucket_name = argv[command_index + 1]; - storj_api->file_path = argv[command_index + 2]; + cli_api->bucket_name = argv[command_index + 1]; + cli_api->file_path = argv[command_index + 2]; - if (!storj_api->bucket_name || !storj_api->file_path) { + if (!cli_api->bucket_name || !cli_api->file_path) { printf("Missing arguments: \n"); status = 1; goto end_program; } - storj_download_files(storj_api); + cli_download_files(cli_api); } else if (strcmp(command, "mkbkt") == 0x00) { char *bucket_name = argv[command_index + 1]; @@ -1512,48 +1512,48 @@ int main(int argc, char **argv) storj_bridge_create_bucket(env, bucket_name, NULL, create_bucket_callback); } else if (strcmp(command, "rm") == 0) { - storj_api->bucket_name = argv[command_index + 1]; - storj_api->file_name = argv[command_index + 2]; + cli_api->bucket_name = argv[command_index + 1]; + cli_api->file_name = argv[command_index + 2]; - if (storj_api->file_name != NULL) { - if (!storj_api->bucket_name|| !storj_api->file_name) { + if (cli_api->file_name != NULL) { + if (!cli_api->bucket_name|| !cli_api->file_name) { printf("Missing arguments, expected: \n"); status = 1; goto end_program; } - storj_remove_file(storj_api); + cli_remove_file(cli_api); } else { - storj_remove_bucket(storj_api); + cli_remove_bucket(cli_api); } } else if (strcmp(command, "ls") == 0x00) { if (argv[command_index + 1] != NULL) { /* bucket-name , used to list files */ - storj_api->bucket_name = argv[command_index + 1]; + cli_api->bucket_name = argv[command_index + 1]; - storj_list_files(storj_api); + cli_list_files(cli_api); } else { - storj_list_buckets(storj_api); + cli_list_buckets(cli_api); } } else if (strcmp(command, "get-bucket-id") == 0) { - storj_api->bucket_name = argv[command_index + 1]; - cli_get_bucket_id(storj_api); + cli_api->bucket_name = argv[command_index + 1]; + cli_get_bucket_id(cli_api); } else if (strcmp(command, "lm") == 0) { - storj_api->bucket_name = argv[command_index + 1]; - storj_api->file_name = argv[command_index + 2]; + cli_api->bucket_name = argv[command_index + 1]; + cli_api->file_name = argv[command_index + 2]; - if (!storj_api->bucket_name|| !storj_api->file_name) { + if (!cli_api->bucket_name|| !cli_api->file_name) { printf("Missing arguments, expected: \n"); status = 1; goto end_program; } - storj_list_mirrors(storj_api); + cli_list_mirrors(cli_api); } else { printf("'%s' is not a storj command. See 'storj --help'\n\n", @@ -1588,8 +1588,8 @@ int main(int argc, char **argv) if (mnemonic) { free(mnemonic); } - if(storj_api){ - free(storj_api); + if(cli_api){ + free(cli_api); } return status; diff --git a/src/cli_callback.c b/src/cli_callback.c index 4ae13df..d471503 100644 --- a/src/cli_callback.c +++ b/src/cli_callback.c @@ -69,7 +69,7 @@ static void printdir(char *dir, int depth, FILE *src_fd, void *handle) char *full_path = NULL; char *s; char * start; - storj_api_t *storj_api = handle; + cli_api_t *cli_api = handle; if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); @@ -102,11 +102,11 @@ static int file_exists(void *handle) { struct stat sb; gid_t st_grpid; - storj_api_t *storj_api = handle; + cli_api_t *cli_api = handle; FILE *src_fd, *dst_fd; - if (stat(storj_api->file_path, &sb) == -1) { + if (stat(cli_api->file_path, &sb) == -1) { perror("stat"); return CLI_NO_SUCH_FILE_OR_DIR; } @@ -120,10 +120,10 @@ static int file_exists(void *handle) printf("character device\n"); break; case S_IFDIR: - if((src_fd = fopen(storj_api->src_list, "w")) == NULL) { + if((src_fd = fopen(cli_api->src_list, "w")) == NULL) { return CLI_UPLOAD_FILE_LOG_ERR; } - printdir(storj_api->file_path, 0, src_fd, handle); + printdir(cli_api->file_path, 0, src_fd, handle); fclose(src_fd); return CLI_VALID_DIR; break; @@ -217,8 +217,8 @@ static void file_progress(double progress, static void upload_file_complete(int status, storj_file_meta_t *file, void *handle) { - storj_api_t *storj_api = handle; - storj_api->rcvd_cmd_resp = "upload-file-resp"; + cli_api_t *cli_api = handle; + cli_api->rcvd_cmd_resp = "upload-file-resp"; printf("\n"); if (status != 0) { @@ -230,7 +230,7 @@ static void upload_file_complete(int status, storj_file_meta_t *file, void *hand storj_free_uploaded_file_info(file); - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); } static void upload_signal_handler(uv_signal_t *req, int signum) @@ -245,7 +245,7 @@ static void upload_signal_handler(uv_signal_t *req, int signum) static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) { - storj_api_t *storj_api = handle; + cli_api_t *cli_api = handle; FILE *fd = fopen(file_path, "r"); @@ -256,12 +256,12 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, const char *file_name = get_filename_separator(file_path); - if (storj_api->dst_file == NULL) { + if (cli_api->dst_file == NULL) { if (!file_name) { file_name = file_path; } } else { - file_name = storj_api->dst_file; + file_name = cli_api->dst_file; } // Upload opts env variables: @@ -311,8 +311,8 @@ static int upload_file(storj_env_t *env, char *bucket_id, const char *file_path, static void upload_files_complete(int status, storj_file_meta_t *file, void *handle) { - storj_api_t *storj_api = handle; - storj_api->rcvd_cmd_resp = "upload-files-resp"; + cli_api_t *cli_api = handle; + cli_api->rcvd_cmd_resp = "upload-files-resp"; printf("\n"); if (status != 0) { @@ -323,12 +323,12 @@ static void upload_files_complete(int status, storj_file_meta_t *file, void *han storj_free_uploaded_file_info(file); } - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); } static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path, void *handle) { - storj_api_t *storj_api = handle; + cli_api_t *cli_api = handle; FILE *fd = fopen(file_path, "r"); @@ -338,19 +338,19 @@ static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path } printf("Uploading[%d]of[%d] src file = %s as ", - storj_api->xfer_count, storj_api->total_files, file_path); + cli_api->xfer_count, cli_api->total_files, file_path); /* replace the dir with __ */ - char *s = strstr(storj_api->src_file, storj_api->file_path); - char *start = s + strlen(storj_api->file_path); + char *s = strstr(cli_api->src_file, cli_api->file_path); + char *start = s + strlen(cli_api->file_path); char tmp_dir[256]; start = replace_char(start, '/', '_'); memset(tmp_dir, 0x00, sizeof(tmp_dir)); - strcat(tmp_dir, storj_api->file_path); + strcat(tmp_dir, cli_api->file_path); strcat(tmp_dir, start); - storj_api->dst_file = tmp_dir; + cli_api->dst_file = tmp_dir; - const char *file_name = get_filename_separator(storj_api->dst_file); + const char *file_name = get_filename_separator(cli_api->dst_file); if (!file_name) { file_name = file_path; @@ -402,13 +402,13 @@ static int upload_files(storj_env_t *env, char *bucket_id, const char *file_path static void verify_upload_files(void *handle) { - storj_api_t *storj_api = handle; + cli_api_t *cli_api = handle; int total_src_files = 0x00; int total_dst_files = 0x00; int ret = 0x00; /* upload list file previously not created? */ - if (storj_api->dst_file == NULL) + if (cli_api->dst_file == NULL) { char pwd_path[256]= {}; memset(pwd_path, 0x00, sizeof(pwd_path)); @@ -423,9 +423,9 @@ static void verify_upload_files(void *handle) } /* check the directory and create the path to upload list file */ - memset(storj_api->src_list, 0x00, sizeof(storj_api->src_list)); - memcpy(storj_api->src_list, upload_list_file, sizeof(pwd_path)); - storj_api->dst_file = storj_api->src_list; + memset(cli_api->src_list, 0x00, sizeof(cli_api->src_list)); + memcpy(cli_api->src_list, upload_list_file, sizeof(pwd_path)); + cli_api->dst_file = cli_api->src_list; } /* create a upload list file src_list.txt */ @@ -433,11 +433,11 @@ static void verify_upload_files(void *handle) } /* create a upload list file src_list.txt */ - storj_api->src_fd = fopen(storj_api->src_list, "r"); + cli_api->src_fd = fopen(cli_api->src_list, "r"); - if (!storj_api->src_fd) { + if (!cli_api->src_fd) { printf("[%s][%d]Invalid file path: %s\n", - __FUNCTION__, __LINE__, storj_api->src_list); + __FUNCTION__, __LINE__, cli_api->src_list); exit(0); } else { /* count total src_list files */ @@ -448,7 +448,7 @@ static void verify_upload_files(void *handle) memset(line, 0x00, sizeof(line)); /* read a line from a file */ - while (fgets(line[i], sizeof(line), storj_api->src_fd) != NULL) + while (fgets(line[i], sizeof(line), cli_api->src_fd) != NULL) { if (i <= MAX_UPLOAD_FILES) { i++; @@ -463,17 +463,17 @@ static void verify_upload_files(void *handle) total_src_files = i; } - storj_api->total_files = total_src_files; - storj_api->xfer_count = 0x01; + cli_api->total_files = total_src_files; + cli_api->xfer_count = 0x01; - storj_api->rcvd_cmd_resp = "verify-upload-files-resp"; - queue_next_cmd_req(storj_api); + cli_api->rcvd_cmd_resp = "verify-upload-files-resp"; + queue_next_cmd_req(cli_api); } static void download_file_complete(int status, FILE *fd, void *handle) { - storj_api_t *storj_api = handle; - storj_api->rcvd_cmd_resp = "download-file-resp"; + cli_api_t *cli_api = handle; + cli_api->rcvd_cmd_resp = "download-file-resp"; printf("\n"); fclose(fd); @@ -493,7 +493,7 @@ static void download_file_complete(int status, FILE *fd, void *handle) printf("Download Success!\n"); } - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); } static void download_signal_handler(uv_signal_t *req, int signum) @@ -526,9 +526,9 @@ static int download_file(storj_env_t *env, char *bucket_id, if (strcmp(user_input, "n") == 0) { printf("\nCanceled overwriting of [%s].\n", path); - storj_api_t *storj_api = handle; - storj_api->rcvd_cmd_resp = "download-file-resp"; - queue_next_cmd_req(storj_api); + cli_api_t *cli_api = handle; + cli_api->rcvd_cmd_resp = "download-file-resp"; + queue_next_cmd_req(cli_api); return 1; } @@ -572,9 +572,9 @@ static void list_mirrors_callback(uv_work_t *work_req, int status) assert(status == 0); json_request_t *req = work_req->data; - storj_api_t *storj_api = req->handle; - storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "list-mirrors-resp"; + cli_api_t *cli_api = req->handle; + cli_api->last_cmd_req = cli_api->curr_cmd_req; + cli_api->rcvd_cmd_resp = "list-mirrors-resp"; if (req->status_code != 200) { printf("Request failed with status code: %i\n", @@ -625,7 +625,7 @@ static void list_mirrors_callback(uv_work_t *work_req, int status) json_object_put(req->response); - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); cleanup: free(req->path); free(req); @@ -637,9 +637,9 @@ static void delete_file_callback(uv_work_t *work_req, int status) assert(status == 0); json_request_t *req = work_req->data; - storj_api_t *storj_api = req->handle; - storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "remove-file-resp"; + cli_api_t *cli_api = req->handle; + cli_api->last_cmd_req = cli_api->curr_cmd_req; + cli_api->rcvd_cmd_resp = "remove-file-resp"; if (req->status_code == 200 || req->status_code == 204) { printf("File was successfully removed from bucket.\n"); @@ -653,7 +653,7 @@ static void delete_file_callback(uv_work_t *work_req, int status) json_object_put(req->response); - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); cleanup: free(req->path); free(req); @@ -665,9 +665,9 @@ static void delete_bucket_callback(uv_work_t *work_req, int status) assert(status == 0); json_request_t *req = work_req->data; - storj_api_t *storj_api = req->handle; - storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "remove-bucket-resp"; + cli_api_t *cli_api = req->handle; + cli_api->last_cmd_req = cli_api->curr_cmd_req; + cli_api->rcvd_cmd_resp = "remove-bucket-resp"; if (req->status_code == 200 || req->status_code == 204) { @@ -686,7 +686,7 @@ static void delete_bucket_callback(uv_work_t *work_req, int status) json_object_put(req->response); - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); cleanup: free(req->path); free(req); @@ -721,11 +721,11 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) { int ret_status = 0x00; assert(status == 0); - get_buckets_request_t *req = work_req->data; - storj_api_t *storj_api = req->handle; + get_bucket_id_request_t *req = work_req->data; + cli_api_t *cli_api = req->handle; - storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "get-bucket-id-resp"; + cli_api->last_cmd_req = cli_api->curr_cmd_req; + cli_api->rcvd_cmd_resp = "get-bucket-id-resp"; if (req->status_code == 401) { printf("Invalid user credentials.\n"); @@ -733,34 +733,17 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) } else if (req->status_code != 200 && req->status_code != 304) { printf("Request failed with status code: %i\n", req->status_code); goto cleanup; - } else if (req->total_buckets == 0) { - printf("No buckets.\n"); - goto cleanup; } - for (int i = 0; i < req->total_buckets; i++) { - storj_bucket_meta_t *bucket = &req->buckets[i]; - if (strcmp(storj_api->bucket_name, bucket->name) == 0x00) { - printf("ID: %s \tDecrypted: %s \tCreated: %s \tName: %s\n", - bucket->id, bucket->decrypted ? "true" : "false", - bucket->created, bucket->name); - - /* store the bucket id */ - memset(storj_api->bucket_id, 0x00, sizeof(storj_api->bucket_id)); - strcpy(storj_api->bucket_id, (char *)bucket->id); - break; - } else { - if (i >= (req->total_buckets - 1)) { - printf("Invalid bucket name. \n"); - goto cleanup; - } - } - } + /* store the bucket id */ + memset(cli_api->bucket_id, 0x00, sizeof(cli_api->bucket_id)); + strcpy(cli_api->bucket_id, (char *)req->bucket_id); + printf("ID: %s \tName: %s\n", req->bucket_id, req->bucket_name); - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); cleanup: - storj_free_get_buckets_request(req); + free(req); free(work_req); } @@ -771,9 +754,9 @@ void list_files_callback(uv_work_t *work_req, int status) assert(status == 0); list_files_request_t *req = work_req->data; - storj_api_t *storj_api = req->handle; - storj_api->last_cmd_req = storj_api->curr_cmd_req; - storj_api->rcvd_cmd_resp = "list-files-resp"; + cli_api_t *cli_api = req->handle; + cli_api->last_cmd_req = cli_api->curr_cmd_req; + cli_api->rcvd_cmd_resp = "list-files-resp"; if (req->status_code == 404) { printf("Bucket id [%s] does not exist\n", req->bucket_id); @@ -804,11 +787,11 @@ void list_files_callback(uv_work_t *work_req, int status) for (int i = 0; i < req->total_files; i++) { storj_file_meta_t *file = &req->files[i]; - if ((storj_api->file_name != NULL) && - (strcmp(storj_api->file_name, file->filename)) == 0x00) { + if ((cli_api->file_name != NULL) && + (strcmp(cli_api->file_name, file->filename)) == 0x00) { /* store the file id */ - memset(storj_api->file_id, 0x00, sizeof(storj_api->file_id)); - strcpy(storj_api->file_id, (char *)file->id); + memset(cli_api->file_id, 0x00, sizeof(cli_api->file_id)); + strcpy(cli_api->file_id, (char *)file->id); } printf("ID: %s \tSize: %" PRIu64 " bytes \tDecrypted: %s \tType: %s \tCreated: %s \tName: %s\n", @@ -822,10 +805,10 @@ void list_files_callback(uv_work_t *work_req, int status) fprintf(dwnld_list_fd, "%s:%s\n",file->id, file->filename); } - storj_api->total_files = req->total_files; - storj_api->xfer_count = 0x01; + cli_api->total_files = req->total_files; + cli_api->xfer_count = 0x01; fclose(dwnld_list_fd); - queue_next_cmd_req(storj_api); + queue_next_cmd_req(cli_api); cleanup: @@ -833,87 +816,87 @@ void list_files_callback(uv_work_t *work_req, int status) free(work_req); } -void queue_next_cmd_req(storj_api_t *storj_api) +void queue_next_cmd_req(cli_api_t *cli_api) { - void *handle = storj_api->handle; + void *handle = cli_api->handle; #ifdef debug_enable printf("[%s][%d]start !!!! expt resp = %s; rcvd resp = %s \n", __FUNCTION__, __LINE__, - storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); + cli_api->excp_cmd_resp, cli_api->rcvd_cmd_resp ); printf("[%s][%d]last cmd = %s; cur cmd = %s; next cmd = %s\n", - __FUNCTION__, __LINE__, storj_api->last_cmd_req, - storj_api->curr_cmd_req, storj_api->next_cmd_req); + __FUNCTION__, __LINE__, cli_api->last_cmd_req, + cli_api->curr_cmd_req, cli_api->next_cmd_req); #endif - if(storj_api->excp_cmd_resp != NULL) { - if (strcmp(storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp) == 0x00) { - if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "list-files-req") == 0x00)) { - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "list-files-resp"; - - storj_bridge_list_files(storj_api->env, storj_api->bucket_id, - storj_api, list_files_callback); - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "remove-bucket-req") == 0x00)) { - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "remove-bucket-resp"; - - storj_bridge_delete_bucket(storj_api->env, storj_api->bucket_id, - storj_api, delete_bucket_callback); - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "remove-file-req") == 0x00)) { + if(cli_api->excp_cmd_resp != NULL) { + if (strcmp(cli_api->excp_cmd_resp, cli_api->rcvd_cmd_resp) == 0x00) { + if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "list-files-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "list-files-resp"; + + storj_bridge_list_files(cli_api->env, cli_api->bucket_id, + cli_api, list_files_callback); + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "remove-bucket-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "remove-bucket-resp"; + + storj_bridge_delete_bucket(cli_api->env, cli_api->bucket_id, + cli_api, delete_bucket_callback); + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "remove-file-req") == 0x00)) { printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", - __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, - storj_api->bucket_name); - - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "remove-file-resp"; - - storj_bridge_delete_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api, delete_file_callback); - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "list-mirrors-req") == 0x00)) { + __FUNCTION__, __LINE__, cli_api->file_name, cli_api->file_id, + cli_api->bucket_name); + + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "remove-file-resp"; + + storj_bridge_delete_file(cli_api->env, cli_api->bucket_id, cli_api->file_id, + cli_api, delete_file_callback); + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "list-mirrors-req") == 0x00)) { printf("[%s][%d]file-name = %s; file-id = %s; bucket-name = %s \n", - __FUNCTION__, __LINE__, storj_api->file_name, storj_api->file_id, - storj_api->bucket_name); - - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "list-mirrors-resp"; - - storj_bridge_list_mirrors(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api, list_mirrors_callback); - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "upload-file-req") == 0x00)) { - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "upload-file-resp"; - - upload_file(storj_api->env, storj_api->bucket_id, storj_api->file_name, storj_api); - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "verify-upload-files-req") == 0x00)) { - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "verify-upload-files-resp"; - - verify_upload_files(storj_api); - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "upload-files-req") == 0x00)) { - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->excp_cmd_resp = "upload-files-resp"; - - FILE *file = fopen(storj_api->src_list, "r"); + __FUNCTION__, __LINE__, cli_api->file_name, cli_api->file_id, + cli_api->bucket_name); + + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "list-mirrors-resp"; + + storj_bridge_list_mirrors(cli_api->env, cli_api->bucket_id, cli_api->file_id, + cli_api, list_mirrors_callback); + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "upload-file-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "upload-file-resp"; + + upload_file(cli_api->env, cli_api->bucket_id, cli_api->file_name, cli_api); + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "verify-upload-files-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "verify-upload-files-resp"; + + verify_upload_files(cli_api); + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "upload-files-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->excp_cmd_resp = "upload-files-resp"; + + FILE *file = fopen(cli_api->src_list, "r"); char line[256][256]; char *temp; @@ -924,41 +907,41 @@ void queue_next_cmd_req(storj_api_t *storj_api) while((fgets(line[i],sizeof(line), file)!= NULL)) {/* read a line from a file */ temp = strrchr(line[i], '\n'); if(temp) *temp = '\0'; - storj_api->src_file = line[i]; + cli_api->src_file = line[i]; i++; - if(i >= storj_api->xfer_count) { + if(i >= cli_api->xfer_count) { break; } } } fclose(file); - if (storj_api->xfer_count <= storj_api->total_files) { + if (cli_api->xfer_count <= cli_api->total_files) { /* is it the last file ? */ - if(storj_api->xfer_count == storj_api->total_files) { - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; + if(cli_api->xfer_count == cli_api->total_files) { + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; } - upload_files(storj_api->env, storj_api->bucket_id, storj_api->src_file, storj_api); - storj_api->xfer_count++; + upload_files(cli_api->env, cli_api->bucket_id, cli_api->src_file, cli_api); + cli_api->xfer_count++; } else { printf("[%s][%d] Invalid xfer counts\n", __FUNCTION__, __LINE__); exit(0); } - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "download-file-req") == 0x00)) { - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "download-file-resp"; - - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, - storj_api->dst_file, storj_api); - } else if ((storj_api->next_cmd_req != NULL) && - (strcmp(storj_api->next_cmd_req, "download-files-req") == 0x00)) { - storj_api->curr_cmd_req = storj_api->next_cmd_req; - storj_api->excp_cmd_resp = "download-file-resp"; + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "download-file-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "download-file-resp"; + + download_file(cli_api->env, cli_api->bucket_id, cli_api->file_id, + cli_api->dst_file, cli_api); + } else if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "download-files-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->excp_cmd_resp = "download-file-resp"; FILE *file = stdout; @@ -976,7 +959,7 @@ void queue_next_cmd_req(storj_api_t *storj_api) temp = strrchr(line[i], '\n'); if(temp) *temp = '\0'; i++; - if (i >= storj_api->xfer_count) { + if (i >= cli_api->xfer_count) { break; } } @@ -989,24 +972,24 @@ void queue_next_cmd_req(storj_api_t *storj_api) token[tk_idx] = strtok(NULL, ":"); } - if(storj_api->xfer_count <= storj_api->total_files) { + if(cli_api->xfer_count <= cli_api->total_files) { /* is it the last file ? */ - if(storj_api->xfer_count == storj_api->total_files) { - storj_api->next_cmd_req = storj_api->final_cmd_req; - storj_api->final_cmd_req = NULL; + if(cli_api->xfer_count == cli_api->total_files) { + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; } - memset(storj_api->file_id, 0x00, sizeof(storj_api->file_id)); - strcpy(storj_api->file_id, token[0]); - strcpy(temp_path, storj_api->file_path); - if (storj_api->file_path[(strlen(storj_api->file_path)-1)] != '/') { + memset(cli_api->file_id, 0x00, sizeof(cli_api->file_id)); + strcpy(cli_api->file_id, token[0]); + strcpy(temp_path, cli_api->file_path); + if (cli_api->file_path[(strlen(cli_api->file_path)-1)] != '/') { strcat(temp_path, "/"); } strcat(temp_path, token[1]); - fprintf(stdout,"*****[%d:%d] downloading file to: %s *****\n", storj_api->xfer_count, storj_api->total_files, temp_path); - storj_api->xfer_count++; + fprintf(stdout,"*****[%d:%d] downloading file to: %s *****\n", cli_api->xfer_count, cli_api->total_files, temp_path); + cli_api->xfer_count++; - download_file(storj_api->env, storj_api->bucket_id, storj_api->file_id, temp_path, storj_api); + download_file(cli_api->env, cli_api->bucket_id, cli_api->file_id, temp_path, cli_api); } else { printf("[%s][%d] Invalid xfer counts\n", __FUNCTION__, __LINE__); exit(0); @@ -1022,13 +1005,117 @@ void queue_next_cmd_req(storj_api_t *storj_api) } else { printf("[%s][%d]Oops !!!! expt resp = %s; rcvd resp = %s \n", __FUNCTION__, __LINE__, - storj_api->excp_cmd_resp, storj_api->rcvd_cmd_resp ); + cli_api->excp_cmd_resp, cli_api->rcvd_cmd_resp ); printf("[%s][%d]last cmd = %s; cur cmd = %s; next cmd = %s\n", - __FUNCTION__, __LINE__, storj_api->last_cmd_req, - storj_api->curr_cmd_req, storj_api->next_cmd_req); + __FUNCTION__, __LINE__, cli_api->last_cmd_req, + cli_api->curr_cmd_req, cli_api->next_cmd_req); } } else { /* calling straight storj calls without going thru the state machine */ exit(0); } } + + +CLI_API int cli_list_buckets(cli_api_t *cli_api) +{ + cli_api->last_cmd_req = NULL; + cli_api->curr_cmd_req = "get-bucket-id-req"; + cli_api->next_cmd_req = NULL; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "get-bucket-id-resp"; + + /* when callback returns, we store the bucket id of bucket name else null */ + return storj_bridge_get_buckets(cli_api->env, cli_api, get_buckets_callback); +} + +CLI_API int cli_get_bucket_id(cli_api_t *cli_api) +{ + int ret = -1; + cli_api->last_cmd_req = NULL; + cli_api->curr_cmd_req = "get-bucket-id-req"; + cli_api->next_cmd_req = NULL; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "get-bucket-id-resp"; + + /* when callback returns, we store the bucket id of bucket name else null */ + //return storj_bridge_get_buckets(cli_api->env, cli_api, get_bucket_id_callback); + return storj_bridge_get_bucket_id(cli_api->env, cli_api->bucket_name, cli_api, get_bucket_id_callback); +} + +CLI_API int cli_list_files(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_get_bucket_id(cli_api); + cli_api->next_cmd_req = "list-files-req"; + cli_api->final_cmd_req = NULL; + + return ret; +} + +CLI_API int cli_remove_bucket(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_get_bucket_id(cli_api); + cli_api->next_cmd_req = "remove-bucket-req"; + cli_api->final_cmd_req = NULL; + + return ret; +} + +CLI_API int cli_remove_file(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_list_files(cli_api); + cli_api->final_cmd_req = "remove-file-req"; + + return ret; +} + +CLI_API int cli_list_mirrors(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_list_files(cli_api); + cli_api->final_cmd_req = "list-mirrors-req"; + + return ret; +} + +CLI_API int cli_upload_file(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_get_bucket_id(cli_api); + cli_api->next_cmd_req = "upload-file-req"; + cli_api->final_cmd_req = NULL; + + return ret; +} + +CLI_API int cli_upload_files(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_get_bucket_id(cli_api); + cli_api->next_cmd_req = "verify-upload-files-req"; + cli_api->final_cmd_req = "upload-files-req"; + + return ret; +} + +CLI_API int cli_download_file(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_list_files(cli_api); + cli_api->final_cmd_req = "download-file-req"; + + return ret; +} + +CLI_API int cli_download_files(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_list_files(cli_api); + cli_api->final_cmd_req = "download-files-req"; + + return ret; +} + diff --git a/src/cli_callback.h b/src/cli_callback.h index ca02577..68360b3 100755 --- a/src/cli_callback.h +++ b/src/cli_callback.h @@ -15,12 +15,51 @@ extern "C" { #include "storj.h" +#if defined(_WIN32) && defined(STORJDLL) +#if defined(DLL_EXPORT) + #define CLI_API __declspec(dllexport) + #else + #define CLI_API __declspec(dllimport) + #endif +#else +#define CLI_API +#endif + #define CLI_NO_SUCH_FILE_OR_DIR 0x00 #define CLI_VALID_REGULAR_FILE 0x01 #define CLI_VALID_DIR 0x02 #define CLI_UNKNOWN_FILE_ATTR 0x03 #define CLI_UPLOAD_FILE_LOG_ERR 0x04 +/** + * @brief A Structure for passing the User's Application info to + * Storj API. + */ +typedef struct cli_api { + storj_env_t *env; + char *bucket_name; + char bucket_id[256]; + char *file_name; + char file_id[256]; + char *file_path; /**< local upload files directory path */ + FILE *src_fd; + char src_list[256]; /**< file list ready to upload */ + char *src_file; /**< next file ready to upload */ + FILE *dst_fd; + char *dst_file; /**< next file ready to upload */ + int xfer_count; /**< # of files xferred (up/down) */ + int total_files; /**< total files to upload */ + char *last_cmd_req; /**< last command requested */ + char *curr_cmd_req; /**< cli curr command requested */ + char *next_cmd_req; /**< cli curr command requested */ + char *final_cmd_req; /**< final command in the seq */ + char *excp_cmd_resp; /**< expected cmd response */ + char *rcvd_cmd_resp; /**< received cmd response */ + int error_status; /**< command response/error status */ + storj_log_levels_t *log; + void *handle; +} cli_api_t; + /** * @brief Callback function listing bucket names & IDs */ @@ -35,7 +74,103 @@ void get_bucket_id_callback(uv_work_t *work_req, int status); /** * @brief Storj api state machine function */ -void queue_next_cmd_req(storj_api_t *storj_api); +void queue_next_cmd_req(cli_api_t *cli_api); + +/** + * @brief Function lists the bucket names & IDs + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_list_buckets(cli_api_t *cli_api); + +/** + * @brief Function returns the corresponding bucket's id for a + * given bucket name + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_get_bucket_id(cli_api_t *cli_api); + +/** + * @brief Function to list files in a given bucket name + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_list_files(cli_api_t *cli_api); + +/** + * @brief Function to remove a given bucket name + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_remove_bucket(cli_api_t *cli_api); + +/** + * @brief Function to remove a file from a given bucket name + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_remove_file(cli_api_t *cli_api); + +/** + * @brief Function to return the node IDs for a given file for a + * given bucket name + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_list_mirrors(cli_api_t *cli_api); + +/** + * @brief Function to upload a local file into a given bucket + * name + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_upload_file(cli_api_t *cli_api); + +/** + * @brief Function to upload local files into a given bucket + * name + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_upload_files(cli_api_t *cli_api); + +/** + * @brief Function to download a file from a given bucket to a + * local folder + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_download_file(cli_api_t *cli_api); + +/** + * @brief Function to download files from a given bucket to a + * local folder + * + * @param[in] cli_api_t structure that passes user's input + * info + * @return A non-zero error value on failure and 0 on success. + */ +CLI_API int cli_download_files(cli_api_t *cli_api); #ifdef __cplusplus } diff --git a/src/storj.c b/src/storj.c index 3d7c273..5b65a1e 100644 --- a/src/storj.c +++ b/src/storj.c @@ -2,7 +2,6 @@ #include "http.h" #include "utils.h" #include "crypto.h" -#include "cli_callback.h" static inline void noop() {}; @@ -1757,114 +1756,4 @@ STORJ_API int storj_bridge_register(storj_env_t *env, return STORJ_MEMORY_ERROR; } return uv_queue_work(env->loop, (uv_work_t*) work, json_request_worker, cb); -} - - -STORJ_API int storj_list_buckets(storj_api_t *storj_api) -{ - storj_api->last_cmd_req = NULL; - storj_api->curr_cmd_req = "get-bucket-id-req"; - storj_api->next_cmd_req = NULL; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "get-bucket-id-resp"; - - /* when callback returns, we store the bucket id of bucket name else null */ - return storj_bridge_get_buckets(storj_api->env, storj_api, get_buckets_callback); -} - -STORJ_API int cli_get_bucket_id(storj_api_t *storj_api) -{ - int ret = -1; - char *bucket_name = storj_api->bucket_name; - if (!bucket_name) - { - printf("Missing : \n"); - return ret; - } - - storj_api->last_cmd_req = NULL; - storj_api->curr_cmd_req = "get-bucket-id-req"; - storj_api->next_cmd_req = NULL; - storj_api->final_cmd_req = NULL; - storj_api->excp_cmd_resp = "get-bucket-id-resp"; - - /* when callback returns, we store the bucket id of bucket name else null */ - return storj_bridge_get_buckets(storj_api->env, storj_api, get_bucket_id_callback); -} - -STORJ_API int storj_list_files(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = cli_get_bucket_id(storj_api); - storj_api->next_cmd_req = "list-files-req"; - storj_api->final_cmd_req = NULL; - - return ret; -} - -STORJ_API int storj_remove_bucket(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = cli_get_bucket_id(storj_api); - storj_api->next_cmd_req = "remove-bucket-req"; - storj_api->final_cmd_req = NULL; - - return ret; -} - -STORJ_API int storj_remove_file(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = storj_list_files(storj_api); - storj_api->final_cmd_req = "remove-file-req"; - - return ret; -} - -STORJ_API int storj_list_mirrors(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = storj_list_files(storj_api); - storj_api->final_cmd_req = "list-mirrors-req"; - - return ret; -} - -STORJ_API int storj_upload_file(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = cli_get_bucket_id(storj_api); - storj_api->next_cmd_req = "upload-file-req"; - storj_api->final_cmd_req = NULL; - - return ret; -} - -STORJ_API int storj_upload_files(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = cli_get_bucket_id(storj_api); - storj_api->next_cmd_req = "verify-upload-files-req"; - storj_api->final_cmd_req = "upload-files-req"; - - return ret; -} - -STORJ_API int storj_download_file(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = storj_list_files(storj_api); - storj_api->final_cmd_req = "download-file-req"; - - return ret; -} - -STORJ_API int storj_download_files(storj_api_t *storj_api) -{ - int ret = 0x00; - ret = storj_list_files(storj_api); - storj_api->final_cmd_req = "download-files-req"; - - return ret; -} - +} \ No newline at end of file diff --git a/src/storj.h b/src/storj.h index 642f996..3c17567 100755 --- a/src/storj.h +++ b/src/storj.h @@ -590,35 +590,6 @@ typedef struct { int pending_work_count; } storj_upload_state_t; -/** - * @brief A Structure for passing the User's Application info to - * Storj API. - */ -typedef struct storj_api { - storj_env_t *env; - char *bucket_name; - char bucket_id[256]; - char *file_name; - char file_id[256]; - char *file_path; /**< local upload files directory path */ - FILE *src_fd; - char src_list[256]; /**< file list ready to upload */ - char *src_file; /**< next file ready to upload */ - FILE *dst_fd; - char *dst_file; /**< next file ready to upload */ - int xfer_count; /**< # of files xferred (up/down) */ - int total_files; /**< total files to upload */ - char *last_cmd_req; /**< last command requested */ - char *curr_cmd_req; /**< cli curr command requested */ - char *next_cmd_req; /**< cli curr command requested */ - char *final_cmd_req; /**< final command in the seq */ - char *excp_cmd_resp; /**< expected cmd response */ - char *rcvd_cmd_resp; /**< received cmd response */ - int error_status; /**< command response/error status */ - storj_log_levels_t *log; - void *handle; -} storj_api_t; - /** * @brief Initialize a Storj environment * @@ -1119,102 +1090,6 @@ STORJ_API int storj_bridge_register(storj_env_t *env, void *handle, uv_after_work_cb cb); -/** - * @brief Function lists the bucket names & IDs - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_list_buckets(storj_api_t *storj_api); - -/** - * @brief Function returns the corresponding bucket's id for a - * given bucket name - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int cli_get_bucket_id(storj_api_t *storj_api); - -/** - * @brief Function to list files in a given bucket name - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_list_files(storj_api_t *storj_api); - -/** - * @brief Function to remove a given bucket name - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_remove_bucket(storj_api_t *storj_api); - -/** - * @brief Function to remove a file from a given bucket name - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_remove_file(storj_api_t *storj_api); - -/** - * @brief Function to return the node IDs for a given file for a - * given bucket name - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_list_mirrors(storj_api_t *storj_api); - -/** - * @brief Function to upload a local file into a given bucket - * name - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_upload_file(storj_api_t *storj_api); - -/** - * @brief Function to upload local files into a given bucket - * name - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_upload_files(storj_api_t *storj_api); - -/** - * @brief Function to download a file from a given bucket to a - * local folder - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_download_file(storj_api_t *storj_api); - -/** - * @brief Function to download files from a given bucket to a - * local folder - * - * @param[in] storj_api_t structure that passes user's input - * info - * @return A non-zero error value on failure and 0 on success. - */ -STORJ_API int storj_download_files(storj_api_t *storj_api); - static inline char separator() { #ifdef _WIN32 From 37e5478e4b94b89e0071cd04e2b9c87655744444 Mon Sep 17 00:00:00 2001 From: kishore Date: Mon, 12 Feb 2018 12:58:49 -0500 Subject: [PATCH 67/68] modified the state machine to used get bucket id and get file id storj api functions --- src/cli_callback.c | 55 ++++++++++++++++++++++++++++++++++++++++++---- src/cli_callback.h | 6 +++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/cli_callback.c b/src/cli_callback.c index d471503..7714cc2 100644 --- a/src/cli_callback.c +++ b/src/cli_callback.c @@ -747,6 +747,35 @@ void get_bucket_id_callback(uv_work_t *work_req, int status) free(work_req); } +void get_file_id_callback(uv_work_t *work_req, int status) +{ + int ret_status = 0x00; + assert(status == 0); + get_file_id_request_t *req = work_req->data; + cli_api_t *cli_api = req->handle; + + cli_api->last_cmd_req = cli_api->curr_cmd_req; + cli_api->rcvd_cmd_resp = "get-file-id-resp"; + + if (req->status_code == 401) { + printf("Invalid user credentials.\n"); + goto cleanup; + } else if (req->status_code != 200 && req->status_code != 304) { + printf("Request failed with status code: %i\n", req->status_code); + goto cleanup; + } + + /* store the bucket id */ + memset(cli_api->file_id, 0x00, sizeof(cli_api->file_id)); + strcpy(cli_api->file_id, (char *)req->file_id); + printf("ID: %s \tName: %s\n", req->file_id, req->file_name); + + queue_next_cmd_req(cli_api); + + cleanup: + free(req); + free(work_req); +} void list_files_callback(uv_work_t *work_req, int status) { @@ -832,6 +861,15 @@ void queue_next_cmd_req(cli_api_t *cli_api) if(cli_api->excp_cmd_resp != NULL) { if (strcmp(cli_api->excp_cmd_resp, cli_api->rcvd_cmd_resp) == 0x00) { if ((cli_api->next_cmd_req != NULL) && + (strcmp(cli_api->next_cmd_req, "get-file-id-req") == 0x00)) { + cli_api->curr_cmd_req = cli_api->next_cmd_req; + cli_api->next_cmd_req = cli_api->final_cmd_req; + cli_api->final_cmd_req = NULL; + cli_api->excp_cmd_resp = "get-file-id-resp"; + + storj_bridge_get_file_id(cli_api->env, cli_api->bucket_id, + cli_api->file_name, cli_api, get_file_id_callback); + } else if ((cli_api->next_cmd_req != NULL) && (strcmp(cli_api->next_cmd_req, "list-files-req") == 0x00)) { cli_api->curr_cmd_req = cli_api->next_cmd_req; cli_api->next_cmd_req = cli_api->final_cmd_req; @@ -1016,7 +1054,6 @@ void queue_next_cmd_req(cli_api_t *cli_api) } } - CLI_API int cli_list_buckets(cli_api_t *cli_api) { cli_api->last_cmd_req = NULL; @@ -1043,6 +1080,16 @@ CLI_API int cli_get_bucket_id(cli_api_t *cli_api) return storj_bridge_get_bucket_id(cli_api->env, cli_api->bucket_name, cli_api, get_bucket_id_callback); } +CLI_API int cli_get_file_id(cli_api_t *cli_api) +{ + int ret = 0x00; + ret = cli_get_bucket_id(cli_api); + cli_api->next_cmd_req = "get-file-id-req"; + cli_api->final_cmd_req = NULL; + + return ret; +} + CLI_API int cli_list_files(cli_api_t *cli_api) { int ret = 0x00; @@ -1066,7 +1113,7 @@ CLI_API int cli_remove_bucket(cli_api_t *cli_api) CLI_API int cli_remove_file(cli_api_t *cli_api) { int ret = 0x00; - ret = cli_list_files(cli_api); + ret = cli_get_file_id(cli_api); cli_api->final_cmd_req = "remove-file-req"; return ret; @@ -1075,7 +1122,7 @@ CLI_API int cli_remove_file(cli_api_t *cli_api) CLI_API int cli_list_mirrors(cli_api_t *cli_api) { int ret = 0x00; - ret = cli_list_files(cli_api); + ret = cli_get_file_id(cli_api); cli_api->final_cmd_req = "list-mirrors-req"; return ret; @@ -1104,7 +1151,7 @@ CLI_API int cli_upload_files(cli_api_t *cli_api) CLI_API int cli_download_file(cli_api_t *cli_api) { int ret = 0x00; - ret = cli_list_files(cli_api); + ret = cli_get_file_id(cli_api); cli_api->final_cmd_req = "download-file-req"; return ret; diff --git a/src/cli_callback.h b/src/cli_callback.h index 68360b3..dffc2e9 100755 --- a/src/cli_callback.h +++ b/src/cli_callback.h @@ -71,6 +71,12 @@ void get_buckets_callback(uv_work_t *work_req, int status); */ void get_bucket_id_callback(uv_work_t *work_req, int status); +/** + * @brief Callback function returning the file id for a given + * file name + */ +void get_file_id_callback(uv_work_t *work_req, int status); + /** * @brief Storj api state machine function */ From 42336cba84b23aec0bfbe5da606992c3cfc7cf7a Mon Sep 17 00:00:00 2001 From: kishore Date: Mon, 12 Feb 2018 13:38:03 -0500 Subject: [PATCH 68/68] updated with review comments: removed cli_api ref, fixed the braces, grouped the linux style commands, updated the uploader file( not sure why it didnt merge when I did the fetch from upstream), but anyhow merged the changes explicitly --- src/cli.c | 46 ++++++++++++++++++++-------------------------- src/cli_callback.c | 22 +++++++++++----------- src/cli_callback.h | 30 ++++++++++-------------------- src/uploader.c | 3 ++- 4 files changed, 43 insertions(+), 58 deletions(-) diff --git a/src/cli.c b/src/cli.c index 39171eb..50a61b4 100644 --- a/src/cli.c +++ b/src/cli.c @@ -37,40 +37,41 @@ extern int errno; #define HELP_TEXT "usage: storj [] []\n\n" \ "These are common Storj commands for various situations:\n\n" \ "setting up users profiles:\n" \ - "=========================\n" \ " register setup a new storj bridge user\n" \ " import-keys import existing user\n" \ " export-keys export bridge user, password and " \ "encryption keys\n\n" \ "working with buckets and files:\n" \ - "==============================\n" \ " list-buckets\n" \ - " ls (lists the available buckets)\n" \ " list-files \n" \ + " remove-file \n" \ + " remove-bucket \n" \ + " add-bucket \n" \ + " list-mirrors \n\n" \ + "uploading files:\n" \ + " upload-file \n\n" \ + "downloading files:\n" \ + " download-file \n\n" \ + "bridge api information:\n" \ + " get-info\n\n" \ + "Linux style CLI command support \n" \ + "===============================\n" \ + "working with buckets and files:\n" \ + " ls (lists the available buckets)\n" \ " ls (lists the files in a bucket)\n" \ " get-bucket-id \n" \ - " remove-file \n" \ " rm (to remove a file from a bucket)\n" \ - " remove-bucket \n" \ " rm (to remove a bucket)\n" \ - " add-bucket \n" \ " mkbkt \n" \ - " list-mirrors \n" \ " lm \n\n" \ "uploading files:\n" \ - "===============\n" \ - " upload-file \n" \ " cp [-rR] " \ "storj:///\n" \ " (e.g. storj cp -[rR] /some-dir/* storj://bucketname/.)\n\n" \ "downloading files:\n" \ - "=================\n" \ - " download-file \n" \ " cp [-rR] storj:/// " \ "\n" \ " (e.g. storj cp -[rR] storj://bucketname/ /some-dir/.)\n\n" \ - "bridge api information\n" \ - " get-info\n\n" \ "options:\n" \ " -h, --help output usage information\n" \ " -v, --version output the version number\n" \ @@ -94,14 +95,12 @@ static int check_file_path(char *file_path) { struct stat sb; - if (stat(file_path, &sb) == -1) - { + if (stat(file_path, &sb) == -1) { perror("stat"); return CLI_NO_SUCH_FILE_OR_DIR; } - switch (sb.st_mode & S_IFMT) - { + switch (sb.st_mode & S_IFMT) { case S_IFBLK: printf("block device\n"); break; @@ -169,23 +168,18 @@ static int validate_cmd_tokenize(char *cmd_str, char *str_token[]) int i = 0x00; /* num of tokens */ int ret = strpos(cmd_str, sub_str); - if( ret == -1) - { + if( ret == -1) { printf("Invalid Command Entry (%d), \ntry ... storj:///\n", ret); } - if (ret == 0x00) - { + if (ret == 0x00) { /* start tokenizing */ str_token[0] = strtok(cmd_str, "/"); - while (str_token[i] != NULL) - { + while (str_token[i] != NULL) { i++; str_token[i] = strtok(NULL, "/"); } - } - else - { + } else { i = ret; } diff --git a/src/cli_callback.c b/src/cli_callback.c index 7714cc2..abcab95 100644 --- a/src/cli_callback.c +++ b/src/cli_callback.c @@ -1054,7 +1054,7 @@ void queue_next_cmd_req(cli_api_t *cli_api) } } -CLI_API int cli_list_buckets(cli_api_t *cli_api) +int cli_list_buckets(cli_api_t *cli_api) { cli_api->last_cmd_req = NULL; cli_api->curr_cmd_req = "get-bucket-id-req"; @@ -1066,7 +1066,7 @@ CLI_API int cli_list_buckets(cli_api_t *cli_api) return storj_bridge_get_buckets(cli_api->env, cli_api, get_buckets_callback); } -CLI_API int cli_get_bucket_id(cli_api_t *cli_api) +int cli_get_bucket_id(cli_api_t *cli_api) { int ret = -1; cli_api->last_cmd_req = NULL; @@ -1080,7 +1080,7 @@ CLI_API int cli_get_bucket_id(cli_api_t *cli_api) return storj_bridge_get_bucket_id(cli_api->env, cli_api->bucket_name, cli_api, get_bucket_id_callback); } -CLI_API int cli_get_file_id(cli_api_t *cli_api) +int cli_get_file_id(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_bucket_id(cli_api); @@ -1090,7 +1090,7 @@ CLI_API int cli_get_file_id(cli_api_t *cli_api) return ret; } -CLI_API int cli_list_files(cli_api_t *cli_api) +int cli_list_files(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_bucket_id(cli_api); @@ -1100,7 +1100,7 @@ CLI_API int cli_list_files(cli_api_t *cli_api) return ret; } -CLI_API int cli_remove_bucket(cli_api_t *cli_api) +int cli_remove_bucket(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_bucket_id(cli_api); @@ -1110,7 +1110,7 @@ CLI_API int cli_remove_bucket(cli_api_t *cli_api) return ret; } -CLI_API int cli_remove_file(cli_api_t *cli_api) +int cli_remove_file(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_file_id(cli_api); @@ -1119,7 +1119,7 @@ CLI_API int cli_remove_file(cli_api_t *cli_api) return ret; } -CLI_API int cli_list_mirrors(cli_api_t *cli_api) +int cli_list_mirrors(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_file_id(cli_api); @@ -1128,7 +1128,7 @@ CLI_API int cli_list_mirrors(cli_api_t *cli_api) return ret; } -CLI_API int cli_upload_file(cli_api_t *cli_api) +int cli_upload_file(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_bucket_id(cli_api); @@ -1138,7 +1138,7 @@ CLI_API int cli_upload_file(cli_api_t *cli_api) return ret; } -CLI_API int cli_upload_files(cli_api_t *cli_api) +int cli_upload_files(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_bucket_id(cli_api); @@ -1148,7 +1148,7 @@ CLI_API int cli_upload_files(cli_api_t *cli_api) return ret; } -CLI_API int cli_download_file(cli_api_t *cli_api) +int cli_download_file(cli_api_t *cli_api) { int ret = 0x00; ret = cli_get_file_id(cli_api); @@ -1157,7 +1157,7 @@ CLI_API int cli_download_file(cli_api_t *cli_api) return ret; } -CLI_API int cli_download_files(cli_api_t *cli_api) +int cli_download_files(cli_api_t *cli_api) { int ret = 0x00; ret = cli_list_files(cli_api); diff --git a/src/cli_callback.h b/src/cli_callback.h index dffc2e9..6f0800b 100755 --- a/src/cli_callback.h +++ b/src/cli_callback.h @@ -15,16 +15,6 @@ extern "C" { #include "storj.h" -#if defined(_WIN32) && defined(STORJDLL) -#if defined(DLL_EXPORT) - #define CLI_API __declspec(dllexport) - #else - #define CLI_API __declspec(dllimport) - #endif -#else -#define CLI_API -#endif - #define CLI_NO_SUCH_FILE_OR_DIR 0x00 #define CLI_VALID_REGULAR_FILE 0x01 #define CLI_VALID_DIR 0x02 @@ -89,7 +79,7 @@ void queue_next_cmd_req(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_list_buckets(cli_api_t *cli_api); +int cli_list_buckets(cli_api_t *cli_api); /** * @brief Function returns the corresponding bucket's id for a @@ -99,7 +89,7 @@ CLI_API int cli_list_buckets(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_get_bucket_id(cli_api_t *cli_api); +int cli_get_bucket_id(cli_api_t *cli_api); /** * @brief Function to list files in a given bucket name @@ -108,7 +98,7 @@ CLI_API int cli_get_bucket_id(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_list_files(cli_api_t *cli_api); +int cli_list_files(cli_api_t *cli_api); /** * @brief Function to remove a given bucket name @@ -117,7 +107,7 @@ CLI_API int cli_list_files(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_remove_bucket(cli_api_t *cli_api); +int cli_remove_bucket(cli_api_t *cli_api); /** * @brief Function to remove a file from a given bucket name @@ -126,7 +116,7 @@ CLI_API int cli_remove_bucket(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_remove_file(cli_api_t *cli_api); +int cli_remove_file(cli_api_t *cli_api); /** * @brief Function to return the node IDs for a given file for a @@ -136,7 +126,7 @@ CLI_API int cli_remove_file(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_list_mirrors(cli_api_t *cli_api); +int cli_list_mirrors(cli_api_t *cli_api); /** * @brief Function to upload a local file into a given bucket @@ -146,7 +136,7 @@ CLI_API int cli_list_mirrors(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_upload_file(cli_api_t *cli_api); +int cli_upload_file(cli_api_t *cli_api); /** * @brief Function to upload local files into a given bucket @@ -156,7 +146,7 @@ CLI_API int cli_upload_file(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_upload_files(cli_api_t *cli_api); +int cli_upload_files(cli_api_t *cli_api); /** * @brief Function to download a file from a given bucket to a @@ -166,7 +156,7 @@ CLI_API int cli_upload_files(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_download_file(cli_api_t *cli_api); +int cli_download_file(cli_api_t *cli_api); /** * @brief Function to download files from a given bucket to a @@ -176,7 +166,7 @@ CLI_API int cli_download_file(cli_api_t *cli_api); * info * @return A non-zero error value on failure and 0 on success. */ -CLI_API int cli_download_files(cli_api_t *cli_api); +int cli_download_files(cli_api_t *cli_api); #ifdef __cplusplus } diff --git a/src/uploader.c b/src/uploader.c index 218a748..ebbea22 100644 --- a/src/uploader.c +++ b/src/uploader.c @@ -370,7 +370,7 @@ static void after_create_bucket_entry(uv_work_t *work, int status) state->info->id = NULL; state->info->bucket_id = state->bucket_id; state->info->decrypted = true; - state->info->index = state->index; + state->info->index = strdup(state->index); struct json_object *file_id_value = NULL; struct json_object *created_value = NULL; @@ -2726,6 +2726,7 @@ STORJ_API void storj_free_uploaded_file_info(storj_file_meta_t *file) free((char *)file->created); free((char *)file->mimetype); free((char *)file->hmac); + free((char *)file->index); } free(file); }