Remove the provider_vtab

It's no longer needed with the newly added FuseDataProvider class. Also
cleans up the parameters for run_fuse_sideload.

Bug: 127071893
Test: unit tests pass, run a sideload
Change-Id: I1ccd6798d187cfc6ac9f559ffb3f3edf08dad55c
This commit is contained in:
xunchang 2019-03-15 16:04:32 -07:00
parent ea2912f187
commit 5e6832a24d
8 changed files with 40 additions and 59 deletions

View file

@ -20,26 +20,21 @@
#include <unistd.h> #include <unistd.h>
#include <functional> #include <functional>
#include <memory>
#include "fuse_provider.h" #include "fuse_provider.h"
#include "fuse_sideload.h" #include "fuse_sideload.h"
bool start_sdcard_fuse(const char* path) { bool start_sdcard_fuse(const char* path) {
FuseFileDataProvider file_data_reader(path, 65536); auto file_data_reader = std::make_unique<FuseFileDataProvider>(path, 65536);
if (!file_data_reader) { if (!file_data_reader->Valid()) {
return false; return false;
} }
provider_vtab vtab;
vtab.read_block = std::bind(&FuseFileDataProvider::ReadBlockAlignedData, &file_data_reader,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
vtab.close = [&file_data_reader]() { file_data_reader.Close(); };
// The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so // 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. // that our open file continues to work but new references see it as unmounted.
umount2("/sdcard", MNT_DETACH); umount2("/sdcard", MNT_DETACH);
return run_fuse_sideload(vtab, file_data_reader.file_size(), return run_fuse_sideload(std::move(file_data_reader)) == 0;
file_data_reader.fuse_block_size()) == 0;
} }

View file

@ -76,7 +76,7 @@ using SHA256Digest = std::array<uint8_t, SHA256_DIGEST_LENGTH>;
struct fuse_data { struct fuse_data {
android::base::unique_fd ffd; // file descriptor for the fuse socket android::base::unique_fd ffd; // file descriptor for the fuse socket
provider_vtab vtab; FuseDataProvider* provider; // Provider of the source data.
uint64_t file_size; // bytes uint64_t file_size; // bytes
@ -236,7 +236,7 @@ static int fetch_block(fuse_data* fd, uint32_t block) {
return 0; return 0;
} }
size_t fetch_size = fd->block_size; uint32_t fetch_size = fd->block_size;
if (block * fd->block_size + fetch_size > fd->file_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 // 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. // host, and pad the rest of the block with zeroes.
@ -244,7 +244,7 @@ static int fetch_block(fuse_data* fd, uint32_t block) {
memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size); memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size);
} }
if (!fd->vtab.read_block(block, fd->block_data, fetch_size)) { if (!fd->provider->ReadBlockAlignedData(fd->block_data, fetch_size, block)) {
return -EIO; return -EIO;
} }
@ -341,12 +341,14 @@ static int handle_read(void* data, fuse_data* fd, const fuse_in_header* hdr) {
return NO_STATUS; return NO_STATUS;
} }
int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t block_size, int run_fuse_sideload(std::unique_ptr<FuseDataProvider>&& provider, const char* mount_point) {
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(mount_point, MNT_FORCE); umount2(mount_point, MNT_FORCE);
uint64_t file_size = provider->file_size();
uint32_t block_size = provider->fuse_block_size();
// 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) {
fprintf(stderr, "block size (%u) is too small\n", block_size); fprintf(stderr, "block size (%u) is too small\n", block_size);
@ -358,7 +360,7 @@ int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t bl
} }
fuse_data fd = {}; fuse_data fd = {};
fd.vtab = vtab; fd.provider = provider.get();
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);
@ -480,7 +482,7 @@ int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t bl
} }
done: done:
fd.vtab.close(); provider->Close();
if (umount2(mount_point, MNT_DETACH) == -1) { if (umount2(mount_point, MNT_DETACH) == -1) {
fprintf(stderr, "fuse_sideload umount failed: %s\n", strerror(errno)); fprintf(stderr, "fuse_sideload umount failed: %s\n", strerror(errno));

View file

@ -37,7 +37,7 @@ class FuseDataProvider {
return fuse_block_size_; return fuse_block_size_;
} }
explicit operator bool() const { bool Valid() const {
return fd_ != -1; return fd_ != -1;
} }

View file

@ -17,7 +17,9 @@
#ifndef __FUSE_SIDELOAD_H #ifndef __FUSE_SIDELOAD_H
#define __FUSE_SIDELOAD_H #define __FUSE_SIDELOAD_H
#include <functional> #include <memory>
#include "fuse_provider.h"
// Define the filenames created by the sideload FUSE filesystem. // Define the filenames created by the sideload FUSE filesystem.
static constexpr const char* FUSE_SIDELOAD_HOST_MOUNTPOINT = "/sideload"; static constexpr const char* FUSE_SIDELOAD_HOST_MOUNTPOINT = "/sideload";
@ -26,15 +28,7 @@ static constexpr const char* FUSE_SIDELOAD_HOST_PATHNAME = "/sideload/package.zi
static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_FLAG = "exit"; static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_FLAG = "exit";
static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_PATHNAME = "/sideload/exit"; static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_PATHNAME = "/sideload/exit";
struct provider_vtab { int run_fuse_sideload(std::unique_ptr<FuseDataProvider>&& provider,
// read a block
std::function<bool(uint32_t block, uint8_t* buffer, uint32_t fetch_size)> read_block;
// close down
std::function<void(void)> close;
};
int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t block_size,
const char* mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT); const char* mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT);
#endif #endif

