Don't let api_device_getchanges print but let it fill datastructures

This commit is contained in:
josch
2014-09-25 21:51:42 +02:00
parent ebc70ee4c4
commit 8ac6e70609
5 changed files with 125 additions and 57 deletions

View File

@@ -26,6 +26,19 @@
#include "folder.h" #include "folder.h"
#include "mfconn.h" #include "mfconn.h"
enum mfconn_device_change_type {
MFCONN_DEVICE_CHANGE_DELETED_FOLDER,
MFCONN_DEVICE_CHANGE_DELETED_FILE,
MFCONN_DEVICE_CHANGE_UPDATED_FOLDER,
MFCONN_DEVICE_CHANGE_UPDATED_FILE
};
struct mfconn_device_change {
enum mfconn_device_change_type change;
char key[20];
uint64_t revision;
};
int mfconn_api_file_get_info(mfconn * conn, mffile * file, int mfconn_api_file_get_info(mfconn * conn, mffile * file,
char *quickkey); char *quickkey);
@@ -60,7 +73,7 @@ int mfconn_api_folder_delete(mfconn * conn, const char *folderkey);
int mfconn_api_device_get_status(mfconn * conn, int mfconn_api_device_get_status(mfconn * conn,
uint64_t * revision); uint64_t * revision);
int mfconn_api_device_get_changes(mfconn * conn, int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision, struct mfconn_device_change
uint64_t revision); **changes);
#endif #endif

View File

