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 <vector> 20 #include <string> 21 #include <memory> 22 #include <unordered_map> 23 24 #include <aconfigd.pb.h> 25 #include "storage_files.h" 26 27 namespace android { 28 namespace aconfigd { 29 /// Manager all storage files across different containers 30 class StorageFilesManager { 31 public: 32 33 /// constructor StorageFilesManager(const std::string & root_dir)34 StorageFilesManager(const std::string& root_dir) 35 : root_dir_(root_dir) 36 , all_storage_files_() 37 , package_to_container_() 38 {} 39 40 /// destructor 41 ~StorageFilesManager() = default; 42 43 /// no copy 44 StorageFilesManager(const StorageFilesManager&) = delete; 45 StorageFilesManager& operator=(const StorageFilesManager&) = delete; 46 47 /// move constructor and assignment StorageFilesManager(StorageFilesManager && rhs)48 StorageFilesManager(StorageFilesManager&& rhs) 49 : root_dir_(rhs.root_dir_) 50 , all_storage_files_() 51 , package_to_container_() { 52 if (this != &rhs) { 53 all_storage_files_ = std::move(rhs.all_storage_files_); 54 package_to_container_ = std::move(rhs.package_to_container_); 55 } 56 } 57 StorageFilesManager& operator=(StorageFilesManager&& rhs) = delete; 58 59 /// has container HasContainer(const std::string & container)60 bool HasContainer(const std::string& container) { 61 return all_storage_files_.count(container); 62 } 63 64 /// get mapped files for a container 65 base::Result<StorageFiles*> GetStorageFiles(const std::string& container); 66 67 /// create mapped files for a container 68 base::Result<StorageFiles*> AddNewStorageFiles(const std::string& container, 69 const std::string& package_map, 70 const std::string& flag_map, 71 const std::string& flag_val, 72 const std::string& flag_info); 73 74 /// restore storage files object from a storage record pb entry 75 base::Result<void> RestoreStorageFiles(const PersistStorageRecord& pb); 76 77 /// update existing storage files object with new storage file set 78 base::Result<void> UpdateStorageFiles(const std::string& container, 79 const std::string& package_map, 80 const std::string& flag_map, 81 const std::string& flag_val, 82 const std::string& flag_info); 83 84 /// add or update storage file set for a container 85 base::Result<bool> AddOrUpdateStorageFiles(const std::string& container, 86 const std::string& package_map, 87 const std::string& flag_map, 88 const std::string& flag_val, 89 const std::string& flag_info); 90 91 /// create boot copy 92 base::Result<void> CreateStorageBootCopy(const std::string& container); 93 94 /// reset all storage 95 base::Result<void> ResetAllStorage(); 96 97 /// get container name given flag package name 98 base::Result<std::string> GetContainer(const std::string& package); 99 100 /// get all storage records 101 std::vector<const StorageRecord*> GetAllStorageRecords(); 102 103 /// get all containers 104 std::vector<std::string> GetAllContainers(); 105 106 /// write to persist storage records pb file 107 base::Result<void> WritePersistStorageRecordsToFile( 108 const std::string& file_name); 109 110 /// apply flag override 111 base::Result<void> UpdateFlagValue( 112 const std::string& package_name, const std::string& flag_name, 113 const std::string& flag_value, 114 const StorageRequestMessage::FlagOverrideType overrideType = 115 StorageRequestMessage::SERVER_ON_REBOOT); 116 117 /// apply ota flags and return remaining ota flags 118 base::Result<std::vector<FlagOverride>> ApplyOTAFlagsForContainer( 119 const std::string& container, 120 const std::vector<FlagOverride>& ota_flags); 121 122 /// remove all local overrides 123 base::Result<void> RemoveAllLocalOverrides( 124 const StorageRequestMessage::RemoveOverrideType removeOverrideType); 125 126 /// remove a local override 127 base::Result<void> RemoveFlagLocalOverride( 128 const std::string& package, const std::string& flag, 129 const StorageRequestMessage::RemoveOverrideType removeOverrideType); 130 131 /// list a flag 132 base::Result<StorageFiles::FlagSnapshot> ListFlag(const std::string& package, 133 const std::string& flag); 134 135 /// list flags in a package 136 base::Result<std::vector<StorageFiles::FlagSnapshot>> ListFlagsInPackage( 137 const std::string& package); 138 139 /// list flags in a containers 140 base::Result<std::vector<StorageFiles::FlagSnapshot>> ListFlagsInContainer( 141 const std::string& container); 142 143 /// list all available flags 144 base::Result<std::vector<StorageFiles::FlagSnapshot>> ListAllAvailableFlags(); 145 146 private: 147 148 /// root directory to store storage files 149 const std::string root_dir_; 150 151 /// a hash table from container name to mapped files 152 std::unordered_map<std::string, std::unique_ptr<StorageFiles>> all_storage_files_; 153 154 /// a hash table from package name to container name 155 std::unordered_map<std::string, std::string> package_to_container_; 156 157 }; // class StorageFilesManager 158 159 } // namespace aconfigd 160 } // namespace android 161