xref: /aosp_15_r20/system/libvintf/Apex.cpp (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
1*70a7ec85SAndroid Build Coastguard Worker /*
2*70a7ec85SAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*70a7ec85SAndroid Build Coastguard Worker  *
4*70a7ec85SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*70a7ec85SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*70a7ec85SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*70a7ec85SAndroid Build Coastguard Worker  *
8*70a7ec85SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*70a7ec85SAndroid Build Coastguard Worker  *
10*70a7ec85SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*70a7ec85SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*70a7ec85SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*70a7ec85SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*70a7ec85SAndroid Build Coastguard Worker  * limitations under the License.
15*70a7ec85SAndroid Build Coastguard Worker  */
16*70a7ec85SAndroid Build Coastguard Worker #include "Apex.h"
17*70a7ec85SAndroid Build Coastguard Worker 
18*70a7ec85SAndroid Build Coastguard Worker #include <android-base/format.h>
19*70a7ec85SAndroid Build Coastguard Worker #include <android-base/logging.h>
20*70a7ec85SAndroid Build Coastguard Worker #include <android-base/strings.h>
21*70a7ec85SAndroid Build Coastguard Worker 
22*70a7ec85SAndroid Build Coastguard Worker #include "com_android_apex.h"
23*70a7ec85SAndroid Build Coastguard Worker #include "constants-private.h"
24*70a7ec85SAndroid Build Coastguard Worker 
25*70a7ec85SAndroid Build Coastguard Worker using android::base::StartsWith;
26*70a7ec85SAndroid Build Coastguard Worker 
27*70a7ec85SAndroid Build Coastguard Worker namespace android::vintf::apex {
28*70a7ec85SAndroid Build Coastguard Worker 
isApexReady(PropertyFetcher * propertyFetcher)29*70a7ec85SAndroid Build Coastguard Worker static bool isApexReady(PropertyFetcher* propertyFetcher) {
30*70a7ec85SAndroid Build Coastguard Worker #ifdef LIBVINTF_TARGET
31*70a7ec85SAndroid Build Coastguard Worker     return propertyFetcher->getBoolProperty("apex.all.ready", false);
32*70a7ec85SAndroid Build Coastguard Worker #else
33*70a7ec85SAndroid Build Coastguard Worker     // When running on host, it assumes that /apex is ready.
34*70a7ec85SAndroid Build Coastguard Worker     // Reason for still relying on PropertyFetcher API is for host-side tests.
35*70a7ec85SAndroid Build Coastguard Worker     return propertyFetcher->getBoolProperty("apex.all.ready", true);
36*70a7ec85SAndroid Build Coastguard Worker #endif
37*70a7ec85SAndroid Build Coastguard Worker }
38*70a7ec85SAndroid Build Coastguard Worker 
GetVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error,std::function<bool (const std::string &)> filter)39*70a7ec85SAndroid Build Coastguard Worker static status_t GetVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
40*70a7ec85SAndroid Build Coastguard Worker                              std::vector<std::string>* dirs, std::string* error,
41*70a7ec85SAndroid Build Coastguard Worker                              std::function<bool(const std::string&)> filter) {
42*70a7ec85SAndroid Build Coastguard Worker     std::string apexInfoFile = details::kApexInfoFile;
43*70a7ec85SAndroid Build Coastguard Worker     std::string apexDir = "/apex";
44*70a7ec85SAndroid Build Coastguard Worker     if (!isApexReady(propertyFetcher)) {
45*70a7ec85SAndroid Build Coastguard Worker         apexInfoFile = details::kBootstrapApexInfoFile;
46*70a7ec85SAndroid Build Coastguard Worker         apexDir = "/bootstrap-apex";
47*70a7ec85SAndroid Build Coastguard Worker     }
48*70a7ec85SAndroid Build Coastguard Worker 
49*70a7ec85SAndroid Build Coastguard Worker     // Load apex-info-list
50*70a7ec85SAndroid Build Coastguard Worker     std::string xml;
51*70a7ec85SAndroid Build Coastguard Worker     auto status = fileSystem->fetch(apexInfoFile, &xml, error);
52*70a7ec85SAndroid Build Coastguard Worker     if (status == NAME_NOT_FOUND) {
53*70a7ec85SAndroid Build Coastguard Worker         if (error) {
54*70a7ec85SAndroid Build Coastguard Worker             error->clear();
55*70a7ec85SAndroid Build Coastguard Worker         }
56*70a7ec85SAndroid Build Coastguard Worker         return OK;
57*70a7ec85SAndroid Build Coastguard Worker     }
58*70a7ec85SAndroid Build Coastguard Worker     if (status != OK) return status;
59*70a7ec85SAndroid Build Coastguard Worker 
60*70a7ec85SAndroid Build Coastguard Worker     auto apexInfoList = com::android::apex::parseApexInfoList(xml.c_str());
61*70a7ec85SAndroid Build Coastguard Worker     if (!apexInfoList.has_value()) {
62*70a7ec85SAndroid Build Coastguard Worker         if (error) {
63*70a7ec85SAndroid Build Coastguard Worker             *error = std::string("Not a valid XML: ") + apexInfoFile;
64*70a7ec85SAndroid Build Coastguard Worker         }
65*70a7ec85SAndroid Build Coastguard Worker         return UNKNOWN_ERROR;
66*70a7ec85SAndroid Build Coastguard Worker     }
67*70a7ec85SAndroid Build Coastguard Worker 
68*70a7ec85SAndroid Build Coastguard Worker     // Get vendor apex vintf dirs
69*70a7ec85SAndroid Build Coastguard Worker     for (const auto& apexInfo : apexInfoList->getApexInfo()) {
70*70a7ec85SAndroid Build Coastguard Worker         // Skip non-active apexes
71*70a7ec85SAndroid Build Coastguard Worker         if (!apexInfo.getIsActive()) continue;
72*70a7ec85SAndroid Build Coastguard Worker 
73*70a7ec85SAndroid Build Coastguard Worker         if (filter(apexInfo.getPartition())) {
74*70a7ec85SAndroid Build Coastguard Worker             dirs->push_back(fmt::format("{}/{}/" VINTF_SUB_DIR, apexDir, apexInfo.getModuleName()));
75*70a7ec85SAndroid Build Coastguard Worker         }
76*70a7ec85SAndroid Build Coastguard Worker     }
77*70a7ec85SAndroid Build Coastguard Worker     LOG(INFO) << "Loaded APEX Infos from " << apexInfoFile;
78*70a7ec85SAndroid Build Coastguard Worker     return OK;
79*70a7ec85SAndroid Build Coastguard Worker }
80*70a7ec85SAndroid Build Coastguard Worker 
GetModifiedTime(FileSystem * fileSystem,PropertyFetcher * propertyFetcher)81*70a7ec85SAndroid Build Coastguard Worker std::optional<timespec> GetModifiedTime(FileSystem* fileSystem, PropertyFetcher* propertyFetcher) {
82*70a7ec85SAndroid Build Coastguard Worker     std::string apexInfoFile = details::kApexInfoFile;
83*70a7ec85SAndroid Build Coastguard Worker     if (!isApexReady(propertyFetcher)) {
84*70a7ec85SAndroid Build Coastguard Worker         apexInfoFile = details::kBootstrapApexInfoFile;
85*70a7ec85SAndroid Build Coastguard Worker     }
86*70a7ec85SAndroid Build Coastguard Worker 
87*70a7ec85SAndroid Build Coastguard Worker     timespec mtime{};
88*70a7ec85SAndroid Build Coastguard Worker     std::string error;
89*70a7ec85SAndroid Build Coastguard Worker     status_t status = fileSystem->modifiedTime(apexInfoFile, &mtime, &error);
90*70a7ec85SAndroid Build Coastguard Worker     if (status == NAME_NOT_FOUND) {
91*70a7ec85SAndroid Build Coastguard Worker         return std::nullopt;
92*70a7ec85SAndroid Build Coastguard Worker     }
93*70a7ec85SAndroid Build Coastguard Worker     if (status != OK) {
94*70a7ec85SAndroid Build Coastguard Worker         LOG(ERROR) << error;
95*70a7ec85SAndroid Build Coastguard Worker         return std::nullopt;
96*70a7ec85SAndroid Build Coastguard Worker     }
97*70a7ec85SAndroid Build Coastguard Worker     return mtime;
98*70a7ec85SAndroid Build Coastguard Worker }
99*70a7ec85SAndroid Build Coastguard Worker 
GetDeviceVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error)100*70a7ec85SAndroid Build Coastguard Worker status_t GetDeviceVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
101*70a7ec85SAndroid Build Coastguard Worker                             std::vector<std::string>* dirs, std::string* error) {
102*70a7ec85SAndroid Build Coastguard Worker     return GetVintfDirs(fileSystem, propertyFetcher, dirs, error, [](const std::string& partition) {
103*70a7ec85SAndroid Build Coastguard Worker         return partition.compare("VENDOR") == 0 || partition.compare("ODM") == 0;
104*70a7ec85SAndroid Build Coastguard Worker     });
105*70a7ec85SAndroid Build Coastguard Worker }
106*70a7ec85SAndroid Build Coastguard Worker 
GetFrameworkVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error)107*70a7ec85SAndroid Build Coastguard Worker status_t GetFrameworkVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
108*70a7ec85SAndroid Build Coastguard Worker                                std::vector<std::string>* dirs, std::string* error) {
109*70a7ec85SAndroid Build Coastguard Worker     return GetVintfDirs(fileSystem, propertyFetcher, dirs, error, [](const std::string& partition) {
110*70a7ec85SAndroid Build Coastguard Worker         return partition.compare("SYSTEM") == 0 || partition.compare("SYSTEM_EXT") == 0 ||
111*70a7ec85SAndroid Build Coastguard Worker                partition.compare("PRODUCT") == 0;
112*70a7ec85SAndroid Build Coastguard Worker     });
113*70a7ec85SAndroid Build Coastguard Worker }
114*70a7ec85SAndroid Build Coastguard Worker 
115*70a7ec85SAndroid Build Coastguard Worker }  // namespace android::vintf::apex
116