forked from TeamWin/Team-Win-Recovery-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_sdcard_provider.c
140 lines (115 loc) · 3.71 KB
/
fuse_sdcard_provider.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "fuse_sideload.h"
struct file_data {
int fd; // the underlying sdcard file
uint64_t file_size;
uint32_t block_size;
};
static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
struct file_data* fd = (struct file_data*)cookie;
off64_t offset = ((off64_t) block) * fd->block_size;
if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) {
fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
return -EIO;
}
while (fetch_size > 0) {
ssize_t r = TEMP_FAILURE_RETRY(read(fd->fd, buffer, fetch_size));
if (r == -1) {
fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
return -EIO;
}
fetch_size -= r;
buffer += r;
}
return 0;
}
static void close_file(void* cookie) {
struct file_data* fd = (struct file_data*)cookie;
close(fd->fd);
}
struct token {
pthread_t th;
const char* path;
int result;
};
static void* run_sdcard_fuse(void* cookie) {
struct token* t = (struct token*)cookie;
struct stat sb;
if (stat(t->path, &sb) < 0) {
fprintf(stderr, "failed to stat %s: %s\n", t->path, strerror(errno));
t->result = -1;
return NULL;
}
struct file_data fd;
struct provider_vtab vtab;
fd.fd = open(t->path, O_RDONLY);
if (fd.fd < 0) {
fprintf(stderr, "failed to open %s: %s\n", t->path, strerror(errno));
t->result = -1;
return NULL;
}
fd.file_size = sb.st_size;
fd.block_size = 65536;
vtab.read_block = read_block_file;
vtab.close = close_file;
t->result = run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size);
return NULL;
}
// How long (in seconds) we wait for the fuse-provided package file to
// appear, before timing out.
#define SDCARD_INSTALL_TIMEOUT 10
void* start_sdcard_fuse(const char* path) {
struct token* t = malloc(sizeof(struct token));
t->path = path;
pthread_create(&(t->th), NULL, run_sdcard_fuse, t);
struct stat st;
int i;
for (i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
sleep(1);
continue;
} else {
return NULL;
}
}
}
// The installation process expects to find the sdcard unmounted.
// Unmount it with MNT_DETACH so that our open file continues to
// work but new references see it as unmounted.
umount2("/sdcard", MNT_DETACH);
return t;
}
void finish_sdcard_fuse(void* cookie) {
if (cookie == NULL) return;
struct token* t = (struct token*)cookie;
// Calling stat() on this magic filename signals the fuse
// filesystem to shut down.
struct stat st;
stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
pthread_join(t->th, NULL);
free(t);
}