1 /*
2  * Copyright (C) 2019 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 MEDIAPROVIDER_JNI_FUSEDAEMON_H_
18 #define MEDIAPROVIDER_JNI_FUSEDAEMON_H_
19 
20 #include <android-base/unique_fd.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include "MediaProviderWrapper.h"
28 #include "jni.h"
29 #include "node-inl.h"
30 
31 struct fuse;
32 namespace mediaprovider {
33 namespace fuse {
34 class FuseDaemon final {
35   public:
36     FuseDaemon(JNIEnv* env, jobject mediaProvider);
37 
38     ~FuseDaemon() = default;
39 
40     /**
41      * Start the FUSE daemon loop that will handle filesystem calls.
42      */
43     void Start(android::base::unique_fd fd, const std::string& path, const bool uncached_mode,
44                const std::vector<std::string>& supported_transcoding_relative_paths,
45                const std::vector<std::string>& supported_uncached_relative_paths);
46 
47     /**
48      * Checks if the FUSE daemon is started.
49      */
50     bool IsStarted() const;
51 
52     /**
53      * Check if file should be opened with FUSE
54      */
55     bool ShouldOpenWithFuse(int fd, bool for_read, const std::string& path);
56 
57     /**
58      * Check if the FUSE daemon uses FUSE passthrough
59      */
60     bool UsesFusePassthrough() const;
61 
62     /**
63      * Invalidate FUSE VFS dentry cache entry for path
64      */
65     void InvalidateFuseDentryCache(const std::string& path);
66 
67     /**
68      * Checks if the given uid has access to the given fd with or without redaction.
69      */
70     std::unique_ptr<FdAccessResult> CheckFdAccess(int fd, uid_t uid) const;
71 
72     /**
73      * Initialize device id for the FUSE daemon with the FUSE device id of the given path.
74      */
75     void InitializeDeviceId(const std::string& path);
76 
77     /**
78      * Setup leveldb instances based on the volume.
79      */
80     void SetupLevelDbInstances();
81 
82     /**
83      * Setup leveldb instances for public volume.
84      */
85     void SetupPublicVolumeLevelDbInstance(const std::string& volume_name);
86 
87     /**
88      * Creates a leveldb instance and sets up a connection.
89      */
90     void SetupLevelDbConnection(const std::string& instance_name);
91 
92     /**
93      * Deletes entry for given key from leveldb.
94      */
95     void DeleteFromLevelDb(const std::string& key);
96 
97     /**
98      * Inserts in leveldb instance of provided volume.
99      */
100     void InsertInLevelDb(const std::string& volume_name, const std::string& key,
101                          const std::string& value);
102 
103     /**
104      * Reads file paths for given volume from leveldb for given range.
105      */
106     std::vector<std::string> ReadFilePathsFromLevelDb(const std::string& volume_name,
107                                                       const std::string& lastReadValue, int limit);
108 
109     /**
110      * Reads backed up data from leveldb.
111      */
112     std::string ReadBackedUpDataFromLevelDb(const std::string& filePath);
113 
114     /**
115      * Reads value for given key, returns empty string if not found.
116      */
117     std::string ReadOwnership(const std::string& key);
118 
119     /**
120      * Creates owner id to owner package identifier and vice versa relation in leveldb.
121      */
122     void CreateOwnerIdRelation(const std::string& ownerId,
123                                const std::string& ownerPackageIdentifier);
124 
125     /**
126      * Removes owner id to owner package identifier and vice versa relation in leveldb.
127      */
128     void RemoveOwnerIdRelation(const std::string& ownerId,
129                                const std::string& ownerPackageIdentifier);
130 
131     /**
132      * Reads owner relationships from leveldb.
133      */
134     std::map<std::string, std::string> GetOwnerRelationship();
135 
136     /**
137      * Returns true if level db setup exists for given instance.
138      */
139     bool CheckLevelDbConnection(const std::string& instance_name);
140 
141   private:
142     FuseDaemon(const FuseDaemon&) = delete;
143     void operator=(const FuseDaemon&) = delete;
144     MediaProviderWrapper mp;
145     std::atomic_bool active;
146     struct ::fuse* fuse;
147 };
148 
149 }  // namespace fuse
150 }  // namespace mediaprovider
151 
152 #endif  // MEDIAPROVIDER_JNI_FUSEDAEMON_H_
153