mirror of
https://github.com/xorgy/mediafire-fuse
synced 2026-01-13 13:14:29 -08:00
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:
@@ -21,6 +21,7 @@
|
||||
#define _MFAPI_APICALLS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "file.h"
|
||||
#include "folder.h"
|
||||
@@ -96,8 +97,7 @@ int mfconn_api_device_get_patch(mfconn * conn, mfpatch * patch,
|
||||
uint64_t target_revision);
|
||||
|
||||
int mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
|
||||
const char *file_path,
|
||||
const char *file_name,
|
||||
FILE * fh, const char *file_name,
|
||||
char **upload_key);
|
||||
|
||||
int mfconn_api_upload_poll_upload(mfconn * conn,
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "../../utils/http.h"
|
||||
#include "../../utils/json.h"
|
||||
@@ -37,43 +37,32 @@ static int _decode_upload_simple(mfhttp * conn, void *data);
|
||||
|
||||
int
|
||||
mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
|
||||
const char *file_path, const char *file_name,
|
||||
char **upload_key)
|
||||
FILE * fh, const char *file_name, char **upload_key)
|
||||
{
|
||||
const char *api_call;
|
||||
int retval;
|
||||
mfhttp *http;
|
||||
FILE *fh;
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
char *file_hash;
|
||||
struct stat file_info;
|
||||
uint64_t file_size;
|
||||
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
fh = fopen(file_path, "r");
|
||||
if (fh == NULL) {
|
||||
perror("cannot open file");
|
||||
fprintf(stderr, "cannot open %s\n", file_path);
|
||||
if (fh == NULL)
|
||||
return -1;
|
||||
}
|
||||
|
||||
retval = calc_sha256(fh, hash);
|
||||
fclose(fh);
|
||||
// make sure that we are at the beginning of the file
|
||||
rewind(fh);
|
||||
|
||||
// calculate hash
|
||||
retval = calc_sha256(fh, hash, &file_size);
|
||||
|
||||
if (retval != 0) {
|
||||
fprintf(stderr, "failed to calculate hash\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&file_info, 0, sizeof(file_info));
|
||||
retval = stat(file_path, &file_info);
|
||||
|
||||
if (retval != 0) {
|
||||
fprintf(stderr, "stat failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
file_hash = binary2hex(hash, SHA256_DIGEST_LENGTH);
|
||||
|
||||
if (folderkey == NULL) {
|
||||
@@ -87,9 +76,12 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey,
|
||||
"&folder_key=%s", folderkey);
|
||||
}
|
||||
|
||||
// make sure that we are at the beginning of the file
|
||||
rewind(fh);
|
||||
|
||||
http = http_create();
|
||||
retval = http_post_file(http, api_call, file_path, file_name,
|
||||
file_info.st_size, file_hash,
|
||||
retval = http_post_file(http, api_call, fh, file_name,
|
||||
file_size, file_hash,
|
||||
_decode_upload_simple, upload_key);
|
||||
http_destroy(http);
|
||||
|
||||
@@ -118,7 +110,11 @@ static int _decode_upload_simple(mfhttp * conn, void *user_ptr)
|
||||
|
||||
j_obj = json_object_get(node, "key");
|
||||
if (j_obj != NULL) {
|
||||
*upload_key = strdup(json_string_value(j_obj));
|
||||
if (strcmp(json_string_value(j_obj), "") == 0) {
|
||||
*upload_key = NULL;
|
||||
} else {
|
||||
*upload_key = strdup(json_string_value(j_obj));
|
||||
}
|
||||
} else {
|
||||
*upload_key = NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user