mirror of
https://github.com/xorgy/mediafire-fuse
synced 2026-01-13 13:14:29 -08:00
fix includes and indentation
This commit is contained in:
123
fuse/filecache.c
123
fuse/filecache.c
@@ -17,33 +17,54 @@
|
||||
*/
|
||||
|
||||
#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 <bits/fcntl-linux.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
@@ -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");
|
||||
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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include <wordexp.h>
|
||||
#include <stdint.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../mfapi/mfconn.h"
|
||||
#include "../mfapi/apicalls.h"
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <stdio.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define bufsize 32768
|
||||
|
||||
@@ -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
|
||||
return base36_decoding_table[(int)(key)[0]] * 36 * 36
|
||||
+ base36_decoding_table[(int)(key)[1]] * 36
|
||||
+ base36_decoding_table[(int)(key)[2]];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user