diff options
Diffstat (limited to '')
-rw-r--r-- | fuse_sdcard_provider.cpp (renamed from fuse_sdcard_provider.c) | 78 | ||||
-rw-r--r-- | fuse_sdcard_provider.h | 9 | ||||
-rw-r--r-- | fuse_sideload.cpp (renamed from fuse_sideload.c) | 31 | ||||
-rw-r--r-- | fuse_sideload.h | 6 |
4 files changed, 30 insertions, 94 deletions
diff --git a/fuse_sdcard_provider.c b/fuse_sdcard_provider.cpp index 4565c7b5b..df9631272 100644 --- a/fuse_sdcard_provider.c +++ b/fuse_sdcard_provider.cpp @@ -18,7 +18,6 @@ #include <stdio.h> #include <string.h> #include <errno.h> -#include <pthread.h> #include <sys/mount.h> #include <sys/stat.h> #include <unistd.h> @@ -34,7 +33,7 @@ struct file_data { }; static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { - struct file_data* fd = (struct file_data*)cookie; + file_data* fd = reinterpret_cast<file_data*>(cookie); off64_t offset = ((off64_t) block) * fd->block_size; if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) { @@ -56,85 +55,34 @@ static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32 } static void close_file(void* cookie) { - struct file_data* fd = (struct file_data*)cookie; + file_data* fd = reinterpret_cast<file_data*>(cookie); close(fd->fd); } -struct token { - pthread_t th; - const char* path; - int result; -}; - -static void* run_sdcard_fuse(void* cookie) { - struct token* t = (struct token*)cookie; - +bool start_sdcard_fuse(const char* path) { struct stat sb; - if (stat(t->path, &sb) < 0) { - fprintf(stderr, "failed to stat %s: %s\n", t->path, strerror(errno)); - t->result = -1; - return NULL; + if (stat(path, &sb) == -1) { + fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno)); + return false; } - struct file_data fd; - struct provider_vtab vtab; - - fd.fd = open(t->path, O_RDONLY); - if (fd.fd < 0) { - fprintf(stderr, "failed to open %s: %s\n", t->path, strerror(errno)); - t->result = -1; - return NULL; + file_data fd; + fd.fd = open(path, O_RDONLY); + if (fd.fd == -1) { + fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno)); + return false; } fd.file_size = sb.st_size; fd.block_size = 65536; + provider_vtab vtab; vtab.read_block = read_block_file; vtab.close = close_file; - t->result = run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size); - return NULL; -} - -// How long (in seconds) we wait for the fuse-provided package file to -// appear, before timing out. -#define SDCARD_INSTALL_TIMEOUT 10 - -void* start_sdcard_fuse(const char* path) { - struct token* t = malloc(sizeof(struct token)); - - t->path = path; - pthread_create(&(t->th), NULL, run_sdcard_fuse, t); - - struct stat st; - int i; - for (i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) { - if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { - if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) { - sleep(1); - continue; - } else { - return NULL; - } - } - } - // The installation process expects to find the sdcard unmounted. // Unmount it with MNT_DETACH so that our open file continues to // work but new references see it as unmounted. umount2("/sdcard", MNT_DETACH); - return t; -} - -void finish_sdcard_fuse(void* cookie) { - if (cookie == NULL) return; - struct token* t = (struct token*)cookie; - - // Calling stat() on this magic filename signals the fuse - // filesystem to shut down. - struct stat st; - stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); - - pthread_join(t->th, NULL); - free(t); + return run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size) == 0; } diff --git a/fuse_sdcard_provider.h b/fuse_sdcard_provider.h index dbfbcd521..bdc60f2ba 100644 --- a/fuse_sdcard_provider.h +++ b/fuse_sdcard_provider.h @@ -17,13 +17,6 @@ #ifndef __FUSE_SDCARD_PROVIDER_H #define __FUSE_SDCARD_PROVIDER_H -#include <sys/cdefs.h> - -__BEGIN_DECLS - -void* start_sdcard_fuse(const char* path); -void finish_sdcard_fuse(void* token); - -__END_DECLS +bool start_sdcard_fuse(const char* path); #endif diff --git a/fuse_sideload.c b/fuse_sideload.cpp index f09b02601..66dc4be86 100644 --- a/fuse_sideload.c +++ b/fuse_sideload.cpp @@ -61,7 +61,8 @@ #include <sys/uio.h> #include <unistd.h> -#include "mincrypt/sha256.h" +#include <openssl/sha.h> + #include "fuse_sideload.h" #define PACKAGE_FILE_ID (FUSE_ROOT_ID+1) @@ -120,7 +121,7 @@ static void fuse_reply(struct fuse_data* fd, __u64 unique, const void *data, siz } static int handle_init(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { - const struct fuse_init_in* req = data; + const struct fuse_init_in* req = reinterpret_cast<const struct fuse_init_in*>(data); struct fuse_init_out out; size_t fuse_struct_size; @@ -174,7 +175,7 @@ static void fill_attr(struct fuse_attr* attr, struct fuse_data* fd, attr->mode = mode; } -static int handle_getattr(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { +static int handle_getattr(void* /* data */, struct fuse_data* fd, const struct fuse_in_header* hdr) { struct fuse_attr_out out; memset(&out, 0, sizeof(out)); out.attr_valid = 10; @@ -200,12 +201,12 @@ static int handle_lookup(void* data, struct fuse_data* fd, out.entry_valid = 10; out.attr_valid = 10; - if (strncmp(FUSE_SIDELOAD_HOST_FILENAME, data, + if (strncmp(FUSE_SIDELOAD_HOST_FILENAME, reinterpret_cast<const char*>(data), sizeof(FUSE_SIDELOAD_HOST_FILENAME)) == 0) { out.nodeid = PACKAGE_FILE_ID; out.generation = PACKAGE_FILE_ID; fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444); - } else if (strncmp(FUSE_SIDELOAD_HOST_EXIT_FLAG, data, + } else if (strncmp(FUSE_SIDELOAD_HOST_EXIT_FLAG, reinterpret_cast<const char*>(data), sizeof(FUSE_SIDELOAD_HOST_EXIT_FLAG)) == 0) { out.nodeid = EXIT_FLAG_ID; out.generation = EXIT_FLAG_ID; @@ -218,7 +219,7 @@ static int handle_lookup(void* data, struct fuse_data* fd, return (out.nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS; } -static int handle_open(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { +static int handle_open(void* /* data */, struct fuse_data* fd, const struct fuse_in_header* hdr) { if (hdr->nodeid == EXIT_FLAG_ID) return -EPERM; if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT; @@ -273,27 +274,27 @@ static int fetch_block(struct fuse_data* fd, uint32_t block) { // block). // - Otherwise, return -EINVAL for the read. - uint8_t hash[SHA256_DIGEST_SIZE]; - SHA256_hash(fd->block_data, fd->block_size, hash); - uint8_t* blockhash = fd->hashes + block * SHA256_DIGEST_SIZE; - if (memcmp(hash, blockhash, SHA256_DIGEST_SIZE) == 0) { + uint8_t hash[SHA256_DIGEST_LENGTH]; + SHA256(fd->block_data, fd->block_size, hash); + uint8_t* blockhash = fd->hashes + block * SHA256_DIGEST_LENGTH; + if (memcmp(hash, blockhash, SHA256_DIGEST_LENGTH) == 0) { return 0; } int i; - for (i = 0; i < SHA256_DIGEST_SIZE; ++i) { + for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) { if (blockhash[i] != 0) { fd->curr_block = -1; return -EIO; } } - memcpy(blockhash, hash, SHA256_DIGEST_SIZE); + memcpy(blockhash, hash, SHA256_DIGEST_LENGTH); return 0; } static int handle_read(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { - const struct fuse_read_in* req = data; + const struct fuse_read_in* req = reinterpret_cast<const struct fuse_read_in*>(data); struct fuse_out_header outhdr; struct iovec vec[3]; int vec_used; @@ -397,10 +398,10 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, goto done; } - fd.hashes = (uint8_t*)calloc(fd.file_blocks, SHA256_DIGEST_SIZE); + fd.hashes = (uint8_t*)calloc(fd.file_blocks, SHA256_DIGEST_LENGTH); if (fd.hashes == NULL) { fprintf(stderr, "failed to allocate %d bites for hashes\n", - fd.file_blocks * SHA256_DIGEST_SIZE); + fd.file_blocks * SHA256_DIGEST_LENGTH); result = -1; goto done; } diff --git a/fuse_sideload.h b/fuse_sideload.h index f9e3bf0d3..c0b16efbe 100644 --- a/fuse_sideload.h +++ b/fuse_sideload.h @@ -17,10 +17,6 @@ #ifndef __FUSE_SIDELOAD_H #define __FUSE_SIDELOAD_H -#include <sys/cdefs.h> - -__BEGIN_DECLS - // define the filenames created by the sideload FUSE filesystem #define FUSE_SIDELOAD_HOST_MOUNTPOINT "/sideload" #define FUSE_SIDELOAD_HOST_FILENAME "package.zip" @@ -39,6 +35,4 @@ struct provider_vtab { int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_size, uint32_t block_size); -__END_DECLS - #endif |