| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 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/FuseBridgeLoop.h" | 
 | 18 |  | 
 | 19 | #include <android-base/logging.h> | 
 | 20 | #include <android-base/unique_fd.h> | 
 | 21 |  | 
 | 22 | namespace android { | 
 | 23 |  | 
 | 24 | bool FuseBridgeLoop::Start( | 
 | 25 |     int raw_dev_fd, int raw_proxy_fd, FuseBridgeLoop::Callback* callback) { | 
 | 26 |   base::unique_fd dev_fd(raw_dev_fd); | 
 | 27 |   base::unique_fd proxy_fd(raw_proxy_fd); | 
 | 28 |  | 
 | 29 |   LOG(DEBUG) << "Start fuse loop."; | 
 | 30 |   while (true) { | 
 | 31 |     if (!buffer_.request.Read(dev_fd)) { | 
 | 32 |       return false; | 
 | 33 |     } | 
 | 34 |  | 
 | 35 |     const uint32_t opcode = buffer_.request.header.opcode; | 
 | 36 |     LOG(VERBOSE) << "Read a fuse packet, opcode=" << opcode; | 
 | 37 |     switch (opcode) { | 
 | 38 |       case FUSE_FORGET: | 
 | 39 |         // Do not reply to FUSE_FORGET. | 
 | 40 |         continue; | 
 | 41 |  | 
 | 42 |       case FUSE_LOOKUP: | 
 | 43 |       case FUSE_GETATTR: | 
 | 44 |       case FUSE_OPEN: | 
 | 45 |       case FUSE_READ: | 
 | 46 |       case FUSE_WRITE: | 
 | 47 |       case FUSE_RELEASE: | 
 | 48 |       case FUSE_FLUSH: | 
 | 49 |         if (!buffer_.request.Write(proxy_fd)) { | 
 | 50 |           LOG(ERROR) << "Failed to write a request to the proxy."; | 
 | 51 |           return false; | 
 | 52 |         } | 
 | 53 |         if (!buffer_.response.Read(proxy_fd)) { | 
 | 54 |           LOG(ERROR) << "Failed to read a response from the proxy."; | 
 | 55 |           return false; | 
 | 56 |         } | 
 | 57 |         break; | 
 | 58 |  | 
 | 59 |       case FUSE_INIT: | 
 | 60 |         buffer_.HandleInit(); | 
 | 61 |         break; | 
 | 62 |  | 
 | 63 |       default: | 
 | 64 |         buffer_.HandleNotImpl(); | 
 | 65 |         break; | 
 | 66 |     } | 
 | 67 |  | 
 | 68 |     if (!buffer_.response.Write(dev_fd)) { | 
 | 69 |       LOG(ERROR) << "Failed to write a response to the device."; | 
 | 70 |       return false; | 
 | 71 |     } | 
 | 72 |  | 
 | 73 |     if (opcode == FUSE_INIT) { | 
 | 74 |       callback->OnMount(); | 
 | 75 |     } | 
 | 76 |   } | 
 | 77 | } | 
 | 78 |  | 
 | 79 | }  // namespace android |