mfshell: add new folder command

This commit is contained in:
josch
2014-09-24 10:48:54 +02:00
parent 18db8742dd
commit 0561a922c5
8 changed files with 219 additions and 3 deletions

View File

@@ -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)

View File

@@ -17,10 +17,12 @@
*
*/
#define _XOPEN_SOURCE // for strptime
#include <jansson.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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;

View File

@@ -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;
}

View File

@@ -20,6 +20,9 @@
#ifndef __MFAPI_FOLDER_H__
#define __MFAPI_FOLDER_H__
#include <stdint.h>
#include <time.h>
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

View File

@@ -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[]);

116
mfshell/commands/folder.c Normal file
View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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 <stdio.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include <stdint.h>
#include <stddef.h>
#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;
}

View File

@@ -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;
}

View File

@@ -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);