blob: ca8c914f9a450e6a0edcacd7bbd4cdc1d3b87940 [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>
21#include <pthread.h>
22#include <sys/mount.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <fcntl.h>
26
27#include "fuse_sideload.h"
28
29struct file_data {
30 int fd; // the underlying sdcard file
31
32 uint64_t file_size;
33 uint32_t block_size;
34};
35
36static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
37 struct file_data* fd = (struct file_data*)cookie;
38
39 if (lseek(fd->fd, block * fd->block_size, SEEK_SET) < 0) {
40 printf("seek on sdcard failed: %s\n", strerror(errno));
41 return -EIO;
42 }
43
44 while (fetch_size > 0) {
45 ssize_t r = read(fd->fd, buffer, fetch_size);
46 if (r < 0) {
47 if (r != -EINTR) {
48 printf("read on sdcard failed: %s\n", strerror(errno));
49 return -EIO;
50 }
51 r = 0;
52 }
53 fetch_size -= r;
54 buffer += r;
55 }
56
57 return 0;
58}
59
60static void close_file(void* cookie) {
61 struct file_data* fd = (struct file_data*)cookie;
62 close(fd->fd);
63}
64
65struct token {
66 pthread_t th;
67 const char* path;
68 int result;
69};
70
71static void* run_sdcard_fuse(void* cookie) {
72 struct token* t = (struct token*)cookie;
73
74 struct stat sb;
75 if (stat(t->path, &sb) < 0) {
76 fprintf(stderr, "failed to stat %s: %s\n", t->path, strerror(errno));
77 t->result = -1;
78 return NULL;
79 }
80
81 struct file_data fd;
82 struct provider_vtab vtab;
83
84 fd.fd = open(t->path, O_RDONLY);
85 if (fd.fd < 0) {
86 fprintf(stderr, "failed to open %s: %s\n", t->path, strerror(errno));
87 t->result = -1;
88 return NULL;
89 }
90 fd.file_size = sb.st_size;
91 fd.block_size = 65536;
92
93 vtab.read_block = read_block_file;
94 vtab.close = close_file;
95
96 t->result = run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size);
97 return NULL;
98}
99
100// How long (in seconds) we wait for the fuse-provided package file to
101// appear, before timing out.
102#define SDCARD_INSTALL_TIMEOUT 10
103
104void* start_sdcard_fuse(const char* path) {
105 struct token* t = malloc(sizeof(struct token));
106
107 t->path = path;
108 pthread_create(&(t->th), NULL, run_sdcard_fuse, t);
109
110 struct stat st;
111 int i;
112 for (i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
113 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
114 if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
115 sleep(1);
116 continue;
117 } else {
118 return NULL;
119 }
120 }
121 }
122
123 // The installation process expects to find the sdcard unmounted.
124 // Unmount it with MNT_DETACH so that our open file continues to
125 // work but new references see it as unmounted.
126 umount2("/sdcard", MNT_DETACH);
127
128 return t;
129}
130
131void finish_sdcard_fuse(void* cookie) {
132 if (cookie == NULL) return;
133 struct token* t = (struct token*)cookie;
134
135 // Calling stat() on this magic filename signals the fuse
136 // filesystem to shut down.
137 struct stat st;
138 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
139
140 pthread_join(t->th, NULL);
141 free(t);
142}