Merge "Clean up fuse_sideload and add a testcase."
am: 73dca3e983
Change-Id: I1d895338f32c5814c22f7134aee579c2a252e8bb
This commit is contained in:
+44
-46
@@ -14,72 +14,70 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include "fuse_sdcard_provider.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/mount.h>
|
#include <sys/mount.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include <android-base/file.h>
|
#include <android-base/file.h>
|
||||||
|
|
||||||
#include "fuse_sideload.h"
|
#include "fuse_sideload.h"
|
||||||
|
|
||||||
struct file_data {
|
struct file_data {
|
||||||
int fd; // the underlying sdcard file
|
int fd; // the underlying sdcard file
|
||||||
|
|
||||||
uint64_t file_size;
|
uint64_t file_size;
|
||||||
uint32_t block_size;
|
uint32_t block_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
|
static int read_block_file(const file_data& fd, uint32_t block, uint8_t* buffer,
|
||||||
file_data* fd = reinterpret_cast<file_data*>(cookie);
|
uint32_t fetch_size) {
|
||||||
|
off64_t offset = static_cast<off64_t>(block) * fd.block_size;
|
||||||
|
if (TEMP_FAILURE_RETRY(lseek64(fd.fd, offset, SEEK_SET)) == -1) {
|
||||||
|
fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
off64_t offset = ((off64_t) block) * fd->block_size;
|
if (!android::base::ReadFully(fd.fd, buffer, fetch_size)) {
|
||||||
if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) {
|
fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
|
||||||
fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
|
return -EIO;
|
||||||
return -EIO;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!android::base::ReadFully(fd->fd, buffer, fetch_size)) {
|
return 0;
|
||||||
fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void close_file(void* cookie) {
|
|
||||||
file_data* fd = reinterpret_cast<file_data*>(cookie);
|
|
||||||
close(fd->fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool start_sdcard_fuse(const char* path) {
|
bool start_sdcard_fuse(const char* path) {
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
if (stat(path, &sb) == -1) {
|
if (stat(path, &sb) == -1) {
|
||||||
fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno));
|
fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_data fd;
|
file_data fd;
|
||||||
fd.fd = open(path, O_RDONLY);
|
fd.fd = open(path, O_RDONLY);
|
||||||
if (fd.fd == -1) {
|
if (fd.fd == -1) {
|
||||||
fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
|
fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fd.file_size = sb.st_size;
|
fd.file_size = sb.st_size;
|
||||||
fd.block_size = 65536;
|
fd.block_size = 65536;
|
||||||
|
|
||||||
provider_vtab vtab;
|
provider_vtab vtab;
|
||||||
vtab.read_block = read_block_file;
|
vtab.read_block = std::bind(&read_block_file, fd, std::placeholders::_1, std::placeholders::_2,
|
||||||
vtab.close = close_file;
|
std::placeholders::_3);
|
||||||
|
vtab.close = [&fd]() { close(fd.fd); };
|
||||||
|
|
||||||
// The installation process expects to find the sdcard unmounted.
|
// The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so
|
||||||
// Unmount it with MNT_DETACH so that our open file continues to
|
// that our open file continues to work but new references see it as unmounted.
|
||||||
// work but new references see it as unmounted.
|
umount2("/sdcard", MNT_DETACH);
|
||||||
umount2("/sdcard", MNT_DETACH);
|
|
||||||
|
|
||||||
return run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size) == 0;
|
return run_fuse_sideload(vtab, fd.file_size, fd.block_size) == 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+250
-287
@@ -41,337 +41,310 @@
|
|||||||
// two files is implemented. In particular, you can't opendir() or
|
// two files is implemented. In particular, you can't opendir() or
|
||||||
// readdir() on the "/sideload" directory; ls on it won't work.
|
// readdir() on the "/sideload" directory; ls on it won't work.
|
||||||
|
|
||||||
#include <ctype.h>
|
#include "fuse_sideload.h"
|
||||||
#include <dirent.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h> // PATH_MAX
|
||||||
#include <linux/fuse.h>
|
#include <linux/fuse.h>
|
||||||
#include <pthread.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/inotify.h>
|
|
||||||
#include <sys/mount.h>
|
#include <sys/mount.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h> // MIN
|
||||||
#include <sys/resource.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/statfs.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <android-base/stringprintf.h>
|
#include <android-base/stringprintf.h>
|
||||||
|
#include <android-base/unique_fd.h>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
#include "fuse_sideload.h"
|
static constexpr uint64_t PACKAGE_FILE_ID = FUSE_ROOT_ID + 1;
|
||||||
|
static constexpr uint64_t EXIT_FLAG_ID = FUSE_ROOT_ID + 2;
|
||||||
|
|
||||||
#define PACKAGE_FILE_ID (FUSE_ROOT_ID+1)
|
static constexpr int NO_STATUS = 1;
|
||||||
#define EXIT_FLAG_ID (FUSE_ROOT_ID+2)
|
static constexpr int NO_STATUS_EXIT = 2;
|
||||||
|
|
||||||
#define NO_STATUS 1
|
using SHA256Digest = std::array<uint8_t, SHA256_DIGEST_LENGTH>;
|
||||||
#define NO_STATUS_EXIT 2
|
|
||||||
|
|
||||||
struct fuse_data {
|
struct fuse_data {
|
||||||
int ffd; // file descriptor for the fuse socket
|
android::base::unique_fd ffd; // file descriptor for the fuse socket
|
||||||
|
|
||||||
struct provider_vtab* vtab;
|
provider_vtab vtab;
|
||||||
void* cookie;
|
|
||||||
|
|
||||||
uint64_t file_size; // bytes
|
uint64_t file_size; // bytes
|
||||||
|
|
||||||
uint32_t block_size; // block size that the adb host is using to send the file to us
|
uint32_t block_size; // block size that the adb host is using to send the file to us
|
||||||
uint32_t file_blocks; // file size in block_size blocks
|
uint32_t file_blocks; // file size in block_size blocks
|
||||||
|
|
||||||
uid_t uid;
|
uid_t uid;
|
||||||
gid_t gid;
|
gid_t gid;
|
||||||
|
|
||||||
uint32_t curr_block; // cache the block most recently read from the host
|
uint32_t curr_block; // cache the block most recently read from the host
|
||||||
uint8_t* block_data;
|
uint8_t* block_data;
|
||||||
|
|
||||||
uint8_t* extra_block; // another block of storage for reads that
|
uint8_t* extra_block; // another block of storage for reads that span two blocks
|
||||||
// span two blocks
|
|
||||||
|
|
||||||
uint8_t* hashes; // SHA-256 hash of each block (all zeros
|
std::vector<SHA256Digest>
|
||||||
// if block hasn't been read yet)
|
hashes; // SHA-256 hash of each block (all zeros if block hasn't been read yet)
|
||||||
};
|
};
|
||||||
|
|
||||||
static void fuse_reply(struct fuse_data* fd, __u64 unique, const void *data, size_t len)
|
static void fuse_reply(const fuse_data* fd, uint64_t unique, const void* data, size_t len) {
|
||||||
{
|
fuse_out_header hdr;
|
||||||
struct fuse_out_header hdr;
|
hdr.len = len + sizeof(hdr);
|
||||||
struct iovec vec[2];
|
hdr.error = 0;
|
||||||
int res;
|
hdr.unique = unique;
|
||||||
|
|
||||||
hdr.len = len + sizeof(hdr);
|
struct iovec vec[2];
|
||||||
hdr.error = 0;
|
vec[0].iov_base = &hdr;
|
||||||
hdr.unique = unique;
|
vec[0].iov_len = sizeof(hdr);
|
||||||
|
vec[1].iov_base = const_cast<void*>(data);
|
||||||
|
vec[1].iov_len = len;
|
||||||
|
|
||||||
vec[0].iov_base = &hdr;
|
int res = writev(fd->ffd, vec, 2);
|
||||||
vec[0].iov_len = sizeof(hdr);
|
if (res == -1) {
|
||||||
vec[1].iov_base = /* const_cast */(void*)(data);
|
printf("*** REPLY FAILED *** %s\n", strerror(errno));
|
||||||
vec[1].iov_len = len;
|
}
|
||||||
|
|
||||||
res = writev(fd->ffd, vec, 2);
|
|
||||||
if (res < 0) {
|
|
||||||
printf("*** REPLY FAILED *** %s\n", strerror(errno));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_init(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) {
|
static int handle_init(void* data, fuse_data* fd, const fuse_in_header* hdr) {
|
||||||
const struct fuse_init_in* req = reinterpret_cast<const struct fuse_init_in*>(data);
|
const fuse_init_in* req = static_cast<const fuse_init_in*>(data);
|
||||||
struct fuse_init_out out;
|
|
||||||
size_t fuse_struct_size;
|
|
||||||
|
|
||||||
|
// Kernel 2.6.16 is the first stable kernel with struct fuse_init_out defined (fuse version 7.6).
|
||||||
|
// The structure is the same from 7.6 through 7.22. Beginning with 7.23, the structure increased
|
||||||
|
// in size and added new parameters.
|
||||||
|
if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
|
||||||
|
printf("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6", req->major,
|
||||||
|
req->minor, FUSE_KERNEL_VERSION);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
|
fuse_init_out out;
|
||||||
* defined (fuse version 7.6). The structure is the same from 7.6 through
|
out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
|
||||||
* 7.22. Beginning with 7.23, the structure increased in size and added
|
size_t fuse_struct_size = sizeof(out);
|
||||||
* new parameters.
|
|
||||||
*/
|
|
||||||
if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
|
|
||||||
printf("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
|
|
||||||
req->major, req->minor, FUSE_KERNEL_VERSION);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
|
|
||||||
fuse_struct_size = sizeof(out);
|
|
||||||
#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
|
#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
|
||||||
/* FUSE_KERNEL_VERSION >= 23. */
|
/* FUSE_KERNEL_VERSION >= 23. */
|
||||||
|
|
||||||
/* If the kernel only works on minor revs older than or equal to 22,
|
// If the kernel only works on minor revs older than or equal to 22, then use the older structure
|
||||||
* then use the older structure size since this code only uses the 7.22
|
// size since this code only uses the 7.22 version of the structure.
|
||||||
* version of the structure. */
|
if (req->minor <= 22) {
|
||||||
if (req->minor <= 22) {
|
fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
|
||||||
fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
|
}
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
out.major = FUSE_KERNEL_VERSION;
|
out.major = FUSE_KERNEL_VERSION;
|
||||||
out.max_readahead = req->max_readahead;
|
out.max_readahead = req->max_readahead;
|
||||||
out.flags = 0;
|
out.flags = 0;
|
||||||
out.max_background = 32;
|
out.max_background = 32;
|
||||||
out.congestion_threshold = 32;
|
out.congestion_threshold = 32;
|
||||||
out.max_write = 4096;
|
out.max_write = 4096;
|
||||||
fuse_reply(fd, hdr->unique, &out, fuse_struct_size);
|
fuse_reply(fd, hdr->unique, &out, fuse_struct_size);
|
||||||
|
|
||||||
return NO_STATUS;
|
return NO_STATUS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fill_attr(struct fuse_attr* attr, struct fuse_data* fd,
|
static void fill_attr(fuse_attr* attr, const fuse_data* fd, uint64_t nodeid, uint64_t size,
|
||||||
uint64_t nodeid, uint64_t size, uint32_t mode) {
|
uint32_t mode) {
|
||||||
memset(attr, 0, sizeof(*attr));
|
*attr = {};
|
||||||
attr->nlink = 1;
|
attr->nlink = 1;
|
||||||
attr->uid = fd->uid;
|
attr->uid = fd->uid;
|
||||||
attr->gid = fd->gid;
|
attr->gid = fd->gid;
|
||||||
attr->blksize = 4096;
|
attr->blksize = 4096;
|
||||||
|
|
||||||
attr->ino = nodeid;
|
attr->ino = nodeid;
|
||||||
attr->size = size;
|
attr->size = size;
|
||||||
attr->blocks = (size == 0) ? 0 : (((size-1) / attr->blksize) + 1);
|
attr->blocks = (size == 0) ? 0 : (((size - 1) / attr->blksize) + 1);
|
||||||
attr->mode = mode;
|
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 */, const fuse_data* fd, const fuse_in_header* hdr) {
|
||||||
struct fuse_attr_out out;
|
fuse_attr_out out = {};
|
||||||
memset(&out, 0, sizeof(out));
|
out.attr_valid = 10;
|
||||||
out.attr_valid = 10;
|
|
||||||
|
|
||||||
if (hdr->nodeid == FUSE_ROOT_ID) {
|
if (hdr->nodeid == FUSE_ROOT_ID) {
|
||||||
fill_attr(&(out.attr), fd, hdr->nodeid, 4096, S_IFDIR | 0555);
|
fill_attr(&(out.attr), fd, hdr->nodeid, 4096, S_IFDIR | 0555);
|
||||||
} else if (hdr->nodeid == PACKAGE_FILE_ID) {
|
} else if (hdr->nodeid == PACKAGE_FILE_ID) {
|
||||||
fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444);
|
fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444);
|
||||||
} else if (hdr->nodeid == EXIT_FLAG_ID) {
|
} else if (hdr->nodeid == EXIT_FLAG_ID) {
|
||||||
fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0);
|
fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0);
|
||||||
} else {
|
} else {
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
fuse_reply(fd, hdr->unique, &out, sizeof(out));
|
fuse_reply(fd, hdr->unique, &out, sizeof(out));
|
||||||
return (hdr->nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS;
|
return (hdr->nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_lookup(void* data, struct fuse_data* fd,
|
static int handle_lookup(void* data, const fuse_data* fd, const fuse_in_header* hdr) {
|
||||||
const struct fuse_in_header* hdr) {
|
if (data == nullptr) return -ENOENT;
|
||||||
struct fuse_entry_out out;
|
|
||||||
memset(&out, 0, sizeof(out));
|
|
||||||
out.entry_valid = 10;
|
|
||||||
out.attr_valid = 10;
|
|
||||||
|
|
||||||
if (strncmp(FUSE_SIDELOAD_HOST_FILENAME, reinterpret_cast<const char*>(data),
|
fuse_entry_out out = {};
|
||||||
sizeof(FUSE_SIDELOAD_HOST_FILENAME)) == 0) {
|
out.entry_valid = 10;
|
||||||
out.nodeid = PACKAGE_FILE_ID;
|
out.attr_valid = 10;
|
||||||
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, reinterpret_cast<const char*>(data),
|
|
||||||
sizeof(FUSE_SIDELOAD_HOST_EXIT_FLAG)) == 0) {
|
|
||||||
out.nodeid = EXIT_FLAG_ID;
|
|
||||||
out.generation = EXIT_FLAG_ID;
|
|
||||||
fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0);
|
|
||||||
} else {
|
|
||||||
return -ENOENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
fuse_reply(fd, hdr->unique, &out, sizeof(out));
|
std::string filename(static_cast<const char*>(data));
|
||||||
return (out.nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS;
|
if (filename == FUSE_SIDELOAD_HOST_FILENAME) {
|
||||||
|
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 (filename == FUSE_SIDELOAD_HOST_EXIT_FLAG) {
|
||||||
|
out.nodeid = EXIT_FLAG_ID;
|
||||||
|
out.generation = EXIT_FLAG_ID;
|
||||||
|
fill_attr(&(out.attr), fd, EXIT_FLAG_ID, 0, S_IFREG | 0);
|
||||||
|
} else {
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
fuse_reply(fd, hdr->unique, &out, sizeof(out));
|
||||||
|
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 */, const fuse_data* fd, const fuse_in_header* hdr) {
|
||||||
if (hdr->nodeid == EXIT_FLAG_ID) return -EPERM;
|
if (hdr->nodeid == EXIT_FLAG_ID) return -EPERM;
|
||||||
if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT;
|
if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT;
|
||||||
|
|
||||||
struct fuse_open_out out;
|
fuse_open_out out = {};
|
||||||
memset(&out, 0, sizeof(out));
|
out.fh = 10; // an arbitrary number; we always use the same handle
|
||||||
out.fh = 10; // an arbitrary number; we always use the same handle
|
fuse_reply(fd, hdr->unique, &out, sizeof(out));
|
||||||
fuse_reply(fd, hdr->unique, &out, sizeof(out));
|
return NO_STATUS;
|
||||||
return NO_STATUS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_flush(void* /* data */, struct fuse_data* /* fd */,
|
static int handle_flush(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) {
|
||||||
const struct fuse_in_header* /* hdr */) {
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_release(void* /* data */, struct fuse_data* /* fd */,
|
static int handle_release(void* /* data */, fuse_data* /* fd */, const fuse_in_header* /* hdr */) {
|
||||||
const struct fuse_in_header* /* hdr */) {
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch a block from the host into fd->curr_block and fd->block_data.
|
// Fetch a block from the host into fd->curr_block and fd->block_data.
|
||||||
// Returns 0 on successful fetch, negative otherwise.
|
// Returns 0 on successful fetch, negative otherwise.
|
||||||
static int fetch_block(struct fuse_data* fd, uint32_t block) {
|
static int fetch_block(fuse_data* fd, uint32_t block) {
|
||||||
if (block == fd->curr_block) {
|
if (block == fd->curr_block) {
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block >= fd->file_blocks) {
|
|
||||||
memset(fd->block_data, 0, fd->block_size);
|
|
||||||
fd->curr_block = block;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t fetch_size = fd->block_size;
|
|
||||||
if (block * fd->block_size + fetch_size > fd->file_size) {
|
|
||||||
// If we're reading the last (partial) block of the file,
|
|
||||||
// expect a shorter response from the host, and pad the rest
|
|
||||||
// of the block with zeroes.
|
|
||||||
fetch_size = fd->file_size - (block * fd->block_size);
|
|
||||||
memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
int result = fd->vtab->read_block(fd->cookie, block, fd->block_data, fetch_size);
|
|
||||||
if (result < 0) return result;
|
|
||||||
|
|
||||||
fd->curr_block = block;
|
|
||||||
|
|
||||||
// Verify the hash of the block we just got from the host.
|
|
||||||
//
|
|
||||||
// - If the hash of the just-received data matches the stored hash
|
|
||||||
// for the block, accept it.
|
|
||||||
// - If the stored hash is all zeroes, store the new hash and
|
|
||||||
// accept the block (this is the first time we've read this
|
|
||||||
// block).
|
|
||||||
// - Otherwise, return -EINVAL for the read.
|
|
||||||
|
|
||||||
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_LENGTH; ++i) {
|
|
||||||
if (blockhash[i] != 0) {
|
|
||||||
fd->curr_block = -1;
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(blockhash, hash, SHA256_DIGEST_LENGTH);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block >= fd->file_blocks) {
|
||||||
|
memset(fd->block_data, 0, fd->block_size);
|
||||||
|
fd->curr_block = block;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fetch_size = fd->block_size;
|
||||||
|
if (block * fd->block_size + fetch_size > fd->file_size) {
|
||||||
|
// If we're reading the last (partial) block of the file, expect a shorter response from the
|
||||||
|
// host, and pad the rest of the block with zeroes.
|
||||||
|
fetch_size = fd->file_size - (block * fd->block_size);
|
||||||
|
memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int result = fd->vtab.read_block(block, fd->block_data, fetch_size);
|
||||||
|
if (result < 0) return result;
|
||||||
|
|
||||||
|
fd->curr_block = block;
|
||||||
|
|
||||||
|
// Verify the hash of the block we just got from the host.
|
||||||
|
//
|
||||||
|
// - If the hash of the just-received data matches the stored hash for the block, accept it.
|
||||||
|
// - If the stored hash is all zeroes, store the new hash and accept the block (this is the first
|
||||||
|
// time we've read this block).
|
||||||
|
// - Otherwise, return -EINVAL for the read.
|
||||||
|
|
||||||
|
SHA256Digest hash;
|
||||||
|
SHA256(fd->block_data, fd->block_size, hash.data());
|
||||||
|
|
||||||
|
const SHA256Digest& blockhash = fd->hashes[block];
|
||||||
|
if (hash == blockhash) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i : blockhash) {
|
||||||
|
if (i != 0) {
|
||||||
|
fd->curr_block = -1;
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fd->hashes[block] = hash;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_read(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) {
|
static int handle_read(void* data, fuse_data* fd, const fuse_in_header* hdr) {
|
||||||
const struct fuse_read_in* req = reinterpret_cast<const struct fuse_read_in*>(data);
|
if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT;
|
||||||
struct fuse_out_header outhdr;
|
|
||||||
struct iovec vec[3];
|
|
||||||
int vec_used;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT;
|
const fuse_read_in* req = static_cast<const fuse_read_in*>(data);
|
||||||
|
uint64_t offset = req->offset;
|
||||||
|
uint32_t size = req->size;
|
||||||
|
|
||||||
uint64_t offset = req->offset;
|
// The docs on the fuse kernel interface are vague about what to do when a read request extends
|
||||||
uint32_t size = req->size;
|
// past the end of the file. We can return a short read -- the return structure does include a
|
||||||
|
// length field -- but in testing that caused the program using the file to segfault. (I
|
||||||
|
// speculate that this is due to the reading program accessing it via mmap; maybe mmap dislikes
|
||||||
|
// when you return something short of a whole page?) To fix this we zero-pad reads that extend
|
||||||
|
// past the end of the file so we're always returning exactly as many bytes as were requested.
|
||||||
|
// (Users of the mapped file have to know its real length anyway.)
|
||||||
|
|
||||||
// The docs on the fuse kernel interface are vague about what to
|
fuse_out_header outhdr;
|
||||||
// do when a read request extends past the end of the file. We
|
outhdr.len = sizeof(outhdr) + size;
|
||||||
// can return a short read -- the return structure does include a
|
outhdr.error = 0;
|
||||||
// length field -- but in testing that caused the program using
|
outhdr.unique = hdr->unique;
|
||||||
// the file to segfault. (I speculate that this is due to the
|
|
||||||
// reading program accessing it via mmap; maybe mmap dislikes when
|
|
||||||
// you return something short of a whole page?) To fix this we
|
|
||||||
// zero-pad reads that extend past the end of the file so we're
|
|
||||||
// always returning exactly as many bytes as were requested.
|
|
||||||
// (Users of the mapped file have to know its real length anyway.)
|
|
||||||
|
|
||||||
outhdr.len = sizeof(outhdr) + size;
|
struct iovec vec[3];
|
||||||
outhdr.error = 0;
|
vec[0].iov_base = &outhdr;
|
||||||
outhdr.unique = hdr->unique;
|
vec[0].iov_len = sizeof(outhdr);
|
||||||
vec[0].iov_base = &outhdr;
|
|
||||||
vec[0].iov_len = sizeof(outhdr);
|
|
||||||
|
|
||||||
uint32_t block = offset / fd->block_size;
|
uint32_t block = offset / fd->block_size;
|
||||||
result = fetch_block(fd, block);
|
int result = fetch_block(fd, block);
|
||||||
|
if (result != 0) return result;
|
||||||
|
|
||||||
|
// Two cases:
|
||||||
|
//
|
||||||
|
// - the read request is entirely within this block. In this case we can reply immediately.
|
||||||
|
//
|
||||||
|
// - the read request goes over into the next block. Note that since we mount the filesystem
|
||||||
|
// with max_read=block_size, a read can never span more than two blocks. In this case we copy
|
||||||
|
// the block to extra_block and issue a fetch for the following block.
|
||||||
|
|
||||||
|
uint32_t block_offset = offset - (block * fd->block_size);
|
||||||
|
|
||||||
|
int vec_used;
|
||||||
|
if (size + block_offset <= fd->block_size) {
|
||||||
|
// First case: the read fits entirely in the first block.
|
||||||
|
|
||||||
|
vec[1].iov_base = fd->block_data + block_offset;
|
||||||
|
vec[1].iov_len = size;
|
||||||
|
vec_used = 2;
|
||||||
|
} else {
|
||||||
|
// Second case: the read spills over into the next block.
|
||||||
|
|
||||||
|
memcpy(fd->extra_block, fd->block_data + block_offset, fd->block_size - block_offset);
|
||||||
|
vec[1].iov_base = fd->extra_block;
|
||||||
|
vec[1].iov_len = fd->block_size - block_offset;
|
||||||
|
|
||||||
|
result = fetch_block(fd, block + 1);
|
||||||
if (result != 0) return result;
|
if (result != 0) return result;
|
||||||
|
vec[2].iov_base = fd->block_data;
|
||||||
|
vec[2].iov_len = size - vec[1].iov_len;
|
||||||
|
vec_used = 3;
|
||||||
|
}
|
||||||
|
|
||||||
// Two cases:
|
if (writev(fd->ffd, vec, vec_used) == -1) {
|
||||||
//
|
printf("*** READ REPLY FAILED: %s ***\n", strerror(errno));
|
||||||
// - the read request is entirely within this block. In this
|
}
|
||||||
// case we can reply immediately.
|
return NO_STATUS;
|
||||||
//
|
|
||||||
// - the read request goes over into the next block. Note that
|
|
||||||
// since we mount the filesystem with max_read=block_size, a
|
|
||||||
// read can never span more than two blocks. In this case we
|
|
||||||
// copy the block to extra_block and issue a fetch for the
|
|
||||||
// following block.
|
|
||||||
|
|
||||||
uint32_t block_offset = offset - (block * fd->block_size);
|
|
||||||
|
|
||||||
if (size + block_offset <= fd->block_size) {
|
|
||||||
// First case: the read fits entirely in the first block.
|
|
||||||
|
|
||||||
vec[1].iov_base = fd->block_data + block_offset;
|
|
||||||
vec[1].iov_len = size;
|
|
||||||
vec_used = 2;
|
|
||||||
} else {
|
|
||||||
// Second case: the read spills over into the next block.
|
|
||||||
|
|
||||||
memcpy(fd->extra_block, fd->block_data + block_offset,
|
|
||||||
fd->block_size - block_offset);
|
|
||||||
vec[1].iov_base = fd->extra_block;
|
|
||||||
vec[1].iov_len = fd->block_size - block_offset;
|
|
||||||
|
|
||||||
result = fetch_block(fd, block+1);
|
|
||||||
if (result != 0) return result;
|
|
||||||
vec[2].iov_base = fd->block_data;
|
|
||||||
vec[2].iov_len = size - vec[1].iov_len;
|
|
||||||
vec_used = 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (writev(fd->ffd, vec, vec_used) < 0) {
|
|
||||||
printf("*** READ REPLY FAILED: %s ***\n", strerror(errno));
|
|
||||||
}
|
|
||||||
return NO_STATUS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_size,
|
int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t block_size,
|
||||||
uint32_t block_size) {
|
const char* mount_point) {
|
||||||
// If something's already mounted on our mountpoint, try to remove it. (Mostly in case of a
|
// If something's already mounted on our mountpoint, try to remove it. (Mostly in case of a
|
||||||
// previous abnormal exit.)
|
// previous abnormal exit.)
|
||||||
umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_FORCE);
|
umount2(mount_point, MNT_FORCE);
|
||||||
|
|
||||||
// fs/fuse/inode.c in kernel code uses the greater of 4096 and the passed-in max_read.
|
// fs/fuse/inode.c in kernel code uses the greater of 4096 and the passed-in max_read.
|
||||||
if (block_size < 4096) {
|
if (block_size < 4096) {
|
||||||
@@ -383,9 +356,8 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_si
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fuse_data fd = {};
|
fuse_data fd = {};
|
||||||
fd.vtab = vtab;
|
fd.vtab = vtab;
|
||||||
fd.cookie = cookie;
|
|
||||||
fd.file_size = file_size;
|
fd.file_size = file_size;
|
||||||
fd.block_size = block_size;
|
fd.block_size = block_size;
|
||||||
fd.file_blocks = (file_size == 0) ? 0 : (((file_size - 1) / block_size) + 1);
|
fd.file_blocks = (file_size == 0) ? 0 : (((file_size - 1) / block_size) + 1);
|
||||||
@@ -397,33 +369,27 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_si
|
|||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd.hashes = (uint8_t*)calloc(fd.file_blocks, SHA256_DIGEST_LENGTH);
|
// All hashes will be zero-initialized.
|
||||||
if (fd.hashes == NULL) {
|
fd.hashes.resize(fd.file_blocks);
|
||||||
fprintf(stderr, "failed to allocate %d bites for hashes\n",
|
|
||||||
fd.file_blocks * SHA256_DIGEST_LENGTH);
|
|
||||||
result = -1;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
fd.uid = getuid();
|
fd.uid = getuid();
|
||||||
fd.gid = getgid();
|
fd.gid = getgid();
|
||||||
|
|
||||||
fd.curr_block = -1;
|
fd.curr_block = -1;
|
||||||
fd.block_data = (uint8_t*)malloc(block_size);
|
fd.block_data = static_cast<uint8_t*>(malloc(block_size));
|
||||||
if (fd.block_data == NULL) {
|
if (fd.block_data == nullptr) {
|
||||||
fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size);
|
fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size);
|
||||||
result = -1;
|
result = -1;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
fd.extra_block = (uint8_t*)malloc(block_size);
|
fd.extra_block = static_cast<uint8_t*>(malloc(block_size));
|
||||||
if (fd.extra_block == NULL) {
|
if (fd.extra_block == nullptr) {
|
||||||
fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size);
|
fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size);
|
||||||
result = -1;
|
result = -1;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd.ffd = open("/dev/fuse", O_RDWR);
|
fd.ffd.reset(open("/dev/fuse", O_RDWR));
|
||||||
if (fd.ffd < 0) {
|
if (!fd.ffd) {
|
||||||
perror("open /dev/fuse");
|
perror("open /dev/fuse");
|
||||||
result = -1;
|
result = -1;
|
||||||
goto done;
|
goto done;
|
||||||
@@ -431,18 +397,18 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_si
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::string opts = android::base::StringPrintf(
|
std::string opts = android::base::StringPrintf(
|
||||||
"fd=%d,user_id=%d,group_id=%d,max_read=%u,allow_other,rootmode=040000", fd.ffd, fd.uid,
|
"fd=%d,user_id=%d,group_id=%d,max_read=%u,allow_other,rootmode=040000", fd.ffd.get(),
|
||||||
fd.gid, block_size);
|
fd.uid, fd.gid, block_size);
|
||||||
|
|
||||||
result = mount("/dev/fuse", FUSE_SIDELOAD_HOST_MOUNTPOINT, "fuse",
|
result = mount("/dev/fuse", mount_point, "fuse", MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC,
|
||||||
MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC, opts.c_str());
|
opts.c_str());
|
||||||
if (result < 0) {
|
if (result == -1) {
|
||||||
perror("mount");
|
perror("mount");
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX * 8];
|
uint8_t request_buffer[sizeof(fuse_in_header) + PATH_MAX * 8];
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
|
ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
@@ -454,13 +420,13 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_si
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (static_cast<size_t>(len) < sizeof(struct fuse_in_header)) {
|
if (static_cast<size_t>(len) < sizeof(fuse_in_header)) {
|
||||||
fprintf(stderr, "request too short: len=%zd\n", len);
|
fprintf(stderr, "request too short: len=%zd\n", len);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fuse_in_header* hdr = reinterpret_cast<struct fuse_in_header*>(request_buffer);
|
fuse_in_header* hdr = reinterpret_cast<fuse_in_header*>(request_buffer);
|
||||||
void* data = request_buffer + sizeof(struct fuse_in_header);
|
void* data = request_buffer + sizeof(fuse_in_header);
|
||||||
|
|
||||||
result = -ENOSYS;
|
result = -ENOSYS;
|
||||||
|
|
||||||
@@ -504,7 +470,7 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_si
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result != NO_STATUS) {
|
if (result != NO_STATUS) {
|
||||||
struct fuse_out_header outhdr;
|
fuse_out_header outhdr;
|
||||||
outhdr.len = sizeof(outhdr);
|
outhdr.len = sizeof(outhdr);
|
||||||
outhdr.error = result;
|
outhdr.error = result;
|
||||||
outhdr.unique = hdr->unique;
|
outhdr.unique = hdr->unique;
|
||||||
@@ -513,15 +479,12 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_si
|
|||||||
}
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
fd.vtab->close(fd.cookie);
|
fd.vtab.close();
|
||||||
|
|
||||||
result = umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_DETACH);
|
if (umount2(mount_point, MNT_DETACH) == -1) {
|
||||||
if (result < 0) {
|
fprintf(stderr, "fuse_sideload umount failed: %s\n", strerror(errno));
|
||||||
printf("fuse_sideload umount failed: %s\n", strerror(errno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd.ffd) close(fd.ffd);
|
|
||||||
free(fd.hashes);
|
|
||||||
free(fd.block_data);
|
free(fd.block_data);
|
||||||
free(fd.extra_block);
|
free(fd.extra_block);
|
||||||
|
|
||||||
|
|||||||
+14
-12
@@ -17,22 +17,24 @@
|
|||||||
#ifndef __FUSE_SIDELOAD_H
|
#ifndef __FUSE_SIDELOAD_H
|
||||||
#define __FUSE_SIDELOAD_H
|
#define __FUSE_SIDELOAD_H
|
||||||
|
|
||||||
// define the filenames created by the sideload FUSE filesystem
|
#include <functional>
|
||||||
#define FUSE_SIDELOAD_HOST_MOUNTPOINT "/sideload"
|
|
||||||
#define FUSE_SIDELOAD_HOST_FILENAME "package.zip"
|
// Define the filenames created by the sideload FUSE filesystem.
|
||||||
#define FUSE_SIDELOAD_HOST_PATHNAME (FUSE_SIDELOAD_HOST_MOUNTPOINT "/" FUSE_SIDELOAD_HOST_FILENAME)
|
static constexpr const char* FUSE_SIDELOAD_HOST_MOUNTPOINT = "/sideload";
|
||||||
#define FUSE_SIDELOAD_HOST_EXIT_FLAG "exit"
|
static constexpr const char* FUSE_SIDELOAD_HOST_FILENAME = "package.zip";
|
||||||
#define FUSE_SIDELOAD_HOST_EXIT_PATHNAME (FUSE_SIDELOAD_HOST_MOUNTPOINT "/" FUSE_SIDELOAD_HOST_EXIT_FLAG)
|
static constexpr const char* FUSE_SIDELOAD_HOST_PATHNAME = "/sideload/package.zip";
|
||||||
|
static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_FLAG = "exit";
|
||||||
|
static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_PATHNAME = "/sideload/exit";
|
||||||
|
|
||||||
struct provider_vtab {
|
struct provider_vtab {
|
||||||
// read a block
|
// read a block
|
||||||
int (*read_block)(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size);
|
std::function<int(uint32_t block, uint8_t* buffer, uint32_t fetch_size)> read_block;
|
||||||
|
|
||||||
// close down
|
// close down
|
||||||
void (*close)(void* cookie);
|
std::function<void(void)> close;
|
||||||
};
|
};
|
||||||
|
|
||||||
int run_fuse_sideload(struct provider_vtab* vtab, void* cookie,
|
int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t block_size,
|
||||||
uint64_t file_size, uint32_t block_size);
|
const char* mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -14,46 +14,43 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include "fuse_adb_provider.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "adb.h"
|
#include "adb.h"
|
||||||
#include "adb_io.h"
|
#include "adb_io.h"
|
||||||
#include "fuse_adb_provider.h"
|
|
||||||
#include "fuse_sideload.h"
|
#include "fuse_sideload.h"
|
||||||
|
|
||||||
int read_block_adb(void* data, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
|
int read_block_adb(const adb_data& ad, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
|
||||||
adb_data* ad = reinterpret_cast<adb_data*>(data);
|
if (!WriteFdFmt(ad.sfd, "%08u", block)) {
|
||||||
|
fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno));
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
if (!WriteFdFmt(ad->sfd, "%08u", block)) {
|
if (!ReadFdExactly(ad.sfd, buffer, fetch_size)) {
|
||||||
fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno));
|
fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno));
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) {
|
return 0;
|
||||||
fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno));
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void close_adb(void* data) {
|
|
||||||
adb_data* ad = reinterpret_cast<adb_data*>(data);
|
|
||||||
WriteFdExactly(ad->sfd, "DONEDONE");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) {
|
int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) {
|
||||||
adb_data ad;
|
adb_data ad;
|
||||||
ad.sfd = sfd;
|
ad.sfd = sfd;
|
||||||
ad.file_size = file_size;
|
ad.file_size = file_size;
|
||||||
ad.block_size = block_size;
|
ad.block_size = block_size;
|
||||||
|
|
||||||
provider_vtab vtab;
|
provider_vtab vtab;
|
||||||
vtab.read_block = read_block_adb;
|
vtab.read_block = std::bind(read_block_adb, ad, std::placeholders::_1, std::placeholders::_2,
|
||||||
vtab.close = close_adb;
|
std::placeholders::_3);
|
||||||
|
vtab.close = [&ad]() { WriteFdExactly(ad.sfd, "DONEDONE"); };
|
||||||
|
|
||||||
return run_fuse_sideload(&vtab, &ad, file_size, block_size);
|
return run_fuse_sideload(vtab, file_size, block_size);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,13 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
struct adb_data {
|
struct adb_data {
|
||||||
int sfd; // file descriptor for the adb channel
|
int sfd; // file descriptor for the adb channel
|
||||||
|
|
||||||
uint64_t file_size;
|
uint64_t file_size;
|
||||||
uint32_t block_size;
|
uint32_t block_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size);
|
int read_block_adb(const adb_data& ad, uint32_t block, uint8_t* buffer, uint32_t fetch_size);
|
||||||
int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size);
|
int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ TEST(fuse_adb_provider, read_block_adb) {
|
|||||||
|
|
||||||
uint32_t block = 1234U;
|
uint32_t block = 1234U;
|
||||||
const char expected_block[] = "00001234";
|
const char expected_block[] = "00001234";
|
||||||
ASSERT_EQ(0, read_block_adb(static_cast<void*>(&data), block,
|
ASSERT_EQ(0, read_block_adb(data, block, reinterpret_cast<uint8_t*>(block_data),
|
||||||
reinterpret_cast<uint8_t*>(block_data), sizeof(expected_data) - 1));
|
sizeof(expected_data) - 1));
|
||||||
|
|
||||||
// Check that read_block_adb requested the right block.
|
// Check that read_block_adb requested the right block.
|
||||||
char block_req[sizeof(expected_block)] = {};
|
char block_req[sizeof(expected_block)] = {};
|
||||||
@@ -84,7 +84,7 @@ TEST(fuse_adb_provider, read_block_adb_fail_write) {
|
|||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
char buf[1];
|
char buf[1];
|
||||||
ASSERT_EQ(-EIO, read_block_adb(static_cast<void*>(&data), 0, reinterpret_cast<uint8_t*>(buf), 1));
|
ASSERT_EQ(-EIO, read_block_adb(data, 0, reinterpret_cast<uint8_t*>(buf), 1));
|
||||||
|
|
||||||
close(sockets[0]);
|
close(sockets[0]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,12 @@
|
|||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <android-base/file.h>
|
||||||
|
#include <android-base/strings.h>
|
||||||
|
#include <android-base/test_utils.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include "fuse_sideload.h"
|
#include "fuse_sideload.h"
|
||||||
@@ -26,11 +32,67 @@ TEST(SideloadTest, fuse_device) {
|
|||||||
|
|
||||||
TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
|
TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
|
||||||
provider_vtab vtab;
|
provider_vtab vtab;
|
||||||
vtab.close = [](void*) {};
|
vtab.close = [](void) {};
|
||||||
|
|
||||||
ASSERT_EQ(-1, run_fuse_sideload(&vtab, nullptr, 4096, 4095));
|
ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, 4095));
|
||||||
ASSERT_EQ(-1, run_fuse_sideload(&vtab, nullptr, 4096, (1 << 22) + 1));
|
ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, (1 << 22) + 1));
|
||||||
|
|
||||||
// Too many blocks.
|
// Too many blocks.
|
||||||
ASSERT_EQ(-1, run_fuse_sideload(&vtab, nullptr, ((1 << 18) + 1) * 4096, 4096));
|
ASSERT_EQ(-1, run_fuse_sideload(vtab, ((1 << 18) + 1) * 4096, 4096));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SideloadTest, run_fuse_sideload) {
|
||||||
|
const std::vector<std::string> blocks = {
|
||||||
|
std::string(2048, 'a') + std::string(2048, 'b'),
|
||||||
|
std::string(2048, 'c') + std::string(2048, 'd'),
|
||||||
|
std::string(2048, 'e') + std::string(2048, 'f'),
|
||||||
|
std::string(2048, 'g') + std::string(2048, 'h'),
|
||||||
|
};
|
||||||
|
const std::string content = android::base::Join(blocks, "");
|
||||||
|
ASSERT_EQ(16384U, content.size());
|
||||||
|
|
||||||
|
provider_vtab vtab;
|
||||||
|
vtab.close = [](void) {};
|
||||||
|
vtab.read_block = [&blocks](uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
|
||||||
|
if (block >= 4) return -1;
|
||||||
|
blocks[block].copy(reinterpret_cast<char*>(buffer), fetch_size);
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
TemporaryDir mount_point;
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid == 0) {
|
||||||
|
ASSERT_EQ(0, run_fuse_sideload(vtab, 16384, 4096, mount_point.path));
|
||||||
|
_exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string package = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_FILENAME;
|
||||||
|
int status;
|
||||||
|
static constexpr int kSideloadInstallTimeout = 10;
|
||||||
|
for (int i = 0; i < kSideloadInstallTimeout; ++i) {
|
||||||
|
ASSERT_NE(-1, waitpid(pid, &status, WNOHANG));
|
||||||
|
|
||||||
|
struct stat sb;
|
||||||
|
if (stat(package.c_str(), &sb) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errno == ENOENT && i < kSideloadInstallTimeout - 1) {
|
||||||
|
sleep(1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
FAIL() << "Timed out waiting for the fuse-provided package.";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string content_via_fuse;
|
||||||
|
ASSERT_TRUE(android::base::ReadFileToString(package, &content_via_fuse));
|
||||||
|
ASSERT_EQ(content, content_via_fuse);
|
||||||
|
|
||||||
|
std::string exit_flag = std::string(mount_point.path) + "/" + FUSE_SIDELOAD_HOST_EXIT_FLAG;
|
||||||
|
struct stat sb;
|
||||||
|
ASSERT_EQ(0, stat(exit_flag.c_str(), &sb));
|
||||||
|
|
||||||
|
waitpid(pid, &status, 0);
|
||||||
|
ASSERT_EQ(0, WEXITSTATUS(status));
|
||||||
|
ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user