Add support for updating local cache through patches

- refactor some code:
   - code handling the file cache is now in fuse/filecache.c
   - base36 and base16 encoding and decoding moved to utils/hash.c
 - mfshell: add "updates" command calling device/get_updates
 - add container class to store patch information as mfapi/patch.c
 - apicalls: store more info retrieved by file/get_info
 - apicalls: add device/get_updates
 - apicalls: add device/get_patch
This commit is contained in:
josch
2014-12-01 16:30:32 +01:00
parent 5034d361ba
commit febb29f75c
16 changed files with 1312 additions and 231 deletions

View File

@@ -24,6 +24,7 @@
#include "file.h"
#include "folder.h"
#include "patch.h"
#include "mfconn.h"
#define MFAPI_MAX_LEN_KEY 15
@@ -81,4 +82,15 @@ int mfconn_api_device_get_status(mfconn * conn,
int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision, struct mfconn_device_change
**changes);
int mfconn_api_device_get_updates(mfconn * conn,
const char *quickkey,
uint64_t revision,
uint64_t target_revision,
mfpatch *** patches);
int mfconn_api_device_get_patch(mfconn * conn, mfpatch * patch,
const char *quickkey,
uint64_t source_revision,
uint64_t target_revision);
#endif

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <jansson.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "../../utils/http.h"
#include "../../utils/json.h"
#include "../mfconn.h"
#include "../patch.h"
#include "../apicalls.h" // IWYU pragma: keep
static int _decode_device_get_patch(mfhttp * conn, void *data);
int mfconn_api_device_get_patch(mfconn * conn, mfpatch * patch,
const char *quickkey, uint64_t source_revision,
uint64_t target_revision)
{
const char *api_call;
int len;
mfhttp *http;
int retval;
if (conn == NULL)
return -1;
if (patch == NULL)
return -1;
if (quickkey == NULL)
return -1;
len = strlen(quickkey);
if (len != 15)
return -1;
api_call = mfconn_create_signed_get(conn, 0, "device/get_patch.php",
"?quick_key=%s"
"&source_revision=%" PRIu64
"&target_revision=%" PRIu64
"&response_format=json", quickkey,
source_revision, target_revision);
http = http_create();
retval = http_get_buf(http, api_call, _decode_device_get_patch,
(void *)patch);
http_destroy(http);
free((void *)api_call);
return retval;
}
static int _decode_device_get_patch(mfhttp * conn, void *data)
{
json_error_t error;
json_t *obj;
mfpatch *patch;
json_t *root;
json_t *node;
if (data == NULL)
return -1;
patch = (mfpatch *) data;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root, "response");
obj = json_object_get(node, "patch_hash");
if (obj != NULL)
patch_set_hash(patch, json_string_value(obj));
obj = json_object_get(node, "patch_link");
if (obj != NULL)
patch_set_link(patch, json_string_value(obj));
if (root != NULL)
json_decref(root);
return 0;
}

View File

