1 /*
2 * Copyright (C) 2022 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 #include "Apex.h"
17
18 #include <android-base/format.h>
19 #include <android-base/logging.h>
20 #include <android-base/strings.h>
21
22 #include "com_android_apex.h"
23 #include "constants-private.h"
24
25 using android::base::StartsWith;
26
27 namespace android::vintf::apex {
28
isApexReady(PropertyFetcher * propertyFetcher)29 static bool isApexReady(PropertyFetcher* propertyFetcher) {
30 #ifdef LIBVINTF_TARGET
31 return propertyFetcher->getBoolProperty("apex.all.ready", false);
32 #else
33 // When running on host, it assumes that /apex is ready.
34 // Reason for still relying on PropertyFetcher API is for host-side tests.
35 return propertyFetcher->getBoolProperty("apex.all.ready", true);
36 #endif
37 }
38
GetVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error,std::function<bool (const std::string &)> filter)39 static status_t GetVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
40 std::vector<std::string>* dirs, std::string* error,
41 std::function<bool(const std::string&)> filter) {
42 std::string apexInfoFile = details::kApexInfoFile;
43 std::string apexDir = "/apex";
44 if (!isApexReady(propertyFetcher)) {
45 apexInfoFile = details::kBootstrapApexInfoFile;
46 apexDir = "/bootstrap-apex";
47 }
48
49 // Load apex-info-list
50 std::string xml;
51 auto status = fileSystem->fetch(apexInfoFile, &xml, error);
52 if (status == NAME_NOT_FOUND) {
53 if (error) {
54 error->clear();
55 }
56 return OK;
57 }
58 if (status != OK) return status;
59
60 auto apexInfoList = com::android::apex::parseApexInfoList(xml.c_str());
61 if (!apexInfoList.has_value()) {
62 if (error) {
63 *error = std::string("Not a valid XML: ") + apexInfoFile;
64 }
65 return UNKNOWN_ERROR;
66 }
67
68 // Get vendor apex vintf dirs
69 for (const auto& apexInfo : apexInfoList->getApexInfo()) {
70 // Skip non-active apexes
71 if (!apexInfo.getIsActive()) continue;
72
73 if (filter(apexInfo.getPartition())) {
74 dirs->push_back(fmt::format("{}/{}/" VINTF_SUB_DIR, apexDir, apexInfo.getModuleName()));
75 }
76 }
77 LOG(INFO) << "Loaded APEX Infos from " << apexInfoFile;
78 return OK;
79 }
80
GetModifiedTime(FileSystem * fileSystem,PropertyFetcher * propertyFetcher)81 std::optional<timespec> GetModifiedTime(FileSystem* fileSystem, PropertyFetcher* propertyFetcher) {
82 std::string apexInfoFile = details::kApexInfoFile;
83 if (!isApexReady(propertyFetcher)) {
84 apexInfoFile = details::kBootstrapApexInfoFile;
85 }
86
87 timespec mtime{};
88 std::string error;
89 status_t status = fileSystem->modifiedTime(apexInfoFile, &mtime, &error);
90 if (status == NAME_NOT_FOUND) {
91 return std::nullopt;
92 }
93 if (status != OK) {
94 LOG(ERROR) << error;
95 return std::nullopt;
96 }
97 return mtime;
98 }
99
GetDeviceVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error)100 status_t GetDeviceVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
101 std::vector<std::string>* dirs, std::string* error) {
102 return GetVintfDirs(fileSystem, propertyFetcher, dirs, error, [](const std::string& partition) {
103 return partition.compare("VENDOR") == 0 || partition.compare("ODM") == 0;
104 });
105 }
106
GetFrameworkVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error)107 status_t GetFrameworkVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
108 std::vector<std::string>* dirs, std::string* error) {
109 return GetVintfDirs(fileSystem, propertyFetcher, dirs, error, [](const std::string& partition) {
110 return partition.compare("SYSTEM") == 0 || partition.compare("SYSTEM_EXT") == 0 ||
111 partition.compare("PRODUCT") == 0;
112 });
113 }
114
115 } // namespace android::vintf::apex
116