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 #ifndef ANDROID_LIBAPPFUSE_FUSEAPPLOOP_H_
18 #define ANDROID_LIBAPPFUSE_FUSEAPPLOOP_H_
19 
20 #include <memory>
21 #include <mutex>
22 
23 #include <android-base/unique_fd.h>
24 
25 #include "libappfuse/FuseBuffer.h"
26 
27 namespace android {
28 namespace fuse {
29 
30 class EpollController;
31 
32 class FuseAppLoopCallback {
33  public:
34    virtual void OnLookup(uint64_t unique, uint64_t inode) = 0;
35    virtual void OnGetAttr(uint64_t unique, uint64_t inode) = 0;
36    virtual void OnFsync(uint64_t unique, uint64_t inode) = 0;
37    virtual void OnWrite(uint64_t unique, uint64_t inode, uint64_t offset, uint32_t size,
38                         const void* data) = 0;
39    virtual void OnRead(uint64_t unique, uint64_t inode, uint64_t offset, uint32_t size) = 0;
40    virtual void OnOpen(uint64_t unique, uint64_t inode) = 0;
41    virtual void OnRelease(uint64_t unique, uint64_t inode) = 0;
42    virtual ~FuseAppLoopCallback();
43 };
44 
45 class FuseAppLoop final {
46   public:
47     FuseAppLoop(base::unique_fd&& fd);
48 
49     void Start(FuseAppLoopCallback* callback);
50     void Break();
51 
52     bool ReplySimple(uint64_t unique, int32_t result);
53     bool ReplyLookup(uint64_t unique, uint64_t inode, int64_t size);
54     bool ReplyGetAttr(uint64_t unique, uint64_t inode, int64_t size, int mode);
55     bool ReplyOpen(uint64_t unique, uint64_t fh);
56     bool ReplyWrite(uint64_t unique, uint32_t size);
57     bool ReplyRead(uint64_t unique, uint32_t size, const void* data);
58 
59   private:
60     base::unique_fd fd_;
61     base::unique_fd break_fd_;
62 
63     // Lock for multi-threading.
64     std::mutex mutex_;
65 };
66 
67 bool StartFuseAppLoop(int fd, FuseAppLoopCallback* callback);
68 
69 }  // namespace fuse
70 }  // namespace android
71 
72 #endif  // ANDROID_LIBAPPFUSE_FUSEAPPLOOP_H_
73