diff --git a/fuse/filecache.c b/fuse/filecache.c index 0fb8b85..c0eeb9f 100644 --- a/fuse/filecache.c +++ b/fuse/filecache.c @@ -17,33 +17,54 @@ */ #include -#include #include #include #include #include #include #include +#include +#include +#include #include "../utils/hash.h" #include "../utils/xdelta3.h" #include "../mfapi/file.h" #include "../mfapi/apicalls.h" #include "../mfapi/patch.h" +#include "../mfapi/mfconn.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); +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) +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; @@ -51,8 +72,7 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision, /* check if the requested file is already in the cache */ cachefile = - strdup_printf("%s/%s_%d", filecache_path, quickkey, - remote_revision); + strdup_printf("%s/%s_%d", filecache_path, quickkey, remote_revision); fd = open(cachefile, O_RDWR); if (fd > 0) { /* file existed - return handle */ @@ -67,15 +87,15 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision, * Otherwise, download the file anew */ cachefile = - strdup_printf("%s/%s_%d", filecache_path, quickkey, - local_revision); + 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); + retval = filecache_update_file(filecache_path, conn, quickkey, + local_revision, remote_revision); if (retval != 0) { fprintf(stderr, "update_file failed\n"); return -1; @@ -83,7 +103,8 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision, } else { /* download the file */ - retval = filecache_download_file(filecache_path, quickkey, remote_revision, conn); + retval = filecache_download_file(filecache_path, quickkey, + remote_revision, conn); if (retval != 0) { fprintf(stderr, "filecache_download_file failed\n"); return -1; @@ -93,8 +114,7 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision, /* 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); + 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"); @@ -110,7 +130,9 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision, return fd; } -static int filecache_check_integrity(const char * path, uint64_t fsize, const unsigned char * fhash) { +static int filecache_check_integrity(const char *path, uint64_t fsize, + const unsigned char *fhash) +{ int retval; retval = filecache_check_integrity_size(path, fsize); @@ -128,7 +150,7 @@ static int filecache_check_integrity(const char * path, uint64_t fsize, const un return 0; } -static int filecache_check_integrity_size(const char * path, uint64_t fsize) +static int filecache_check_integrity_size(const char *path, uint64_t fsize) { struct stat file_info; int retval; @@ -155,7 +177,8 @@ static int filecache_check_integrity_size(const char * path, uint64_t fsize) return 0; } -static int filecache_check_integrity_hash(const char * path, const unsigned char * fhash) +static int filecache_check_integrity_hash(const char *path, + const unsigned char *fhash) { int retval; FILE *fh; @@ -191,7 +214,9 @@ static int filecache_check_integrity_hash(const char * path, const unsigned char return 0; } -static int filecache_download_file(const char * filecache_path, const char * quickkey, uint64_t remote_revision, mfconn * conn) +static int filecache_download_file(const char *filecache_path, + const char *quickkey, + uint64_t remote_revision, mfconn * conn) { const char *url; mffile *file; @@ -239,7 +264,10 @@ static int filecache_download_file(const char * filecache_path, const char * qui return 0; } -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_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; @@ -247,23 +275,23 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con uint64_t last_target_revision; char *cachefile; - mfpatch **patches = NULL; + mfpatch **patches = NULL; retval = mfconn_api_device_get_updates(conn, quickkey, - local_revision, remote_revision, - &patches); + 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); + retval = filecache_download_file(filecache_path, quickkey, + remote_revision, conn); if (retval != 0) { fprintf(stderr, "filecache_download_file failed\n"); return -1; @@ -278,12 +306,18 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con /* 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"); + 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); + 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; @@ -292,7 +326,7 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con /* 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])); + patch_get_source_revision(patches[i])); hex2binary(patch_get_source_hash(patches[i]), hash2); retval = filecache_check_integrity_hash(cachefile, hash2); free(cachefile); @@ -302,7 +336,9 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con } /* 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])); + 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; @@ -311,7 +347,7 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con /* verify that the patched file has the right hash */ cachefile = strdup_printf("%s/%s_%d", filecache_path, quickkey, - patch_get_target_revision(patches[i])); + patch_get_target_revision(patches[i])); hex2binary(patch_get_target_hash(patches[i]), hash2); retval = filecache_check_integrity_hash(cachefile, hash2); free(cachefile); @@ -326,7 +362,8 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con /* 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]); + for (; patches[i] != NULL; i++) + free(patches[i]); free(patches); return -1; } @@ -336,14 +373,19 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con /* 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"); + 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) +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; @@ -355,8 +397,7 @@ static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64 /* first retrieve the patch url */ patch = patch_alloc(); retval = mfconn_api_device_get_patch(conn, patch, quickkey, - source_revision, - target_revision); + source_revision, target_revision); mfconn_update_secret_key(conn); if (retval == -1) { @@ -367,7 +408,8 @@ static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64 /* 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"); + fprintf(stderr, "the expected patch hash is not equal the hash " + "returned by device/get_patch\n"); patch_free(patch); return -1; } @@ -383,7 +425,7 @@ static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64 patchfile = strdup_printf("%s/%s_patch_%d_%d", filecache_path, quickkey, - source_revision, target_revision); + source_revision, target_revision); http = http_create(); retval = http_get_file(http, url, patchfile); @@ -411,7 +453,10 @@ static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64 return 0; } -static int filecache_patch_file(const char * filecache_path, const char * quickkey, uint64_t source_revision, uint64_t target_revision) +static int filecache_patch_file(const char *filecache_path, + const char *quickkey, + uint64_t source_revision, + uint64_t target_revision) { char *patchfile; char *sourcefile; @@ -422,8 +467,7 @@ static int filecache_patch_file(const char * filecache_path, const char * quickk int retval; sourcefile = - strdup_printf("%s/%s_%d", filecache_path, quickkey, - source_revision); + 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); @@ -435,7 +479,7 @@ static int filecache_patch_file(const char * filecache_path, const char * quickk patchfile = strdup_printf("%s/%s_patch_%d_%d", filecache_path, quickkey, - source_revision, target_revision); + source_revision, target_revision); patchfile_fh = fopen(patchfile, "r"); if (patchfile_fh == NULL) { fprintf(stderr, "cannot open %s\n", patchfile); @@ -447,8 +491,7 @@ static int filecache_patch_file(const char * filecache_path, const char * quickk free(patchfile); targetfile = - strdup_printf("%s/%s_%d", filecache_path, quickkey, - target_revision); + 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); diff --git a/fuse/filecache.h b/fuse/filecache.h index 4906b61..fd49d35 100644 --- a/fuse/filecache.h +++ b/fuse/filecache.h @@ -19,9 +19,10 @@ #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); - +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 diff --git a/fuse/hashtbl.c b/fuse/hashtbl.c index 66b45c8..69be513 100644 --- a/fuse/hashtbl.c +++ b/fuse/hashtbl.c @@ -721,7 +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) { struct h_entry *entry; - int retval; + int retval; entry = folder_tree_lookup_path(tree, conn, path); diff --git a/fuse/main.c b/fuse/main.c index 065f036..355956d 100644 --- a/fuse/main.c +++ b/fuse/main.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "../mfapi/mfconn.h" #include "../mfapi/apicalls.h" diff --git a/mfapi/apicalls/file_get_info.c b/mfapi/apicalls/file_get_info.c index 0e1cf70..a48f0aa 100644 --- a/mfapi/apicalls/file_get_info.c +++ b/mfapi/apicalls/file_get_info.c @@ -105,7 +105,6 @@ 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); diff --git a/mfshell/commands/updates.c b/mfshell/commands/updates.c index 87f7a00..0f50b5d 100644 --- a/mfshell/commands/updates.c +++ b/mfshell/commands/updates.c @@ -57,8 +57,8 @@ int mfshell_cmd_updates(mfshell * mfshell, int argc, char *const argv[]) target_revision = atoll(argv[3]); break; default: - fprintf(stderr, "Invalid number of arguments\n"); - return -1; + fprintf(stderr, "Invalid number of arguments\n"); + return -1; } quickkey = argv[1]; diff --git a/utils/hash.c b/utils/hash.c index 8a305ec..4de4aa2 100644 --- a/utils/hash.c +++ b/utils/hash.c @@ -20,6 +20,7 @@ #include #include #include +#include #define bufsize 32768 @@ -130,7 +131,7 @@ void hex2binary(const char *hex, unsigned char *binary) } } -char *binary2hex(const unsigned char *binary, size_t length) +char *binary2hex(const unsigned char *binary, size_t length) { char *out; char *p; @@ -161,7 +162,7 @@ char *binary2hex(const unsigned char *binary, size_t length) */ 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]]; + return base36_decoding_table[(int)(key)[0]] * 36 * 36 + + base36_decoding_table[(int)(key)[1]] * 36 + + base36_decoding_table[(int)(key)[2]]; }