blob: df96312723e1c3e9415cc5cf61ace9782db858b6 [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
17#include <stdlib.h>
18#include <stdio.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
Doug Zongker945fc682014-07-10 10:50:39 -070020#include <errno.h>
Doug Zongker945fc682014-07-10 10:50:39 -070021#include <sys/mount.h>
22#include <sys/stat.h>
23#include <unistd.h>
24#include <fcntl.h>
25
26#include "fuse_sideload.h"
27
28struct file_data {
29 int fd; // the underlying sdcard file
30
31 uint64_t file_size;
32 uint32_t block_size;
33};
34
35static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
Tao Bao0d4e0022015-07-19 08:40:37 -070036 file_data* fd = reinterpret_cast<file_data*>(cookie);
Doug Zongker945fc682014-07-10 10:50:39 -070037
Elliott Hughes7bad7c42015-04-28 17:24:24 -070038 off64_t offset = ((off64_t) block) * fd->block_size;
39 if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) {
40 fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
Doug Zongker945fc682014-07-10 10:50:39 -070041 return -EIO;
42 }
43
44 while (fetch_size > 0) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070045 ssize_t r = TEMP_FAILURE_RETRY(read(fd->fd, buffer, fetch_size));
46 if (r == -1) {
47 fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
48 return -EIO;
Doug Zongker945fc682014-07-10 10:50:39 -070049 }
50 fetch_size -= r;
51 buffer += r;
52 }
53
54 return 0;
55}
56
57static void close_file(void* cookie) {
Tao Bao0d4e0022015-07-19 08:40:37 -070058 file_data* fd = reinterpret_cast<file_data*>(cookie);
Doug Zongker945fc682014-07-10 10:50:39 -070059 close(fd->fd);
60}
61
Tao Baocdcf28f2016-01-13 15:05:20 -080062bool start_sdcard_fuse(const char* path) {
Doug Zongker945fc682014-07-10 10:50:39 -070063 struct stat sb;
Tao Baocdcf28f2016-01-13 15:05:20 -080064 if (stat(path, &sb) == -1) {
65 fprintf(stderr, "failed to stat %s: %s\n", path, strerror(errno));
66 return false;
Doug Zongker945fc682014-07-10 10:50:39 -070067 }
68
Tao Baocdcf28f2016-01-13 15:05:20 -080069 file_data fd;
70 fd.fd = open(path, O_RDONLY);
71 if (fd.fd == -1) {
72 fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
73 return false;
Doug Zongker945fc682014-07-10 10:50:39 -070074 }
75 fd.file_size = sb.st_size;
76 fd.block_size = 65536;
77
Tao Baocdcf28f2016-01-13 15:05:20 -080078 provider_vtab vtab;
Doug Zongker945fc682014-07-10 10:50:39 -070079 vtab.read_block = read_block_file;
80 vtab.close = close_file;
81
Doug Zongker945fc682014-07-10 10:50:39 -070082 // The installation process expects to find the sdcard unmounted.
83 // Unmount it with MNT_DETACH so that our open file continues to
84 // work but new references see it as unmounted.
85 umount2("/sdcard", MNT_DETACH);
86
Tao Baocdcf28f2016-01-13 15:05:20 -080087 return run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size) == 0;
Doug Zongker945fc682014-07-10 10:50:39 -070088}