From 0561a922c551481f5801e8ab1e806803dfc52c51 Mon Sep 17 00:00:00 2001 From: josch Date: Wed, 24 Sep 2014 10:48:54 +0200 Subject: [PATCH] mfshell: add new folder command --- CMakeLists.txt | 2 +- mfapi/apicalls/folder_get_info.c | 28 ++++++++ mfapi/folder.c | 53 ++++++++++++++ mfapi/folder.h | 15 ++++ mfshell/commands.h | 3 + mfshell/commands/folder.c | 116 +++++++++++++++++++++++++++++++ mfshell/mfshell.c | 1 + utils/http.c | 4 +- 8 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 mfshell/commands/folder.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 99f03e2..1dffeed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set_target_properties(mfapi PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror") add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c) set_target_properties(mfutils PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror") -add_executable(mediafire-shell mfshell/main.c mfshell/mfshell.c mfshell/commands/auth.c mfshell/commands/chdir.c mfshell/commands/debug.c mfshell/commands/file.c mfshell/commands/get.c mfshell/commands/help.c mfshell/commands/host.c mfshell/commands/lcd.c mfshell/commands/links.c mfshell/commands/list.c mfshell/commands/lpwd.c mfshell/commands/mkdir.c mfshell/commands/pwd.c mfshell/commands/whoami.c mfshell/commands/rmdir.c mfshell/config.c mfshell/options.c) +add_executable(mediafire-shell mfshell/main.c mfshell/mfshell.c mfshell/commands/folder.c mfshell/commands/auth.c mfshell/commands/chdir.c mfshell/commands/debug.c mfshell/commands/file.c mfshell/commands/get.c mfshell/commands/help.c mfshell/commands/host.c mfshell/commands/lcd.c mfshell/commands/links.c mfshell/commands/list.c mfshell/commands/lpwd.c mfshell/commands/mkdir.c mfshell/commands/pwd.c mfshell/commands/whoami.c mfshell/commands/rmdir.c mfshell/config.c mfshell/options.c) set_target_properties(mediafire-shell PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror") target_link_libraries(mediafire-shell curl ssl crypto jansson mfapi mfutils) diff --git a/mfapi/apicalls/folder_get_info.c b/mfapi/apicalls/folder_get_info.c index 04cbb0c..ab69791 100644 --- a/mfapi/apicalls/folder_get_info.c +++ b/mfapi/apicalls/folder_get_info.c @@ -17,10 +17,12 @@ * */ +#define _XOPEN_SOURCE // for strptime #include #include #include #include +#include #include "../../utils/http.h" #include "../../utils/json.h" @@ -74,9 +76,14 @@ static int _decode_folder_get_info(mfhttp * conn, void *data) json_t *node; json_t *folderkey; json_t *folder_name; + json_t *revision; + json_t *epoch; + json_t *created; json_t *parent_folder; int retval = 0; mffolder *folder; + char *ret; + struct tm tm; if (data == NULL) return -1; @@ -103,6 +110,27 @@ static int _decode_folder_get_info(mfhttp * conn, void *data) if (parent_folder == NULL && folderkey != NULL) folder_set_parent(folder, NULL); + revision = json_object_get(node, "revision"); + if (revision != NULL) { + folder_set_revision(folder, atol(json_string_value(revision))); + } + + epoch = json_object_get(node, "epoch"); + if (epoch != NULL) { + folder_set_epoch(folder, atol(json_string_value(epoch))); + } + + created = json_object_get(node, "created"); + if (created != NULL) { + memset(&tm, 0, sizeof(struct tm)); + ret = strptime(json_string_value(created), "%F %T", &tm); + if (ret[0] != '\0') { + fprintf(stderr, "cannot parse time\n"); + } else { + folder_set_created(folder, mktime(&tm)); + } + } + if (folderkey == NULL) retval = -1; diff --git a/mfapi/folder.c b/mfapi/folder.c index d19d2c5..be7cbb8 100644 --- a/mfapi/folder.c +++ b/mfapi/folder.c @@ -30,6 +30,8 @@ struct mffolder { uint64_t revision; uint32_t folder_count; uint32_t file_count; + time_t epoch; + time_t created; }; mffolder *folder_alloc(void) @@ -131,3 +133,54 @@ const char *folder_get_name(mffolder * folder) return folder->name; } + +int folder_set_revision(mffolder * folder, uint64_t revision) +{ + if (folder == NULL) + return -1; + + folder->revision = revision; + return 0; +} + +uint64_t folder_get_revision(mffolder * folder) +{ + if (folder == NULL) + return -1; + + return folder->revision; +} + +int folder_set_epoch(mffolder * folder, time_t epoch) +{ + if (folder == NULL) + return -1; + + folder->epoch = epoch; + return 0; +} + +time_t folder_get_epoch(mffolder * folder) +{ + if (folder == NULL) + return -1; + + return folder->epoch; +} + +int folder_set_created(mffolder * folder, time_t created) +{ + if (folder == NULL) + return -1; + + folder->created = created; + return 0; +} + +time_t folder_get_created(mffolder * folder) +{ + if (folder == NULL) + return -1; + + return folder->created; +} diff --git a/mfapi/folder.h b/mfapi/folder.h index 315af60..e921bc2 100644 --- a/mfapi/folder.h +++ b/mfapi/folder.h @@ -20,6 +20,9 @@ #ifndef __MFAPI_FOLDER_H__ #define __MFAPI_FOLDER_H__ +#include +#include + typedef struct mffolder mffolder; mffolder *folder_alloc(void); @@ -38,4 +41,16 @@ int folder_set_name(mffolder * folder, const char *name); const char *folder_get_name(mffolder * folder); +int folder_set_revision(mffolder * folder, uint64_t revision); + +uint64_t folder_get_revision(mffolder * folder); + +int folder_set_epoch(mffolder * folder, time_t epoch); + +time_t folder_get_epoch(mffolder * folder); + +int folder_set_created(mffolder * folder, time_t created); + +time_t folder_get_created(mffolder * folder); + #endif diff --git a/mfshell/commands.h b/mfshell/commands.h index 5c96aef..ab49f74 100644 --- a/mfshell/commands.h +++ b/mfshell/commands.h @@ -55,6 +55,9 @@ int mfshell_cmd_lcd(mfshell * mfshell, int argc, int mfshell_cmd_file(mfshell * mfshell, int argc, char *const argv[]); +int mfshell_cmd_folder(mfshell * mfshell, int argc, + char *const argv[]); + int mfshell_cmd_links(mfshell * mfshell, int argc, char *const argv[]); diff --git a/mfshell/commands/folder.c b/mfshell/commands/folder.c new file mode 100644 index 0000000..eebde24 --- /dev/null +++ b/mfshell/commands/folder.c @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2013 Bryan Christ + * 2014 Johannes Schauer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2, as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "../../mfapi/apicalls.h" +#include "../mfshell.h" +#include "../../mfapi/folder.h" +#include "../../mfapi/mfconn.h" +#include "../commands.h" // IWYU pragma: keep + +#define max_time_len 20 + +int mfshell_cmd_folder(mfshell * mfshell, int argc, char *const argv[]) +{ + mffolder *folder; + int retval; + const char *folderkey; + const char *name; + const char *parent; + uint32_t revision; + time_t epoch; + time_t created; + char ftime[max_time_len]; + size_t ftime_ret; + struct tm tm; + + if (mfshell == NULL) { + fprintf(stderr, "Zero shell\n"); + return -1; + } + + switch (argc) { + case 1: + folderkey = NULL; + break; + case 2: + folderkey = argv[1]; + break; + default: + fprintf(stderr, "Invalid number of arguments\n"); + return -1; + } + + if (folderkey != NULL && strlen(folderkey) != 13) { + fprintf(stderr, "Invalid folderkey length\n"); + return -1; + } + + folder = folder_alloc(); + + retval = mfconn_api_folder_get_info(mfshell->conn, folder, folderkey); + if (retval != 0) { + fprintf(stderr, "api call unsuccessful\n"); + } + mfconn_update_secret_key(mfshell->conn); + + folderkey = folder_get_key(folder); + name = folder_get_name(folder); + parent = folder_get_parent(folder); + revision = folder_get_revision(folder); + epoch = folder_get_epoch(folder); + created = folder_get_created(folder); + + if (name != NULL && name[0] != '\0') + printf(" %-15.15s %s\n\r", "foldername:", name); + + if (folderkey != NULL && folderkey[0] != '\0') + printf(" %-15.15s %s\n\r", "folderkey:", folderkey); + + if (parent != NULL && parent[0] != '\0') + printf(" %-15.15s %s\n\r", "parent:", parent); + + if (revision != 0) + printf(" %-15.15s %"PRIu32"\n\r", "revision:", revision); + + if (epoch != 0) { + memset(&tm, 0, sizeof(struct tm)); + gmtime_r(&epoch, &tm); + // print ISO-8601 date followed by 24-hour time + ftime_ret = strftime(ftime, max_time_len, "%F %T", &tm); + printf(" %-15.15s %s\n\r", "epoch:", ftime); + } + + if (created != 0) { + gmtime_r(&created, &tm); + // print ISO-8601 date followed by 24-hour time + ftime_ret = strftime(ftime, max_time_len, "%F %T", &tm); + printf(" %-15.15s %s\n\r", "created:", ftime); + } + + folder_free(folder); + + return 0; +} diff --git a/mfshell/mfshell.c b/mfshell/mfshell.c index 9fff605..295d87e 100644 --- a/mfshell/mfshell.c +++ b/mfshell/mfshell.c @@ -101,6 +101,7 @@ int mfshell_exec(mfshell * shell, int argc, char *const argv[]) return curr_cmd->handler(shell, argc, argv); } } + fprintf(stderr, "Invalid command: %s", argv[0]); return -1; } diff --git a/utils/http.c b/utils/http.c index d664705..9e29090 100644 --- a/utils/http.c +++ b/utils/http.c @@ -128,7 +128,7 @@ http_get_buf(mfhttp * conn, const char *url, curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_buf_cb); curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn); - //fprintf(stderr, "GET: %s\n", url); + fprintf(stderr, "GET: %s\n", url); retval = curl_easy_perform(conn->curl_handle); if (retval != CURLE_OK) { fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf); @@ -172,7 +172,7 @@ http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr) data_len = size * nmemb; if (data_len > 0) { - //fwrite(data, size, nmemb, stderr); + fwrite(data, size, nmemb, stderr); conn->write_buf = (char *)realloc(conn->write_buf, conn->write_buf_len + data_len); memcpy(conn->write_buf + conn->write_buf_len, data, data_len);