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         // Skip if no preinstalled paths. This shouldn't happen but XML schema says it's optional.
73         if (!apexInfo.hasPreinstalledModulePath()) continue;
74 
75         const std::string& path = apexInfo.getPreinstalledModulePath();
76         if (filter(path)) {
77             dirs->push_back(fmt::format("{}/{}/" VINTF_SUB_DIR, apexDir, apexInfo.getModuleName()));
78         }
79     }
80     LOG(INFO) << "Loaded APEX Infos from " << apexInfoFile;
81     return OK;
82 }
83 
GetModifiedTime(FileSystem * fileSystem,PropertyFetcher * propertyFetcher)84 std::optional<timespec> GetModifiedTime(FileSystem* fileSystem, PropertyFetcher* propertyFetcher) {
85     std::string apexInfoFile = details::kApexInfoFile;
86     if (!isApexReady(propertyFetcher)) {
87         apexInfoFile = details::kBootstrapApexInfoFile;
88     }
89 
90     timespec mtime{};
91     std::string error;
92     status_t status = fileSystem->modifiedTime(apexInfoFile, &mtime, &error);
93     if (status == NAME_NOT_FOUND) {
94         return std::nullopt;
95     }
96     if (status != OK) {
97         LOG(ERROR) << error;
98         return std::nullopt;
99     }
100     return mtime;
101 }
102 
GetDeviceVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error)103 status_t GetDeviceVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
104                             std::vector<std::string>* dirs, std::string* error) {
105     return GetVintfDirs(fileSystem, propertyFetcher, dirs, error, [](const std::string& path) {
106         return StartsWith(path, "/vendor/apex/") || StartsWith(path, "/system/vendor/apex/");
107     });
108 }
109 
GetFrameworkVintfDirs(FileSystem * fileSystem,PropertyFetcher * propertyFetcher,std::vector<std::string> * dirs,std::string * error)110 status_t GetFrameworkVintfDirs(FileSystem* fileSystem, PropertyFetcher* propertyFetcher,
111                                std::vector<std::string>* dirs, std::string* error) {
112     return GetVintfDirs(fileSystem, propertyFetcher, dirs, error, [](const std::string& path) {
113         return StartsWith(path, "/system/apex/") || StartsWith(path, "/system_ext/apex/") ||
114                StartsWith(path, "/system/system_ext/apex/") || StartsWith(path, "/product/apex/") ||
115                StartsWith(path, "/system/product/apex/");
116     });
117 }
118 
119 }  // namespace android::vintf::apex
120