Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 17 | #include "fuse_sdcard_provider.h" |
| 18 | |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 24 | #include <sys/mount.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 27 | |
| 28 | #include <functional> |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 29 | |
Tianjie Xu | 71e182b | 2016-08-31 18:06:33 -0700 | [diff] [blame] | 30 | #include <android-base/file.h> |
| 31 | |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 32 | #include "fuse_sideload.h" |
| 33 | |
| 34 | struct file_data { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 35 | int fd; // the underlying sdcard file |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 36 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 37 | uint64_t file_size; |
| 38 | uint32_t block_size; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 41 | static int read_block_file(const file_data& fd, uint32_t block, uint8_t* buffer, |
| 42 | uint32_t fetch_size) { |
| 43 | off64_t offset = static_cast<off64_t>(block) * fd.block_size; |
| 44 | if (TEMP_FAILURE_RETRY(lseek64(fd.fd, offset, SEEK_SET)) == -1) { |
| 45 | fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno)); |
| 46 | return -EIO; |
| 47 | } |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 48 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 49 | if (!android::base::ReadFully(fd.fd, buffer, fetch_size)) { |
| 50 | fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno)); |
| 51 | return -EIO; |
| 52 | } |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 53 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 54 | return 0; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 57 | bool start_sdcard_fuse(const char* path) { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 58 | struct stat sb; |
| 59 | if (stat(path, &sb) == -1) { |
| 60 | fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno)); |
| 61 | return false; |
| 62 | } |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 63 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 64 | file_data fd; |
| 65 | fd.fd = open(path, O_RDONLY); |
| 66 | if (fd.fd == -1) { |
| 67 | fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno)); |
| 68 | return false; |
| 69 | } |
| 70 | fd.file_size = sb.st_size; |
| 71 | fd.block_size = 65536; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 72 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 73 | provider_vtab vtab; |
| 74 | vtab.read_block = std::bind(&read_block_file, fd, std::placeholders::_1, std::placeholders::_2, |
| 75 | std::placeholders::_3); |
| 76 | vtab.close = [&fd]() { close(fd.fd); }; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 77 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 78 | // The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so |
| 79 | // that our open file continues to work but new references see it as unmounted. |
| 80 | umount2("/sdcard", MNT_DETACH); |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 81 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 82 | return run_fuse_sideload(vtab, fd.file_size, fd.block_size) == 0; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 83 | } |