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 * Creates a leveldb instance and sets up a connection. 84 */ 85 void SetupLevelDbConnection(const std::string& instance_name); 86 87 /** 88 * Deletes entry for given key from leveldb. 89 */ 90 void DeleteFromLevelDb(const std::string& key); 91 92 /** 93 * Inserts in leveldb instance of volume derived from path. 94 */ 95 void InsertInLevelDb(const std::string& key, const std::string& value); 96 97 /** 98 * Reads file paths for given volume from leveldb for given range. 99 */ 100 std::vector<std::string> ReadFilePathsFromLevelDb(const std::string& volume_name, 101 const std::string& lastReadValue, int limit); 102 103 /** 104 * Reads backed up data from leveldb. 105 */ 106 std::string ReadBackedUpDataFromLevelDb(const std::string& filePath); 107 108 /** 109 * Reads value for given key, returns empty string if not found. 110 */ 111 std::string ReadOwnership(const std::string& key); 112 113 /** 114 * Creates owner id to owner package identifier and vice versa relation in leveldb. 115 */ 116 void CreateOwnerIdRelation(const std::string& ownerId, 117 const std::string& ownerPackageIdentifier); 118 119 /** 120 * Removes owner id to owner package identifier and vice versa relation in leveldb. 121 */ 122 void RemoveOwnerIdRelation(const std::string& ownerId, 123 const std::string& ownerPackageIdentifier); 124 125 /** 126 * Reads owner relationships from leveldb. 127 */ 128 std::map<std::string, std::string> GetOwnerRelationship(); 129 130 /** 131 * Returns true if level db setup exists for given instance. 132 */ 133 bool CheckLevelDbConnection(const std::string& instance_name); 134 135 private: 136 FuseDaemon(const FuseDaemon&) = delete; 137 void operator=(const FuseDaemon&) = delete; 138 MediaProviderWrapper mp; 139 std::atomic_bool active; 140 struct ::fuse* fuse; 141 }; 142 143 } // namespace fuse 144 } // namespace mediaprovider 145 146 #endif // MEDIAPROVIDER_JNI_FUSEDAEMON_H_ 147