blob: 46bdf177488f7f6a3c237518234f1e8089040ab7 [file] [log] [blame]
Doug Zongker945fc682014-07-10 10:50:39 -07001/*
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 Bao91a7aa42017-05-01 15:57:38 -070017#include "fuse_sdcard_provider.h"
18
Doug Zongker945fc682014-07-10 10:50:39 -070019#include <errno.h>
Tao Bao91a7aa42017-05-01 15:57:38 -070020#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Doug Zongker945fc682014-07-10 10:50:39 -070024#include <sys/mount.h>
25#include <sys/stat.h>
26#include <unistd.h>
Tao Bao91a7aa42017-05-01 15:57:38 -070027
28#include <functional>
Doug Zongker945fc682014-07-10 10:50:39 -070029
Tianjie Xu71e182b2016-08-31 18:06:33 -070030#include <android-base/file.h>
31
Doug Zongker945fc682014-07-10 10:50:39 -070032#include "fuse_sideload.h"
33
34struct file_data {
Tao Bao91a7aa42017-05-01 15:57:38 -070035 int fd; // the underlying sdcard file
Doug Zongker945fc682014-07-10 10:50:39 -070036
Tao Bao91a7aa42017-05-01 15:57:38 -070037 uint64_t file_size;
38 uint32_t block_size;
Doug Zongker945fc682014-07-10 10:50:39 -070039};
40
Tao Bao91a7aa42017-05-01 15:57:38 -070041static 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 Zongker945fc682014-07-10 10:50:39 -070048
Tao Bao91a7aa42017-05-01 15:57:38 -070049 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 Zongker945fc682014-07-10 10:50:39 -070053
Tao Bao91a7aa42017-05-01 15:57:38 -070054 return 0;
Doug Zongker945fc682014-07-10 10:50:39 -070055}
56
Tao Baocdcf28f2016-01-13 15:05:20 -080057bool start_sdcard_fuse(const char* path) {
Tao Bao91a7aa42017-05-01 15:57:38 -070058 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 Zongker945fc682014-07-10 10:50:39 -070063
Tao Bao91a7aa42017-05-01 15:57:38 -070064 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 Zongker945fc682014-07-10 10:50:39 -070072
Tao Bao91a7aa42017-05-01 15:57:38 -070073 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 Zongker945fc682014-07-10 10:50:39 -070077
Tao Bao91a7aa42017-05-01 15:57:38 -070078 // 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 Zongker945fc682014-07-10 10:50:39 -070081
Tao Bao91a7aa42017-05-01 15:57:38 -070082 return run_fuse_sideload(vtab, fd.file_size, fd.block_size) == 0;
Doug Zongker945fc682014-07-10 10:50:39 -070083}