aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao2017-05-01 17:57:38 -0500
committerTao Bao2017-11-03 20:30:41 -0500
commit91a7aa4577feb5f80038a30d15765e8b79845cf1 (patch)
tree9ecb91bec8c5ad34382a5b3d24f36274e9be7aaa /fuse_sdcard_provider.cpp
parent0da645fefa55d7cb067b76820c620939d02f106a (diff)
downloadplatform-bootable-recovery-91a7aa4577feb5f80038a30d15765e8b79845cf1.tar.gz
platform-bootable-recovery-91a7aa4577feb5f80038a30d15765e8b79845cf1.tar.xz
platform-bootable-recovery-91a7aa4577feb5f80038a30d15765e8b79845cf1.zip
Clean up fuse_sideload and add a testcase.
This CL mainly changes: a) moving the interface in struct provider_vtab to std::function; b) code cleanup, such as moving the declaration closer to the uses, using explicit type conversion. Test: recovery_component_test Test: minadbd_test Test: Sideload a package on marlin. Change-Id: Id0e3c70f1ada54a4cd985b54c84438c23ed4687e
Diffstat (limited to 'fuse_sdcard_provider.cpp')
-rw-r--r--fuse_sdcard_provider.cpp88
1 files changed, 43 insertions, 45 deletions
diff --git a/fuse_sdcard_provider.cpp b/fuse_sdcard_provider.cpp
index b0ecf96b..46bdf177 100644
--- a/fuse_sdcard_provider.cpp
+++ b/fuse_sdcard_provider.cpp
@@ -14,72 +14,70 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#include <stdlib.h> 17#include "fuse_sdcard_provider.h"
18
19#include <errno.h>
20#include <fcntl.h>
18#include <stdio.h> 21#include <stdio.h>
22#include <stdlib.h>
19#include <string.h> 23#include <string.h>
20#include <errno.h>
21#include <sys/mount.h> 24#include <sys/mount.h>
22#include <sys/stat.h> 25#include <sys/stat.h>
23#include <unistd.h> 26#include <unistd.h>
24#include <fcntl.h> 27
28#include <functional>
25 29
26#include <android-base/file.h> 30#include <android-base/file.h>
27 31
28#include "fuse_sideload.h" 32#include "fuse_sideload.h"
29 33
30struct file_data { 34struct file_data {
31 int fd; // the underlying sdcard file 35 int fd; // the underlying sdcard file
32 36
33 uint64_t file_size; 37 uint64_t file_size;
34 uint32_t block_size; 38 uint32_t block_size;
35}; 39};
36 40
37static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { 41static int read_block_file(const file_data& fd, uint32_t block, uint8_t* buffer,
38 file_data* fd = reinterpret_cast<file_data*>(cookie); 42 uint32_t fetch_size) {
39 43 off64_t offset = static_cast<off64_t>(block) * fd.block_size;
40 off64_t offset = ((off64_t) block) * fd->block_size; 44 if (TEMP_FAILURE_RETRY(lseek64(fd.fd, offset, SEEK_SET)) == -1) {
41 if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) { 45 fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
42 fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno)); 46 return -EIO;
43 return -EIO; 47 }
44 }
45 48
46 if (!android::base::ReadFully(fd->fd, buffer, fetch_size)) { 49 if (!android::base::ReadFully(fd.fd, buffer, fetch_size)) {
47 fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno)); 50 fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
48 return -EIO; 51 return -EIO;
49 } 52 }
50
51 return 0;
52}
53 53
54static void close_file(void* cookie) { 54 return 0;
55 file_data* fd = reinterpret_cast<file_data*>(cookie);
56 close(fd->fd);
57} 55}
58 56
59bool start_sdcard_fuse(const char* path) { 57bool start_sdcard_fuse(const char* path) {
60 struct stat sb; 58 struct stat sb;
61 if (stat(path, &sb) == -1) { 59 if (stat(path, &sb) == -1) {
62 fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno)); 60 fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno));
63 return false; 61 return false;
64 } 62 }
65 63
66 file_data fd; 64 file_data fd;
67 fd.fd = open(path, O_RDONLY); 65 fd.fd = open(path, O_RDONLY);
68 if (fd.fd == -1) { 66 if (fd.fd == -1) {
69 fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno)); 67 fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
70 return false; 68 return false;
71 } 69 }
72 fd.file_size = sb.st_size; 70 fd.file_size = sb.st_size;
73 fd.block_size = 65536; 71 fd.block_size = 65536;
74 72
75 provider_vtab vtab; 73 provider_vtab vtab;
76 vtab.read_block = read_block_file; 74 vtab.read_block = std::bind(&read_block_file, fd, std::placeholders::_1, std::placeholders::_2,
77 vtab.close = close_file; 75 std::placeholders::_3);
76 vtab.close = [&fd]() { close(fd.fd); };
78 77
79 // The installation process expects to find the sdcard unmounted. 78 // The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so
80 // Unmount it with MNT_DETACH so that our open file continues to 79 // that our open file continues to work but new references see it as unmounted.
81 // work but new references see it as unmounted. 80 umount2("/sdcard", MNT_DETACH);
82 umount2("/sdcard", MNT_DETACH);
83 81
84 return run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size) == 0; 82 return run_fuse_sideload(vtab, fd.file_size, fd.block_size) == 0;
85} 83}