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