View file

@ -18,14 +18,10 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <functional>
#include "adb.h" #include "adb.h"
#include "adb_io.h" #include "adb_io.h"
#include "fuse_sideload.h"
bool FuseAdbDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, bool FuseAdbDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
uint32_t start_block) const { uint32_t start_block) const {
@ -45,14 +41,3 @@ bool FuseAdbDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_s
void FuseAdbDataProvider::Close() { void FuseAdbDataProvider::Close() {
WriteFdExactly(fd_, "DONEDONE"); WriteFdExactly(fd_, "DONEDONE");
} }
int run_adb_fuse(android::base::unique_fd&& sfd, uint64_t file_size, uint32_t block_size) {
FuseAdbDataProvider adb_data_reader(std::move(sfd), file_size, block_size);
provider_vtab vtab;
vtab.read_block = std::bind(&FuseAdbDataProvider::ReadBlockAlignedData, &adb_data_reader,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
vtab.close = [&adb_data_reader]() { adb_data_reader.Close(); };
return run_fuse_sideload(vtab, file_size, block_size);
}

View file

@ -35,6 +35,4 @@ class FuseAdbDataProvider : public FuseDataProvider {
void Close() override; void Close() override;
}; };
int run_adb_fuse(android::base::unique_fd&& sfd, uint64_t file_size, uint32_t block_size);
#endif #endif

View file

@ -22,6 +22,7 @@
#include <unistd.h> #include <unistd.h>
#include <functional> #include <functional>
#include <memory>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <thread> #include <thread>
@ -30,6 +31,7 @@
#include "adb_unique_fd.h" #include "adb_unique_fd.h"
#include "fdevent.h" #include "fdevent.h"
#include "fuse_adb_provider.h" #include "fuse_adb_provider.h"
#include "fuse_sideload.h"
#include "services.h" #include "services.h"
#include "sysdeps.h" #include "sysdeps.h"
@ -44,7 +46,9 @@ static void sideload_host_service(unique_fd sfd, const std::string& args) {
printf("sideload-host file size %" PRId64 " block size %d\n", file_size, block_size); printf("sideload-host file size %" PRId64 " block size %d\n", file_size, block_size);
int result = run_adb_fuse(std::move(sfd), file_size, block_size); auto adb_data_reader =
std::make_unique<FuseAdbDataProvider>(std::move(sfd), file_size, block_size);
int result = run_fuse_sideload(std::move(adb_data_reader));
printf("sideload_host finished\n"); printf("sideload_host finished\n");
exit(result == 0 ? 0 : 1); exit(result == 0 ? 0 : 1);

View file

@ -16,13 +16,16 @@
#include <unistd.h> #include <unistd.h>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/strings.h> #include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "fuse_provider.h"
#include "fuse_sideload.h" #include "fuse_sideload.h"
TEST(SideloadTest, fuse_device) { TEST(SideloadTest, fuse_device) {
@ -30,14 +33,17 @@ TEST(SideloadTest, fuse_device) {
} }
TEST(SideloadTest, run_fuse_sideload_wrong_parameters) { TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
provider_vtab vtab; auto provider_small_block =
vtab.close = [](void) {}; std::make_unique<FuseFileDataProvider>(android::base::unique_fd(), 4096, 4095);
ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_small_block)));
ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, 4095)); auto provider_large_block =
ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, (1 << 22) + 1)); std::make_unique<FuseFileDataProvider>(android::base::unique_fd(), 4096, (1 << 22) + 1);
ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_large_block)));
// Too many blocks. auto provider_too_many_blocks = std::make_unique<FuseFileDataProvider>(
ASSERT_EQ(-1, run_fuse_sideload(vtab, ((1 << 18) + 1) * 4096, 4096)); android::base::unique_fd(), ((1 << 18) + 1) * 4096, 4096);
ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_too_many_blocks)));
} }
TEST(SideloadTest, run_fuse_sideload) { TEST(SideloadTest, run_fuse_sideload) {
@ -50,18 +56,15 @@ TEST(SideloadTest, run_fuse_sideload) {
const std::string content = android::base::Join(blocks, ""); const std::string content = android::base::Join(blocks, "");
ASSERT_EQ(16384U, content.size()); ASSERT_EQ(16384U, content.size());
provider_vtab vtab; TemporaryFile temp_file;
vtab.close = [](void) {}; ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
vtab.read_block = [&blocks](uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
if (block >= 4) return false;
blocks[block].copy(reinterpret_cast<char*>(buffer), fetch_size);
return true;
};
auto provider = std::make_unique<FuseFileDataProvider>(temp_file.path, 4096);
ASSERT_TRUE(provider->Valid());
TemporaryDir mount_point; TemporaryDir mount_point;
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
ASSERT_EQ(0, run_fuse_sideload(vtab, 16384, 4096, mount_point.path)); ASSERT_EQ(0, run_fuse_sideload(std::move(provider), mount_point.path));
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
} }