1 /* 2 * Copyright (C) 2024 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 specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <string> 20 21 #include <android-base/result.h> 22 23 #include <aconfig_storage/aconfig_storage_read_api.hpp> 24 #include <aconfig_storage/aconfig_storage_write_api.hpp> 25 #include <aconfigd.pb.h> 26 27 namespace android { 28 namespace aconfigd { 29 30 /// In memory data structure for storage file locations for each container 31 struct StorageRecord { 32 int version; 33 std::string container; // container name 34 std::string package_map; // package.map on container 35 std::string flag_map; // flag.map on container 36 std::string flag_val; // flag.val on container 37 std::string persist_package_map; // persist package.map (backup copy for OTA) 38 std::string persist_flag_map; // persist flag.map (backup copy for OTA) 39 std::string persist_flag_val; // persist flag.val 40 std::string persist_flag_info; // persist flag.info 41 std::string local_overrides; // local flag overrides pb file 42 std::string boot_flag_val; // boot flag.val 43 std::string boot_flag_info; // boot flag.info 44 std::string digest; // digest of storage files 45 }; 46 47 /// Mapped files for a container 48 class StorageFiles { 49 public: 50 51 /// constructor for a new storage file set 52 StorageFiles(const std::string& container, 53 const std::string& package_map, 54 const std::string& flag_map, 55 const std::string& flag_val, 56 const std::string& root_dir, 57 base::Result<void>& status); 58 59 /// constructor for existing new storage file set 60 StorageFiles(const PersistStorageRecord& pb, 61 const std::string& root_dir); 62 63 /// destructor 64 ~StorageFiles() = default; 65 66 /// no copy 67 StorageFiles(const StorageFiles&) = delete; 68 StorageFiles& operator=(const StorageFiles&) = delete; 69 70 /// move constructor and assignment 71 StorageFiles(StorageFiles&& rhs); 72 StorageFiles& operator=(StorageFiles&& rhs); 73 74 /// get storage record GetStorageRecord()75 const StorageRecord& GetStorageRecord() { 76 return storage_record_; 77 } 78 79 /// has boot copy 80 bool HasBootCopy(); 81 82 /// return result for package and flag context 83 struct PackageFlagContext { 84 const std::string& package; 85 const std::string& flag; 86 bool package_exists; 87 bool flag_exists; 88 aconfig_storage::FlagValueType value_type; 89 uint32_t flag_index; 90 PackageFlagContextPackageFlagContext91 PackageFlagContext(const std::string& package_name, 92 const std::string& flag_name) 93 : package(package_name) 94 , flag(flag_name) 95 , package_exists(false) 96 , flag_exists(false) 97 , value_type() 98 , flag_index() 99 {} 100 }; 101 102 /// Find package and flag context 103 base::Result<PackageFlagContext> GetPackageFlagContext( 104 const std::string& package, const std::string& flag); 105 106 /// check if has package 107 base::Result<bool> HasPackage(const std::string& package); 108 109 /// check if has flag 110 base::Result<bool> HasFlag(const std::string& package, 111 const std::string& flag); 112 113 /// get persistent flag attribute 114 base::Result<uint8_t> GetFlagAttribute(const PackageFlagContext& context); 115 116 /// get server flag value 117 base::Result<std::string> GetServerFlagValue(const PackageFlagContext& context); 118 119 /// get local flag value 120 base::Result<std::string> GetLocalFlagValue(const PackageFlagContext& context); 121 122 /// get boot flag value 123 base::Result<std::string> GetBootFlagValue(const PackageFlagContext& context); 124 125 /// get default flag value 126 base::Result<std::string> GetDefaultFlagValue(const PackageFlagContext& context); 127 128 /// server flag override, update persistent flag value 129 base::Result<void> SetServerFlagValue(const PackageFlagContext& context, 130 const std::string& flag_value); 131 132 /// local flag override, update local flag override pb filee 133 base::Result<void> SetLocalFlagValue(const PackageFlagContext& context, 134 const std::string& flag_value); 135 136 /// set has server override in flag info 137 base::Result<void> SetHasServerOverride(const PackageFlagContext& context, 138 bool has_server_override); 139 140 /// set has local override in flag info 141 base::Result<void> SetHasLocalOverride(const PackageFlagContext& context, 142 bool has_local_override); 143 144 /// remove a single flag local override, return if removed 145 base::Result<bool> RemoveLocalFlagValue(const PackageFlagContext& context); 146 147 /// remove all local overrides 148 base::Result<void> RemoveAllLocalFlagValue(); 149 150 /// strcut for server flag value entries 151 struct ServerOverride { 152 std::string package_name; 153 std::string flag_name; 154 std::string flag_value; 155 }; 156 157 /// get all current server override 158 base::Result<std::vector<ServerOverride>> GetServerFlagValues(); 159 160 /// remove all persist storage files 161 base::Result<void> RemoveAllPersistFiles(); 162 163 /// create boot flag value and info files 164 base::Result<void> CreateBootStorageFiles(); 165 166 /// struct for flag snapshot 167 struct FlagSnapshot { 168 std::string package_name; 169 std::string flag_name; 170 std::string server_flag_value; 171 std::string local_flag_value; 172 std::string boot_flag_value; 173 std::string default_flag_value; 174 bool is_readwrite; 175 bool has_server_override; 176 bool has_local_override; 177 }; 178 179 /// list a flag 180 base::Result<StorageFiles::FlagSnapshot> ListFlag(const std::string& package, 181 const std::string& flag); 182 183 /// list flags 184 base::Result<std::vector<FlagSnapshot>> ListFlags( 185 const std::string& package = ""); 186 187 private: 188 189 /// get package map 190 base::Result<const aconfig_storage::MappedStorageFile*> GetPackageMap(); 191 192 /// get flag map 193 base::Result<const aconfig_storage::MappedStorageFile*> GetFlagMap(); 194 195 /// get default flag val 196 base::Result<const aconfig_storage::MappedStorageFile*> GetFlagVal(); 197 198 /// get boot flag val 199 base::Result<const aconfig_storage::MappedStorageFile*> GetBootFlagVal(); 200 201 /// get boot flag info 202 base::Result<const aconfig_storage::MappedStorageFile*> GetBootFlagInfo(); 203 204 /// get persist flag val 205 base::Result<const aconfig_storage::MutableMappedStorageFile*> GetPersistFlagVal(); 206 207 /// get persist flag info 208 base::Result<const aconfig_storage::MutableMappedStorageFile*> GetPersistFlagInfo(); 209 210 /// check if flag is read only 211 base::Result<bool> IsFlagReadOnly(const PackageFlagContext& context); 212 213 /// apply local update to boot flag value copy 214 base::Result<void> ApplyLocalOverrideToBootFlagValue(); 215 216 private: 217 218 /// container name 219 std::string container_; 220 221 // storage record for current container 222 StorageRecord storage_record_; 223 224 /// mapped package map file 225 std::unique_ptr<aconfig_storage::MappedStorageFile> package_map_; 226 227 /// mapped flag map file 228 std::unique_ptr<aconfig_storage::MappedStorageFile> flag_map_; 229 230 /// mapped default flag value file 231 std::unique_ptr<aconfig_storage::MappedStorageFile> flag_val_; 232 233 /// mapped boot flag value file 234 std::unique_ptr<aconfig_storage::MappedStorageFile> boot_flag_val_; 235 236 /// mapped boot flag info file 237 std::unique_ptr<aconfig_storage::MappedStorageFile> boot_flag_info_; 238 239 /// mapped persist flag value file 240 std::unique_ptr<aconfig_storage::MutableMappedStorageFile> persist_flag_val_; 241 242 /// mapped persist flag info file 243 std::unique_ptr<aconfig_storage::MutableMappedStorageFile> persist_flag_info_; 244 245 }; // class StorageFiles 246 247 } // namespace aconfigd 248 } // namespace android 249