@@ -0,0 +1,156 @@
/*
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stddef.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <jansson.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../utils/http.h"
#include "../../utils/json.h"
#include "../patch.h"
#include "../mfconn.h"
#include "../apicalls.h" // IWYU pragma: keep
static int _decode_device_get_updates(mfhttp * conn, void *data);
int mfconn_api_device_get_updates(mfconn * conn, const char *quickkey,
uint64_t revision, uint64_t target_revision,
mfpatch *** patches)
{
const char *api_call;
int len;
mfhttp *http;
int retval;
if (conn == NULL)
return -1;
if (patches == NULL)
return -1;
if (quickkey == NULL)
return -1;
len = strlen(quickkey);
if (len != 15)
return -1;
if (target_revision == 0) {
api_call = mfconn_create_signed_get(conn, 0, "device/get_updates.php",
"?quick_key=%s"
"&revision=%" PRIu64
"&response_format=json", quickkey,
revision);
} else {
api_call = mfconn_create_signed_get(conn, 0, "device/get_updates.php",
"?quick_key=%s"
"&revision=%" PRIu64
"&target_revision=%" PRIu64
"&response_format=json", quickkey,
revision, target_revision);
}
http = http_create();
retval = http_get_buf(http, api_call, _decode_device_get_updates,
(void *)patches);
http_destroy(http);
free((void *)api_call);
return retval;
}
static int _decode_device_get_updates(mfhttp * conn, void *user_ptr)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *obj_array;
json_t *data;
json_t *source_revision;
json_t *target_revision;
json_t *source_hash;
json_t *target_hash;
json_t *patch_hash;
int array_sz;
int i;
mfpatch ***patches;
mfpatch *tmp_patch;
size_t len_patches;
if (user_ptr == NULL)
return -1;
patches = (mfpatch ***) user_ptr;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root, "response");
len_patches = 0;
obj_array = json_object_get(node, "updates");
if (json_is_array(obj_array)) {
array_sz = json_array_size(obj_array);
for (i = 0; i < array_sz; i++) {
data = json_array_get(obj_array, i);
if (!json_is_object(data))
continue;
source_revision = json_object_get(data, "source_revision");
target_revision = json_object_get(data, "target_revision");
source_hash = json_object_get(data, "source_hash");
target_hash = json_object_get(data, "target_hash");
patch_hash = json_object_get(data, "patch_hash");
if (source_revision == NULL || target_revision == NULL
|| source_hash == NULL || target_hash == NULL
|| patch_hash == NULL) {
fprintf(stderr, "patch with missing info\n");
}
tmp_patch = patch_alloc();
patch_set_source_revision(tmp_patch,
atoll(json_string_value
(source_revision)));
patch_set_target_revision(tmp_patch,
atoll(json_string_value
(target_revision)));
patch_set_source_hash(tmp_patch, json_string_value(source_hash));
patch_set_target_hash(tmp_patch, json_string_value(target_hash));
patch_set_hash(tmp_patch, json_string_value(patch_hash));
len_patches++;
*patches = (mfpatch **) realloc(*patches,
len_patches * sizeof(mfpatch *));
(*patches)[len_patches - 1] = tmp_patch;
}
}
// append a terminating empty patch
len_patches++;
*patches = (mfpatch **) realloc(*patches, len_patches * sizeof(mfpatch *));
// write an empty last element
(*patches)[len_patches - 1] = NULL;
if (root != NULL)
json_decref(root);
return 0;
}

View File

@@ -17,10 +17,12 @@
*
*/
#define _XOPEN_SOURCE // for strptime
#include <jansson.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../../utils/http.h"
#include "../../utils/json.h"
@@ -74,6 +76,8 @@ static int _decode_file_get_info(mfhttp * conn, void *data)
json_t *quickkey;
int retval = 0;
mffile *file;
char *ret;
struct tm tm;
if (data == NULL)
return -1;
@@ -101,10 +105,32 @@ static int _decode_file_get_info(mfhttp * conn, void *data)
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);
obj = json_object_get(node, "created");
if (obj != NULL) {
memset(&tm, 0, sizeof(struct tm));
ret = strptime(json_string_value(obj), "%F %T", &tm);
if (ret[0] != '\0') {
fprintf(stderr, "cannot parse time\n");
} else {
file_set_created(file, mktime(&tm));
}
}
obj = json_object_get(node, "revision");
if (obj != NULL) {
file_set_revision(file, atoll(json_string_value(obj)));
}
obj = json_object_get(node, "size");
if (obj != NULL) {
file_set_size(file, atoll(json_string_value(obj)));
}
if (quickkey == NULL)
retval = -1;

View File

@@ -112,7 +112,7 @@ static int _decode_folder_get_info(mfhttp * conn, void *data)
revision = json_object_get(node, "revision");
if (revision != NULL) {
folder_set_revision(folder, atol(json_string_value(revision)));
folder_set_revision(folder, atoll(json_string_value(revision)));
}
created = json_object_get(node, "created");

213
mfapi/patch.c Normal file
View File

