mirror of
https://github.com/xorgy/mediafire-fuse
synced 2026-01-13 13:14:29 -08:00
Optimize device/get_changes updates
- don't use folder/get_content but rely on parent information - add many more debugging information and sanity checks - ignore trash - correctly update local device revision using device_response - remove "visited" member and change housekeeping function to check for consistency of parents and children (and retrieve fix from remote if necessary) - print key and parent key in the debugging function - check for remote updates on every fs function - introduce global defines for key and filename length - add parent member, getters and setters to file.h
This commit is contained in:
@@ -26,17 +26,22 @@
|
||||
#include "folder.h"
|
||||
#include "mfconn.h"
|
||||
|
||||
#define MFAPI_MAX_LEN_KEY 15
|
||||
#define MFAPI_MAX_LEN_NAME 255
|
||||
|
||||
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
|
||||
MFCONN_DEVICE_CHANGE_UPDATED_FILE,
|
||||
MFCONN_DEVICE_CHANGE_END
|
||||
};
|
||||
|
||||
struct mfconn_device_change {
|
||||
enum mfconn_device_change_type change;
|
||||
char key[20];
|
||||
char key[16];
|
||||
uint64_t revision;
|
||||
char parent[16];
|
||||
};
|
||||
|
||||
int mfconn_api_file_get_info(mfconn * conn, mffile * file,
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../utils/http.h"
|
||||
#include "../../utils/json.h"
|
||||
@@ -62,14 +63,16 @@ int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision,
|
||||
return retval;
|
||||
}
|
||||
|
||||
static void aux(json_t * key, json_t * revision,
|
||||
static void aux(json_t * key, json_t * parent, 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)
|
||||
if (key == NULL || revision == NULL || parent == NULL) {
|
||||
fprintf(stderr, "change without either key, revision or parent");
|
||||
return;
|
||||
}
|
||||
(*len_changes)++;
|
||||
*changes = (struct mfconn_device_change *)
|
||||
realloc(*changes,
|
||||
@@ -77,6 +80,8 @@ static void aux(json_t * key, json_t * revision,
|
||||
tmp_change = *changes + (*len_changes) - 1;
|
||||
tmp_change->change = change;
|
||||
strncpy(tmp_change->key, json_string_value(key), sizeof(tmp_change->key));
|
||||
strncpy(tmp_change->parent, json_string_value(parent),
|
||||
sizeof(tmp_change->parent));
|
||||
tmp_change->revision = atoll(json_string_value(revision));
|
||||
}
|
||||
|
||||
@@ -95,7 +100,9 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
|
||||
|
||||
json_t *obj_array;
|
||||
json_t *key;
|
||||
json_t *parent;
|
||||
json_t *revision;
|
||||
json_t *device_revision;
|
||||
|
||||
int array_sz;
|
||||
int i = 0;
|
||||
@@ -109,6 +116,15 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
|
||||
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
node = json_object_by_path(root, "response");
|
||||
|
||||
device_revision = json_object_get(node, "device_revision");
|
||||
if (device_revision == NULL) {
|
||||
fprintf(stderr,
|
||||
"response/device_revision is not part of the result\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len_changes = 0;
|
||||
|
||||
node = json_object_by_path(root, "response/updated");
|
||||
@@ -121,9 +137,10 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
|
||||
if (!json_is_object(data))
|
||||
continue;
|
||||
key = json_object_get(data, "quickkey");
|
||||
parent = json_object_get(data, "parent_folderkey");
|
||||
revision = json_object_get(data, "revision");
|
||||
aux(key, revision, MFCONN_DEVICE_CHANGE_UPDATED_FILE, changes,
|
||||
&len_changes);
|
||||
aux(key, parent, revision, MFCONN_DEVICE_CHANGE_UPDATED_FILE,
|
||||
changes, &len_changes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,9 +152,10 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
|
||||
if (!json_is_object(data))
|
||||
continue;
|
||||
key = json_object_get(data, "folderkey");
|
||||
parent = json_object_get(data, "parent_folderkey");
|
||||
revision = json_object_get(data, "revision");
|
||||
aux(key, revision, MFCONN_DEVICE_CHANGE_UPDATED_FOLDER, changes,
|
||||
&len_changes);
|
||||
aux(key, parent, revision, MFCONN_DEVICE_CHANGE_UPDATED_FOLDER,
|
||||
changes, &len_changes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,9 +169,10 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
|
||||
if (!json_is_object(data))
|
||||
continue;
|
||||
key = json_object_get(data, "quickkey");
|
||||
parent = json_object_get(data, "parent_folderkey");
|
||||
revision = json_object_get(data, "revision");
|
||||
aux(key, revision, MFCONN_DEVICE_CHANGE_DELETED_FILE, changes,
|
||||
&len_changes);
|
||||
aux(key, parent, revision, MFCONN_DEVICE_CHANGE_DELETED_FILE,
|
||||
changes, &len_changes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,20 +184,25 @@ static int _decode_device_get_changes(mfhttp * conn, void *user_ptr)
|
||||
if (!json_is_object(data))
|
||||
continue;
|
||||
key = json_object_get(data, "folderkey");
|
||||
parent = json_object_get(data, "parent_folderkey");
|
||||
revision = json_object_get(data, "revision");
|
||||
aux(key, revision, MFCONN_DEVICE_CHANGE_DELETED_FOLDER, changes,
|
||||
&len_changes);
|
||||
aux(key, parent, revision, MFCONN_DEVICE_CHANGE_DELETED_FOLDER,
|
||||
changes, &len_changes);
|
||||
}
|
||||
}
|
||||
// sort
|
||||
qsort(*changes, len_changes, sizeof(struct mfconn_device_change),
|
||||
change_compare);
|
||||
|
||||
// put a zero valued entry at the end
|
||||
// put an entry with change type MFCONN_DEVICE_CHANGE_END at the end
|
||||
// encode the current device revision as its revision
|
||||
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));
|
||||
(*changes)[len_changes - 1].change = MFCONN_DEVICE_CHANGE_END;
|
||||
(*changes)[len_changes - 1].revision =
|
||||
atoll(json_string_value(device_revision));
|
||||
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
@@ -69,9 +69,8 @@ static int _decode_file_get_info(mfhttp * conn, void *data)
|
||||
json_error_t error;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *obj;
|
||||
json_t *quickkey;
|
||||
json_t *file_hash;
|
||||
json_t *file_name;
|
||||
int retval = 0;
|
||||
mffile *file;
|
||||
|
||||
@@ -88,15 +87,23 @@ static int _decode_file_get_info(mfhttp * conn, void *data)
|
||||
if (quickkey != NULL)
|
||||
file_set_key(file, json_string_value(quickkey));
|
||||
|
||||
file_name = json_object_get(node, "filename");
|
||||
if (file_name != NULL)
|
||||
file_set_name(file, json_string_value(file_name));
|
||||
obj = json_object_get(node, "filename");
|
||||
if (obj != NULL)
|
||||
file_set_name(file, json_string_value(obj));
|
||||
|
||||
file_hash = json_object_get(node, "hash");
|
||||
if (file_hash != NULL) {
|
||||
file_set_hash(file, json_string_value(file_hash));
|
||||
obj = json_object_get(node, "hash");
|
||||
if (obj != NULL) {
|
||||
file_set_hash(file, json_string_value(obj));
|
||||
}
|
||||
|
||||
obj = json_object_get(node, "parent_folderkey");
|
||||
if (obj != NULL) {
|
||||
file_set_parent(file, json_string_value(obj));
|
||||
}
|
||||
// infer that the parent folder must be root
|
||||
if (obj == NULL && quickkey != NULL)
|
||||
file_set_parent(file, NULL);
|
||||
|
||||
if (quickkey == NULL)
|
||||
retval = -1;
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../utils/http.h"
|
||||
#include "../../utils/json.h"
|
||||
@@ -129,6 +130,7 @@ static int _decode_folder_get_content_folders(mfhttp * conn, void *user_ptr)
|
||||
|
||||
folders_array = json_object_get(node, "folders");
|
||||
if (!json_is_array(folders_array)) {
|
||||
fprintf(stderr, "is not an array: folders");
|
||||
json_decref(root);
|
||||
return -1;
|
||||
}
|
||||
@@ -225,6 +227,7 @@ static int _decode_folder_get_content_files(mfhttp * conn, void *user_ptr)
|
||||
|
||||
files_array = json_object_get(node, "files");
|
||||
if (!json_is_array(files_array)) {
|
||||
fprintf(stderr, "is not an array: files");
|
||||
json_decref(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
37
mfapi/file.c
37
mfapi/file.c
@@ -27,11 +27,13 @@
|
||||
#include "../utils/http.h"
|
||||
#include "../utils/strings.h"
|
||||
#include "file.h"
|
||||
#include "apicalls.h"
|
||||
|
||||
struct mffile {
|
||||
char quickkey[18];
|
||||
char quickkey[MFAPI_MAX_LEN_KEY + 1];
|
||||
char parent[MFAPI_MAX_LEN_NAME + 1];
|
||||
char hash[65];
|
||||
char name[256];
|
||||
char name[MFAPI_MAX_LEN_NAME + 1];
|
||||
time_t created;
|
||||
uint64_t revision;
|
||||
uint64_t size;
|
||||
@@ -81,7 +83,7 @@ int file_set_key(mffile * file, const char *key)
|
||||
return -1;
|
||||
|
||||
memset(file->quickkey, 0, sizeof(file->quickkey));
|
||||
strncpy(file->quickkey, key, sizeof(file->quickkey) - 1);
|
||||
strncpy(file->quickkey, key, sizeof(file->quickkey));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -94,6 +96,33 @@ const char *file_get_key(mffile * file)
|
||||
return file->quickkey;
|
||||
}
|
||||
|
||||
int file_set_parent(mffile * file, const char *parent_key)
|
||||
{
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
if (parent_key == NULL) {
|
||||
memset(file->parent, 0, sizeof(file->parent));
|
||||
} else {
|
||||
memset(file->parent, 0, sizeof(file->parent));
|
||||
strncpy(file->parent, parent_key, sizeof(file->parent));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *file_get_parent(mffile * file)
|
||||
{
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
if (file->parent[0] == '\0') {
|
||||
return NULL;
|
||||
} else {
|
||||
return file->parent;
|
||||
}
|
||||
}
|
||||
|
||||
int file_set_hash(mffile * file, const char *hash)
|
||||
{
|
||||
if (file == NULL)
|
||||
@@ -130,7 +159,7 @@ int file_set_name(mffile * file, const char *name)
|
||||
return -1;
|
||||
|
||||
memset(file->name, 0, sizeof(file->name));
|
||||
strncpy(file->name, name, sizeof(file->name) - 1);
|
||||
strncpy(file->name, name, sizeof(file->name));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,10 @@ int file_set_key(mffile * file, const char *quickkey);
|
||||
|
||||
const char *file_get_key(mffile * file);
|
||||
|
||||
int file_set_parent(mffile * file, const char *parent_key);
|
||||
|
||||
const char *file_get_parent(mffile * file);
|
||||
|
||||
int file_set_hash(mffile * file, const char *hash);
|
||||
|
||||
const char *file_get_hash(mffile * file);
|
||||
|
||||
@@ -22,11 +22,12 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "folder.h"
|
||||
#include "apicalls.h"
|
||||
|
||||
struct mffolder {
|
||||
char folderkey[20];
|
||||
char name[41];
|
||||
char parent[20];
|
||||
char folderkey[MFAPI_MAX_LEN_KEY + 1];
|
||||
char name[MFAPI_MAX_LEN_NAME + 1];
|
||||
char parent[MFAPI_MAX_LEN_KEY + 1];
|
||||
uint64_t revision;
|
||||
time_t created;
|
||||
};
|
||||
@@ -62,7 +63,7 @@ int folder_set_key(mffolder * folder, const char *key)
|
||||
return -1;
|
||||
|
||||
memset(folder->folderkey, 0, sizeof(folder->folderkey));
|
||||
strncpy(folder->folderkey, key, sizeof(folder->folderkey) - 1);
|
||||
strncpy(folder->folderkey, key, sizeof(folder->folderkey));
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -88,12 +89,8 @@ int folder_set_parent(mffolder * folder, const char *parent_key)
|
||||
if (parent_key == NULL) {
|
||||
memset(folder->parent, 0, sizeof(folder->parent));
|
||||
} else {
|
||||
if (strlen(parent_key) != 13) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(folder->parent, 0, sizeof(folder->parent));
|
||||
strncpy(folder->parent, parent_key, sizeof(folder->parent) - 1);
|
||||
strncpy(folder->parent, parent_key, sizeof(folder->parent));
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -104,7 +101,11 @@ const char *folder_get_parent(mffolder * folder)
|
||||
if (folder == NULL)
|
||||
return NULL;
|
||||
|
||||
return folder->parent;
|
||||
if (folder->parent[0] == '\0') {
|
||||
return NULL;
|
||||
} else {
|
||||
return folder->parent;
|
||||
}
|
||||
}
|
||||
|
||||
int folder_set_name(mffolder * folder, const char *name)
|
||||
@@ -118,7 +119,7 @@ int folder_set_name(mffolder * folder, const char *name)
|
||||
return -1;
|
||||
|
||||
memset(folder->name, 0, sizeof(folder->name));
|
||||
strncpy(folder->name, name, sizeof(folder->name) - 1);
|
||||
strncpy(folder->name, name, sizeof(folder->name));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user