Allow to create new files (but changing them is not allowed yet)

- since mediafire cannot deal with empty files, we put new files into a
   temporary location and upload them once they get closed
 - add create and write functions to fuse
 - pass a file handle to mfconn_api_upload_simple instead of a path
 - allow calc_sha256 to also compute the file size
 - error out when the key returned by upload/simple is empty
 - make valgrind.supp more lenient
This commit is contained in:
josch
2014-12-04 16:07:12 +01:00
parent f1bf926145
commit 71f6396a9a
13 changed files with 258 additions and 44 deletions

View File

@@ -96,21 +96,32 @@ int calc_md5(FILE * file, unsigned char *hash)
return 0;
}
int calc_sha256(FILE * file, unsigned char *hash)
/*
* calculate the SHA256 sum and optionally (if size != NULL) count the file
* size
*/
int calc_sha256(FILE * file, unsigned char *hash, uint64_t * size)
{
int bytesRead;
char *buffer;
SHA256_CTX sha256;
uint64_t bytesRead_sum;
SHA256_Init(&sha256);
buffer = malloc(bufsize);
if (buffer == NULL) {
return -1;
}
bytesRead_sum = 0;
while ((bytesRead = fread(buffer, 1, bufsize, file))) {
SHA256_Update(&sha256, buffer, bytesRead);
bytesRead_sum += bytesRead;
}
SHA256_Final(hash, &sha256);
if (size != NULL) {
*size = bytesRead_sum;
}
free(buffer);
return 0;
}
@@ -232,7 +243,7 @@ int file_check_integrity_hash(const char *path, const unsigned char *fhash)
return -1;
}
retval = calc_sha256(fh, hash);
retval = calc_sha256(fh, hash, NULL);
if (retval != 0) {
fprintf(stderr, "failed to calculate hash\n");
fclose(fh);

View File

@@ -20,7 +20,8 @@
#define _MFSHELL_HASH_H_
int calc_md5(FILE * file, unsigned char *hash);
int calc_sha256(FILE * file, unsigned char *hash);
int calc_sha256(FILE * file, unsigned char *hash,
uint64_t * file_size);
int base36_decode_triplet(const char *key);
void hex2binary(const char *hex, unsigned char *binary);
char *binary2hex(const unsigned char *binary, size_t length);

View File

@@ -277,7 +277,7 @@ http_read_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
}
int
http_post_file(mfhttp * conn, const char *url, const char *path,
http_post_file(mfhttp * conn, const char *url, FILE * fh,
const char *filename, uint64_t filesize, const char *fhash,
int (*data_handler) (mfhttp * conn, void *data), void *data)
{
@@ -318,10 +318,9 @@ http_post_file(mfhttp * conn, const char *url, const char *path,
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDSIZE, filesize);
conn->stream = fopen(path, "r");
conn->stream = fh;
fprintf(stderr, "POST: %s\n", url);
retval = curl_easy_perform(conn->curl_handle);
fclose(conn->stream);
curl_slist_free_all(custom_headers);
if (retval != CURLE_OK) {
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);

View File

@@ -39,7 +39,7 @@ int http_get_file(mfhttp * conn, const char *url,
json_t *http_parse_buf_json(mfhttp * conn, size_t flags,
json_error_t * error);
int http_post_file(mfhttp * conn, const char *url,
const char *path, const char *filename,
FILE * fh, const char *filename,
uint64_t filesize, const char *fhash,
int (*data_handler) (mfhttp * conn, void *data),
void *data);