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

@@ -10,16 +10,16 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -Werror")
# the following is until we learn how to reorder the gcc arguments to correctly link on Ubuntu # the following is until we learn how to reorder the gcc arguments to correctly link on Ubuntu
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-as-needed") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-as-needed")
add_library(mfapi SHARED mfapi/mfconn.c mfapi/file.c mfapi/folder.c mfapi/apicalls/file_get_info.c mfapi/apicalls/user_get_info.c mfapi/apicalls/file_get_links.c mfapi/apicalls/user_session.c mfapi/apicalls/folder_get_info.c mfapi/apicalls/folder_create.c mfapi/apicalls/folder_get_content.c mfapi/apicalls/folder_delete.c mfapi/apicalls/device_get_status.c mfapi/apicalls/device_get_changes.c) add_library(mfapi SHARED mfapi/mfconn.c mfapi/file.c mfapi/folder.c mfapi/patch.c mfapi/apicalls/file_get_info.c mfapi/apicalls/user_get_info.c mfapi/apicalls/file_get_links.c mfapi/apicalls/user_session.c mfapi/apicalls/folder_get_info.c mfapi/apicalls/folder_create.c mfapi/apicalls/folder_get_content.c mfapi/apicalls/folder_delete.c mfapi/apicalls/device_get_status.c mfapi/apicalls/device_get_changes.c mfapi/apicalls/device_get_patch.c mfapi/apicalls/device_get_updates.c)
add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c utils/xdelta3.c utils/hash.c) add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c utils/xdelta3.c utils/hash.c)
add_executable(mediafire-shell mfshell/main.c mfshell/mfshell.c mfshell/commands/folder.c mfshell/commands/auth.c mfshell/commands/chdir.c mfshell/commands/debug.c mfshell/commands/file.c mfshell/commands/get.c mfshell/commands/help.c mfshell/commands/host.c mfshell/commands/lcd.c mfshell/commands/links.c mfshell/commands/list.c mfshell/commands/lpwd.c mfshell/commands/mkdir.c mfshell/commands/pwd.c mfshell/commands/whoami.c mfshell/commands/rmdir.c mfshell/commands/status.c mfshell/commands/changes.c mfshell/config.c mfshell/options.c) add_executable(mediafire-shell mfshell/main.c mfshell/mfshell.c mfshell/commands/folder.c mfshell/commands/auth.c mfshell/commands/chdir.c mfshell/commands/debug.c mfshell/commands/file.c mfshell/commands/get.c mfshell/commands/help.c mfshell/commands/host.c mfshell/commands/lcd.c mfshell/commands/links.c mfshell/commands/list.c mfshell/commands/lpwd.c mfshell/commands/mkdir.c mfshell/commands/pwd.c mfshell/commands/whoami.c mfshell/commands/rmdir.c mfshell/commands/status.c mfshell/commands/changes.c mfshell/config.c mfshell/options.c mfshell/commands/updates.c)
target_link_libraries(mediafire-shell curl ssl crypto jansson mfapi mfutils) target_link_libraries(mediafire-shell curl ssl crypto jansson mfapi mfutils)
enable_testing() enable_testing()
add_executable(mediafire-fuse fuse/main.c fuse/hashtbl.c) add_executable(mediafire-fuse fuse/main.c fuse/hashtbl.c fuse/filecache.c)
set_target_properties(mediafire-fuse PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") set_target_properties(mediafire-fuse PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
target_link_libraries(mediafire-fuse curl ssl fuse jansson mfapi mfutils) target_link_libraries(mediafire-fuse curl ssl fuse jansson mfapi mfutils)

477
fuse/filecache.c Normal file
View File

@@ -0,0 +1,477 @@
/*
* 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 <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <openssl/sha.h>
#include "../utils/hash.h"
#include "../utils/xdelta3.h"
#include "../mfapi/file.h"
#include "../mfapi/apicalls.h"
#include "../mfapi/patch.h"
#include "../utils/http.h"
#include "../utils/strings.h"
static int filecache_check_integrity(const char * cachefile, uint64_t fsize, const unsigned char * fhash);
static int filecache_check_integrity_size(const char * cachefile, uint64_t fsize);
static int filecache_check_integrity_hash(const char * cachefile, const unsigned char * fhash);
static int filecache_update_file(const char * filecache_path, mfconn * conn, const char * quickkey, uint64_t local_revision, uint64_t remote_revision);
static int filecache_download_file(const char * filecache_path, const char * quickkey, uint64_t remote_revision, mfconn * conn);
static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64_t source_revision, uint64_t target_revision, const char * phash, const char * filecache_path);
static int filecache_patch_file(const char * filecache_path, const char * quickkey, uint64_t source_revision, uint64_t target_revision);
int filecache_open_file(const char * quickkey, uint64_t local_revision,
uint64_t remote_revision, uint64_t fsize, const unsigned char * fhash,
const char * filecache_path, mfconn * conn)
{
char *cachefile;
int fd;
int retval;
/* check if the requested file is already in the cache */
cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey,
remote_revision);
fd = open(cachefile, O_RDWR);
if (fd > 0) {
/* file existed - return handle */
free(cachefile);
return fd;
}
free(cachefile);
/* if the file with remote revision didn't exist, then check whether an
* old revision exists and in that case update that.
*
* Otherwise, download the file anew */
cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey,
local_revision);
fd = open(cachefile, O_RDWR);
free(cachefile);
if (fd > 0) {
close(fd);
/* file exists, so we have to update it with one or more patches from
* the remote */
retval = filecache_update_file(filecache_path, conn, quickkey, local_revision, remote_revision);
if (retval != 0) {
fprintf(stderr, "update_file failed\n");
return -1;
}
} else {
/* download the file */
retval = filecache_download_file(filecache_path, quickkey, remote_revision, conn);
if (retval != 0) {
fprintf(stderr, "filecache_download_file failed\n");
return -1;
}
}
/* check whether the patched or newly downloaded file matches the hash we
* have stored */
cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey,
remote_revision);
retval = filecache_check_integrity(cachefile, fsize, fhash);
if (retval != 0) {
fprintf(stderr, "checking integrity failed\n");
free(cachefile);
return -1;
}
fd = open(cachefile, O_RDWR);
free(cachefile);
/* return the file handle */
return fd;
}
static int filecache_check_integrity(const char * path, uint64_t fsize, const unsigned char * fhash) {
int retval;
retval = filecache_check_integrity_size(path, fsize);
if (retval != 0) {
fprintf(stderr, "filecache_check_integrity_size failed\n");
return -1;
}
retval = filecache_check_integrity_hash(path, fhash);
if (retval != 0) {
fprintf(stderr, "filecache_check_integrity_hash failed\n");
return -1;
}
return 0;
}
static int filecache_check_integrity_size(const char * path, uint64_t fsize)
{
struct stat file_info;
int retval;
uint64_t bytes_read;
/* check if the size of the downloaded file matches the expected size */
memset(&file_info, 0, sizeof(file_info));
retval = stat(path, &file_info);
if (retval != 0) {
fprintf(stderr, "stat failed\n");
return -1;
}
bytes_read = file_info.st_size;
if (bytes_read != fsize) {
fprintf(stderr,
"expected %" PRIu64 " bytes but got %" PRIu64 " bytes\n",
fsize, bytes_read);
return -1;
}
return 0;
}
static int filecache_check_integrity_hash(const char * path, const unsigned char * fhash)
{
int retval;
FILE *fh;
unsigned char hash[SHA256_DIGEST_LENGTH];
char *hexhash;
fh = fopen(path, "r");
if (fh == NULL) {
perror("cannot open file");
return -1;
}
retval = calc_sha256(fh, hash);
if (retval != 0) {
fprintf(stderr, "failed to calculate hash\n");
fclose(fh);
return -1;
}
fclose(fh);
if (memcmp(fhash, hash, SHA256_DIGEST_LENGTH) != 0) {
fprintf(stderr, "hashes are not equal\n");
hexhash = binary2hex(fhash, SHA256_DIGEST_LENGTH);
fprintf(stderr, "remote: %s\n", hexhash);
free(hexhash);
hexhash = binary2hex(hash, SHA256_DIGEST_LENGTH);
fprintf(stderr, "downloaded: %s\n", hexhash);
free(hexhash);
return -1;
}
return 0;
}
static int filecache_download_file(const char * filecache_path, const char * quickkey, uint64_t remote_revision, mfconn * conn)
{
const char *url;
mffile *file;
mfhttp *http;
char *cachefile;
int retval;
cachefile = strdup_printf("%s/%s_%d", filecache_path, quickkey,
remote_revision);
file = file_alloc();
retval = mfconn_api_file_get_links(conn, file, (char *)quickkey);
mfconn_update_secret_key(conn);
if (retval == -1) {
fprintf(stderr, "mfconn_api_file_get_links failed\n");
free(cachefile);
file_free(file);
return -1;
}
url = file_get_direct_link(file);
if (url == NULL) {
fprintf(stderr, "file_get_direct_link failed\n");
free(cachefile);
file_free(file);
return -1;
}
http = http_create();
retval = http_get_file(http, url, cachefile);
http_destroy(http);
if (retval != 0) {
fprintf(stderr, "download failed\n");
free(cachefile);
file_free(file);
return -1;
}
free(cachefile);
file_free(file);
return 0;
}
static int filecache_update_file(const char * filecache_path, mfconn * conn, const char * quickkey, uint64_t local_revision, uint64_t remote_revision)
{
unsigned char hash2[SHA256_DIGEST_LENGTH];
int retval;
int i;
uint64_t last_target_revision;
char *cachefile;
mfpatch **patches = NULL;
retval = mfconn_api_device_get_updates(conn, quickkey,
local_revision, remote_revision,
&patches);
mfconn_update_secret_key(conn);
if (retval != 0) {
fprintf(stderr, "device/get_updates api call unsuccessful\n");
return -1;
}
// if no patches are returned, then the full file has to be downloaded
if (patches[0] == NULL) {
free(patches);
retval = filecache_download_file(filecache_path, quickkey, remote_revision, conn);
if (retval != 0) {
fprintf(stderr, "filecache_download_file failed\n");
return -1;
}
return 0;
}
last_target_revision = local_revision;
// else, go through all patches and download and apply them
for (i = 0; patches[i] != NULL; i++) {
/* verify that the source revision is equal to the last target
* revision */
if (patch_get_source_revision(patches[i]) != last_target_revision) {
fprintf(stderr, "the source revision is unequal the last target revision\n");
break;
}
last_target_revision = patch_get_target_revision(patches[i]);
retval = filecache_download_patch(conn, quickkey, patch_get_source_revision(patches[i]), patch_get_target_revision(patches[i]), patch_get_hash(patches[i]), filecache_path);
if (retval != 0) {
fprintf(stderr, "filecache_download_patch failed\n");
break;
}
/* verify that the file to patch has the right hash */
cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey,
patch_get_source_revision(patches[i]));
hex2binary(patch_get_source_hash(patches[i]), hash2);
retval = filecache_check_integrity_hash(cachefile, hash2);
free(cachefile);
if (retval != 0) {
fprintf(stderr, "the source file has the wrong hash\n");
break;
}
/* now apply the patch in patchfile to the file in cachefile */
retval = filecache_patch_file(filecache_path, quickkey, patch_get_source_revision(patches[i]), patch_get_target_revision(patches[i]));
if (retval != 0) {
fprintf(stderr, "filecache_patch_file failed\n");
break;
}
/* verify that the patched file has the right hash */
cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey,
patch_get_target_revision(patches[i]));
hex2binary(patch_get_target_hash(patches[i]), hash2);
retval = filecache_check_integrity_hash(cachefile, hash2);
free(cachefile);
if (retval != 0) {
fprintf(stderr, "the target file has the wrong hash\n");
break;
}
free(patches[i]);
}
/* check if the terminating NULL was reached or if processing was aborted
* before that */
if (patches[i] != NULL) {
for (; patches[i] != NULL; i++) free(patches[i]);
free(patches);
return -1;
}
free(patches);
/* verify that the last target revision is equal to the requested remote
* revision */
if (last_target_revision != remote_revision) {
fprintf(stderr, "last_target_revision is not equal to the requested remote revision\n");
return -1;
}
return 0;
}
static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64_t source_revision, uint64_t target_revision, const char * phash, const char * filecache_path)
{
mfpatch *patch;
const char *url;
mfhttp *http;
int retval;
char *patchfile;
unsigned char hash2[SHA256_DIGEST_LENGTH];
/* first retrieve the patch url */
patch = patch_alloc();
retval = mfconn_api_device_get_patch(conn, patch, quickkey,
source_revision,
target_revision);
mfconn_update_secret_key(conn);
if (retval == -1) {
fprintf(stderr, "mfconn_api_device_get_patch failed\n");
patch_free(patch);
return -1;
}
/* verify if the retrieved patch hash is the expected patch hash */
if (strcmp(phash, patch_get_hash(patch)) != 0) {
fprintf(stderr, "the expected patch hash is not equal the hash returned by device/get_patch\n");
patch_free(patch);
return -1;
}
/* then download the patch */
url = patch_get_link(patch);
if (url == NULL || url[0] == '\0') {
fprintf(stderr, "patch_get_link failed\n");
patch_free(patch);
return -1;
}
patchfile =
strdup_printf("%s/%s_patch_%d_%d", filecache_path, quickkey,
source_revision, target_revision);
http = http_create();
retval = http_get_file(http, url, patchfile);
http_destroy(http);
if (retval != 0) {
fprintf(stderr, "download failed\n");
free(patchfile);
patch_free(patch);
return -1;
}
/* verify the integrity of the patch */
hex2binary(patch_get_hash(patch), hash2);
retval = filecache_check_integrity_hash(patchfile, hash2);
if (retval != 0) {
fprintf(stderr, "filecache_check_integrity_hash failed for patch\n");
patch_free(patch);
return -1;
}
patch_free(patch);
return 0;
}
static int filecache_patch_file(const char * filecache_path, const char * quickkey, uint64_t source_revision, uint64_t target_revision)
{
char *patchfile;
char *sourcefile;
char *targetfile;
FILE *sourcefile_fh;
FILE *patchfile_fh;
FILE *targetfile_fh;
int retval;
sourcefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey,
source_revision);
sourcefile_fh = fopen(sourcefile, "r");
if (sourcefile_fh == NULL) {
fprintf(stderr, "cannot open %s\n", sourcefile);
free(sourcefile);
return -1;
}
free(sourcefile);
patchfile =
strdup_printf("%s/%s_patch_%d_%d", filecache_path, quickkey,
source_revision, target_revision);
patchfile_fh = fopen(patchfile, "r");
if (patchfile_fh == NULL) {
fprintf(stderr, "cannot open %s\n", patchfile);
free(patchfile);
fclose(sourcefile_fh);
return -1;
}
free(patchfile);
targetfile =
strdup_printf("%s/%s_%d", filecache_path, quickkey,
target_revision);
targetfile_fh = fopen(targetfile, "w");
if (targetfile_fh == NULL) {
fprintf(stderr, "cannot open %s\n", targetfile);
fclose(sourcefile_fh);
fclose(patchfile_fh);
free(targetfile);
return -1;
}
free(targetfile);
retval = xdelta3_patch(sourcefile_fh, patchfile_fh, targetfile_fh);
if (retval != 0) {
fprintf(stderr, "unable to patch\n");
fclose(sourcefile_fh);
fclose(patchfile_fh);
fclose(targetfile_fh);
return -1;
}
fclose(sourcefile_fh);
fclose(patchfile_fh);
fclose(targetfile_fh);
return 0;
}

