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 | |
| 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 19 | #include <string.h> |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 21 | #include <sys/mount.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | |
Tianjie Xu | 71e182b | 2016-08-31 18:06:33 -0700 | [diff] [blame] | 26 | #include <android-base/file.h> |
| 27 | |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 28 | #include "fuse_sideload.h" |
| 29 | |
| 30 | struct file_data { |
| 31 | int fd; // the underlying sdcard file |
| 32 | |
| 33 | uint64_t file_size; |
| 34 | uint32_t block_size; |
| 35 | }; |
| 36 | |
| 37 | static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { |
Tao Bao | 0d4e002 | 2015-07-19 08:40:37 -0700 | [diff] [blame] | 38 | file_data* fd = reinterpret_cast<file_data*>(cookie); |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 40 | off64_t offset = ((off64_t) block) * fd->block_size; |
| 41 | if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) { |
| 42 | fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno)); |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 43 | return -EIO; |
| 44 | } |
| 45 | |
Tianjie Xu | 71e182b | 2016-08-31 18:06:33 -0700 | [diff] [blame] | 46 | if (!android::base::ReadFully(fd->fd, buffer, fetch_size)) { |
| 47 | fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno)); |
| 48 | return -EIO; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static void close_file(void* cookie) { |
Tao Bao | 0d4e002 | 2015-07-19 08:40:37 -0700 | [diff] [blame] | 55 | file_data* fd = reinterpret_cast<file_data*>(cookie); |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 56 | close(fd->fd); |
| 57 | } |
| 58 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 59 | bool start_sdcard_fuse(const char* path) { |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 60 | struct stat sb; |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 61 | if (stat(path, &sb) == -1) { |
| 62 | fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno)); |
| 63 | return false; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 66 | file_data fd; |
| 67 | fd.fd = open(path, O_RDONLY); |
| 68 | if (fd.fd == -1) { |
| 69 | fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno)); |
| 70 | return false; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 71 | } |
| 72 | fd.file_size = sb.st_size; |
| 73 | fd.block_size = 65536; |
| 74 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 75 | provider_vtab vtab; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 76 | vtab.read_block = read_block_file; |
| 77 | vtab.close = close_file; |
| 78 | |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 79 | // The installation process expects to find the sdcard unmounted. |
| 80 | // Unmount it with MNT_DETACH so that our open file continues to |
| 81 | // work but new references see it as unmounted. |
| 82 | umount2("/sdcard", MNT_DETACH); |
| 83 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 84 | return run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size) == 0; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 85 | } |