@@ -18,10 +18,11 @@
*/ */
#include <jansson.h> #include <jansson.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <stddef.h>
#include "../../utils/http.h" #include "../../utils/http.h"
#include "../../utils/json.h" #include "../../utils/json.h"
@@ -30,7 +31,14 @@
static int _decode_device_get_changes(mfhttp * conn, void *data); static int _decode_device_get_changes(mfhttp * conn, void *data);
int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision) /*
* the mfconn_device_change array will be realloc'ed so it must either point
* to a malloc'ed memory or be NULL
*
* the returned array will be sorted by revision
*/
int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision,
struct mfconn_device_change **changes)
{ {
const char *api_call; const char *api_call;
int retval; int retval;
@@ -44,7 +52,9 @@ int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision)
"&response_format=json", revision); "&response_format=json", revision);
http = http_create(); http = http_create();
retval = http_get_buf(http, api_call, _decode_device_get_changes, NULL); retval =
http_get_buf(http, api_call, _decode_device_get_changes,
(void *)changes);
http_destroy(http); http_destroy(http);
free((void *)api_call); free((void *)api_call);
@@ -52,6 +62,30 @@ int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision)
return retval; return retval;
} }
static void aux(json_t * key, json_t * revision,
enum mfconn_device_change_type change,
struct mfconn_device_change **changes, size_t * len_changes)
{
struct mfconn_device_change *tmp_change;
if (key == NULL || revision == NULL)
return;
(*len_changes)++;
*changes = (struct mfconn_device_change *)
realloc(*changes,
(*len_changes) * sizeof(struct mfconn_device_change));
tmp_change = *changes + (*len_changes) - 1;
tmp_change->change = change;
strncpy(tmp_change->key, json_string_value(key), sizeof(tmp_change->key));
tmp_change->revision = atoll(json_string_value(revision));
}
static int change_compare(const void *a, const void *b)
{
return ((struct mfconn_device_change *)a)->revision
- ((struct mfconn_device_change *)b)->revision;
}
static int _decode_device_get_changes(mfhttp * conn, void *user_ptr) static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
{ {
json_error_t error; json_error_t error;
@@ -61,57 +95,50 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
json_t *obj_array; json_t *obj_array;
json_t *key; json_t *key;
json_t *parent;
json_t *revision; json_t *revision;
int array_sz; int array_sz;
int i = 0; int i = 0;
if (user_ptr != NULL) struct mfconn_device_change **changes;
size_t len_changes;
changes = (struct mfconn_device_change **)user_ptr;
if (changes == NULL)
return -1; return -1;
root = http_parse_buf_json(conn, 0, &error); root = http_parse_buf_json(conn, 0, &error);
len_changes = 0;
node = json_object_by_path(root, "response/updated"); node = json_object_by_path(root, "response/updated");
obj_array = json_object_get(node, "files"); obj_array = json_object_get(node, "files");
if (json_is_array(obj_array)) { if (json_is_array(obj_array)) {
array_sz = json_array_size(obj_array); array_sz = json_array_size(obj_array);
printf("updated files:\n");
for (i = 0; i < array_sz; i++) { for (i = 0; i < array_sz; i++) {
data = json_array_get(obj_array, i); data = json_array_get(obj_array, i);
if (!json_is_object(data))
if (json_is_object(data)) { continue;
key = json_object_get(data, "quickkey"); key = json_object_get(data, "quickkey");
parent = json_object_get(data, "parent_folderkey"); revision = json_object_get(data, "revision");
revision = json_object_get(data, "revision"); aux(key, revision, MFCONN_DEVICE_CHANGE_UPDATED_FILE, changes,
&len_changes);
printf(" %s %s %s\n\r", json_string_value(key),
json_string_value(parent), json_string_value(revision));
}
} }
printf("\n");
} }
node = json_object_by_path(root, "response/updated");
obj_array = json_object_get(node, "folders"); obj_array = json_object_get(node, "folders");
if (json_is_array(obj_array)) { if (json_is_array(obj_array)) {
array_sz = json_array_size(obj_array); array_sz = json_array_size(obj_array);
printf("updated folders:\n");
for (i = 0; i < array_sz; i++) { for (i = 0; i < array_sz; i++) {
data = json_array_get(obj_array, i); data = json_array_get(obj_array, i);
if (!json_is_object(data))
if (json_is_object(data)) { continue;
key = json_object_get(data, "folderkey"); key = json_object_get(data, "folderkey");
parent = json_object_get(data, "parent_folderkey"); revision = json_object_get(data, "revision");
revision = json_object_get(data, "revision"); aux(key, revision, MFCONN_DEVICE_CHANGE_UPDATED_FOLDER, changes,
&len_changes);
printf(" %s %s %s\n\r", json_string_value(key),
json_string_value(parent), json_string_value(revision));
}
} }
printf("\n");
} }
node = json_object_by_path(root, "response/deleted"); node = json_object_by_path(root, "response/deleted");
@@ -119,42 +146,39 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
obj_array = json_object_get(node, "files"); obj_array = json_object_get(node, "files");
if (json_is_array(obj_array)) { if (json_is_array(obj_array)) {
array_sz = json_array_size(obj_array); array_sz = json_array_size(obj_array);
printf("deleted files:\n");
for (i = 0; i < array_sz; i++) { for (i = 0; i < array_sz; i++) {
data = json_array_get(obj_array, i); data = json_array_get(obj_array, i);
if (!json_is_object(data))
if (json_is_object(data)) { continue;
key = json_object_get(data, "quickkey"); key = json_object_get(data, "quickkey");
parent = json_object_get(data, "parent_folderkey"); revision = json_object_get(data, "revision");
revision = json_object_get(data, "revision"); aux(key, revision, MFCONN_DEVICE_CHANGE_DELETED_FILE, changes,
&len_changes);
printf(" %s %s %s\n\r", json_string_value(key),
json_string_value(parent), json_string_value(revision));
}
} }
printf("\n");
} }
node = json_object_by_path(root, "response/deleted");
obj_array = json_object_get(node, "folders"); obj_array = json_object_get(node, "folders");
if (json_is_array(obj_array)) { if (json_is_array(obj_array)) {
array_sz = json_array_size(obj_array); array_sz = json_array_size(obj_array);
printf("deleted folders:\n");
for (i = 0; i < array_sz; i++) { for (i = 0; i < array_sz; i++) {
data = json_array_get(obj_array, i); data = json_array_get(obj_array, i);
if (!json_is_object(data))
if (json_is_object(data)) { continue;
key = json_object_get(data, "folderkey"); key = json_object_get(data, "folderkey");
parent = json_object_get(data, "parent_folderkey"); revision = json_object_get(data, "revision");
revision = json_object_get(data, "revision"); aux(key, revision, MFCONN_DEVICE_CHANGE_DELETED_FOLDER, changes,
&len_changes);
printf(" %s %s %s\n\r", json_string_value(key),
json_string_value(parent), json_string_value(revision));
}
} }
printf("\n");
} }
// sort
qsort(*changes, len_changes, sizeof(struct mfconn_device_change),
change_compare);
// put a zero valued entry at the end
len_changes++;
*changes = (struct mfconn_device_change *)
realloc(*changes, len_changes * sizeof(struct mfconn_device_change));
memset(*changes + len_changes - 1, 0, sizeof(struct mfconn_device_change));
if (root != NULL) if (root != NULL)
json_decref(root); json_decref(root);

View File

@@ -40,6 +40,10 @@ static int _decode_folder_get_content_files(mfhttp * conn, void *data);
* mffolder_result and mffile_result so make sure that those are either NULL * mffolder_result and mffile_result so make sure that those are either NULL
* or values of already malloc'ed regions. They must not be uninitialized * or values of already malloc'ed regions. They must not be uninitialized
* values. * values.
*
* results are triple pointers because we cannot create an array of mffolder
* or mffile as we do not know their sizes. We can only create an array of
* pointers to them.
*/ */
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,

View File

@@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <inttypes.h>
#include "../../mfapi/apicalls.h" #include "../../mfapi/apicalls.h"
#include "../mfshell.h" #include "../mfshell.h"
@@ -31,6 +32,8 @@ int mfshell_cmd_changes(mfshell * mfshell, int argc, char *const argv[])
(void)argv; (void)argv;
int retval; int retval;
uint64_t revision; uint64_t revision;
struct mfconn_device_change *changes;
int i;
if (mfshell == NULL) if (mfshell == NULL)
return -1; return -1;
@@ -47,8 +50,32 @@ int mfshell_cmd_changes(mfshell * mfshell, int argc, char *const argv[])
return -1; return -1;
} }
retval = mfconn_api_device_get_changes(mfshell->conn, revision); changes = NULL;
retval = mfconn_api_device_get_changes(mfshell->conn, revision, &changes);
mfconn_update_secret_key(mfshell->conn); mfconn_update_secret_key(mfshell->conn);
for (i = 0; changes[i].revision != 0; i++) {
switch (changes[i].change) {
case MFCONN_DEVICE_CHANGE_DELETED_FOLDER:
printf("%" PRIu64 " deleted folder: %s\n", changes[i].revision,
changes[i].key);
break;
case MFCONN_DEVICE_CHANGE_DELETED_FILE:
printf("%" PRIu64 " deleted file: %s\n", changes[i].revision,
changes[i].key);
break;
case MFCONN_DEVICE_CHANGE_UPDATED_FOLDER:
printf("%" PRIu64 " updated folder: %s\n", changes[i].revision,
changes[i].key);
break;
case MFCONN_DEVICE_CHANGE_UPDATED_FILE:
printf("%" PRIu64 " updated file: %s\n", changes[i].revision,
changes[i].key);
break;
}
}
free(changes);
return retval; return retval;
} }

View File

@@ -21,4 +21,4 @@ if [ ! -f "./.mediafire-tools.conf" -a ! -f "~/.mediafire-tools.conf" ]; then
echo "no configuration file found" >&2 echo "no configuration file found" >&2
exit 1 exit 1
fi fi
$cmd "${binary_dir}/mediafire-shell" -c "help; ls; whoami" $cmd "${binary_dir}/mediafire-shell" -c "help; ls; changes"