fix includes and indentation

This commit is contained in:
josch
2014-12-01 22:03:09 +01:00
parent e730107b8b
commit 2f2ce91597
7 changed files with 104 additions and 59 deletions

View File

@@ -17,32 +17,53 @@
*/ */
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <bits/fcntl-linux.h>
#include <stdint.h>
#include <stdlib.h>
#include "../utils/hash.h" #include "../utils/hash.h"
#include "../utils/xdelta3.h" #include "../utils/xdelta3.h"
#include "../mfapi/file.h" #include "../mfapi/file.h"
#include "../mfapi/apicalls.h" #include "../mfapi/apicalls.h"
#include "../mfapi/patch.h" #include "../mfapi/patch.h"
#include "../mfapi/mfconn.h"
#include "../utils/http.h" #include "../utils/http.h"
#include "../utils/strings.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(const char *cachefile,
static int filecache_check_integrity_size(const char * cachefile, uint64_t fsize); uint64_t fsize,
static int filecache_check_integrity_hash(const char * cachefile, const unsigned char * fhash); 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_check_integrity_size(const char *cachefile,
static int filecache_download_file(const char * filecache_path, const char * quickkey, uint64_t remote_revision, mfconn * conn); uint64_t fsize);
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_check_integrity_hash(const char *cachefile,
static int filecache_patch_file(const char * filecache_path, const char * quickkey, uint64_t source_revision, uint64_t target_revision); 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, int filecache_open_file(const char *quickkey, uint64_t local_revision,
uint64_t remote_revision, uint64_t fsize, const unsigned char * fhash, uint64_t remote_revision, uint64_t fsize,
const unsigned char *fhash,
const char *filecache_path, mfconn * conn) const char *filecache_path, mfconn * conn)
{ {
char *cachefile; char *cachefile;
@@ -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 */ /* check if the requested file is already in the cache */
cachefile = cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey, strdup_printf("%s/%s_%d", filecache_path, quickkey, remote_revision);
remote_revision);
fd = open(cachefile, O_RDWR); fd = open(cachefile, O_RDWR);
if (fd > 0) { if (fd > 0) {
/* file existed - return handle */ /* file existed - return handle */
@@ -67,15 +87,15 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision,
* Otherwise, download the file anew */ * Otherwise, download the file anew */
cachefile = cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey, strdup_printf("%s/%s_%d", filecache_path, quickkey, local_revision);
local_revision);
fd = open(cachefile, O_RDWR); fd = open(cachefile, O_RDWR);
free(cachefile); free(cachefile);
if (fd > 0) { if (fd > 0) {
close(fd); close(fd);
/* file exists, so we have to update it with one or more patches from /* file exists, so we have to update it with one or more patches from
* the remote */ * 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) { if (retval != 0) {
fprintf(stderr, "update_file failed\n"); fprintf(stderr, "update_file failed\n");
return -1; return -1;
@@ -83,7 +103,8 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision,
} else { } else {
/* download the file */ /* 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) { if (retval != 0) {
fprintf(stderr, "filecache_download_file failed\n"); fprintf(stderr, "filecache_download_file failed\n");
return -1; 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 /* check whether the patched or newly downloaded file matches the hash we
* have stored */ * have stored */
cachefile = cachefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey, strdup_printf("%s/%s_%d", filecache_path, quickkey, remote_revision);
remote_revision);
retval = filecache_check_integrity(cachefile, fsize, fhash); retval = filecache_check_integrity(cachefile, fsize, fhash);
if (retval != 0) { if (retval != 0) {
fprintf(stderr, "checking integrity failed\n"); fprintf(stderr, "checking integrity failed\n");
@@ -110,7 +130,9 @@ int filecache_open_file(const char * quickkey, uint64_t local_revision,
return fd; 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; int retval;
retval = filecache_check_integrity_size(path, fsize); retval = filecache_check_integrity_size(path, fsize);
@@ -155,7 +177,8 @@ static int filecache_check_integrity_size(const char * path, uint64_t fsize)
return 0; 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; int retval;
FILE *fh; FILE *fh;
@@ -191,7 +214,9 @@ static int filecache_check_integrity_hash(const char * path, const unsigned char
return 0; 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; const char *url;
mffile *file; mffile *file;
@@ -239,7 +264,10 @@ static int filecache_download_file(const char * filecache_path, const char * qui
return 0; 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]; unsigned char hash2[SHA256_DIGEST_LENGTH];
int retval; int retval;
@@ -258,12 +286,12 @@ static int filecache_update_file(const char * filecache_path, mfconn * conn, con
fprintf(stderr, "device/get_updates api call unsuccessful\n"); fprintf(stderr, "device/get_updates api call unsuccessful\n");
return -1; return -1;
} }
// if no patches are returned, then the full file has to be downloaded // if no patches are returned, then the full file has to be downloaded
if (patches[0] == NULL) { if (patches[0] == NULL) {
free(patches); 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) { if (retval != 0) {
fprintf(stderr, "filecache_download_file failed\n"); fprintf(stderr, "filecache_download_file failed\n");
return -1; 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 /* verify that the source revision is equal to the last target
* revision */ * revision */
if (patch_get_source_revision(patches[i]) != 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; break;
} }
last_target_revision = patch_get_target_revision(patches[i]); 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) { if (retval != 0) {
fprintf(stderr, "filecache_download_patch failed\n"); fprintf(stderr, "filecache_download_patch failed\n");
break; break;
@@ -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 */ /* 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) { if (retval != 0) {
fprintf(stderr, "filecache_patch_file failed\n"); fprintf(stderr, "filecache_patch_file failed\n");
break; break;
@@ -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 /* check if the terminating NULL was reached or if processing was aborted
* before that */ * before that */
if (patches[i] != NULL) { if (patches[i] != NULL) {
for (; patches[i] != NULL; i++) free(patches[i]); for (; patches[i] != NULL; i++)
free(patches[i]);
free(patches); free(patches);
return -1; 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 /* verify that the last target revision is equal to the requested remote
* revision */ * revision */
if (last_target_revision != 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 -1;
} }
return 0; 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; mfpatch *patch;
const char *url; const char *url;
@@ -355,8 +397,7 @@ static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64
/* first retrieve the patch url */ /* first retrieve the patch url */
patch = patch_alloc(); patch = patch_alloc();
retval = mfconn_api_device_get_patch(conn, patch, quickkey, retval = mfconn_api_device_get_patch(conn, patch, quickkey,
source_revision, source_revision, target_revision);
target_revision);
mfconn_update_secret_key(conn); mfconn_update_secret_key(conn);
if (retval == -1) { 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 */ /* verify if the retrieved patch hash is the expected patch hash */
if (strcmp(phash, patch_get_hash(patch)) != 0) { 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); patch_free(patch);
return -1; return -1;
} }
@@ -411,7 +453,10 @@ static int filecache_download_patch(mfconn * conn, const char * quickkey, uint64
return 0; 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 *patchfile;
char *sourcefile; char *sourcefile;
@@ -422,8 +467,7 @@ static int filecache_patch_file(const char * filecache_path, const char * quickk
int retval; int retval;
sourcefile = sourcefile =
strdup_printf("%s/%s_%d", filecache_path, quickkey, strdup_printf("%s/%s_%d", filecache_path, quickkey, source_revision);
source_revision);
sourcefile_fh = fopen(sourcefile, "r"); sourcefile_fh = fopen(sourcefile, "r");
if (sourcefile_fh == NULL) { if (sourcefile_fh == NULL) {
fprintf(stderr, "cannot open %s\n", sourcefile); fprintf(stderr, "cannot open %s\n", sourcefile);
@@ -447,8 +491,7 @@ static int filecache_patch_file(const char * filecache_path, const char * quickk
free(patchfile); free(patchfile);
targetfile = targetfile =
strdup_printf("%s/%s_%d", filecache_path, quickkey, strdup_printf("%s/%s_%d", filecache_path, quickkey, target_revision);
target_revision);
targetfile_fh = fopen(targetfile, "w"); targetfile_fh = fopen(targetfile, "w");
if (targetfile_fh == NULL) { if (targetfile_fh == NULL) {
fprintf(stderr, "cannot open %s\n", targetfile); fprintf(stderr, "cannot open %s\n", targetfile);

View File

@@ -19,9 +19,10 @@
#ifndef __FUSE_FILECACHE_H__ #ifndef __FUSE_FILECACHE_H__
#define __FUSE_FILECACHE_H__ #define __FUSE_FILECACHE_H__
int filecache_open_file(const char * quickkey, uint64_t local_revision, int filecache_open_file(const char *quickkey,
uint64_t remote_revision, uint64_t fsize, const unsigned char * fhash, uint64_t local_revision,
uint64_t remote_revision, uint64_t fsize,
const unsigned char *fhash,
const char *filecache, mfconn * conn); const char *filecache, mfconn * conn);
#endif #endif

View File

@@ -35,6 +35,7 @@
#include <wordexp.h> #include <wordexp.h>
#include <stdint.h> #include <stdint.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include "../mfapi/mfconn.h" #include "../mfapi/mfconn.h"
#include "../mfapi/apicalls.h" #include "../mfapi/apicalls.h"

View File

@@ -105,7 +105,6 @@ 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);

View File

@@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <openssl/md5.h> #include <openssl/md5.h>
#include <stddef.h>
#define bufsize 32768 #define bufsize 32768