diff options
author | Tianjie Xu <xunchang@google.com> | 2019-03-19 22:00:22 +0100 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-03-19 22:00:22 +0100 |
commit | 61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014 (patch) | |
tree | 9887b8f3b9f06abe942e4c2bd3669150fc17b3dd /minadbd | |
parent | Merge "Skip calling verify_package_compatibility when resuming." am: 98beb6414b am: c4148b529a (diff) | |
parent | Merge "Create a FuseDataProvider base class" am: eeea86f5fd (diff) | |
download | android_bootable_recovery-61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014.tar android_bootable_recovery-61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014.tar.gz android_bootable_recovery-61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014.tar.bz2 android_bootable_recovery-61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014.tar.lz android_bootable_recovery-61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014.tar.xz android_bootable_recovery-61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014.tar.zst android_bootable_recovery-61307a2e3c9d60d6b94130ad2fbbf64ad5ec7014.zip |
Diffstat (limited to 'minadbd')
-rw-r--r-- | minadbd/Android.bp | 1 | ||||
-rw-r--r-- | minadbd/fuse_adb_provider.cpp | 30 | ||||
-rw-r--r-- | minadbd/fuse_adb_provider.h | 20 | ||||
-rw-r--r-- | minadbd/fuse_adb_provider_test.cpp | 31 | ||||
-rw-r--r-- | minadbd/minadbd_services.cpp | 2 |
5 files changed, 45 insertions, 39 deletions
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 <stdint.h> -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 <string> +#include <android-base/unique_fd.h> #include <gtest/gtest.h> #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<uint8_t*>(block_data), - sizeof(expected_data) - 1)); + ASSERT_TRUE(data.ReadBlockAlignedData(reinterpret_cast<uint8_t*>(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<uint8_t*>(buf), 1)); - - close(sockets[0]); + ASSERT_FALSE(data.ReadBlockAlignedData(reinterpret_cast<uint8_t*>(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); |