mirror of
https://github.com/xorgy/mediafire-fuse
synced 2026-01-13 13:14:29 -08:00
use gnu indent to enforce coding style and adapt source
- indent options are listed in ./.indent.pro - use test case to run indent
This commit is contained in:
33
.indent.pro
vendored
Normal file
33
.indent.pro
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
--blank-lines-after-declarations
|
||||||
|
--blank-lines-after-procedures
|
||||||
|
--blank-lines-after-commas
|
||||||
|
--break-before-boolean-operator
|
||||||
|
--honour-newlines
|
||||||
|
--braces-on-if-line
|
||||||
|
--braces-on-struct-decl-line
|
||||||
|
--comment-indentation33
|
||||||
|
--declaration-comment-column33
|
||||||
|
--no-comment-delimiters-on-blank-lines
|
||||||
|
--cuddle-else
|
||||||
|
--continuation-indentation4
|
||||||
|
--case-indentation4
|
||||||
|
--line-comments-indentation0
|
||||||
|
--declaration-indentation16
|
||||||
|
--dont-format-first-column-comments
|
||||||
|
--indent-level4
|
||||||
|
--parameter-indentation0
|
||||||
|
--line-length80
|
||||||
|
--continue-at-parentheses
|
||||||
|
--no-space-after-function-call-names
|
||||||
|
--no-space-after-parentheses
|
||||||
|
--dont-break-procedure-type
|
||||||
|
--space-after-if
|
||||||
|
--space-after-for
|
||||||
|
--space-after-while
|
||||||
|
--no-space-after-casts
|
||||||
|
--swallow-optional-blank-lines
|
||||||
|
--dont-format-comments
|
||||||
|
--else-endif-column33
|
||||||
|
--space-special-semicolon
|
||||||
|
--indent-label1
|
||||||
|
--no-tabs
|
||||||
@@ -19,3 +19,4 @@ target_link_libraries(mfshell curl ssl crypto jansson mfapi mfutils)
|
|||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
add_test(iwyu ../tests/iwyu.py)
|
add_test(iwyu ../tests/iwyu.py)
|
||||||
|
add_test(indent ../tests/indent.sh)
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef _MFAPI_APICALLS_H_
|
#ifndef _MFAPI_APICALLS_H_
|
||||||
#define _MFAPI_APICALLS_H_
|
#define _MFAPI_APICALLS_H_
|
||||||
|
|
||||||
@@ -27,20 +26,29 @@
|
|||||||
#include "folder.h"
|
#include "folder.h"
|
||||||
#include "mfconn.h"
|
#include "mfconn.h"
|
||||||
|
|
||||||
int mfconn_api_file_get_info(mfconn *conn, mffile *file, char *quickkey);
|
int mfconn_api_file_get_info(mfconn * conn, mffile * file,
|
||||||
|
char *quickkey);
|
||||||
|
|
||||||
int mfconn_api_file_get_links(mfconn *conn, mffile *file, char *quickkey);
|
int mfconn_api_file_get_links(mfconn * conn, mffile * file,
|
||||||
|
char *quickkey);
|
||||||
|
|
||||||
int mfconn_api_folder_create(mfconn *conn, char *parent, char *name);
|
int mfconn_api_folder_create(mfconn * conn, char *parent,
|
||||||
|
char *name);
|
||||||
|
|
||||||
long mfconn_api_folder_get_content(mfconn *conn, int mode, mffolder *folder_curr);
|
long mfconn_api_folder_get_content(mfconn * conn, int mode,
|
||||||
|
mffolder * folder_curr);
|
||||||
|
|
||||||
int mfconn_api_folder_get_info(mfconn *conn, mffolder *folder, char *folderkey);
|
int mfconn_api_folder_get_info(mfconn * conn, mffolder * folder,
|
||||||
|
char *folderkey);
|
||||||
|
|
||||||
int mfconn_api_user_get_info(mfconn *conn);
|
int mfconn_api_user_get_info(mfconn * conn);
|
||||||
|
|
||||||
int mfconn_api_user_get_session_token(mfconn *conn, char *server,
|
int mfconn_api_user_get_session_token(mfconn * conn, char *server,
|
||||||
char *username, char *password, int app_id, char *app_key,
|
char *username,
|
||||||
uint32_t *secret_key, char **secret_time, char **session_token);
|
char *password, int app_id,
|
||||||
|
char *app_key,
|
||||||
|
uint32_t * secret_key,
|
||||||
|
char **secret_time,
|
||||||
|
char **session_token);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -26,76 +25,80 @@
|
|||||||
#include "../../utils/json.h"
|
#include "../../utils/json.h"
|
||||||
#include "../mfconn.h"
|
#include "../mfconn.h"
|
||||||
#include "../file.h"
|
#include "../file.h"
|
||||||
#include "../apicalls.h" // IWYU pragma: keep
|
#include "../apicalls.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static int
|
static int _decode_file_get_info(mfhttp * conn, void *data);
|
||||||
_decode_file_get_info(mfhttp *conn, void *data);
|
|
||||||
|
|
||||||
int
|
int mfconn_api_file_get_info(mfconn * conn, mffile * file, char *quickkey)
|
||||||
mfconn_api_file_get_info(mfconn *conn,mffile *file,char *quickkey)
|
|
||||||
{
|
{
|
||||||
char *api_call;
|
char *api_call;
|
||||||
int retval;
|
int retval;
|
||||||
int len;
|
int len;
|
||||||
|
mfhttp *http;
|
||||||
|
|
||||||
if(conn == NULL) return -1;
|
if (conn == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(quickkey == NULL) return -1;
|
return -1;
|
||||||
|
if (quickkey == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
len = strlen(quickkey);
|
len = strlen(quickkey);
|
||||||
|
|
||||||
// key must either be 11 or 15 chars
|
// key must either be 11 or 15 chars
|
||||||
if(len != 11 && len != 15) return -1;
|
if (len != 11 && len != 15)
|
||||||
|
return -1;
|
||||||
|
|
||||||
api_call = mfconn_create_signed_get(conn, 1, "file/get_info.php",
|
api_call = mfconn_create_signed_get(conn, 1, "file/get_info.php",
|
||||||
"?quick_key=%s&response_format=json", quickkey);
|
"?quick_key=%s&response_format=json",
|
||||||
|
quickkey);
|
||||||
|
|
||||||
mfhttp *http = http_create();
|
http = http_create();
|
||||||
retval = http_get_buf(http, api_call, _decode_file_get_info, file);
|
retval = http_get_buf(http, api_call, _decode_file_get_info, file);
|
||||||
http_destroy(http);
|
http_destroy(http);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int _decode_file_get_info(mfhttp * conn, void *data)
|
||||||
_decode_file_get_info(mfhttp *conn, void *data)
|
|
||||||
{
|
{
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t *root;
|
json_t *root;
|
||||||
json_t *node;
|
json_t *node;
|
||||||
json_t *quickkey;
|
json_t *quickkey;
|
||||||
json_t *file_hash;
|
json_t *file_hash;
|
||||||
json_t *file_name;
|
json_t *file_name;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
mffile *file;
|
mffile *file;
|
||||||
|
|
||||||
if(data == NULL) return -1;
|
if (data == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
file = (mffile *)data;
|
file = (mffile *) data;
|
||||||
|
|
||||||
root = http_parse_buf_json(conn, 0, &error);
|
root = http_parse_buf_json(conn, 0, &error);
|
||||||
|
|
||||||
node = json_object_by_path(root,"response/file_info");
|
node = json_object_by_path(root, "response/file_info");
|
||||||
|
|
||||||
quickkey = json_object_get(node,"quickkey");
|
quickkey = json_object_get(node, "quickkey");
|
||||||
if(quickkey != NULL)
|
if (quickkey != NULL)
|
||||||
file_set_key(file,(char*)json_string_value(quickkey));
|
file_set_key(file, (char *)json_string_value(quickkey));
|
||||||
|
|
||||||
file_name = json_object_get(node,"filename");
|
file_name = json_object_get(node, "filename");
|
||||||
if(file_name != NULL)
|
if (file_name != NULL)
|
||||||
file_set_name(file,(char*)json_string_value(file_name));
|
file_set_name(file, (char *)json_string_value(file_name));
|
||||||
|
|
||||||
file_hash = json_object_get(node,"hash");
|
file_hash = json_object_get(node, "hash");
|
||||||
if(file_hash != NULL)
|
if (file_hash != NULL) {
|
||||||
{
|
file_set_hash(file, (char *)json_string_value(file_hash));
|
||||||
file_set_hash(file,(char*)json_string_value(file_hash));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(quickkey == NULL) retval = -1;
|
if (quickkey == NULL)
|
||||||
|
retval = -1;
|
||||||
|
|
||||||
if(root != NULL) json_decref(root);
|
if (root != NULL)
|
||||||
|
json_decref(root);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -26,95 +25,95 @@
|
|||||||
#include "../../utils/json.h"
|
#include "../../utils/json.h"
|
||||||
#include "../mfconn.h"
|
#include "../mfconn.h"
|
||||||
#include "../file.h"
|
#include "../file.h"
|
||||||
#include "../apicalls.h" // IWYU pragma: keep
|
#include "../apicalls.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static int
|
static int _decode_file_get_links(mfhttp * conn, void *data);
|
||||||
_decode_file_get_links(mfhttp *conn, void *data);
|
|
||||||
|
|
||||||
int
|
int mfconn_api_file_get_links(mfconn * conn, mffile * file, char *quickkey)
|
||||||
mfconn_api_file_get_links(mfconn *conn,mffile *file,char *quickkey)
|
|
||||||
{
|
{
|
||||||
char *api_call;
|
char *api_call;
|
||||||
int retval;
|
int retval;
|
||||||
int len;
|
int len;
|
||||||
|
mfhttp *http;
|
||||||
|
|
||||||
if(conn == NULL) return -1;
|
if (conn == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(quickkey == NULL) return -1;
|
return -1;
|
||||||
|
if (quickkey == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
len = strlen(quickkey);
|
len = strlen(quickkey);
|
||||||
|
|
||||||
// key must either be 11 or 15 chars
|
// key must either be 11 or 15 chars
|
||||||
if(len != 11 && len != 15) return -1;
|
if (len != 11 && len != 15)
|
||||||
|
return -1;
|
||||||
|
|
||||||
api_call = mfconn_create_signed_get(conn,0,"file/get_links.php",
|
api_call = mfconn_create_signed_get(conn, 0, "file/get_links.php",
|
||||||
"?quick_key=%s&response_format=json", quickkey);
|
"?quick_key=%s&response_format=json",
|
||||||
|
quickkey);
|
||||||
|
|
||||||
mfhttp *http = http_create();
|
http = http_create();
|
||||||
retval = http_get_buf(http, api_call, _decode_file_get_links, file);
|
retval = http_get_buf(http, api_call, _decode_file_get_links, file);
|
||||||
http_destroy(http);
|
http_destroy(http);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int _decode_file_get_links(mfhttp * conn, void *data)
|
||||||
_decode_file_get_links(mfhttp *conn, void *data)
|
|
||||||
{
|
{
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t *root;
|
json_t *root;
|
||||||
json_t *node;
|
json_t *node;
|
||||||
json_t *quickkey;
|
json_t *quickkey;
|
||||||
json_t *share_link;
|
json_t *share_link;
|
||||||
json_t *direct_link;
|
json_t *direct_link;
|
||||||
json_t *onetime_link;
|
json_t *onetime_link;
|
||||||
json_t *links_array;
|
json_t *links_array;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
mffile *file;
|
mffile *file;
|
||||||
|
|
||||||
if(data == NULL) return -1;
|
if (data == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
file = (mffile *)data;
|
file = (mffile *) data;
|
||||||
|
|
||||||
root = http_parse_buf_json(conn, 0, &error);
|
root = http_parse_buf_json(conn, 0, &error);
|
||||||
|
|
||||||
node = json_object_by_path(root,"response");
|
node = json_object_by_path(root, "response");
|
||||||
|
|
||||||
links_array = json_object_get(node,"links");
|
links_array = json_object_get(node, "links");
|
||||||
if(!json_is_array(links_array))
|
if (!json_is_array(links_array)) {
|
||||||
{
|
|
||||||
json_decref(root);
|
json_decref(root);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// just get the first one. maybe later support multi-quickkey
|
// just get the first one. maybe later support multi-quickkey
|
||||||
node = json_array_get(links_array,0);
|
node = json_array_get(links_array, 0);
|
||||||
|
|
||||||
quickkey = json_object_get(node,"quickkey");
|
quickkey = json_object_get(node, "quickkey");
|
||||||
if(quickkey != NULL)
|
if (quickkey != NULL)
|
||||||
file_set_key(file,(char*)json_string_value(quickkey));
|
file_set_key(file, (char *)json_string_value(quickkey));
|
||||||
|
|
||||||
share_link = json_object_get(node,"normal_download");
|
share_link = json_object_get(node, "normal_download");
|
||||||
if(share_link != NULL)
|
if (share_link != NULL)
|
||||||
file_set_share_link(file,(char*)json_string_value(share_link));
|
file_set_share_link(file, (char *)json_string_value(share_link));
|
||||||
|
|
||||||
direct_link = json_object_get(node,"direct_download");
|
direct_link = json_object_get(node, "direct_download");
|
||||||
if(direct_link != NULL)
|
if (direct_link != NULL) {
|
||||||
{
|
file_set_direct_link(file, (char *)json_string_value(direct_link));
|
||||||
file_set_direct_link(file,(char*)json_string_value(direct_link));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onetime_link = json_object_get(node,"one_time_download");
|
onetime_link = json_object_get(node, "one_time_download");
|
||||||
if(onetime_link != NULL)
|
if (onetime_link != NULL) {
|
||||||
{
|
file_set_onetime_link(file, (char *)json_string_value(onetime_link));
|
||||||
file_set_onetime_link(file,(char*)json_string_value(onetime_link));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if this is false something went horribly wrong
|
// if this is false something went horribly wrong
|
||||||
if(share_link == NULL) retval = -1;
|
if (share_link == NULL)
|
||||||
|
retval = -1;
|
||||||
|
|
||||||
if(root != NULL) json_decref(root);
|
if (root != NULL)
|
||||||
|
json_decref(root);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,53 +17,51 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "../../utils/http.h"
|
#include "../../utils/http.h"
|
||||||
#include "../mfconn.h"
|
#include "../mfconn.h"
|
||||||
#include "../apicalls.h" // IWYU pragma: keep
|
#include "../apicalls.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfconn_api_folder_create(mfconn * conn, char *parent, char *name)
|
||||||
mfconn_api_folder_create(mfconn *conn,char *parent,char *name)
|
|
||||||
{
|
{
|
||||||
char *api_call;
|
char *api_call;
|
||||||
int retval;
|
int retval;
|
||||||
|
mfhttp *http;
|
||||||
|
|
||||||
if(conn == NULL) return -1;
|
if (conn == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(name == NULL) return -1;
|
if (name == NULL)
|
||||||
if(strlen(name) < 1) return -1;
|
return -1;
|
||||||
|
if (strlen(name) < 1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// key must either be 11 chars or "myfiles"
|
// key must either be 11 chars or "myfiles"
|
||||||
if(parent != NULL)
|
if (parent != NULL) {
|
||||||
{
|
if (strlen(parent) != 13) {
|
||||||
if(strlen(parent) != 13)
|
|
||||||
{
|
|
||||||
// if it is myfiles, set paret to NULL
|
// if it is myfiles, set paret to NULL
|
||||||
if(strcmp(parent,"myfiles") == 0) parent = NULL;
|
if (strcmp(parent, "myfiles") == 0)
|
||||||
|
parent = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(parent != NULL)
|
if (parent != NULL) {
|
||||||
{
|
api_call =
|
||||||
api_call = mfconn_create_signed_get(conn,0,"folder/create.php",
|
mfconn_create_signed_get(conn, 0, "folder/create.php",
|
||||||
"?parent_key=%s"
|
"?parent_key=%s" "&foldername=%s"
|
||||||
"&foldername=%s"
|
"&response_format=json", parent, name);
|
||||||
"&response_format=json",
|
} else {
|
||||||
parent,name);
|
api_call =
|
||||||
}
|
mfconn_create_signed_get(conn, 0, "folder/create.php",
|
||||||
else
|
"?foldername=%s&response_format=json",
|
||||||
{
|
name);
|
||||||
api_call = mfconn_create_signed_get(conn,0,"folder/create.php",
|
|
||||||
"?foldername=%s&response_format=json", name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mfhttp *http = http_create();
|
http = http_create();
|
||||||
retval = http_get_buf(http, api_call, NULL, NULL);
|
retval = http_get_buf(http, api_call, NULL, NULL);
|
||||||
http_destroy(http);
|
http_destroy(http);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -27,67 +26,69 @@
|
|||||||
#include "../../utils/strings.h"
|
#include "../../utils/strings.h"
|
||||||
#include "../folder.h"
|
#include "../folder.h"
|
||||||
#include "../mfconn.h"
|
#include "../mfconn.h"
|
||||||
#include "../apicalls.h" // IWYU pragma: keep
|
#include "../apicalls.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static int
|
static int _decode_folder_get_content_folders(mfhttp * conn, void *data);
|
||||||
_decode_folder_get_content_folders(mfhttp *conn, void *data);
|
|
||||||
|
|
||||||
static int
|
static int _decode_folder_get_content_files(mfhttp * conn, void *data);
|
||||||
_decode_folder_get_content_files(mfhttp *conn, void *data);
|
|
||||||
|
|
||||||
long
|
long
|
||||||
mfconn_api_folder_get_content(mfconn *conn, int mode, mffolder *folder_curr)
|
mfconn_api_folder_get_content(mfconn * conn, int mode, mffolder * folder_curr)
|
||||||
{
|
{
|
||||||
char *api_call;
|
char *api_call;
|
||||||
int retval;
|
int retval;
|
||||||
char *content_type;
|
char *content_type;
|
||||||
|
mfhttp *http;
|
||||||
|
const char *folderkey;
|
||||||
|
|
||||||
if(conn == NULL) return -1;
|
if (conn == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(mode == 0)
|
if (mode == 0)
|
||||||
content_type = "folders";
|
content_type = "folders";
|
||||||
else
|
else
|
||||||
content_type = "files";
|
content_type = "files";
|
||||||
|
|
||||||
const char *folderkey = folder_get_key(folder_curr);
|
folderkey = folder_get_key(folder_curr);
|
||||||
if (folderkey == NULL) {
|
if (folderkey == NULL) {
|
||||||
fprintf(stderr, "folder_get_key NULL\n");
|
fprintf(stderr, "folder_get_key NULL\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/*if (folderkey[0] == '\0') {
|
/*if (folderkey[0] == '\0') {
|
||||||
fprintf(stderr, "folder_get_key '\\0'\n");
|
fprintf(stderr, "folder_get_key '\\0'\n");
|
||||||
return 0;
|
return 0;
|
||||||
}*/
|
} */
|
||||||
api_call = mfconn_create_signed_get(conn,0,"folder/get_content.php",
|
api_call = mfconn_create_signed_get(conn, 0, "folder/get_content.php",
|
||||||
"?folder_key=%s"
|
"?folder_key=%s"
|
||||||
"&content_type=%s"
|
"&content_type=%s"
|
||||||
"&response_format=json",
|
"&response_format=json",
|
||||||
folderkey,
|
folderkey, content_type);
|
||||||
content_type);
|
|
||||||
|
|
||||||
mfhttp* http = http_create();
|
http = http_create();
|
||||||
|
if (mode == 0)
|
||||||
if(mode == 0)
|
retval =
|
||||||
retval = http_get_buf(http, api_call, _decode_folder_get_content_folders, NULL);
|
http_get_buf(http, api_call,
|
||||||
|
_decode_folder_get_content_folders, NULL);
|
||||||
else
|
else
|
||||||
retval = http_get_buf(http, api_call, _decode_folder_get_content_files, NULL);
|
retval =
|
||||||
|
http_get_buf(http, api_call,
|
||||||
|
_decode_folder_get_content_files, NULL);
|
||||||
http_destroy(http);
|
http_destroy(http);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int _decode_folder_get_content_folders(mfhttp * conn, void *user_ptr)
|
||||||
_decode_folder_get_content_folders(mfhttp *conn, void *user_ptr)
|
|
||||||
{
|
{
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t *root;
|
json_t *root;
|
||||||
json_t *node;
|
json_t *node;
|
||||||
json_t *data;
|
json_t *data;
|
||||||
|
|
||||||
json_t *folders_array;
|
json_t *folders_array;
|
||||||
json_t *folderkey;
|
json_t *folderkey;
|
||||||
json_t *folder_name;
|
json_t *folder_name;
|
||||||
char *folder_name_tmp;
|
char *folder_name_tmp;
|
||||||
|
|
||||||
int array_sz;
|
int array_sz;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -98,58 +99,53 @@ _decode_folder_get_content_folders(mfhttp *conn, void *user_ptr)
|
|||||||
root = http_parse_buf_json(conn, 0, &error);
|
root = http_parse_buf_json(conn, 0, &error);
|
||||||
|
|
||||||
/*json_t *result = json_object_by_path(root, "response/action");
|
/*json_t *result = json_object_by_path(root, "response/action");
|
||||||
fprintf(stderr, "response/action: %s\n", (char*)json_string_value(result));*/
|
fprintf(stderr, "response/action: %s\n", (char*)json_string_value(result)); */
|
||||||
|
|
||||||
node = json_object_by_path(root,"response/folder_content");
|
node = json_object_by_path(root, "response/folder_content");
|
||||||
|
|
||||||
folders_array = json_object_get(node,"folders");
|
folders_array = json_object_get(node, "folders");
|
||||||
if(!json_is_array(folders_array))
|
if (!json_is_array(folders_array)) {
|
||||||
{
|
|
||||||
json_decref(root);
|
json_decref(root);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
array_sz = json_array_size(folders_array);
|
array_sz = json_array_size(folders_array);
|
||||||
for(i = 0;i < array_sz;i++)
|
for (i = 0; i < array_sz; i++) {
|
||||||
{
|
data = json_array_get(folders_array, i);
|
||||||
data = json_array_get(folders_array,i);
|
|
||||||
|
|
||||||
if(json_is_object(data))
|
if (json_is_object(data)) {
|
||||||
{
|
folderkey = json_object_get(data, "folderkey");
|
||||||
folderkey = json_object_get(data,"folderkey");
|
|
||||||
|
|
||||||
folder_name = json_object_get(data,"name");
|
folder_name = json_object_get(data, "name");
|
||||||
|
|
||||||
if(folderkey != NULL && folder_name != NULL)
|
if (folderkey != NULL && folder_name != NULL) {
|
||||||
{
|
|
||||||
folder_name_tmp = strdup_printf("< %s >",
|
folder_name_tmp = strdup_printf("< %s >",
|
||||||
json_string_value(folder_name));
|
json_string_value(folder_name));
|
||||||
|
|
||||||
printf(" %-15.13s %s\n\r",
|
printf(" %-15.13s %s\n\r",
|
||||||
json_string_value(folderkey),
|
json_string_value(folderkey), folder_name_tmp);
|
||||||
folder_name_tmp);
|
|
||||||
|
|
||||||
free(folder_name_tmp);
|
free(folder_name_tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(root != NULL) json_decref(root);
|
if (root != NULL)
|
||||||
|
json_decref(root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int _decode_folder_get_content_files(mfhttp * conn, void *user_ptr)
|
||||||
_decode_folder_get_content_files(mfhttp *conn, void *user_ptr)
|
|
||||||
{
|
{
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t *root;
|
json_t *root;
|
||||||
json_t *node;
|
json_t *node;
|
||||||
json_t *data;
|
json_t *data;
|
||||||
|
|
||||||
json_t *files_array;
|
json_t *files_array;
|
||||||
json_t *quickkey;
|
json_t *quickkey;
|
||||||
json_t *file_name;
|
json_t *file_name;
|
||||||
int array_sz;
|
int array_sz;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
@@ -158,36 +154,33 @@ _decode_folder_get_content_files(mfhttp *conn, void *user_ptr)
|
|||||||
|
|
||||||
root = http_parse_buf_json(conn, 0, &error);
|
root = http_parse_buf_json(conn, 0, &error);
|
||||||
|
|
||||||
node = json_object_by_path(root,"response/folder_content");
|
node = json_object_by_path(root, "response/folder_content");
|
||||||
|
|
||||||
files_array = json_object_get(node,"files");
|
files_array = json_object_get(node, "files");
|
||||||
if(!json_is_array(files_array))
|
if (!json_is_array(files_array)) {
|
||||||
{
|
|
||||||
json_decref(root);
|
json_decref(root);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
array_sz = json_array_size(files_array);
|
array_sz = json_array_size(files_array);
|
||||||
for(i = 0;i < array_sz;i++)
|
for (i = 0; i < array_sz; i++) {
|
||||||
{
|
data = json_array_get(files_array, i);
|
||||||
data = json_array_get(files_array,i);
|
|
||||||
|
|
||||||
if(json_is_object(data))
|
if (json_is_object(data)) {
|
||||||
{
|
quickkey = json_object_get(data, "quickkey");
|
||||||
quickkey = json_object_get(data,"quickkey");
|
|
||||||
|
|
||||||
file_name = json_object_get(data,"filename");
|
file_name = json_object_get(data, "filename");
|
||||||
|
|
||||||
if(quickkey != NULL && file_name != NULL)
|
if (quickkey != NULL && file_name != NULL) {
|
||||||
{
|
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r",
|
||||||
json_string_value(quickkey),
|
json_string_value(quickkey),
|
||||||
json_string_value(file_name));
|
json_string_value(file_name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(root != NULL) json_decref(root);
|
if (root != NULL)
|
||||||
|
json_decref(root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -26,79 +25,83 @@
|
|||||||
#include "../../utils/json.h"
|
#include "../../utils/json.h"
|
||||||
#include "../folder.h"
|
#include "../folder.h"
|
||||||
#include "../mfconn.h"
|
#include "../mfconn.h"
|
||||||
#include "../apicalls.h" // IWYU pragma: keep
|
#include "../apicalls.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static int
|
static int _decode_folder_get_info(mfhttp * conn, void *data);
|
||||||
_decode_folder_get_info(mfhttp *conn, void *data);
|
|
||||||
|
|
||||||
int
|
int
|
||||||
mfconn_api_folder_get_info(mfconn *conn,mffolder *folder,char *folderkey)
|
mfconn_api_folder_get_info(mfconn * conn, mffolder * folder, char *folderkey)
|
||||||
{
|
{
|
||||||
char *api_call;
|
char *api_call;
|
||||||
int retval;
|
int retval;
|
||||||
|
mfhttp *http;
|
||||||
|
|
||||||
if(conn == NULL) return -1;
|
if (conn == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(folder == NULL) return -1;
|
if (folder == NULL)
|
||||||
if(folderkey == NULL) return -1;
|
return -1;
|
||||||
|
if (folderkey == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// key must either be 11 chars or "myfiles"
|
// key must either be 11 chars or "myfiles"
|
||||||
if(strlen(folderkey) != 13)
|
if (strlen(folderkey) != 13) {
|
||||||
{
|
if (strcmp(folderkey, "myfiles") == 0)
|
||||||
if(strcmp(folderkey,"myfiles") == 0) return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
api_call = mfconn_create_signed_get(conn,0,"folder/get_info.php",
|
api_call = mfconn_create_signed_get(conn, 0, "folder/get_info.php",
|
||||||
"?folder_key=%s&response_format=json", folderkey);
|
"?folder_key=%s&response_format=json",
|
||||||
|
folderkey);
|
||||||
|
|
||||||
mfhttp *http = http_create();
|
http = http_create();
|
||||||
retval = http_get_buf(http, api_call, _decode_folder_get_info, folder);
|
retval = http_get_buf(http, api_call, _decode_folder_get_info, folder);
|
||||||
http_destroy(http);
|
http_destroy(http);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int _decode_folder_get_info(mfhttp * conn, void *data)
|
||||||
_decode_folder_get_info(mfhttp *conn, void *data)
|
|
||||||
{
|
{
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t *root;
|
json_t *root;
|
||||||
json_t *node;
|
json_t *node;
|
||||||
json_t *folderkey;
|
json_t *folderkey;
|
||||||
json_t *folder_name;
|
json_t *folder_name;
|
||||||
json_t *parent_folder;
|
json_t *parent_folder;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
mffolder *folder;
|
mffolder *folder;
|
||||||
|
|
||||||
if(data == NULL) return -1;
|
if (data == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
folder = (mffolder *)data;
|
folder = (mffolder *) data;
|
||||||
|
|
||||||
root = http_parse_buf_json(conn, 0, &error);
|
root = http_parse_buf_json(conn, 0, &error);
|
||||||
|
|
||||||
node = json_object_by_path(root,"response/folder_info");
|
node = json_object_by_path(root, "response/folder_info");
|
||||||
|
|
||||||
folderkey = json_object_get(node,"folderkey");
|
folderkey = json_object_get(node, "folderkey");
|
||||||
if(folderkey != NULL)
|
if (folderkey != NULL)
|
||||||
folder_set_key(folder,(char*)json_string_value(folderkey));
|
folder_set_key(folder, (char *)json_string_value(folderkey));
|
||||||
|
|
||||||
folder_name = json_object_get(node,"name");
|
folder_name = json_object_get(node, "name");
|
||||||
if(folder_name != NULL)
|
if (folder_name != NULL)
|
||||||
folder_set_name(folder,(char*)json_string_value(folder_name));
|
folder_set_name(folder, (char *)json_string_value(folder_name));
|
||||||
|
|
||||||
parent_folder = json_object_get(node,"parent_folderkey");
|
parent_folder = json_object_get(node, "parent_folderkey");
|
||||||
if(parent_folder != NULL)
|
if (parent_folder != NULL) {
|
||||||
{
|
folder_set_parent(folder, (char *)json_string_value(parent_folder));
|
||||||
folder_set_parent(folder,(char*)json_string_value(parent_folder));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// infer that the parent folder must be "myfiles" root
|
// infer that the parent folder must be "myfiles" root
|
||||||
if(parent_folder == NULL && folderkey != NULL)
|
if (parent_folder == NULL && folderkey != NULL)
|
||||||
folder_set_parent(folder,"myfiles");
|
folder_set_parent(folder, "myfiles");
|
||||||
|
|
||||||
if(folderkey == NULL) retval = -1;
|
if (folderkey == NULL)
|
||||||
|
retval = -1;
|
||||||
|
|
||||||
if(root != NULL) json_decref(root);
|
if (root != NULL)
|
||||||
|
json_decref(root);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,69 +17,69 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "../../utils/http.h"
|
#include "../../utils/http.h"
|
||||||
#include "../../utils/json.h"
|
#include "../../utils/json.h"
|
||||||
#include "../mfconn.h"
|
#include "../mfconn.h"
|
||||||
#include "../apicalls.h" // IWYU pragma: keep
|
#include "../apicalls.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static int
|
static int _decode_user_get_info(mfhttp * conn, void *data);
|
||||||
_decode_user_get_info(mfhttp *conn, void *data);
|
|
||||||
|
|
||||||
int
|
int mfconn_api_user_get_info(mfconn * conn)
|
||||||
mfconn_api_user_get_info(mfconn *conn)
|
|
||||||
{
|
{
|
||||||
char *api_call;
|
char *api_call;
|
||||||
int retval;
|
int retval;
|
||||||
|
mfhttp *http;
|
||||||
|
|
||||||
// char *rx_buffer;
|
// char *rx_buffer;
|
||||||
|
|
||||||
if(conn == NULL) return -1;
|
if (conn == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
api_call = mfconn_create_signed_get(conn,0,"user/get_info.php",
|
api_call = mfconn_create_signed_get(conn, 0, "user/get_info.php",
|
||||||
"&response_format=json");
|
"&response_format=json");
|
||||||
|
|
||||||
mfhttp* http = http_create();
|
http = http_create();
|
||||||
retval = http_get_buf(http, api_call, _decode_user_get_info, NULL);
|
retval = http_get_buf(http, api_call, _decode_user_get_info, NULL);
|
||||||
http_destroy(http);
|
http_destroy(http);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int _decode_user_get_info(mfhttp * conn, void *data)
|
||||||
_decode_user_get_info(mfhttp *conn, void *data)
|
|
||||||
{
|
{
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t *root;
|
json_t *root;
|
||||||
json_t *node;
|
json_t *node;
|
||||||
json_t *email;
|
json_t *email;
|
||||||
json_t *first_name;
|
json_t *first_name;
|
||||||
json_t *last_name;
|
json_t *last_name;
|
||||||
|
|
||||||
if (data != NULL)
|
if (data != NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
root = http_parse_buf_json(conn, 0, &error);
|
root = http_parse_buf_json(conn, 0, &error);
|
||||||
|
|
||||||
node = json_object_by_path(root,"response/user_info");
|
node = json_object_by_path(root, "response/user_info");
|
||||||
|
|
||||||
email = json_object_get(node,"email");
|
email = json_object_get(node, "email");
|
||||||
if(email != NULL)
|
if (email != NULL)
|
||||||
printf("Email: %s\n\r",(char*)json_string_value(email));
|
printf("Email: %s\n\r", (char *)json_string_value(email));
|
||||||
|
|
||||||
first_name = json_object_get(node,"first_name");
|
first_name = json_object_get(node, "first_name");
|
||||||
if(first_name != NULL)
|
if (first_name != NULL)
|
||||||
printf("Name: %s ",(char*)json_string_value(first_name));
|
printf("Name: %s ", (char *)json_string_value(first_name));
|
||||||
|
|
||||||
last_name = json_object_get(node,"last_name");
|
last_name = json_object_get(node, "last_name");
|
||||||
if(node != NULL)
|
if (node != NULL)
|
||||||
printf("%s",(char*)json_string_value(last_name));
|
printf("%s", (char *)json_string_value(last_name));
|
||||||
|
|
||||||
printf("\n\r");
|
printf("\n\r");
|
||||||
|
|
||||||
if(root != NULL) json_decref(root);
|
if (root != NULL)
|
||||||
|
json_decref(root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -28,49 +27,53 @@
|
|||||||
#include "../../utils/json.h"
|
#include "../../utils/json.h"
|
||||||
#include "../../utils/strings.h"
|
#include "../../utils/strings.h"
|
||||||
#include "../mfconn.h"
|
#include "../mfconn.h"
|
||||||
#include "../apicalls.h" // IWYU pragma: keep
|
#include "../apicalls.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static int
|
static int _decode_get_session_token(mfhttp * conn, void *data);
|
||||||
_decode_get_session_token(mfhttp *conn, void *data);
|
|
||||||
|
|
||||||
struct user_get_session_token_response
|
struct user_get_session_token_response {
|
||||||
{
|
uint32_t secret_key;
|
||||||
uint32_t secret_key;
|
char *secret_time;
|
||||||
char *secret_time;
|
char *session_token;
|
||||||
char *session_token;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
mfconn_api_user_get_session_token(mfconn *conn, char *server,
|
mfconn_api_user_get_session_token(mfconn * conn, char *server,
|
||||||
char *username, char *password, int app_id, char *app_key,
|
char *username, char *password,
|
||||||
uint32_t *secret_key, char **secret_time, char **session_token)
|
int app_id, char *app_key,
|
||||||
|
uint32_t * secret_key,
|
||||||
|
char **secret_time, char **session_token)
|
||||||
{
|
{
|
||||||
char *login_url;
|
char *login_url;
|
||||||
char *post_args;
|
char *post_args;
|
||||||
char *user_signature;
|
char *user_signature;
|
||||||
int retval;
|
int retval;
|
||||||
struct user_get_session_token_response response;
|
struct user_get_session_token_response response;
|
||||||
|
mfhttp *http;
|
||||||
|
|
||||||
if(conn == NULL) return -1;
|
if (conn == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// configure url for operation
|
// configure url for operation
|
||||||
login_url = strdup_printf("https://%s/api/user/get_session_token.php",
|
login_url = strdup_printf("https://%s/api/user/get_session_token.php",
|
||||||
server);
|
server);
|
||||||
|
|
||||||
// create user signature
|
// create user signature
|
||||||
user_signature = mfconn_create_user_signature(conn, username, password, app_id, app_key);
|
user_signature =
|
||||||
|
mfconn_create_user_signature(conn, username, password, app_id, app_key);
|
||||||
|
|
||||||
post_args = strdup_printf(
|
post_args = strdup_printf("email=%s"
|
||||||
"email=%s"
|
"&password=%s"
|
||||||
"&password=%s"
|
"&application_id=35860"
|
||||||
"&application_id=35860"
|
"&signature=%s"
|
||||||
"&signature=%s"
|
"&token_version=2"
|
||||||
"&token_version=2"
|
"&response_format=json",
|
||||||
"&response_format=json",
|
username, password, user_signature);
|
||||||
username, password, user_signature);
|
|
||||||
|
|
||||||
mfhttp *http = http_create();
|
http = http_create();
|
||||||
retval = http_post_buf(http, login_url, post_args, _decode_get_session_token, (void *)(&response));
|
retval =
|
||||||
|
http_post_buf(http, login_url, post_args,
|
||||||
|
_decode_get_session_token, (void *)(&response));
|
||||||
http_destroy(http);
|
http_destroy(http);
|
||||||
|
|
||||||
free(login_url);
|
free(login_url);
|
||||||
@@ -83,48 +86,49 @@ mfconn_api_user_get_session_token(mfconn *conn, char *server,
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int _decode_get_session_token(mfhttp * conn, void *user_ptr)
|
||||||
_decode_get_session_token(mfhttp *conn, void *user_ptr)
|
|
||||||
{
|
{
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t *root = NULL;
|
json_t *root = NULL;
|
||||||
json_t *data;
|
json_t *data;
|
||||||
json_t *session_token;
|
json_t *session_token;
|
||||||
json_t *secret_key;
|
json_t *secret_key;
|
||||||
json_t *secret_time;
|
json_t *secret_time;
|
||||||
struct user_get_session_token_response *response;
|
struct user_get_session_token_response *response;
|
||||||
|
|
||||||
if(user_ptr == NULL) return -1;
|
if (user_ptr == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
response = (struct user_get_session_token_response *)user_ptr;
|
response = (struct user_get_session_token_response *)user_ptr;
|
||||||
|
|
||||||
root = http_parse_buf_json(conn, 0, &error);
|
root = http_parse_buf_json(conn, 0, &error);
|
||||||
|
|
||||||
data = json_object_by_path(root,"response");
|
data = json_object_by_path(root, "response");
|
||||||
if(data == NULL) return -1;
|
if (data == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
session_token = json_object_get(data,"session_token");
|
session_token = json_object_get(data, "session_token");
|
||||||
if(session_token == NULL)
|
if (session_token == NULL) {
|
||||||
{
|
|
||||||
json_decref(root);
|
json_decref(root);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
response->session_token = strdup(json_string_value(session_token));
|
response->session_token = strdup(json_string_value(session_token));
|
||||||
|
|
||||||
secret_key = json_object_get(data,"secret_key");
|
secret_key = json_object_get(data, "secret_key");
|
||||||
if(secret_key != NULL)
|
if (secret_key != NULL)
|
||||||
response->secret_key = atoll(json_string_value(secret_key));
|
response->secret_key = atoll(json_string_value(secret_key));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
time looks like a float but we must store it as a string to
|
time looks like a float but we must store it as a string to
|
||||||
remain congruent with the server on decimal place presentation.
|
remain congruent with the server on decimal place presentation.
|
||||||
*/
|
*/
|
||||||
secret_time = json_object_get(data,"time");
|
secret_time = json_object_get(data, "time");
|
||||||
if(secret_time != NULL)
|
if (secret_time != NULL)
|
||||||
response->secret_time = strdup(json_string_value(secret_time));
|
response->secret_time = strdup(json_string_value(secret_time));
|
||||||
|
|
||||||
if(root != NULL) json_decref(root);
|
if (root != NULL)
|
||||||
|
json_decref(root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
211
mfapi/file.c
211
mfapi/file.c
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -29,122 +28,128 @@
|
|||||||
#include "../utils/strings.h"
|
#include "../utils/strings.h"
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
|
|
||||||
|
struct mffile {
|
||||||
|
char quickkey[18];
|
||||||
|
char hash[65];
|
||||||
|
char name[256];
|
||||||
|
char mtime[16];
|
||||||
|
uint64_t revision;
|
||||||
|
|
||||||
struct mffile
|
char *share_link;
|
||||||
{
|
char *direct_link;
|
||||||
char quickkey[18];
|
char *onetime_link;
|
||||||
char hash[65];
|
|
||||||
char name[256];
|
|
||||||
char mtime[16];
|
|
||||||
uint64_t revision;
|
|
||||||
|
|
||||||
char *share_link;
|
|
||||||
char *direct_link;
|
|
||||||
char *onetime_link;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mffile*
|
mffile *file_alloc(void)
|
||||||
file_alloc(void)
|
|
||||||
{
|
{
|
||||||
mffile *file;
|
mffile *file;
|
||||||
|
|
||||||
file = (mffile*)calloc(1,sizeof(mffile));
|
file = (mffile *) calloc(1, sizeof(mffile));
|
||||||
|
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void file_free(mffile * file)
|
||||||
file_free(mffile *file)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return;
|
if (file == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
if(file->share_link != NULL) free(file->share_link);
|
if (file->share_link != NULL)
|
||||||
if(file->direct_link != NULL) free(file->direct_link);
|
free(file->share_link);
|
||||||
if(file->onetime_link != NULL) free(file->onetime_link);
|
if (file->direct_link != NULL)
|
||||||
|
free(file->direct_link);
|
||||||
|
if (file->onetime_link != NULL)
|
||||||
|
free(file->onetime_link);
|
||||||
|
|
||||||
free(file);
|
free(file);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int file_set_key(mffile * file, const char *key)
|
||||||
file_set_key(mffile *file,const char *key)
|
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(key == NULL) return -1;
|
return -1;
|
||||||
|
if (key == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
len = strlen(key);
|
len = strlen(key);
|
||||||
if(len != 11 && len != 15) return -1;
|
if (len != 11 && len != 15)
|
||||||
|
return -1;
|
||||||
|
|
||||||
memset(file->quickkey,0,sizeof(file->quickkey));
|
memset(file->quickkey, 0, sizeof(file->quickkey));
|
||||||
strncpy(file->quickkey,key,sizeof(file->quickkey) - 1);
|
strncpy(file->quickkey, key, sizeof(file->quickkey) - 1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *file_get_key(mffile * file)
|
||||||
file_get_key(mffile *file)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return NULL;
|
if (file == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return file->quickkey;
|
return file->quickkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int file_set_hash(mffile * file, const char *hash)
|
||||||
file_set_hash(mffile *file,const char *hash)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(hash == NULL) return -1;
|
return -1;
|
||||||
|
if (hash == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// system supports SHA256 (current) and MD5 (legacy)
|
// system supports SHA256 (current) and MD5 (legacy)
|
||||||
if(strlen(hash) < 32) return -1;
|
if (strlen(hash) < 32)
|
||||||
|
return -1;
|
||||||
|
|
||||||
memset(file->hash,0,sizeof(file->hash));
|
memset(file->hash, 0, sizeof(file->hash));
|
||||||
strncpy(file->hash,hash,sizeof(file->hash) - 1);
|
strncpy(file->hash, hash, sizeof(file->hash) - 1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *file_get_hash(mffile * file)
|
||||||
file_get_hash(mffile *file)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return NULL;
|
if (file == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return file->hash;
|
return file->hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int file_set_name(mffile * file, const char *name)
|
||||||
file_set_name(mffile *file,const char *name)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(name == NULL) return -1;
|
return -1;
|
||||||
|
if (name == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(strlen(name) > 255) return -1;
|
if (strlen(name) > 255)
|
||||||
|
return -1;
|
||||||
|
|
||||||
memset(file->name,0,sizeof(file->name));
|
memset(file->name, 0, sizeof(file->name));
|
||||||
strncpy(file->name,name,sizeof(file->name) - 1);
|
strncpy(file->name, name, sizeof(file->name) - 1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *file_get_name(mffile * file)
|
||||||
file_get_name(mffile *file)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return NULL;
|
if (file == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return file->name;
|
return file->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int file_set_share_link(mffile * file, const char *share_link)
|
||||||
file_set_share_link(mffile *file,const char *share_link)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(share_link == NULL) return -1;
|
return -1;
|
||||||
|
if (share_link == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(file->share_link != NULL)
|
if (file->share_link != NULL) {
|
||||||
{
|
|
||||||
free(file->share_link);
|
free(file->share_link);
|
||||||
file->share_link = NULL;
|
file->share_link = NULL;
|
||||||
}
|
}
|
||||||
@@ -154,22 +159,22 @@ file_set_share_link(mffile *file,const char *share_link)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *file_get_share_link(mffile * file)
|
||||||
file_get_share_link(mffile *file)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return NULL;
|
if (file == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return file->share_link;
|
return file->share_link;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int file_set_direct_link(mffile * file, const char *direct_link)
|
||||||
file_set_direct_link(mffile *file,const char *direct_link)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(direct_link == NULL) return -1;
|
return -1;
|
||||||
|
if (direct_link == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(file->direct_link != NULL)
|
if (file->direct_link != NULL) {
|
||||||
{
|
|
||||||
free(file->direct_link);
|
free(file->direct_link);
|
||||||
file->direct_link = NULL;
|
file->direct_link = NULL;
|
||||||
}
|
}
|
||||||
@@ -179,22 +184,22 @@ file_set_direct_link(mffile *file,const char *direct_link)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *file_get_direct_link(mffile * file)
|
||||||
file_get_direct_link(mffile *file)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return NULL;
|
if (file == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return file->direct_link;
|
return file->direct_link;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int file_set_onetime_link(mffile * file, const char *onetime_link)
|
||||||
file_set_onetime_link(mffile *file,const char *onetime_link)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(onetime_link == NULL) return -1;
|
return -1;
|
||||||
|
if (onetime_link == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(file->onetime_link != NULL)
|
if (file->onetime_link != NULL) {
|
||||||
{
|
|
||||||
free(file->onetime_link);
|
free(file->onetime_link);
|
||||||
file->onetime_link = NULL;
|
file->onetime_link = NULL;
|
||||||
}
|
}
|
||||||
@@ -204,53 +209,59 @@ file_set_onetime_link(mffile *file,const char *onetime_link)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *file_get_onetime_link(mffile * file)
|
||||||
file_get_onetime_link(mffile *file)
|
|
||||||
{
|
{
|
||||||
if(file == NULL) return NULL;
|
if (file == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return file->onetime_link;
|
return file->onetime_link;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t
|
ssize_t file_download_direct(mffile * file, char *local_dir)
|
||||||
file_download_direct(mffile *file, char *local_dir)
|
|
||||||
{
|
{
|
||||||
const char *url;
|
const char *url;
|
||||||
const char *file_name;
|
const char *file_name;
|
||||||
char *file_path;
|
char *file_path;
|
||||||
struct stat file_info;
|
struct stat file_info;
|
||||||
ssize_t bytes_read = 0;
|
ssize_t bytes_read = 0;
|
||||||
int retval;
|
int retval;
|
||||||
|
mfhttp *conn;
|
||||||
|
|
||||||
if(file == NULL) return -1;
|
if (file == NULL)
|
||||||
if(local_dir == NULL) return -1;
|
return -1;
|
||||||
|
if (local_dir == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
url = file_get_direct_link(file);
|
url = file_get_direct_link(file);
|
||||||
if(url == NULL) return -1;
|
if (url == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
file_name = file_get_name(file);
|
file_name = file_get_name(file);
|
||||||
if(file_name == NULL) return -1;
|
if (file_name == NULL)
|
||||||
if(strlen(file_name) < 1) return -1;
|
return -1;
|
||||||
|
if (strlen(file_name) < 1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(local_dir[strlen(local_dir) - 1] == '/')
|
if (local_dir[strlen(local_dir) - 1] == '/')
|
||||||
file_path = strdup_printf("%s%s",local_dir,file_name);
|
file_path = strdup_printf("%s%s", local_dir, file_name);
|
||||||
else
|
else
|
||||||
file_path = strdup_printf("%s/%s",local_dir,file_name);
|
file_path = strdup_printf("%s/%s", local_dir, file_name);
|
||||||
|
|
||||||
mfhttp *conn = http_create();
|
conn = http_create();
|
||||||
retval = http_get_file(conn, url, file_path);
|
retval = http_get_file(conn, url, file_path);
|
||||||
http_destroy(conn);
|
http_destroy(conn);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
it is preferable to have the vfs tell us how many bytes the
|
it is preferable to have the vfs tell us how many bytes the
|
||||||
transfer actually is. it's really all that matters.
|
transfer actually is. it's really all that matters.
|
||||||
*/
|
*/
|
||||||
memset(&file_info,0,sizeof(file_info));
|
memset(&file_info, 0, sizeof(file_info));
|
||||||
retval = stat(file_path,&file_info);
|
retval = stat(file_path, &file_info);
|
||||||
|
|
||||||
free(file_path);
|
free(file_path);
|
||||||
|
|
||||||
if(retval != 0) return -1;
|
if (retval != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
bytes_read = file_info.st_size;
|
bytes_read = file_info.st_size;
|
||||||
|
|
||||||
|
|||||||
30
mfapi/file.h
30
mfapi/file.h
@@ -24,34 +24,34 @@
|
|||||||
|
|
||||||
typedef struct mffile mffile;
|
typedef struct mffile mffile;
|
||||||
|
|
||||||
mffile* file_alloc(void);
|
mffile *file_alloc(void);
|
||||||
|
|
||||||
void file_free(mffile *file);
|
void file_free(mffile * file);
|
||||||
|
|
||||||
int file_set_key(mffile *file,const char *quickkey);
|
int file_set_key(mffile * file, const char *quickkey);
|
||||||
|
|
||||||
const char* file_get_key(mffile *file);
|
const char *file_get_key(mffile * file);
|
||||||
|
|
||||||
int file_set_hash(mffile *file,const char *hash);
|
int file_set_hash(mffile * file, const char *hash);
|
||||||
|
|
||||||
const char* file_get_hash(mffile *file);
|
const char *file_get_hash(mffile * file);
|
||||||
|
|
||||||
int file_set_name(mffile *file,const char *name);
|
int file_set_name(mffile * file, const char *name);
|
||||||
|
|
||||||
const char* file_get_name(mffile *file);
|
const char *file_get_name(mffile * file);
|
||||||
|
|
||||||
int file_set_share_link(mffile *file,const char *share_link);
|
int file_set_share_link(mffile * file, const char *share_link);
|
||||||
|
|
||||||
const char* file_get_share_link(mffile *file);
|
const char *file_get_share_link(mffile * file);
|
||||||
|
|
||||||
int file_set_direct_link(mffile *file,const char *direct_link);
|
int file_set_direct_link(mffile * file, const char *direct_link);
|
||||||
|
|
||||||
const char* file_get_direct_link(mffile *file);
|
const char *file_get_direct_link(mffile * file);
|
||||||
|
|
||||||
int file_set_onetime_link(mffile *file,const char *onetime_link);
|
int file_set_onetime_link(mffile * file, const char *onetime_link);
|
||||||
|
|
||||||
const char* file_get_onetime_link(mffile *file);
|
const char *file_get_onetime_link(mffile * file);
|
||||||
|
|
||||||
ssize_t file_download_direct(mffile *file, char *local_dir);
|
ssize_t file_download_direct(mffile * file, char *local_dir);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
100
mfapi/folder.c
100
mfapi/folder.c
@@ -17,110 +17,110 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "folder.h"
|
#include "folder.h"
|
||||||
|
|
||||||
struct mffolder
|
struct mffolder {
|
||||||
{
|
char folderkey[20];
|
||||||
char folderkey[20];
|
char name[41];
|
||||||
char name[41];
|
char parent[20];
|
||||||
char parent[20];
|
uint64_t revision;
|
||||||
uint64_t revision;
|
uint32_t folder_count;
|
||||||
uint32_t folder_count;
|
uint32_t file_count;
|
||||||
uint32_t file_count;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mffolder*
|
mffolder *folder_alloc(void)
|
||||||
folder_alloc(void)
|
|
||||||
{
|
{
|
||||||
mffolder *folder;
|
mffolder *folder;
|
||||||
|
|
||||||
folder = (mffolder*)calloc(1,sizeof(mffolder));
|
folder = (mffolder *) calloc(1, sizeof(mffolder));
|
||||||
|
|
||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void folder_free(mffolder * folder)
|
||||||
folder_free(mffolder *folder)
|
|
||||||
{
|
{
|
||||||
if(folder == NULL) return;
|
if (folder == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
free(folder);
|
free(folder);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int folder_set_key(mffolder * folder, const char *key)
|
||||||
folder_set_key(mffolder *folder,const char *key)
|
|
||||||
{
|
{
|
||||||
if(folder == NULL) return -1;
|
if (folder == NULL)
|
||||||
if(key == NULL) return -1;
|
return -1;
|
||||||
|
if (key == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(strlen(key) != 13) return -1;
|
if (strlen(key) != 13)
|
||||||
|
return -1;
|
||||||
|
|
||||||
memset(folder->folderkey,0,sizeof(folder->folderkey));
|
memset(folder->folderkey, 0, sizeof(folder->folderkey));
|
||||||
strncpy(folder->folderkey,key,sizeof(folder->folderkey) - 1);
|
strncpy(folder->folderkey, key, sizeof(folder->folderkey) - 1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *folder_get_key(mffolder * folder)
|
||||||
folder_get_key(mffolder *folder)
|
|
||||||
{
|
{
|
||||||
if(folder == NULL) return NULL;
|
if (folder == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return folder->folderkey;
|
return folder->folderkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int folder_set_parent(mffolder * folder, const char *parent_key)
|
||||||
folder_set_parent(mffolder *folder,const char *parent_key)
|
|
||||||
{
|
{
|
||||||
if(folder == NULL) return -1;
|
if (folder == NULL)
|
||||||
if(parent_key == NULL) return -1;
|
return -1;
|
||||||
|
if (parent_key == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(strlen(parent_key) != 13)
|
if (strlen(parent_key) != 13) {
|
||||||
{
|
if (strcmp(parent_key, "myfiles") != 0)
|
||||||
if(strcmp(parent_key,"myfiles") != 0) return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(folder->parent,0,sizeof(folder->parent));
|
memset(folder->parent, 0, sizeof(folder->parent));
|
||||||
strncpy(folder->parent,parent_key,sizeof(folder->parent) -1);
|
strncpy(folder->parent, parent_key, sizeof(folder->parent) - 1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *folder_get_parent(mffolder * folder)
|
||||||
folder_get_parent(mffolder *folder)
|
|
||||||
{
|
{
|
||||||
if(folder == NULL) return NULL;
|
if (folder == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return folder->parent;
|
return folder->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int folder_set_name(mffolder * folder, const char *name)
|
||||||
folder_set_name(mffolder *folder,const char *name)
|
|
||||||
{
|
{
|
||||||
if(folder == NULL) return -1;
|
if (folder == NULL)
|
||||||
if(name == NULL) return -1;
|
return -1;
|
||||||
|
if (name == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(strlen(name) > 40) return -1;
|
if (strlen(name) > 40)
|
||||||
|
return -1;
|
||||||
|
|
||||||
memset(folder->name,0,sizeof(folder->name));
|
memset(folder->name, 0, sizeof(folder->name));
|
||||||
strncpy(folder->name,name,sizeof(folder->name) - 1);
|
strncpy(folder->name, name, sizeof(folder->name) - 1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *folder_get_name(mffolder * folder)
|
||||||
folder_get_name(mffolder *folder)
|
|
||||||
{
|
{
|
||||||
if(folder == NULL) return NULL;
|
if (folder == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return folder->name;
|
return folder->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,20 +22,20 @@
|
|||||||
|
|
||||||
typedef struct mffolder mffolder;
|
typedef struct mffolder mffolder;
|
||||||
|
|
||||||
mffolder* folder_alloc(void);
|
mffolder *folder_alloc(void);
|
||||||
|
|
||||||
void folder_free(mffolder *folder);
|
void folder_free(mffolder * folder);
|
||||||
|
|
||||||
int folder_set_key(mffolder *folder,const char *folderkey);
|
int folder_set_key(mffolder * folder, const char *folderkey);
|
||||||
|
|
||||||
const char* folder_get_key(mffolder *folder);
|
const char *folder_get_key(mffolder * folder);
|
||||||
|
|
||||||
int folder_set_parent(mffolder *folder,const char *folderkey);
|
int folder_set_parent(mffolder * folder, const char *folderkey);
|
||||||
|
|
||||||
const char* folder_get_parent(mffolder *folder);
|
const char *folder_get_parent(mffolder * folder);
|
||||||
|
|
||||||
int folder_set_name(mffolder *folder,const char *name);
|
int folder_set_name(mffolder * folder, const char *name);
|
||||||
|
|
||||||
const char* folder_get_name(mffolder *folder);
|
const char *folder_get_name(mffolder * folder);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
169
mfapi/mfconn.c
169
mfapi/mfconn.c
@@ -28,26 +28,28 @@
|
|||||||
#include "apicalls.h"
|
#include "apicalls.h"
|
||||||
#include "mfconn.h"
|
#include "mfconn.h"
|
||||||
|
|
||||||
struct mfconn
|
struct mfconn {
|
||||||
{
|
char *server;
|
||||||
char *server;
|
uint32_t secret_key;
|
||||||
uint32_t secret_key;
|
char *secret_time;
|
||||||
char *secret_time;
|
char *session_token;
|
||||||
char *session_token;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mfconn*
|
mfconn *mfconn_create(char *server, char *username, char *password,
|
||||||
mfconn_create(char *server, char *username, char *password, int app_id, char *app_key)
|
int app_id, char *app_key)
|
||||||
{
|
{
|
||||||
mfconn *conn;
|
mfconn *conn;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
conn = (mfconn *)calloc(1,sizeof(mfconn));
|
conn = (mfconn *) calloc(1, sizeof(mfconn));
|
||||||
|
|
||||||
conn->server = strdup(server);
|
conn->server = strdup(server);
|
||||||
retval = mfconn_api_user_get_session_token(conn, conn->server,
|
retval = mfconn_api_user_get_session_token(conn, conn->server,
|
||||||
username, password, app_id, app_key, &(conn->secret_key),
|
username, password, app_id,
|
||||||
&(conn->secret_time), &(conn->session_token));
|
app_key,
|
||||||
|
&(conn->secret_key),
|
||||||
|
&(conn->secret_time),
|
||||||
|
&(conn->session_token));
|
||||||
|
|
||||||
if (retval == 0)
|
if (retval == 0)
|
||||||
return conn;
|
return conn;
|
||||||
@@ -55,8 +57,7 @@ mfconn_create(char *server, char *username, char *password, int app_id, char *ap
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void mfconn_destroy(mfconn * conn)
|
||||||
mfconn_destroy(mfconn *conn)
|
|
||||||
{
|
{
|
||||||
free(conn->server);
|
free(conn->server);
|
||||||
free(conn->secret_time);
|
free(conn->secret_time);
|
||||||
@@ -64,14 +65,14 @@ mfconn_destroy(mfconn *conn)
|
|||||||
free(conn);
|
free(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void mfconn_update_secret_key(mfconn * conn)
|
||||||
mfconn_update_secret_key(mfconn *conn)
|
|
||||||
{
|
{
|
||||||
uint64_t new_val;
|
uint64_t new_val;
|
||||||
|
|
||||||
if(conn == NULL) return;
|
if (conn == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
new_val = ((uint64_t)conn->secret_key) * 16807;
|
new_val = ((uint64_t) conn->secret_key) * 16807;
|
||||||
new_val %= 0x7FFFFFFF;
|
new_val %= 0x7FFFFFFF;
|
||||||
|
|
||||||
conn->secret_key = new_val;
|
conn->secret_key = new_val;
|
||||||
@@ -79,98 +80,106 @@ mfconn_update_secret_key(mfconn *conn)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *mfconn_create_user_signature(mfconn * conn, char *username,
|
||||||
mfconn_create_user_signature(mfconn *conn, char *username, char *password,
|
char *password, int app_id,
|
||||||
int app_id, char *app_key)
|
char *app_key)
|
||||||
{
|
{
|
||||||
char *signature_raw;
|
char *signature_raw;
|
||||||
unsigned char signature_enc[20]; // sha1 is 160 bits
|
unsigned char signature_enc[20]; // sha1 is 160 bits
|
||||||
char signature_hex[41];
|
char signature_hex[41];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if(conn == NULL) return NULL;
|
if (conn == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
signature_raw = strdup_printf("%s%s%d%s",
|
signature_raw = strdup_printf("%s%s%d%s",
|
||||||
username, password, app_id, app_key);
|
username, password, app_id, app_key);
|
||||||
|
|
||||||
SHA1((const unsigned char *)signature_raw,
|
SHA1((const unsigned char *)signature_raw,
|
||||||
strlen(signature_raw),signature_enc);
|
strlen(signature_raw), signature_enc);
|
||||||
|
|
||||||
free(signature_raw);
|
free(signature_raw);
|
||||||
|
|
||||||
for(i = 0;i < 20;i++)
|
for (i = 0; i < 20; i++) {
|
||||||
{
|
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
||||||
sprintf(&signature_hex[i*2],"%02x",signature_enc[i]);
|
|
||||||
}
|
}
|
||||||
signature_hex[40] = '\0';
|
signature_hex[40] = '\0';
|
||||||
|
|
||||||
return strdup((const char *)signature_hex);
|
return strdup((const char *)signature_hex);
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *mfconn_create_call_signature(mfconn * conn, char *url,
|
||||||
mfconn_create_call_signature(mfconn *conn,char *url,char *args)
|
char *args)
|
||||||
{
|
{
|
||||||
char *signature_raw;
|
char *signature_raw;
|
||||||
unsigned char signature_enc[16]; // md5 is 128 bits
|
unsigned char signature_enc[16]; // md5 is 128 bits
|
||||||
char signature_hex[33];
|
char signature_hex[33];
|
||||||
char *api;
|
char *api;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if(conn == NULL) return NULL;
|
if (conn == NULL)
|
||||||
if(url == NULL) return NULL;
|
return NULL;
|
||||||
if(args == NULL) return NULL;
|
if (url == NULL)
|
||||||
|
return NULL;
|
||||||
|
if (args == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
// printf("url: %s\n\rargs: %s\n\r",url,args);
|
// printf("url: %s\n\rargs: %s\n\r",url,args);
|
||||||
|
|
||||||
api = strstr(url,"/api/");
|
api = strstr(url, "/api/");
|
||||||
|
|
||||||
if(api == NULL) return NULL;
|
if (api == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
signature_raw = strdup_printf("%d%s%s%s",
|
signature_raw = strdup_printf("%d%s%s%s",
|
||||||
(conn->secret_key % 256),
|
(conn->secret_key % 256),
|
||||||
conn->secret_time,
|
conn->secret_time, api, args);
|
||||||
api,args);
|
|
||||||
|
|
||||||
MD5((const unsigned char *)signature_raw,
|
MD5((const unsigned char *)signature_raw,
|
||||||
strlen(signature_raw),signature_enc);
|
strlen(signature_raw), signature_enc);
|
||||||
|
|
||||||
free(signature_raw);
|
free(signature_raw);
|
||||||
|
|
||||||
for(i = 0;i < 16;i++)
|
for (i = 0; i < 16; i++) {
|
||||||
{
|
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
||||||
sprintf(&signature_hex[i*2],"%02x",signature_enc[i]);
|
|
||||||
}
|
}
|
||||||
signature_hex[32] = '\0';
|
signature_hex[32] = '\0';
|
||||||
|
|
||||||
return strdup((const char *)signature_hex);
|
return strdup((const char *)signature_hex);
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *mfconn_create_signed_get(mfconn * conn, int ssl, char *api,
|
||||||
mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
char *fmt, ...)
|
||||||
{
|
{
|
||||||
char *api_request = NULL;
|
char *api_request = NULL;
|
||||||
char *api_args = NULL;
|
char *api_args = NULL;
|
||||||
char *signature;
|
char *signature;
|
||||||
char *call_hash;
|
char *call_hash;
|
||||||
char *session_token;
|
char *session_token;
|
||||||
int bytes_to_alloc;
|
int bytes_to_alloc;
|
||||||
int api_args_len;
|
int api_args_len;
|
||||||
int api_len;
|
int api_len;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
if(conn == NULL) return NULL;
|
if (conn == NULL)
|
||||||
if(conn->server == NULL) return NULL;
|
return NULL;
|
||||||
if(conn->secret_time == NULL) return NULL;
|
if (conn->server == NULL)
|
||||||
if(conn->session_token == NULL) return NULL;
|
return NULL;
|
||||||
|
if (conn->secret_time == NULL)
|
||||||
|
return NULL;
|
||||||
|
if (conn->session_token == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
// make sure the api (ex: user/get_info.php) is sane
|
// make sure the api (ex: user/get_info.php) is sane
|
||||||
if(api == NULL) return NULL;
|
if (api == NULL)
|
||||||
|
return NULL;
|
||||||
api_len = strlen(api);
|
api_len = strlen(api);
|
||||||
if(api_len < 3) return NULL;
|
if (api_len < 3)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
// calculate how big of a buffer we need
|
// calculate how big of a buffer we need
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
|
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
session_token = strdup_printf("&session_token=%s", conn->session_token);
|
session_token = strdup_printf("&session_token=%s", conn->session_token);
|
||||||
@@ -178,7 +187,7 @@ mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
|||||||
api_args_len += strlen(session_token);
|
api_args_len += strlen(session_token);
|
||||||
|
|
||||||
// create the correctly sized buffer and process the args
|
// create the correctly sized buffer and process the args
|
||||||
api_args = (char*)calloc(api_args_len,sizeof(char));
|
api_args = (char *)calloc(api_args_len, sizeof(char));
|
||||||
|
|
||||||
// printf("\n\r%d\n\r",api_args_len);
|
// printf("\n\r%d\n\r",api_args_len);
|
||||||
|
|
||||||
@@ -190,25 +199,26 @@ mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
|||||||
free(session_token);
|
free(session_token);
|
||||||
|
|
||||||
// correct user error of trailing slash
|
// correct user error of trailing slash
|
||||||
if(api[api_len - 1] == '/') api[api_len - 1] = '\0';
|
if (api[api_len - 1] == '/')
|
||||||
|
api[api_len - 1] = '\0';
|
||||||
|
|
||||||
api_request = strdup_printf("%s//%s/api/%s",
|
api_request = strdup_printf("%s//%s/api/%s",
|
||||||
(ssl ? "https:" : "http:"), conn->server,api);
|
(ssl ? "https:" : "http:"), conn->server, api);
|
||||||
|
|
||||||
call_hash = mfconn_create_call_signature(conn,api_request,api_args);
|
call_hash = mfconn_create_call_signature(conn, api_request, api_args);
|
||||||
signature = strdup_printf("&signature=%s",call_hash);
|
signature = strdup_printf("&signature=%s", call_hash);
|
||||||
free(call_hash);
|
free(call_hash);
|
||||||
|
|
||||||
// compute the amount of space requred to realloc() the request
|
// compute the amount of space requred to realloc() the request
|
||||||
bytes_to_alloc = api_args_len;
|
bytes_to_alloc = api_args_len;
|
||||||
bytes_to_alloc += strlen(api_request);
|
bytes_to_alloc += strlen(api_request);
|
||||||
bytes_to_alloc += strlen(signature);
|
bytes_to_alloc += strlen(signature);
|
||||||
bytes_to_alloc += 1; // null termination
|
bytes_to_alloc += 1; // null termination
|
||||||
|
|
||||||
// append api GET args to api request
|
// append api GET args to api request
|
||||||
api_request = (char*)realloc(api_request,bytes_to_alloc);
|
api_request = (char *)realloc(api_request, bytes_to_alloc);
|
||||||
strncat(api_request,api_args,api_args_len);
|
strncat(api_request, api_args, api_args_len);
|
||||||
strcat(api_request,signature);
|
strcat(api_request, signature);
|
||||||
|
|
||||||
free(signature);
|
free(signature);
|
||||||
free(api_args);
|
free(api_args);
|
||||||
@@ -216,20 +226,17 @@ mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
|||||||
return api_request;
|
return api_request;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *mfconn_get_session_token(mfconn * conn)
|
||||||
mfconn_get_session_token(mfconn *conn)
|
|
||||||
{
|
{
|
||||||
return conn->session_token;
|
return conn->session_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char*
|
const char *mfconn_get_secret_time(mfconn * conn)
|
||||||
mfconn_get_secret_time(mfconn *conn)
|
|
||||||
{
|
{
|
||||||
return conn->secret_time;
|
return conn->secret_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t
|
uint32_t mfconn_get_secret_key(mfconn * conn)
|
||||||
mfconn_get_secret_key(mfconn *conn)
|
|
||||||
{
|
{
|
||||||
return conn->secret_key;
|
return conn->secret_key;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,22 +26,25 @@
|
|||||||
|
|
||||||
typedef struct mfconn mfconn;
|
typedef struct mfconn mfconn;
|
||||||
|
|
||||||
mfconn* mfconn_create(char *server, char *username, char *password, int app_id, char *app_key);
|
mfconn *mfconn_create(char *server, char *username, char *password,
|
||||||
|
int app_id, char *app_key);
|
||||||
|
|
||||||
void mfconn_destroy(mfconn *conn);
|
void mfconn_destroy(mfconn * conn);
|
||||||
|
|
||||||
ssize_t mfconn_download_direct(mffile *file,char *local_dir);
|
ssize_t mfconn_download_direct(mffile * file, char *local_dir);
|
||||||
|
|
||||||
char* mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...);
|
char *mfconn_create_signed_get(mfconn * conn, int ssl, char *api,
|
||||||
|
char *fmt, ...);
|
||||||
|
|
||||||
char* mfconn_create_user_signature(mfconn *conn, char *username,
|
char *mfconn_create_user_signature(mfconn * conn, char *username,
|
||||||
char *password, int app_id, char *app_key);
|
char *password, int app_id,
|
||||||
|
char *app_key);
|
||||||
|
|
||||||
void mfconn_update_secret_key(mfconn *conn);
|
void mfconn_update_secret_key(mfconn * conn);
|
||||||
|
|
||||||
const char* mfconn_get_session_token(mfconn *conn);
|
const char *mfconn_get_session_token(mfconn * conn);
|
||||||
|
|
||||||
const char* mfconn_get_secret_time(mfconn *conn);
|
const char *mfconn_get_secret_time(mfconn * conn);
|
||||||
|
|
||||||
uint32_t mfconn_get_secret_key(mfconn *conn);
|
uint32_t mfconn_get_secret_key(mfconn * conn);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -17,40 +17,39 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef _MFSHELL_COMMAND_H_
|
#ifndef _MFSHELL_COMMAND_H_
|
||||||
#define _MFSHELL_COMMAND_H_
|
#define _MFSHELL_COMMAND_H_
|
||||||
|
|
||||||
#include "mfshell.h"
|
#include "mfshell.h"
|
||||||
|
|
||||||
int mfshell_cmd_help(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_help(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_debug(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_debug(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_host(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_host(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_auth(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_whomai(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_whomai(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_list(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_list(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_chdir(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_chdir(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_pwd(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_pwd(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_lpwd(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_lpwd(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_lcd(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_file(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_file(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_links(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_links(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_mkdir(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_mkdir(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_get(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_get(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_cmd_whoami(mfshell *mfshell, int argc, char **argv);
|
int mfshell_cmd_whoami(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -27,22 +26,21 @@
|
|||||||
|
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static char*
|
static char *_get_login_from_user(void);
|
||||||
_get_login_from_user(void);
|
|
||||||
|
|
||||||
static char*
|
static char *_get_passwd_from_user(void);
|
||||||
_get_passwd_from_user(void);
|
|
||||||
|
|
||||||
int
|
int mfshell_cmd_auth(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
char *username;
|
char *username;
|
||||||
char *password;
|
char *password;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
if(mfshell->server == NULL) return -1;
|
return -1;
|
||||||
|
if (mfshell->server == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
switch (argc) {
|
switch (argc) {
|
||||||
case 1:
|
case 1:
|
||||||
@@ -62,10 +60,11 @@ mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(username == NULL || password == NULL) return -1;
|
if (username == NULL || password == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
mfshell->conn = mfconn_create(mfshell->server, username, password,
|
mfshell->conn = mfconn_create(mfshell->server, username, password,
|
||||||
mfshell->app_id, mfshell->app_key);
|
mfshell->app_id, mfshell->app_key);
|
||||||
|
|
||||||
if (mfshell->conn != NULL)
|
if (mfshell->conn != NULL)
|
||||||
printf("\n\rAuthentication SUCCESS\n\r");
|
printf("\n\rAuthentication SUCCESS\n\r");
|
||||||
@@ -75,39 +74,35 @@ mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv)
|
|||||||
return (mfshell->conn != NULL);
|
return (mfshell->conn != NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *_get_login_from_user(void)
|
||||||
_get_login_from_user(void)
|
|
||||||
{
|
{
|
||||||
char *login = NULL;
|
char *login = NULL;
|
||||||
size_t len;
|
size_t len;
|
||||||
ssize_t bytes_read;
|
ssize_t bytes_read;
|
||||||
|
|
||||||
printf("login: ");
|
printf("login: ");
|
||||||
bytes_read = getline(&login,&len,stdin);
|
bytes_read = getline(&login, &len, stdin);
|
||||||
|
|
||||||
if(bytes_read < 3)
|
if (bytes_read < 3) {
|
||||||
{
|
if (login != NULL) {
|
||||||
if(login != NULL)
|
|
||||||
{
|
|
||||||
free(login);
|
free(login);
|
||||||
login = NULL;
|
login = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (login[strlen(login)-1] == '\n')
|
if (login[strlen(login) - 1] == '\n')
|
||||||
login[strlen(login)-1] = '\0';
|
login[strlen(login) - 1] = '\0';
|
||||||
|
|
||||||
|
|
||||||
return login;
|
return login;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *_get_passwd_from_user(void)
|
||||||
_get_passwd_from_user(void)
|
|
||||||
{
|
{
|
||||||
char *passwd = NULL;
|
char *passwd = NULL;
|
||||||
size_t len;
|
size_t len;
|
||||||
ssize_t bytes_read;
|
ssize_t bytes_read;
|
||||||
struct termios old, new;
|
struct termios old,
|
||||||
|
new;
|
||||||
|
|
||||||
printf("passwd: ");
|
printf("passwd: ");
|
||||||
|
|
||||||
@@ -118,22 +113,19 @@ _get_passwd_from_user(void)
|
|||||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != 0)
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
bytes_read = getline(&passwd,&len,stdin);
|
bytes_read = getline(&passwd, &len, stdin);
|
||||||
|
|
||||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
|
||||||
|
|
||||||
if(bytes_read < 3)
|
if (bytes_read < 3) {
|
||||||
{
|
if (passwd != NULL) {
|
||||||
if(passwd != NULL)
|
|
||||||
{
|
|
||||||
free(passwd);
|
free(passwd);
|
||||||
passwd = NULL;
|
passwd = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (passwd[strlen(passwd)-1] == '\n')
|
if (passwd[strlen(passwd) - 1] == '\n')
|
||||||
passwd[strlen(passwd)-1] = '\0';
|
passwd[strlen(passwd) - 1] = '\0';
|
||||||
|
|
||||||
return passwd;
|
return passwd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -25,18 +24,18 @@
|
|||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/folder.h"
|
#include "../../mfapi/folder.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_chdir(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_chdir(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
mffolder *folder_new;
|
mffolder *folder_new;
|
||||||
const char *folder_curr;
|
const char *folder_curr;
|
||||||
const char *folder_parent;
|
const char *folder_parent;
|
||||||
const char *folderkey;
|
const char *folderkey;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -44,67 +43,60 @@ mfshell_cmd_chdir(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
folderkey = argv[1];
|
folderkey = argv[1];
|
||||||
if(folderkey == NULL) return -1;
|
if (folderkey == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// change to root
|
// change to root
|
||||||
if(strcmp(folderkey,"/") == 0) folderkey = "myfiles";
|
if (strcmp(folderkey, "/") == 0)
|
||||||
|
folderkey = "myfiles";
|
||||||
|
|
||||||
// user wants to navigate up a level
|
// user wants to navigate up a level
|
||||||
if(strcmp(folderkey,"..") == 0)
|
if (strcmp(folderkey, "..") == 0) {
|
||||||
{
|
|
||||||
// do several sanity checks to see if we're already at the root
|
// do several sanity checks to see if we're already at the root
|
||||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||||
|
|
||||||
if(folder_curr == NULL) return 0;
|
if (folder_curr == NULL)
|
||||||
if(strcmp(folder_curr,"myfiles") == 0) return 0;
|
return 0;
|
||||||
|
if (strcmp(folder_curr, "myfiles") == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
folder_parent = folder_get_parent(mfshell->folder_curr);
|
folder_parent = folder_get_parent(mfshell->folder_curr);
|
||||||
|
|
||||||
if(folder_parent == NULL) return 0;
|
if (folder_parent == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// it's pretty sure that we're not at the root
|
// it's pretty sure that we're not at the root
|
||||||
folderkey = folder_parent;
|
folderkey = folder_parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the lenght of the key
|
// check the lenght of the key
|
||||||
if(strlen(folderkey) != 13)
|
if (strlen(folderkey) != 13) {
|
||||||
{
|
|
||||||
// as a folder moniker, "myfiles" is an exception
|
// as a folder moniker, "myfiles" is an exception
|
||||||
if(strcmp(folderkey,"myfiles") != 0) return -1;
|
if (strcmp(folderkey, "myfiles") != 0)
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new folder object to store the results
|
// create a new folder object to store the results
|
||||||
folder_new = folder_alloc();
|
folder_new = folder_alloc();
|
||||||
|
|
||||||
// navigate to root is a special case
|
// navigate to root is a special case
|
||||||
if(strcmp(folderkey,"myfiles") == 0)
|
if (strcmp(folderkey, "myfiles") == 0) {
|
||||||
{
|
folder_set_key(folder_new, "myfiles");
|
||||||
folder_set_key(folder_new,"myfiles");
|
|
||||||
retval = 0;
|
retval = 0;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
retval = mfconn_api_folder_get_info(mfshell->conn,
|
retval = mfconn_api_folder_get_info(mfshell->conn,
|
||||||
folder_new,(char*)folderkey);
|
folder_new, (char *)folderkey);
|
||||||
mfconn_update_secret_key(mfshell->conn);
|
mfconn_update_secret_key(mfshell->conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(retval == 0)
|
if (retval == 0) {
|
||||||
{
|
if (mfshell->folder_curr != NULL) {
|
||||||
if(mfshell->folder_curr != NULL)
|
|
||||||
{
|
|
||||||
folder_free(mfshell->folder_curr);
|
folder_free(mfshell->folder_curr);
|
||||||
mfshell->folder_curr = NULL;
|
mfshell->folder_curr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
mfshell->folder_curr = folder_new;
|
mfshell->folder_curr = folder_new;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
folder_free(folder_new);
|
folder_free(folder_new);
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,17 +17,15 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_debug(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_debug(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
@@ -35,35 +33,21 @@ mfshell_cmd_debug(mfshell *mfshell, int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "server:", mfshell->server);
|
||||||
"server:",
|
|
||||||
mfshell->server);
|
|
||||||
|
|
||||||
const char *session_token = mfconn_get_session_token(mfshell->conn);
|
const char *session_token = mfconn_get_session_token(mfshell->conn);
|
||||||
const char *secret_time = mfconn_get_secret_time(mfshell->conn);
|
const char *secret_time = mfconn_get_secret_time(mfshell->conn);
|
||||||
uint32_t secret_key = mfconn_get_secret_key(mfshell->conn);
|
uint32_t secret_key = mfconn_get_secret_key(mfshell->conn);
|
||||||
if(session_token != NULL && secret_time != NULL)
|
|
||||||
{
|
|
||||||
printf(" %-15.15s %"PRIu32"\n\r",
|
|
||||||
"secret key:",
|
|
||||||
secret_key);
|
|
||||||
|
|
||||||
printf(" %-15.15s %s\n\r",
|
if (session_token != NULL && secret_time != NULL) {
|
||||||
"secret time:",
|
printf(" %-15.15s %" PRIu32 "\n\r", "secret key:", secret_key);
|
||||||
secret_time);
|
|
||||||
|
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "secret time:", secret_time);
|
||||||
"status:",
|
|
||||||
"Authenticated");
|
printf(" %-15.15s %s\n\r", "status:", "Authenticated");
|
||||||
}
|
} else {
|
||||||
else
|
printf(" %-15.15s %s\n\r", "status:", "Not authenticated");
|
||||||
{
|
|
||||||
printf(" %-15.15s %s\n\r",
|
|
||||||
"status:",
|
|
||||||
"Not authenticated");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -25,19 +24,19 @@
|
|||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/file.h"
|
#include "../../mfapi/file.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_file(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
mffile *file;
|
mffile *file;
|
||||||
int len;
|
int len;
|
||||||
int retval;
|
int retval;
|
||||||
const char *quickkey;
|
const char *quickkey;
|
||||||
const char *name;
|
const char *name;
|
||||||
const char *hash;
|
const char *hash;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -45,15 +44,17 @@ mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
quickkey = argv[1];
|
quickkey = argv[1];
|
||||||
if(quickkey == NULL) return -1;
|
if (quickkey == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
len = strlen(quickkey);
|
len = strlen(quickkey);
|
||||||
|
|
||||||
if(len != 11 && len != 15) return -1;
|
if (len != 11 && len != 15)
|
||||||
|
return -1;
|
||||||
|
|
||||||
file = file_alloc();
|
file = file_alloc();
|
||||||
|
|
||||||
retval = mfconn_api_file_get_info(mfshell->conn,file,(char*)quickkey);
|
retval = mfconn_api_file_get_info(mfshell->conn, file, (char *)quickkey);
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
fprintf(stderr, "api call unsuccessful\n");
|
fprintf(stderr, "api call unsuccessful\n");
|
||||||
}
|
}
|
||||||
@@ -63,20 +64,16 @@ mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
|||||||
name = file_get_name(file);
|
name = file_get_name(file);
|
||||||
hash = file_get_hash(file);
|
hash = file_get_hash(file);
|
||||||
|
|
||||||
if(name != NULL && name[0] != '\0')
|
if (name != NULL && name[0] != '\0')
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "filename:", name);
|
||||||
"filename:", name);
|
|
||||||
|
|
||||||
if(quickkey != NULL && quickkey[0] != '\0')
|
if (quickkey != NULL && quickkey[0] != '\0')
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "quickkey:", quickkey);
|
||||||
"quickkey:", quickkey);
|
|
||||||
|
|
||||||
if(hash != NULL && hash[0] != '\0')
|
if (hash != NULL && hash[0] != '\0')
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "hash:", hash);
|
||||||
"hash:", hash);
|
|
||||||
|
|
||||||
file_free(file);
|
file_free(file);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -28,18 +27,18 @@
|
|||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/file.h"
|
#include "../../mfapi/file.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_get(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
mffile *file;
|
mffile *file;
|
||||||
int len;
|
int len;
|
||||||
int retval;
|
int retval;
|
||||||
ssize_t bytes_read;
|
ssize_t bytes_read;
|
||||||
const char *quickkey;
|
const char *quickkey;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -47,45 +46,42 @@ mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
quickkey = argv[1];
|
quickkey = argv[1];
|
||||||
if(quickkey == NULL) return -1;
|
if (quickkey == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
len = strlen(quickkey);
|
len = strlen(quickkey);
|
||||||
|
|
||||||
if(len != 11 && len != 15) return -1;
|
if (len != 11 && len != 15)
|
||||||
|
return -1;
|
||||||
|
|
||||||
file = file_alloc();
|
file = file_alloc();
|
||||||
|
|
||||||
// get file name
|
// get file name
|
||||||
retval = mfconn_api_file_get_info(mfshell->conn,file,(char*)quickkey);
|
retval = mfconn_api_file_get_info(mfshell->conn, file, (char *)quickkey);
|
||||||
mfconn_update_secret_key(mfshell->conn);
|
mfconn_update_secret_key(mfshell->conn);
|
||||||
|
|
||||||
if(retval == -1)
|
if (retval == -1) {
|
||||||
{
|
|
||||||
file_free(file);
|
file_free(file);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// request a direct download (streaming) link
|
// request a direct download (streaming) link
|
||||||
retval = mfconn_api_file_get_links(mfshell->conn,file,(char*)quickkey);
|
retval = mfconn_api_file_get_links(mfshell->conn, file, (char *)quickkey);
|
||||||
mfconn_update_secret_key(mfshell->conn);
|
mfconn_update_secret_key(mfshell->conn);
|
||||||
|
|
||||||
if(retval == -1)
|
if (retval == -1) {
|
||||||
{
|
|
||||||
file_free(file);
|
file_free(file);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure we have a valid working directory to download to
|
// make sure we have a valid working directory to download to
|
||||||
if(mfshell->local_working_dir == NULL)
|
if (mfshell->local_working_dir == NULL) {
|
||||||
{
|
mfshell->local_working_dir = (char *)calloc(PATH_MAX + 1, sizeof(char));
|
||||||
mfshell->local_working_dir = (char*)calloc(PATH_MAX + 1,sizeof(char));
|
getcwd(mfshell->local_working_dir, PATH_MAX);
|
||||||
getcwd(mfshell->local_working_dir,PATH_MAX);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes_read = file_download_direct(file, mfshell->local_working_dir);
|
bytes_read = file_download_direct(file, mfshell->local_working_dir);
|
||||||
|
|
||||||
if(bytes_read != -1)
|
if (bytes_read != -1)
|
||||||
printf("\r Downloaded %zd bytes OK!\n\r",bytes_read);
|
printf("\r Downloaded %zd bytes OK!\n\r", bytes_read);
|
||||||
else
|
else
|
||||||
printf("\r\n Download FAILED!\n\r");
|
printf("\r\n Download FAILED!\n\r");
|
||||||
|
|
||||||
@@ -93,4 +89,3 @@ mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,33 +17,29 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_help(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_help(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
unsigned int column1_width = 0;
|
||||||
|
unsigned int column2_width = 0;
|
||||||
|
mfcmd *curr_cmd;
|
||||||
|
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(
|
printf(" arguments:\n\r"
|
||||||
" arguments:\n\r"
|
" <optional>\n\r" " [required]\n\r");
|
||||||
" <optional>\n\r"
|
|
||||||
" [required]\n\r");
|
|
||||||
|
|
||||||
printf("\n\r");
|
printf("\n\r");
|
||||||
|
|
||||||
unsigned int column1_width = 0;
|
|
||||||
unsigned int column2_width = 0;
|
|
||||||
|
|
||||||
mfcmd* curr_cmd;
|
|
||||||
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
||||||
if (strlen(curr_cmd->name) > column1_width)
|
if (strlen(curr_cmd->name) > column1_width)
|
||||||
column1_width = strlen(curr_cmd->name);
|
column1_width = strlen(curr_cmd->name);
|
||||||
@@ -52,10 +48,9 @@ mfshell_cmd_help(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
||||||
printf("%*s %*s %s\n", column1_width, curr_cmd->name, column2_width, curr_cmd->argstring, curr_cmd->help);
|
printf("%*s %*s %s\n", column1_width, curr_cmd->name,
|
||||||
|
column2_width, curr_cmd->argstring, curr_cmd->help);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -25,16 +24,14 @@
|
|||||||
|
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static char*
|
static char *_get_host_from_user(void);
|
||||||
_get_host_from_user(void);
|
|
||||||
|
|
||||||
int
|
int mfshell_cmd_host(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_host(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
char *alt_host = NULL;
|
char *alt_host = NULL;
|
||||||
char *host;
|
char *host;
|
||||||
|
|
||||||
switch (argc) {
|
switch (argc) {
|
||||||
case 1:
|
case 1:
|
||||||
@@ -49,17 +46,15 @@ mfshell_cmd_host(mfshell *mfshell, int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mfshell->server != NULL)
|
if (mfshell->server != NULL) {
|
||||||
{
|
|
||||||
// do nothing if the server is exactly the same
|
// do nothing if the server is exactly the same
|
||||||
if(strcmp(mfshell->server,host) == 0)
|
if (strcmp(mfshell->server, host) == 0) {
|
||||||
{
|
if (alt_host != NULL)
|
||||||
if(alt_host != NULL) free(alt_host);
|
free(alt_host);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mfshell->server != NULL)
|
if (mfshell->server != NULL) {
|
||||||
{
|
|
||||||
free(mfshell->server);
|
free(mfshell->server);
|
||||||
mfshell->server = NULL;
|
mfshell->server = NULL;
|
||||||
}
|
}
|
||||||
@@ -69,27 +64,26 @@ mfshell_cmd_host(mfshell *mfshell, int argc, char **argv)
|
|||||||
|
|
||||||
mfconn_destroy(mfshell->conn);
|
mfconn_destroy(mfshell->conn);
|
||||||
|
|
||||||
if(alt_host != NULL) free(alt_host);
|
if (alt_host != NULL)
|
||||||
|
free(alt_host);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *_get_host_from_user(void)
|
||||||
_get_host_from_user(void)
|
|
||||||
{
|
{
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
char *host = NULL;
|
char *host = NULL;
|
||||||
|
|
||||||
printf("host: [www.mediafire.com] ");
|
printf("host: [www.mediafire.com] ");
|
||||||
getline(&host,&len,stdin);
|
getline(&host, &len, stdin);
|
||||||
if (host[strlen(host)-1] == '\n')
|
if (host[strlen(host) - 1] == '\n')
|
||||||
host[strlen(host)-1] = '\0';
|
host[strlen(host) - 1] = '\0';
|
||||||
|
|
||||||
if(host == NULL)
|
if (host == NULL)
|
||||||
return strdup("www.mediafire.com");
|
return strdup("www.mediafire.com");
|
||||||
|
|
||||||
if(strlen(host) < 2)
|
if (strlen(host) < 2) {
|
||||||
{
|
|
||||||
free(host);
|
free(host);
|
||||||
return strdup("www.mediafire.com");
|
return strdup("www.mediafire.com");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,22 +17,21 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_lcd(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
int retval;
|
int retval;
|
||||||
const char *dir;
|
const char *dir;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -40,15 +39,15 @@ mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
dir = argv[1];
|
dir = argv[1];
|
||||||
if(dir == NULL) return -1;
|
if (dir == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if(strlen(dir) < 1) return -1;
|
if (strlen(dir) < 1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
retval = chdir(dir);
|
retval = chdir(dir);
|
||||||
if(retval == 0)
|
if (retval == 0) {
|
||||||
{
|
if (mfshell->local_working_dir != NULL) {
|
||||||
if(mfshell->local_working_dir != NULL)
|
|
||||||
{
|
|
||||||
free(mfshell->local_working_dir);
|
free(mfshell->local_working_dir);
|
||||||
mfshell->local_working_dir = NULL;
|
mfshell->local_working_dir = NULL;
|
||||||
}
|
}
|
||||||
@@ -58,4 +57,3 @@ mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv)
|
|||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -25,20 +24,20 @@
|
|||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/file.h"
|
#include "../../mfapi/file.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_links(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
mffile *file;
|
mffile *file;
|
||||||
int len;
|
int len;
|
||||||
int retval;
|
int retval;
|
||||||
const char *quickkey;
|
const char *quickkey;
|
||||||
const char *share_link;
|
const char *share_link;
|
||||||
const char *direct_link;
|
const char *direct_link;
|
||||||
const char *onetime_link;
|
const char *onetime_link;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -46,15 +45,17 @@ mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
quickkey = argv[1];
|
quickkey = argv[1];
|
||||||
if(quickkey == NULL) return -1;
|
if (quickkey == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
len = strlen(quickkey);
|
len = strlen(quickkey);
|
||||||
|
|
||||||
if(len != 11 && len != 15) return -1;
|
if (len != 11 && len != 15)
|
||||||
|
return -1;
|
||||||
|
|
||||||
file = file_alloc();
|
file = file_alloc();
|
||||||
|
|
||||||
retval = mfconn_api_file_get_links(mfshell->conn,file,(char*)quickkey);
|
retval = mfconn_api_file_get_links(mfshell->conn, file, (char *)quickkey);
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
fprintf(stderr, "api call unsuccessful\n");
|
fprintf(stderr, "api call unsuccessful\n");
|
||||||
}
|
}
|
||||||
@@ -64,20 +65,16 @@ mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
|||||||
direct_link = file_get_direct_link(file);
|
direct_link = file_get_direct_link(file);
|
||||||
onetime_link = file_get_onetime_link(file);
|
onetime_link = file_get_onetime_link(file);
|
||||||
|
|
||||||
if(share_link != NULL && share_link[0] != '\0')
|
if (share_link != NULL && share_link[0] != '\0')
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "sharing url:", share_link);
|
||||||
"sharing url:", share_link);
|
|
||||||
|
|
||||||
if(direct_link != NULL && direct_link[0] != '\0')
|
if (direct_link != NULL && direct_link[0] != '\0')
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "direct url:", direct_link);
|
||||||
"direct url:", direct_link);
|
|
||||||
|
|
||||||
if(onetime_link != NULL && onetime_link[0] != '\0')
|
if (onetime_link != NULL && onetime_link[0] != '\0')
|
||||||
printf(" %-15.15s %s\n\r",
|
printf(" %-15.15s %s\n\r", "1-time url:", onetime_link);
|
||||||
"1-time url:", onetime_link);
|
|
||||||
|
|
||||||
file_free(file);
|
file_free(file);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,23 +17,22 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "../../mfapi/apicalls.h"
|
#include "../../mfapi/apicalls.h"
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/folder.h"
|
#include "../../mfapi/folder.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_list(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_list(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
int retval;
|
int retval;
|
||||||
const char *folder_curr;
|
const char *folder_curr;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -43,22 +42,22 @@ mfshell_cmd_list(mfshell *mfshell, int argc, char **argv)
|
|||||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||||
|
|
||||||
// safety check... this should never happen
|
// safety check... this should never happen
|
||||||
if(folder_curr == NULL)
|
if (folder_curr == NULL)
|
||||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||||
|
|
||||||
// safety check... this should never happen
|
// safety check... this should never happen
|
||||||
if(folder_curr[0] == '\0')
|
if (folder_curr[0] == '\0')
|
||||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||||
|
|
||||||
// first folders
|
// first folders
|
||||||
retval = mfconn_api_folder_get_content(mfshell->conn, 0, mfshell->folder_curr);
|
retval =
|
||||||
|
mfconn_api_folder_get_content(mfshell->conn, 0, mfshell->folder_curr);
|
||||||
mfconn_update_secret_key(mfshell->conn);
|
mfconn_update_secret_key(mfshell->conn);
|
||||||
|
|
||||||
// then files
|
// then files
|
||||||
retval = mfconn_api_folder_get_content(mfshell->conn, 1, mfshell->folder_curr);
|
retval =
|
||||||
|
mfconn_api_folder_get_content(mfshell->conn, 1, mfshell->folder_curr);
|
||||||
mfconn_update_secret_key(mfshell->conn);
|
mfconn_update_secret_key(mfshell->conn);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,34 +17,31 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_lpwd(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_lpwd(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
fprintf(stderr, "Invalid number of argumens\n");
|
fprintf(stderr, "Invalid number of argumens\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mfshell->local_working_dir == NULL)
|
if (mfshell->local_working_dir == NULL) {
|
||||||
{
|
mfshell->local_working_dir = (char *)calloc(PATH_MAX + 1, sizeof(char));
|
||||||
mfshell->local_working_dir = (char*)calloc(PATH_MAX + 1,sizeof(char));
|
getcwd(mfshell->local_working_dir, PATH_MAX);
|
||||||
getcwd(mfshell->local_working_dir,PATH_MAX);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n\r", mfshell->local_working_dir);
|
printf("%s\n\r", mfshell->local_working_dir);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,23 +17,22 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "../../mfapi/apicalls.h"
|
#include "../../mfapi/apicalls.h"
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/folder.h"
|
#include "../../mfapi/folder.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_mkdir(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_mkdir(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
int retval;
|
int retval;
|
||||||
const char *folder_curr;
|
const char *folder_curr;
|
||||||
const char *name;
|
const char *name;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -41,24 +40,25 @@ mfshell_cmd_mkdir(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
name = argv[1];
|
name = argv[1];
|
||||||
if (name == NULL) return -1;
|
if (name == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||||
|
|
||||||
// safety check... this should never happen
|
// safety check... this should never happen
|
||||||
if(folder_curr == NULL)
|
if (folder_curr == NULL)
|
||||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||||
|
|
||||||
// safety check... this should never happen
|
// safety check... this should never happen
|
||||||
if(folder_curr[0] == '\0')
|
if (folder_curr[0] == '\0')
|
||||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||||
|
|
||||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||||
|
|
||||||
retval = mfconn_api_folder_create(mfshell->conn,(char*)folder_curr,(char*)name);
|
retval =
|
||||||
|
mfconn_api_folder_create(mfshell->conn, (char *)folder_curr,
|
||||||
|
(char *)name);
|
||||||
mfconn_update_secret_key(mfshell->conn);
|
mfconn_update_secret_key(mfshell->conn);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,24 +17,24 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "../../utils/strings.h"
|
#include "../../utils/strings.h"
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/folder.h"
|
#include "../../mfapi/folder.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_pwd(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_pwd(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
const char *folder_name;
|
const char *folder_name;
|
||||||
char *folder_name_tmp = NULL;
|
char *folder_name_tmp = NULL;
|
||||||
|
|
||||||
if(mfshell == NULL) return -1;
|
if (mfshell == NULL)
|
||||||
if(mfshell->folder_curr == NULL) return -1;
|
return -1;
|
||||||
|
if (mfshell->folder_curr == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -42,16 +42,15 @@ mfshell_cmd_pwd(mfshell *mfshell, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
folder_name = folder_get_name(mfshell->folder_curr);
|
folder_name = folder_get_name(mfshell->folder_curr);
|
||||||
if(folder_name[0] == '\0') return -1;
|
if (folder_name[0] == '\0')
|
||||||
|
return -1;
|
||||||
|
|
||||||
folder_name_tmp = strdup_printf("< %s >",folder_name);
|
folder_name_tmp = strdup_printf("< %s >", folder_name);
|
||||||
|
|
||||||
printf("%-15.13s %-50.50s\n\r",
|
printf("%-15.13s %-50.50s\n\r",
|
||||||
folder_get_key(mfshell->folder_curr),
|
folder_get_key(mfshell->folder_curr), folder_name_tmp);
|
||||||
folder_name_tmp);
|
|
||||||
|
|
||||||
free(folder_name_tmp);
|
free(folder_name_tmp);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,19 +17,17 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "../../mfapi/apicalls.h"
|
#include "../../mfapi/apicalls.h"
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#include "../commands.h" // IWYU pragma: keep
|
||||||
|
|
||||||
int
|
int mfshell_cmd_whoami(mfshell * mfshell, int argc, char **argv)
|
||||||
mfshell_cmd_whoami(mfshell *mfshell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
fprintf(stderr, "Invalid number of arguments\n");
|
fprintf(stderr, "Invalid number of arguments\n");
|
||||||
@@ -41,5 +39,3 @@ mfshell_cmd_whoami(mfshell *mfshell, int argc, char **argv)
|
|||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
110
mfshell/main.c
110
mfshell/main.c
@@ -27,11 +27,9 @@
|
|||||||
|
|
||||||
#include "mfshell.h"
|
#include "mfshell.h"
|
||||||
|
|
||||||
static void
|
static void mfshell_run(mfshell * mfshell);
|
||||||
mfshell_run(mfshell *mfshell);
|
|
||||||
|
|
||||||
static void
|
static void mfshell_parse_commands(mfshell * mfshell, char *command);
|
||||||
mfshell_parse_commands(mfshell *mfshell, char *command);
|
|
||||||
|
|
||||||
void print_help(char *cmd)
|
void print_help(char *cmd)
|
||||||
{
|
{
|
||||||
@@ -46,33 +44,36 @@ void print_help(char *cmd)
|
|||||||
fprintf(stderr, " -p, --password=<PASS> Login password\n");
|
fprintf(stderr, " -p, --password=<PASS> Login password\n");
|
||||||
fprintf(stderr, " -s, --server=<SERVER> Login server\n");
|
fprintf(stderr, " -s, --server=<SERVER> Login server\n");
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, "Username and password are optional. If not given, they\n"
|
fprintf(stderr,
|
||||||
"have to be entered via standard input.\n");
|
"Username and password are optional. If not given, they\n"
|
||||||
|
"have to be entered via standard input.\n");
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, "You should not pass your password as a commandline\n"
|
fprintf(stderr, "You should not pass your password as a commandline\n"
|
||||||
"argument as other users with access to the list of\n"
|
"argument as other users with access to the list of\n"
|
||||||
"running processes will then be able to see it.\n");
|
"running processes will then be able to see it.\n");
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, "Use the \"help\" command to print a list of available\n"
|
fprintf(stderr,
|
||||||
"commands in the interactive environment:\n");
|
"Use the \"help\" command to print a list of available\n"
|
||||||
|
"commands in the interactive environment:\n");
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, " %s -c help\n", cmd);
|
fprintf(stderr, " %s -c help\n", cmd);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_argv(int argc, char **argv, char **username,
|
void
|
||||||
char **password, char **server, char **command)
|
parse_argv(int argc, char **argv, char **username,
|
||||||
|
char **password, char **server, char **command)
|
||||||
{
|
{
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
{"command", required_argument, 0, 'c'},
|
{"command", required_argument, 0, 'c'},
|
||||||
{"username", required_argument, 0, 'u'},
|
{"username", required_argument, 0, 'u'},
|
||||||
{"password", required_argument, 0, 'p'},
|
{"password", required_argument, 0, 'p'},
|
||||||
{"server", required_argument, 0, 's'},
|
{"server", required_argument, 0, 's'},
|
||||||
{"help", no_argument, 0, 'h'},
|
{"help", no_argument, 0, 'h'},
|
||||||
{"version", no_argument, 0, 'v'}
|
{"version", no_argument, 0, 'v'}
|
||||||
};
|
};
|
||||||
|
int c;
|
||||||
|
|
||||||
int c;
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = getopt_long(argc, argv, "c:u:p:s:hv", long_options, NULL);
|
c = getopt_long(argc, argv, "c:u:p:s:hv", long_options, NULL);
|
||||||
if (c == -1)
|
if (c == -1)
|
||||||
@@ -116,20 +117,21 @@ void parse_argv(int argc, char **argv, char **username,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc,char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
mfshell *mfshell;
|
mfshell *mfshell;
|
||||||
char *server = "www.mediafire.com";
|
char *server = "www.mediafire.com";
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
char *password = NULL;
|
char *password = NULL;
|
||||||
char *command = NULL;
|
char *command = NULL;
|
||||||
|
|
||||||
SSL_library_init();
|
SSL_library_init();
|
||||||
|
|
||||||
parse_argv(argc, argv, &username, &password, &server, &command);
|
parse_argv(argc, argv, &username, &password, &server, &command);
|
||||||
|
|
||||||
mfshell = mfshell_create(35860,
|
mfshell = mfshell_create(35860,
|
||||||
"2c6dq0gb2sr8rgsue5a347lzpjnaay46yjazjcjg",server);
|
"2c6dq0gb2sr8rgsue5a347lzpjnaay46yjazjcjg",
|
||||||
|
server);
|
||||||
|
|
||||||
if (command == NULL) {
|
if (command == NULL) {
|
||||||
// begin shell mode
|
// begin shell mode
|
||||||
@@ -142,23 +144,33 @@ int main(int argc,char **argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void mfshell_parse_commands(mfshell * mfshell, char *command)
|
||||||
mfshell_parse_commands(mfshell *mfshell, char *command)
|
|
||||||
{
|
{
|
||||||
char *next;
|
char *next;
|
||||||
int ret;
|
int ret;
|
||||||
wordexp_t p;
|
wordexp_t p;
|
||||||
|
|
||||||
// FIXME: don't split by semicolon but by unescaped/unquoted semicolon
|
// FIXME: don't split by semicolon but by unescaped/unquoted semicolon
|
||||||
while ((next = strsep(&command, ";")) != NULL) {
|
while ((next = strsep(&command, ";")) != NULL) {
|
||||||
// FIXME: handle non-zero return value of wordexp
|
// FIXME: handle non-zero return value of wordexp
|
||||||
ret = wordexp(next, &p, WRDE_SHOWERR | WRDE_UNDEF);
|
ret = wordexp(next, &p, WRDE_SHOWERR | WRDE_UNDEF);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case WRDE_BADCHAR: fprintf(stderr, "wordexp: WRDE_BADCHAR\n"); break;
|
case WRDE_BADCHAR:
|
||||||
case WRDE_BADVAL: fprintf(stderr, "wordexp: WRDE_BADVAL\n"); break;
|
fprintf(stderr, "wordexp: WRDE_BADCHAR\n");
|
||||||
case WRDE_CMDSUB: fprintf(stderr, "wordexp: WRDE_CMDSUB\n"); break;
|
break;
|
||||||
case WRDE_NOSPACE: fprintf(stderr, "wordexp: WRDE_NOSPACE\n"); break;
|
case WRDE_BADVAL:
|
||||||
case WRDE_SYNTAX: fprintf(stderr, "wordexp: WRDE_SYNTAX\n"); break;
|
fprintf(stderr, "wordexp: WRDE_BADVAL\n");
|
||||||
|
break;
|
||||||
|
case WRDE_CMDSUB:
|
||||||
|
fprintf(stderr, "wordexp: WRDE_CMDSUB\n");
|
||||||
|
break;
|
||||||
|
case WRDE_NOSPACE:
|
||||||
|
fprintf(stderr, "wordexp: WRDE_NOSPACE\n");
|
||||||
|
break;
|
||||||
|
case WRDE_SYNTAX:
|
||||||
|
fprintf(stderr, "wordexp: WRDE_SYNTAX\n");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (p.we_wordc < 1) {
|
if (p.we_wordc < 1) {
|
||||||
@@ -170,45 +182,41 @@ mfshell_parse_commands(mfshell *mfshell, char *command)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void mfshell_run(mfshell * mfshell)
|
||||||
mfshell_run(mfshell *mfshell)
|
|
||||||
{
|
{
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
size_t len;
|
size_t len;
|
||||||
int abort = 0;
|
int abort = 0;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
printf("\n\rmfshell > ");
|
printf("\n\rmfshell > ");
|
||||||
|
|
||||||
retval = getline(&cmd,&len,stdin);
|
retval = getline(&cmd, &len, stdin);
|
||||||
if (retval == -1) {
|
if (retval == -1) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmd[strlen(cmd)-1] == '\n')
|
if (cmd[strlen(cmd) - 1] == '\n')
|
||||||
cmd[strlen(cmd)-1] = '\0';
|
cmd[strlen(cmd) - 1] = '\0';
|
||||||
|
|
||||||
printf("\n\r");
|
printf("\n\r");
|
||||||
|
|
||||||
if(strcmp(cmd,"exit") == 0)
|
if (strcmp(cmd, "exit") == 0) {
|
||||||
{
|
|
||||||
abort = 1;
|
abort = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(strcmp(cmd,"quit") == 0)
|
if (strcmp(cmd, "quit") == 0) {
|
||||||
{
|
|
||||||
abort = 1;
|
abort = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = mfshell_exec_shell_command(mfshell,cmd);
|
retval = mfshell_exec_shell_command(mfshell, cmd);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
cmd = NULL;
|
cmd = NULL;
|
||||||
}
|
}
|
||||||
while(abort == 0);
|
while (abort == 0);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -27,46 +26,49 @@
|
|||||||
#include "mfshell.h"
|
#include "mfshell.h"
|
||||||
#include "../mfapi/folder.h"
|
#include "../mfapi/folder.h"
|
||||||
|
|
||||||
struct mfcmd commands[] = {
|
struct mfcmd commands[] = {
|
||||||
{"help", "", "show this help", mfshell_cmd_help},
|
{"help", "", "show this help", mfshell_cmd_help},
|
||||||
{"debug", "", "show debug information", mfshell_cmd_debug},
|
{"debug", "", "show debug information", mfshell_cmd_debug},
|
||||||
{"host", "<server>", "change target server", mfshell_cmd_host},
|
{"host", "<server>", "change target server", mfshell_cmd_host},
|
||||||
{"auth", "<user <pass>>", "authenticate with active server",
|
{"auth", "<user <pass>>", "authenticate with active server",
|
||||||
mfshell_cmd_auth},
|
mfshell_cmd_auth},
|
||||||
{"whoami", "", "show basic user info", mfshell_cmd_whoami},
|
{"whoami", "", "show basic user info", mfshell_cmd_whoami},
|
||||||
{"ls", "", "show contents of active folder",
|
{"ls", "", "show contents of active folder",
|
||||||
mfshell_cmd_list},
|
mfshell_cmd_list},
|
||||||
{"cd", "[folderkey]", "change active folder", mfshell_cmd_chdir},
|
{"cd", "[folderkey]", "change active folder", mfshell_cmd_chdir},
|
||||||
{"pwd", "", "show the active folder", mfshell_cmd_pwd},
|
{"pwd", "", "show the active folder", mfshell_cmd_pwd},
|
||||||
{"lpwd", "", "show the local working directory",
|
{"lpwd", "", "show the local working directory",
|
||||||
mfshell_cmd_lpwd},
|
mfshell_cmd_lpwd},
|
||||||
{"lcd", "[dir]", "change the local working directory",
|
{"lcd", "[dir]", "change the local working directory",
|
||||||
mfshell_cmd_lcd},
|
mfshell_cmd_lcd},
|
||||||
{"mkdir", "[folder name]", "create a new folder", mfshell_cmd_mkdir},
|
{"mkdir", "[folder name]", "create a new folder", mfshell_cmd_mkdir},
|
||||||
{"file", "[quickkey]", "show file information", mfshell_cmd_file},
|
{"file", "[quickkey]", "show file information", mfshell_cmd_file},
|
||||||
{"links", "[quickkey]", "show access urls for the file",
|
{"links", "[quickkey]", "show access urls for the file",
|
||||||
mfshell_cmd_links},
|
mfshell_cmd_links},
|
||||||
{"get", "[quickkey]", "download a file", mfshell_cmd_get},
|
{"get", "[quickkey]", "download a file", mfshell_cmd_get},
|
||||||
{NULL, NULL, NULL, NULL}
|
{NULL, NULL, NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
mfshell*
|
mfshell *mfshell_create(int app_id, char *app_key, char *server)
|
||||||
mfshell_create(int app_id,char *app_key,char *server)
|
|
||||||
{
|
{
|
||||||
mfshell *shell;
|
mfshell *shell;
|
||||||
|
|
||||||
if(app_id <= 0) return NULL;
|
if (app_id <= 0)
|
||||||
if(app_key == NULL) return NULL;
|
return NULL;
|
||||||
if(server == NULL) return NULL;
|
if (app_key == NULL)
|
||||||
|
return NULL;
|
||||||
|
if (server == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
check to see if the server contains a forward-slash. if so,
|
check to see if the server contains a forward-slash. if so,
|
||||||
the caller did not understand the API and passed in the wrong
|
the caller did not understand the API and passed in the wrong
|
||||||
type of server resource.
|
type of server resource.
|
||||||
*/
|
*/
|
||||||
if(strchr(server,'/') != NULL) return NULL;
|
if (strchr(server, '/') != NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
shell = (mfshell*)calloc(1,sizeof(mfshell));
|
shell = (mfshell *) calloc(1, sizeof(mfshell));
|
||||||
|
|
||||||
shell->app_id = app_id;
|
shell->app_id = app_id;
|
||||||
shell->app_key = strdup(app_key);
|
shell->app_key = strdup(app_key);
|
||||||
@@ -74,7 +76,7 @@ mfshell_create(int app_id,char *app_key,char *server)
|
|||||||
|
|
||||||
// object to track folder location
|
// object to track folder location
|
||||||
shell->folder_curr = folder_alloc();
|
shell->folder_curr = folder_alloc();
|
||||||
folder_set_key(shell->folder_curr,"myfiles");
|
folder_set_key(shell->folder_curr, "myfiles");
|
||||||
|
|
||||||
// shell commands
|
// shell commands
|
||||||
shell->commands = commands;
|
shell->commands = commands;
|
||||||
@@ -82,10 +84,10 @@ mfshell_create(int app_id,char *app_key,char *server)
|
|||||||
return shell;
|
return shell;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int mfshell_exec(mfshell * shell, int argc, char **argv)
|
||||||
mfshell_exec(mfshell *shell, int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
mfcmd* curr_cmd;
|
mfcmd *curr_cmd;
|
||||||
|
|
||||||
for (curr_cmd = shell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
for (curr_cmd = shell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
||||||
if (strcmp(argv[0], curr_cmd->name) == 0) {
|
if (strcmp(argv[0], curr_cmd->name) == 0) {
|
||||||
return curr_cmd->handler(shell, argc, argv);
|
return curr_cmd->handler(shell, argc, argv);
|
||||||
@@ -94,24 +96,35 @@ mfshell_exec(mfshell *shell, int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int mfshell_exec_shell_command(mfshell * shell, char *command)
|
||||||
mfshell_exec_shell_command(mfshell *shell,char *command)
|
|
||||||
{
|
{
|
||||||
wordexp_t p;
|
wordexp_t p;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
if(shell == NULL) return -1;
|
if (shell == NULL)
|
||||||
if(command == NULL) return -1;
|
return -1;
|
||||||
|
if (command == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// FIXME: handle non-zero return value of wordexp
|
// FIXME: handle non-zero return value of wordexp
|
||||||
retval = wordexp(command, &p, WRDE_SHOWERR | WRDE_UNDEF);
|
retval = wordexp(command, &p, WRDE_SHOWERR | WRDE_UNDEF);
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
switch (retval) {
|
switch (retval) {
|
||||||
case WRDE_BADCHAR: fprintf(stderr, "wordexp: WRDE_BADCHAR\n"); break;
|
case WRDE_BADCHAR:
|
||||||
case WRDE_BADVAL: fprintf(stderr, "wordexp: WRDE_BADVAL\n"); break;
|
fprintf(stderr, "wordexp: WRDE_BADCHAR\n");
|
||||||
case WRDE_CMDSUB: fprintf(stderr, "wordexp: WRDE_CMDSUB\n"); break;
|
break;
|
||||||
case WRDE_NOSPACE: fprintf(stderr, "wordexp: WRDE_NOSPACE\n"); break;
|
case WRDE_BADVAL:
|
||||||
case WRDE_SYNTAX: fprintf(stderr, "wordexp: WRDE_SYNTAX\n"); break;
|
fprintf(stderr, "wordexp: WRDE_BADVAL\n");
|
||||||
|
break;
|
||||||
|
case WRDE_CMDSUB:
|
||||||
|
fprintf(stderr, "wordexp: WRDE_CMDSUB\n");
|
||||||
|
break;
|
||||||
|
case WRDE_NOSPACE:
|
||||||
|
fprintf(stderr, "wordexp: WRDE_NOSPACE\n");
|
||||||
|
break;
|
||||||
|
case WRDE_SYNTAX:
|
||||||
|
fprintf(stderr, "wordexp: WRDE_SYNTAX\n");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,4 +141,3 @@ mfshell_exec_shell_command(mfshell *shell,char *command)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef _MFSHELL_H_
|
#ifndef _MFSHELL_H_
|
||||||
#define _MFSHELL_H_
|
#define _MFSHELL_H_
|
||||||
|
|
||||||
@@ -27,42 +26,39 @@
|
|||||||
struct mfcmd;
|
struct mfcmd;
|
||||||
struct mfshell;
|
struct mfshell;
|
||||||
|
|
||||||
typedef struct mfcmd mfcmd;
|
typedef struct mfcmd mfcmd;
|
||||||
typedef struct mfshell mfshell;
|
typedef struct mfshell mfshell;
|
||||||
|
|
||||||
struct mfcmd
|
struct mfcmd {
|
||||||
{
|
char *name;
|
||||||
char *name;
|
char *argstring;
|
||||||
char *argstring;
|
char *help;
|
||||||
char *help;
|
int (*handler) (mfshell * mfshell, int argc, char **argv);
|
||||||
int (*handler) (mfshell *mfshell, int argc, char **argv);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mfshell
|
struct mfshell {
|
||||||
{
|
int app_id;
|
||||||
int app_id;
|
char *app_key;
|
||||||
char *app_key;
|
char *server;
|
||||||
char *server;
|
|
||||||
|
|
||||||
/* REST API tracking */
|
/* REST API tracking */
|
||||||
mffolder *folder_curr;
|
mffolder *folder_curr;
|
||||||
|
|
||||||
/* Local tracking */
|
/* Local tracking */
|
||||||
char *local_working_dir;
|
char *local_working_dir;
|
||||||
|
|
||||||
/* shell commands */
|
/* shell commands */
|
||||||
mfcmd *commands;
|
mfcmd *commands;
|
||||||
|
|
||||||
mfconn *conn;
|
mfconn *conn;
|
||||||
};
|
};
|
||||||
|
|
||||||
mfshell* mfshell_create(int app_id,char *app_key,char *server);
|
mfshell *mfshell_create(int app_id, char *app_key, char *server);
|
||||||
|
|
||||||
int mfshell_authenticate_user(mfshell *mfshell);
|
int mfshell_authenticate_user(mfshell * mfshell);
|
||||||
|
|
||||||
int mfshell_exec(mfshell *mfshell, int argc, char **argv);
|
int mfshell_exec(mfshell * mfshell, int argc, char **argv);
|
||||||
|
|
||||||
int mfshell_exec_shell_command(mfshell *mfshell,char *command);
|
int mfshell_exec_shell_command(mfshell * mfshell, char *command);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
15
tests/indent.sh
Executable file
15
tests/indent.sh
Executable file
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
ret=0
|
||||||
|
for f in ../**/*.c ../**/*.h; do
|
||||||
|
case $f in
|
||||||
|
../**/CMakeFiles/**/*)
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
INDENT_PROFILE=../.indent.pro indent -st $f | diff -u $f -
|
||||||
|
ret=$((ret+$?))
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
exit $ret
|
||||||
154
utils/http.c
154
utils/http.c
@@ -24,23 +24,26 @@
|
|||||||
|
|
||||||
#include "http.h"
|
#include "http.h"
|
||||||
|
|
||||||
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow, double ultotal, double ulnow);
|
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
||||||
static size_t http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
|
double ultotal, double ulnow);
|
||||||
static size_t http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
|
static size_t http_read_buf_cb(char *data, size_t size, size_t nmemb,
|
||||||
static size_t http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
|
void *user_ptr);
|
||||||
|
static size_t http_write_buf_cb(char *data, size_t size, size_t nmemb,
|
||||||
|
void *user_ptr);
|
||||||
|
static size_t http_write_file_cb(char *data, size_t size, size_t nmemb,
|
||||||
|
void *user_ptr);
|
||||||
|
|
||||||
struct mfhttp
|
struct mfhttp {
|
||||||
{
|
CURL *curl_handle;
|
||||||
CURL *curl_handle;
|
char *write_buf;
|
||||||
char *write_buf;
|
size_t write_buf_len;
|
||||||
size_t write_buf_len;
|
double ul_len;
|
||||||
double ul_len;
|
double ul_now;
|
||||||
double ul_now;
|
double dl_len;
|
||||||
double dl_len;
|
double dl_now;
|
||||||
double dl_now;
|
bool show_progress;
|
||||||
bool show_progress;
|
char error_buf[CURL_ERROR_SIZE];
|
||||||
char error_buf[CURL_ERROR_SIZE];
|
FILE *stream;
|
||||||
FILE *stream;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -51,20 +54,22 @@ struct mfhttp
|
|||||||
* keep-alive anyways.
|
* keep-alive anyways.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mfhttp*
|
mfhttp *http_create(void)
|
||||||
http_create(void)
|
|
||||||
{
|
{
|
||||||
mfhttp *conn;
|
mfhttp *conn;
|
||||||
CURL *curl_handle;
|
CURL *curl_handle;
|
||||||
curl_handle = curl_easy_init();
|
|
||||||
if(curl_handle == NULL) return NULL;
|
|
||||||
|
|
||||||
conn = (mfhttp*)calloc(1,sizeof(mfhttp));
|
curl_handle = curl_easy_init();
|
||||||
|
if (curl_handle == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
conn = (mfhttp *) calloc(1, sizeof(mfhttp));
|
||||||
conn->curl_handle = curl_handle;
|
conn->curl_handle = curl_handle;
|
||||||
|
|
||||||
conn->show_progress = false;
|
conn->show_progress = false;
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_NOPROGRESS,0);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_NOPROGRESS, 0);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSFUNCTION, http_progress_cb);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSFUNCTION,
|
||||||
|
http_progress_cb);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSDATA, (void *)conn);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSDATA, (void *)conn);
|
||||||
|
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_FOLLOWLOCATION, 1);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_FOLLOWLOCATION, 1);
|
||||||
@@ -77,28 +82,29 @@ http_create(void)
|
|||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t *
|
json_t *http_parse_buf_json(mfhttp * conn, size_t flags,
|
||||||
http_parse_buf_json(mfhttp* conn, size_t flags, json_error_t *error)
|
json_error_t * error)
|
||||||
{
|
{
|
||||||
return json_loadb(conn->write_buf, conn->write_buf_len, flags, error);
|
return json_loadb(conn->write_buf, conn->write_buf_len, flags, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void http_destroy(mfhttp * conn)
|
||||||
http_destroy(mfhttp* conn)
|
|
||||||
{
|
{
|
||||||
curl_easy_cleanup(conn->curl_handle);
|
curl_easy_cleanup(conn->curl_handle);
|
||||||
free(conn->write_buf);
|
free(conn->write_buf);
|
||||||
free(conn);
|
free(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
static int
|
||||||
double ultotal, double ulnow)
|
http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
||||||
|
double ultotal, double ulnow)
|
||||||
{
|
{
|
||||||
mfhttp *conn;
|
mfhttp *conn;
|
||||||
|
|
||||||
if (user_ptr == NULL) return 0;
|
if (user_ptr == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
conn = (mfhttp *)user_ptr;
|
conn = (mfhttp *) user_ptr;
|
||||||
|
|
||||||
conn->ul_len = ultotal;
|
conn->ul_len = ultotal;
|
||||||
conn->ul_now = ulnow;
|
conn->ul_now = ulnow;
|
||||||
@@ -110,18 +116,21 @@ static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
http_get_buf(mfhttp *conn, const char *url, int (*data_handler)(mfhttp *conn, void *data), void *data)
|
http_get_buf(mfhttp * conn, const char *url,
|
||||||
|
int (*data_handler) (mfhttp * conn, void *data), void *data)
|
||||||
{
|
{
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
curl_easy_reset(conn->curl_handle);
|
curl_easy_reset(conn->curl_handle);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_buf_cb);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
|
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);
|
retval = curl_easy_perform(conn->curl_handle);
|
||||||
if(retval != CURLE_OK) {
|
if (retval != CURLE_OK) {
|
||||||
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
@@ -131,14 +140,15 @@ http_get_buf(mfhttp *conn, const char *url, int (*data_handler)(mfhttp *conn, vo
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t
|
static size_t
|
||||||
http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||||
{
|
{
|
||||||
size_t data_len;
|
size_t data_len;
|
||||||
|
|
||||||
if (user_ptr == NULL) return 0;
|
if (user_ptr == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
data_len = size*nmemb;
|
data_len = size * nmemb;
|
||||||
|
|
||||||
if (data_len > 0) {
|
if (data_len > 0) {
|
||||||
fwrite(data, size, nmemb, stderr);
|
fwrite(data, size, nmemb, stderr);
|
||||||
@@ -149,21 +159,22 @@ http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t
|
static size_t
|
||||||
http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||||
{
|
{
|
||||||
mfhttp *conn;
|
mfhttp *conn;
|
||||||
size_t data_len;
|
size_t data_len;
|
||||||
|
|
||||||
if (user_ptr == NULL) return 0;
|
if (user_ptr == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
conn = (mfhttp*)user_ptr;
|
conn = (mfhttp *) user_ptr;
|
||||||
data_len = size*nmemb;
|
data_len = size * nmemb;
|
||||||
|
|
||||||
if (data_len > 0) {
|
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 = (char *)realloc(conn->write_buf,
|
||||||
conn->write_buf_len + data_len);
|
conn->write_buf_len + data_len);
|
||||||
memcpy(conn->write_buf + conn->write_buf_len, data, data_len);
|
memcpy(conn->write_buf + conn->write_buf_len, data, data_len);
|
||||||
conn->write_buf_len += data_len;
|
conn->write_buf_len += data_len;
|
||||||
}
|
}
|
||||||
@@ -172,18 +183,21 @@ http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
http_post_buf(mfhttp *conn, const char *url, const char *post_args, int (*data_handler)(mfhttp *conn, void *data), void *data)
|
http_post_buf(mfhttp * conn, const char *url, const char *post_args,
|
||||||
|
int (*data_handler) (mfhttp * conn, void *data), void *data)
|
||||||
{
|
{
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
curl_easy_reset(conn->curl_handle);
|
curl_easy_reset(conn->curl_handle);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_buf_cb);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
|
http_write_buf_cb);
|
||||||
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDS, post_args);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDS, post_args);
|
||||||
retval = curl_easy_perform(conn->curl_handle);
|
retval = curl_easy_perform(conn->curl_handle);
|
||||||
if(retval != CURLE_OK) {
|
if (retval != CURLE_OK) {
|
||||||
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
@@ -193,40 +207,42 @@ http_post_buf(mfhttp *conn, const char *url, const char *post_args, int (*data_h
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int http_get_file(mfhttp * conn, const char *url, const char *path)
|
||||||
http_get_file(mfhttp *conn, const char *url, const char *path)
|
|
||||||
{
|
{
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
curl_easy_reset(conn->curl_handle);
|
curl_easy_reset(conn->curl_handle);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_file_cb);
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
||||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
|
http_write_file_cb);
|
||||||
|
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
||||||
// FIXME: handle fopen() return value
|
// FIXME: handle fopen() return value
|
||||||
conn->stream = fopen(path, "w+");
|
conn->stream = fopen(path, "w+");
|
||||||
retval = curl_easy_perform(conn->curl_handle);
|
retval = curl_easy_perform(conn->curl_handle);
|
||||||
fclose(conn->stream);
|
fclose(conn->stream);
|
||||||
if(retval != CURLE_OK) {
|
if (retval != CURLE_OK) {
|
||||||
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t
|
static size_t
|
||||||
http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||||
{
|
{
|
||||||
mfhttp *conn;
|
mfhttp *conn;
|
||||||
|
|
||||||
if (user_ptr == NULL) return 0;
|
if (user_ptr == NULL)
|
||||||
conn = (mfhttp*)user_ptr;
|
return 0;
|
||||||
|
conn = (mfhttp *) user_ptr;
|
||||||
|
|
||||||
fwrite(data, size, nmemb, conn->stream);
|
fwrite(data, size, nmemb, conn->stream);
|
||||||
|
|
||||||
fprintf(stderr, "\r %.0f / %.0f", conn->dl_now, conn->dl_len);
|
fprintf(stderr, "\r %.0f / %.0f", conn->dl_now, conn->dl_len);
|
||||||
|
|
||||||
return size*nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*int
|
/*int
|
||||||
|
|||||||
22
utils/http.h
22
utils/http.h
@@ -25,13 +25,19 @@
|
|||||||
|
|
||||||
typedef struct mfhttp mfhttp;
|
typedef struct mfhttp mfhttp;
|
||||||
|
|
||||||
mfhttp* http_create(void);
|
mfhttp *http_create(void);
|
||||||
void http_destroy(mfhttp* conn);
|
void http_destroy(mfhttp * conn);
|
||||||
int http_get_buf(mfhttp *conn, const char *url, int (*data_handler)(mfhttp *conn, void *data), void *data);
|
int http_get_buf(mfhttp * conn, const char *url,
|
||||||
int http_post_buf(mfhttp *conn, const char *url, const char *post_args, int (*data_handler)(mfhttp *conn, void *data), void *data);
|
int (*data_handler) (mfhttp * conn, void *data),
|
||||||
int http_get_file(mfhttp *conn, const char *url, const char *path);
|
void *data);
|
||||||
int http_post_file(mfhttp *conn, const char *url, const char *post_args, FILE *fd);
|
int http_post_buf(mfhttp * conn, const char *url,
|
||||||
json_t *http_parse_buf_json(mfhttp* conn, size_t flags, json_error_t *error);
|
const char *post_args,
|
||||||
|
int (*data_handler) (mfhttp * conn, void *data),
|
||||||
|
void *data);
|
||||||
|
int http_get_file(mfhttp * conn, const char *url, const char *path);
|
||||||
|
int http_post_file(mfhttp * conn, const char *url,
|
||||||
|
const char *post_args, FILE * fd);
|
||||||
|
json_t *http_parse_buf_json(mfhttp * conn, size_t flags,
|
||||||
|
json_error_t * error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
43
utils/json.c
43
utils/json.c
@@ -16,39 +16,40 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
#include "stringv.h"
|
#include "stringv.h"
|
||||||
|
|
||||||
json_t*
|
json_t *json_object_by_path(json_t * start, const char *path)
|
||||||
json_object_by_path(json_t *start,const char *path)
|
|
||||||
{
|
{
|
||||||
char **path_nodes;
|
char **path_nodes;
|
||||||
char **curr;
|
char **curr;
|
||||||
json_t *node = NULL;
|
json_t *node = NULL;
|
||||||
json_t *data = NULL;
|
json_t *data = NULL;
|
||||||
|
|
||||||
if(start == NULL) return NULL;
|
if (start == NULL)
|
||||||
if(path == NULL) return NULL;
|
return NULL;
|
||||||
|
if (path == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
path_nodes = stringv_split((char*)path,"/",10);
|
path_nodes = stringv_split((char *)path, "/", 10);
|
||||||
|
|
||||||
if(path_nodes == NULL) return NULL;
|
if (path_nodes == NULL)
|
||||||
|
return NULL;
|
||||||
curr = path_nodes;
|
curr = path_nodes;
|
||||||
node = start;
|
node = start;
|
||||||
|
|
||||||
while(curr != NULL)
|
while (curr != NULL) {
|
||||||
{
|
if (*curr == NULL)
|
||||||
if(*curr == NULL) break;
|
break;
|
||||||
|
|
||||||
node = json_object_get(node,*curr);
|
node = json_object_get(node, *curr);
|
||||||
if(node == NULL) break;
|
if (node == NULL)
|
||||||
|
break;
|
||||||
|
|
||||||
if(!json_is_object(node))
|
if (!json_is_object(node)) {
|
||||||
{
|
stringv_free(path_nodes, STRINGV_FREE_ALL);
|
||||||
stringv_free(path_nodes,STRINGV_FREE_ALL);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ json_object_by_path(json_t *start,const char *path)
|
|||||||
data = node;
|
data = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
stringv_free(path_nodes,STRINGV_FREE_ALL);
|
stringv_free(path_nodes, STRINGV_FREE_ALL);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -101,5 +102,3 @@ json_object_by_path(json_t *start,const char *path)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,14 +16,11 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef _MFSHELL_JSON_H_
|
#ifndef _MFSHELL_JSON_H_
|
||||||
#define _MFSHELL_JSON_H_
|
#define _MFSHELL_JSON_H_
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
|
|
||||||
json_t* json_object_by_path(json_t *start,const char *path);
|
json_t *json_object_by_path(json_t * start, const char *path);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
127
utils/strings.c
127
utils/strings.c
@@ -16,7 +16,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -27,86 +26,90 @@
|
|||||||
#include "strings.h"
|
#include "strings.h"
|
||||||
#include "stringv.h"
|
#include "stringv.h"
|
||||||
|
|
||||||
char*
|
char *strdup_printf(char *fmt, ...)
|
||||||
strdup_printf(char* fmt, ...)
|
|
||||||
{
|
{
|
||||||
// Good for glibc 2.1 and above. Fedora5 is 2.4.
|
// Good for glibc 2.1 and above. Fedora5 is 2.4.
|
||||||
|
|
||||||
char *ret_str = NULL;
|
char *ret_str = NULL;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int bytes_to_allocate;
|
int bytes_to_allocate;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);
|
bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
// Add one for '\0'
|
// Add one for '\0'
|
||||||
bytes_to_allocate++;
|
bytes_to_allocate++;
|
||||||
|
|
||||||
ret_str = (char*)malloc(bytes_to_allocate * sizeof(char));
|
ret_str = (char *)malloc(bytes_to_allocate * sizeof(char));
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
|
bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return ret_str;
|
return ret_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *strdup_join(char *string1, char *string2)
|
||||||
strdup_join(char *string1,char *string2)
|
|
||||||
{
|
{
|
||||||
char *new_string;
|
char *new_string;
|
||||||
size_t string1_len;
|
size_t string1_len;
|
||||||
size_t string2_len;
|
size_t string2_len;
|
||||||
|
|
||||||
if(string1 == NULL || string2 == NULL) return NULL;
|
if (string1 == NULL || string2 == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
string1_len = strlen(string1);
|
string1_len = strlen(string1);
|
||||||
string2_len = strlen(string2);
|
string2_len = strlen(string2);
|
||||||
|
|
||||||
new_string = (char*)malloc(string1_len + string2_len + 1);
|
new_string = (char *)malloc(string1_len + string2_len + 1);
|
||||||
|
|
||||||
strncpy(new_string,string1,string1_len);
|
strncpy(new_string, string1, string1_len);
|
||||||
strncat(new_string,string2,string2_len);
|
strncat(new_string, string2, string2_len);
|
||||||
|
|
||||||
new_string[string1_len + string2_len] = '\0';
|
new_string[string1_len + string2_len] = '\0';
|
||||||
|
|
||||||
return new_string;
|
return new_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *strdup_subst(char *string, char *token, char *subst,
|
||||||
char*
|
unsigned int max)
|
||||||
strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
|
||||||
{
|
{
|
||||||
size_t string_len = 0;
|
size_t string_len = 0;
|
||||||
size_t subst_len = 0;
|
size_t subst_len = 0;
|
||||||
size_t token_len = 0;
|
size_t token_len = 0;
|
||||||
size_t total_len = 0;
|
size_t total_len = 0;
|
||||||
size_t copy_len = 0;
|
size_t copy_len = 0;
|
||||||
size_t token_count;
|
size_t token_count;
|
||||||
char *str_new = NULL;
|
char *str_new = NULL;
|
||||||
char **vectors;
|
char **vectors;
|
||||||
char **rewind;
|
char **rewind;
|
||||||
|
|
||||||
if(string == NULL) return NULL;
|
if (string == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if(token == NULL || subst == NULL) return NULL;
|
if (token == NULL || subst == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
string_len = strlen(string);
|
string_len = strlen(string);
|
||||||
token_len = strlen(token);
|
token_len = strlen(token);
|
||||||
|
|
||||||
// return on conditions that we could never handle.
|
// return on conditions that we could never handle.
|
||||||
if(token_len > string_len) return NULL;
|
if (token_len > string_len)
|
||||||
if(token[0] == '\0') return NULL;
|
return NULL;
|
||||||
|
if (token[0] == '\0')
|
||||||
|
return NULL;
|
||||||
|
|
||||||
vectors = stringv_find(string,token,max);
|
vectors = stringv_find(string, token, max);
|
||||||
if(vectors == NULL) return NULL;
|
if (vectors == NULL)
|
||||||
|
return NULL;
|
||||||
rewind = vectors;
|
rewind = vectors;
|
||||||
|
|
||||||
// count the number of tokens found in the string
|
// count the number of tokens found in the string
|
||||||
token_count = stringv_len(vectors);
|
token_count = stringv_len(vectors);
|
||||||
|
|
||||||
if(token_count > max) token_count = max;
|
if (token_count > max)
|
||||||
|
token_count = max;
|
||||||
|
|
||||||
// start with the original string size;
|
// start with the original string size;
|
||||||
total_len = string_len;
|
total_len = string_len;
|
||||||
@@ -117,24 +120,22 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
|||||||
// add back the total number of subst chars to be inserted
|
// add back the total number of subst chars to be inserted
|
||||||
total_len += (subst_len * token_count);
|
total_len += (subst_len * token_count);
|
||||||
|
|
||||||
str_new = (char*)malloc((total_len + 1) * sizeof(char));
|
str_new = (char *)malloc((total_len + 1) * sizeof(char));
|
||||||
str_new[0] = '\0';
|
str_new[0] = '\0';
|
||||||
|
|
||||||
while(*vectors != NULL)
|
while (*vectors != NULL) {
|
||||||
{
|
|
||||||
// calculate distance to the next token from current position
|
// calculate distance to the next token from current position
|
||||||
copy_len = *vectors - string;
|
copy_len = *vectors - string;
|
||||||
|
|
||||||
if(copy_len > 0)
|
if (copy_len > 0) {
|
||||||
{
|
strncat(str_new, string, copy_len);
|
||||||
strncat(str_new,string,copy_len);
|
|
||||||
string += copy_len;
|
string += copy_len;
|
||||||
|
|
||||||
// when total_len == 0 the process is complete
|
// when total_len == 0 the process is complete
|
||||||
total_len -= copy_len;
|
total_len -= copy_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncat(str_new,token,token_len);
|
strncat(str_new, token, token_len);
|
||||||
|
|
||||||
// when total_len == 0 the process is complete
|
// when total_len == 0 the process is complete
|
||||||
total_len -= token_len;
|
total_len -= token_len;
|
||||||
@@ -143,33 +144,31 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// might have one more copy operation to complete
|
// might have one more copy operation to complete
|
||||||
if(total_len > 0)
|
if (total_len > 0) {
|
||||||
{
|
strcat(str_new, string);
|
||||||
strcat(str_new,string);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: can't free vectors directly cuz it was incremented
|
// todo: can't free vectors directly cuz it was incremented
|
||||||
free(rewind);
|
free(rewind);
|
||||||
|
|
||||||
return str_new;
|
return str_new;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void string_chomp(char *string)
|
||||||
string_chomp(char *string)
|
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
char *pos;
|
char *pos;
|
||||||
|
|
||||||
if(string == NULL) return;
|
if (string == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
len = strlen(string);
|
len = strlen(string);
|
||||||
|
|
||||||
if(len == 0) return;
|
if (len == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
pos = &string[len - 1];
|
pos = &string[len - 1];
|
||||||
|
|
||||||
while(isspace((int)*pos) != 0)
|
while (isspace((int)*pos) != 0) {
|
||||||
{
|
|
||||||
*pos = '\0';
|
*pos = '\0';
|
||||||
pos--;
|
pos--;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,17 +16,17 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef _STR_TOOLS_H_
|
#ifndef _STR_TOOLS_H_
|
||||||
#define _STR_TOOLS_H_
|
#define _STR_TOOLS_H_
|
||||||
|
|
||||||
char* strdup_printf(char* fmt, ...);
|
char *strdup_printf(char *fmt, ...);
|
||||||
|
|
||||||
char* strdup_join(char *string1,char *string2);
|
char *strdup_join(char *string1, char *string2);
|
||||||
|
|
||||||
char* strdup_subst(char *string,char *str_old,char *str_new, unsigned int max);
|
char *strdup_subst(char *string, char *str_old, char *str_new,
|
||||||
|
unsigned int max);
|
||||||
|
|
||||||
void string_chomp(char *string);
|
void string_chomp(char *string);
|
||||||
|
|
||||||
// void string_strip_head(char *string,char c);
|
// void string_strip_head(char *string,char c);
|
||||||
|
|
||||||
|
|||||||
143
utils/stringv.c
143
utils/stringv.c
@@ -16,74 +16,72 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "stringv.h"
|
#include "stringv.h"
|
||||||
|
|
||||||
size_t
|
size_t stringv_len(char **array)
|
||||||
stringv_len(char **array)
|
|
||||||
{
|
{
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
char **pos;
|
char **pos;
|
||||||
|
|
||||||
if(array == NULL) return 0;
|
if (array == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
pos = array;
|
pos = array;
|
||||||
while(pos[0] != NULL)
|
while (pos[0] != NULL) {
|
||||||
{
|
|
||||||
pos++;
|
pos++;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void stringv_free(char **array, int b_free)
|
||||||
stringv_free(char **array,int b_free)
|
|
||||||
{
|
{
|
||||||
char **pos;
|
char **pos;
|
||||||
|
|
||||||
if(array == NULL) return;
|
if (array == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
pos = array;
|
pos = array;
|
||||||
|
|
||||||
while((*pos) != NULL)
|
while ((*pos) != NULL) {
|
||||||
{
|
free(*pos);
|
||||||
free(*pos);
|
++pos;
|
||||||
++pos;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(b_free == STRINGV_FREE_ALL) free(array);
|
if (b_free == STRINGV_FREE_ALL)
|
||||||
|
free(array);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char**
|
char **stringv_copy(char **array)
|
||||||
stringv_copy(char **array)
|
|
||||||
{
|
{
|
||||||
uint32_t array_len;
|
uint32_t array_len;
|
||||||
char **array_pos;
|
char **array_pos;
|
||||||
|
|
||||||
char **dup_array;
|
char **dup_array;
|
||||||
char **dup_pos;
|
char **dup_pos;
|
||||||
|
|
||||||
if(array == NULL) return (char**)NULL;
|
if (array == NULL)
|
||||||
|
return (char **)NULL;
|
||||||
|
|
||||||
array_pos = array;
|
array_pos = array;
|
||||||
|
|
||||||
array_len = stringv_len(array);
|
array_len = stringv_len(array);
|
||||||
|
|
||||||
if(array_len > UINT32_MAX - 1) array_len = UINT32_MAX -1;
|
if (array_len > UINT32_MAX - 1)
|
||||||
|
array_len = UINT32_MAX - 1;
|
||||||
|
|
||||||
dup_array = (char**)calloc(array_len,sizeof(char*));
|
dup_array = (char **)calloc(array_len, sizeof(char *));
|
||||||
dup_pos = dup_array;
|
dup_pos = dup_array;
|
||||||
|
|
||||||
while((*array_pos) != NULL)
|
while ((*array_pos) != NULL) {
|
||||||
{
|
*dup_pos = strdup((const char *)*array_pos);
|
||||||
*dup_pos = strdup((const char*)*array_pos);
|
|
||||||
|
|
||||||
array_pos++;
|
array_pos++;
|
||||||
dup_pos++;
|
dup_pos++;
|
||||||
@@ -92,82 +90,89 @@ stringv_copy(char **array)
|
|||||||
return dup_array;
|
return dup_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
char**
|
char **stringv_find(char *string, char *token, int limit)
|
||||||
stringv_find(char *string,char *token,int limit)
|
|
||||||
{
|
{
|
||||||
char **results = NULL;
|
char **results = NULL;
|
||||||
char *pos = NULL;
|
char *pos = NULL;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
if(string == NULL) return (char**)NULL;
|
if (string == NULL)
|
||||||
if(token == NULL) return (char**)NULL;
|
return (char **)NULL;
|
||||||
if(limit == 0) return (char**)NULL;
|
if (token == NULL)
|
||||||
|
return (char **)NULL;
|
||||||
|
if (limit == 0)
|
||||||
|
return (char **)NULL;
|
||||||
|
|
||||||
pos = string;
|
pos = string;
|
||||||
|
|
||||||
if(strlen(token) > strlen(string)) return (char**)NULL;
|
if (strlen(token) > strlen(string))
|
||||||
|
return (char **)NULL;
|
||||||
|
|
||||||
while(count != limit)
|
while (count != limit) {
|
||||||
{
|
pos = strstr(pos, token);
|
||||||
pos = strstr(pos,token);
|
if (pos == NULL)
|
||||||
if(pos == NULL) break;
|
break;
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
results = (char**)realloc((void*)results,sizeof(char*) * count + 1);
|
results = (char **)realloc((void *)results, sizeof(char *) * count + 1);
|
||||||
|
|
||||||
results[count - 1] = pos;
|
results[count - 1] = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count == 0) return (char**)NULL;
|
if (count == 0)
|
||||||
|
return (char **)NULL;
|
||||||
|
|
||||||
results[count] = (char*)NULL;
|
results[count] = (char *)NULL;
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
char**
|
char **stringv_split(char *string, char *token, int limit)
|
||||||
stringv_split(char *string,char *token,int limit)
|
|
||||||
{
|
{
|
||||||
char **results = NULL;
|
char **results = NULL;
|
||||||
char *curr = NULL;
|
char *curr = NULL;
|
||||||
char *next = NULL;
|
char *next = NULL;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
unsigned int len;
|
unsigned int len;
|
||||||
size_t copy_len = 0;
|
size_t copy_len = 0;
|
||||||
|
|
||||||
if(string == NULL) return (char**)NULL;
|
if (string == NULL)
|
||||||
if(token == NULL) return (char**)NULL;
|
return (char **)NULL;
|
||||||
if(limit == 0) return (char**)NULL;
|
if (token == NULL)
|
||||||
|
return (char **)NULL;
|
||||||
|
if (limit == 0)
|
||||||
|
return (char **)NULL;
|
||||||
|
|
||||||
len = strlen(string);
|
len = strlen(string);
|
||||||
if(strlen(token) > len) return (char**)NULL;
|
if (strlen(token) > len)
|
||||||
|
return (char **)NULL;
|
||||||
|
|
||||||
curr = string;
|
curr = string;
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
// alloc space for current item plus NULL vector terminator
|
// alloc space for current item plus NULL vector terminator
|
||||||
results = (char**)realloc(results,sizeof(char*) * (count + 2));
|
results = (char **)realloc(results, sizeof(char *) * (count + 2));
|
||||||
|
|
||||||
// find the next occurrence
|
// find the next occurrence
|
||||||
next = strstr(curr,token);
|
next = strstr(curr, token);
|
||||||
|
|
||||||
if(next != NULL)
|
if (next != NULL)
|
||||||
copy_len = next - curr;
|
copy_len = next - curr;
|
||||||
else
|
else
|
||||||
copy_len = strlen(curr);
|
copy_len = strlen(curr);
|
||||||
|
|
||||||
results[count] = (char*)calloc(copy_len + 1,sizeof(char));
|
results[count] = (char *)calloc(copy_len + 1, sizeof(char));
|
||||||
memcpy(results[count],curr,copy_len);
|
memcpy(results[count], curr, copy_len);
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
|
|
||||||
if(next == NULL) break;
|
if (next == NULL)
|
||||||
|
break;
|
||||||
|
|
||||||
curr = next;
|
curr = next;
|
||||||
curr++;
|
curr++;
|
||||||
}
|
}
|
||||||
while(count < limit);
|
while (count < limit);
|
||||||
|
|
||||||
results[count] = NULL;
|
results[count] = NULL;
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef _STRING_V_H_
|
#ifndef _STRING_V_H_
|
||||||
#define _STRING_V_H_
|
#define _STRING_V_H_
|
||||||
|
|
||||||
@@ -25,20 +24,18 @@
|
|||||||
#define STRINGV_FREE_ALL 1
|
#define STRINGV_FREE_ALL 1
|
||||||
|
|
||||||
// count number of strings in a NULL in a string vector
|
// count number of strings in a NULL in a string vector
|
||||||
size_t stringv_len(char **array);
|
size_t stringv_len(char **array);
|
||||||
|
|
||||||
// free all of the strings in a vector and optionally the vector pointer
|
// free all of the strings in a vector and optionally the vector pointer
|
||||||
void stringv_free(char **array,int b_free);
|
void stringv_free(char **array, int b_free);
|
||||||
|
|
||||||
// deep copy of string vector. returns a new vector pointer
|
// deep copy of string vector. returns a new vector pointer
|
||||||
char** stringv_copy(char **array);
|
char **stringv_copy(char **array);
|
||||||
|
|
||||||
// returns a NULL terminated vector array to every location 'token' is found
|
// returns a NULL terminated vector array to every location 'token' is found
|
||||||
char** stringv_find(char *string,char *token,int limit);
|
char **stringv_find(char *string, char *token, int limit);
|
||||||
|
|
||||||
// returns a NULL terminated vector array of items delimited by 'token'
|
// returns a NULL terminated vector array of items delimited by 'token'
|
||||||
char** stringv_split(char *string,char *token,int limit);
|
char **stringv_split(char *string, char *token, int limit);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user