• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
28 namespace vintf {
29 namespace details {
30 
DeviceVintfDirs(FileSystem * fileSystem,std::vector<std::string> * dirs,std::string * error)31 status_t Apex::DeviceVintfDirs(FileSystem* fileSystem, std::vector<std::string>* dirs,
32                                std::string* error) {
33     std::vector<std::string> vendor;
34     std::vector<std::string> odm;
35 
36     // Update cached mtime_
37     int64_t mtime;
38     auto status = fileSystem->modifiedTime(kApexInfoFile, &mtime, error);
39 
40     if (status != OK) {
41         switch (status) {
42             case NAME_NOT_FOUND:
43                 status = OK;
44                 break;
45             case -EACCES:
46                 // Don't error out on access errors, but log it
47                 LOG(WARNING) << "APEX Device VINTF Dirs: EACCES: "
48                              << (error ? *error : "(unknown error message)");
49                 status = OK;
50                 break;
51             default:
52                 break;
53         }
54 
55         if ((status == OK) && (error)) {
56             error->clear();
57         }
58 
59         return status;
60     }
61 
62     mtime_ = mtime;
63 
64     // Load apex-info-list
65     std::string xml;
66     status = fileSystem->fetch(kApexInfoFile, &xml, error);
67     if (status == NAME_NOT_FOUND) {
68         if (error) {
69             error->clear();
70         }
71         return OK;
72     }
73     if (status != OK) return status;
74 
75     auto apexInfoList = com::android::apex::parseApexInfoList(xml.c_str());
76     if (!apexInfoList.has_value()) {
77         if (error) {
78             *error = std::string("Not a valid XML ") + kApexInfoFile;
79         }
80         return UNKNOWN_ERROR;
81     }
82 
83     // Get vendor apex vintf dirs
84     for (const auto& apexInfo : apexInfoList->getApexInfo()) {
85         // Skip non-active apexes
86         if (!apexInfo.getIsActive()) continue;
87         // Skip if no preinstalled paths. This shouldn't happen but XML schema says it's optional.
88         if (!apexInfo.hasPreinstalledModulePath()) continue;
89 
90         const std::string& path = apexInfo.getPreinstalledModulePath();
91         if (StartsWith(path, "/vendor/apex/") || StartsWith(path, "/system/vendor/apex/")) {
92             dirs->push_back(fmt::format("/apex/{}/" VINTF_SUB_DIR, apexInfo.getModuleName()));
93         }
94     }
95     return OK;
96 }
97 
98 // Returns true when /apex/apex-info-list.xml is updated
HasUpdate(FileSystem * fileSystem) const99 bool Apex::HasUpdate(FileSystem* fileSystem) const {
100     int64_t mtime{};
101     std::string error;
102     status_t status = fileSystem->modifiedTime(kApexInfoFile, &mtime, &error);
103     if (status == NAME_NOT_FOUND) {
104         return false;
105     }
106     if (status != OK) {
107         LOG(ERROR) << error;
108         return false;
109     }
110     return mtime != mtime_;
111 }
112 
113 }  // namespace details
114 }  // namespace vintf
115 }  // namespace android
116