@@ -0,0 +1,213 @@
/*
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#define _POSIX_C_SOURCE 200809L // for strdup
#include <stdint.h>
#include <openssl/sha.h>
#include <string.h>
#include <stdlib.h>
#include "patch.h"
struct mfpatch {
/* revision of the file to be patched */
uint64_t source_revision;
/* revision of the target file */
uint64_t target_revision;
/* hash of the patch */
char hash[SHA256_DIGEST_LENGTH * 2 + 1];
/* hash of the file to be patched */
char source_hash[SHA256_DIGEST_LENGTH * 2 + 1];
/* hash of the patched file */
char target_hash[SHA256_DIGEST_LENGTH * 2 + 1];
/* expected size of the patched file */
uint64_t target_size;
char *link;
};
mfpatch *patch_alloc(void)
{
mfpatch *patch;
patch = (mfpatch *) calloc(1, sizeof(mfpatch));
return patch;
}
void patch_free(mfpatch * patch)
{
if (patch == NULL)
return;
if (patch->link != NULL)
free(patch->link);
free(patch);
return;
}
uint64_t patch_get_source_revision(mfpatch * patch)
{
if (patch == NULL)
return -1;
return patch->source_revision;
}
int patch_set_source_revision(mfpatch * patch, uint64_t revision)
{
if (patch == NULL)
return -1;
patch->source_revision = revision;
return 0;
}
uint64_t patch_get_target_revision(mfpatch * patch)
{
if (patch == NULL)
return -1;
return patch->target_revision;
}
int patch_set_target_revision(mfpatch * patch, uint64_t revision)
{
if (patch == NULL)
return -1;
patch->target_revision = revision;
return 0;
}
const char *patch_get_hash(mfpatch * patch)
{
if (patch == NULL)
return NULL;
return patch->hash;
}
int patch_set_hash(mfpatch * patch, const char *hash)
{
if (patch == NULL)
return -1;
if (hash == NULL)
return -1;
if (strlen(hash) < 32)
return -1;
memset(patch->hash, 0, sizeof(patch->hash));
strncpy(patch->hash, hash, sizeof(patch->hash) - 1);
return 0;
}
const char *patch_get_source_hash(mfpatch * patch)
{
if (patch == NULL)
return NULL;
return patch->source_hash;
}
int patch_set_source_hash(mfpatch * patch, const char *hash)
{
if (patch == NULL)
return -1;
if (hash == NULL)
return -1;
if (strlen(hash) < 32)
return -1;
memset(patch->source_hash, 0, sizeof(patch->source_hash));
strncpy(patch->source_hash, hash, sizeof(patch->source_hash) - 1);
return 0;
}
const char *patch_get_target_hash(mfpatch * patch)
{
if (patch == NULL)
return NULL;
return patch->target_hash;
}
int patch_set_target_hash(mfpatch * patch, const char *hash)
{
if (patch == NULL)
return -1;
if (hash == NULL)
return -1;
if (strlen(hash) < 32)
return -1;
memset(patch->target_hash, 0, sizeof(patch->target_hash));
strncpy(patch->target_hash, hash, sizeof(patch->target_hash) - 1);
return 0;
}
uint64_t patch_get_target_size(mfpatch * patch)
{
if (patch == NULL)
return -1;
return patch->target_size;
}
int patch_set_target_size(mfpatch * patch, uint64_t size)
{
if (patch == NULL)
return -1;
patch->target_size = size;
return 0;
}
const char *patch_get_link(mfpatch * patch)
{
if (patch == NULL)
return NULL;
return patch->link;
}
int patch_set_link(mfpatch * patch, const char *link)
{
if (patch == NULL)
return -1;
if (link == NULL)
return -1;
if (patch->link != NULL) {
free(patch->link);
}
patch->link = strdup(link);
return 0;
}

56
mfapi/patch.h Normal file
View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef __MFAPI_PATCH_H__
#define __MFAPI_PATCH_H__
typedef struct mfpatch mfpatch;
mfpatch *patch_alloc(void);
void patch_free(mfpatch * patch);
uint64_t patch_get_source_revision(mfpatch * patch);
int patch_set_source_revision(mfpatch * patch, uint64_t revision);
uint64_t patch_get_target_revision(mfpatch * patch);
int patch_set_target_revision(mfpatch * patch, uint64_t revision);
const char *patch_get_hash(mfpatch * patch);
int patch_set_hash(mfpatch * patch, const char *hash);
const char *patch_get_source_hash(mfpatch * patch);
int patch_set_source_hash(mfpatch * patch, const char *hash);
const char *patch_get_target_hash(mfpatch * patch);
int patch_set_target_hash(mfpatch * patch, const char *hash);
uint64_t patch_get_target_size(mfpatch * patch);
int patch_set_target_size(mfpatch * patch, uint64_t size);
const char *patch_get_link(mfpatch * patch);
int patch_set_link(mfpatch * patch, const char *link);
#endif