allow to change local files

- fuse/filecache:
     * add filecache_upload_patch
     * allow opening files in modes other than RDONLY
 - fuse/hashtbl:
     * add folder_tree_upload_patch
 - fuse/operations:
     * allow opening files in modes other than RDONLY
     * add members to private context which allow tracking of
       not-yet-uploaded files and files opened for writing and
       files opened in read-only mode
 - mfapi/apicalls/upload_patch:
     * supply x-filename and x-filesize headers
 - mfapi/apicalls/upload_simple:
     * do not supply the x-filehash header as it is not used by the
       server
 - utils/hash:
     * hex characters must be lower case for the server
 - utils/strings:
     * clean up unused functions strdup_join, strdup_substr,
       string_chomp
 - utils/stringv:
     * complete rewrite with different string vector implementation
This commit is contained in:
josch
2014-12-08 14:12:17 +01:00
parent 5bd9e418c4
commit 171fd815f2
16 changed files with 521 additions and 439 deletions

View File

@@ -107,8 +107,11 @@ int mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
char **upload_key);
int mfconn_api_upload_patch(mfconn * conn, const char *quickkey,
FILE * source_fh, FILE * target_fh,
FILE * patch_fh, char **upload_key);
const char *source_hash,
const char *target_hash,
uint64_t target_size,
const char *patch_path,
char **upload_key);
int mfconn_api_upload_poll_upload(mfconn * conn,
const char *upload_key,

View File

@@ -26,10 +26,11 @@
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <openssl/sha.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include "../../utils/http.h"
#include "../../utils/hash.h"
#include "../../utils/strings.h"
#include "../mfconn.h"
#include "../apicalls.h" // IWYU pragma: keep
@@ -37,52 +38,47 @@ static int _decode_upload_patch(mfhttp * conn, void *data);
int
mfconn_api_upload_patch(mfconn * conn, const char *quickkey,
FILE * source_fh, FILE * target_fh,
FILE * patch_fh, char **upload_key)
const char *source_hash, const char *target_hash,
uint64_t target_size,
const char *patch_path, char **upload_key)
{
const char *api_call;
int retval;
mfhttp *http;
uint64_t target_size;
int i;
unsigned char hash[SHA256_DIGEST_LENGTH];
char *source_hash;
char *target_hash;
FILE *patch_fh;
struct curl_slist *custom_headers = NULL;
char *tmpheader;
uint64_t patch_size;
struct stat file_info;
if (conn == NULL)
return -1;
if (source_fh == NULL)
return -1;
if (target_fh == NULL)
return -1;
rewind(source_fh);
retval = calc_sha256(source_fh, hash, NULL);
memset(&file_info, 0, sizeof(file_info));
retval = stat(patch_path, &file_info);
if (retval != 0) {
fprintf(stderr, "failed to calculate hash\n");
fprintf(stderr, "stat failed\n");
return -1;
}
source_hash = binary2hex(hash, SHA256_DIGEST_LENGTH);
rewind(target_fh);
retval = calc_sha256(target_fh, hash, &target_size);
if (retval != 0) {
fprintf(stderr, "failed to calculate hash\n");
return -1;
}
target_hash = binary2hex(hash, SHA256_DIGEST_LENGTH);
patch_size = file_info.st_size;
for (i = 0; i < mfconn_get_max_num_retries(conn); i++) {
if (*upload_key != NULL) {
free(*upload_key);
*upload_key = NULL;
}
if (custom_headers != NULL) {
curl_slist_free_all(custom_headers);
custom_headers = NULL;
}
patch_fh = fopen(patch_path, "r");
if (patch_fh == NULL) {
fprintf(stderr, "cannot open %s\n", patch_path);
return -1;
}
api_call = mfconn_create_signed_get(conn, 0,
"upload/patch.php",
@@ -90,19 +86,23 @@ mfconn_api_upload_patch(mfconn * conn, const char *quickkey,
"&source_hash=%s"
"&target_hash=%s"
"&target_size=%" PRIu64
"&quick_key=%s", source_hash,
"&quickkey=%s", source_hash,
target_hash, target_size,
quickkey);
// make sure that we are at the beginning of the file
rewind(patch_fh);
custom_headers = curl_slist_append(custom_headers,
"x-filename: dummy.patch");
tmpheader = strdup_printf("x-filesize: %" PRIu64, patch_size);
custom_headers = curl_slist_append(custom_headers, tmpheader);
free(tmpheader);
http = http_create();
retval = http_post_file(http, api_call, patch_fh, NULL, target_size,
_decode_upload_patch, upload_key);
retval = http_post_file(http, api_call, patch_fh, &custom_headers,
patch_size, _decode_upload_patch, upload_key);
http_destroy(http);
mfconn_update_secret_key(conn);
fclose(patch_fh);
free((void *)api_call);
if (retval != 127 && retval != 28)
@@ -122,9 +122,6 @@ mfconn_api_upload_patch(mfconn * conn, const char *quickkey,
}
}
free(source_hash);
free(target_hash);
return retval;
}

View File

@@ -26,11 +26,9 @@
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <openssl/sha.h>
#include <curl/curl.h>
#include "../../utils/http.h"
#include "../../utils/hash.h"
#include "../../utils/strings.h"
#include "../mfconn.h"
#include "../apicalls.h" // IWYU pragma: keep
@@ -44,8 +42,7 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
const char *api_call;
int retval;
mfhttp *http;
unsigned char hash[SHA256_DIGEST_LENGTH];
char *file_hash;
long l_file_size;
uint64_t file_size;
int i;
struct curl_slist *custom_headers = NULL;
@@ -57,18 +54,21 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
if (fh == NULL)
return -1;
// make sure that we are at the beginning of the file
rewind(fh);
// calculate hash
retval = calc_sha256(fh, hash, &file_size);
retval = fseek(fh, 0, SEEK_END);
if (retval != 0) {
fprintf(stderr, "failed to calculate hash\n");
fprintf(stderr, "fseek failed\n");
return -1;
}
file_hash = binary2hex(hash, SHA256_DIGEST_LENGTH);
l_file_size = ftell(fh);
if (l_file_size == -1) {
fprintf(stderr, "ftell failed\n");
return -1;
}
file_size = l_file_size;
// make sure that we are at the beginning of the file
rewind(fh);
for (i = 0; i < mfconn_get_max_num_retries(conn); i++) {
if (*upload_key != NULL) {
@@ -102,9 +102,6 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
tmpheader = strdup_printf("x-filesize: %" PRIu64, file_size);
custom_headers = curl_slist_append(custom_headers, tmpheader);
free(tmpheader);
tmpheader = strdup_printf("x-filehash: %s", file_hash);
custom_headers = curl_slist_append(custom_headers, tmpheader);
free(tmpheader);
http = http_create();
retval = http_post_file(http, api_call, fh, &custom_headers, file_size,
@@ -135,8 +132,6 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
}
}
free(file_hash);
return retval;
}