27
fuse/filecache.h Normal file
View File

@@ -0,0 +1,27 @@
/*
* 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 __FUSE_FILECACHE_H__
#define __FUSE_FILECACHE_H__
int filecache_open_file(const char * quickkey, uint64_t local_revision,
uint64_t remote_revision, uint64_t fsize, const unsigned char * fhash,
const char * filecache, mfconn * conn);
#endif

View File

@@ -36,13 +36,16 @@
#include <openssl/sha.h> #include <openssl/sha.h>
#include "hashtbl.h" #include "hashtbl.h"
#include "filecache.h"
#include "../mfapi/mfconn.h" #include "../mfapi/mfconn.h"
#include "../mfapi/file.h" #include "../mfapi/file.h"
#include "../mfapi/patch.h"
#include "../mfapi/folder.h" #include "../mfapi/folder.h"
#include "../mfapi/apicalls.h" #include "../mfapi/apicalls.h"
#include "../utils/strings.h" #include "../utils/strings.h"
#include "../utils/http.h" #include "../utils/http.h"
#include "../utils/hash.h" #include "../utils/hash.h"
#include "../utils/xdelta3.h"
/* /*
* we build a hashtable using the first three characters of the file or folder * we build a hashtable using the first three characters of the file or folder
@@ -51,108 +54,6 @@
*/ */
#define NUM_BUCKETS 46656 #define NUM_BUCKETS 46656
/*
* we use this table to convert from a base36 char (ignoring case) to an
* integer or from a hex string to binary (in the latter case letters g-z and
* G-Z remain unused)
* we "waste" these 128 bytes of memory so that we don't need branching
* instructions when decoding
* we only need 128 bytes because the input is a *signed* char
*/
static unsigned char base36_decoding_table[] = {
/* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x30 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
/* 0x40 */ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
/* 0x50 */ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0, 0, 0, 0, 0,
/* 0x60 */ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
/* 0x70 */ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0, 0, 0, 0, 0
};
/*
* table to convert from a byte into the two hexadecimal digits representing
* it
*/
static char base16_encoding_table[][2] = {
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B",
"0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B",
"3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53",
"54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B",
"6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83",
"84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B",
"9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
"A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3",
"B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
"C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB",
"CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
"D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3",
"E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
"F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB",
"FC", "FD", "FE", "FF"
};
/*
* a macro to convert a char* of the key into a hash of its first three
* characters, treating those first three characters as if they represented a
* number in base36
*
* in the future this could be made more dynamic by using the ability of
* strtoll to convert numbers of base36 and then only retrieving the desired
* amount of high-bits for the desired size of the hashtable
*/
#define HASH_OF_KEY(key) base36_decoding_table[(int)(key)[0]]*36*36+\
base36_decoding_table[(int)(key)[1]]*36+\
base36_decoding_table[(int)(key)[2]]
/* decodes a zero terminated string containing hex characters into their
* binary representation. The length of the string must be even as pairs of
* characters are converted to one output byte. The output buffer must be at
* least half the length of the input string.
*/
static void hex2binary(const char *hex, unsigned char *binary)
{
unsigned char val1,
val2;
const char *c1,
*c2;
unsigned char *b;
for (b = binary, c1 = hex, c2 = hex + 1;
*c1 != '\0' && *c2 != '\0'; b++, c1 += 2, c2 += 2) {
val1 = base36_decoding_table[(int)(*c1)];
val2 = base36_decoding_table[(int)(*c2)];
*b = (val1 << 4) | val2;
}
}
static char *binary2hex(unsigned char *binary, size_t length)
{
char *out;
char *p;
size_t i;
out = malloc(length * 2 + 1);
if (out == NULL) {
fprintf(stderr, "cannot allocate memory\n");
return NULL;
}
for (i = 0; i < length; i++) {
p = base16_encoding_table[binary[i]];
out[i * 2] = p[0];
out[i * 2 + 1] = p[1];
}
out[length * 2] = '\0';
return out;
}
struct h_entry { struct h_entry {
/* /*
* keys are either 13 (folders) or 15 (files) long since the structure * keys are either 13 (folders) or 15 (files) long since the structure
@@ -165,12 +66,13 @@ struct h_entry {
/* char parent[20]; */ /* char parent[20]; */
/* local revision */ /* local revision */
uint64_t remote_revision; uint64_t remote_revision;
/* the revision of the local version. For folders, this is the last
* revision for which the folder contents have been retrieved. For files
* this is the last revision for which the file contents have been
* retrieved */
uint64_t local_revision;
/* creation time */ /* creation time */
uint64_t ctime; uint64_t ctime;
/* Whether or not this h_entry needs to be updated before it can be used.
* This value is set to true when directories and files are added as
* children of a directory but their content has not bee retrieved yet */
uint64_t local_revision;
/* the containing folder */ /* the containing folder */
union { union {
/* during runtime this is a pointer to the containing h_entry struct */ /* during runtime this is a pointer to the containing h_entry struct */
@@ -350,7 +252,7 @@ int folder_tree_store(folder_tree * tree, FILE * stream)
if (tmp_parent == &(tree->root)) { if (tmp_parent == &(tree->root)) {
tree->buckets[i][j]->parent_offs = 0; tree->buckets[i][j]->parent_offs = 0;
} else { } else {
bucket_id = HASH_OF_KEY(tmp_parent->key); bucket_id = base36_decode_triplet(tmp_parent->key);
found = false; found = false;
for (k = 0; k < tree->bucket_lens[bucket_id]; k++) { for (k = 0; k < tree->bucket_lens[bucket_id]; k++) {
if (tree->buckets[bucket_id][k] == tmp_parent) { if (tree->buckets[bucket_id][k] == tmp_parent) {
@@ -489,7 +391,7 @@ folder_tree *folder_tree_load(FILE * stream, char *filecache)
parent->children[parent->num_children - 1] = ordered_entries[i]; parent->children[parent->num_children - 1] = ordered_entries[i];
/* put the entry into the hashtable */ /* put the entry into the hashtable */
bucket_id = HASH_OF_KEY(ordered_entries[i]->key); bucket_id = base36_decode_triplet(ordered_entries[i]->key);
tree->bucket_lens[bucket_id]++; tree->bucket_lens[bucket_id]++;
tree->buckets[bucket_id] = tree->buckets[bucket_id] =
(struct h_entry **)realloc(tree->buckets[bucket_id], (struct h_entry **)realloc(tree->buckets[bucket_id],
@@ -564,7 +466,7 @@ static struct h_entry *folder_tree_lookup_key(folder_tree * tree,
return &(tree->root); return &(tree->root);
} }
/* retrieve the right bucket for this key */ /* retrieve the right bucket for this key */
bucket_id = HASH_OF_KEY(key); bucket_id = base36_decode_triplet(key);
for (i = 0; i < tree->bucket_lens[bucket_id]; i++) { for (i = 0; i < tree->bucket_lens[bucket_id]; i++) {
if (strcmp(tree->buckets[bucket_id][i]->key, key) == 0) { if (strcmp(tree->buckets[bucket_id][i]->key, key) == 0) {
@@ -630,7 +532,7 @@ static struct h_entry *folder_tree_lookup_path(folder_tree * tree,
result = curr_dir->children[i]; result = curr_dir->children[i];
// make sure that result is up to date // make sure that result is up to date
if (result->atime == 0 && if (result->atime == 0
&& result->local_revision != result->remote_revision) { && result->local_revision != result->remote_revision) {
folder_tree_rebuild_helper(tree, conn, result); folder_tree_rebuild_helper(tree, conn, result);
} }
@@ -819,17 +721,7 @@ int folder_tree_readdir(folder_tree * tree, mfconn * conn, const char *path,
int folder_tree_open_file(folder_tree * tree, mfconn * conn, const char *path) int folder_tree_open_file(folder_tree * tree, mfconn * conn, const char *path)
{ {
struct h_entry *entry; struct h_entry *entry;
char *cachefile; int retval;
int fd;
struct stat file_info;
uint64_t bytes_read;
const char *url;
mffile *file;
int retval;
mfhttp *http;
FILE *fh;
unsigned char hash[SHA256_DIGEST_LENGTH];
char *hexhash;
entry = folder_tree_lookup_path(tree, conn, path); entry = folder_tree_lookup_path(tree, conn, path);
@@ -837,116 +729,23 @@ int folder_tree_open_file(folder_tree * tree, mfconn * conn, const char *path)
if (entry == NULL || entry->atime == 0) { if (entry == NULL || entry->atime == 0) {
return -ENOENT; return -ENOENT;
} }
fprintf(stderr, "opening %s with local %" PRIu64 " and remote %" PRIu64
"\n", entry->key, entry->local_revision, entry->remote_revision);
/* TODO: check entry->needs_update retval = filecache_open_file(entry->key, entry->local_revision,
* check if local size is equal to remote size entry->remote_revision, entry->fsize,
* check if local version exists and needs updating */ entry->hash, tree->filecache, conn);
/* check if the requested file is already in the cache */
cachefile =
strdup_printf("%s/%s_%d", tree->filecache, entry->key,
entry->remote_revision);
fd = open(cachefile, O_RDWR);
if (fd > 0) {
/* file existed - return handle */
free(cachefile);
return fd;
}
/* download the file */
file = file_alloc();
retval = mfconn_api_file_get_links(conn, file, (char *)entry->key);
mfconn_update_secret_key(conn);
if (retval == -1) { if (retval == -1) {
fprintf(stderr, "mfconn_api_file_get_links failed\n"); fprintf(stderr, "filecache_open_file failed\n");
free(cachefile);
file_free(file);
return -1; return -1;
} }
url = file_get_direct_link(file); /* make sure that the local_revision is equal to the remote revision
* because filecache_open_file took care of doing any updating if it was
* necessary */
entry->local_revision = entry->remote_revision;
if (url == NULL) { return retval;
fprintf(stderr, "file_get_direct_link failed\n");
free(cachefile);
file_free(file);
return -1;
}
http = http_create();
retval = http_get_file(http, url, cachefile);
http_destroy(http);
if (retval != 0) {
fprintf(stderr, "download failed\n");
free(cachefile);
file_free(file);
return -1;
}
memset(&file_info, 0, sizeof(file_info));
retval = stat(cachefile, &file_info);
if (retval != 0) {
fprintf(stderr, "stat failed\n");
free(cachefile);
file_free(file);
return -1;
}
bytes_read = file_info.st_size;
if (bytes_read != entry->fsize) {
fprintf(stderr,
"expected %" PRIu64 " bytes but got %" PRIu64 " bytes\n",
entry->fsize, bytes_read);
free(cachefile);
file_free(file);
return -1;
}
/* size matches - now compare the hash as well */
fh = fopen(cachefile, "r");
if (fh == NULL) {
perror("cannot open file");
free(cachefile);
file_free(file);
return -1;
}
retval = calc_sha256(fh, hash);
if (retval != 0) {
fprintf(stderr, "failed to calculate hash\n");
free(cachefile);
file_free(file);
fclose(fh);
return -1;
}
fclose(fh);
if (memcmp(entry->hash, hash, SHA256_DIGEST_LENGTH) != 0) {
fprintf(stderr, "hashes are not equal\n");
hexhash = binary2hex(entry->hash, SHA256_DIGEST_LENGTH);
fprintf(stderr, "remote: %s\n", hexhash);
free(hexhash);
hexhash = binary2hex(hash, SHA256_DIGEST_LENGTH);
fprintf(stderr, "downloaded: %s\n", hexhash);
free(hexhash);
free(cachefile);
file_free(file);
return -1;
}
file_free(file);
fd = open(cachefile, O_RDWR);
free(cachefile);
/* return the file handle */
return fd;
} }
static bool folder_tree_is_root(struct h_entry *entry) static bool folder_tree_is_root(struct h_entry *entry)
@@ -993,7 +792,7 @@ static struct h_entry *folder_tree_allocate_entry(folder_tree * tree,
"key is NULL but this is fine, we just create it now\n"); "key is NULL but this is fine, we just create it now\n");
/* entry was not found, so append it to the end of the bucket */ /* entry was not found, so append it to the end of the bucket */
entry = (struct h_entry *)calloc(1, sizeof(struct h_entry)); entry = (struct h_entry *)calloc(1, sizeof(struct h_entry));
bucket_id = HASH_OF_KEY(key); bucket_id = base36_decode_triplet(key);
tree->bucket_lens[bucket_id]++; tree->bucket_lens[bucket_id]++;
tree->buckets[bucket_id] = tree->buckets[bucket_id] =
realloc(tree->buckets[bucket_id], realloc(tree->buckets[bucket_id],
@@ -1328,7 +1127,7 @@ static void folder_tree_remove(folder_tree * tree, const char *key)
return; return;
} }
bucket_id = HASH_OF_KEY(key); bucket_id = base36_decode_triplet(key);
/* check if the key exists */ /* check if the key exists */
found = 0; found = 0;

View File

@@ -24,6 +24,7 @@
#include "file.h" #include "file.h"
#include "folder.h" #include "folder.h"
#include "patch.h"
#include "mfconn.h" #include "mfconn.h"
#define MFAPI_MAX_LEN_KEY 15 #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 int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision, struct mfconn_device_change
**changes); **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 #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 <jansson.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "../../utils/http.h" #include "../../utils/http.h"
#include "../../utils/json.h" #include "../../utils/json.h"
@@ -74,6 +76,8 @@ static int _decode_file_get_info(mfhttp * conn, void *data)
json_t *quickkey; json_t *quickkey;
int retval = 0; int retval = 0;
mffile *file; mffile *file;
char *ret;
struct tm tm;
if (data == NULL) if (data == NULL)
return -1; return -1;
@@ -101,10 +105,32 @@ static int _decode_file_get_info(mfhttp * conn, void *data)
if (obj != NULL) { if (obj != NULL) {
file_set_parent(file, json_string_value(obj)); file_set_parent(file, json_string_value(obj));
} }
// infer that the parent folder must be root // infer that the parent folder must be root
if (obj == NULL && quickkey != NULL) if (obj == NULL && quickkey != NULL)
file_set_parent(file, 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) if (quickkey == NULL)
retval = -1; retval = -1;

View File

@@ -112,7 +112,7 @@ static int _decode_folder_get_info(mfhttp * conn, void *data)
revision = json_object_get(node, "revision"); revision = json_object_get(node, "revision");
if (revision != NULL) { 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"); 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

View File

@@ -79,4 +79,7 @@ int mfshell_cmd_status(mfshell * mfshell, int argc,
int mfshell_cmd_changes(mfshell * mfshell, int argc, int mfshell_cmd_changes(mfshell * mfshell, int argc,
char *const argv[]); char *const argv[]);
int mfshell_cmd_updates(mfshell * mfshell, int argc,
char *const argv[]);
#endif #endif

100
mfshell/commands/updates.c Normal file
View File

@@ -0,0 +1,100 @@
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <inttypes.h>
#include "../../mfapi/apicalls.h"
#include "../../mfapi/mfconn.h"
#include "../../mfapi/patch.h"
#include "../mfshell.h"
#include "../commands.h" // IWYU pragma: keep
int mfshell_cmd_updates(mfshell * mfshell, int argc, char *const argv[])
{
mfpatch **patches;
uint64_t revision;
uint64_t target_revision;
const char *quickkey;
size_t len;
int i;
int retval;
if (mfshell == NULL) {
fprintf(stderr, "Zero shell\n");
return -1;
}
if (mfshell->conn == NULL) {
fprintf(stderr, "conn is NULL\n");
return -1;
}
switch (argc) {
case 3:
target_revision = 0;
break;
case 4:
target_revision = atoll(argv[3]);
break;
default:
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
quickkey = argv[1];
if (quickkey == NULL)
return -1;
len = strlen(quickkey);
if (len != 15)
return -1;
revision = atoll(argv[2]);
patches = NULL;
retval = mfconn_api_device_get_updates(mfshell->conn, quickkey, revision,
target_revision, &patches);
mfconn_update_secret_key(mfshell->conn);
if (retval != 0) {
fprintf(stderr, "api call unsuccessful\n");
return -1;
}
fprintf(stdout,
"source_revision target_revision source_hash target_hash patch_hash\n");
for (i = 0; patches[i] != NULL; i++) {
fprintf(stdout, "%" PRIu64 " %" PRIu64 " %s %s %s\n",
patch_get_source_revision(patches[i]),
patch_get_target_revision(patches[i]),
patch_get_source_hash(patches[i]),
patch_get_target_hash(patches[i]), patch_get_hash(patches[i]));
free(patches[i]);
}
free(patches);
return 0;
}

View File

@@ -56,6 +56,8 @@ struct mfcmd commands[] = {
{"status", "", "device status", mfshell_cmd_status}, {"status", "", "device status", mfshell_cmd_status},
{"changes", "<revision>", "device changes (default: 0)", {"changes", "<revision>", "device changes (default: 0)",
mfshell_cmd_changes}, mfshell_cmd_changes},
{"updates", "[quickkey] [revision]", "list available updates for a file",
mfshell_cmd_updates},
{NULL, NULL, NULL, NULL} {NULL, NULL, NULL, NULL}
}; };

View File

@@ -23,6 +23,54 @@
#define bufsize 32768 #define bufsize 32768
/*
* we use this table to convert from a base36 char (ignoring case) to an
* integer or from a hex string to binary (in the latter case letters g-z and
* G-Z remain unused)
* we "waste" these 128 bytes of memory so that we don't need branching
* instructions when decoding
* we only need 128 bytes because the input is a *signed* char
*/
static unsigned char base36_decoding_table[] = {
/* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x30 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
/* 0x40 */ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
/* 0x50 */ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0, 0, 0, 0, 0,
/* 0x60 */ 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
/* 0x70 */ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0, 0, 0, 0, 0
};
/*
* table to convert from a byte into the two hexadecimal digits representing
* it
*/
static char base16_encoding_table[][2] = {
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B",
"0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B",
"3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53",
"54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B",
"6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83",
"84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B",
"9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
"A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3",
"B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
"C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB",
"CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
"D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3",
"E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
"F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB",
"FC", "FD", "FE", "FF"
};
int calc_md5(FILE * file, unsigned char *hash) int calc_md5(FILE * file, unsigned char *hash)
{ {
int bytesRead; int bytesRead;
@@ -60,3 +108,60 @@ int calc_sha256(FILE * file, unsigned char *hash)
free(buffer); free(buffer);
return 0; return 0;
} }
/* decodes a zero terminated string containing hex characters into their
* binary representation. The length of the string must be even as pairs of
* characters are converted to one output byte. The output buffer must be at
* least half the length of the input string.
*/
void hex2binary(const char *hex, unsigned char *binary)
{
unsigned char val1,
val2;
const char *c1,
*c2;
unsigned char *b;
for (b = binary, c1 = hex, c2 = hex + 1;
*c1 != '\0' && *c2 != '\0'; b++, c1 += 2, c2 += 2) {
val1 = base36_decoding_table[(int)(*c1)];
val2 = base36_decoding_table[(int)(*c2)];
*b = (val1 << 4) | val2;
}
}
char *binary2hex(const unsigned char *binary, size_t length)
{
char *out;
char *p;
size_t i;
out = malloc(length * 2 + 1);
if (out == NULL) {
fprintf(stderr, "cannot allocate memory\n");
return NULL;
}
for (i = 0; i < length; i++) {
p = base16_encoding_table[binary[i]];
out[i * 2] = p[0];
out[i * 2 + 1] = p[1];
}
out[length * 2] = '\0';
return out;
}
/*
* a function to convert a char* of the key into a hash of its first three
* characters, treating those first three characters as if they represented a
* number in base36
*
* in the future this could be made more dynamic by using the ability of
* strtoll to convert numbers of base36 and then only retrieving the desired
* amount of high-bits for the desired size of the hashtable
*/
int base36_decode_triplet(const char *key)
{
return base36_decoding_table[(int)(key)[0]]*36*36
+ base36_decoding_table[(int)(key)[1]]*36
+ base36_decoding_table[(int)(key)[2]];
}

View File

@@ -21,5 +21,8 @@
int calc_md5(FILE * file, unsigned char *hash); int calc_md5(FILE * file, unsigned char *hash);
int calc_sha256(FILE * file, unsigned char *hash); int calc_sha256(FILE * file, unsigned char *hash);
int base36_decode_triplet(const char *key);
void hex2binary(const char *hex, unsigned char *binary);
char *binary2hex(const unsigned char *binary, size_t length);
#endif #endif