summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaichi Hirono2016-11-07 19:17:51 -0600
committerDaichi Hirono2016-11-14 18:47:31 -0600
commita0aecda12b9a76aa15a8c5175e15538574a05af7 (patch)
treeac1e8eda1f03a8922445ae13d6d7a4173158c4ec /libappfuse/FuseAppLoop.cc
parentb5ce6f02dd11b42c03884dd9531c6c8a80bcecda (diff)
downloadplatform-system-core-a0aecda12b9a76aa15a8c5175e15538574a05af7.tar.gz
platform-system-core-a0aecda12b9a76aa15a8c5175e15538574a05af7.tar.xz
platform-system-core-a0aecda12b9a76aa15a8c5175e15538574a05af7.zip
Add FuseAppLoop to libappfuse.
The class is used at the app side (StorageManager) to parse FUSE commands. Bug: 32260320 Test: libappfuse_test Change-Id: I1ae2904d3290a041f1efbf8fc10ba032eda5449c
Diffstat (limited to 'libappfuse/FuseAppLoop.cc')
-rw-r--r--libappfuse/FuseAppLoop.cc221
1 files changed, 221 insertions, 0 deletions
diff --git a/libappfuse/FuseAppLoop.cc b/libappfuse/FuseAppLoop.cc
new file mode 100644
index 000000000..a31880e41
--- /dev/null
+++ b/libappfuse/FuseAppLoop.cc
@@ -0,0 +1,221 @@
1/*
2 * Copyright (C) 2016 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 specic language governing permissions and
14 * limitations under the License.
15 */
16
17#include "libappfuse/FuseAppLoop.h"
18
19#include <sys/stat.h>
20
21#include <android-base/logging.h>
22#include <android-base/unique_fd.h>
23
24namespace android {
25namespace fuse {
26
27namespace {
28
29void HandleLookUp(FuseBuffer* buffer, FuseAppLoopCallback* callback) {
30 // AppFuse does not support directory structure now.
31 // It can lookup only files under the mount point.
32 if (buffer->request.header.nodeid != FUSE_ROOT_ID) {
33 LOG(ERROR) << "Nodeid is not FUSE_ROOT_ID.";
34 buffer->response.Reset(0, -ENOENT, buffer->request.header.unique);
35 return;
36 }
37
38 // Ensure that the filename ends with 0.
39 const size_t filename_length =
40 buffer->request.header.len - sizeof(fuse_in_header);
41 if (buffer->request.lookup_name[filename_length - 1] != 0) {
42 LOG(ERROR) << "File name does not end with 0.";
43 buffer->response.Reset(0, -ENOENT, buffer->request.header.unique);
44 return;
45 }
46
47 const uint64_t inode =
48 static_cast<uint64_t>(atol(buffer->request.lookup_name));
49 if (inode == 0 || inode == LONG_MAX) {
50 LOG(ERROR) << "Invalid filename";
51 buffer->response.Reset(0, -ENOENT, buffer->request.header.unique);
52 return;
53 }
54
55 const int64_t size = callback->OnGetSize(inode);
56 if (size < 0) {
57 buffer->response.Reset(0, size, buffer->request.header.unique);
58 return;
59 }
60
61 buffer->response.Reset(sizeof(fuse_entry_out), 0,
62 buffer->request.header.unique);
63 buffer->response.entry_out.nodeid = inode;
64 buffer->response.entry_out.attr_valid = 10;
65 buffer->response.entry_out.entry_valid = 10;
66 buffer->response.entry_out.attr.ino = inode;
67 buffer->response.entry_out.attr.mode = S_IFREG | 0777;
68 buffer->response.entry_out.attr.size = size;
69}
70
71void HandleGetAttr(FuseBuffer* buffer, FuseAppLoopCallback* callback) {
72 const uint64_t nodeid = buffer->request.header.nodeid;
73 int64_t size;
74 uint32_t mode;
75 if (nodeid == FUSE_ROOT_ID) {
76 size = 0;
77 mode = S_IFDIR | 0777;
78 } else {
79 size = callback->OnGetSize(buffer->request.header.nodeid);
80 if (size < 0) {
81 buffer->response.Reset(0, size, buffer->request.header.unique);
82 return;
83 }
84 mode = S_IFREG | 0777;
85 }
86
87 buffer->response.Reset(sizeof(fuse_attr_out), 0,
88 buffer->request.header.unique);
89 buffer->response.attr_out.attr_valid = 10;
90 buffer->response.attr_out.attr.ino = nodeid;
91 buffer->response.attr_out.attr.mode = mode;
92 buffer->response.attr_out.attr.size = size;
93}
94
95void HandleOpen(FuseBuffer* buffer, FuseAppLoopCallback* callback) {
96 const int32_t file_handle = callback->OnOpen(buffer->request.header.nodeid);
97 if (file_handle < 0) {
98 buffer->response.Reset(0, file_handle, buffer->request.header.unique);
99 return;
100 }
101 buffer->response.Reset(sizeof(fuse_open_out), kFuseSuccess,
102 buffer->request.header.unique);
103 buffer->response.open_out.fh = file_handle;
104}
105
106void HandleFsync(FuseBuffer* buffer, FuseAppLoopCallback* callback) {
107 buffer->response.Reset(0, callback->OnFsync(buffer->request.header.nodeid),
108 buffer->request.header.unique);
109}
110
111void HandleRelease(FuseBuffer* buffer, FuseAppLoopCallback* callback) {
112 buffer->response.Reset(0, callback->OnRelease(buffer->request.header.nodeid),
113 buffer->request.header.unique);
114}
115
116void HandleRead(FuseBuffer* buffer, FuseAppLoopCallback* callback) {
117 const uint64_t unique = buffer->request.header.unique;
118 const uint64_t nodeid = buffer->request.header.nodeid;
119 const uint64_t offset = buffer->request.read_in.offset;
120 const uint32_t size = buffer->request.read_in.size;
121
122 if (size > kFuseMaxRead) {
123 buffer->response.Reset(0, -EINVAL, buffer->request.header.unique);
124 return;
125 }
126
127 const int32_t read_size = callback->OnRead(nodeid, offset, size,
128 buffer->response.read_data);
129 if (read_size < 0) {
130 buffer->response.Reset(0, read_size, buffer->request.header.unique);
131 return;
132 }
133
134 buffer->response.ResetHeader(read_size, kFuseSuccess, unique);
135}
136
137void HandleWrite(FuseBuffer* buffer, FuseAppLoopCallback* callback) {
138 const uint64_t unique = buffer->request.header.unique;
139 const uint64_t nodeid = buffer->request.header.nodeid;
140 const uint64_t offset = buffer->request.write_in.offset;
141 const uint32_t size = buffer->request.write_in.size;
142
143 if (size > kFuseMaxWrite) {
144 buffer->response.Reset(0, -EINVAL, buffer->request.header.unique);
145 return;
146 }
147
148 const int32_t write_size = callback->OnWrite(nodeid, offset, size,
149 buffer->request.write_data);
150 if (write_size < 0) {
151 buffer->response.Reset(0, write_size, buffer->request.header.unique);
152 return;
153 }
154
155 buffer->response.Reset(sizeof(fuse_write_out), kFuseSuccess, unique);
156 buffer->response.write_out.size = write_size;
157}
158
159} // namespace
160
161bool StartFuseAppLoop(int raw_fd, FuseAppLoopCallback* callback) {
162 base::unique_fd fd(raw_fd);
163 FuseBuffer buffer;
164
165 LOG(DEBUG) << "Start fuse loop.";
166 while (callback->IsActive()) {
167 if (!buffer.request.Read(fd)) {
168 return false;
169 }
170
171 const uint32_t opcode = buffer.request.header.opcode;
172 LOG(VERBOSE) << "Read a fuse packet, opcode=" << opcode;
173 switch (opcode) {
174 case FUSE_FORGET:
175 // Do not reply to FUSE_FORGET.
176 continue;
177
178 case FUSE_LOOKUP:
179 HandleLookUp(&buffer, callback);
180 break;
181
182 case FUSE_GETATTR:
183 HandleGetAttr(&buffer, callback);
184 break;
185
186 case FUSE_OPEN:
187 HandleOpen(&buffer, callback);
188 break;
189
190 case FUSE_READ:
191 HandleRead(&buffer, callback);
192 break;
193
194 case FUSE_WRITE:
195 HandleWrite(&buffer, callback);
196 break;
197
198 case FUSE_RELEASE:
199 HandleRelease(&buffer, callback);
200 break;
201
202 case FUSE_FSYNC:
203 HandleFsync(&buffer, callback);
204 break;
205
206 default:
207 buffer.HandleNotImpl();
208 break;
209 }
210
211 if (!buffer.response.Write(fd)) {
212 LOG(ERROR) << "Failed to write a response to the device.";
213 return false;
214 }
215 }
216
217 return true;
218}
219
220} // namespace fuse
221} // namespace android