From ea2912f187cec8d426cfc2ca715b9a503355fa37 Mon Sep 17 00:00:00 2001 From: xunchang Date: Sun, 17 Mar 2019 16:45:12 -0700 Subject: Create a FuseDataProvider base class The fuse data provider for adb/sdcard shares common code and structures. This cl creates a FuseDataProvider base class and provides implementations for adb and sdcard. In the follow cls, we can kill the provider_vtab struct; and also add another implementation to parse a block map file and provides data. Test: unit tests pass, sideload a package, apply a package from sdcard Change-Id: If8311666a52a2e3c0fbae0ee9688fa6d01e4ad09 --- minadbd/Android.bp | 1 + minadbd/fuse_adb_provider.cpp | 30 ++++++++++++++++-------------- minadbd/fuse_adb_provider.h | 20 ++++++++++++++------ minadbd/fuse_adb_provider_test.cpp | 31 +++++++++++++------------------ minadbd/minadbd_services.cpp | 2 +- 5 files changed, 45 insertions(+), 39 deletions(-) (limited to 'minadbd') diff --git a/minadbd/Android.bp b/minadbd/Android.bp index a95d979a5..9b889f4a2 100644 --- a/minadbd/Android.bp +++ b/minadbd/Android.bp @@ -66,6 +66,7 @@ cc_test { static_libs: [ "libminadbd_services", + "libfusesideload", "libadbd", ], diff --git a/minadbd/fuse_adb_provider.cpp b/minadbd/fuse_adb_provider.cpp index 9bd3f2392..cada4bd2a 100644 --- a/minadbd/fuse_adb_provider.cpp +++ b/minadbd/fuse_adb_provider.cpp @@ -27,30 +27,32 @@ #include "adb_io.h" #include "fuse_sideload.h" -int read_block_adb(const adb_data& ad, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { - if (!WriteFdFmt(ad.sfd, "%08u", block)) { +bool FuseAdbDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, + uint32_t start_block) const { + if (!WriteFdFmt(fd_, "%08u", start_block)) { fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); - return -EIO; + return false; } - if (!ReadFdExactly(ad.sfd, buffer, fetch_size)) { + if (!ReadFdExactly(fd_, buffer, fetch_size)) { fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); - return -EIO; + return false; } - return 0; + return true; } -int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { - adb_data ad; - ad.sfd = sfd; - ad.file_size = file_size; - ad.block_size = block_size; +void FuseAdbDataProvider::Close() { + 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(read_block_adb, ad, std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3); - vtab.close = [&ad]() { WriteFdExactly(ad.sfd, "DONEDONE"); }; + 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); } diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h index 36d86d539..e93aa0468 100644 --- a/minadbd/fuse_adb_provider.h +++ b/minadbd/fuse_adb_provider.h @@ -19,14 +19,22 @@ #include -struct adb_data { - int sfd; // file descriptor for the adb channel +#include "android-base/unique_fd.h" - uint64_t file_size; - uint32_t block_size; +#include "fuse_provider.h" + +// This class reads data from adb server. +class FuseAdbDataProvider : public FuseDataProvider { + public: + FuseAdbDataProvider(android::base::unique_fd&& fd, uint64_t file_size, uint32_t block_size) + : FuseDataProvider(std::move(fd), file_size, block_size) {} + + bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, + uint32_t start_block) const override; + + void Close() override; }; -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(android::base::unique_fd&& sfd, uint64_t file_size, uint32_t block_size); #endif diff --git a/minadbd/fuse_adb_provider_test.cpp b/minadbd/fuse_adb_provider_test.cpp index 00250e505..0b097129e 100644 --- a/minadbd/fuse_adb_provider_test.cpp +++ b/minadbd/fuse_adb_provider_test.cpp @@ -21,19 +21,19 @@ #include +#include #include #include "adb_io.h" #include "fuse_adb_provider.h" TEST(fuse_adb_provider, read_block_adb) { - adb_data data = {}; - int sockets[2]; + android::base::unique_fd device_socket; + android::base::unique_fd host_socket; - ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)); - data.sfd = sockets[0]; + ASSERT_TRUE(android::base::Socketpair(AF_UNIX, SOCK_STREAM, 0, &device_socket, &host_socket)); + FuseAdbDataProvider data(std::move(device_socket), 0, 0); - int host_socket = sockets[1]; fcntl(host_socket, F_SETFL, O_NONBLOCK); const char expected_data[] = "foobar"; @@ -46,8 +46,8 @@ TEST(fuse_adb_provider, read_block_adb) { uint32_t block = 1234U; const char expected_block[] = "00001234"; - ASSERT_EQ(0, read_block_adb(data, block, reinterpret_cast(block_data), - sizeof(expected_data) - 1)); + ASSERT_TRUE(data.ReadBlockAlignedData(reinterpret_cast(block_data), + sizeof(expected_data) - 1, block)); // Check that read_block_adb requested the right block. char block_req[sizeof(expected_block)] = {}; @@ -65,26 +65,21 @@ TEST(fuse_adb_provider, read_block_adb) { errno = 0; ASSERT_EQ(-1, read(host_socket, &tmp, 1)); ASSERT_EQ(EWOULDBLOCK, errno); - - close(sockets[0]); - close(sockets[1]); } TEST(fuse_adb_provider, read_block_adb_fail_write) { - adb_data data = {}; - int sockets[2]; + android::base::unique_fd device_socket; + android::base::unique_fd host_socket; - ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)); - data.sfd = sockets[0]; + ASSERT_TRUE(android::base::Socketpair(AF_UNIX, SOCK_STREAM, 0, &device_socket, &host_socket)); + FuseAdbDataProvider data(std::move(device_socket), 0, 0); - ASSERT_EQ(0, close(sockets[1])); + host_socket.reset(); // write(2) raises SIGPIPE since the reading end has been closed. Ignore the signal to avoid // failing the test. signal(SIGPIPE, SIG_IGN); char buf[1]; - ASSERT_EQ(-EIO, read_block_adb(data, 0, reinterpret_cast(buf), 1)); - - close(sockets[0]); + ASSERT_FALSE(data.ReadBlockAlignedData(reinterpret_cast(buf), 1, 0)); } diff --git a/minadbd/minadbd_services.cpp b/minadbd/minadbd_services.cpp index 9309ed749..3e1128546 100644 --- a/minadbd/minadbd_services.cpp +++ b/minadbd/minadbd_services.cpp @@ -44,7 +44,7 @@ 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); - int result = run_adb_fuse(sfd, file_size, block_size); + int result = run_adb_fuse(std::move(sfd), file_size, block_size); printf("sideload_host finished\n"); exit(result == 0 ? 0 : 1); -- cgit v1.2.3